Public: anyone can read this; the crew can change it.
A fork's follow mode is one line on its page: follow automatically or only propose #15127
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15089-a-fork-s-follow-mode-is-one-line-on-its → main 5 commits fork 3 threads · 3 unresolved
docs/features/fork-follow-undo.md added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | --- | |
| 2 | topic: Ship and undo | |
| 3 | row: A fork's follow mode in one tap | |
| 4 | --- | |
| 5 | # A fork's follow mode in one tap (#15089) | |
| 6 | ||
| 7 | On a fork's `/following` card the follow mode switches in one tap; only "Don't follow" keeps its confirm step, because it severs the upstream. After a change the card shows a receipt ("Changed from “…” <time>.") with an Undo button that restores the previous mode. The receipt stands only while that change is the latest one. | |
| 8 | ||
| 9 | - Web: the card in `src/web/pages/following.ts`; the previous mode is recorded in the `forge.follow_mode` event (`was`) and read by `lastModeChange` in `src/actions/follow.ts`. | |
| 10 | - Terminal: `src/tui/screens/forks.ts` shows the same receipt as a row ("changed from “…” <ago>") and `z` undoes it, for anyone who may press `m`. The page puts the receipt on its own muted line under the mode line. | |
| 11 | - Final: on a repository that follows an address rather than a parent, "Don't follow" severs the upstream for good, so it leaves no receipt and no Undo (`lastModeChange`); a fork's own "Don't follow" is just a mode and can be undone. | |
| 12 | - Admins only; everyone else sees the standing line without buttons. |
src/actions/follow.ts +18 −1
| @@ -17,6 +17,7 @@ import { FAMILY_WORDS } from "./family_words.ts"; | ||
| 17 | 17 | import { FOLLOWS_NOTHING, FOLLOW_LINE, agoWords, driftWords, followModeWords, followStandingLine, lastFollowClause } from "./follow_words.ts"; |
| 18 | 18 | import { FORK_FOLLOW_MODES, forkFollowMode, lastTookAt, isForkFollowMode, setForkFollowMode, type ForkFollowMode } from "../core/fork_follow.ts"; |
| 19 | 19 | import { lastFollow } from "../core/last_follow.ts"; |
| 20 | import { listEvents } from "../core/events.ts"; | |
| 20 | 21 | import { requireUser } from "./access.ts"; |
| 21 | 22 | import { getFollow, setFollowTaking } from "../core/upstream_follow.ts"; |
| 22 | 23 | import { isChecking, severUpstream } from "./upstream_follow.ts"; |
| @@ -245,6 +246,7 @@ export async function setFollowMode( | ||
| 245 | 246 | // After the admin check, so anyone else gets the same 403 and learns nothing of a check. |
| 246 | 247 | if (isChecking(repo.id)) return err("updating"); |
| 247 | 248 | if (!isForkFollowMode(mode)) return err("invalid_mode"); |
| 249 | const was = await followModeOf(app, repo); | |
| 248 | 250 | if (upstream !== undefined) { |
| 249 | 251 | if (upstream.severedAt !== null) return err("severed"); |
| 250 | 252 | if (mode === "dont-follow") await severUpstream(app, who.value, repo); |
| @@ -260,11 +262,26 @@ export async function setFollowMode( | ||
| 260 | 262 | kind: "forge.follow_mode", |
| 261 | 263 | repoId: repo.id, |
| 262 | 264 | actorId: who.value.id, |
| 263 | payload: { repo: `${repo.ownerName}/${repo.name}`, mode }, | |
| 265 | payload: { repo: `${repo.ownerName}/${repo.name}`, mode, was }, | |
| 264 | 266 | }); |
| 265 | 267 | return ok(mode); |
| 266 | 268 | } |
| 267 | 269 | |
| 270 | /** | |
| 271 | * The last mode change of a repository that is still the standing one, for the page's receipt and | |
| 272 | * its Undo: what it was before, and when. Undefined when none was recorded, a later change | |
| 273 | * (which has its own receipt) came after, or the change was to "dont-follow" on a repository | |
| 274 | * that follows an address rather than a parent: that severs the upstream, and setFollowMode | |
| 275 | * refuses every mode after it ("severed"), so there is nothing to undo. A fork's own | |
| 276 | * "dont-follow" is only a stored mode and can be undone. | |
| 277 | */ | |
outdated
lastModeChange returns a receipt for a change to dont-follow, whose undo is refused after the sever. Return undefined for it, or flag it as not undoable. |
||
| 278 | export function lastModeChange(app: App, repo: Repo, now: ForkFollowMode): { was: ForkFollowMode; at: string } | undefined { | |
| 279 | const last = listEvents(app.db, { repoId: repo.id, kind: "forge.follow_mode", limit: 1 })[0]; | |
| 280 | const p = last?.payload as { mode?: unknown; was?: unknown } | undefined; | |
| 281 | if (last === undefined || p === undefined || p.mode !== now || (now === "dont-follow" && repo.forkParentId === null) || !isForkFollowMode(p.was) || p.was === now) return undefined; | |
| 282 | return { was: p.was, at: last.createdAt }; | |
| 283 | } | |
| 284 | ||
| 268 | 285 | /** The sentence every door answers a mode change with. */ |
| 269 | 286 | export function followModeSaid(parent: Repo | undefined, mode: ForkFollowMode): string { |
| 270 | 287 | const upstream = parent === undefined ? "its parent" : `${parent.ownerName}/${parent.name}`; |
src/tui/screens/forks.ts +19 −2
| @@ -22,7 +22,7 @@ import { | ||
| 22 | 22 | type ForkRelation, |
| 23 | 23 | } from "../../actions/forks.ts"; |
| 24 | 24 | import { FOLLOW_CHOICES, FOLLOW_PHRASE, FOLLOWS_NOTHING, followChoiceLine, proposeRefusalWords } from "../../actions/follow_words.ts"; |
| 25 | import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts"; | |
| 25 | import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, lastModeChange, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts"; | |
| 26 | 26 | import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts"; |
| 27 | 27 | import { convergeFollowed, followedAs, followedOf } from "../../actions/upstream_sync.ts"; |
| 28 | 28 | import { forkOffers, sentOffers } from "../../core/fork_offers.ts"; |
| @@ -107,6 +107,7 @@ export function followingRows( | ||
| 107 | 107 | relation: ForkRelation | undefined, |
| 108 | 108 | converge: Converge | undefined, |
| 109 | 109 | mode?: ForkFollowMode, |
| 110 | undo?: { was: ForkFollowMode; at: string }, | |
| 110 | 111 | ): ForkRow[] { |
| 111 | 112 | if (relation === undefined) return []; |
| 112 | 113 | const { parent } = relation; |
| @@ -128,6 +129,9 @@ export function followingRows( | ||
| 128 | 129 | heading: "", |
| 129 | 130 | }, |
| 130 | 131 | ...(mode === undefined ? [] : [{ repo: parent, name: ` ${FOLLOW_CHOICES[mode].sentence}`, words: "", heading: "" }]), |
| 132 | ...(undo === undefined | |
| 133 | ? [] | |
| 134 | : [{ repo: parent, name: ` changed from “${FOLLOW_CHOICES[undo.was].label}” ${agoWords(undo.at)}`, words: "z undoes it", heading: "" }]), | |
| 131 | 135 | ...(converge === undefined |
| 132 | 136 | ? [] |
| 133 | 137 | : [ |
| @@ -189,6 +193,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 189 | 193 | let followed: ReturnType<typeof followedAs> | undefined; |
| 190 | 194 | let open: Pull | undefined; |
| 191 | 195 | let mode: ForkFollowMode | undefined; |
| 196 | let undo: { was: ForkFollowMode; at: string } | undefined; | |
| 192 | 197 | /** The one plain line the page and MCP say: how this fork follows, and when it last took. */ |
| 193 | 198 | let standing: string | undefined; |
| 194 | 199 | /** |
| @@ -264,7 +269,8 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 264 | 269 | standing = (await followWords(f.app, repo, f.user)) ?? (await followStanding(f.app, repo, f.user)); |
| 265 | 270 | failed = getRepoById(f.db, repo.id)?.origin?.fetchError ?? ""; |
| 266 | 271 | mode = relation === undefined ? undefined : await followModeOf(f.app, repo); |
| 267 | following = followingRows(relation, converge, mode); | |
| 272 | undo = mode === undefined ? undefined : lastModeChange(f.app, repo, mode); | |
| 273 | following = followingRows(relation, converge, mode, undo); | |
| 268 | 274 | const here = forkOffers(f.db, repo, f.user); |
| 269 | 275 | const sizes = await offerSizes(f.app, repo, here.map((o) => o.number)); |
| 270 | 276 | offered = here.map((o) => ({ |
| @@ -356,6 +362,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 356 | 362 | ? [binding(["u"], open === undefined ? "propose upstream" : "open proposal")] |
| 357 | 363 | : []), |
| 358 | 364 | ...(canSetMode() ? [binding(["m"], "follow mode")] : []), |
| 365 | ...(canSetMode() && undo !== undefined ? [binding(["z"], "undo mode change")] : []), | |
| 359 | 366 | binding(["esc"], "close"), |
| 360 | 367 | ]; |
| 361 | 368 | }, |
| @@ -439,6 +446,16 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 439 | 446 | }); |
| 440 | 447 | return true; |
| 441 | 448 | } |
| 449 | if (e.key === "z" && canSetMode() && undo !== undefined) { | |
| 450 | const back = undo.was; | |
| 451 | runAsync(ctx, async () => { | |
| 452 | const set = await setFollowMode(f.app, f.user, repo, back); | |
| 453 | if (!set.ok) return ctx.toast(FOLLOW_MODE_REFUSALS[set.error].message, "danger"); | |
| 454 | await read(); | |
| 455 | ctx.toast(followChoiceLine(set.value), "success"); | |
| 456 | }); | |
| 457 | return true; | |
| 458 | } | |
| 442 | 459 | if (e.key === "esc" || e.key === "q") { |
| 443 | 460 | ctx.pop(); |
| 444 | 461 | return true; |
src/web/pages/following.ts +19 −6
| @@ -37,10 +37,10 @@ import { | ||
| 37 | 37 | type ParentReleases, |
| 38 | 38 | } from "../../core/parent_releases.ts"; |
| 39 | 39 | import { running } from "../../kernel/version.ts"; |
| 40 | import { FOLLOW_SWITCH_LABELS, followModeWords } from "../../actions/follow_words.ts"; | |
| 40 | import { FOLLOW_CHOICES, FOLLOW_SWITCH_LABELS, followModeWords } from "../../actions/follow_words.ts"; | |
| 41 | 41 | import type { ForkFollowMode } from "../../core/fork_follow.ts"; |
| 42 | 42 | import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts"; |
| 43 | import { convergeWords, followModeOf, takeOrTell, takeWords } from "../../actions/follow.ts"; | |
| 43 | import { convergeWords, followModeOf, lastModeChange, takeOrTell, takeWords } from "../../actions/follow.ts"; | |
| 44 | 44 | import { FOLLOW_WORD, FOLLOWS_NOTHING, HIDDEN_FORK_WORD, proposeRefusalWords } from "../../actions/follow_words.ts"; |
| 45 | 45 | import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts"; |
| 46 | 46 | import { openProposal, proposeUpstream } from "../../actions/upstream.ts"; |
| @@ -451,19 +451,32 @@ export function followingCard( | ||
| 451 | 451 | prose.push(html`<p class="muted small">${relation.words}</p>`); |
| 452 | 452 | if (foot !== null) prose.push(html`<p class="muted small">${foot}</p>`); |
| 453 | 453 | const words = followModeWords(following.mode, parentName); |
| 454 | const last = lastModeChange(ctx.app, repo, following.mode); | |
| 455 | const mayChange = canAdmin(ctx.app.db, repo, ctx.auth.user) && writer; | |
| 456 | const undone = | |
| 457 | last === undefined | |
Principle 3: the receipt and Undo share the line with the mode line and two buttons; on a 360px phone this wraps messily. Give the receipt its own muted line. Also, when last.was is reached from a dont-follow change, Undo posts a mode that setFollowMode refuses with 'severed'; hide Undo in that case. |
||
| 458 | ? null | |
| 459 | : html`<p class="muted small" data-follow-receipt>Changed from “${FOLLOW_CHOICES[last.was].label}” ${time(last.at)}.${ | |
| 460 | mayChange | |
| 461 | ? html` <form method="post" action="${base}/following/mode/${last.was}" class="inline">${btn("Undo", { variant: "outline" })}</form>` | |
| 462 | : null | |
| 463 | }</p>`; | |
| 454 | 464 | prose.push( |
| 455 | 465 | html`<p data-follow-mode="${following.mode}"> |
| 456 | 466 | <strong>${words.line}</strong>. |
| 457 | 467 | ${canAdmin(ctx.app.db, repo, ctx.auth.user) && writer |
| 458 | 468 | ? words.others.map((m) => |
| 459 | btn(FOLLOW_SWITCH_LABELS[m], { | |
| 460 | href: `/-/confirm?to=${encodeURIComponent(`${base}/following/mode/${m}`)}`, | |
| 461 | size: "sm", | |
| 462 | }), | |
| 469 | m === "dont-follow" | |
| 470 | ? btn(FOLLOW_SWITCH_LABELS[m], { | |
| 471 | href: `/-/confirm?to=${encodeURIComponent(`${base}/following/mode/${m}`)}`, | |
| 472 | size: "sm", | |
| 473 | }) | |
| 474 | : html`<form method="post" action="${base}/following/mode/${m}" class="inline">${btn(FOLLOW_SWITCH_LABELS[m], { size: "sm" })}</form>`, | |
| 463 | 475 | ) |
| 464 | 476 | : null} |
| 465 | 477 | </p>`, |
| 466 | 478 | ); |
| 479 | if (undone !== null) prose.push(undone); | |
| 467 | 480 | if (followers > 0 && o.family !== false) |
| 468 | 481 | rows.push( |
| 469 | 482 | row({ |
test/fork-follow-mode.test.ts +43 −1
| @@ -2,13 +2,14 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | ||
| 2 | 2 | import { mkdtemp, rm } from "node:fs/promises"; |
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | import { setFollowMode } from "../src/actions/follow.ts"; | |
| 5 | import { lastModeChange, setFollowMode } from "../src/actions/follow.ts"; | |
| 6 | 6 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; |
| 7 | 7 | import { createApp, type App } from "../src/app.ts"; |
| 8 | 8 | import { forkFollowMode } from "../src/core/fork_follow.ts"; |
| 9 | 9 | import { forkRelation } from "../src/core/following.ts"; |
| 10 | 10 | import { getRepo, type Repo } from "../src/core/repos.ts"; |
| 11 | 11 | import { listEvents } from "../src/core/events.ts"; |
| 12 | import { putFollow } from "../src/core/upstream_follow.ts"; | |
| 12 | 13 | import { createUser, type User } from "../src/core/users.ts"; |
| 13 | 14 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 14 | 15 | import { forksScreen } from "../src/tui/screens/forks.ts"; |
| @@ -181,4 +182,45 @@ describe("the terminal", () => { | ||
| 181 | 182 | expect(text).not.toContain("follow mode"); |
| 182 | 183 | expect(forkFollowMode(app.db, fork.id)).toBe("tell-me"); |
| 183 | 184 | }); |
| 185 | ||
| 186 | test("z undoes the last mode change, and a change to dont-follow offers no undo", async () => { | |
| 187 | must(await setFollowMode(app, users["bea"]!, fork, "tell-me")); | |
| 188 | must(await setFollowMode(app, users["bea"]!, fork, "take-updates")); | |
| 189 | const shown = await open("bea", ""); | |
| 190 | expect(shown).toContain("changed from “Follow and ask”"); | |
| 191 | expect(shown).toContain("undo mode change"); | |
| 192 | const undone = await open("bea", "z"); | |
| 193 | expect(forkFollowMode(app.db, fork.id)).toBe("tell-me"); | |
| 194 | // The undo is itself the latest change, so it carries its own receipt (undoing the undo). | |
| 195 | expect(undone).toContain("changed from “Follow and merge”"); | |
| 196 | expect(await open("cal", "")).not.toContain("undo mode change"); | |
| 197 | // Someone who cannot write presses z and nothing moves. | |
| 198 | must(await setFollowMode(app, users["bea"]!, fork, "take-updates")); | |
| 199 | await open("cal", "z"); | |
| 200 | expect(forkFollowMode(app.db, fork.id)).toBe("take-updates"); | |
| 201 | }); | |
| 202 | }); | |
| 203 | ||
| 204 | describe("undo and dont-follow", () => { | |
| 205 | test("a fork's own dont-follow is only a mode: it keeps its receipt and the undo works", async () => { | |
| 206 | const fay = must(await createUser(app.db, { origin: "seed", trusted: true, username: "fay", email: "[email protected]", password: "a-good-passphrase" })); | |
| 207 | promote(app.db, fay); | |
| 208 | const own = must(await forkRepo(app, fay, original, "yard-stop")); | |
| 209 | must(await setFollowMode(app, fay, own, "dont-follow")); | |
| 210 | expect(lastModeChange(app, own, "dont-follow")?.was).toBe("tell-me"); | |
| 211 | expect((await setFollowMode(app, fay, own, "tell-me")).ok).toBe(true); | |
| 212 | }); | |
| 213 | ||
| 214 | test("a repository that follows an address is severed by dont-follow: no receipt, and the undo would be refused", async () => { | |
| 215 | const gus = must(await createUser(app.db, { origin: "seed", trusted: true, username: "gus", email: "[email protected]", password: "a-good-passphrase" })); | |
| 216 | promote(app.db, gus); | |
| 217 | const own = must(await createRepo(app, gus, { name: "addr", visibility: "public" })); | |
| 218 | putFollow(app.db, { repoId: own.id, url: "http://127.0.0.1:9/o/x.git", branch: "main", enabled: false, byId: gus.id, originId: "" }); | |
| 219 | must(await setFollowMode(app, gus, own, "take-updates")); | |
| 220 | expect(lastModeChange(app, own, "take-updates")?.was).toBe("tell-me"); | |
| 221 | must(await setFollowMode(app, gus, own, "dont-follow")); | |
| 222 | expect(lastModeChange(app, own, "dont-follow")).toBeUndefined(); | |
| 223 | const again = await setFollowMode(app, gus, own, "tell-me"); | |
| 224 | expect(again.ok).toBe(false); | |
| 225 | }); | |
| 184 | 226 | }); |
test/fork-tree-web.test.ts +17 −8
| @@ -127,12 +127,10 @@ describe("the fork tree, on one forge", () => { | ||
| 127 | 127 | const before = await (await get("/bea/yard", cookie)).text(); |
| 128 | 128 | expect(before).toContain("Proposes updates from ann/yard"); |
| 129 | 129 | expect(before).not.toContain("automatically</strong>"); |
| 130 | expect(before).toContain("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Ftake-updates"); | |
| 131 | ||
| 132 | const confirm = await (await get("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Ftake-updates", cookie)).text(); | |
| 133 | expect(confirm).toContain("Follows ann/yard automatically?"); | |
| 134 | expect(confirm).toContain("the crew is told to converge"); | |
| 135 | expect(confirm).toContain('action="/bea/yard/following/mode/take-updates"'); | |
| 130 | // One tap: a form that posts the mode, no confirm step; only stopping still asks first. | |
| 131 | expect(before).toContain('action="/bea/yard/following/mode/take-updates"'); | |
| 132 | expect(before).toContain("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Fdont-follow"); | |
| 133 | expect(before).not.toContain("data-follow-receipt"); | |
| 136 | 134 | |
| 137 | 135 | const cross = await post("/bea/yard/following/mode/take-updates", cookie, {}, "https://evil.test"); |
| 138 | 136 | expect(cross.status).toBe(403); |
| @@ -150,12 +148,22 @@ describe("the fork tree, on one forge", () => { | ||
| 150 | 148 | const receipt = audits()[0]!; |
| 151 | 149 | expect(receipt.repoId).toBe(middle.id); |
| 152 | 150 | expect(receipt.actorId).toBe(bea.id); |
| 153 | expect(receipt.payload).toMatchObject({ repo: "bea/yard", mode: "take-updates" }); | |
| 151 | expect(receipt.payload).toMatchObject({ repo: "bea/yard", mode: "take-updates", was: "tell-me" }); | |
| 154 | 152 | |
| 155 | 153 | const after = await (await get("/bea/yard", cookie)).text(); |
| 156 | 154 | expect(after).toContain("Follows ann/yard automatically"); |
| 157 | 155 | expect(after).toContain('data-follow-mode="take-updates"'); |
| 158 | 156 | expect(after).toContain("Only propose updates"); |
| 157 | // The receipt names what it was, and Undo puts it back. | |
| 158 | expect(after).toContain("data-follow-receipt"); | |
| 159 | expect(after).toContain('action="/bea/yard/following/mode/tell-me"'); | |
| 160 | const readerPage = await (await get("/bea/yard", await signIn("cal"))).text(); | |
| 161 | expect(readerPage).toContain("data-follow-receipt"); | |
| 162 | expect(readerPage).not.toContain('action="/bea/yard/following/mode/tell-me"'); | |
| 163 | expect((await post("/bea/yard/following/mode/tell-me", cookie, {})).status).toBe(303); | |
| 164 | expect(forkFollowMode(app.db, middle.id)).toBe("tell-me"); | |
| 165 | expect(audits()[0]!.payload).toMatchObject({ mode: "tell-me", was: "take-updates" }); | |
| 166 | expect((await post("/bea/yard/following/mode/take-updates", cookie, {})).status).toBe(303); | |
| 159 | 167 | |
| 160 | 168 | // A reader sees the line but no switch. |
| 161 | 169 | const reader = await (await get("/bea/yard", await signIn("cal"))).text(); |
| @@ -188,7 +196,8 @@ describe("the fork tree, on one forge", () => { | ||
| 188 | 196 | expect(forks).not.toContain("forks follow this one"); |
| 189 | 197 | const now = forkFollowMode(app.db, middle.id); |
| 190 | 198 | const others = (["take-updates", "tell-me", "dont-follow"] as const).filter((m) => m !== now); |
| 191 | for (const m of others) expect(forks).toContain(`following%2Fmode%2F${m}"`); | |
| 199 | for (const m of others) | |
| 200 | expect(forks).toMatch(new RegExp(m === "dont-follow" ? `following%2Fmode%2F${m}"` : `action="/bea/yard/following/mode/${m}"`)); | |
| 192 | 201 | expect(forks).not.toContain(`following%2Fmode%2F${now}"`); |
| 193 | 202 | |
| 194 | 203 | const reader = await (await get("/bea/yard/forks", await signIn("cal"))).text(); |
The terminal does not show the receipt or an Undo; forks.ts only lists FOLLOW_CHOICES. Either build it there or say so here; as written the doc is wrong.