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

/explore.atom's `updated` is the newest entry's time, so a reader sees no change until there is one #13910

Merged opened this

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

Merged

mason/forge:mason/13885-explore-atom-s-updated-is-the-newest-ent → main 8 commits fork 3 threads · 3 unresolved

  • Merged. mason/13885-explore-atom-s-updated-is-the-newest-ent was merged into main as e5727fa.

Review

Sign in to review this change.
8 files changed +97 −20
src/core/feed_epoch.ts added +15
@@ -0,0 +1,15 @@
1 import type { Database } from "bun:sqlite";
2 import { repoCreatedAt } from "./repos.ts";
3
4 /**
5 * The fixed time an empty Atom feed says it was updated: the repository's
6 * creation time, or with no repository the forge's (its first account), or
7 * the epoch on a forge with none. Never "now", so a reader sees no change.
8 */
9 export function feedEpoch(db: Database, repoId?: string): string {
10 const at =
11 repoId === undefined
12 ? db.query<{ at: string | null }, []>("SELECT MIN(created_at) AS at FROM user").get()?.at
13 : repoCreatedAt(db, repoId);
14 return at ?? "1970-01-01T00:00:00Z";
15 }
src/core/repos.ts +5
@@ -1564,3 +1564,8 @@ export function listAdminOwnedRepos(db: Database): Repo[] {
15641564 .all()
15651565 .map(toRepo);
15661566 }
1567
1568 /** When a repository was created, or undefined when it does not exist. */
1569 export function repoCreatedAt(db: Database, repoId: string): string | undefined {
1570 return db.query<{ at: string }, [string]>("SELECT created_at AS at FROM repo WHERE id = ?1").get(repoId)?.at;
1571 }
src/http/atom_routes.ts +6 −5
@@ -8,12 +8,12 @@
88
99 import type { Database } from "bun:sqlite";
1010 import { getIssuesByIds, type Issue } from "../core/issues.ts";
11 import { feedEpoch } from "../core/feed_epoch.ts";
1112 import { canRead, getRepo, getRepoById, repoDatesFor, visibleForks, type Repo } from "../core/repos.ts";
1213 import { notePhase, takeSessionTime, timePhase } from "../core/route_timing.ts";
1314 import { getUserByName, hiddenFrom } from "../core/users.ts";
1415 import { plainTitle } from "../story/gather.ts";
1516 import { holds, stamp, xml } from "../web/feeds.ts";
16 import { STARTED } from "../web/started.ts";
1717 import { ownerProfile } from "../web/pages/org.ts";
1818 import type { Ctx, Handler } from "./context.ts";
1919 import { notFound } from "./respond.ts";
@@ -61,7 +61,7 @@ export function postsBy(db: Database, authorId: string, limit: number): Issue[]
6161 return ids.flatMap((id) => (issues.has(id) ? [issues.get(id)!] : []));
6262 }
6363
64 function render(ctx: Ctx, f: { cache?: string | undefined; vary?: boolean; path: string; html: string; title: string; subtitle: string; items: Item[] }): Response {
64 function render(ctx: Ctx, f: { repoId?: string; cache?: string | undefined; vary?: boolean; path: string; html: string; title: string; subtitle: string; items: Item[] }): Response {
6565 const origin = ctx.url.origin;
6666 const etag = `W/"${Bun.hash(JSON.stringify([f.title, f.subtitle, f.items])).toString(36)}"`;
6767 const headers: Record<string, string> = { ...HEADERS, etag };
@@ -82,7 +82,7 @@ function render(ctx: Ctx, f: { cache?: string | undefined; vary?: boolean; path:
8282 <id>${xml(origin + f.path)}</id>
8383 <title>${xml(f.title)}</title>
8484 <subtitle>${xml(f.subtitle)}</subtitle>
85 <updated>${xml(stamp(f.items[0]?.at ?? STARTED))}</updated>
85 <updated>${xml(stamp(f.items[0]?.at ?? feedEpoch(ctx.app.db, f.repoId)))}</updated>
8686 <link rel="self" type="application/atom+xml" href="${xml(origin + f.path)}"/>
8787 <link rel="alternate" type="text/html" href="${xml(origin + f.html)}"/>
8888 <generator>forge</generator>
@@ -105,7 +105,7 @@ export function shipsAtom(ctx: Ctx): Response {
105105 author: issue.originAuthor ?? issue.authorName,
106106 link: `${ctx.url.origin}/${slug}/pulls/${issue.number}`,
107107 }));
108 return render(ctx, { cache: repo.visibility === "public" ? undefined : "private, no-store", vary: true, path: `/${slug}/ships.atom`, html: `/${slug}`, title: `${slug} ships`, subtitle: repo.description === "" ? "What shipped" : repo.description, items });
108 return render(ctx, { repoId: repo.id, cache: repo.visibility === "public" ? undefined : "private, no-store", vary: true, path: `/${slug}/ships.atom`, html: `/${slug}`, title: `${slug} ships`, subtitle: repo.description === "" ? "What shipped" : repo.description, items });
109109 }
110110
111111 /** `GET /:owner/:name/forks.atom`: the newest forks the asker may read, by when they were made. */
@@ -117,8 +117,9 @@ export function forksAtom(ctx: Ctx): Response {
117117 const items = timePhase(ctx.request, "forks", () => {
118118 const forks = visibleForks(ctx.app.db, repo.id, ctx.auth.user);
119119 const dates = repoDatesFor(ctx.app.db, forks.map((f) => f.id));
120 const epoch = feedEpoch(ctx.app.db);
120121 return forks
121 .map((f) => ({ f, at: dates.get(f.id)?.createdAt ?? STARTED }))
122 .map((f) => ({ f, at: dates.get(f.id)?.createdAt ?? epoch }))
122123 .sort((a, b) => Date.parse(stamp(b.at)) - Date.parse(stamp(a.at)))
123124 .slice(0, FORK_ENTRIES)
124125 .map(({ f, at }) => ({
src/web/feeds.ts +6 −5
@@ -19,6 +19,7 @@ import { eventActor, eventVerb } from "../design/activity.ts";
1919 import { audienceFilter } from "../core/event_audience.ts";
2020 import { listEvents, type ForgeEvent } from "../core/events.ts";
2121 import { getIssuesByRef } from "../core/issues.ts";
22 import { feedEpoch } from "../core/feed_epoch.ts";
2223 import { canRead, forkParentFor, getRepo, repoDatesFor } from "../core/repos.ts";
2324 import { explore, type ExploreQuery, type ExploreRow } from "../actions/explore.ts";
2425 import { getUsersByIds } from "../core/users.ts";
@@ -31,7 +32,6 @@ import { shippedAnswer } from "../release/answer.ts";
3132 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts";
3233 import type { Ctx } from "../http/context.ts";
3334 import { notFound } from "../http/respond.ts";
34 import { STARTED } from "./started.ts";
3535
3636 const FEED_KINDS = new Set([
3737 "push",
@@ -173,7 +173,7 @@ export function activityAtom(ctx: Ctx): Response {
173173 </entry>`;
174174 });
175175
176 const updated = events[0]?.createdAt ?? STARTED;
176 const updated = events[0]?.createdAt ?? feedEpoch(ctx.app.db, repo.id);
177177 const body = `<?xml version="1.0" encoding="utf-8"?>
178178 <feed xmlns="http://www.w3.org/2005/Atom">
179179 <id>${xml(base)}/activity.atom</id>
@@ -294,7 +294,7 @@ export async function changelogAtom(ctx: Ctx): Promise<Response> {
294294 <summary>${xml(`Landed in ${l.repo} as pull #${l.number} on ${stamp(l.at).slice(0, 10)} and is live.`)}</summary>
295295 </entry>`];
296296 });
297 const updated = live[0]?.at ?? STARTED;
297 const updated = live[0]?.at ?? feedEpoch(ctx.app.db);
298298 const body = `<?xml version="1.0" encoding="utf-8"?>
299299 <feed xmlns="http://www.w3.org/2005/Atom">
300300 <id>${xml(origin)}/changelog.atom</id>
@@ -346,8 +346,9 @@ export async function exploreAtom(ctx: Ctx): Promise<Response> {
346346 return { row, repo };
347347 });
348348 const dates = repoDatesFor(ctx.app.db, made.flatMap((m) => (m.repo === undefined ? [] : [m.repo.id])));
349 const epoch = feedEpoch(ctx.app.db);
outdated

feedEpoch runs a SQL query for every entry without a repo, inside the map. Compute it once before the map (lazily if wanted) and reuse it.

349350 const dated = made
350 .map((m) => ({ ...m, at: (m.repo === undefined ? undefined : dates.get(m.repo.id)?.createdAt) ?? STARTED }))
351 .map((m) => ({ ...m, at: (m.repo === undefined ? undefined : dates.get(m.repo.id)?.createdAt) ?? epoch }))
351352 .sort((a, b) => Date.parse(stamp(b.at)) - Date.parse(stamp(a.at)));
352353 const etag = `W/"${Bun.hash(JSON.stringify(dated.map((m) => [m.row.url, m.row.description, m.row.numbers, m.at]))).toString(36)}"`;
353354 const headers = { etag, "cache-control": "public, max-age=300" };
@@ -360,7 +361,7 @@ export async function exploreAtom(ctx: Ctx): Promise<Response> {
360361 <link rel="alternate" type="text/html" href="${xml(`${origin}${row.url}`)}"/>
361362 <summary>${xml(exploreSummary(row))}</summary>
362363 </entry>`);
363 const updated = dated[0]?.at ?? STARTED;
364 const updated = dated[0]?.at ?? epoch;
364365 const body = `<?xml version="1.0" encoding="utf-8"?>
365366 <feed xmlns="http://www.w3.org/2005/Atom">
366367 <id>${xml(origin)}/explore.atom</id>
src/web/pages/waves.ts +2 −2
@@ -6,12 +6,12 @@
66 * it does for any other page of a private repository.
77 */
88
9 import { feedEpoch } from "../../core/feed_epoch.ts";
910 import { canRead, getRepo } from "../../core/repos.ts";
1011 import { mayDirectCrew } from "../../crew/direct.ts";
1112 import { notFound } from "../../http/respond.ts";
1213 import { waitingKeys } from "../../release/landings.ts";
1314 import { holds, stamp, xml } from "../feeds.ts";
14 import { STARTED } from "../started.ts";
1515 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
1616 import { repoDir } from "../../git/repos.ts";
1717 import type { Ctx } from "../../http/context.ts";
@@ -167,7 +167,7 @@ export async function wavesAtom(ctx: Ctx): Promise<Response> {
167167 <id>${xml(base)}/waves.atom</id>
168168 <title>${xml(`${repo.ownerName}/${repo.name} waves`)}</title>
169169 <subtitle>${xml(repo.description === "" ? "Waves" : repo.description)}</subtitle>
170 <updated>${xml(newest ? updatedAt(newest.date) : STARTED)}</updated>
170 <updated>${xml(newest ? updatedAt(newest.date) : feedEpoch(ctx.app.db, repo.id))}</updated>
171171 <link rel="self" type="application/atom+xml" href="${xml(base)}/waves.atom"/>
172172 <link rel="alternate" type="text/html" href="${xml(base)}/waves"/>
173173 <generator>forge</generator>
src/web/started.ts deleted −2
@@ -1,2 +0,0 @@
1 /** When this process began serving: taken once, so an empty feed's `<updated>` is stable and its ETag holds. */
2 export const STARTED = new Date().toISOString();
test/atom-feeds.test.ts +57 −3
@@ -13,6 +13,7 @@ import { FORK_ENTRIES } from "../src/http/atom_routes.ts";
1313 import { createUser, getUserByName } from "../src/core/users.ts";
1414 import { serve, type Servers } from "../src/http/server.ts";
1515 import { writeWave } from "../src/story/wave.ts";
16 import { feedEpoch } from "../src/core/feed_epoch.ts";
1617 import { xml } from "../src/web/feeds.ts";
1718 import { checkAtom } from "./helpers/atom.ts";
1819
@@ -76,6 +77,59 @@ describe("atom feeds", () => {
7677 });
7778 }
7879
80 const updatedOf = (xmlText: string) => /<\/subtitle>\s*(?:<author>.*?<\/author>\s*)?<updated>([^<]+)</s.exec(xmlText)?.[1];
81
82 test("an empty feed's updated is the repository's creation time, byte-equal on repeat, and 304s", async () => {
83 const owner = getUserByName(app.db, "alice")!;
84 const made = await createRepo(app, owner, { name: "empty", visibility: "public", description: "" });
85 if (!made.ok) throw new Error(String(made.error));
86 const url = `${servers.url}/alice/empty/ships.atom`;
87 const first = await fetch(url);
88 const text = await first.text();
89 const created = feedEpoch(app.db, made.value.id);
90 expect(updatedOf(text)).toBe(created);
91 await Bun.sleep(20); // real-timer: a "now" stamp would differ across it
outdated

This test passes on the old code too (STARTED was stable across 20ms), so it would not fail without the change. Assert the empty explore and changelog feeds' updated equals feedEpoch(app.db) exactly.

92 const second = await fetch(url);
93 expect(await second.text()).toBe(text);
94 expect(second.headers.get("etag")).toBe(first.headers.get("etag"));
95 const again = await fetch(url, { headers: { "if-none-match": first.headers.get("etag")! } });
96 expect(again.status).toBe(304);
97 });
98
99 test("empty explore and changelog feeds say updated is the forge's first account time, fixed, not now", async () => {
100 const emptyDir = await mkdtemp(join(tmpdir(), "forge-atom-empty-"));
101 const empty = createApp({ dataDir: emptyDir, port: 0 });
102 const emptyServers = await serve(empty);
103 try {
104 const u = await createUser(empty.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
105 if (!u.ok) throw new Error(u.error);
106 const epoch = feedEpoch(empty.db);
107 expect(epoch).toBe(getUserByName(empty.db, "bob")!.createdAt);
108 for (const path of ["/explore.atom", "/changelog.atom"]) {
109 const a = await fetch(emptyServers.url + path);
110 const body = await a.text();
111 expect(body).not.toContain("<entry>");
112 expect(/<updated>([^<]+)</.exec(body)![1]).toBe(epoch);
113 await Bun.sleep(20); // real-timer: a "now" stamp would differ across it
114 const b = await fetch(emptyServers.url + path);
115 expect(await b.text()).toBe(body);
116 const etag = a.headers.get("etag")!;
117 expect((await fetch(emptyServers.url + path, { headers: { "if-none-match": etag } })).status).toBe(304);
118 }
119 } finally {
120 emptyServers.stop();
121 empty.close();
122 await rm(emptyDir, { recursive: true, force: true });
123 }
124 });
125
126 test("a feed with entries takes updated from its newest entry", async () => {
127 const res = await fetch(`${servers.url}/alice.atom`);
128 const text = await res.text();
129 const entry = /<entry>[\s\S]*?<updated>([^<]+)</.exec(text)![1];
130 expect(updatedOf(text)).toBe(entry!);
131 });
132
79133 test("characters XML forbids are dropped, not emitted", () => {
80134 expect(xml("a\u0000b\u0008c\u001fd&e")).toBe("abcd&amp;e");
81135 });
@@ -147,11 +201,11 @@ describe("forks.atom", () => {
147201
148202 test("the feed is capped at FORK_ENTRIES", async () => {
149203 const root = getRepo(app.db, "alice", "app")!;
204 // Accounts without a password skip argon2, which is what made this slow.
150205 for (let i = 0; i < FORK_ENTRIES + 3; i++) {
151 const u = await createUser(app.db, { origin: "seed", trusted: true, username: `forker${i}`, email: `forker${i}@f.dev`, password: "a-good-passphrase" });
206 const u = await createUser(app.db, { origin: "seed", trusted: true, username: `forker${i}`, email: `forker${i}@f.dev` });
152207 if (!u.ok) throw new Error(u.error);
153 const alice = getUserByName(app.db, "alice")!;
154 const promoted = setRole(app, alice, `forker${i}`, "member");
208 const promoted = setRole(app, getUserByName(app.db, "alice")!, `forker${i}`, "member");
155209 if (!promoted.ok) throw new Error(promoted.error);
156210 const r = await forkRepo(app, u.value, root, `many${i}`);
157211 if (!r.ok) throw new Error(String(r.error));
test/changelog-atom.test.ts +6 −3
@@ -10,6 +10,7 @@ import { createApp, type App } from "../src/app.ts";
1010 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1111 import { createUser, getUserByName } from "../src/core/users.ts";
1212 import { forgetLive, keepLive } from "../src/release/landings.ts";
13 import { feedEpoch } from "../src/core/feed_epoch.ts";
1314 import { getRepo } from "../src/core/repos.ts";
1415 import { writeWave } from "../src/story/wave.ts";
1516 import { checkAtom } from "./helpers/atom.ts";
@@ -234,15 +235,17 @@ describe("GET /changelog.atom", () => {
234235 });
235236
236237 describe("GET /changelog.atom on an empty forge", () => {
237 test("says when the process started, not 1970, and the same twice", async () => {
238 test("says when the forge was made (its first account), not now, and the same twice", async () => {
238239 const dataDir = await mkdtemp(join(tmpdir(), "forge-changelog-atom-empty-"));
239240 const app = createApp({ dataDir, port: 0, forgeRepo: "alice/forge" });
240241 const servers = await serve(app);
241242 try {
243 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
244 if (!made.ok) throw new Error(made.error);
242245 const get = async () => (await (await fetch(`${servers.url}/changelog.atom`)).text()).match(/<updated>([^<]*)<\/updated>/)![1]!;
243246 const first = await get();
244 expect(first).not.toContain("1970-01-01");
245 expect(Date.parse(first)).toBeGreaterThan(Date.parse("2026-01-01"));
247 expect(first).toBe(feedEpoch(app.db));
248 await Bun.sleep(20); // real-timer: a "now" stamp would differ across it
246249 expect(await get()).toBe(first);
247250 } finally {
248251 servers.stop();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs