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

Merged opened this

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

Merged

scout/forge:scout/14639-the-feed-s-older-posts-load-by-a-plain-s → main 2 commits fork

  • Merged. scout/14639-the-feed-s-older-posts-load-by-a-plain-s was merged into main as 06e6fcc.

Review

Sign in to review this change.
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
507507 // The ranked page ends where its window does; the log goes on, newest first.
508508 const deeper = timeline ? html`` : html`<a href="/feed?before=${TIMELINE_START}">Everything, newest first</a>`;
509509 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>`;
512512 return next === null
513513 ? 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>`
514514 : html`<p class="bar muted small" data-caught-up="more">
src/web/studio/home.ts +13
@@ -67,6 +67,8 @@ import {
6767 import { writableAppsOf } from "./asks.ts";
6868 import { needsYouSection } from "./asks_page.ts";
6969 import { flowCards } from "./flow_card.ts";
70 import { parseBefore } from "../../feed/timeline.ts";
71 import { parseCursor } from "../../feed/feed.ts";
7072 import { composer, publicFeed, feedJs, feedNote, feedRegion } from "./feed.ts";
7173 import { heldLine } from "./held.ts";
7274 import { waitingLine } from "./kpi_row.ts";
@@ -345,6 +347,17 @@ export async function studioHome(
345347 */
346348 export async function feedPage(ctx: Ctx): Promise<Response> {
347349 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 }
348361 if (!user) {
349362 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
350363 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", () => {
5454 emit(3);
5555 const text = await home();
5656 expect(text).toContain("That's everything");
57 expect(text).not.toContain("Older posts");
57 expect(text).not.toContain("Show older");
5858 });
5959
6060 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", () => {
6262 const text = await home();
6363 expect(text).toContain("You're caught up to");
6464 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);
6666 expect(href).not.toBeNull();
6767 const older = await home(href![1]!.replaceAll("&amp;", "&"));
6868 expect(older).toContain("data-item=");
@@ -72,7 +72,7 @@ describe("the timeline's caught-up line", () => {
7272 });
7373
7474 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("&amp;", "&");
75 const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Show older/.exec(await home())![1]!.replaceAll("&amp;", "&");
7676 const cursor = href.slice("/feed?cursor=".length).split("&day=")[0];
7777 const older = await home(href);
7878 expect(older).toContain(`data-live-src="/live/feed?cursor=${cursor}&amp;day=`);
@@ -87,8 +87,8 @@ describe("the timeline's caught-up line", () => {
8787 expect(html).not.toContain("2024-06-01T00:00:00.000Z");
8888 });
8989
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 () => {
9191 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");
9393 });
9494 });
test/feed-cursor.test.ts +4 −4
@@ -167,12 +167,12 @@ describe("the feed pages by cursor", () => {
167167 const res = await fetch(`${servers.url}/feed?before=now`, { headers: { cookie: bobCookie } });
168168 expect(res.status).toBe(200);
169169 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);
171171 expect(href).not.toBeNull();
172172 const next = await fetch(`${servers.url}${href![1]}`, { headers: { cookie: bobCookie } });
173173 expect(next.status).toBe(200);
174174 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);
176176 });
177177
178178 const mcp = async (token: string, args: object) => {
@@ -230,7 +230,7 @@ describe("the feed pages by cursor", () => {
230230 expect(page.next_cursor).not.toBeNull();
231231 const cookie = `forge_session=${createSession(app.db, carol.id).token}`;
232232 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`);
234234 // Following it reaches the posts the mute left.
235235 const on = await (await fetch(`${servers.url}/feed?before=${page.next_cursor}`, { headers: { cookie } })).text();
236236 expect(on).toContain('data-item="event:');
@@ -262,7 +262,7 @@ describe("the feed page pages 70 posts, 30 at a time", () => {
262262 const found = [...html.matchAll(/data-item="(event:[^"]+)"/g)].map((m) => m[1]!);
263263 sizes.push(found.length);
264264 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);
266266 path = next?.[1];
267267 }
268268 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", () => {
6969 }
7070 const first = await get("/feed");
7171 expect(dividerLabels(first)[0]).toBe("Today");
72 const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Older posts/.exec(first)![1]!.replaceAll("&amp;", "&");
72 const href = /href="(\/feed\?cursor=[^"]+)"[^>]*>Show older/.exec(first)![1]!.replaceAll("&amp;", "&");
7373 const older = await get(href);
7474 expect(older).toContain("data-item=");
7575 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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs