Public: anyone can read this; the crew can change it.

The client list shows when each forge link was last opened, or that it has not been #13218

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/13137-the-client-list-shows-when-each-forge-li → main 4 commits fork 3 threads · 3 unresolved

  • Merged. mason/13137-the-client-list-shows-when-each-forge-li was merged into main as 4da3d71.

Review

Sign in to review this change.
4 files changed +72 −16
src/contract/openapi/routes/getApiV1Forge.ts +1 −1
@@ -1,3 +1,3 @@
11 import type { Route } from "../shared.ts";
22
3 export const route: Route = { method: "get", path: "/api/v1/forge", summary: "A spawned forge's word about itself: { claimed, lastActivityAt, link? }; lastActivityAt, link ({ state, revokedAt, usedAt }) are null or absent, as is madeFrom ({ template, at }), unless the parent's secret is sent", auth: false, role: "anonymous", refusals: { not_found: "404: this forge was not spawned" } };
3 export const route: Route = { method: "get", path: "/api/v1/forge", summary: "A spawned forge's word about itself: { claimed, lastActivityAt, link? }; lastActivityAt, link ({ state, revokedAt, usedAt, openedAt, lastOpenedAt }) are null or absent, as is madeFrom ({ template, at }), unless the parent's secret is sent", auth: false, role: "anonymous", refusals: { not_found: "404: this forge was not spawned" } };
src/spawn/claim.ts +16 −8
@@ -56,6 +56,8 @@ interface Stored {
5656 readonly r?: string;
5757 /** When a browser first opened the claim page, ISO; absent until one did. */
5858 readonly o?: string;
59 /** When a browser last opened the claim page, ISO; absent until one did. */
60 readonly l?: string;
5961 }
6062
6163 export type ClaimState =
@@ -388,8 +390,8 @@ export function becomeOrg(
388390 db.query("DELETE FROM session WHERE user_id = ?1").run(orgId);
389391 if (input.credential !== undefined && !addPasskey(db, orgId, "Passkey", input.credential).ok) throw new PasskeyRefused();
390392 // The one-time claim link is the proof, as it is for an account made beside the organisation
391 // (`createUser` with `trusted`). An organisation needed no proof; the person it becomes does, or
392 // the crew answers their first ask with "Verify your email first."
393 // (`createUser` with `trusted`). An organisation needed no proof; the person it becomes does,
394 // or the crew answers their first ask with "Verify your email first."
393395 markVerified(db, orgId, new Date().toISOString());
394396 return true;
395397 });
@@ -431,11 +433,15 @@ export interface LinkInfo {
431433 readonly usedAt?: string | null;
432434 /** When a person's browser first opened the claim page, ISO; null while none did. */
433435 readonly openedAt?: string | null;
436 /** When a person's browser last opened the claim page, ISO; null while none did. */
437 readonly lastOpenedAt?: string | null;
434438 /** When the link stops working, ISO. */
435439 readonly expiresAt?: string;
436440 }
437441
438 /** True while an ended link can still be brought back: revoked in the last minute, in date, unspent. */
442 /**
443 * True while an ended link can still be brought back: revoked in the last minute, in date, unspent.
444 */
439445 export const undoable = (link: LinkInfo, now = Date.now()): boolean =>
440446 link.state === "ended" && link.revokedAt !== null && now - Date.parse(link.revokedAt) <= REVOKE_UNDO_MS;
441447
@@ -460,7 +466,7 @@ const stateOf = (s: Stored, now: number): LinkState =>
460466 /** The claim link in this forge's own database; null when it has none. */
461467 export function linkOf(db: Database, now = Date.now()): LinkInfo | null {
462468 const found = readStored(db);
463 return found === undefined ? null : { state: stateOf(found.stored, now), revokedAt: found.stored.r ?? null, usedAt: found.stored.u, openedAt: found.stored.o ?? null, expiresAt: found.stored.e };
469 return found === undefined ? null : { state: stateOf(found.stored, now), revokedAt: found.stored.r ?? null, usedAt: found.stored.u, openedAt: found.stored.o ?? null, lastOpenedAt: found.stored.l ?? found.stored.o ?? null, expiresAt: found.stored.e };
464470 }
465471
466472 /** Replace the stored link only if nobody changed it since it was read. */
@@ -487,14 +493,16 @@ export function isBrowserOpen(req: Request): boolean {
487493 }
488494
489495 /**
490 * Remember the first time the open link's page was opened; false when it was not open or
491 * already noted.
496 * Remember that the open link's page was opened: the first open sets the first and the last
497 * time, each later one moves the last time only. One write per open; no address, no agent
498 * string. False when the link was not open.
492499 */
493500 export function noteOpened(db: Database, now = Date.now()): boolean {
494501 const row = db.query<{ user_id: string }, [string]>("SELECT user_id FROM user_setting WHERE key = ?1").get(KEY);
495502 const found = readStored(db);
496 if (row === null || found === undefined || found.stored.o !== undefined || stateOf(found.stored, now) !== "open") return false;
497 return swap(db, row.user_id, found, { ...found.stored, o: new Date(now).toISOString() });
503 if (row === null || found === undefined || stateOf(found.stored, now) !== "open") return false;
504 const at = new Date(now).toISOString();
505 return swap(db, row.user_id, found, { ...found.stored, o: found.stored.o ?? at, l: at });
498506 }
499507
500508 /** Take back an open link: from now it answers "ended". False when it was not open. */
src/spawn/clients.ts +11 −5
@@ -223,12 +223,12 @@ export async function forgeState(
223223 const LINK_STATES: readonly LinkState[] = ["open", "used", "ended"];
224224
225225 /** The link a child reported, or null when it said nothing usable. */
226 function linkOfBody(v: unknown): LinkInfo | null {
226 export function linkOfBody(v: unknown): LinkInfo | null {
227227 if (typeof v !== "object" || v === null) return null;
228228 const { state, revokedAt } = v as Record<string, unknown>;
229229 if (!LINK_STATES.includes(state as LinkState)) return null;
230230 const iso = (x: unknown): string | null => (typeof x === "string" && !Number.isNaN(Date.parse(x)) ? x : null);
231 return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso((v as Record<string, unknown>)["usedAt"]), openedAt: iso((v as Record<string, unknown>)["openedAt"]) };
231 return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso((v as Record<string, unknown>)["usedAt"]), openedAt: iso((v as Record<string, unknown>)["openedAt"]), lastOpenedAt: iso((v as Record<string, unknown>)["lastOpenedAt"]) };
232232 }
233233
234234 async function probe(app: App, child: Child): Promise<ForgeState | undefined> {
@@ -598,8 +598,13 @@ export interface Seen {
598598 readonly sentAt: string | null;
599599 /** True once a browser opened the page or the link was spent. */
600600 readonly opened: boolean;
601 /** ISO of the first browser open; null while unopened, or spent before opens were noted. */
601 /**
602 * ISO of the last browser open (the first when it was opened once); null while unopened, or spent
603 * before opens were noted.
604 */
602605 readonly openedAt: string | null;
606 /** ISO of the first browser open; null while unopened, or spent before opens were noted. */
607 readonly firstOpenedAt: string | null;

Non-blocking: firstOpenedAt has no reader in this change. It is dead until the stage logic uses it; drop it or use it.

603608 /** ISO of a person's last action there; null until they signed in. */
604609 readonly lastSeenAt: string | null;
605610 }
@@ -607,8 +612,9 @@ export interface Seen {
607612 /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */
608613 export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "link" | "lastActivityAt">): Seen | null {
609614 if (!r.made || r.claimed === null || r.link === null) return null;
610 const openedAt = r.link.openedAt ?? null;
611 return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null };
615 const firstOpenedAt = r.link.openedAt ?? null;
616 const openedAt = r.link.lastOpenedAt ?? firstOpenedAt;
617 return { sentAt: r.sentAt, firstOpenedAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null };
612618 }
613619
614620 /** The one line as parts to join with " · ": the words, and the instant to say beside them. */

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs