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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13885-explore-atom-s-updated-is-the-newest-ent → main 8 commits fork 3 threads · 3 unresolved
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[] { | ||
| 1564 | 1564 | .all() |
| 1565 | 1565 | .map(toRepo); |
| 1566 | 1566 | } |
| 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 @@ | ||
| 8 | 8 | |
| 9 | 9 | import type { Database } from "bun:sqlite"; |
| 10 | 10 | import { getIssuesByIds, type Issue } from "../core/issues.ts"; |
| 11 | import { feedEpoch } from "../core/feed_epoch.ts"; | |
| 11 | 12 | import { canRead, getRepo, getRepoById, repoDatesFor, visibleForks, type Repo } from "../core/repos.ts"; |
| 12 | 13 | import { notePhase, takeSessionTime, timePhase } from "../core/route_timing.ts"; |
| 13 | 14 | import { getUserByName, hiddenFrom } from "../core/users.ts"; |
| 14 | 15 | import { plainTitle } from "../story/gather.ts"; |
| 15 | 16 | import { holds, stamp, xml } from "../web/feeds.ts"; |
| 16 | import { STARTED } from "../web/started.ts"; | |
| 17 | 17 | import { ownerProfile } from "../web/pages/org.ts"; |
| 18 | 18 | import type { Ctx, Handler } from "./context.ts"; |
| 19 | 19 | import { notFound } from "./respond.ts"; |
| @@ -61,7 +61,7 @@ export function postsBy(db: Database, authorId: string, limit: number): Issue[] | ||
| 61 | 61 | return ids.flatMap((id) => (issues.has(id) ? [issues.get(id)!] : [])); |
| 62 | 62 | } |
| 63 | 63 | |
| 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 { | |
| 65 | 65 | const origin = ctx.url.origin; |
| 66 | 66 | const etag = `W/"${Bun.hash(JSON.stringify([f.title, f.subtitle, f.items])).toString(36)}"`; |
| 67 | 67 | const headers: Record<string, string> = { ...HEADERS, etag }; |
| @@ -82,7 +82,7 @@ function render(ctx: Ctx, f: { cache?: string | undefined; vary?: boolean; path: | ||
| 82 | 82 | <id>${xml(origin + f.path)}</id> |
| 83 | 83 | <title>${xml(f.title)}</title> |
| 84 | 84 | <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> | |
| 86 | 86 | <link rel="self" type="application/atom+xml" href="${xml(origin + f.path)}"/> |
| 87 | 87 | <link rel="alternate" type="text/html" href="${xml(origin + f.html)}"/> |
| 88 | 88 | <generator>forge</generator> |
| @@ -105,7 +105,7 @@ export function shipsAtom(ctx: Ctx): Response { | ||
| 105 | 105 | author: issue.originAuthor ?? issue.authorName, |
| 106 | 106 | link: `${ctx.url.origin}/${slug}/pulls/${issue.number}`, |
| 107 | 107 | })); |
| 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 }); | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** `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 { | ||
| 117 | 117 | const items = timePhase(ctx.request, "forks", () => { |
| 118 | 118 | const forks = visibleForks(ctx.app.db, repo.id, ctx.auth.user); |
| 119 | 119 | const dates = repoDatesFor(ctx.app.db, forks.map((f) => f.id)); |
| 120 | const epoch = feedEpoch(ctx.app.db); | |
| 120 | 121 | return forks |
| 121 | .map((f) => ({ f, at: dates.get(f.id)?.createdAt ?? STARTED })) | |
| 122 | .map((f) => ({ f, at: dates.get(f.id)?.createdAt ?? epoch })) | |
| 122 | 123 | .sort((a, b) => Date.parse(stamp(b.at)) - Date.parse(stamp(a.at))) |
| 123 | 124 | .slice(0, FORK_ENTRIES) |
| 124 | 125 | .map(({ f, at }) => ({ |
src/web/feeds.ts +6 −5
| @@ -19,6 +19,7 @@ import { eventActor, eventVerb } from "../design/activity.ts"; | ||
| 19 | 19 | import { audienceFilter } from "../core/event_audience.ts"; |
| 20 | 20 | import { listEvents, type ForgeEvent } from "../core/events.ts"; |
| 21 | 21 | import { getIssuesByRef } from "../core/issues.ts"; |
| 22 | import { feedEpoch } from "../core/feed_epoch.ts"; | |
| 22 | 23 | import { canRead, forkParentFor, getRepo, repoDatesFor } from "../core/repos.ts"; |
| 23 | 24 | import { explore, type ExploreQuery, type ExploreRow } from "../actions/explore.ts"; |
| 24 | 25 | import { getUsersByIds } from "../core/users.ts"; |
| @@ -31,7 +32,6 @@ import { shippedAnswer } from "../release/answer.ts"; | ||
| 31 | 32 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts"; |
| 32 | 33 | import type { Ctx } from "../http/context.ts"; |
| 33 | 34 | import { notFound } from "../http/respond.ts"; |
| 34 | import { STARTED } from "./started.ts"; | |
| 35 | 35 | |
| 36 | 36 | const FEED_KINDS = new Set([ |
| 37 | 37 | "push", |
| @@ -173,7 +173,7 @@ export function activityAtom(ctx: Ctx): Response { | ||
| 173 | 173 | </entry>`; |
| 174 | 174 | }); |
| 175 | 175 | |
| 176 | const updated = events[0]?.createdAt ?? STARTED; | |
| 176 | const updated = events[0]?.createdAt ?? feedEpoch(ctx.app.db, repo.id); | |
| 177 | 177 | const body = `<?xml version="1.0" encoding="utf-8"?> |
| 178 | 178 | <feed xmlns="http://www.w3.org/2005/Atom"> |
| 179 | 179 | <id>${xml(base)}/activity.atom</id> |
| @@ -294,7 +294,7 @@ export async function changelogAtom(ctx: Ctx): Promise<Response> { | ||
| 294 | 294 | <summary>${xml(`Landed in ${l.repo} as pull #${l.number} on ${stamp(l.at).slice(0, 10)} and is live.`)}</summary> |
| 295 | 295 | </entry>`]; |
| 296 | 296 | }); |
| 297 | const updated = live[0]?.at ?? STARTED; | |
| 297 | const updated = live[0]?.at ?? feedEpoch(ctx.app.db); | |
| 298 | 298 | const body = `<?xml version="1.0" encoding="utf-8"?> |
| 299 | 299 | <feed xmlns="http://www.w3.org/2005/Atom"> |
| 300 | 300 | <id>${xml(origin)}/changelog.atom</id> |
| @@ -346,8 +346,9 @@ export async function exploreAtom(ctx: Ctx): Promise<Response> { | ||
| 346 | 346 | return { row, repo }; |
| 347 | 347 | }); |
| 348 | 348 | const dates = repoDatesFor(ctx.app.db, made.flatMap((m) => (m.repo === undefined ? [] : [m.repo.id]))); |
| 349 | const epoch = feedEpoch(ctx.app.db); | |
| 349 | 350 | 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 })) | |
| 351 | 352 | .sort((a, b) => Date.parse(stamp(b.at)) - Date.parse(stamp(a.at))); |
| 352 | 353 | const etag = `W/"${Bun.hash(JSON.stringify(dated.map((m) => [m.row.url, m.row.description, m.row.numbers, m.at]))).toString(36)}"`; |
| 353 | 354 | const headers = { etag, "cache-control": "public, max-age=300" }; |
| @@ -360,7 +361,7 @@ export async function exploreAtom(ctx: Ctx): Promise<Response> { | ||
| 360 | 361 | <link rel="alternate" type="text/html" href="${xml(`${origin}${row.url}`)}"/> |
| 361 | 362 | <summary>${xml(exploreSummary(row))}</summary> |
| 362 | 363 | </entry>`); |
| 363 | const updated = dated[0]?.at ?? STARTED; | |
| 364 | const updated = dated[0]?.at ?? epoch; | |
| 364 | 365 | const body = `<?xml version="1.0" encoding="utf-8"?> |
| 365 | 366 | <feed xmlns="http://www.w3.org/2005/Atom"> |
| 366 | 367 | <id>${xml(origin)}/explore.atom</id> |
src/web/pages/waves.ts +2 −2
| @@ -6,12 +6,12 @@ | ||
| 6 | 6 | * it does for any other page of a private repository. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { feedEpoch } from "../../core/feed_epoch.ts"; | |
| 9 | 10 | import { canRead, getRepo } from "../../core/repos.ts"; |
| 10 | 11 | import { mayDirectCrew } from "../../crew/direct.ts"; |
| 11 | 12 | import { notFound } from "../../http/respond.ts"; |
| 12 | 13 | import { waitingKeys } from "../../release/landings.ts"; |
| 13 | 14 | import { holds, stamp, xml } from "../feeds.ts"; |
| 14 | import { STARTED } from "../started.ts"; | |
| 15 | 15 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 16 | 16 | import { repoDir } from "../../git/repos.ts"; |
| 17 | 17 | import type { Ctx } from "../../http/context.ts"; |
| @@ -167,7 +167,7 @@ export async function wavesAtom(ctx: Ctx): Promise<Response> { | ||
| 167 | 167 | <id>${xml(base)}/waves.atom</id> |
| 168 | 168 | <title>${xml(`${repo.ownerName}/${repo.name} waves`)}</title> |
| 169 | 169 | <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> | |
| 171 | 171 | <link rel="self" type="application/atom+xml" href="${xml(base)}/waves.atom"/> |
| 172 | 172 | <link rel="alternate" type="text/html" href="${xml(base)}/waves"/> |
| 173 | 173 | <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"; | ||
| 13 | 13 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 14 | 14 | import { serve, type Servers } from "../src/http/server.ts"; |
| 15 | 15 | import { writeWave } from "../src/story/wave.ts"; |
| 16 | import { feedEpoch } from "../src/core/feed_epoch.ts"; | |
| 16 | 17 | import { xml } from "../src/web/feeds.ts"; |
| 17 | 18 | import { checkAtom } from "./helpers/atom.ts"; |
| 18 | 19 | |
| @@ -76,6 +77,59 @@ describe("atom feeds", () => { | ||
| 76 | 77 | }); |
| 77 | 78 | } |
| 78 | 79 | |
| 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 | ||
| 79 | 133 | test("characters XML forbids are dropped, not emitted", () => { |
| 80 | 134 | expect(xml("a\u0000b\u0008c\u001fd&e")).toBe("abcd&e"); |
| 81 | 135 | }); |
| @@ -147,11 +201,11 @@ describe("forks.atom", () => { | ||
| 147 | 201 | |
| 148 | 202 | test("the feed is capped at FORK_ENTRIES", async () => { |
| 149 | 203 | const root = getRepo(app.db, "alice", "app")!; |
| 204 | // Accounts without a password skip argon2, which is what made this slow. | |
| 150 | 205 | 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` }); | |
| 152 | 207 | 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"); | |
| 155 | 209 | if (!promoted.ok) throw new Error(promoted.error); |
| 156 | 210 | const r = await forkRepo(app, u.value, root, `many${i}`); |
| 157 | 211 | 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"; | ||
| 10 | 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 11 | 11 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 12 | 12 | import { forgetLive, keepLive } from "../src/release/landings.ts"; |
| 13 | import { feedEpoch } from "../src/core/feed_epoch.ts"; | |
| 13 | 14 | import { getRepo } from "../src/core/repos.ts"; |
| 14 | 15 | import { writeWave } from "../src/story/wave.ts"; |
| 15 | 16 | import { checkAtom } from "./helpers/atom.ts"; |
| @@ -234,15 +235,17 @@ describe("GET /changelog.atom", () => { | ||
| 234 | 235 | }); |
| 235 | 236 | |
| 236 | 237 | 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 () => { | |
| 238 | 239 | const dataDir = await mkdtemp(join(tmpdir(), "forge-changelog-atom-empty-")); |
| 239 | 240 | const app = createApp({ dataDir, port: 0, forgeRepo: "alice/forge" }); |
| 240 | 241 | const servers = await serve(app); |
| 241 | 242 | 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); | |
| 242 | 245 | const get = async () => (await (await fetch(`${servers.url}/changelog.atom`)).text()).match(/<updated>([^<]*)<\/updated>/)![1]!; |
| 243 | 246 | 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 | |
| 246 | 249 | expect(await get()).toBe(first); |
| 247 | 250 | } finally { |
| 248 | 251 | servers.stop(); |
new above
offline
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.