Public: anyone can read this; the crew can change it.
Every chip on the home feed filters the timeline or is not drawn, and a test presses each one #5815
scout/forge:scout/5801-every-chip-on-the-home-feed-filters-the → main 8 commits fork 8 threads · 8 unresolved
src/feed/chips.ts +50 −1
| @@ -4,7 +4,7 @@ | ||
| 4 | 4 | * actor; an item that matches neither stays, whatever else it is. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { type Chips, type KindChip } from "../core/feed_chips.ts"; | |
| 7 | import { KIND_CHIPS, type Chips, type KindChip } from "../core/feed_chips.ts"; | |
| 8 | 8 | import type { FeedKind } from "./items.ts"; |
| 9 | 9 | |
| 10 | 10 | /** Which chip covers a kind of post; the rest (runs, costs, maps…) have none. */ |
| @@ -29,3 +29,52 @@ export function chipHides( | ||
| 29 | 29 | if (chip !== undefined && chips.kinds.has(chip)) return true; |
| 30 | 30 | return item.actorId !== null && chips.people.has(item.actorId); |
| 31 | 31 | } |
| 32 | ||
| 33 | /** | |
| 34 | * How many posts each kind chip covers, asks included: an ask the home pins | |
| 35 | * above the timeline is a pull or a decision, and its chip hides it there. A | |
| 36 | * chip that counts none has nothing to filter and is not drawn. | |
| 37 | */ | |
| 38 | export function chipCounts( | |
| 39 | posts: readonly { readonly kind: FeedKind }[], | |
| 40 | asks: readonly { readonly pull?: unknown }[], | |
| 41 | ): Record<KindChip, number> { | |
| 42 | const n: Record<KindChip, number> = { reviews: 0, decisions: 0, crew: 0, releases: 0 }; | |
| 43 | for (const p of posts) { | |
| 44 | const chip = CHIP_OF[p.kind]; | |
| 45 | if (chip !== undefined) n[chip]++; | |
| 46 | } | |
| 47 | for (const a of asks) n[a.pull !== undefined ? "reviews" : "decisions"]++; | |
High: the home renders no pinned asks on this base. |
||
| 48 | return n; | |
| 49 | } | |
| 50 | ||
| 51 | /** The chips to draw: those with posts to filter, and those switched off (so they can come back). */ | |
| 52 | export const chipDrawn = (chips: Chips, kind: KindChip, count: number): boolean => | |
| 53 | count > 0 || chips.kinds.has(kind); | |
| 54 | ||
| 55 | const CHIP_LABEL: Record<KindChip, string> = { | |
| 56 | reviews: "Pull requests", | |
| 57 | decisions: "Decisions", | |
| 58 | crew: "Crew words", | |
| 59 | releases: "Releases", | |
| 60 | }; | |
| 61 | ||
| 62 | /** | |
| 63 | * The kind chips to draw, with their counts: what pressing each would hide on | |
| 64 | * the page in hand (its posts) and among the asks pinned above it. A chip | |
| 65 | * with nothing to hide is left out unless the viewer switched it off. The web | |
| 66 | * and the MCP both read this, from the same first page. | |
| 67 | */ | |
| 68 | export function kindChips( | |
| 69 | chips: Chips, | |
| 70 | posts: readonly { readonly kind: FeedKind }[], | |
| 71 | asks: readonly { readonly pull?: unknown }[], | |
| 72 | ): { chip: KindChip; label: string; count: number; off: boolean }[] { | |
| 73 | const counts = chipCounts(posts, asks); | |
| 74 | return KIND_CHIPS.filter((k) => chipDrawn(chips, k, counts[k])).map((k) => ({ | |
| 75 | chip: k, | |
| 76 | label: CHIP_LABEL[k], | |
| 77 | count: counts[k], | |
| 78 | off: chips.kinds.has(k), | |
| 79 | })); | |
| 80 | } |
src/feed/feed.ts +2
| @@ -22,6 +22,8 @@ import { settledOf } from "./settled.ts"; | ||
| 22 | 22 | |
| 23 | 23 | export const FEED_LIMIT_DEFAULT = 20; |
| 24 | 24 | export const FEED_LIMIT_MAX = 50; |
| 25 | /** The posts the home draws first; the chips are counted on the same page. */ | |
| 26 | export const FEED_FIRST_PAGE = 20; | |
| 25 | 27 | |
| 26 | 28 | const CURSOR = /^(-?\d{1,6}(?:\.\d{1,6})?)\|([a-z]{2,12}:[A-Za-z0-9_.:-]{1,160})$/; |
| 27 | 29 |
src/http/mcp/tools/feed_chips.ts +10 −1
| @@ -1,5 +1,9 @@ | ||
| 1 | 1 | import { getUserByName, getUsersByIds } from "../../../core/users.ts"; |
| 2 | 2 | import { chipsOf, isKindChip, setChip } from "../../../core/feed_chips.ts"; |
| 3 | import { kindChips } from "../../../feed/chips.ts"; | |
outdated
The MCP tool reaching into |
||
| 4 | import { FEED_FIRST_PAGE, readFeedPage } from "../../../feed/feed.ts"; | |
| 5 | import { listReposVisibleTo } from "../../../core/repos.ts"; | |
| 6 | import { waitingOn } from "../../../web/studio/asks.ts"; | |
outdated
The MCP tool should not import from a page module. Move |
||
| 3 | 7 | import { KIND_HAS_NO_CHIP } from "../../../actions/feed_words.ts"; |
| 4 | 8 | import { good, bad, type Tool, str } from "../shared.ts"; |
| 5 | 9 | import { HIDDEN_WORDS } from "../../../core/arg_words.ts"; |
| @@ -8,7 +12,7 @@ export const tool: Tool = { | ||
| 8 | 12 | name: "feed_chips", |
| 9 | 13 | doc: "`{ kind?, value?, hidden? }`: the feed chips you switched off, which `feed` and `asks` leave out; with `kind` (`kind` or `person`), `value` and `hidden` it switches one off or on", |
| 10 | 14 | description: |
| 11 | "Feed chips you switched off. With `kind`, `value` and `hidden`, sets one.", | |
| 15 | "Feed chips with counts; those you switched off apply in `feed` and `asks`. With `kind`, `value` and `hidden`, sets one.", | |
| 12 | 16 | inputSchema: { |
| 13 | 17 | type: "object", |
| 14 | 18 | properties: { |
| @@ -34,11 +38,16 @@ export const tool: Tool = { | ||
| 34 | 38 | } else return bad("invalid", "kind is `kind` or `person`"); |
| 35 | 39 | } |
| 36 | 40 | const chips = chipsOf(ctx.app.db, user.id); |
| 41 | // The chips are counted on the page the home draws: its first page and the asks pinned above it. | |
| 42 | const page = readFeedPage(ctx.app.db, user, { limit: FEED_FIRST_PAGE, withoutAsks: true }); | |
| 43 | const asks = waitingOn(ctx.app.db, user, listReposVisibleTo(ctx.app.db, user, { limit: 200, withoutArchived: true }), chips); | |
| 37 | 44 | return good({ |
| 38 | 45 | kinds: [...chips.kinds].sort(), |
| 39 | 46 | people: [...getUsersByIds(ctx.app.db, [...chips.people]).values()] |
| 40 | 47 | .map((u) => u.username) |
| 41 | 48 | .sort(), |
| 49 | // The chips the home draws, with the posts each would hide. | |
| 50 | chips: kindChips(chips, page.items, asks).map((k) => ({ kind: k.chip, label: k.label, count: k.count, hidden: k.off })), | |
| 42 | 51 | }); |
| 43 | 52 | }, |
| 44 | 53 | }; |
src/web/studio/feed.ts +14 −13
| @@ -8,14 +8,15 @@ | ||
| 8 | 8 | * Every string on a card is plain text and is escaped here, once. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { chipsOf, KIND_CHIPS, type KindChip } from "../../core/feed_chips.ts"; | |
| 11 | import { chipsOf, type Chips } from "../../core/feed_chips.ts"; | |
| 12 | 12 | import { marksByName, type Mark } from "../../core/marks.ts"; |
| 13 | 13 | import { getUsersByIds, type User } from "../../core/users.ts"; |
| 14 | 14 | import type { Ctx } from "../../http/context.ts"; |
| 15 | 15 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 16 | 16 | import { placementsOf, type Placement } from "../../core/placements.ts"; |
| 17 | 17 | import { recentThoughts, type Thought } from "../../core/thoughts.ts"; |
| 18 | import { parseCursor, readFeedPage } from "../../feed/feed.ts"; | |
| 18 | import { kindChips } from "../../feed/chips.ts"; | |
| 19 | import { FEED_FIRST_PAGE, parseCursor, readFeedPage } from "../../feed/feed.ts"; | |
| 19 | 20 | import type { FeedItem } from "../../feed/rank.ts"; |
| 20 | 21 | import { getAppByRepo } from "../../core/apps.ts"; |
| 21 | 22 | import { getRepo, getRepoById } from "../../core/repos.ts"; |
| @@ -26,14 +27,15 @@ import { html, type Html } from "../html.ts"; | ||
| 26 | 27 | import { fragment } from "../layout.ts"; |
| 27 | 28 | import { draftJs } from "../draft.ts"; |
| 28 | 29 | import { outboxJs } from "../outbox.ts"; |
| 29 | import { avatar, btn, card, disclosure, empty, nameMark, stateBadge, time } from "../ui.ts"; | |
| 30 | import { writableAppsOf } from "./asks.ts"; | |
| 30 | import { avatar, btn, card, disclosure, empty, nameMark, pill, stateBadge, time } from "../ui.ts"; | |
| 31 | import { listReposVisibleTo } from "../../core/repos.ts"; | |
| 32 | import { waitingOn, writableAppsOf } from "./asks.ts"; | |
| 31 | 33 | |
| 32 | 34 | /** Events that can change what the feed says. */ |
| 33 | 35 | export const FEED_LIVE = |
| 34 | 36 | "run.finished,issue.opened,issue.commented,pull.commented,pull.opened,pull.merged,pull.reviewed,pull.closed,look.taken,app.serving,issue.blocked,issue.unblocked,crew.governed,issue.closed,decision.*,thought.received,thought.placed"; |
| 35 | 37 | |
| 36 | export const FEED_FIRST_PAGE = 20; | |
| 38 | export { FEED_FIRST_PAGE }; | |
| 37 | 39 | |
| 38 | 40 | const RUN_LINK = /^\/[^/]+\/[^/]+\/runs\/[A-Za-z0-9_-]+$/; |
| 39 | 41 | |
| @@ -254,12 +256,9 @@ export function feedNote(): Html { | ||
| 254 | 256 | ></p>`; |
| 255 | 257 | } |
| 256 | 258 | |
outdated
Drop this. It is a |
||
| 257 | const CHIP_LABEL: Record<KindChip, string> = { | |
| 258 | reviews: "Pull requests", | |
| 259 | decisions: "Decisions", | |
| 260 | crew: "Crew words", | |
| 261 | releases: "Releases", | |
| 262 | }; | |
| 259 | /** The asks pinned above the page, which a chip hides there: one read, scored once. */ | |
| 260 | const askedOf = (ctx: Ctx, user: User, chips: Chips) => | |
| 261 | waitingOn(ctx.app.db, user, listReposVisibleTo(ctx.app.db, user, { limit: 200, withoutArchived: true }), chips); | |
outdated
This doc block now sits right under |
||
| 263 | 262 | |
| 264 | 263 | /** |
| 265 | 264 | * The chips: a button per kind and per person in sight (and per person |
| @@ -275,7 +274,7 @@ function chipsBar(user: User, ctx: Ctx, items: readonly FeedItem[]): Html { | ||
| 275 | 274 | ]); |
outdated
High: on every home render and live refresh this is a second full rank of the feed (limit 50), and the next line adds |
||
| 276 | 275 | names.delete(user.username); |
| 277 | 276 | const hiddenNames = new Set(hidden.map((u) => u.username)); |
| 278 | const chip = (kind: string, value: string, label: string, shown: boolean): Html => | |
| 277 | const chip = (kind: string, value: string, label: Html | string, shown: boolean): Html => | |
| 279 | 278 | html`<button |
| 280 | 279 | type="button" |
| 281 | 280 | class="btn feed-chip" data-variant="ghost" data-size="sm" |
| @@ -284,7 +283,9 @@ function chipsBar(user: User, ctx: Ctx, items: readonly FeedItem[]): Html { | ||
| 284 | 283 | aria-pressed="${String(shown)}" |
| 285 | 284 | >${label}</button>`; |
| 286 | 285 | return html`<div class="bar feed-chips" role="group" aria-label="Filter the feed"> |
| 287 | ${KIND_CHIPS.map((k) => chip("kind", k, CHIP_LABEL[k], !chips.kinds.has(k)))} | |
| 286 | ${kindChips(chips, items, askedOf(ctx, user, chips)).map((k) => | |
| 287 | chip("kind", k.chip, html`${k.label}${k.count > 0 ? pill(k.count) : null}`, !k.off), | |
| 288 | )} | |
| 288 | 289 | ${[...names].sort().map((n) => chip("person", n, n, !hiddenNames.has(n)))} |
| 289 | 290 | </div>`; |
| 290 | 291 | } |
test/asks-mute.test.ts +23
| @@ -227,6 +227,29 @@ describe("muting a repository's asks", () => { | ||
| 227 | 227 | expect(lines()).toBe(1); |
| 228 | 228 | }); |
| 229 | 229 | |
| 230 | test("a pending ask draws its chip with the count, and pressing it removes the pinned ask", async () => { | |
| 231 | const token = createToken(app.db, alice.id, "rw", "rw").token; | |
| 232 | const region = () => fetch(`${servers.url}/live/feed`, { headers: headers(cookie) }).then((r) => r.text()); | |
| 233 | const reviews = /data-chip="kind"\s+data-value="reviews"\s+aria-pressed="(\w+)"[^>]*>\s*Pull requests\s*<span class="pill"\s*>2<\/span\s*>/; | |
| 234 | // Two: the pull's post in the timeline and the ask pinned above it (without the asks it reads 1). | |
This pins the wrong behaviour: the 2 counts an ask the home does not draw. The home should read 1 (the pull's post). Pressing the chip removing the ask is a fact about |
||
| 235 | for (const v of ["reviews", "decisions", "crew"] as const) setChip(app.db, alice.id, { kind: "kind", value: v }, false); | |
| 236 | expect((await region()).match(reviews)?.[1]).toBe("true"); | |
| 237 | const mcp = (await mcpTool(servers.url, token, "feed_chips")).value; | |
| 238 | expect(mcp.chips).toContainEqual({ kind: "reviews", label: "Pull requests", count: 2, hidden: false }); | |
| 239 | expect(await onYou()).toContain("Muted work"); | |
| 240 | const press = await fetch(`${servers.url}/api/v1/feed/chips`, { | |
| 241 | method: "POST", | |
| 242 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 243 | body: JSON.stringify({ kind: "kind", value: "reviews", hidden: true }), | |
| 244 | }); | |
| 245 | expect(press.status).toBe(200); | |
| 246 | expect(await onYou()).not.toContain("Muted work"); | |
| 247 | // Switched off it stays drawn, to come back. | |
| 248 | expect((await region()).match(/data-value="reviews"\s+aria-pressed="(\w+)"/)?.[1]).toBe("false"); | |
| 249 | setChip(app.db, alice.id, { kind: "kind", value: "reviews" }, false); | |
| 250 | expect(await onYou()).toContain("Muted work"); | |
| 251 | }); | |
| 252 | ||
| 230 | 253 | test("MCP asks applies the saved filter, and takes the same filter as arguments", async () => { |
| 231 | 254 | const token = createToken(app.db, alice.id, "ro", "ro").token; |
| 232 | 255 | // Its own preconditions, not the earlier tests' leftovers: listening, and no chip saved. |
test/feed-chips.test.ts +23
| @@ -215,4 +215,27 @@ describe("the chips of a forge", () => { | ||
| 215 | 215 | expect(((await tool(bobToken, "feed", { hide_kinds: ["x"] })) as any).error).toContain("hide_kinds"); |
| 216 | 216 | expect(((await tool(bobToken, "feed", { hide_people: [5] })) as any).error).toContain("hide_people"); |
| 217 | 217 | }); |
| 218 | test("every drawn chip changes what is visible when pressed, and carries its count", async () => { | |
| 219 | const cookie = { cookie: `forge_session=${createSession(app.db, bob.id).token}` }; | |
| 220 | const region = async () => (await fetch(`${servers.url}/live/feed`, { headers: cookie })).text(); | |
| 221 | const cards = (page: string) => [...page.matchAll(/data-kind="(\w+)"/g)].map((m) => m[1]!).sort(); | |
| 222 | const drawn = (page: string) => | |
| 223 | [...page.matchAll(/data-chip="kind"\s+data-value="(\w+)"[^>]*>\s*([^<]*?)\s*(?:<span class="pill"\s*>(\d+))?</g)].map((m) => [m[1]!, m[3] === undefined ? m[2]! : `${m[2]} ${m[3]}`]); | |
| 224 | const before = await region(); | |
| 225 | const chips = drawn(before); | |
| 226 | // Releases, reviews and crew words have posts; decisions has none and is not drawn. | |
| 227 | expect(chips.map(([k]) => k).sort()).toEqual(["crew", "releases", "reviews"]); | |
| 228 | expect(Object.fromEntries(chips)).toEqual({ crew: "Crew words 1", releases: "Releases 1", reviews: "Pull requests 1" }); | |
| 229 | const mcp = (await mcpTool(servers.url, bobToken, "feed_chips")).value; | |
| 230 | expect(mcp.chips.map((c: any) => [c.kind, c.count])).toEqual([["reviews", 1], ["crew", 1], ["releases", 1]]); | |
| 231 | for (const [chip] of chips) { | |
| 232 | await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: chip, hidden: true }); | |
| 233 | const off = await region(); | |
| 234 | expect(cards(off)).not.toEqual(cards(before)); | |
| 235 | // Switched off it stays drawn, with its count, so it can be switched back on. | |
| 236 | expect(drawn(off).find(([k]) => k === chip)).toBeDefined(); | |
| 237 | await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: chip, hidden: false }); | |
| 238 | expect(cards(await region())).toEqual(cards(before)); | |
| 239 | } | |
| 240 | }); | |
| 218 | 241 | }); |
test/mcp.test.ts +5 −5
| @@ -1515,14 +1515,14 @@ describe("the forge over MCP", () => { | ||
| 1515 | 1515 | return ((await res.json()) as { items: { kind: string }[] }).items.map((i) => i.kind); |
| 1516 | 1516 | }; |
| 1517 | 1517 | expect(await kinds()).toContain("release"); |
| 1518 | expect((await tool(rw, "feed_chips")).value).toEqual({ kinds: [], people: [] }); | |
| 1518 | expect((await tool(rw, "feed_chips")).value).toMatchObject({ kinds: [], people: [] }); | |
| 1519 | 1519 | |
| 1520 | 1520 | const set = await tool(rw, "feed_chips", { kind: "kind", value: "releases", hidden: true }); |
| 1521 | expect(set.value).toEqual({ kinds: ["releases"], people: [] }); | |
| 1521 | expect(set.value).toMatchObject({ kinds: ["releases"], people: [] }); | |
| 1522 | 1522 | expect(await kinds()).not.toContain("release"); |
| 1523 | 1523 | expect(await api()).not.toContain("release"); |
| 1524 | 1524 | // A read-only token may read the chips, not switch them. |
| 1525 | expect((await tool(ro, "feed_chips")).value).toEqual({ kinds: ["releases"], people: [] }); | |
| 1525 | expect((await tool(ro, "feed_chips")).value).toMatchObject({ kinds: ["releases"], people: [] }); | |
| 1526 | 1526 | expect((await tool(ro, "feed_chips", { kind: "kind", value: "releases", hidden: false })).error).toContain("forbidden"); |
| 1527 | 1527 | expect((await tool(rw, "feed_chips")).value.kinds).toEqual(["releases"]); |
| 1528 | 1528 | |
| @@ -1530,10 +1530,10 @@ describe("the forge over MCP", () => { | ||
| 1530 | 1530 | expect((await tool(rw, "feed_chips", { kind: "person", value: "nobody", hidden: true })).error).toBeDefined(); |
| 1531 | 1531 | expect((await tool(rw, "feed_chips", { kind: "kind", value: "crew" })).error).toBeDefined(); |
| 1532 | 1532 | const person = await tool(rw, "feed_chips", { kind: "person", value: "bob", hidden: true }); |
| 1533 | expect(person.value).toEqual({ kinds: ["releases"], people: ["bob"] }); | |
| 1533 | expect(person.value).toMatchObject({ kinds: ["releases"], people: ["bob"] }); | |
| 1534 | 1534 | await tool(rw, "feed_chips", { kind: "person", value: "bob", hidden: false }); |
| 1535 | 1535 | const back = await tool(rw, "feed_chips", { kind: "kind", value: "releases", hidden: false }); |
| 1536 | expect(back.value).toEqual({ kinds: [], people: [] }); | |
| 1536 | expect(back.value).toMatchObject({ kinds: [], people: [] }); | |
| 1537 | 1537 | expect(await kinds()).toContain("release"); |
| 1538 | 1538 | }); |
| 1539 | 1539 |
No test covers this branch. Add a seeded pending ask and assert that its chip is drawn with the count and that pressing it removes the pinned ask.