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

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

Merged opened this

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

Merged

revert-13218 → main 1 commit

  • Merged. revert-13218 was merged into main as 74912c5.

Review

Sign in to review this change.
4 files changed +16 −72
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, 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" } };
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" } };
src/spawn/claim.ts +8 −16
@@ -56,8 +56,6 @@ 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;
6159 }
6260
6361 export type ClaimState =
@@ -390,8 +388,8 @@ export function becomeOrg(
390388 db.query("DELETE FROM session WHERE user_id = ?1").run(orgId);
391389 if (input.credential !== undefined && !addPasskey(db, orgId, "Passkey", input.credential).ok) throw new PasskeyRefused();
392390 // The one-time claim link is the proof, as it is for an account made beside the organisation
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."
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."
395393 markVerified(db, orgId, new Date().toISOString());
396394 return true;
397395 });
@@ -433,15 +431,11 @@ export interface LinkInfo {
433431 readonly usedAt?: string | null;
434432 /** When a person's browser first opened the claim page, ISO; null while none did. */
435433 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;
438434 /** When the link stops working, ISO. */
439435 readonly expiresAt?: string;
440436 }
441437
442 /**
443 * True while an ended link can still be brought back: revoked in the last minute, in date, unspent.
444 */
438 /** True while an ended link can still be brought back: revoked in the last minute, in date, unspent. */
445439 export const undoable = (link: LinkInfo, now = Date.now()): boolean =>
446440 link.state === "ended" && link.revokedAt !== null && now - Date.parse(link.revokedAt) <= REVOKE_UNDO_MS;
447441
@@ -466,7 +460,7 @@ const stateOf = (s: Stored, now: number): LinkState =>
466460 /** The claim link in this forge's own database; null when it has none. */
467461 export function linkOf(db: Database, now = Date.now()): LinkInfo | null {
468462 const found = readStored(db);
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 };
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 };
470464 }
471465
472466 /** Replace the stored link only if nobody changed it since it was read. */
@@ -493,16 +487,14 @@ export function isBrowserOpen(req: Request): boolean {
493487 }
494488
495489 /**
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.
490 * Remember the first time the open link's page was opened; false when it was not open or
491 * already noted.
499492 */
500493 export function noteOpened(db: Database, now = Date.now()): boolean {
501494 const row = db.query<{ user_id: string }, [string]>("SELECT user_id FROM user_setting WHERE key = ?1").get(KEY);
502495 const found = readStored(db);
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 });
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() });
506498 }
507499
508500 /** Take back an open link: from now it answers "ended". False when it was not open. */
src/spawn/clients.ts +5 −11
@@ -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 export function linkOfBody(v: unknown): LinkInfo | null {
226 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"]), lastOpenedAt: iso((v as Record<string, unknown>)["lastOpenedAt"]) };
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"]) };
232232 }
233233
234234 async function probe(app: App, child: Child): Promise<ForgeState | undefined> {
@@ -598,13 +598,8 @@ 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 /**
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 */
605 readonly openedAt: string | null;
606601 /** ISO of the first browser open; null while unopened, or spent before opens were noted. */
607 readonly firstOpenedAt: string | null;
602 readonly openedAt: string | null;
608603 /** ISO of a person's last action there; null until they signed in. */
609604 readonly lastSeenAt: string | null;
610605 }
@@ -612,9 +607,8 @@ export interface Seen {
612607 /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */
613608 export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "link" | "lastActivityAt">): Seen | null {
614609 if (!r.made || r.claimed === null || r.link === null) return 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 };
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 };
618612 }
619613
620614 /** 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