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

The feed marks an item as seen once it is on screen for the signed-in owner, and shows a 'new' count since last visit #13476

Merged opened this

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

Merged

scout/forge:scout/13458-the-feed-marks-an-item-as-seen-once-it-i → main 4 commits fork 3 threads · 3 unresolved

  • Merged. scout/13458-the-feed-marks-an-item-as-seen-once-it-i was merged into main as 0930c49.

Review

Sign in to review this change.
9 files changed +256 −11
docs/features/README.md +1
@@ -49,6 +49,7 @@ Read with the guide page [What a forge is](../guide-what-a-forge-is.md).
4949 - [The fork tree](fork-tree.md)
5050 - [The home](living-home.md)
5151 - [The quiet feed names its one next thing](quiet-feed.md)
52 - [The feed says how many posts are new since your last visit](feed-new-count.md)
5253 - [The roadmap](roadmap.md)
5354
5455 ### Getting around: keys, the palette and search
docs/features/feed-new-count.md added +8
@@ -0,0 +1,8 @@
1 # The feed's "N new" line
2
3 The feed head says "N new since you were last here" when posts newer than your stored last visit
4 (`core/home_visit.ts`, one row per person) are in your feed. It is server-rendered, needs no
5 JavaScript, and counts what the feed would show you (muted apps, dismissed posts and switched-off
6 chips left out), up to 99+. Opening the feed moves the mark on the next request, so the first
7 paint still shows the count. A first visit, or nothing new, says nothing. The terminal feed
8 shows the same line as a toast and never moves the mark. Code: `src/feed/last_seen.ts`.
src/feed/feed.ts +1 −1
@@ -97,7 +97,7 @@ export function quietRunCount(
9797 return cache.get(key, stamp, count);
9898 }
9999
100 function viewerCandidates(
100 export function viewerCandidates(
101101 db: Database,
102102 viewer: User,
103103 now: number,
src/feed/last_seen.ts added +39
@@ -0,0 +1,39 @@
1 /**
2 * "N new since you were last here": how many posts the viewer's feed holds that are newer than
3 * their stored last visit (`core/home_visit.ts`, one row per person). Read-only: the mark moves
4 * on the next request (`noteVisit`), never before the first paint. A first visit has no mark,
5 * so it counts nothing and says nothing.
6 */
7
8 import type { Database } from "bun:sqlite";
9 import { chipsOf } from "../core/feed_chips.ts";
10 import { dismissedItems, mutedRepos } from "../core/interactions.ts";
11 import type { User } from "../core/users.ts";
12 import { chipHides } from "./chips.ts";
13 import { viewerCandidates } from "./feed.ts";
14
15 /** Counted up to here; more is "99+", so the read never walks a long absence. */
16 export const NEW_COUNT_CAP = 99;
17
18 /**
19 * New posts since `since`, as the feed would show them to `viewer`; undefined `since` is a first
20 * visit.
21 */
22 export function newSince(db: Database, viewer: User, since: number | undefined, now: number): number {
23 if (since === undefined) return 0;
24 const muted = mutedRepos(db, viewer.id);
25 const dismissed = dismissedItems(db, viewer.id);
26 const chips = chipsOf(db, viewer.id);
27 let n = 0;
28 for (const c of viewerCandidates(db, viewer, now, chips, false)) {
29 if (Date.parse(c.at) <= since || muted.has(c.repoId) || dismissed.has(c.id) || chipHides(chips, c)) continue;
30 if (++n > NEW_COUNT_CAP) break;
31 }
32 return n;
33 }
34
35 /** The feed head's words; no new posts, no line. */
36 export function newLine(count: number): string | null {
37 if (count <= 0) return null;
38 return `${count > NEW_COUNT_CAP ? `${NEW_COUNT_CAP}+` : count} new since you were last here`;
39 }
src/tui/screens/feed.ts +7
@@ -17,6 +17,8 @@
1717
1818 import { addCommentThrough } from "../../actions/issues.ts";
1919 import { getIssue } from "../../core/issues.ts";
20 import { lastVisit } from "../../core/home_visit.ts";
21 import { newLine, newSince } from "../../feed/last_seen.ts";
2022 import { parseCursor, readFeed } from "../../feed/feed.ts";
2123 import { parseBefore, readTimeline, TIMELINE_START } from "../../feed/timeline.ts";
2224 import type { FeedItem } from "../../feed/rank.ts";
@@ -87,6 +89,11 @@ export function feedScreen(f: Session, ctx: Ctx): Screen {
8789 ctx.invalidate();
8890 };
8991 read();
92 // The page's head line, read-only: the terminal never moves the mark.
93 if (f.user !== undefined) {
94 const line = newLine(newSince(f.db, f.user, lastVisit(f.db, f.user.id), Date.now()));
95 if (line !== null) ctx.toast(line);

The terminal toast has no test; add one that the toast shows and lastVisit is unchanged.

96 }
9097
9198 /** The post under the cursor, when it is about a thread this person may reply on. */
9299 const selected = () => {
src/web/studio/home.ts +12 −5
@@ -76,6 +76,7 @@ import { archivedAppCount } from "../pages/apps.ts";
7676 import { pinnedPanels } from "./pinned.ts";
7777 import { isNavigation, lastVisit, noteVisit } from "../../core/home_visit.ts";
7878 import { sinceYouWere } from "./since.ts";
79 import { newLine, newSince } from "../../feed/last_seen.ts";
7980 import { homeTabs, unshippedAsks } from "./home_tabs.ts";
8081 import { homeFlowPage } from "../pages/home_flow.ts";
8182 import { crewKeyCard } from "./crew_key.ts";
@@ -348,15 +349,21 @@ export async function feedPage(ctx: Ctx): Promise<Response> {
348349 const before = timePhase(ctx.request, "visit", () =>
349350 isNavigation(ctx.request) ? noteVisit(ctx.app.db, user.id, visitedAt) : lastVisit(ctx.app.db, user.id),
350351 );
351 const since = timePhase(ctx.request, "since", () => sinceYouWere(ctx.app.db, user, before, visitedAt));
352352 const draws = homeDraws(ctx, user);
353353 const stalled = flowCards(ctx, user);
354 // Milestones are only an input here: the feed does not draw them (#12328).
355 const quiet = isQuiet({ stalled: stalled !== null, milestones: biggestMilestones(ctx, appRepos).length > 0, landed: since !== null });
356354 const access = timePhase(ctx.request, "access", () => accessFingerprint(ctx.app.db, user));
357355 const regionOf = (name: HomeRegion): Promise<Html> =>
358356 homeRegion(ctx.app.db, ctx.request, user.id, name, homeVariant(ctx.url), access, draws[name]);
357 // The feed reads first: the count below then reuses its cached candidates instead of warming
358 // them.
359 const feedRegion = await regionOf("feed");
360 const fresh = timePhase(ctx.request, "new", () => newLine(newSince(ctx.app.db, user, before, visitedAt)));
361 // One sentence, once: the count rides in the digest's head; with no digest it stands alone.
362 const since = timePhase(ctx.request, "since", () => sinceYouWere(ctx.app.db, user, before, visitedAt, fresh));
363 // Milestones are only an input here: the feed does not draw them (#12328).
364 const quiet = isQuiet({ stalled: stalled !== null, milestones: biggestMilestones(ctx, appRepos).length > 0, landed: since !== null });
359365 const body = html`<h1 class="sr-only">Feed</h1>
outdated

Principle 1: this repeats what the "Since you were here" digest right below already says. One confident sentence, once: merge the count into the digest or remove the duplicate.

366 ${since === null && fresh !== null ? html`<p id="feed-new" class="small muted" role="status">${fresh}</p>` : null}
360367 ${await regionOf("waiting")}
361368 ${user.isAdmin ? await regionOf("panels") : null}
362369 ${since}
@@ -367,7 +374,7 @@ export async function feedPage(ctx: Ctx): Promise<Response> {
367374 ${composer(ctx, user)}
368375 ${stalled}
369376 ${feedNote()}
370 ${await regionOf("feed")}
377 ${feedRegion}
371378 </div>
372379 <aside class="stack">
373380 ${user.isAdmin || appRepos.length > 0 ? crewKeyCard(ctx) : null}
@@ -382,7 +389,7 @@ export async function feedPage(ctx: Ctx): Promise<Response> {
382389 body,
383390 scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${feedJs}\n${refreshJs}`,
384391 });
385 notePhaseRest(ctx.request, "render", rendering, ["waiting", "feed", "asked", "visit", "since", "access", "repos"]);
392 notePhaseRest(ctx.request, "render", rendering, ["waiting", "feed", "asked", "visit", "since", "new", "access", "repos"]);
386393 return res;
387394 }
388395
src/web/studio/since.ts +23 −5
@@ -41,7 +41,9 @@ function readable(db: Database, viewer: User | undefined): (repoId: string) => R
4141 };
4242 }
4343
44 /** A page a person reads: the pull when one is known, else the repository's pulls; never an API URL. */
44 /**
45 * A page a person reads: the pull when one is known, else the repository's pulls; never an API URL.
46 */
4547 function pageOf(repo: Repo, pull: number | null | undefined): string {
4648 const base = `/${repo.ownerName}/${repo.name}/pulls`;
4749 return Number.isInteger(pull) && (pull as number) > 0 ? `${base}/${pull}` : base;
@@ -57,7 +59,10 @@ function firstPull(json: string): number | null {
5759 }
5860 }
5961
60 /** What happened in the window, in the repositories `viewer` may read: landed pulls, releases, red gates. */
62 /**
63 * What happened in the window, in the repositories `viewer` may read: landed pulls, releases, red
64 * gates.
65 */
6166 export function eventsSince(db: Database, viewer: User | undefined, since: number, now: number): DigestEvent[] {
6267 const from = new Date(since).toISOString();
6368 const to = new Date(now).toISOString();
@@ -105,16 +110,29 @@ export function sinceDigest(
105110 return lines.length === 0 ? undefined : { heading: headingOf(window), lines };
106111 }
107112
108 /** The digest a feed's first page carries (HTTP and MCP alike): read-only, a visit is never noted; `null` when there is nothing. */
113 /**
114 * The digest a feed's first page carries (HTTP and MCP alike): read-only, a visit is never noted;
115 * `null` when there is nothing.
116 */
109117 export function feedDigest(db: Database, viewer: User, now: number): { heading: string; lines: DigestLine[] } | null {
110118 return sinceDigest(db, viewer, visitOf(db, viewer.id), now) ?? null;
111119 }
112120
113 export function sinceYouWere(db: Database, viewer: User | undefined, lastVisit: number | undefined, now: number): Html | null {
121 /**
122 * The digest card; `fresh`, the new-post count's sentence, rides in its head so the page says it
123 * once.
124 */
125 export function sinceYouWere(
126 db: Database,
127 viewer: User | undefined,
128 lastVisit: number | undefined,
129 now: number,
130 fresh: string | null = null,
131 ): Html | null {
114132 const d = sinceDigest(db, viewer, lastVisit, now);
115133 if (d === undefined) return null;
116134 return listCard(
117135 d.lines.map((l) => row({ title: html`${l.says}`, href: l.href, nav: true })),
118 { id: "since-you-were-here", head: html`<h2>${d.heading}</h2>` },
136 { id: "since-you-were-here", head: html`<h2>${d.heading}</h2>${fresh === null ? null : html`<p id="feed-new" class="small muted" role="status">${fresh}</p>`}` },
119137 );
120138 }
test/feed-new-count-exact.test.ts added +106
@@ -0,0 +1,106 @@
1 /**
2 * The feed head's new count is exact: posts either side of the mark, a muted app, a dismissed post.
3 */
4 import { afterAll, beforeAll, expect, test } from "bun:test";
5 import { mkdtemp, rm } from "node:fs/promises";
6 import { tmpdir } from "node:os";
7 import { join } from "node:path";
8 import { createRepo } from "../src/actions/repos.ts";
9 import { createApp, type App as Forge } from "../src/app.ts";
10 import { lastVisit } from "../src/core/home_visit.ts";
11 import { muteRepo, recordInteraction } from "../src/core/interactions.ts";
12 import type { Repo } from "../src/core/repos.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { newSince } from "../src/feed/last_seen.ts";
15 import { createForgeApp } from "../src/tui/forge.ts";
16 import { feedScreen } from "../src/tui/screens/feed.ts";
17 import { headlessTerminal } from "../src/tui/term/terminal.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19 import { promote } from "./helpers/members.ts";
20 import { settleApp } from "./helpers/tui.ts";
21
22 quietLogs();
23
24 let dataDir: string;
25 let forge: Forge;
26 let alice: User;
27 let bob: User;
28 let repo: Repo;
29 let other: Repo;
30 const T0 = Date.now() - 3_600_000;
31
32 const post = (r: Repo, at: number): string => {
33 const ev = forge.bus.append({
34 kind: "run.finished",
35 repoId: r.id,
36 actorId: alice.id,
37 payload: { kind: "deploy", state: "failed", summary: `run ${at}`, runId: `r${at}` },
38 });
39 forge.db.query("UPDATE event SET created_at = ?1 WHERE id = ?2").run(new Date(at).toISOString(), ev.id);
40 return `event:${ev.id}`;
41 };
42 let itemIds: string[] = [];
43
44 beforeAll(async () => {
45 dataDir = await mkdtemp(join(tmpdir(), "forge-new-count-"));
46 forge = createApp({ dataDir, port: 0 });
47 const mk = async (username: string, first = false) => {
48 const r = await createUser(forge.db, { origin: "seed", trusted: true, username, allowReserved: true,
49 email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
50 if (!r.ok) throw new Error(r.error);
51 promote(forge.db, r.value);
52 return r.value;
53 };
54 await mk("root", true);
55 alice = await mk("alice");
56 bob = await mk("bob");
57 const a = await createRepo(forge, alice, { name: "open", visibility: "public" });
58 const b = await createRepo(forge, alice, { name: "quiet", visibility: "public" });
59 if (!a.ok || !b.ok) throw new Error("repo");
60 repo = a.value;
61 other = b.value;
62 // Two before the mark (T0 + 2 s), three after.
63 itemIds = [post(repo, T0), post(repo, T0 + 1000), post(repo, T0 + 3000), post(repo, T0 + 4000), post(repo, T0 + 5000)];
64 });
65
66 afterAll(async () => {
67 forge.close();
68 await rm(dataDir, { recursive: true, force: true });
69 });
70
71 const count = (mark: number | undefined): number => newSince(forge.db, bob, mark, Date.now());
72
73 test("the count is exactly the posts newer than the mark", () => {
74 expect(count(T0 + 2000)).toBe(3);
75 expect(count(T0 + 4000)).toBe(1);
76 expect(count(T0 + 5000)).toBe(0);
77 expect(count(T0 - 1)).toBe(5);
78 });
79
80 test("a dismissed post and a muted app are left out", () => {
81 recordInteraction(forge.db, { viewerId: bob.id, itemId: itemIds[2]!, kind: "dismissed", repoId: repo.id, actorId: bob.id });
82 expect(count(T0 + 2000)).toBe(2);
83 const extra = post(other, T0 + 6000);
84 expect(extra).toBeTruthy();
85 expect(count(T0 + 2000)).toBe(3);
86 muteRepo(forge.db, bob.id, other.id);
87 expect(count(T0 + 2000)).toBe(2);
88 });
89
90 test("the terminal toasts the count and does not move the mark", async () => {
91 forge.db.query("DELETE FROM home_visit WHERE user_id = ?1").run(bob.id);
92 forge.db.query("INSERT INTO home_visit (user_id, at) VALUES (?1, ?2)").run(bob.id, T0 + 2000);
93 const { app, session } = createForgeApp(
94 forge,
95 { sshUsername: "bob", remoteAddress: "127.0.0.1", user: bob },
96 { terminal: headlessTerminal(140, 44), exitHooks: false },
97 );
98 app.start();
99 await settleApp(app);
100 app.push(feedScreen(session, app.ctx));
101 await settleApp(app);
102 const text = app.screen.toText();
103 app.stop();
104 expect(text).toContain("2 new since you were last here");
105 expect(lastVisit(forge.db, bob.id)).toBe(T0 + 2000);
106 });
test/feed-new-count.test.ts added +59
@@ -0,0 +1,59 @@
1 /**
2 * The feed head's "N new since you were last here": first visit, new posts, none; the mark moves on
3 * the next request.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { lastVisit } from "../src/core/home_visit.ts";
7 import { newLine, newSince, NEW_COUNT_CAP } from "../src/feed/last_seen.ts";
8 import { snappyFixture, type SnappyFixture } from "../bench/snappy.ts";
9
10 let f: SnappyFixture;
11 beforeAll(async () => {
12 f = await snappyFixture();
13 }, 120_000);
14 afterAll(async () => {
15 await f.close();
16 });
17
18 const setVisit = (at: number | undefined): void => {
19 f.app.db.query("DELETE FROM home_visit WHERE user_id = ?1").run(f.owner.id);
20 if (at !== undefined) f.app.db.query("INSERT INTO home_visit (user_id, at) VALUES (?1, ?2)").run(f.owner.id, at);
21 };
22 const head = async (): Promise<string | null> => {
23 const text = await (await fetch(`${f.base}/feed`, { headers: { cookie: f.ownerCookie } })).text();
24 return /<p id="feed-new"[^>]*>([^<]*)<\/p>/.exec(text)?.[1] ?? null;
25 };
26
27 test("the words: nothing new says nothing, a count is capped", () => {
28 expect(newLine(0)).toBeNull();
29 expect(newLine(1)).toBe("1 new since you were last here");
30 expect(newLine(NEW_COUNT_CAP + 1)).toBe("99+ new since you were last here");
31 });
32
33 test("a first visit counts nothing; a long absence counts the feed; a visit after everything counts none", () => {
34 const now = Date.now();
35 expect(newSince(f.app.db, f.owner, undefined, now)).toBe(0);
36 const all = newSince(f.app.db, f.owner, 0, now);
37 expect(all).toBeGreaterThan(0);
38 expect(newSince(f.app.db, f.owner, now + 3_600_000, now)).toBe(0);

Only a regex on the count: this passes for any positive number. Seed posts either side of the mark and assert the exact count, plus a muted/dismissed post excluded.

39 });
40
41 test("the page says it server-rendered, then the next request has moved the mark", async () => {
42 setVisit(1);
43 const first = await head();
44 expect(first).toMatch(/^\d+\+? new since you were last here$/);
45 // Moved by the request above, after its paint was drawn: the same load now has nothing new.
46 expect(lastVisit(f.app.db, f.owner.id)).toBeGreaterThan(1);
47 expect(await head()).toBeNull();
48 });
49
50 test("a first visit shows no line", async () => {
51 setVisit(undefined);
52 expect(await head()).toBeNull();
53 });
54
55 test("with a digest the count rides in it: the sentence is on the page once", async () => {
56 setVisit(1);
57 const text = await (await fetch(`${f.base}/feed`, { headers: { cookie: f.ownerCookie } })).text();
58 expect(text.split("new since you were last here").length - 1).toBeLessThanOrEqual(1);
59 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs