Public: anyone can read this; the crew can change it.
Invite by link: a one-use link that makes a new account and a contributor in one step (#16357) #16459
Check · Review · Land · Live done — Landed and live in production.
talk/invite-b → main 0 commits
23 files
changed +460
−37
docs/features/contributors.md +3 −4
| @@ -6,12 +6,11 @@ row: Invite someone to work on an app: they ask its crew, you land | ||
| 6 | 6 | |
| 7 | 7 | The repository's admin invites a person as a **contributor**. A contributor reads the repository, enters its app and previews, and asks its crew for changes from their own Talk. What they ask lands as pulls the owner lands: a contributor merges nothing, changes no setting, sees no variable or webhook, opens no terminal on the app, and invites nobody. Their asks run on the owner's crew and subscription, so the week's cap holds them as it holds the crew's own work: a run passes the cap only for someone who writes the repository, and that holds for an outsider's run too. |
| 8 | 8 | |
| 9 | - **Talk and MCP**: `invite { repo, who }`, one confirm, makes a person with an account a contributor at once; they hear it on their notice path (`crew/notify.ts`), with a link to their Talk on the app. `invite { repo, who, remove: true }` takes them off; `invite { repo }` lists who works on it (a read, never held). The repository's admin only: a contributor is told it is the admin's, anyone else gets not found. | |
| 10 | - **The terminal**: on an app's deploys screen, `W` lists who works on it; `a` invites a person by username and `d` takes the one under the cursor off, each after a confirm. | |
| 9 | - **Talk and MCP**: `invite { repo, who }`, one confirm, makes a person with an account a contributor at once; they hear it on their notice path (`crew/notify.ts`), with a link to their Talk on the app. `invite { repo, who, remove: true }` takes them off; `invite { repo, link: true }`, one confirm, makes a one-use link for anyone, with or without an account (below); `invite { repo, revoke: <id> }` takes an open link back. `invite { repo }` lists who works on it and the links, by state and never with a token (a read, never held). The repository's admin only: a contributor is told it is the admin's, anyone else gets not found. | |
| 10 | - **A one-use link**: `/join/<token>`, good for one person for 7 days; the forge keeps only the token's hash and shows the link once, to whoever made it, never in the conversation it was asked in. Opening it spends nothing: signed out it offers Create an account and Sign in, signed in one Join button. Where the forge asks for an invite to sign up, the link is one: the sign-up spends it and makes the new account a contributor in the same write, then lands on their Talk on the app. A used, taken back or expired link answers one sentence (410). | |
| 11 | - **The terminal**: on an app's deploys screen, `W` lists who works on it; `a` invites a person by username, `l` makes a one-use link (shown once) and `d` takes the one under the cursor off, each after a confirm. | |
| 11 | 12 | - **Exports**: the owner's account export names who contributes to each repository, by username only; a contributor's names the repositories they contribute to. |
| 12 | 13 | - **Proposing from another forge**: a contributor may fork the repository even while the forge holds new accounts (`guard_new_accounts`), since a fork is how a reader proposes; nothing else they make skips that wait. Someone not invited who is held hears it from `propose_upstream` in one sentence: the forge holds forks by new accounts, ask the owner to invite you. |
| 13 | 14 | - **Taking someone off** is at once: every read, app entry and crew direction asks the row on its request. |
| 14 | 15 | |
| 15 | Someone without an account cannot be invited yet: a one-use link for them is the next step. | |
| 16 | ||
| 17 | 16 | Proof: `test/invite-contributors.test.ts`. The rules are `core/invites.ts` and `actions/invites.ts`; the checks are `canRead`, `canEnterApp` (`core/repos.ts`), `REPO_VISIBLE` (`core/visibility.ts`) and `mayDirectCrew` (`crew/direct.ts`). |
src/actions/conversation_confirms.ts +1 −1
| @@ -130,7 +130,7 @@ export async function sayYes(ctx: Ctx, user: User, confirmId: string): Promise<Y | ||
| 130 | 130 | done: ran.ok, |
| 131 | 131 | says, |
| 132 | 132 | ...(told ? { told: true } : {}), |
| 133 | ...(ran.ok ? { result: kept(keptOf(held.tool, ran.value)) } : { error: ran.error, message: ran.message }), | |
| 133 | ...(ran.ok ? { result: kept(keptOf(held.tool, ran.value, held.args)) } : { error: ran.error, message: ran.message }), | |
| 134 | 134 | ...(undo === undefined ? {} : { undo }), |
| 135 | 135 | ...(receipt === undefined ? {} : { receipt }), |
| 136 | 136 | }; |
src/actions/invites.ts +94 −5
| @@ -7,13 +7,21 @@ import type { App } from "../app.ts"; | ||
| 7 | 7 | import { |
| 8 | 8 | addContributor, |
| 9 | 9 | contributedSince, |
| 10 | isContributor, | |
| 10 | 11 | listContributors, |
| 11 | 12 | listInvites, |
| 12 | 13 | mintInvite, |
| 14 | mintRepoInvite, | |
| 13 | 15 | parseDays, |
| 16 | redeemInvite, | |
| 17 | takeUpInvite, | |
| 14 | 18 | removeContributor, |
| 19 | repoInvites, | |
| 20 | repoOfInvite, | |
| 15 | 21 | revokeInvite, |
| 22 | revokeRepoInvite, | |
| 16 | 23 | type Invite, |
| 24 | type RepoInvite, | |
| 17 | 25 | } from "../core/invites.ts"; |
| 18 | 26 | import { canRead, canWrite, getRepoById, type Repo } from "../core/repos.ts"; |
| 19 | 27 | import { getUserByName } from "../core/users.ts"; |
| @@ -82,14 +90,23 @@ export type ContributorError = | ||
| 82 | 90 | | "not_a_person" |
| 83 | 91 | /** They already write here. */ |
| 84 | 92 | | "already_writes" |
| 85 | | "no_such_contributor"; | |
| 93 | | "no_such_contributor" | |
| 94 | /** No open invite link of the repository has that id. */ | |
| 95 | | "no_such_invite" | |
| 96 | /** The link was used, taken back or ran out. */ | |
| 97 | | "spent"; | |
| 98 | ||
| 99 | /** What every door says of an invite link that was used, taken back or ran out. */ | |
| 100 | export const INVITE_SPENT = "This invite was used, taken back or has expired: ask whoever sent it for a new one."; | |
| 86 | 101 | |
| 87 | 102 | /** The refusals, in the words every door says. */ |
| 88 | 103 | export const CONTRIBUTOR_WORDS: Record<Exclude<ContributorError, AccessError>, string> = { |
| 89 | no_such_person: "Nobody here has that username: they need an account on this forge first.", | |
| 104 | no_such_person: "Nobody here has that username: send them an invite link instead, which lets them make an account.", | |
| 90 | 105 | not_a_person: "Only a person is invited as a contributor, never an agent or an organization.", |
| 91 | 106 | already_writes: "They already write here: a contributor is someone who does not.", |
| 92 | 107 | no_such_contributor: "They are not a contributor here.", |
| 108 | no_such_invite: "No open invite link here has that id.", | |
| 109 | spent: INVITE_SPENT, | |
| 93 | 110 | }; |
| 94 | 111 | |
| 95 | 112 | /** |
| @@ -119,10 +136,82 @@ export function inviteContributor( | ||
| 119 | 136 | return ok({ username: person.username, added: true }); |
| 120 | 137 | } |
| 121 | 138 | |
| 122 | /** Who works on `repo` as a contributor: its admin's to read. */ | |
| 123 | export function repoContributors(app: App, actor: User | undefined, repo: Repo): Result<ReturnType<typeof listContributors>, AccessError> { | |
| 139 | /** | |
| 140 | * Who works on `repo` as a contributor, and its invite links, never a token: its admin's to read. | |
| 141 | */ | |
| 142 | export function repoPeople( | |
| 143 | app: App, | |
| 144 | actor: User | undefined, | |
| 145 | repo: Repo, | |
| 146 | ): Result<{ contributors: ReturnType<typeof listContributors>; links: RepoInvite[] }, AccessError> { | |
| 124 | 147 | const admin = adminOf(app, actor, repo); |
| 125 | return admin.ok ? ok(listContributors(app.db, repo.id)) : admin; | |
| 148 | return admin.ok ? ok({ contributors: listContributors(app.db, repo.id), links: repoInvites(app.db, repo.id) }) : admin; | |
| 149 | } | |
| 150 | ||
| 151 | /** | |
| 152 | * The address of an invite link to a repository: the join page, which spends it only by a press. | |
| 153 | */ | |
| 154 | export const joinLink = (origin: string, token: string): string => `${origin.replace(/\/$/, "")}/join/${token}`; | |
| 155 | ||
| 156 | /** | |
| 157 | * A single-use link to work on `repo`, shown this once: whoever opens it may make an account (where | |
| 158 | * the forge asks for an invite, the link is one) and becomes a contributor. Its admin's to make. | |
| 159 | */ | |
| 160 | export function inviteLinkTo( | |
| 161 | app: App, | |
| 162 | actor: User | undefined, | |
| 163 | repo: Repo, | |
| 164 | origin: string, | |
| 165 | ): Result<{ link: string; invite: RepoInvite }, AccessError> { | |
| 166 | const admin = adminOf(app, actor, repo); | |
| 167 | if (!admin.ok) return admin; | |
| 168 | const made = mintRepoInvite(app.db, admin.value.id, repo.id); | |
| 169 | app.bus.append({ kind: "repo.invite_minted", repoId: repo.id, actorId: admin.value.id, payload: { id: made.invite.id, expiresAt: made.invite.expiresAt } }); | |
| 170 | return ok({ link: joinLink(origin, made.token), invite: made.invite }); | |
| 171 | } | |
| 172 | ||
| 173 | /** Take back an open invite link of `repo`: it opens nothing after. */ | |
| 174 | export function revokeInviteLink(app: App, actor: User | undefined, repo: Repo, inviteId: string): Result<void, ContributorError> { | |
| 175 | const admin = adminOf(app, actor, repo); | |
| 176 | if (!admin.ok) return admin; | |
| 177 | if (!revokeRepoInvite(app.db, repo.id, inviteId)) return err("no_such_invite"); | |
| 178 | app.bus.append({ kind: "repo.invite_revoked", repoId: repo.id, actorId: admin.value.id, payload: { id: inviteId } }); | |
| 179 | return ok(undefined); | |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * What an invite token opens for `viewer`: the repository while the link is open; `joined` when | |
| 184 | * the viewer spent it already (their sign-up did); undefined when it opens nothing. | |
| 185 | */ | |
| 186 | export function inviteRepo(app: App, token: string, viewer: User | undefined): { repo: Repo; joined: boolean } | undefined { | |
| 187 | const named = repoOfInvite(app.db, token); | |
| 188 | const repo = named === undefined ? undefined : getRepoById(app.db, named.repoId); | |
| 189 | if (named === undefined || repo === undefined) return undefined; | |
| 190 | const joined = viewer !== undefined && named.usedBy === viewer.id; | |
| 191 | return named.open || joined ? { repo, joined } : undefined; | |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * A signed-in person spends an invite link to a repository by a press: its contributor from now | |
| 196 | * on, by the one redemption a sign-up uses (`redeemInvite`, then `takeUpInvite`). Someone who | |
| 197 | * already works on it spends nothing; a used, revoked or expired link is refused in one sentence. | |
| 198 | */ | |
| 199 | export function joinRepo(app: App, actor: User | undefined, token: string): Result<Repo, ContributorError> { | |
| 200 | if (actor === undefined) return err("unauthorized"); | |
| 201 | if (actor.kind !== "user") return err("not_a_person"); | |
| 202 | const opened = inviteRepo(app, token, actor); | |
| 203 | if (opened === undefined) return err("spent"); | |
| 204 | const repo = opened.repo; | |
| 205 | // Already in, or their own sign-up spent it: the link stays for whoever it was meant for. | |
| 206 | if (opened.joined || canWrite(app.db, repo, actor) || isContributor(app.db, repo.id, actor.id)) return ok(repo); | |
| 207 | const spent = app.db.transaction((): boolean => { | |
| 208 | if (!redeemInvite(app.db, token, actor.id)) return false; | |
| 209 | takeUpInvite(app.db, token, actor.id); | |
| 210 | return true; | |
| 211 | }); | |
| 212 | if (!spent()) return err("spent"); | |
| 213 | app.bus.append({ kind: "repo.contributor_added", repoId: repo.id, actorId: actor.id, payload: { username: actor.username, via: "invite" } }); | |
| 214 | return ok(repo); | |
| 126 | 215 | } |
| 127 | 216 | |
| 128 | 217 | /** Take `username` off `repo`: their next request reads, enters and directs nothing here. */ |
src/actions/users.ts +6 −3
| @@ -26,7 +26,7 @@ import { | ||
| 26 | 26 | } from "../core/tokens.ts"; |
| 27 | 27 | import { mintViaToken, VIA_TOKEN_MAX_MS } from "../core/via_token.ts"; |
| 28 | 28 | import { id } from "../kernel/id.ts"; |
| 29 | import { inviteSpentBy } from "../core/invites.ts"; | |
| 29 | import { contributedSince, inviteSpentBy } from "../core/invites.ts"; | |
| 30 | 30 | import { cleanAsk, NOTE_MAX, SOURCE_MAX } from "../core/requests.ts"; |
| 31 | 31 | import { |
| 32 | 32 | changePassword as rehash, |
| @@ -248,8 +248,11 @@ export async function signUpWithPasskey( | ||
| 248 | 248 | /** Using an invite link is an event: the invite's id and who it let in, never its token. */ |
| 249 | 249 | function auditInvite(app: App, userId: string): void { |
| 250 | 250 | const inviteId = inviteSpentBy(app.db, userId); |
| 251 | if (inviteId !== undefined) | |
| 252 | app.bus.append({ kind: "invite.used", actorId: userId, payload: { id: inviteId } }); | |
| 251 | if (inviteId === undefined) return; | |
| 252 | app.bus.append({ kind: "invite.used", actorId: userId, payload: { id: inviteId } }); | |
| 253 | // A link to work on a repository made the new account its contributor in the same write. | |
| 254 | for (const c of contributedSince(app.db, userId, "")) | |
| 255 | app.bus.append({ kind: "repo.contributor_added", repoId: c.repoId, actorId: userId, payload: { via: "invite" } }); | |
| 253 | 256 | } |
| 254 | 257 | |
| 255 | 258 | /** |
src/core/event_audience.ts +3 −1
| @@ -277,8 +277,10 @@ export function classifiedAudience(kind: string): EventAudience | undefined { | ||
| 277 | 277 | kind.startsWith("keeper_") || |
| 278 | 278 | kind === "forge_down" || |
| 279 | 279 | kind === "forge_up" || |
| 280 | // Who works on a repository as a contributor is its admins' to hear (`actions/invites.ts`). | |
| 280 | // Who works on a repository as a contributor, and its invite links, are its admins' to hear | |
| 281 | // (`actions/invites.ts`). | |
| 281 | 282 | kind.startsWith("repo.contributor_") || |
| 283 | kind.startsWith("repo.invite_") || | |
| 282 | 284 | // A repository's monthly spend cap, set or cleared, is its admins' (`actions/wallet.ts`). |
| 283 | 285 | kind === "wallet.cap" || |
| 284 | 286 | // A wallet under a tenth of its cap, once a month, is theirs to hear first (`core/wallet.ts`). |
src/core/invites.ts +80
| @@ -38,6 +38,9 @@ interface Row { | ||
| 38 | 38 | revoked_at: string | null; |
| 39 | 39 | used_at: string | null; |
| 40 | 40 | used_by_name: string | null; |
| 41 | /** The repository a link to work on it names; null for an invite to the forge. */ | |
| 42 | repo_id: string | null; | |
| 43 | used_by: string | null; | |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | 46 | /** An invite with its redeemer's username, resolved in the one query. */ |
| @@ -105,6 +108,17 @@ export function redeemInvite(db: Database, token: string, userId: string, now: n | ||
| 105 | 108 | ); |
| 106 | 109 | } |
| 107 | 110 | |
| 111 | /** | |
| 112 | * After `redeemInvite`, in its transaction and once the account exists: a link to work on a | |
| 113 | * repository makes its holder a contributor, whether a sign-up spent it or a signed-in press on | |
| 114 | * `/join` did. An invite to the forge alone adds nothing. | |
| 115 | */ | |
| 116 | export function takeUpInvite(db: Database, token: string, userId: string, now: number = Date.now()): void { | |
| 117 | db.query( | |
| 118 | "INSERT OR IGNORE INTO repo_contributor (repo_id, user_id, by_id, at) SELECT repo_id, ?2, created_by, ?3 FROM invite WHERE token_hash = ?1 AND used_by = ?2 AND repo_id IS NOT NULL", | |
| 119 | ).run(sha256(token), userId, new Date(now).toISOString()); | |
| 120 | } | |
| 121 | ||
| 108 | 122 | /** Give back the invite an account spent, when that account is taken back. */ |
| 109 | 123 | export function reopenInvite(db: Database, userId: string): void { |
| 110 | 124 | db.query("UPDATE invite SET used_at = NULL, used_by = NULL WHERE used_by = ?1").run(userId); |
| @@ -151,6 +165,72 @@ export function inviteByToken( | ||
| 151 | 165 | return { invite: view(row, now), from: from?.username }; |
| 152 | 166 | } |
| 153 | 167 | |
| 168 | /** A repository's invite link as its admin lists it: never its token. */ | |
| 169 | export interface RepoInvite { | |
| 170 | readonly id: string; | |
| 171 | readonly createdAt: string; | |
| 172 | readonly expiresAt: string; | |
| 173 | readonly state: InviteState; | |
| 174 | /** The username it let in, once used. */ | |
| 175 | readonly usedBy: string | null; | |
| 176 | } | |
| 177 | ||
| 178 | const repoView = (r: Row, now: number): RepoInvite => { | |
| 179 | const v = view(r, now); | |
| 180 | return { id: v.id, createdAt: v.createdAt, expiresAt: v.expiresAt, state: v.state, usedBy: v.usedBy }; | |
| 181 | }; | |
| 182 | ||
| 183 | /** | |
| 184 | * A single-use link to work on one repository as a contributor, `DEFAULT_DAYS` days, the token's | |
| 185 | * hash alone kept. Where the forge asks for an invite to sign up, it opens that door for its | |
| 186 | * holder too. The token is returned here and nowhere else. | |
| 187 | */ | |
| 188 | export function mintRepoInvite( | |
| 189 | db: Database, | |
| 190 | createdBy: string, | |
| 191 | repoId: string, | |
| 192 | now: number = Date.now(), | |
| 193 | ): { invite: RepoInvite; token: string } { | |
| 194 | const token = `invite_${secret()}`; | |
| 195 | const inviteId = id(now); | |
| 196 | db.query( | |
| 197 | "INSERT INTO invite (id, token_hash, created_by, created_at, expires_at, repo_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | |
| 198 | ).run(inviteId, sha256(token), createdBy, new Date(now).toISOString(), new Date(now + DEFAULT_DAYS * DAY).toISOString(), repoId); | |
| 199 | const row = db.query<Row, [string]>(`${SELECT} WHERE invite.id = ?1`).get(inviteId)!; | |
| 200 | return { invite: repoView(row, now), token }; | |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * The repository an invite token names, whether it is still open and who spent it; undefined when | |
| 205 | * it names none. | |
| 206 | */ | |
| 207 | export function repoOfInvite( | |
| 208 | db: Database, | |
| 209 | token: string, | |
| 210 | now: number = Date.now(), | |
| 211 | ): { repoId: string; open: boolean; usedBy: string | null } | undefined { | |
| 212 | const row = db.query<Row, [string]>(`${SELECT} WHERE invite.token_hash = ?1`).get(sha256(token)); | |
| 213 | if (row === null || row.repo_id === null) return undefined; | |
| 214 | return { repoId: row.repo_id, open: stateOf(row, now) === "open", usedBy: row.revoked_at === null ? row.used_by : null }; | |
| 215 | } | |
| 216 | ||
| 217 | /** A repository's invite links, newest first, never with a token. */ | |
| 218 | export function repoInvites(db: Database, repoId: string, now: number = Date.now()): RepoInvite[] { | |
| 219 | return db | |
| 220 | .query<Row, [string]>(`${SELECT} WHERE invite.repo_id = ?1 ORDER BY invite.id DESC LIMIT 100`) | |
| 221 | .all(repoId) | |
| 222 | .map((r) => repoView(r, now)); | |
| 223 | } | |
| 224 | ||
| 225 | /** Take back a repository's open invite link; false when it is not one of its open ones. */ | |
| 226 | export function revokeRepoInvite(db: Database, repoId: string, inviteId: string, now: number = Date.now()): boolean { | |
| 227 | return ( | |
| 228 | db | |
| 229 | .query("UPDATE invite SET revoked_at = ?3 WHERE id = ?1 AND repo_id = ?2 AND revoked_at IS NULL AND used_at IS NULL") | |
| 230 | .run(inviteId, repoId, new Date(now).toISOString()).changes === 1 | |
| 231 | ); | |
| 232 | } | |
| 233 | ||
| 154 | 234 | /** |
| 155 | 235 | * Whether `userId` is a contributor to the repository: read on every request, so removal is at |
| 156 | 236 | * once. |
src/core/migration-pins/20261125180000-invite-repo.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 812fe8d2e0ab941bcad871ba7a5a9469934aeaf6fa3c822af8a66a4eb12f008a |
src/core/migrations/20261125180000-invite-repo.ts added +7
| @@ -0,0 +1,7 @@ | ||
| 1 | // An invite link to work on one repository (`core/invites.ts`, `mintRepoInvite`): the repository | |
| 2 | // whose contributor it makes its holder, and NULL for an invite to the forge. Its rows go with the | |
| 3 | // repository. Additive. | |
| 4 | export default ` | |
| 5 | ALTER TABLE invite ADD COLUMN repo_id TEXT REFERENCES repo(id) ON DELETE CASCADE; | |
| 6 | CREATE INDEX invite_repo ON invite(repo_id) WHERE repo_id IS NOT NULL; | |
| 7 | `; |
src/core/reserved.ts +1
| @@ -130,6 +130,7 @@ export const SYSTEM_NAMES: readonly string[] = [ | ||
| 130 | 130 | "plugins", |
| 131 | 131 | "signin", |
| 132 | 132 | "signup", |
| 133 | "join", | |
| 133 | 134 | "sitemap.xml", |
| 134 | 135 | "oembed", |
| 135 | 136 | "releases", |
src/core/users.ts +2 −1
| @@ -13,7 +13,7 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 13 | 13 | import { admitSignin } from "./signin-window.ts"; |
| 14 | 14 | import { waitingCount, WAITING_CAP } from "./requests.ts"; |
| 15 | 15 | import { getSignup, type SignupMode } from "../settings/signup.ts"; |
| 16 | import { inviteOpen, redeemInvite, reopenInvite } from "./invites.ts"; | |
| 16 | import { inviteOpen, redeemInvite, reopenInvite, takeUpInvite } from "./invites.ts"; | |
| 17 | 17 | import type { SignupSlot } from "../actions/signup-window.ts"; |
| 18 | 18 | |
| 19 | 19 | export interface User { |
| @@ -380,6 +380,7 @@ export async function createUser( | ||
| 380 | 380 | admin.adminIfFirst ? 1 : 0, |
| 381 | 381 | agent ? 1 : 0, |
| 382 | 382 | ); |
| 383 | if (invite !== undefined) takeUpInvite(db, invite, userId); | |
| 383 | 384 | // A stranger's account keeps its ask, in the same write, waiting until allowed. |
| 384 | 385 | if (admitted.request === "waiting") { |
| 385 | 386 | const at = new Date().toISOString(); |
src/crew/conversation_ask.ts +5 −1
| @@ -300,7 +300,11 @@ export const ASKED_AS: Readonly<Record<string, Form>> = { | ||
| 300 | 300 | }, |
| 301 | 301 | pinned_forge: (a) => pinnedForge(a), |
| 302 | 302 | invite: (a) => |
| 303 | val(a, "who") === "" | |
| 303 | a["link"] === true | |
| 304 | ? `Make a one-use link to work on ${repo(a)}: whoever opens it can make an account, open the app and ask the crew for changes; they cannot merge or change settings` | |
| 305 | : val(a, "revoke") !== "" | |
| 306 | ? `Take back the invite link ${val(a, "revoke")} to ${repo(a)}: it opens nothing after` | |
| 307 | : val(a, "who") === "" | |
| 304 | 308 | ? `List who works on ${repo(a)}` |
| 305 | 309 | : a["remove"] === true |
| 306 | 310 | ? `Take ${val(a, "who")} off ${repo(a)}: they can no longer open it, its app or ask its crew` |
src/crew/conversation_confirm.ts +11 −2
| @@ -28,6 +28,8 @@ export interface Asking { | ||
| 28 | 28 | * instead. |
| 29 | 29 | */ |
| 30 | 30 | readonly once?: string; |
| 31 | /** Which calls' results are shown once; absent, every call's. */ | |
| 32 | readonly onceWhen?: (args: Args) => boolean; | |
| 31 | 33 | } |
| 32 | 34 | |
| 33 | 35 | /** Ten minutes: a yes later than this is refused, and the agent asks again. */ |
| @@ -106,7 +108,11 @@ export const ASKING: Readonly<Record<string, Asking>> = { | ||
| 106 | 108 | land_past_gate: {}, |
| 107 | 109 | share: {}, |
| 108 | 110 | invites: { asks: (a) => a["action"] !== "list" }, |
| 109 | invite: { asks: (a) => typeof a["who"] === "string" && a["who"] !== "" }, | |
| 111 | invite: { | |
| 112 | asks: (a) => (typeof a["who"] === "string" && a["who"] !== "") || a["link"] === true || (typeof a["revoke"] === "string" && a["revoke"] !== ""), | |
| 113 | once: "The invite link was shown once, to you, and is not kept here.", | |
| 114 | onceWhen: (a) => a["link"] === true, | |
| 115 | }, | |
| 110 | 116 | // The access wall is a person's decision: the agent puts it to them, in the wall's words. |
| 111 | 117 | wall: { undo: (a) => (a["answer"] === "allow" ? "`take_back` opens the pull that reverts it" : undefined) }, |
| 112 | 118 | egress_grants: { asks: not("op", "list") }, |
| @@ -298,7 +304,10 @@ const UNDONE: Readonly<Record<string, (args: Args) => string | undefined>> = { | ||
| 298 | 304 | }; |
| 299 | 305 | |
| 300 | 306 | /** What the result turn keeps of a done call: a sentence in place of a result shown only once. */ |
| 301 | export const keptOf = (tool: string, value: unknown): unknown => ASKING[tool]?.once ?? value; | |
| 307 | export const keptOf = (tool: string, value: unknown, args: Args = {}): unknown => { | |
| 308 | const asking = ASKING[tool]; | |
| 309 | return asking?.once !== undefined && (asking.onceWhen?.(args) ?? true) ? asking.once : value; | |
| 310 | }; | |
| 302 | 311 | |
| 303 | 312 | /** How a done call is undone, or undefined when it has no undo. */ |
| 304 | 313 | export const undoOf = (tool: string, args: Args): string | undefined => |
src/http/marks/join.json added +4
| @@ -0,0 +1,4 @@ | ||
| 1 | { | |
| 2 | "head": "/join", | |
| 3 | "mark": "open" | |
| 4 | } |
src/http/mcp/tools/invite.ts +34 −7
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | import { |
| 2 | 2 | CONTRIBUTOR_WORDS, |
| 3 | 3 | inviteContributor, |
| 4 | inviteLinkTo, | |
| 4 | 5 | removeContributorFrom, |
| 5 | repoContributors, | |
| 6 | repoPeople, | |
| 7 | revokeInviteLink, | |
| 6 | 8 | type ContributorError, |
| 7 | 9 | } from "../../../actions/invites.ts"; |
| 8 | 10 | import { type Answer, bad, good, isAnswer, REPO_ARG, repoArg, str, type Tool } from "../shared.ts"; |
| @@ -15,26 +17,51 @@ const refused = (e: ContributorError): Answer => | ||
| 15 | 17 | ? bad("not_found", "no such app or repository (owner/name)") |
| 16 | 18 | : bad(e, CONTRIBUTOR_WORDS[e]); |
| 17 | 19 | |
| 20 | /** Only a list reads: every other call changes who works on the app. */ | |
| 21 | const lists = (args: Record<string, unknown>): boolean => | |
| 22 | str(args["who"]) === "" && args["link"] !== true && str(args["revoke"]) === ""; | |
| 23 | ||
| 18 | 24 | export const tool: Tool = { |
| 19 | 25 | name: "invite", |
| 20 | doc: "`{ repo, who?, remove? }`: admin adds or takes off contributor `who`; none lists", | |
| 21 | description: "Invite someone with an account to work on an app; list or remove contributors.", | |
| 26 | doc: "`{ repo, who?, remove?, link?, revoke? }`: admin adds or takes off contributor `who`, makes a one-use `link` (it may make an account), or `revoke`s a link by id; none lists", | |
| 27 | description: | |
| 28 | "Invite someone to work on an app, by name or a one-use link that may make an account; list or remove.", | |
| 22 | 29 | inputSchema: { |
| 23 | 30 | type: "object", |
| 24 | properties: { repo: REPO_ARG, who: { type: "string" }, remove: { type: "boolean" } }, | |
| 31 | properties: { | |
| 32 | repo: REPO_ARG, | |
| 33 | who: { type: "string" }, | |
| 34 | remove: { type: "boolean" }, | |
| 35 | link: { type: "boolean" }, | |
| 36 | revoke: { type: "string" }, | |
| 37 | }, | |
| 25 | 38 | required: ["repo"], |
| 26 | 39 | }, |
| 27 | 40 | writes: true, |
| 28 | 41 | // The list only reads: the agent need not ask for it. |
| 29 | heldNotWhen: (args) => str(args["who"]) === "", | |
| 42 | heldNotWhen: lists, | |
| 30 | 43 | run(ctx, user, args) { |
| 31 | 44 | const repo = repoArg(ctx, user, args); |
| 32 | 45 | if (isAnswer(repo)) return repo; |
| 33 | 46 | const who = str(args["who"]); |
| 34 | 47 | const name = `${repo.ownerName}/${repo.name}`; |
| 48 | if (args["link"] === true) { | |
| 49 | const made = inviteLinkTo(ctx.app, user, repo, ctx.app.config.url ?? ctx.url.origin); | |
| 50 | if (!made.ok) return refused(made.error); | |
| 51 | return good({ | |
| 52 | link: made.value.link, | |
| 53 | expiresAt: made.value.invite.expiresAt, | |
| 54 | says: `A one-use link to work on ${name}, shown this once: whoever opens it can make an account, then asks its crew for changes and lands nothing.`, | |
| 55 | }); | |
| 56 | } | |
| 57 | const revoke = str(args["revoke"]); | |
| 58 | if (revoke !== "") { | |
| 59 | const done = revokeInviteLink(ctx.app, user, repo, revoke); | |
| 60 | return done.ok ? good({ revoked: revoke, says: "That link opens nothing now." }) : refused(done.error); | |
| 61 | } | |
| 35 | 62 | if (who === "") { |
| 36 | const people = repoContributors(ctx.app, user, repo); | |
| 37 | return people.ok ? good({ contributors: people.value }) : refused(people.error); | |
| 63 | const people = repoPeople(ctx.app, user, repo); | |
| 64 | return people.ok ? good(people.value) : refused(people.error); | |
| 38 | 65 | } |
| 39 | 66 | if (args["remove"] === true) { |
| 40 | 67 | const done = removeContributorFrom(ctx.app, user, repo, who); |
src/http/server.ts +4
| @@ -167,6 +167,8 @@ import { | ||
| 167 | 167 | signinSubmit, |
| 168 | 168 | signupPage, |
| 169 | 169 | requestedPage, |
| 170 | joinPage, | |
| 171 | joinSubmit, | |
| 170 | 172 | signupSubmit, |
| 171 | 173 | verifyPage, |
| 172 | 174 | themeSubmit, |
| @@ -1072,6 +1074,8 @@ export function buildRouter(): Router<Handler> { | ||
| 1072 | 1074 | router.add("POST", "/claim/start/:token", startClaimSubmit); |
| 1073 | 1075 | router.add("POST", "/claim/start/:token/account", startClaimAccountSubmit); |
| 1074 | 1076 | router.add("GET", "/signup/requested", requestedPage); |
| 1077 | router.add("GET", "/join/:token", joinPage); | |
| 1078 | router.add("POST", "/join/:token", joinSubmit); | |
| 1075 | 1079 | router.add("GET", "/verify", verifyPage); |
| 1076 | 1080 | router.add("POST", "/signup", signupSubmit); |
| 1077 | 1081 | router.add("POST", "/logout", logoutSubmit); |
src/tui/screens/deploys.ts +19 −7
| @@ -140,7 +140,8 @@ import { envTone } from "./runwords.ts"; | ||
| 140 | 140 | import { pullScreen } from "./pull.ts"; |
| 141 | 141 | import { local } from "./where.ts"; |
| 142 | 142 | import { appMembersScreen } from "./appmembers.ts"; |
| 143 | import { CONTRIBUTOR_WORDS, inviteContributor, removeContributorFrom, repoContributors, type ContributorError } from "../../actions/invites.ts"; | |
| 143 | import { CONTRIBUTOR_WORDS, inviteContributor, inviteLinkTo, removeContributorFrom, repoPeople, type ContributorError } from "../../actions/invites.ts"; | |
| 144 | import { publicOrigin } from "../../core/public_origin.ts"; | |
| 144 | 145 | import { mayManagePrompts, promptsScreen } from "./prompts.ts"; |
| 145 | 146 | import { editDnsFromTerminal } from "./domain_dns.ts"; |
| 146 | 147 | import type { DnsStep } from "../../actions/domain_dns.ts"; |
| @@ -1028,19 +1029,22 @@ export function contributorRefusal(e: ContributorError): string { | ||
| 1028 | 1029 | |
| 1029 | 1030 | /** |
| 1030 | 1031 | * Who works on an app with its owner, in the terminal: `W` on the deploys screen, the same list |
| 1031 | * and actions as Talk's `invite` (`actions/invites.ts`). `a` invites a person by username and `d` | |
| 1032 | * takes the one under the cursor off, each after a confirm. The repository's admin only. | |
| 1032 | * and actions as Talk's `invite` (`actions/invites.ts`). `a` invites a person by username, `l` | |
| 1033 | * makes a one-use link (shown this once; it may make an account) and `d` takes the one under the | |
| 1034 | * cursor off, each after a confirm. The repository's admin only. | |
| 1033 | 1035 | */ |
| 1034 | 1036 | export function contributorsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { |
| 1035 | 1037 | const state = new ListState(); |
| 1036 | 1038 | let people: readonly string[] = []; |
| 1039 | let open = 0; | |
| 1037 | 1040 | let canChange = false; |
| 1038 | 1041 | let note = ""; |
| 1039 | 1042 | const name = `${repo.ownerName}/${repo.name}`; |
| 1040 | 1043 | const reload = (): void => { |
| 1041 | const read = repoContributors(f.app, f.user, repo); | |
| 1044 | const read = repoPeople(f.app, f.user, repo); | |
| 1042 | 1045 | canChange = read.ok; |
| 1043 | people = read.ok ? read.value.map((c) => c.username) : []; | |
| 1046 | people = read.ok ? read.value.contributors.map((c) => c.username) : []; | |
| 1047 | open = read.ok ? read.value.links.filter((l) => l.state === "open").length : 0; | |
| 1044 | 1048 | if (!read.ok && note === "") note = contributorRefusal(read.error); |
| 1045 | 1049 | state.clamp(people.length); |
| 1046 | 1050 | ctx.invalidate(); |
| @@ -1083,7 +1087,7 @@ export function contributorsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 1083 | 1087 | get keys() { |
| 1084 | 1088 | return [ |
| 1085 | 1089 | binding(["j", "k"], "move"), |
| 1086 | ...(canChange ? [binding(["a"], "invite someone"), binding(["d"], "take them off")] : []), | |
| 1090 | ...(canChange ? [binding(["a"], "invite someone"), binding(["l"], "make a link"), binding(["d"], "take them off")] : []), | |
| 1087 | 1091 | binding(["esc"], "back"), |
| 1088 | 1092 | ]; |
| 1089 | 1093 | }, |
| @@ -1095,7 +1099,8 @@ export function contributorsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 1095 | 1099 | : people.length === 0 |
| 1096 | 1100 | ? `Nobody works on ${name} with you yet. Press a to invite someone.` |
| 1097 | 1101 | : `${people.length} work on ${name} with you: they read it, enter the app and ask its crew for changes.`; |
| 1098 | const said = wrap(text, Math.max(1, area.w)).slice(0, 4); | |
| 1102 | const links = open === 0 || note !== "" ? "" : ` ${open} invite ${open === 1 ? "link is" : "links are"} open.`; | |
| 1103 | const said = wrap(text + links, Math.max(1, area.w)).slice(0, 4); | |
| 1099 | 1104 | const [head, list] = split(area, "v", [len(said.length), fill(1)]); |
| 1100 | 1105 | said.forEach((line, i) => s.text(head!.x, head!.y + i, line, style(c.mutedFg, c.bg), head!.x + head!.w)); |
| 1101 | 1106 | drawList(s, list!, c, state, people.map((p) => ({ title: p }))); |
| @@ -1107,6 +1112,13 @@ export function contributorsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 1107 | 1112 | ctx.push(invite()); |
| 1108 | 1113 | return true; |
| 1109 | 1114 | } |
| 1115 | if (e.key === "l") { | |
| 1116 | confirm("Make a link", `A one-use link to work on ${name}: whoever opens it can make an account and ask its crew for changes, and lands nothing.`, "Make it", () => { | |
| 1117 | const made = inviteLinkTo(f.app, f.user, repo, publicOrigin(f.app.config.url, `http://localhost:${f.app.config.port}`)); | |
| 1118 | say(made.ok ? `Send this link; it is shown once and works for one person until ${made.value.invite.expiresAt.slice(0, 10)}: ${made.value.link}` : contributorRefusal(made.error)); | |
| 1119 | }); | |
| 1120 | return true; | |
| 1121 | } | |
| 1110 | 1122 | const who = people[state.index]; |
| 1111 | 1123 | if (e.key === "d" && who !== undefined) { |
| 1112 | 1124 | confirm("Take off", `${who} reads, enters and asks for nothing on ${name} from now on.`, "Take off", () => { |
src/web/pages/site.ts +46 −1
| @@ -26,7 +26,7 @@ import { REPO_DIR_IN_THE_WAY, REPO_INIT_FAILED, REPO_NAME_RULES } from "../../ac | ||
| 26 | 26 | import { NOTE_MAX, requestOf } from "../../core/requests.ts"; |
| 27 | 27 | import { DECLINED_MESSAGE, REQUEST_IN_MESSAGE, STILL_WAITING_MESSAGE } from "../../actions/users.ts"; |
| 28 | 28 | import { inviteByToken } from "../../core/invites.ts"; |
| 29 | import { inviteExpiredLine, inviteExpiryLine } from "../../actions/invites.ts"; | |
| 29 | import { INVITE_SPENT, inviteExpiredLine, inviteExpiryLine, inviteRepo, joinRepo } from "../../actions/invites.ts"; | |
| 30 | 30 | import { whenWords } from "../../core/when_words.ts"; |
| 31 | 31 | import { HUMAN_CHECK_MESSAGE, INVITE_SPENT_MESSAGE, PASSKEY_REQUIRED_MESSAGE, SIGNUP_CLOSED_MESSAGE, signUp, signupClosed } from "../../actions/users.ts"; |
| 32 | 32 | import { accessAllows, listOrgsForUser, orgAccess } from "../../core/orgs.ts"; |
| @@ -674,6 +674,51 @@ export function requestPage(ctx: Ctx, said: string, title = "Your request is in" | ||
| 674 | 674 | ); |
| 675 | 675 | } |
| 676 | 676 | |
| 677 | // GET /join/:token — a link to work on one app: where, and one button. It spends nothing: only the | |
| 678 | // signed-in press does (`joinRepo`), so a link preview or a crawler never joins. Once its holder | |
| 679 | // has joined (their sign-up spent it), it is their way into Talk on the app. | |
| 680 | export function joinPage(ctx: Ctx): Response { | |
| 681 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 682 | const token = ctx.params["token"] ?? ""; | |
| 683 | const opened = timePhase(ctx.request, "invite", () => inviteRepo(ctx.app, token, ctx.auth.user)); | |
| 684 | return timePhase(ctx.request, "render", () => { | |
| 685 | if (opened === undefined) return requestPage(ctx, INVITE_SPENT, "This invite opens nothing now", 410); | |
| 686 | const name = `${opened.repo.ownerName}/${opened.repo.name}`; | |
| 687 | if (opened.joined) return redirect(`/talk?about=${encodeURIComponent(name)}`); | |
| 688 | const here = `/join/${encodeURIComponent(token)}`; | |
| 689 | const said = html`You are invited to work on <strong>${name}</strong>: read it, open the app and ask its crew for changes from your own Talk. Whoever owns it lands what you ask.`; | |
| 690 | return page({ | |
| 691 | ctx, | |
| 692 | title: `Work on ${name}`, | |
| 693 | body: html`<div class="narrow stack"> | |
| 694 | ${pageHead(`Work on ${name}`)} | |
| 695 | <p>${said}</p> | |
| 696 | ${ctx.auth.user === undefined | |
| 697 | ? html`<div class="form-actions"> | |
| 698 | ${signupClosed(ctx.app, token) ? null : btn("Create an account", { href: `/signup?${new URLSearchParams({ invite: token, next: here })}`, variant: "primary" })} | |
| 699 | ${btn("Sign in", { href: withReturn("/signin", here), variant: "outline" })} | |
| 700 | </div>` | |
| 701 | : html`<form method="post" action="${here}" class="form-actions">${btn("Join", { variant: "primary" })}</form>`} | |
| 702 | </div>`, | |
| 703 | }); | |
| 704 | }); | |
| 705 | } | |
| 706 | ||
| 707 | // POST /join/:token — the signed-in press that spends the link, then their Talk on the app. | |
| 708 | export async function joinSubmit(ctx: Ctx): Promise<Response> { | |
| 709 | const form = await readForm(ctx, "rw"); | |
| 710 | if (form instanceof Response) return form; | |
| 711 | const token = ctx.params["token"] ?? ""; | |
| 712 | const joined = joinRepo(ctx.app, ctx.auth.user, token); | |
| 713 | if (!joined.ok) | |
| 714 | return joined.error === "unauthorized" | |
| 715 | ? redirect(withReturn("/signin", `/join/${encodeURIComponent(token)}`)) | |
| 716 | : requestPage(ctx, INVITE_SPENT, "This invite opens nothing now", 410); | |
| 717 | const name = `${joined.value.ownerName}/${joined.value.name}`; | |
| 718 | // Their own Talk on the app: the address carries no token. | |
| 719 | return flashTo(ctx, `/talk?about=${encodeURIComponent(name)}`, { message: `You work on ${name} now: ask for a change.` }); | |
| 720 | } | |
| 721 | ||
| 677 | 722 | // GET /signup/requested — where a request lands: no session, nothing to act on yet. |
| 678 | 723 | export function requestedPage(ctx: Ctx): Response { |
| 679 | 724 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
src/web/pages/talk.ts +7 −1
| @@ -110,6 +110,10 @@ const HERE = "/talk"; | ||
| 110 | 110 | const FIND_CHARS = 100; |
| 111 | 111 | const CONVERSATION_ID = /^[0-9a-z]{1,40}$/; |
| 112 | 112 | |
| 113 | /** `?about=owner/name`, as a repository name; anything else (an issue, a page) is not one. */ | |
| 114 | const aboutRepo = (about: string | null): string | undefined => | |
| 115 | about !== null && /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,99}\/[A-Za-z0-9_][A-Za-z0-9_.-]{0,99}$/.test(about) ? about : undefined; | |
| 116 | ||
| 113 | 117 | /** The one line naming what the talk is about (`core/talk_about.ts`), or nothing. */ |
| 114 | 118 | function aboutLine(ctx: Ctx): Html | null { |
| 115 | 119 | const line = aboutText(ctx.app.db, ctx.url.searchParams.get("about"), ctx.auth.user); |
| @@ -583,7 +587,9 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 583 | 587 | const shown = view ?? "chat"; |
| 584 | 588 | // Below 900 px (56.25rem) the list and the thread take turns; ?chats=1 asks for the list. |
| 585 | 589 | const listing = ctx.url.searchParams.get("chats") === "1"; |
| 586 | const cited = repoOf(turns); | |
| 590 | // A conversation that names nothing yet is about what `?about=` names, when it is a repository: | |
| 591 | // an invitee's first open frames the app they were invited to (`/join`), read as they may. | |
| 592 | const cited = repoOf(turns) ?? aboutRepo(ctx.url.searchParams.get("about")); | |
| 587 | 593 | const steer = steerOf(turns); |
| 588 | 594 | const refs = refTarget(ctx.app.db, user, cited); |
| 589 | 595 | const voice = voiceOf(ctx.app.db, user.id, conversation.id, turns, ctx.url.origin); |
test/conversation-confirm.test.ts +13
| @@ -360,6 +360,19 @@ describe("a result shown once", () => { | ||
| 360 | 360 | expect(last.text).not.toContain(String(done.result.link)); |
| 361 | 361 | expect(JSON.parse(last.text).result).toBe(ASKING["mint_invite"]!.once); |
| 362 | 362 | }); |
| 363 | ||
| 364 | test("invite's one-use link is answered to the person and never kept; its list and its adds are kept as they are", async () => { | |
| 365 | const w = await world(alice); | |
| 366 | const asked = JSON.parse((await call(w.token, "invite", { repo: w.repo, link: true })).text); | |
| 367 | expect(asked.needs_confirm).toMatch(/^[0-9a-z]+$/); | |
| 368 | const done: any = await (await yes(asked.needs_confirm, alice)).json(); | |
| 369 | expect(String(done.result.link)).toContain("/join/invite_"); | |
| 370 | const last = turns(alice).at(-1)!; | |
| 371 | expect(last.text).not.toContain(String(done.result.link)); | |
| 372 | expect(JSON.parse(last.text).result).toBe(ASKING["invite"]!.once); | |
| 373 | // Only the link is shown once: a list is not held, and its answer is the list. | |
| 374 | expect((await call(w.token, "invite", { repo: w.repo })).text).toContain('"links"'); | |
| 375 | }); | |
| 363 | 376 | }); |
| 364 | 377 | |
| 365 | 378 | describe("the yes, reached by a person", () => { |
test/invite-contributors.test.ts +114 −3
| @@ -36,11 +36,12 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 36 | 36 | import { setHeadroom } from "../src/core/standing.ts"; |
| 37 | 37 | import { addSubscription } from "../src/core/subscriptions.ts"; |
| 38 | 38 | import { createToken } from "../src/core/tokens.ts"; |
| 39 | import { createUser, type User } from "../src/core/users.ts"; | |
| 39 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 40 | 40 | import { createWebhook } from "../src/core/webhooks.ts"; |
| 41 | 41 | import { mayDirectCrew } from "../src/crew/direct.ts"; |
| 42 | 42 | import { accountExport } from "../src/core/account_export.ts"; |
| 43 | import { contributorLines } from "../src/actions/invites.ts"; | |
| 43 | import { contributorLines, INVITE_SPENT } from "../src/actions/invites.ts"; | |
| 44 | import { setSignup } from "../src/settings/signup.ts"; | |
| 44 | 45 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 45 | 46 | import { contributorsScreen } from "../src/tui/screens/deploys.ts"; |
| 46 | 47 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -283,7 +284,7 @@ describe("the doors: Talk's invite and the terminal", () => { | ||
| 283 | 284 | test("only the repository's admin invites: a contributor is told it is the admin's, a stranger is told nothing, a name with no account is said so", async () => { |
| 284 | 285 | expect((await mcp(ben, "invite", { repo: name(), who: "carol" })).text).toContain("only the repository's admin"); |
| 285 | 286 | expect((await mcp(carol, "invite", { repo: name(), who: "carol" })).text).toContain("not_found"); |
| 286 | expect((await mcp(alice, "invite", { repo: name(), who: "nobody-here" })).text).toContain("they need an account on this forge first"); | |
| 287 | expect((await mcp(alice, "invite", { repo: name(), who: "nobody-here" })).text).toContain("send them an invite link instead"); | |
| 287 | 288 | expect(canRead(standIn.app.db, fresh(), carol)).toBe(false); |
| 288 | 289 | // The admin's list names who works on it; the list only reads. |
| 289 | 290 | expect((await mcp(alice, "invite", { repo: name() })).text).toContain('"username":"ben"'); |
| @@ -344,6 +345,116 @@ describe("the doors: Talk's invite and the terminal", () => { | ||
| 344 | 345 | }, 30_000); |
| 345 | 346 | }); |
| 346 | 347 | |
| 348 | describe("the one-use link: anyone, with or without an account", () => { | |
| 349 | const tokenOf = (text: string): string => /\/join\/(invite_[A-Za-z0-9_-]+)/.exec(text)?.[1] ?? ""; | |
| 350 | /** Every row of every table, as text: where a token must never be. */ | |
| 351 | const everything = (): string => | |
| 352 | standIn.app.db | |
| 353 | .query<{ name: string }, []>("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'") | |
| 354 | .all() | |
| 355 | .map((t) => JSON.stringify(standIn.app.db.query(`SELECT * FROM "${t.name}"`).all())) | |
| 356 | .join("\n"); | |
| 357 | let frank: User; | |
| 358 | let hal: User; | |
| 359 | beforeAll(async () => { | |
| 360 | [frank, hal] = [await person("frank"), await person("hal")]; | |
| 361 | }); | |
| 362 | ||
| 363 | test("only the admin makes or takes back a link: a contributor is told it is the admin's, a stranger nothing", async () => { | |
| 364 | expect((await mcp(ben, "invite", { repo: name(), link: true })).text).toContain("only the repository's admin"); | |
| 365 | expect((await mcp(hal, "invite", { repo: name(), link: true })).text).toContain("not_found"); | |
| 366 | expect((await mcp(alice, "invite", { repo: name(), revoke: "nope" })).text).toContain("No open invite link here has that id."); | |
| 367 | }); | |
| 368 | ||
| 369 | test("a link is shown once and kept hashed; a GET spends nothing; frank joins by a press; a second redemption is one sentence", async () => { | |
| 370 | const { app } = standIn; | |
| 371 | const made = await mcp(alice, "invite", { repo: name(), link: true }); | |
| 372 | const token = tokenOf(made.text); | |
| 373 | expect(token).not.toBe(""); | |
| 374 | expect(everything()).not.toContain(token); | |
| 375 | const listed = await mcp(alice, "invite", { repo: name() }); | |
| 376 | expect([listed.text.includes(token), listed.text.includes('"state":"open"')]).toEqual([false, true]); | |
| 377 | expect(await (await page(undefined, `/join/${token}`)).text()).toContain("Create an account"); | |
| 378 | expect(await (await page(frank, `/join/${token}`)).text()).toContain(`<form method="post" action="/join/${token}"`); | |
| 379 | expect(canRead(app.db, fresh(), frank)).toBe(false); | |
| 380 | const joined = await page(frank, `/join/${token}`, { method: "POST", body: "" }); | |
| 381 | expect([joined.status, joined.headers.get("location")]).toEqual([303, `/talk?about=${encodeURIComponent(name())}`]); | |
| 382 | expect([canRead(app.db, fresh(), frank), mayDirectCrew(app.db, fresh(), frank)]).toEqual([true, true]); | |
| 383 | // Frank's Talk on it frames the app; a stranger's names nothing of it. | |
| 384 | expect(await (await page(frank, `/talk?about=${encodeURIComponent(name())}&pane=preview&fragment=pane`)).text()).toContain(`data-pane-of="${name()}"`); | |
| 385 | expect(await (await page(hal, `/talk?about=${encodeURIComponent(name())}&pane=preview&fragment=pane`)).text()).not.toContain(name()); | |
| 386 | const again = await page(hal, `/join/${token}`, { method: "POST", body: "" }); | |
| 387 | expect(again.status).toBe(410); | |
| 388 | expect(await again.text()).toContain(INVITE_SPENT); | |
| 389 | expect(canRead(app.db, fresh(), hal)).toBe(false); | |
| 390 | expect((await mcp(alice, "invite", { repo: name() })).text).toContain('"usedBy":"frank"'); | |
| 391 | expect(await takeOff(frank)).toContain("is no longer a contributor"); | |
| 392 | }); | |
| 393 | ||
| 394 | test("where the forge asks for an invite, the link makes the account and the contributor in one sign-up, and lands on Talk", async () => { | |
| 395 | const { app } = standIn; | |
| 396 | setSignup(app.db, "invite", alice.id); | |
| 397 | try { | |
| 398 | const token = tokenOf((await mcp(alice, "invite", { repo: name(), link: true })).text); | |
| 399 | const offered = await (await page(undefined, `/join/${token}`)).text(); | |
| 400 | expect(offered).toContain(`/signup?invite=${token}`); | |
| 401 | const signed = await fetch(`${url}/signup?${new URLSearchParams({ invite: token, next: `/join/${token}` })}`, { | |
| 402 | method: "POST", | |
| 403 | redirect: "manual", | |
| 404 | headers: { origin: url, "content-type": "application/x-www-form-urlencoded" }, | |
| 405 | body: new URLSearchParams({ username: "ivy", email: "[email protected]", password: "a-good-passphrase" }), | |
| 406 | }); | |
| 407 | expect([signed.status, signed.headers.get("location")]).toEqual([303, `/join/${token}`]); | |
| 408 | const ivy = getUserByName(app.db, "ivy")!; | |
| 409 | expect([canRead(app.db, fresh(), ivy), mayDirectCrew(app.db, fresh(), ivy)]).toEqual([true, true]); | |
| 410 | // Her own way back in is to Talk on the app; anyone else is told the link is spent. | |
| 411 | const back = await page(ivy, `/join/${token}`); | |
| 412 | expect([back.status, back.headers.get("location")]).toEqual([303, `/talk?about=${encodeURIComponent(name())}`]); | |
| 413 | expect((await page(hal, `/join/${token}`)).status).toBe(410); | |
| 414 | // Without a link the door stays shut. | |
| 415 | const shut = await fetch(`${url}/signup`, { | |
| 416 | method: "POST", | |
| 417 | redirect: "manual", | |
| 418 | headers: { origin: url, "content-type": "application/x-www-form-urlencoded" }, | |
| 419 | body: new URLSearchParams({ username: "jay", email: "[email protected]", password: "a-good-passphrase" }), | |
| 420 | }); | |
| 421 | expect(shut.status).not.toBe(303); | |
| 422 | expect(getUserByName(app.db, "jay")).toBeUndefined(); | |
| 423 | expect(await takeOff(ivy)).toContain("is no longer a contributor"); | |
| 424 | } finally { | |
| 425 | setSignup(app.db, "open", alice.id); | |
| 426 | } | |
| 427 | }); | |
| 428 | ||
| 429 | test("a link taken back opens nothing, by its page or its press", async () => { | |
| 430 | const token = tokenOf((await mcp(alice, "invite", { repo: name(), link: true })).text); | |
| 431 | const open = /"id":"([^"]+)","createdAt"[^}]*"state":"open"/.exec((await mcp(alice, "invite", { repo: name() })).text)![1]!; | |
| 432 | expect((await mcp(alice, "invite", { repo: name(), revoke: open })).error).toBe(false); | |
| 433 | expect((await page(hal, `/join/${token}`)).status).toBe(410); | |
| 434 | expect((await page(hal, `/join/${token}`, { method: "POST", body: "" })).status).toBe(410); | |
| 435 | expect(canRead(standIn.app.db, fresh(), hal)).toBe(false); | |
| 436 | }); | |
| 437 | ||
| 438 | test("the terminal makes a link by a confirm and shows it once", async () => { | |
| 439 | const terminal = headlessTerminal(200, 44); | |
| 440 | const { app: tui, session } = createForgeApp(standIn.app, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user: alice }, { terminal, exitHooks: false }); | |
| 441 | tui.start(); | |
| 442 | await settleApp(tui); | |
| 443 | tui.push(contributorsScreen(session, tui.ctx, fresh())); | |
| 444 | await settleApp(tui); | |
| 445 | for (const k of ["l", "\x1b[C", "\r"]) { | |
| 446 | terminal.feed(k); | |
| 447 | await settleApp(tui); | |
| 448 | } | |
| 449 | const shown = tui.screen.toText().replace(/\s+/g, ""); | |
| 450 | tui.stop(); | |
| 451 | const token = tokenOf(shown); | |
| 452 | expect(token).not.toBe(""); | |
| 453 | expect(everything()).not.toContain(token); | |
| 454 | expect(await (await page(hal, `/join/${token}`)).text()).toContain(`action="/join/${token}"`); | |
| 455 | }, 30_000); | |
| 456 | }); | |
| 457 | ||
| 347 | 458 | describe("taking someone off is at once", () => { |
| 348 | 459 | test("dave's next request after removal reads, enters and directs nothing", async () => { |
| 349 | 460 | const { app } = standIn; |
test/no-polling.test.ts +1
| @@ -318,6 +318,7 @@ const NOT_WALKED: Record<string, string> = { | ||
| 318 | 318 | "/boards/:slug/live": "needs a board; a stream, not a page", |
| 319 | 319 | "/bring/:id/:job": "needs a bring job", |
| 320 | 320 | "/claim/start/:token": "needs the one-time token an anonymous start returned", |
| 321 | "/join/:token": "needs a one-use link to work on a repository, which a repository's admin mints", | |
| 321 | 322 | "/docs/:file": "a redirect to the documentation, not a page; the file name is one the seed cannot guess", |
| 322 | 323 | "/docs/features/:file": "a reference note of the documentation; the seed supplies no :file", |
| 323 | 324 | "/fleet/ops/:id": "needs a fleet operation", |
test/pages-budget.test.ts +3
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | * so no reading answers here while the pages are asked, and a page that waits |
| 18 | 18 | * for one is over `AFTER_IDLE_MS` by construction, on any machine. |
| 19 | 19 | */ |
| 20 | import { mintRepoInvite } from "../src/core/invites.ts"; | |
| 20 | 21 | import { holdName, move, newId, newTicket } from "../src/billing/orders.ts"; |
| 21 | 22 | import { fileReport } from "../src/actions/reports.ts"; |
| 22 | 23 | import { commitLog } from "../src/git/read.ts"; |
| @@ -201,6 +202,8 @@ beforeAll(async () => { | ||
| 201 | 202 | // An agent's start the claim page can show (`/claim/start/:token`): bob's, so no extra account |
| 202 | 203 | // joins the walk. |
| 203 | 204 | recordStart(app.db, bob.id, "claim_fixture", "127.0.0.1"); |
| 205 | // A one-use link to work on alice's forge repository (`/join/:token`): opened, never spent. | |
| 206 | FIXTURE["/join/:token"] = `/join/${mintRepoInvite(app.db, alice.id, forge.id).token}`; | |
| 204 | 207 | // A paid order for the receipt pages (#16046): alice, an admin, may read it. |
| 205 | 208 | const orderId = newId(); |
| 206 | 209 | holdName(app.db, { id: orderId, name: "fixture-shop", plan: "small", priceUsd: 19, email: "[email protected]", ticket: newTicket(), address: "t" }, Date.now()); |
test/security-floor-routes.test.ts +1
| @@ -74,6 +74,7 @@ const UNSEEDED: Record<string, string> = { | ||
| 74 | 74 | "/api/v1/repos/:owner/:name/drop/:file": "a file name in an app's private drop; the seed drops none", |
| 75 | 75 | "/api/v1/runner/jobs/:id/git/info/refs": "a job a runner holds, read with that job's own token; the seed offers none (test/checks-runner-placement.test.ts covers it)", |
| 76 | 76 | "/claim/start/:token": "needs a one-time start claim token, which the seed does not mint (test/start.test.ts covers it)", |
| 77 | "/join/:token": "needs a one-use link to work on a repository, which the seed does not mint (test/invite-contributors.test.ts covers it)", | |
| 77 | 78 | "/signin/access/:token": "needs a one-time access link an admin minted", |
| 78 | 79 | "/tell/share/:id": "needs a receipt for a share sent by the signed-in person, which the seed has none of", |
| 79 | 80 | "/pricing/orders/:id": "needs an order id the buyer's cookie names; the seed has no order (test/billing-order-receipt.test.ts covers it)", |
new above
offline