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

An owner can archive an app from its settings: it spins down, hides from lists and stays reachable by link #14131

opened this

Check · Review · Land · Live — Its check is queued. Next: reviewer, no review yet.

Ready to land

torque/forge:torque/14085-an-owner-can-archive-an-app-from-its-set → main 3 commits fork

Review

Sign in to review this change.
9 files changed +170 −34
docs/features/README.md +1
@@ -255,6 +255,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
255255 - [App templates](app-templates.md)
256256 - [Template page](template-page.md)
257257 - [Archive listing](archive-listing.md)
258 - [Archive an app: it spins down, hides from lists, stays reachable](app-archive.md)
258259 - [A binary file shows its size and type](binary-file.md)
259260 - [An OPML or RSS/Atom file opens as its entries](feed-xml.md)
260261 - [A big .log or .txt opens its last lines](log-tail.md)
docs/features/app-archive.md added +12
@@ -0,0 +1,12 @@
1 # Archive an app
2
3 An owner archives an app from its settings (Settings → Archive) or the app page's Archive link. The button opens the confirm page first, which says what will happen; nothing changes until its second button is pressed (a same-origin POST to `/:owner/:name/archive`).
4
5 Receipt: the next page says the app is stopped, hidden from home and `/apps`, and still opens by its address.
6
7 - **Stops:** production and every preview spin down (`moveArchive`, `src/actions/repos.ts`).
8 - **Hidden:** home and `/apps` leave it out; `/apps?archived=1` lists it, and `/apps` says how many are archived.
9 - **Reachable:** its URL, clone and push keep working. The page carries an "Archived on <date>" banner (`archivedLine`, `src/web/pages/following.ts`), with Restore for an admin.
10 - **Restore:** Settings, the banner or the archived list posts `archived=0` to `/:owner/:name/settings/archive`; the app is listed again and wakes on its next visit.
11
12 Terminal: the Archived repositories screen (`src/tui/screens/archived.ts`); MCP: the `archive` tool. Test: `test/app-archive.test.ts`.
src/tui/screens/archived.ts +1 −1
@@ -51,7 +51,7 @@ export function archivedScreen(f: Session, ctx: Ctx): Screen {
5151 const rows: ListRow[] = repos.map((r) => ({
5252 title: `${r.ownerName}/${r.name}`,
5353 secondary: (r.archivedAt ?? "").slice(0, 10),
54 meta: withApp.has(r.id) ? "app spun down" : "",
54 meta: withApp.has(r.id) ? "app spun down, still at its address" : "",
5555 }));
5656 const line = mayRestore(selected()) ? "enter opens it · a restores it" : "enter opens it";
5757 s.text(head!.x, head!.y, truncate(line, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
src/web/pages/following.ts +22
@@ -180,6 +180,28 @@ export function archivedParentLine(ctx: Ctx, repo: Repo): Html | null {
180180 </p>`;
181181 }
182182
183 /**
184 * "Archived on <date>" with, for an admin, the Restore button. A repository put
185 * away still clones, pushes and links as before; this only says so.
186 */
187 export function archivedLine(ctx: Ctx, repo: Repo): Html | null {
188 if (repo.archivedAt === null) return null;
189 return html`<div class="mt archived">
190 <p>
191 Archived on
192 <time datetime="${repo.archivedAt}">${repo.archivedAt.slice(0, 10)}</time>
193 </p>
194 ${when(
195 canAdmin(ctx.app.db, repo, ctx.auth.user),
196 () => html`<form method="post" action="${repoBase(repo)}/settings/archive">
197 <input type="hidden" name="archived" value="0" />
198 <input type="hidden" name="to" value="repo" />
199 ${btn("Restore", { size: "sm" })}
200 </form>`,
201 )}
202 </div>`;
203 }
204
183205 /**
184206 * "N releases behind <parent>", each with its name and link; or, when the
185207 * parent cannot be read, that it cannot and how old the last copy is.
src/web/pages/repo.ts +1 −22
@@ -46,6 +46,7 @@ import { FORK_DEFAULT_MODE, forkFollowModes, type ForkFollowMode } from "../../c
4646 import { FOLLOW_PHRASE, isSevered } from "../../actions/follow_words.ts";
4747 import { lineageOf, MINE_ANCHOR, ownsInTree } from "../../core/following.ts";
4848 import {
49 archivedLine,
4950 archivedParentLine,
5051 followingCard,
5152 followingOf,
@@ -1453,28 +1454,6 @@ function reportLine(ctx: Ctx, repo: Repo): Html | null {
14531454 </p>`;
14541455 }
14551456
1456 /**
1457 * "Archived on <date>" with, for an admin, the Restore button. A repository put
1458 * away still clones, pushes and links as before; this only says so.
1459 */
1460 function archivedLine(ctx: Ctx, repo: Repo): Html | null {
1461 if (repo.archivedAt === null) return null;
1462 return html`<div class="mt archived">
1463 <p>
1464 Archived on
1465 <time datetime="${repo.archivedAt}">${repo.archivedAt.slice(0, 10)}</time>
1466 </p>
1467 ${when(
1468 canAdmin(ctx.app.db, repo, ctx.auth.user),
1469 () => html`<form method="post" action="${repoBase(repo)}/settings/archive">
1470 <input type="hidden" name="archived" value="0" />
1471 <input type="hidden" name="to" value="repo" />
1472 ${btn("Restore", { size: "sm" })}
1473 </form>`,
1474 )}
1475 </div>`;
1476 }
1477
14781457 /** The code at a ref: the tree, the README and the About rail — with, in
14791458 * the studio, the offer to make a repository that is not an app one. */
14801459 async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
src/web/pages/settings.ts +15 −10
@@ -351,15 +351,16 @@ export async function settingsPage(
351351 const trail = provenanceCard(ctx, repo);
352352 const archive = html`<section class="settings-block">
353353 <h2>Archive</h2>
354 <form method="post" action="${base}/settings/archive">
355 <input type="hidden" name="archived" value="${repo.archivedAt === null ? "1" : "0"}" />
356 ${repo.archivedAt === null ? null : html`<p class="muted small">Archived on ${repo.archivedAt.slice(0, 10)}.</p>`}
357 <div class="form-actions">
358 ${btn(repo.archivedAt === null ? "Archive" : "Restore", {
359 variant: "primary",
360 })}
361 </div>
362 </form>
354 ${repo.archivedAt === null
355 ? html`<form method="get" action="/-/confirm">
356 <input type="hidden" name="to" value="${base}/archive" />
357 <div class="form-actions">${btn("Archive", { variant: "primary" })}</div>
358 </form>`
359 : html`<form method="post" action="${base}/settings/archive">
360 <input type="hidden" name="archived" value="0" />
361 <p class="muted small">Archived on ${repo.archivedAt.slice(0, 10)}.</p>
362 <div class="form-actions">${btn("Restore", { variant: "primary" })}</div>
363 </form>`}
363364 </section>`;
364365 const rename = html`<form id="rename" method="post" action="${base}/settings/rename">
365366 ${formField({
@@ -564,7 +565,11 @@ async function archiveWith(ctx: Ctx, fixed: "1" | undefined): Promise<Response>
564565 ctx,
565566 back,
566567 updated.ok
567 ? { message: archived ? "Archived." : "Restored." }
568 ? {
569 message: archived
570 ? "Archived: it is stopped and hidden from home and the apps list; its address still opens, with a banner. Restore it from its page or Settings."
571 : "Restored: it is back on home and the apps list, and wakes on its next visit.",
572 }
568573 : { tone: "danger", message: NOT_CHANGED },
569574 );
570575 }
src/web/studio/app.ts +3
@@ -38,6 +38,7 @@ import { appLine } from "../describe.ts";
3838 import { page } from "../layout.ts";
3939 import { liveClientJs, liveMount } from "../live.ts";
4040 import {
41 archivedLine,
4142 archivedParentLine,
4243 followingCard,
4344 followingOf,
@@ -518,6 +519,7 @@ export async function studioApp(
518519 ${following === undefined
519520 ? archivedParentLine(ctx, repo)
520521 : followingCard(ctx, repo, following)}
522 ${archivedLine(ctx, repo)}
521523 <div id="app-head">${head}</div>
522524 ${ownerFacts ? crewKeyCard(ctx, true) : null}
523525 ${ownerFacts ? landOnApp(ctx, repo) : null}
@@ -543,6 +545,7 @@ export async function studioApp(
543545 ${following === undefined
544546 ? archivedParentLine(ctx, repo)
545547 : followingCard(ctx, repo, following)}
548 ${archivedLine(ctx, repo)}
546549 <div id="app-head">${head}</div>
547550 ${askingLine(ctx, repo)}
548551 ${policy}
test/app-archive.test.ts added +112
@@ -0,0 +1,112 @@
1 /**
2 * Archive from settings (#14085): the button previews on the confirm page, the
3 * receipt says what happened, an archived app leaves home and /apps but opens
4 * by its address with a banner, and Restore brings it back.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtempSync, rmSync } from "node:fs";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { createApp as makeAppRecord } from "../src/core/apps.ts";
13 import { getRepo } from "../src/core/repos.ts";
14 import { createUser } from "../src/core/users.ts";
15 import { serve, type Servers } from "../src/http/server.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { proveAll } from "./helpers/verified.ts";
18
19 quietLogs();
20
21 let root = "";
22 let app: App;
23 let servers: Servers;
24 let cookie = "";
25 const ROW = '<a href="/alice/attic" class="row"';
26
27 beforeAll(async () => {
28 root = mkdtempSync(join(tmpdir(), "forge-app-archive-"));
29 app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" });
30 const u = await createUser(app.db, {
31 origin: "seed",
32 trusted: true,
33 username: "alice",
34 email: "[email protected]",
35 password: "a-good-passphrase",
36 adminIfFirst: true,
37 });
38 if (!u.ok) throw new Error(u.error);
39 proveAll(app);
40 const made = await createRepo(app, u.value, { name: "attic" });
41 if (!made.ok) throw new Error(made.error);
42 const rec = makeAppRecord(app.db, { repoId: made.value.id, slug: "attic", access: "public" });
43 if (!rec.ok) throw new Error(rec.error);
44 servers = await serve(app);
45 const signin = await fetch(`${servers.url}/signin`, {
46 method: "POST",
47 headers: { origin: servers.url },
48 body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }),
49 redirect: "manual",
50 });
51 cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!;
52 });
53 afterAll(() => {
54 servers.stop();
55 app.close();
56 rmSync(root, { recursive: true, force: true });
57 });
58
59 const get = async (path: string, extra = "") => {
60 const res = await fetch(`${servers.url}${path}`, { headers: { cookie: cookie + extra }, redirect: "manual" });
61 return { status: res.status, body: await res.text(), headers: res.headers };
62 };
63 const post = (path: string, body: Record<string, string>, origin = servers.url) =>
64 fetch(`${servers.url}${path}`, {
65 method: "POST",
66 headers: { cookie, origin },
67 body: new URLSearchParams(body),
68 redirect: "manual",
69 });
70
71 // The receipt rides a cookie set on the redirect; carry it to the next page.
72 const flashOf = (res: Response): string =>
73 res.headers.getSetCookie().map((c) => `; ${c.split(";")[0]!}`).join("");
74
75 test("hide, reach and restore, from settings", async () => {
76 // Preview: the settings button asks the confirm page; it changes nothing.
77 const settings = await get("/alice/attic/settings");
78 expect(settings.body).toContain('action="/-/confirm"');
79 expect(settings.body).toContain('value="/alice/attic/archive"');
80 expect(settings.body).not.toContain('action="/alice/attic/settings/archive"');
81 const preview = await get("/-/confirm?to=%2Falice%2Fattic%2Farchive");
82 expect(preview.body).toContain("Archive alice/attic?");
83 expect(getRepo(app.db, "alice", "attic")!.archivedAt).toBeNull();
84 expect((await get("/apps")).body).toContain(ROW);
85
86 // Do it: a cross-origin post is refused, the same-origin one archives.
87 expect((await post("/alice/attic/archive", {}, "https://evil.example")).status).toBeGreaterThanOrEqual(400);
88 expect(getRepo(app.db, "alice", "attic")!.archivedAt).toBeNull();
89 const done = await post("/alice/attic/archive", {});
90 expect(done.status).toBe(303);
91 expect(getRepo(app.db, "alice", "attic")!.archivedAt).not.toBeNull();
92
93 // Receipt: the next page says what happened.
94 const after = await get(new URL(done.headers.get("location")!, servers.url).pathname, flashOf(done));
95 expect(after.body).toContain("Archived: it is stopped and hidden");
96
97 // Hidden from the lists, reachable by address, with a banner.
98 expect((await get("/apps")).body).not.toContain(ROW);
99 const direct = await get("/alice/attic");
100 expect(direct.status).toBe(200);
101 expect(direct.body).toContain("Archived on");
102 expect((await get("/alice/attic/settings")).body).toContain("Restore");
103
104 // Restore: one post, a receipt, and back on the list.
105 const back = await post("/alice/attic/settings/archive", { archived: "0" });
106 expect(back.status).toBe(303);
107 expect(getRepo(app.db, "alice", "attic")!.archivedAt).toBeNull();
108 expect((await get("/apps")).body).toContain(ROW);
109 expect((await get("/alice/attic/settings")).body).not.toContain("Archived on");
110 const receipt = await get(new URL(back.headers.get("location")!, servers.url).pathname, flashOf(back));
111 expect(receipt.body).toContain("Restored: it is back on home");
112 });
test/settings.test.ts +3 −1
@@ -73,10 +73,12 @@ describe("repo settings", () => {
7373 for (const path of [
7474 "/settings",
7575 "/settings/wave",
76 "/settings/archive",
7776 "/settings/provenance",
7877 ])
7978 expect(page).toContain(posts(path));
79 // Archive asks first too (#14085): the button is a GET to the confirm page.
80 expect(page).toContain('action="/-/confirm"');
81 expect(page).toContain('value="/olive/proj/archive"');
8082 // Stopping the mirror asks first: the page links to the confirm, which posts.
8183 expect(page).toContain(`/-/confirm?to=${encodeURIComponent("/olive/proj/settings/mirror/stop")}`);
8284 for (const h of [

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs