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

A client's invite link page names who sent it and what the forge holds, in plain words #14885

Merged opened this

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

Merged

scout/forge:scout/14856-a-client-s-invite-link-page-names-who-se → main 6 commits fork 2 threads · 2 unresolved

  • Merged. scout/14856-a-client-s-invite-link-page-names-who-se was merged into main as 6d0d536.

Review

Sign in to review this change.
5 files changed +162 −5
docs/features/claim-page-words.md +7 −1
@@ -8,6 +8,12 @@ A new forge's claim link (`/claim`, and `/welcome` for the person who started it
88 server-rendered page says the forge's name once, in the sentence about who made it. Below the form it shows the crew
99 line and three next steps.
1010
11 Under it, when the forge holds public apps, one line says "It comes with ..." and names them, six at most, then "N
12 more" for the rest of the public ones. Private apps are neither named nor counted, so a stranger holding the bare link
13 learns nothing about them; a forge with no public app shows no line. A used link says "You already used this link" and
14 "ask <sender> to send a new one"; an expired one says "Ask <sender> to send you a new one". The sender there is the display
15 name when one is set, else the username; the first page says "<username> made ...". Never the email.
16
1117 The crew line is the forge's latest public agent runs, each as what, who on which repository, and a state in plain
1218 words ("working now", "waiting to start", "finished"). With no public run it says "Your crew is starting, and its first
1319 public work shows here."
@@ -21,4 +27,4 @@ Try it: `curl /claim` on a new forge, or open the link in a browser.
2127 The terminal (both doors): the crew screen (`src/tui/screens/crew_now.ts`) shows the same runs and the same three steps.
2228 Both doors render from `NEXT_STEPS` in `src/core/crew_now.ts`, so a change to a step's words changes both.
2329
24 Pinned by `test/claim-page-words.test.ts` and `test/link-crew-now.test.ts`.
30 Pinned by `test/invite-words.test.ts`, `test/claim-page-words.test.ts` and `test/link-crew-now.test.ts`.
src/web/pages/claim.ts +26 −1
@@ -22,6 +22,8 @@
2222 * so before anyone submits, and where to remove it.
2323 */
2424
25 import type { Database } from "bun:sqlite";
26 import { listReposOwnedBy } from "../../core/repos.ts";
2527 import { USERNAME_PATTERN } from "../username_pattern.ts";
2628 import { crewNow, NEXT_STEPS } from "../../core/crew_now.ts";
2729 import { crewNowHtml } from "../crew_now.ts";
@@ -111,6 +113,25 @@ function sender(user: User | undefined): string | undefined {
111113 return user === undefined ? undefined : user.displayName || user.username;
112114 }
113115
116 /**
117 * What the forge holds, in plain words: public apps by name (six at most, then "N more" of the
118 * public ones), private apps not even counted, so a stranger who opens the bare page learns
119 * neither a private name nor that any exist. Empty when it holds no public app.
120 */
121 export function holdsWords(db: Database, orgId: string): string {
122 const names = listReposOwnedBy(db, orgId)
123 .filter((r) => r.visibility === "public")
124 .map((r) => r.name)
125 .sort();
126 const parts = names.slice(0, 6);
outdated

Over 100 columns; wrap. Also see finding 4: hidden counts private apps into '1 more'.

127 const more = names.length - parts.length;
128 if (more > 0) parts.push(`${more} more`);
129 if (parts.length === 0) return "";
130 const list =
131 parts.length === 1 ? parts[0]! : `${parts.slice(0, -1).join(", ")} and ${parts.at(-1)}`;
132 return `It comes with ${list}.`;
133 }
134
114135 /** What a revoked link says to whoever opens it: no sender, no forge detail (#14518). */
115136 export const LINK_TURNED_OFF = "This link was turned off. Ask the person who sent it for a new one.";
116137
@@ -140,7 +161,9 @@ function dead(
140161 // (A POST that tried to claim with it again stays 410: it did not succeed.)
141162 status: ctx.request.method === "GET" ? 200 : 410,
142163 title: "This forge is already yours",
143 text: "You already used this link to make your account. Sign in to carry on.",
164 text: `You already used this link to make your account. Sign in to carry on.${
165 own ? "" : ` If that was not you, ask ${ask} to send a new one.`
166 }`,
144167 },
145168 expired: {
146169 status: 410,
@@ -258,10 +281,12 @@ function claimForm(
258281 ${crewNowHtml(crewNow(ctx.app.db))}
259282 ${nextSteps(true)}
260283 </div></div>`;
284 const holds = holdsWords(ctx.app.db, spawned.org.id);
261285 return html`<div class="shell"><div class="narrow">
outdated

holdsWords is called twice here (a second db query). Compute once into a const above the template.

262286 ${pageHead("This forge is yours")}
263287 <section class="settings-block">
264288 <p class="mb" id="forge-name">${spawner === undefined ? `${spawned.org.username} was made` : `${spawner.username} made ${spawned.org.username}`} for you. Your account will be its admin, so it can change everything here.</p>
289 ${holds === "" ? null : html`<p class="mb" id="claim-holds">${holds}</p>`}
265290 ${expiresAt === undefined
266291 ? null
267292 : html`<p class="mb" id="claim-expiry">This link works until
test/claim-page-words.test.ts +1 −1
@@ -123,7 +123,7 @@ describe("the claim page in plain words", () => {
123123 const text = await res.text();
124124 expect(text).toContain("This forge is already yours");
125125 expect(text).toContain('href="/signin"');
126 expect(text).not.toContain("alice");
126 expect(text).toContain("ask alice to send a new one");
127127 expect(text).not.toMatch(/token|\b4\d\d\b/i);
128128 });
129129
test/invite-words.test.ts added +126
@@ -0,0 +1,126 @@
1 /**
2 * A client's link page in plain words (#14856): who made the forge and what it holds before
3 * signing in, and a used or expired link that says so kindly and names the sender as the page does:
4 * the display name when set, else the username.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { childDirOf } from "../src/actions/spawn.ts";
11 import { createRepo } from "../src/actions/repos.ts";
12 import { createApp, type App } from "../src/app.ts";
13 import { createToken } from "../src/core/tokens.ts";
14 import { createUser, getUserByName } from "../src/core/users.ts";
15 import { serve, type Servers } from "../src/http/server.ts";
16 import { holdsWords } from "../src/web/pages/claim.ts";
17 import { firstBoot } from "../src/spawn/first-boot.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19
20 quietLogs();
21
22 describe("the invite link page in plain words", () => {
23 let dir = "";
24 let parent: App;
25 let parentServers: Servers;
26 let child: App;
27 let childServers: Servers;
28
29 const get = async () => {
30 const res = await fetch(`${childServers.url}/claim`);
31 return { status: res.status, text: await res.text() };
32 };
33 const setStored = (patch: Record<string, unknown>) => {
34 const key = "spawn_claim";
35 const stored = JSON.parse(child.db.query<{ value: string }, [string]>("SELECT value FROM user_setting WHERE key = ?1").get(key)!.value);
36 child.db.query("UPDATE user_setting SET value = ?2 WHERE key = ?1").run(key, JSON.stringify(Object.assign(stored, patch)));
37 };
38 const noJargon = (text: string) => {
39 const body = text.replace(/<(script|style)[\s\S]*?<\/\1>/g, "").replace(/<[^>]+>/g, " ");
40 expect(body).not.toMatch(/\b(fork|pull|token)\b/i);
41 expect(body).not.toContain("[email protected]");
42 };
43
44 beforeAll(async () => {
45 dir = await mkdtemp(join(tmpdir(), "forge-invite-words-"));
46 parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
47 const alice = await createUser(parent.db, {
48 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
49 password: "a-long-claim-passphrase", isAdmin: true,
50 });
51 if (!alice.ok) throw new Error("user");
52 const rw = createToken(parent.db, alice.value.id, "rw", "rw").token;
53 parentServers = await serve(parent);
54 const made = await fetch(`${parentServers.url}/api/v1/forges`, {
55 method: "POST",
56 headers: { authorization: `Bearer ${rw}`, "content-type": "application/json" },
57 body: JSON.stringify({ name: "maria", apps: [], dryRun: false }),
58 });
59 if (made.status !== 201) throw new Error(`spawn ${made.status}`);
60 const body = (await made.json()) as { bodyId: string };
61 child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 });
62 await firstBoot(child);
63 const org = getUserByName(child.db, "maria");
64 if (!org) throw new Error("org");
65 for (const [name, visibility] of [["notes", "public"], ["recipes", "public"], ["secret-plans", "private"]] as const) {
66 const r = await createRepo(child, org, { name, visibility });
67 if (!r.ok) throw new Error(String(r.error));
68 }
69 childServers = await serve(child);
70 }, 60_000);
71
72 afterAll(async () => {
73 childServers.stop();
74 child.close();
75 parentServers.stop();
76 parent.close();
77 await rm(dir, { recursive: true, force: true });
78 });
79
80 test("a valid link says who made it and names the public apps, never a private one, a count of them or an email", async () => {
81 const { status, text } = await get();
82 expect(status).toBe(200);
83 expect(text).toContain("alice made maria for you");
84 expect(text).toMatch(/It comes with notes and recipes\./);
85 expect(text).not.toContain("secret-plans");
86 noJargon(text);
87 });
88
89 test("a used link says so kindly and offers the sender by username when no display name is set", async () => {
90 setStored({ e: new Date(Date.now() + 3_600_000).toISOString(), u: new Date().toISOString() });
91 const { status, text } = await get();
92 expect(status).toBe(200);
93 expect(text).toContain("You already used this link");
94 expect(text).toContain("ask alice to send a new one");
95 noJargon(text);
96 });
97
98 test("with a display name set, the used link shows it and the first page keeps the username; the email stays out", async () => {
99 setStored({ e: new Date(Date.now() + 3_600_000).toISOString(), u: new Date().toISOString() });
100 child.db.query("UPDATE user SET display_name = 'Alice Archer' WHERE username = 'alice'").run();
101 const used = await get();
102 expect(used.text).toContain("ask Alice Archer to send a new one");
103 expect(used.text).not.toContain("[email protected]");
104 setStored({ e: new Date(Date.now() + 3_600_000).toISOString(), u: null });
105 const valid = await get();
106 expect(valid.text).toContain("alice made maria for you");
107 expect(valid.text).not.toContain("[email protected]");
108 noJargon(valid.text);
109 child.db.query("UPDATE user SET display_name = '' WHERE username = 'alice'").run();
110 });
111
112 test("more than six public apps name six and count the rest; private ones add nothing", async () => {
113 const org = getUserByName(child.db, "maria")!;
114 for (const n of ["a1", "a2", "a3", "a4", "a5"]) await createRepo(child, org, { name: n, visibility: "public" });
115 expect(holdsWords(child.db, org.id)).toBe("It comes with a1, a2, a3, a4, a5, notes and 1 more.");
116 });
117
118 test("an expired link says so kindly and offers the sender by username when no display name is set", async () => {
119 setStored({ e: new Date(Date.now() - 3_600_000).toISOString(), u: null });
120 const { status, text } = await get();
121 expect(status).toBe(410);
122 expect(text).toContain("This link has expired");
123 expect(text).toContain("Ask alice to send you a new one");
124 noJargon(text);
125 });
126 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs