Public: anyone can read this; the crew can change it.
The feed has a Mine filter: only my tells, my replies and what the crew did on my repositories #12725
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12696-the-feed-has-a-mine-filter-only-my-tells → main 7 commits fork 3 threads · 3 unresolved
8 files
changed +351
−17
docs/features/feed-cursor.md +6
| @@ -3,3 +3,9 @@ | ||
| 3 | 3 | `GET /api/v1/feed?before=now` (also MCP `feed` with `before`, and `/feed?before=now` as a page) reads the log newest first: `{ items, next_cursor }`; pass `next_cursor` back as `before`. The cursor is an event id (a ULID: its time and its identity), resolved to the log's write order, so posts arriving between two requests land above the cursor and nothing repeats or skips. A cursor that is not an event id is a 422; a valid one only names where to start, and visibility is still the log query's. |
| 4 | 4 | |
| 5 | 5 | The ranked feed (`cursor=`) is unchanged; its page ends where its 300-event window does, and its last line links to "Everything, newest first". The timeline leaves out shares, asks and other standing items, and does not fold bursts. Code: `src/feed/timeline.ts`; test: `test/feed-cursor.test.ts`. |
| 6 | ||
| 7 | ## Mine | |
| 8 | ||
| 9 | `/feed?show=mine` is the timeline narrowed to the viewer's own: posts by them, replies to their posts, and what the crew did on repositories they administer (`src/feed/mine.ts`). It filters events the viewer may already read, so a private repository's post never reaches someone who cannot read it. The "Mine" link on the feed toggles it, the page says which filter is on, and the older-posts links and live updates keep it. Unknown `show` values are ignored. | |
| 10 | ||
| 11 | In the terminal, `m` on the Feed screen toggles only-mine (the same `mine` option of `readTimeline`), and `t` from there returns to the ranked feed. Code: `src/tui/screens/feed.ts`; test: `test/tui-feed-mine.test.ts`. |
src/feed/mine.ts added +63
| @@ -0,0 +1,63 @@ | ||
| 1 | /** | |
Both doors: Mine has no terminal screen. Add it in src/tui/ (feed screen toggle) using readTimeline({mine:true}), plus a test. |
||
| 2 | * "Mine": the posts the viewer owns a stake in. Posts by them, replies to | |
| 3 | * their posts, and what the crew (agents) did on repositories they | |
| 4 | * administer. It only narrows events the viewer may already read | |
| 5 | * (`timeline.ts` applies it after the visibility filters), so it can never | |
| 6 | * show a private repository's post to someone who cannot read it. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { canAdmin, type Repo } from "../core/repos.ts"; | |
| 11 | import type { ForgeEvent } from "../core/events.ts"; | |
| 12 | import { getIssuesByNumbers, numberKey } from "../core/issues.ts"; | |
| 13 | import { getUsersByIds, type User } from "../core/users.ts"; | |
| 14 | ||
| 15 | /** The values `?show=` takes; anything else is no filter. */ | |
| 16 | export const SHOW_VALUES = ["mine"] as const; | |
| 17 | export type Show = (typeof SHOW_VALUES)[number]; | |
| 18 | ||
| 19 | export function parseShow(raw: string | null): Show | undefined { | |
| 20 | return (SHOW_VALUES as readonly string[]).includes(raw ?? "") ? (raw as Show) : undefined; | |
| 21 | } | |
| 22 | ||
| 23 | const REPLIES = new Set(["issue.commented", "pull.commented"]); | |
| 24 | ||
| 25 | /** | |
| 26 | * A predicate over a batch of events: whether each is the viewer's. | |
| 27 | * One read per batch for subjects and actors. | |
| 28 | */ | |
| 29 | export function mineFilter( | |
| 30 | db: Database, | |
| 31 | viewer: User, | |
| 32 | events: readonly ForgeEvent[], | |
| 33 | repos: ReadonlyMap<string, Repo>, | |
| 34 | ): (e: ForgeEvent) => boolean { | |
| 35 | const issues = getIssuesByNumbers( | |
| 36 | db, | |
| 37 | events.flatMap((e) => | |
| 38 | REPLIES.has(e.kind) && typeof e.payload["number"] === "number" && e.repoId !== null | |
| 39 | ? [{ repoId: e.repoId, number: e.payload["number"] }] | |
| 40 | : [], | |
| 41 | ), | |
| 42 | ); | |
| 43 | const actors = getUsersByIds( | |
| 44 | db, | |
| 45 | events.flatMap((e) => (e.actorId !== null && e.actorId !== viewer.id ? [e.actorId] : [])), | |
| 46 | ); | |
| 47 | const admin = new Map<string, boolean>(); | |
| 48 | const administers = (repo: Repo): boolean => { | |
| 49 | if (!admin.has(repo.id)) admin.set(repo.id, canAdmin(db, repo, viewer)); | |
| 50 | return admin.get(repo.id)!; | |
| 51 | }; | |
| 52 | return (e) => { | |
| 53 | if (e.actorId === viewer.id) return true; | |
| 54 | if (REPLIES.has(e.kind) && typeof e.payload["number"] === "number" && e.repoId !== null) { | |
| 55 | if (issues.get(numberKey(e.repoId, e.payload["number"]))?.authorId === viewer.id) return true; | |
| 56 | } | |
| 57 | if (e.actorId !== null && actors.get(e.actorId)?.kind === "agent") { | |
| 58 | const repo = e.repoId === null ? undefined : repos.get(e.repoId); | |
| 59 | return repo !== undefined && administers(repo); | |
| 60 | } | |
| 61 | return false; | |
| 62 | }; | |
| 63 | } |
src/feed/timeline.ts +6 −1
| @@ -27,6 +27,7 @@ import type { User } from "../core/users.ts"; | ||
| 27 | 27 | import { chipHides } from "./chips.ts"; |
| 28 | 28 | import { decorate, FEED_LIMIT_DEFAULT, FEED_LIMIT_MAX } from "./feed.ts"; |
| 29 | 29 | import { eventCandidates } from "./items.ts"; |
| 30 | import { mineFilter } from "./mine.ts"; | |
| 30 | 31 | import { ago, type FeedItem } from "./rank.ts"; |
| 31 | 32 | |
| 32 | 33 | /** `before=now` starts at the newest post. */ |
| @@ -52,6 +53,8 @@ export function readTimeline( | ||
| 52 | 53 | limit?: number; |
| 53 | 54 | now?: number; |
| 54 | 55 | chips?: Chips; |
| 56 | /** Only the viewer's own (`mine.ts`). */ | |
| 57 | mine?: boolean; | |
| 55 | 58 | phase?: (name: string, ms: number) => void; |
| 56 | 59 | } = {}, |
| 57 | 60 | ): { items: FeedItem[]; next_cursor: string | null } { |
| @@ -76,7 +79,9 @@ export function readTimeline( | ||
| 76 | 79 | more = batch.length === BATCH; |
| 77 | 80 | const heard = batch.filter(audienceFilter(db, viewer, batch)); |
| 78 | 81 | const repos = getReposByIds(db, heard.map((e) => e.repoId!)); |
| 79 | for (const c of eventCandidates(db, viewer, now, heard, repos)) { | |
| 82 | const own = options.mine === true ? mineFilter(db, viewer, heard, repos) : undefined; | |
| 83 | const kept = own === undefined ? heard : heard.filter(own); | |
| 84 | for (const c of eventCandidates(db, viewer, now, kept, repos)) { | |
| 80 | 85 | if (chipHides(chips, c) || dismissed.has(c.id) || muted.has(c.repoId)) continue; |
| 81 | 86 | picked.push({ |
| 82 | 87 | event: c.id.slice("event:".length), |
src/tui/screens/feed.ts +16 −7
| @@ -4,11 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * A reply is a comment on the issue or pull the post links to: the same |
| 6 | 6 | * `addCommentThrough` the issue and pull screens call, offered on the same |
| 7 | * posts to the same people as the page's Reply (`feed/reply.ts`). `enter` on a folded burst lists its members; `l` likes | |
| 8 | * the post under the cursor and a second press takes it back: the page's | |
| 7 | * posts to the same people as the page's Reply (`feed/reply.ts`). `enter` on a folded burst | |
| 8 | * lists its members; `l` likes the post under the cursor and a second press takes it back: the page's | |
| 9 | 9 | * Like, behind the same `readInteraction` check (`feed/likes.ts`); `h` lists |
| 10 | 10 | * who liked or unliked it and when, newest first, behind that check too. Twenty |
| 11 | * posts at a time; `t` switches to every post, newest first (`readTimeline`); `n` reads the next page from the cursor the last returned. | |
| 11 | * posts at a time; `t` switches to every post, newest first (`readTimeline`); `m` narrows that to only mine (its `mine` option, the page's | |
| 12 | * `?show=mine`); `n` reads the next page from the cursor the last returned. | |
| 12 | 13 | */ |
| 13 | 14 | |
| 14 | 15 | import { addCommentThrough } from "../../actions/issues.ts"; |
| @@ -56,13 +57,16 @@ export function feedScreen(f: Session, ctx: Ctx): Screen { | ||
| 56 | 57 | let next: string | null = null; |
| 57 | 58 | // `t`: the log newest first, deeper than the ranked page reaches (the page's `?before=now`). |
| 58 | 59 | let timeline = false; |
| 60 | // `m`: only mine (`feed/mine.ts`), the page's `?show=mine`; read as the timeline is. | |
| 61 | let mine = false; | |
| 59 | 62 | |
| 60 | 63 | const read = (cursor?: string): void => { |
| 61 | 64 | if (f.user === undefined) return; |
| 62 | if (timeline) { | |
| 65 | if (timeline || mine) { | |
| 63 | 66 | const from = parseBefore(cursor ?? TIMELINE_START); |
| 64 | 67 | const page = readTimeline(f.db, f.user, { |
| 65 | 68 | limit: PAGE, |
| 69 | ...(mine ? { mine: true } : {}), | |
| 66 | 70 | ...(from?.id === undefined ? {} : { before: from.id }), |
| 67 | 71 | }); |
| 68 | 72 | items.push(...page.items); |
| @@ -146,7 +150,8 @@ export function feedScreen(f: Session, ctx: Ctx): Screen { | ||
| 146 | 150 | ...(items.length === 0 ? [] : [binding(["l"], "like")]), |
| 147 | 151 | ...(items.length === 0 ? [] : [binding(["h"], "who liked")]), |
| 148 | 152 | ...(next === null ? [] : [binding(["n"], "next page")]), |
| 149 | binding(["t"], timeline ? "ranked" : "newest first"), | |
| 153 | binding(["t"], timeline || mine ? "ranked" : "newest first"), | |
| 154 | binding(["m"], mine ? "everyone's" : "only mine"), | |
| 150 | 155 | binding(["esc", "q"], "back"), |
| 151 | 156 | ]; |
| 152 | 157 | }, |
| @@ -208,8 +213,12 @@ export function feedScreen(f: Session, ctx: Ctx): Screen { | ||
| 208 | 213 | } else if (e.key === "enter" && items[state.index]?.opens === true) members = items[state.index]!; |
| 209 | 214 | else if (e.key === "l") like(); |
| 210 | 215 | else if (e.key === "h") showHistory(); |
| 211 | else if (e.key === "t") { | |
| 212 | timeline = !timeline; | |
| 216 | else if (e.key === "t" || e.key === "m") { | |
| 217 | if (e.key === "m") mine = !mine; | |
| 218 | else { | |
| 219 | timeline = mine ? false : !timeline; | |
| 220 | mine = false; | |
| 221 | } | |
| 213 | 222 | items.length = 0; |
| 214 | 223 | state.index = 0; |
| 215 | 224 | state.offset = 0; |
src/web/studio/feed.ts +29 −8
| @@ -18,6 +18,7 @@ import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.t | ||
| 18 | 18 | import { placementsOf, type Placement } from "../../core/placements.ts"; |
| 19 | 19 | import { recentThoughts, type Thought } from "../../core/thoughts.ts"; |
| 20 | 20 | import { parseCursor, readFeedPage } from "../../feed/feed.ts"; |
| 21 | import { parseShow, type Show } from "../../feed/mine.ts"; | |
| 21 | 22 | import { parseBefore, readTimeline, TIMELINE_START } from "../../feed/timeline.ts"; |
| 22 | 23 | import type { FeedItem } from "../../feed/rank.ts"; |
| 23 | 24 | import { replyPath, replyRepo, threadOf } from "../../feed/reply.ts"; |
| @@ -340,6 +341,17 @@ function chipsBar(user: User, ctx: Ctx, items: readonly FeedItem[]): Html { | ||
| 340 | 341 | </div>`; |
| 341 | 342 | } |
| 342 | 343 | |
| 344 | /** | |
| 345 | * Mine: a plain link that toggles `?show=mine`; the page says which is on | |
| 346 | * (`aria-current`, "Mine ✓"); the chip is the one signal. No script. | |
| 347 | */ | |
| 348 | function mineChip(show: Show | undefined): Html { | |
| 349 | const on = show === "mine"; | |
| 350 | return html`<p class="bar small" data-show="${on ? "mine" : "all"}"> | |
| 351 | <a class="btn" data-variant="ghost" data-size="sm" href="${on ? "/feed" : "/feed?show=mine"}" ${on ? html`aria-current="true"` : html``}>${on ? "Mine ✓" : "Mine"}</a> | |
Principle 1: this sentence and the Mine ✓ chip say the same thing, and "Showing everything" explains the default. Keep one signal (the chip with aria-current) and cut the prose. |
||
| 352 | </p>`; | |
| 353 | } | |
| 354 | ||
| 343 | 355 | /** What the region shows, read: the posts, the thoughts, where they sit, who posted. */ |
| 344 | 356 | function readRegion(user: User, ctx: Ctx) { |
| 345 | 357 | // What needs you is pinned above the timeline (`needsYouSection`), so an |
| @@ -347,7 +359,11 @@ function readRegion(user: User, ctx: Ctx) { | ||
| 347 | 359 | // `?cursor=` is a page the line below the last post pointed at; one not issued here is the first page. |
| 348 | 360 | const cursor = parseCursor(ctx.url.searchParams.get("cursor") ?? ""); |
| 349 | 361 | // `?before=` is the timeline, newest first, deeper than the ranked page reaches (`feed/timeline.ts`). |
| 350 | const before = parseBefore(ctx.url.searchParams.get("before") ?? ""); | |
| 362 | // `?show=mine` narrows the timeline to the viewer's own, so it is the timeline. | |
| 363 | const show = parseShow(ctx.url.searchParams.get("show")); | |
| 364 | const before = | |
| 365 | parseBefore(ctx.url.searchParams.get("before") ?? "") ?? | |
| 366 | (show === undefined ? undefined : { id: undefined }); | |
| 351 | 367 | const page = |
| 352 | 368 | before === undefined |
| 353 | 369 | ? readFeedPage(ctx.app.db, user, { |
| @@ -359,13 +375,14 @@ function readRegion(user: User, ctx: Ctx) { | ||
| 359 | 375 | : { |
| 360 | 376 | ...readTimeline(ctx.app.db, user, { |
| 361 | 377 | limit: FEED_FIRST_PAGE, |
| 378 | mine: show === "mine", | |
| 362 | 379 | phase: (name, ms) => notePhase(ctx.request, name, ms), |
| 363 | 380 | ...(before.id === undefined ? {} : { before: before.id }), |
| 364 | 381 | }), |
| 365 | 382 | quiet: { count: 0, repoId: null }, |
| 366 | 383 | }; |
| 367 | 384 | const timeline = before !== undefined; |
| 368 | const asked = ctx.url.searchParams.get("before") ?? ""; | |
| 385 | const asked = timeline ? (ctx.url.searchParams.get("before") || TIMELINE_START) : ""; | |
| 369 | 386 | const items = page.items; |
| 370 | 387 | // The day the page before ended on, so a continuing page draws no divider twice (`day_divider.ts`). |
| 371 | 388 | const day = cursor === undefined && !timeline ? "" : (ctx.url.searchParams.get("day") ?? ""); |
| @@ -380,7 +397,7 @@ function readRegion(user: User, ctx: Ctx) { | ||
| 380 | 397 | const quiet = page.quiet; |
| 381 | 398 | const quietRepo = quiet.repoId === null ? undefined : getRepoById(ctx.app.db, quiet.repoId); |
| 382 | 399 | const quietLink = quietRepo === undefined ? "/explore" : `/${quietRepo.ownerName}/${quietRepo.name}/runs?kind=agent`; |
| 383 | return { items, next: page.next_cursor, cursor, timeline, asked, lastDayOnPage, thoughts, placed, marks, pictures: picturesFor(ctx, items), quiet: quiet.count, quietLink }; | |
| 400 | return { items, next: page.next_cursor, cursor, timeline, show, asked, lastDayOnPage, thoughts, placed, marks, pictures: picturesFor(ctx, items), quiet: quiet.count, quietLink }; | |
| 384 | 401 | } |
| 385 | 402 | |
| 386 | 403 | /** The one line an empty timeline says; the tell box above it is the action. */ |
| @@ -395,16 +412,18 @@ export function feedRegion(user: User, ctx: Ctx): Html { | ||
| 395 | 412 | * The line under the last post: how far back the page reaches and the way |
| 396 | 413 | * to older ones, or that there is nothing older. |
| 397 | 414 | */ |
| 398 | export function caughtUp(items: readonly FeedItem[], next: string | null, cursor?: unknown, timeline = false): Html | null { | |
| 415 | export function caughtUp(items: readonly FeedItem[], next: string | null, cursor?: unknown, timeline = false, show?: Show): Html | null { | |
| 416 | const keep = show === undefined ? "" : `&show=${show}`; | |
| 417 | const only = show === undefined ? "" : `?show=${show}`; | |
| 399 | 418 | // Ranked by score, not time: the last card is not always the oldest. |
| 400 | 419 | const oldest = items.reduce<string | undefined>((min, item) => (min === undefined || item.at < min ? item.at : min), undefined); |
| 401 | 420 | // A page the filters emptied can still have older posts behind it: keep the way on. |
| 402 | 421 | if (oldest === undefined && next === null) return null; |
| 403 | const newest = cursor === undefined && !timeline ? html`` : html`<a href="/feed">Newest</a>`; | |
| 422 | const newest = cursor === undefined && !timeline ? html`` : html`<a href="/feed${only}">Newest</a>`; | |
| 404 | 423 | // The ranked page ends where its window does; the log goes on, newest first. |
| 405 | 424 | const deeper = timeline ? html`` : html`<a href="/feed?before=${TIMELINE_START}">Everything, newest first</a>`; |
| 406 | 425 | const older = (n: string) => timeline |
| 407 | ? html`<a href="/feed?before=${encodeURIComponent(n)}">Older posts</a>` | |
| 426 | ? html`<a href="/feed?before=${encodeURIComponent(n)}${keep}">Older posts</a>` | |
| 408 | 427 | : html`<a href="/feed?cursor=${encodeURIComponent(n)}&day=${encodeURIComponent(items[items.length - 1]?.at ?? "")}">Older posts</a>`; |
| 409 | 428 | return next === null |
| 410 | 429 | ? 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>` |
| @@ -417,7 +436,7 @@ export function caughtUp(items: readonly FeedItem[], next: string | null, cursor | ||
| 417 | 436 | } |
| 418 | 437 | |
| 419 | 438 | function drawRegion(user: User, ctx: Ctx, read: ReturnType<typeof readRegion>): Html { |
| 420 | const { items, next, cursor, timeline, asked, lastDayOnPage, thoughts, placed, marks, pictures, quiet, quietLink } = read; | |
| 439 | const { items, next, cursor, timeline, show, asked, lastDayOnPage, thoughts, placed, marks, pictures, quiet, quietLink } = read; | |
| 421 | 440 | const now = new Date(); |
| 422 | 441 | // One divider per change of day between neighbours, in the order the feed already has. |
| 423 | 442 | const posts: Html[] = []; |
| @@ -437,6 +456,7 @@ function drawRegion(user: User, ctx: Ctx, read: ReturnType<typeof readRegion>): | ||
| 437 | 456 | } |
| 438 | 457 | const liveQuery = [ |
| 439 | 458 | timeline ? `before=${encodeURIComponent(asked)}` : "", |
| 459 | show === undefined ? "" : `show=${show}`, | |
| 440 | 460 | cursor === undefined ? "" : `cursor=${encodeURIComponent(`${cursor.score}|${cursor.id}`)}`, |
| 441 | 461 | cursor === undefined || lastDayOnPage === undefined ? "" : `day=${encodeURIComponent(lastDayOnPage)}`, |
| 442 | 462 | ].filter(Boolean).join("&"); |
| @@ -449,6 +469,7 @@ function drawRegion(user: User, ctx: Ctx, read: ReturnType<typeof readRegion>): | ||
| 449 | 469 | data-live-hold |
| 450 | 470 | data-live-src="/live/feed${liveQuery === "" ? "" : `?${liveQuery}`}" |
| 451 | 471 | > |
| 472 | ${mineChip(show)} | |
| 452 | 473 | ${chipsBar(user, ctx, items)} |
| 453 | 474 | ${thoughts.map((t) => thoughtCard(t, user.username, placed.get(t.id)))} |
| 454 | 475 | ${items.length === 0 && thoughts.length === 0 |
| @@ -457,7 +478,7 @@ function drawRegion(user: User, ctx: Ctx, read: ReturnType<typeof readRegion>): | ||
| 457 | 478 | ${quiet === 0 |
| 458 | 479 | ? "" |
| 459 | 480 | : html`<p class="small muted"><a href="${quietLink}">${quiet} quiet ${quiet === 1 ? "run" : "runs"} in the last 24 h</a></p>`} |
| 460 | ${caughtUp(items, next, cursor, timeline)} | |
| 481 | ${caughtUp(items, next, cursor, timeline, show)} | |
| 461 | 482 | </section>`; |
| 462 | 483 | } |
| 463 | 484 |
src/web/studio/home_last.ts +7 −1
| @@ -17,6 +17,7 @@ import { notePhase } from "../../core/route_timing.ts"; | ||
| 17 | 17 | import { forgetAll, recall, remember } from "../../core/region_memory.ts"; |
| 18 | 18 | import { writeStamp } from "../../core/write_clock.ts"; |
| 19 | 19 | import { parseCursor } from "../../feed/feed.ts"; |
| 20 | import { parseShow } from "../../feed/mine.ts"; | |
| 20 | 21 | import { parseBefore } from "../../feed/timeline.ts"; |
| 21 | 22 | import { escapeHtml, raw, type Html } from "../html.ts"; |
| 22 | 23 | import { HOME_MAX_AGE_MS } from "./home_cache.ts"; |
| @@ -29,7 +30,10 @@ export type HomeRegion = (typeof HOME_REGIONS)[number]; | ||
| 29 | 30 | const REDRAW_GAP_MS = 5_000; |
| 30 | 31 | const drawing = new Map<string, number>(); |
| 31 | 32 | |
| 32 | /** The query string home's regions read, rebuilt from what validates: `all`, a feed `cursor` and its `day`. */ | |
| 33 | /** | |
| 34 | * The query string home's regions read, rebuilt from what validates: | |
| 35 | * `all`, a feed `cursor` and its `day`, `before`, `show`. | |
| 36 | */ | |
| 33 | 37 | export function homeVariant(url: URL): string { |
| 34 | 38 | const q = new URLSearchParams(); |
| 35 | 39 | if (url.searchParams.get("all") !== null) q.set("all", "1"); |
| @@ -41,6 +45,8 @@ export function homeVariant(url: URL): string { | ||
| 41 | 45 | } |
| 42 | 46 | const before = url.searchParams.get("before") ?? ""; |
| 43 | 47 | if (before.length <= 26 && parseBefore(before) !== undefined) q.set("before", before); |
| 48 | const show = parseShow(url.searchParams.get("show")); | |
| 49 | if (show !== undefined) q.set("show", show); | |
| 44 | 50 | const text = q.toString(); |
| 45 | 51 | return text === "" ? "" : `?${text}`; |
| 46 | 52 | } |
test/feed-mine.test.ts added +157
| @@ -0,0 +1,157 @@ | ||
| 1 | /** | |
| 2 | * `?show=mine` narrows the timeline to the viewer's own (`feed/mine.ts`): | |
| 3 | * their posts, replies to their posts, the crew's posts on repositories they | |
| 4 | * administer; never someone else's, never a private repository's for a | |
| 5 | * non-reader. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createIssue } from "../src/core/issues.ts"; | |
| 14 | import { createSession } from "../src/core/sessions.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { mineFilter, parseShow } from "../src/feed/mine.ts"; | |
| 19 | import { readTimeline } from "../src/feed/timeline.ts"; | |
| 20 | import { promote } from "./helpers/members.ts"; | |
| 21 | ||
| 22 | describe("the Mine filter", () => { | |
| 23 | let dataDir: string; | |
| 24 | let app: App; | |
| 25 | let servers: Servers; | |
| 26 | let alice: User; | |
| 27 | let bob: User; | |
| 28 | let mineRepo: Repo; | |
| 29 | let bobRepo: Repo; | |
| 30 | let secret: Repo; | |
| 31 | let aliceCookie: string; | |
| 32 | const links = (viewer: User, mine: boolean) => | |
| 33 | readTimeline(app.db, viewer, { mine, limit: 50 }).items.map((i) => i.link); | |
| 34 | ||
| 35 | const issue = (repo: Repo, author: User, title: string) => { | |
| 36 | const r = createIssue(app.db, { repoId: repo.id, authorId: author.id, title }); | |
| 37 | if (!r.ok) throw new Error(r.error); | |
| 38 | app.bus.append({ kind: "issue.opened", repoId: repo.id, actorId: author.id, payload: { number: r.value.number } }); | |
| 39 | return r.value.number; | |
| 40 | }; | |
| 41 | const comment = (repo: Repo, actor: User, number: number) => | |
| 42 | app.bus.append({ kind: "issue.commented", repoId: repo.id, actorId: actor.id, payload: { number } }); | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | dataDir = await mkdtemp(join(tmpdir(), "forge-feed-mine-")); | |
| 46 | app = createApp({ dataDir, port: 0 }); | |
| 47 | servers = await serve(app); | |
| 48 | const mk = async (username: string, first = false) => { | |
| 49 | const r = await createUser(app.db, { | |
| 50 | origin: "seed", trusted: true, username, allowReserved: true, | |
| 51 | email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first, | |
| 52 | }); | |
| 53 | if (!r.ok) throw new Error(r.error); | |
| 54 | promote(app.db, r.value); | |
| 55 | return r.value; | |
| 56 | }; | |
| 57 | await mk("root", true); | |
| 58 | alice = await mk("alice"); | |
| 59 | bob = await mk("bob"); | |
| 60 | aliceCookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 61 | app.db.run("INSERT INTO user (id, username, email, password_hash, is_agent) VALUES ('agent-scout', 'scout', '[email protected]', '', 1)"); | |
| 62 | const a = await createRepo(app, alice, { name: "mine", visibility: "public" }); | |
| 63 | const b = await createRepo(app, bob, { name: "theirs", visibility: "public" }); | |
| 64 | const s = await createRepo(app, bob, { name: "secret", visibility: "private" }); | |
| 65 | if (!a.ok || !b.ok || !s.ok) throw new Error("repos"); | |
| 66 | mineRepo = a.value; | |
| 67 | bobRepo = b.value; | |
| 68 | secret = s.value; | |
| 69 | }, 30_000); | |
| 70 | ||
| 71 | afterAll(async () => { | |
| 72 | servers.stop(); | |
| 73 | app.close(); | |
| 74 | await rm(dataDir, { recursive: true, force: true }); | |
| 75 | }); | |
| 76 | ||
| 77 | test("my post, a reply to it and the crew on my repository appear; another person's post does not", () => { | |
| 78 | const mineN = issue(bobRepo, alice, "alice asks bob"); | |
| 79 | const replyTo = issue(bobRepo, alice, "alice again"); | |
| 80 | const bobsN = issue(bobRepo, bob, "bob's own"); | |
| 81 | comment(bobRepo, bob, replyTo); // a reply to alice's post | |
| 82 | comment(bobRepo, bob, bobsN); // not a reply to alice's | |
| 83 | app.bus.append({ kind: "pull.opened", repoId: mineRepo.id, actorId: "agent-scout", payload: { number: 9 } }); | |
| 84 | app.bus.append({ kind: "pull.opened", repoId: bobRepo.id, actorId: "agent-scout", payload: { number: 8 } }); | |
| 85 | ||
| 86 | const all = links(alice, false); | |
| 87 | const only = links(alice, true); | |
| 88 | expect(only).toContain(`/bob/theirs/issues/${mineN}`); | |
| 89 | expect(only).toContain(`/bob/theirs/issues/${replyTo}`); // the reply links to her post | |
| 90 | expect(only).toContain("/alice/mine/pulls/9"); // crew on my repository | |
| 91 | expect(only).not.toContain("/bob/theirs/pulls/8"); // crew on someone else's | |
| 92 | expect(only).not.toContain(`/bob/theirs/issues/${bobsN}`); | |
| 93 | expect(all).toContain(`/bob/theirs/issues/${bobsN}`); // the filter is what removed it | |
| 94 | expect(only.length).toBeLessThan(all.length); | |
| 95 | }); | |
| 96 | ||
| 97 | test("a comment on my pull counts as a reply to me, as one on my issue does", () => { | |
| 98 | // No `issue.opened` event: the comment is the only post that names each conversation. | |
| 99 | const bare = (author: User, title: string) => { | |
| 100 | const r = createIssue(app.db, { repoId: bobRepo.id, authorId: author.id, title }); | |
| 101 | if (!r.ok) throw new Error(r.error); | |
| 102 | return r.value.number; | |
| 103 | }; | |
| 104 | const n = bare(alice, "alice's pull stands as a conversation"); | |
| 105 | const other = bare(bob, "bob's conversation"); | |
| 106 | for (const number of [n, other]) | |
| 107 | app.bus.append({ kind: "pull.commented", repoId: bobRepo.id, actorId: bob.id, payload: { number } }); | |
| 108 | const only = links(alice, true); | |
| 109 | const all = links(alice, false); | |
| 110 | expect(all).toContain(`/bob/theirs/pulls/${other}`); | |
| 111 | expect(only).toContain(`/bob/theirs/pulls/${n}`); | |
| 112 | expect(only).not.toContain(`/bob/theirs/pulls/${other}`); | |
| 113 | }); | |
| 114 | ||
| 115 | test("a private post of someone else does not reach a non-reader, filter or not", () => { | |
| 116 | const n = issue(secret, bob, "bob's secret"); | |
| 117 | comment(secret, bob, n); | |
| 118 | expect(links(alice, true).some((l) => l.includes("/bob/secret/"))).toBe(false); | |
| 119 | expect(links(bob, true).some((l) => l.includes("/bob/secret/"))).toBe(true); | |
| 120 | }); | |
| 121 | ||
| 122 | test("the filter alone, before any audience filter, says no to what is not the viewer's", () => { | |
| 123 | const n = issue(secret, bob, "bob's other secret"); | |
| 124 | const events = [ | |
| 125 | comment(secret, bob, n), // someone else's reply to someone else's post | |
| 126 | app.bus.append({ kind: "pull.opened", repoId: secret.id, actorId: "agent-scout", payload: { number: 7 } }), | |
| 127 | app.bus.append({ kind: "issue.opened", repoId: secret.id, actorId: bob.id, payload: { number: n } }), | |
| 128 | ]; | |
| 129 | const repos = new Map([[secret.id, secret]]); | |
| 130 | const forAlice = mineFilter(app.db, alice, events, repos); | |
| 131 | expect(events.map(forAlice)).toEqual([false, false, false]); | |
| 132 | const forBob = mineFilter(app.db, bob, events, repos); | |
| 133 | expect(events.map(forBob)).toEqual([true, true, true]); // the same events are his | |
| 134 | }); | |
| 135 | ||
| 136 | test("?show= takes allowlisted values only", () => { | |
| 137 | expect(parseShow("mine")).toBe("mine"); | |
| 138 | expect(parseShow("everything")).toBeUndefined(); | |
| 139 | expect(parseShow(null)).toBeUndefined(); | |
| 140 | }); | |
| 141 | ||
| 142 | test("the page narrows, says which filter is on, and keeps it in its links", async () => { | |
| 143 | const get = async (q: string) => | |
| 144 | (await fetch(`${servers.url}/feed${q}`, { headers: { cookie: aliceCookie } })).text(); | |
| 145 | const on = await get("?show=mine"); | |
| 146 | expect(on).toContain('data-show="mine"'); | |
| 147 | expect(on).toContain("alice asks bob"); | |
| 148 | expect(on).not.toContain("bob's own"); | |
| 149 | expect(on).toContain('data-live-src="/live/feed?before=now&show=mine"'); | |
| 150 | const off = await get("?show=bogus"); | |
| 151 | expect(off).toContain('data-show="all"'); | |
| 152 | expect(off).toContain('href="/feed?show=mine"'); | |
| 153 | expect(on).toContain('aria-current="true"'); | |
| 154 | expect(on).not.toContain("Showing"); | |
| 155 | expect(off).not.toContain("Showing"); | |
| 156 | }); | |
| 157 | }); |
test/tui-feed-mine.test.ts added +67
| @@ -0,0 +1,67 @@ | ||
| 1 | /** In the terminal `m` narrows the feed to the viewer's own, as the page's `?show=mine` does. */ | |
| 2 | import { afterAll, beforeAll, 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 { openIssue } from "../src/actions/issues.ts"; | |
| 7 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 10 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 11 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | import { promote } from "./helpers/members.ts"; | |
| 14 | import { settleApp } from "./helpers/tui.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | let dir = ""; | |
| 19 | let forge: App; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | dir = await mkdtemp(join(tmpdir(), "forge-tui-feed-mine-")); | |
| 23 | forge = createApp({ dataDir: dir, port: 0 }); | |
| 24 | for (const [username, first] of [["root", true], ["alice", false], ["bob", false]] as const) { | |
| 25 | const made = await createUser(forge.db, { | |
| 26 | origin: "seed", trusted: true, allowReserved: true, username, email: `${username}@forge.local`, | |
| 27 | password: "a-good-passphrase", adminIfFirst: first, | |
| 28 | }); | |
| 29 | if (!made.ok) throw new Error(made.error); | |
| 30 | promote(forge.db, made.value); | |
| 31 | } | |
| 32 | const alice = getUserByName(forge.db, "alice")!; | |
| 33 | const bob = getUserByName(forge.db, "bob")!; | |
| 34 | const repo = await createRepo(forge, bob, { name: "shared", visibility: "public" }); | |
| 35 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 36 | await commitToBranch(forge, bob, repo.value, { message: "seed", files: [{ path: "README.md", content: "# s\n" }] }); | |
| 37 | for (const [who, title] of [[alice, "Alices question"], [bob, "Bobs question"]] as const) { | |
| 38 | const r = openIssue(forge, who, repo.value, { title, body: "?" }); | |
| 39 | if (!r.ok) throw new Error(String(r.error)); | |
| 40 | } | |
| 41 | }); | |
| 42 | ||
| 43 | afterAll(async () => { | |
| 44 | forge.db.close(); | |
| 45 | await rm(dir, { recursive: true, force: true }); | |
| 46 | }); | |
| 47 | ||
| 48 | test("m shows only my posts, and a second m brings everyone's back", async () => { | |
| 49 | const terminal = headlessTerminal(140, 32); | |
| 50 | const identity = { sshUsername: "t", remoteAddress: "127.0.0.1", user: getUserByName(forge.db, "alice")! }; | |
| 51 | const { app } = createForgeApp(forge, identity, { terminal, exitHooks: false }); | |
| 52 | app.start(); | |
| 53 | const press = async (...keys: string[]) => { | |
| 54 | for (const k of keys) app.dispatch({ type: "key", key: k, ctrl: false, alt: false, shift: false }); | |
| 55 | await settleApp(app); | |
| 56 | }; | |
| 57 | app.dispatch({ type: "key", key: "k", ctrl: true, alt: false, shift: false }); | |
| 58 | await settleApp(app); | |
| 59 | await press(..."Feed", "enter"); | |
| 60 | expect(app.screen.toText()).toContain("Bobs question"); | |
| 61 | await press("m"); | |
| 62 | expect(app.screen.toText()).toContain("Alices question"); | |
| 63 | expect(app.screen.toText()).not.toContain("Bobs question"); | |
| 64 | await press("m"); | |
| 65 | expect(app.screen.toText()).toContain("Bobs question"); | |
| 66 | app.stop(); | |
| 67 | }); |
new above
offline
Add the terminal: on the Feed screen
mtoggles only-mine (the samemineoption ofreadTimeline), andtreturns to the ranked feed.