Public: anyone can read this; the crew can change it.
The feed's older posts load by a plain 'Show older' link, with no script, and the place survives a reload #14653
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14639-the-feed-s-older-posts-load-by-a-plain-s → main 2 commits fork
6 files
changed +108
−12
src/web/studio/feed.ts +2 −2
| @@ -507,8 +507,8 @@ export function caughtUp(items: readonly FeedItem[], next: string | null, cursor | ||
| 507 | 507 | // The ranked page ends where its window does; the log goes on, newest first. |
| 508 | 508 | const deeper = timeline ? html`` : html`<a href="/feed?before=${TIMELINE_START}">Everything, newest first</a>`; |
| 509 | 509 | const older = (n: string) => timeline |
| 510 | ? html`<a href="/feed?before=${encodeURIComponent(n)}${keep}">Older posts</a>` | |
| 511 | : html`<a href="/feed?cursor=${encodeURIComponent(n)}&day=${encodeURIComponent(items[items.length - 1]?.at ?? "")}${app === undefined ? "" : `&app=${encodeURIComponent(app)}`}">Older posts</a>`; | |
| 510 | ? html`<a href="/feed?before=${encodeURIComponent(n)}${keep}">Show older</a>` | |
| 511 | : html`<a href="/feed?cursor=${encodeURIComponent(n)}&day=${encodeURIComponent(items[items.length - 1]?.at ?? "")}${app === undefined ? "" : `&app=${encodeURIComponent(app)}`}">Show older</a>`; | |
| 512 | 512 | return next === null |
| 513 | 513 | ? html`<p class="bar muted small" data-caught-up="end"><span>${timeline ? html`That's every post` : html`That's everything`}</span><span class="spacer"></span>${newest}${deeper}</p>` |
| 514 | 514 | : html`<p class="bar muted small" data-caught-up="more"> |
src/web/studio/home.ts +13
| @@ -67,6 +67,8 @@ import { | ||
| 67 | 67 | import { writableAppsOf } from "./asks.ts"; |
| 68 | 68 | import { needsYouSection } from "./asks_page.ts"; |
| 69 | 69 | import { flowCards } from "./flow_card.ts"; |
| 70 | import { parseBefore } from "../../feed/timeline.ts"; | |
| 71 | import { parseCursor } from "../../feed/feed.ts"; | |
| 70 | 72 | import { composer, publicFeed, feedJs, feedNote, feedRegion } from "./feed.ts"; |
| 71 | 73 | import { heldLine } from "./held.ts"; |
| 72 | 74 | import { waitingLine } from "./kpi_row.ts"; |
| @@ -345,6 +347,17 @@ export async function studioHome( | ||
| 345 | 347 | */ |
| 346 | 348 | export async function feedPage(ctx: Ctx): Promise<Response> { |
| 347 | 349 | const user = ctx.auth.user; |
| 350 | // A place in the timeline this forge did not issue is refused in plain words, not read as the | |
| 351 | // first page. | |
| 352 | const asked = ctx.url.searchParams; | |
| 353 | const badBefore = asked.has("before") && parseBefore(asked.get("before") ?? "") === undefined; | |
| 354 | const badCursor = asked.has("cursor") && parseCursor(asked.get("cursor") ?? "") === undefined; | |
| 355 | if (user && (badBefore || badCursor)) { | |
| 356 | return new Response("That place in the feed is not one this forge gave out. Open the feed again to start from the newest post.\n", { | |
| 357 | status: 400, | |
| 358 | headers: { "content-type": "text/plain; charset=utf-8" }, | |
| 359 | }); | |
| 360 | } | |
| 348 | 361 | if (!user) { |
| 349 | 362 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 350 | 363 | const ships = timePhase(ctx.request, "feed", () => publicShips(ctx.app.db)); |
test/feed-caught-up.test.ts +5 −5
| @@ -54,7 +54,7 @@ describe("the timeline's caught-up line", () => { | ||
| 54 | 54 | emit(3); |
| 55 | 55 | const text = await home(); |
| 56 | 56 | expect(text).toContain("That's everything"); |
| 57 | expect(text).not.toContain("Older posts"); | |
| 57 | expect(text).not.toContain("Show older"); | |
| 58 | 58 | }); |
| 59 | 59 | |
| 60 | 60 | test("a feed with more pages says how far it reaches and links to the next", async () => { |
| @@ -62,7 +62,7 @@ describe("the timeline's caught-up line", () => { | ||
| 62 | 62 | const text = await home(); |
| 63 | 63 | expect(text).toContain("You're caught up to"); |
| 64 | 64 | expect(text).not.toContain("That's everything"); |
| 65 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Older posts/.exec(text); | |
| 65 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Show older/.exec(text); | |
| 66 | 66 | expect(href).not.toBeNull(); |
| 67 | 67 | const older = await home(href![1]!.replaceAll("&", "&")); |
| 68 | 68 | expect(older).toContain("data-item="); |
| @@ -72,7 +72,7 @@ describe("the timeline's caught-up line", () => { | ||
| 72 | 72 | }); |
| 73 | 73 | |
| 74 | 74 | test("a page after the first carries its own address, so a chip refetch stays on it", async () => { |
| 75 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Older posts/.exec(await home())![1]!.replaceAll("&", "&"); | |
| 75 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Show older/.exec(await home())![1]!.replaceAll("&", "&"); | |
| 76 | 76 | const cursor = href.slice("/feed?cursor=".length).split("&day=")[0]; |
| 77 | 77 | const older = await home(href); |
| 78 | 78 | expect(older).toContain(`data-live-src="/live/feed?cursor=${cursor}&day=`); |
| @@ -87,8 +87,8 @@ describe("the timeline's caught-up line", () => { | ||
| 87 | 87 | expect(html).not.toContain("2024-06-01T00:00:00.000Z"); |
| 88 | 88 | }); |
| 89 | 89 | |
| 90 | test("a cursor not issued here is the first page", async () => { | |
| 90 | test("a cursor not issued here is refused in plain words (400)", async () => { | |
| 91 | 91 | const text = await home("/feed?cursor=garbage"); |
| 92 | expect(text).toContain("You're caught up to"); | |
| 92 | expect(text).toContain("not one this forge gave out"); | |
| 93 | 93 | }); |
| 94 | 94 | }); |
test/feed-cursor.test.ts +4 −4
| @@ -167,12 +167,12 @@ describe("the feed pages by cursor", () => { | ||
| 167 | 167 | const res = await fetch(`${servers.url}/feed?before=now`, { headers: { cookie: bobCookie } }); |
| 168 | 168 | expect(res.status).toBe(200); |
| 169 | 169 | const text = await res.text(); |
| 170 | const href = /href="(\/feed\?before=[0-9a-z]{26})">Older posts/.exec(text); | |
| 170 | const href = /href="(\/feed\?before=[0-9a-z]{26})">Show older/.exec(text); | |
| 171 | 171 | expect(href).not.toBeNull(); |
| 172 | 172 | const next = await fetch(`${servers.url}${href![1]}`, { headers: { cookie: bobCookie } }); |
| 173 | 173 | expect(next.status).toBe(200); |
| 174 | 174 | expect(await next.text()).toContain('data-item="event:'); |
| 175 | expect((await fetch(`${servers.url}/feed?before=garbage`, { headers: { cookie: bobCookie } })).status).toBe(200); | |
| 175 | expect((await fetch(`${servers.url}/feed?before=garbage`, { headers: { cookie: bobCookie } })).status).toBe(400); | |
| 176 | 176 | }); |
| 177 | 177 | |
| 178 | 178 | const mcp = async (token: string, args: object) => { |
| @@ -230,7 +230,7 @@ describe("the feed pages by cursor", () => { | ||
| 230 | 230 | expect(page.next_cursor).not.toBeNull(); |
| 231 | 231 | const cookie = `forge_session=${createSession(app.db, carol.id).token}`; |
| 232 | 232 | const html = await (await fetch(`${servers.url}/feed?before=now`, { headers: { cookie } })).text(); |
| 233 | expect(html).toContain(`/feed?before=${page.next_cursor}">Older posts`); | |
| 233 | expect(html).toContain(`/feed?before=${page.next_cursor}">Show older`); | |
| 234 | 234 | // Following it reaches the posts the mute left. |
| 235 | 235 | const on = await (await fetch(`${servers.url}/feed?before=${page.next_cursor}`, { headers: { cookie } })).text(); |
| 236 | 236 | expect(on).toContain('data-item="event:'); |
| @@ -262,7 +262,7 @@ describe("the feed page pages 70 posts, 30 at a time", () => { | ||
| 262 | 262 | const found = [...html.matchAll(/data-item="(event:[^"]+)"/g)].map((m) => m[1]!); |
| 263 | 263 | sizes.push(found.length); |
| 264 | 264 | ids.push(...found); |
| 265 | const next = /href="(\/feed\?before=[0-9a-z]{26})">Older posts/.exec(html); | |
| 265 | const next = /href="(\/feed\?before=[0-9a-z]{26})">Show older/.exec(html); | |
| 266 | 266 | path = next?.[1]; |
| 267 | 267 | } |
| 268 | 268 | expect(sizes).toEqual([30, 30, 10]); |
test/feed-day-divider.test.ts +1 −1
| @@ -69,7 +69,7 @@ describe("day dividers on the home timeline", () => { | ||
| 69 | 69 | } |
| 70 | 70 | const first = await get("/feed"); |
| 71 | 71 | expect(dividerLabels(first)[0]).toBe("Today"); |
| 72 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Older posts/.exec(first)![1]!.replaceAll("&", "&"); | |
| 72 | const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Show older/.exec(first)![1]!.replaceAll("&", "&"); | |
| 73 | 73 | const older = await get(href); |
| 74 | 74 | expect(older).toContain("data-item="); |
| 75 | 75 | expect(dividerLabels(older)).not.toContain("Today"); |
test/feed-older.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | /** The feed's "Show older" link: a plain link with a cursor, no script, the same page on reload. */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createUser, type User } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { promote } from "./helpers/members.ts"; | |
| 12 | ||
| 13 | describe("Show older", () => { | |
| 14 | let dataDir: string; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | let cookie: string; | |
| 18 | ||
| 19 | beforeAll(async () => { | |
| 20 | dataDir = await mkdtemp(join(tmpdir(), "forge-feed-older-")); | |
| 21 | app = createApp({ dataDir, port: 0 }); | |
| 22 | servers = await serve(app); | |
| 23 | const mk = async (username: string, first = false): Promise<User> => { | |
| 24 | const r = await createUser(app.db, { | |
| 25 | origin: "seed", trusted: true, username, allowReserved: true, | |
| 26 | email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first, | |
| 27 | }); | |
| 28 | if (!r.ok) throw new Error(r.error); | |
| 29 | promote(app.db, r.value); | |
| 30 | return r.value; | |
| 31 | }; | |
| 32 | await mk("root", true); | |
| 33 | const alice = await mk("alice"); | |
| 34 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 35 | const repo = await createRepo(app, alice, { name: "open", visibility: "public" }); | |
| 36 | if (!repo.ok) throw new Error("repo"); | |
| 37 | for (let n = 1; n <= 40; n++) | |
| 38 | app.bus.append({ kind: "issue.opened", repoId: repo.value.id, actorId: alice.id, payload: { number: n } }); | |
| 39 | }); | |
| 40 | ||
| 41 | afterAll(async () => { | |
| 42 | servers.stop(); | |
| 43 | app.close(); | |
| 44 | await rm(dataDir, { recursive: true, force: true }); | |
| 45 | }); | |
| 46 | ||
| 47 | const get = (path: string) => fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 48 | const older = (html: string) => /href="(\/feed\?before=[0-9a-z]{26})">Show older/.exec(html)?.[1]; | |
| 49 | const posts = (html: string) => [...html.matchAll(/data-item="(event:[^"]+)"/g)].map((m) => m[1]!); | |
| 50 | ||
| 51 | test("first page ends with a plain link, the second is that link rendered server-side and reloads the same", async () => { | |
| 52 | const first = await (await get("/feed?before=now")).text(); | |
| 53 | const link = older(first); | |
| 54 | expect(link).toBeDefined(); | |
| 55 | const a = await (await get(link!)).text(); | |
| 56 | const b = await (await get(link!)).text(); | |
| 57 | expect(posts(a).length).toBeGreaterThan(0); | |
| 58 | expect(posts(a)).toEqual(posts(b)); | |
| 59 | expect(posts(a).filter((p) => posts(first).includes(p))).toEqual([]); | |
| 60 | }); | |
| 61 | ||
| 62 | test("following the link to the end, the last page shows no link", async () => { | |
| 63 | let path: string | undefined = "/feed?before=now"; | |
| 64 | let last = ""; | |
| 65 | for (let i = 0; i < 20 && path !== undefined; i++) { | |
| 66 | last = await (await get(path)).text(); | |
| 67 | path = older(last); | |
| 68 | } | |
| 69 | expect(path).toBeUndefined(); | |
| 70 | expect(last).not.toContain("Show older"); | |
| 71 | expect(last).toContain("That's every post"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("a malformed cursor is a 400 in plain words", async () => { | |
| 75 | for (const bad of ["garbage", "x".repeat(200), "Z".repeat(26)]) { | |
| 76 | for (const key of ["before", "cursor"]) { | |
| 77 | const res = await get(`/feed?${key}=${encodeURIComponent(bad)}`); | |
| 78 | expect(res.status).toBe(400); | |
| 79 | expect(await res.text()).toContain("not one this forge gave out"); | |
| 80 | } | |
| 81 | } | |
| 82 | }); | |
| 83 | }); |
new above
offline