More at home on the desktop
Freddy's colleagues use the desktop app now, and they keep asking for things a browser tab can't do: edit the slides in PowerPoint without downloading and uploading them again, see a change at once, glance at a note without leaving the list, follow Ada through the notes on a call. Each is a few lines.
The attachment, in its own app
Chapter 12 put a PDF of the slides on a team note. Grete wants to fix a typo in the
Word document instead, in Word. With the system feature beside
backend, a command opens the file in the user's own app for it and
sends it back each time they save:
#[command(can = "notes.update")]
async fn attachments_edit(ctx: Ctx, id: i64) -> elyra::Result<Editing> {
Ok(ctx.get::<Backend>()
.edit(format!("/api/attachments/{id}/file")) // downloaded, opened in its app
.only(["docx", "xlsx", "pptx", "pdf"])
.open()
.await?)
}
The file is downloaded into a folder of the app's own and opened with the system's default app, so Word's title bar shows its real name. Every save is noticed, sent once the file is still, and only if its content changed. Each save is checked against the version the download had: if Ada changed the attachment on the web meanwhile, Laravel answers 412 and Grete gets a choice, keep mine or take theirs, which downloads Ada's version beside Grete's file, never over a file Word may have open. Without a connection only the last save waits, and signing out is refused while it does, as for chapter 14's outbox. Programs, shortcuts and web pages are never opened.
On the Laravel side the GET answers with the file and its version,
and the PUT checks it with a trait from
elyracode/elyra-backend:
use Elyra\Backend\Concerns\ChecksUnmodifiedSince;
class Attachment extends Model
{
use ChecksUnmodifiedSince;
}
Route::get('/attachments/{attachment}/file', fn (Attachment $attachment) =>
Storage::download($attachment->path, $attachment->name)
->setLastModified($attachment->updated_at));
Route::put('/attachments/{attachment}/file', function (Request $request, Attachment $attachment) {
abort_if($attachment->changedSince($request), 412, 'Someone changed it meanwhile.');
$request->validate(['file' => ['required', 'file', 'max:51200']]);
$attachment->replaceFile($request->file('file'));
return response()->json($attachment)->setLastModified($attachment->updated_at);
});
The runtime's editing store lists what is being edited, with its
state, saved, waiting or conflict, and the page acts on one by its id, never a
path: editing.keepMine(id), editing.takeTheirs(id),
editing.finish(id).
Shown at once
Pinning a note takes a round trip to Laravel before the list moves. Marked
.optimistic(), a write to a record shows in every window the moment
the command sends it:
#[command(can = "notes.update")]
async fn team_notes_update(ctx: Ctx, id: i64, input: TeamNoteInput) -> elyra::Result<TeamNote> {
Ok(ctx.get::<Backend>()
.put(format!("/api/notes/{id}"))
.body(&input)
.optimistic_with(&input)
.queue_offline()
.resource()
.await?)
}
The live queries that read the note or its lists re-run from chapter 14's offline
copy with the change applied, without asking the server, so it needs
.offline(). It stays applied while the write is on its way or waits in
the outbox, across a restart, and is taken back if Laravel says no: a 422, a 403, a
409 from someone else's change. Only fields the note already has change, and the
copy itself stays what the server said. It is for a PUT,
PATCH or DELETE of a record; a new note isn't shown before
the server says where it goes in a sorted list. Chapter 12's
make:resource --backend writes it on the update and delete commands
since 0.50. And $pending says which notes have a change not sent yet:
<Table page={notes} bind:query columns={["title"]} pending={$pending.notes} />
Quick Look
In Finder, Space on a file shows it without opening it. The generated notes list
does the same: its <Table> has a preview, so Space
on a row shows the note in a window beside the app's, ↓ and
↑ move it along while the list keeps the keyboard, and Space or Esc
closes it. Enter still opens the note.
<Table page={notes} bind:query columns={["title"]}
onrowclick={(n) => navigate(`/notes/${n.id}`)}
preview={(n) => `/notes/${n.id}`} />
The window is the app's own page at that route, so the note's detail view from
chapter 6 is what it shows. And it only reads: it is a window with
WindowConfig::read_only(), where only the app's live commands run, as
a live query's re-run does, so a write in one fails, and can says no to
what only writers need, so the detail view's Edit and Delete buttons hide. The
rest of what could change something is refused there too: the store's
set, the clipboard, dialogs, signing out, the assistant's key, the
app's language. A window of Freddy's own can be read-only the same way.
Following Ada
On a call, Ada goes through the notes that need tidying, and Freddy would rather
see them than have her share her screen. Chapter 13's presence channels do it: a
channel for everyone in the app, and follow on its avatars.
Backend::new("https://notes.example.com")
.broadcasting(Broadcasting::pusher("app-key"))
.presence(["office", "notes.*"])
// routes/channels.php
Broadcast::channel('office', fn ($user) => ['id' => $user->id, 'name' => $user->name]);
<Presence channel="office" follow />
Freddy clicks Ada's avatar and Follow. Ada is asked first, in her
focused window, and nothing of where she is is sent before she allows it. Then only
her route travels, as a whisper on the channel, and Freddy's window goes there as
Freddy, with his own token and abilities, so a note he may not see stays hidden
from him. It ends when either presses Stop, Freddy goes somewhere himself, or
either leaves the channel. following.share(fn) keeps routes back,
the settings, say.
The two halves agree
One morning the notes list fails to decode for everyone: a colleague renamed
title to name in the API Resource, and the desktop app's
TeamNote still says title. The two halves drift apart
without anyone noticing, so Freddy checks them against each other.
elyracode/elyra-backend describes the API, sampled as a user from the
test database: its routes, each FormRequest's rules, the abilities,
and the field names and types each read answers with, never a value:
# in the Laravel app
php artisan elyra:contract --as=ada@example.com > elyra-contract.json
# in the desktop app
rata check --backend elyra-contract.json
error team_notes_index decodes `title`, which GET /api/notes doesn't send; it sends `name`.
Rename the field to `name`, or #[serde(rename = "name")] it.
It finds a route a command calls that isn't there, a field its type needs that
isn't sent or can be null, an input field the rules require, and an
ability no user has. In elyra.toml, [check] backend
names the Laravel app's folder and backend_as the user, so a plain
rata check, the one chapter 11 runs and chapter 18 puts in CI,
includes it.
The guides are the files, shown-at-once and following sections of
A Laravel backend, Quick Look in
Windows, and rata check in
the CLI.
What you learned
Backend::edit(..)opens a file in the user's own app and sends each save back, checked against its version, never over someone else's change..optimistic()shows a write in every window before the server answers, and takes it back if Laravel says no.<Table preview>shows a row in Quick Look, a window that only reads.<Presence follow>follows a colleague who allows it, as yourself, by their route alone.elyra:contractandrata check --backendcatch the two halves drifting apart.