Public: anyone can read this; the crew can change it.
Kind and people chips filter the home, the inbox and /api/v1/feed, saved per user #1731
scout/forge:scout/1598-kind-and-people-chips-filter-the-home-th → main 2 commits fork
13 files
changed +546
−5
docs/FEED.md +6
| @@ -10,6 +10,12 @@ look`. `what` is the post in its own words; `reason` the sentence for why it | ||
| 10 | 10 | is there. Strings are plain text, not HTML: whatever renders them escapes |
| 11 | 11 | them. The MCP tool `feed { limit, cursor }` answers the same. |
| 12 | 12 | |
| 13 | Chips: `GET /api/v1/feed/chips` → `{ kinds, people }`, what the viewer hid; | |
| 14 | `POST` `{ kind }` (`pulls decisions words releases`) or `{ person }` (a | |
| 15 | username), with `hidden: false` to show again (`rw`). A chip removes what it | |
| 16 | names from the feed, the asks (inbox, `/api/v1/asks`) and the home's apps | |
| 17 | rail, and nothing else. On the home they are buttons above the timeline. | |
| 18 | ||
| 13 | 19 | `POST /api/v1/feed/interactions` `{ item, kind, app? }` records what the viewer |
| 14 | 20 | did (`opened dwelled steered told swiped shipped shared dismissed muted`). |
| 15 | 21 | Same-origin checked for browser sessions, `rw` for tokens. An `event:` item |
scripts/gate.ts +1
| @@ -28,6 +28,7 @@ export const GATE: readonly string[] = [ | ||
| 28 | 28 | "test/accounts-retire.test.ts", |
| 29 | 29 | "test/accounts.test.ts", |
| 30 | 30 | "test/asks-mute.test.ts", |
| 31 | "test/feed-chips.test.ts", | |
| 31 | 32 | "test/agents.test.ts", |
| 32 | 33 | "test/app-domains.test.ts", |
| 33 | 34 | "test/app.test.ts", |
src/core/feed_chips.ts added +112
| @@ -0,0 +1,112 @@ | ||
| 1 | /** | |
| 2 | * The chips a viewer turned off on their home, inbox and feed (#1598): kinds | |
| 3 | * of post and people. Pure data, saved per viewer beside the mutes; a chip | |
| 4 | * removes what it names and nothing else. Whoever records one owes it only | |
| 5 | * the viewer's own id — a chip is never about anyone else's view. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { getUserByName, getUsersByIds } from "./users.ts"; | |
| 10 | ||
| 11 | /** The kind chips and the feed kinds each one names. */ | |
| 12 | export const KIND_CHIPS = { | |
| 13 | pulls: ["pull"], | |
| 14 | decisions: ["decision"], | |
| 15 | words: ["tell", "reply"], | |
| 16 | releases: ["release"], | |
| 17 | } as const; | |
| 18 | export type KindChip = keyof typeof KIND_CHIPS; | |
| 19 | export const KIND_CHIP_NAMES = Object.keys(KIND_CHIPS) as KindChip[]; | |
| 20 | ||
| 21 | export const KIND_CHIP_LABEL: Record<KindChip, string> = { | |
| 22 | pulls: "Pulls to review", | |
| 23 | decisions: "Decisions", | |
| 24 | words: "The crew's words", | |
| 25 | releases: "Releases", | |
| 26 | }; | |
| 27 | ||
| 28 | export const isKindChip = (v: unknown): v is KindChip => | |
| 29 | typeof v === "string" && Object.hasOwn(KIND_CHIPS, v); | |
| 30 | ||
| 31 | /** The most people one viewer may hide: a bound, not a feature. */ | |
| 32 | export const PEOPLE_CHIPS_MAX = 100; | |
| 33 | ||
| 34 | export interface Chips { | |
| 35 | /** Kinds hidden. */ | |
| 36 | readonly kinds: ReadonlySet<KindChip>; | |
| 37 | /** User ids hidden. */ | |
| 38 | readonly people: ReadonlySet<string>; | |
| 39 | } | |
| 40 | ||
| 41 | export function chipsFor(db: Database, viewerId: string): Chips { | |
| 42 | const kinds = new Set<KindChip>(); | |
| 43 | const people = new Set<string>(); | |
| 44 | for (const r of db | |
| 45 | .query<{ type: string; value: string }, [string]>( | |
| 46 | "SELECT type, value FROM feed_chip WHERE viewer_id = ?1", | |
| 47 | ) | |
| 48 | .all(viewerId)) { | |
| 49 | if (r.type === "kind" && isKindChip(r.value)) kinds.add(r.value); | |
| 50 | else if (r.type === "person") people.add(r.value); | |
| 51 | } | |
| 52 | return { kinds, people }; | |
| 53 | } | |
| 54 | ||
| 55 | /** Whether any chip is set: the common viewer pays for nothing else. */ | |
| 56 | export const anyChips = (c: Chips): boolean => c.kinds.size > 0 || c.people.size > 0; | |
| 57 | ||
| 58 | /** The feed kinds the hidden kind chips name. */ | |
| 59 | export function hiddenFeedKinds(c: Chips): Set<string> { | |
| 60 | const out = new Set<string>(); | |
| 61 | for (const k of c.kinds) for (const f of KIND_CHIPS[k]) out.add(f); | |
| 62 | return out; | |
| 63 | } | |
| 64 | ||
| 65 | /** Hide (`hidden`) or show a kind of post. Twice is the same as once. */ | |
| 66 | export function setKindChip(db: Database, viewerId: string, kind: KindChip, hidden: boolean): void { | |
| 67 | setChip(db, viewerId, "kind", kind, hidden); | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Hide or show one person by username. A name that is no account, or the | |
| 72 | * viewer's own, is refused: `"unknown"` or `"self"`; past the bound, `"full"`. | |
| 73 | */ | |
| 74 | export function setPersonChip( | |
| 75 | db: Database, | |
| 76 | viewerId: string, | |
| 77 | username: string, | |
| 78 | hidden: boolean, | |
| 79 | ): "ok" | "unknown" | "self" | "full" { | |
| 80 | const user = getUserByName(db, username); | |
| 81 | if (!user) return "unknown"; | |
| 82 | if (user.id === viewerId) return "self"; | |
| 83 | if (hidden && chipsFor(db, viewerId).people.size >= PEOPLE_CHIPS_MAX) { | |
| 84 | if (!chipsFor(db, viewerId).people.has(user.id)) return "full"; | |
| 85 | } | |
| 86 | setChip(db, viewerId, "person", user.id, hidden); | |
| 87 | return "ok"; | |
| 88 | } | |
| 89 | ||
| 90 | function setChip( | |
| 91 | db: Database, | |
| 92 | viewerId: string, | |
| 93 | type: "kind" | "person", | |
| 94 | value: string, | |
| 95 | hidden: boolean, | |
| 96 | ): void { | |
| 97 | if (hidden) | |
| 98 | db.query( | |
| 99 | "INSERT OR IGNORE INTO feed_chip (viewer_id, type, value) VALUES (?1, ?2, ?3)", | |
| 100 | ).run(viewerId, type, value); | |
| 101 | else | |
| 102 | db.query( | |
| 103 | "DELETE FROM feed_chip WHERE viewer_id = ?1 AND type = ?2 AND value = ?3", | |
| 104 | ).run(viewerId, type, value); | |
| 105 | } | |
| 106 | ||
| 107 | /** The usernames of the hidden people, in order. */ | |
| 108 | export function hiddenPeopleNames(db: Database, c: Chips): string[] { | |
| 109 | return [...getUsersByIds(db, [...c.people]).values()] | |
| 110 | .map((u) => u.username) | |
| 111 | .sort(); | |
| 112 | } |
src/core/schema.ts +13
| @@ -1705,6 +1705,19 @@ export const migrations: readonly string[] = [ | ||
| 1705 | 1705 | ); |
| 1706 | 1706 | CREATE INDEX passkey_user ON passkey(user_id); |
| 1707 | 1707 | `, |
| 1708 | // 83 — feed chips (#1598): what a viewer chose not to see on their home, | |
| 1709 | // inbox and feed. A `kind` row hides a kind of post (`pulls`, `decisions`, | |
| 1710 | // `words`, `releases`); a `person` row hides one account's posts. Kept | |
| 1711 | // beside the mutes, one row per choice, per viewer. | |
| 1712 | ` | |
| 1713 | CREATE TABLE feed_chip ( | |
| 1714 | viewer_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE, | |
| 1715 | type TEXT NOT NULL CHECK (type IN ('kind', 'person')), | |
| 1716 | value TEXT NOT NULL, | |
| 1717 | created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), | |
| 1718 | PRIMARY KEY (viewer_id, type, value) | |
| 1719 | ); | |
| 1720 | `, | |
| 1708 | 1721 | ]; |
| 1709 | 1722 | |
| 1710 | 1723 | /** |
src/feed/feed.ts +12 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import type { Database } from "bun:sqlite"; |
| 10 | import { anyChips, chipsFor, hiddenFeedKinds } from "../core/feed_chips.ts"; | |
| 10 | 11 | import { dismissedItems, mutedRepos } from "../core/interactions.ts"; |
| 11 | 12 | import { affinityFor } from "./affinity.ts"; |
| 12 | 13 | import { canRead, getRepoById } from "../core/repos.ts"; |
| @@ -56,7 +57,17 @@ export function readFeed( | ||
| 56 | 57 | : !dismissed.has(c.id) && |
| 57 | 58 | (c.kind === "ask" || c.kind === "milestone" || !muted.has(c.repoId)), |
| 58 | 59 | ); |
| 59 | let ranked = rank(candidates, affinityFor(db, viewer, now), now); | |
| 60 | // The viewer's chips (`core/feed_chips.ts`): each removes what it names. | |
| 61 | const chips = chipsFor(db, viewer.id); | |
| 62 | const hiddenKinds = hiddenFeedKinds(chips); | |
| 63 | const visible = anyChips(chips) | |
| 64 | ? candidates.filter( | |
| 65 | (c) => | |
| 66 | !hiddenKinds.has(c.kind) && | |
| 67 | !(c.actorId !== null && chips.people.has(c.actorId)), | |
| 68 | ) | |
| 69 | : candidates; | |
| 70 | let ranked = rank(visible, affinityFor(db, viewer, now), now); | |
| 60 | 71 | const cursor = options.cursor; |
| 61 | 72 | if (cursor) { |
| 62 | 73 | const at = ranked.findIndex((i) => i.id === cursor.id); |
src/feed/items.ts +2 −1
| @@ -39,6 +39,7 @@ export type FeedKind = | ||
| 39 | 39 | | "tell" |
| 40 | 40 | | "reply" |
| 41 | 41 | | "look" |
| 42 | | "release" | |
| 42 | 43 | | "milestone"; |
| 43 | 44 | |
| 44 | 45 | /** What a candidate is, before it is ranked and worded. */ |
| @@ -333,7 +334,7 @@ function fromEvent( | ||
| 333 | 334 | case "look.taken": |
| 334 | 335 | return { ...common, kind: "look", link: base(repo), what: "a new picture of it is ready" }; |
| 335 | 336 | case "app.serving": |
| 336 | return { ...common, kind: "pull", link: base(repo), what: "a new version is serving" }; | |
| 337 | return { ...common, kind: "release", link: base(repo), what: "a new version is serving" }; | |
| 337 | 338 | // A tell is a post; a comment is a reply under it, in its own words. |
| 338 | 339 | case "issue.opened": { |
| 339 | 340 | if (number === undefined) return undefined; |
src/http/feed.ts +48
| @@ -9,6 +9,16 @@ import { | ||
| 9 | 9 | UNVERIFIED_WORDS, |
| 10 | 10 | VARIANTS_MAX, |
| 11 | 11 | } from "../actions/tell.ts"; |
| 12 | import type { Database } from "bun:sqlite"; | |
| 13 | import { | |
| 14 | KIND_CHIP_NAMES, | |
| 15 | PEOPLE_CHIPS_MAX, | |
| 16 | chipsFor, | |
| 17 | hiddenPeopleNames, | |
| 18 | isKindChip, | |
| 19 | setKindChip, | |
| 20 | setPersonChip, | |
| 21 | } from "../core/feed_chips.ts"; | |
| 12 | 22 | import { DIRECTION_NOTE } from "../crew/direct.ts"; |
| 13 | 23 | import { audienceFilter } from "../core/event_audience.ts"; |
| 14 | 24 | import { getEvent } from "../core/events.ts"; |
| @@ -44,6 +54,14 @@ import { | ||
| 44 | 54 | unauthorized, |
| 45 | 55 | } from "./respond.ts"; |
| 46 | 56 | |
| 57 | function chipsJson(db: Database, viewerId: string) { | |
| 58 | const chips = chipsFor(db, viewerId); | |
| 59 | return { | |
| 60 | kinds: KIND_CHIP_NAMES.filter((k) => chips.kinds.has(k)), | |
| 61 | people: hiddenPeopleNames(db, chips), | |
| 62 | }; | |
| 63 | } | |
| 64 | ||
| 47 | 65 | export function feedRoutes(router: Router<Handler>): void { |
| 48 | 66 | router.add("GET", "/api/v1/feed", (ctx) => { |
| 49 | 67 | const viewer = ctx.auth.user; |
| @@ -70,6 +88,36 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 70 | 88 | ); |
| 71 | 89 | }); |
| 72 | 90 | |
| 91 | // The viewer's chips: what they hid, and hide or show one more. | |
| 92 | router.add("GET", "/api/v1/feed/chips", (ctx) => { | |
| 93 | const viewer = ctx.auth.user; | |
| 94 | if (!viewer) return unauthorized(); | |
| 95 | return json(chipsJson(ctx.app.db, viewer.id), 200, { | |
| 96 | "cache-control": "private, no-store", | |
| 97 | }); | |
| 98 | }); | |
| 99 | ||
| 100 | router.add("POST", "/api/v1/feed/chips", async (ctx) => { | |
| 101 | const denied = requireScope(ctx, "rw"); | |
| 102 | if (denied) return denied; | |
| 103 | const viewer = ctx.auth.user!; | |
| 104 | const input = await body(ctx.request); | |
| 105 | const hidden = input.hidden === undefined ? true : input.hidden; | |
| 106 | if (typeof hidden !== "boolean") | |
| 107 | return apiError("invalid_hidden", 422, "hidden is true or false"); | |
| 108 | if (input.kind !== undefined) { | |
| 109 | if (!isKindChip(input.kind)) | |
| 110 | return apiError("invalid_kind", 422, `kind is one of ${KIND_CHIP_NAMES.join(", ")}`); | |
| 111 | setKindChip(ctx.app.db, viewer.id, input.kind, hidden); | |
| 112 | } else { | |
| 113 | const done = setPersonChip(ctx.app.db, viewer.id, str(input.person), hidden); | |
| 114 | if (done === "unknown") return notFound(); | |
| 115 | if (done === "self") return apiError("invalid_person", 422, "you cannot hide yourself"); | |
| 116 | if (done === "full") return apiError("too_many", 422, `at most ${PEOPLE_CHIPS_MAX} people`); | |
| 117 | } | |
| 118 | return json(chipsJson(ctx.app.db, viewer.id), 201); | |
| 119 | }); | |
| 120 | ||
| 73 | 121 | // What asks the viewer, ranked, with the week's measure (`asksAnswer`). |
| 74 | 122 | router.add("GET", "/api/v1/asks", (ctx) => { |
| 75 | 123 | const viewer = ctx.auth.user; |
src/http/server.ts +2
| @@ -209,6 +209,7 @@ import { | ||
| 209 | 209 | asksPage, |
| 210 | 210 | } from "../web/studio/asks_page.ts"; |
| 211 | 211 | import { liveFeedFragment } from "../web/studio/feed.ts"; |
| 212 | import { feedChipsSubmit } from "../web/studio/feed_chips.ts"; | |
| 212 | 213 | import { serviceWorker } from "../web/sw.ts"; |
| 213 | 214 | import { explorePage } from "../web/studio/explore.ts"; |
| 214 | 215 | import { |
| @@ -595,6 +596,7 @@ export function buildRouter(): Router<Handler> { | ||
| 595 | 596 | router.add("POST", "/asks/bulk/:id/undo", asksBulkUndo); |
| 596 | 597 | router.add("POST", "/asks/listen", asksListen); |
| 597 | 598 | router.add("POST", "/asks/mute", asksMute); |
| 599 | router.add("POST", "/feed/chips", feedChipsSubmit); | |
| 598 | 600 | router.add("POST", "/asks/decide", asksDecide); |
| 599 | 601 | router.add("POST", "/asks/decide/:id/undo", asksDecideUndo); |
| 600 | 602 | // What people asked of the crew, and what became of it (core/asked.ts). |
src/web/studio/asks.ts +13 −2
| @@ -16,6 +16,7 @@ import { appsRuntime } from "../../apps/runtime.ts"; | ||
| 16 | 16 | import { appsForRepos, getAppByRepo, listApps } from "../../core/apps.ts"; |
| 17 | 17 | import { latestDecision, stopStands } from "../../core/decisions.ts"; |
| 18 | 18 | import { getHold, holdsFor } from "../../core/holds.ts"; |
| 19 | import { anyChips, chipsFor, hiddenPeopleNames } from "../../core/feed_chips.ts"; | |
| 19 | 20 | import { askDismissals, mutedRepos } from "../../core/interactions.ts"; |
| 20 | 21 | import { askMeasure, syncAskLedger, type AskEnd } from "../../core/ask_ledger.ts"; |
| 21 | 22 | import { |
| @@ -487,12 +488,22 @@ function notDismissed<T extends AskCandidate>( | ||
| 487 | 488 | ): T[] { |
| 488 | 489 | const muted = mutedRepos(db, user.id); |
| 489 | 490 | const dismissed = askDismissals(db, user.id); |
| 490 | if (dismissed.size === 0 && muted.size === 0) return [...asks]; | |
| 491 | // Chips (`core/feed_chips.ts`): a hidden person's asks, and the asks of a | |
| 492 | // hidden kind — those about a pull, or the ones about no pull at all. | |
| 493 | const chips = chipsFor(db, user.id); | |
| 494 | const people = new Set(hiddenPeopleNames(db, chips).map((n) => n.toLowerCase())); | |
| 495 | const chipped = (a: T): boolean => | |
| 496 | people.has(a.who.toLowerCase()) || | |
| 497 | (a.pull !== undefined ? chips.kinds.has("pulls") : chips.kinds.has("decisions")); | |
| 498 | if (dismissed.size === 0 && muted.size === 0 && !anyChips(chips)) return [...asks]; | |
| 491 | 499 | const holds = (id: string, since: string): boolean => |
| 492 | 500 | (dismissed.get(`ask:${id}`) ?? "") > since; |
| 493 | 501 | return asks.filter( |
| 494 | 502 | (a) => |
| 495 | !muted.has(a.repo.id) && !holds(a.id, a.since) && !holds(groupId(a), a.since), | |
| 503 | !muted.has(a.repo.id) && | |
| 504 | !chipped(a) && | |
| 505 | !holds(a.id, a.since) && | |
| 506 | !holds(groupId(a), a.since), | |
| 496 | 507 | ); |
| 497 | 508 | } |
| 498 | 509 |
src/web/studio/feed.ts +1
| @@ -38,6 +38,7 @@ const KIND_LABEL: Record<FeedItem["kind"], string> = { | ||
| 38 | 38 | tell: "Told", |
| 39 | 39 | reply: "Reply", |
| 40 | 40 | look: "Look", |
| 41 | release: "Release", | |
| 41 | 42 | }; |
| 42 | 43 | |
| 43 | 44 | /** |
src/web/studio/feed_chips.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** | |
| 2 | * The chips above the feed (#1598): a button for each kind of post and each | |
| 3 | * hidden person, one tap to hide or show. Plain forms that post and come | |
| 4 | * home, so there is no script and no layout shift; the choice is saved per | |
| 5 | * viewer (`core/feed_chips.ts`) and the feed, the inbox and the apps rail | |
| 6 | * read it. | |
| 7 | */ | |
| 8 | ||
| 9 | import { | |
| 10 | KIND_CHIP_LABEL, | |
| 11 | KIND_CHIP_NAMES, | |
| 12 | chipsFor, | |
| 13 | hiddenPeopleNames, | |
| 14 | isKindChip, | |
| 15 | setKindChip, | |
| 16 | setPersonChip, | |
| 17 | } from "../../core/feed_chips.ts"; | |
| 18 | import type { User } from "../../core/users.ts"; | |
| 19 | import type { Ctx } from "../../http/context.ts"; | |
| 20 | import { signInFirst } from "../chrome.ts"; | |
| 21 | import { readForm } from "../form.ts"; | |
| 22 | import { html, type Html } from "../html.ts"; | |
| 23 | import { redirect } from "../layout.ts"; | |
| 24 | ||
| 25 | const USERNAME = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/; | |
| 26 | ||
| 27 | export function chipsBar(ctx: Ctx, user: User): Html { | |
| 28 | const chips = chipsFor(ctx.app.db, user.id); | |
| 29 | const chip = (field: "kind" | "person", value: string, label: string, hidden: boolean): Html => | |
| 30 | html`<form method="post" action="/feed/chips" class="inline"> | |
| 31 | <input type="hidden" name="${field}" value="${value}" /> | |
| 32 | <input type="hidden" name="hide" value="${hidden ? "0" : "1"}" /> | |
| 33 | <button | |
| 34 | type="submit" | |
| 35 | class="btn btn-sm${hidden ? " btn-ghost" : ""}" | |
| 36 | aria-pressed="${hidden ? "false" : "true"}" | |
| 37 | title="${hidden ? "Show" : "Hide"} ${label}" | |
| 38 | > | |
| 39 | ${hidden ? "Show" : ""} ${label} | |
| 40 | </button> | |
| 41 | </form>`; | |
| 42 | return html`<div class="bar feed-chips" id="feed-chips" aria-label="Filter the feed"> | |
| 43 | ${KIND_CHIP_NAMES.map((k) => chip("kind", k, KIND_CHIP_LABEL[k], chips.kinds.has(k)))} | |
| 44 | ${hiddenPeopleNames(ctx.app.db, chips).map((n) => chip("person", n, n, true))} | |
| 45 | <form method="post" action="/feed/chips" class="inline"> | |
| 46 | <input type="hidden" name="hide" value="1" /> | |
| 47 | <input | |
| 48 | type="text" | |
| 49 | name="person" | |
| 50 | class="small" | |
| 51 | placeholder="Hide a person" | |
| 52 | aria-label="Hide a person" | |
| 53 | maxlength="64" | |
| 54 | autocomplete="off" | |
| 55 | /> | |
| 56 | </form> | |
| 57 | </div>`; | |
| 58 | } | |
| 59 | ||
| 60 | /** POST /feed/chips — hide or show one kind or one person for the viewer. */ | |
| 61 | export async function feedChipsSubmit(ctx: Ctx): Promise<Response> { | |
| 62 | const user = ctx.auth.user; | |
| 63 | if (!user) return signInFirst(ctx); | |
| 64 | const form = await readForm(ctx, "rw"); | |
| 65 | if (form instanceof Response) return form; | |
| 66 | const hide = form.get("hide") !== "0"; | |
| 67 | const kind = form.get("kind"); | |
| 68 | const person = form.get("person"); | |
| 69 | if (typeof kind === "string" && kind !== "") { | |
| 70 | if (!isKindChip(kind)) return new Response("bad request", { status: 400 }); | |
| 71 | setKindChip(ctx.app.db, user.id, kind, hide); | |
| 72 | } else if (typeof person === "string" && person.trim() !== "") { | |
| 73 | const name = person.trim().replace(/^@/, ""); | |
| 74 | if (!USERNAME.test(name)) return new Response("bad request", { status: 400 }); | |
| 75 | // A name that is no one is not said: nothing changes. | |
| 76 | setPersonChip(ctx.app.db, user.id, name, hide); | |
| 77 | } else return new Response("bad request", { status: 400 }); | |
| 78 | return redirect("/"); | |
| 79 | } |
src/web/studio/home.ts +10 −1
| @@ -16,6 +16,7 @@ import { | ||
| 16 | 16 | } from "../../actions/front.ts"; |
| 17 | 17 | import { appsRuntime } from "../../apps/runtime.ts"; |
| 18 | 18 | import { appsForRepos } from "../../core/apps.ts"; |
| 19 | import { chipsFor, hiddenPeopleNames } from "../../core/feed_chips.ts"; | |
| 19 | 20 | import { WEIGHT_MAX, WEIGHT_NAMES } from "../../core/front.ts"; |
| 20 | 21 | import { LOOK_SIZES } from "../../core/looks.ts"; |
| 21 | 22 | import { canRead, getRepo, type Repo } from "../../core/repos.ts"; |
| @@ -46,6 +47,7 @@ import { | ||
| 46 | 47 | } from "../pages/site.ts"; |
| 47 | 48 | import { writableAppsOf } from "./asks.ts"; |
| 48 | 49 | import { needsYouSection, type AsksTop } from "./asks_page.ts"; |
| 50 | import { chipsBar } from "./feed_chips.ts"; | |
| 49 | 51 | import { composer, feedJs, feedNote, feedRegion } from "./feed.ts"; |
| 50 | 52 | import { homeTabs } from "./home_tabs.ts"; |
| 51 | 53 | import { crewKeyNote } from "./crew.ts"; |
| @@ -67,7 +69,13 @@ const RAIL_APPS = 8; | ||
| 67 | 69 | * The apps' full rows are `/apps`. |
| 68 | 70 | */ |
| 69 | 71 | function yourApps(ctx: Ctx, user: User): Html { |
| 70 | const { repos } = writableAppsOf(ctx, user); | |
| 72 | // An app of a person the viewer hid is not on their rail (`core/feed_chips.ts`). | |
| 73 | const hidden = new Set( | |
| 74 | hiddenPeopleNames(ctx.app.db, chipsFor(ctx.app.db, user.id)).map((n) => n.toLowerCase()), | |
| 75 | ); | |
| 76 | const repos = writableAppsOf(ctx, user).repos.filter( | |
| 77 | (r) => !hidden.has(r.ownerName.toLowerCase()), | |
| 78 | ); | |
| 71 | 79 | const runtime = appsRuntime(ctx.app); |
| 72 | 80 | const records = appsForRepos( |
| 73 | 81 | ctx.app.db, |
| @@ -197,6 +205,7 @@ export async function studioHome( | ||
| 197 | 205 | <a class="small muted" href="/asked">What you asked for</a> |
| 198 | 206 | </div> |
| 199 | 207 | ${feedNote()} |
| 208 | ${chipsBar(ctx, user)} | |
| 200 | 209 | ${feedRegion(user, ctx)} |
| 201 | 210 | </div> |
| 202 | 211 | <aside class="dash-rail stack home-rail"> |
test/feed-chips.test.ts added +247
| @@ -0,0 +1,247 @@ | ||
| 1 | /** | |
| 2 | * Kind and people chips (#1598): each removes what it names and nothing | |
| 3 | * else, from the feed (`readFeed`, `/api/v1/feed`), the asks (the inbox and | |
| 4 | * `/api/v1/asks`), the home's apps rail (`/live/apps`); saved per viewer. | |
| 5 | */ | |
| 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 { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { assignIssue } from "../src/actions/issues.ts"; | |
| 14 | import { openPull } from "../src/actions/pulls.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { createApp as createAppRecord } from "../src/core/apps.ts"; | |
| 17 | import { chipsFor, setKindChip, setPersonChip } from "../src/core/feed_chips.ts"; | |
| 18 | import { getIssueById } from "../src/core/issues.ts"; | |
| 19 | import type { Repo } from "../src/core/repos.ts"; | |
| 20 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; | |
| 21 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 22 | import { readFeed } from "../src/feed/feed.ts"; | |
| 23 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 24 | import { askLinesFor } from "../src/web/studio/asks.ts"; | |
| 25 | import { quietLogs } from "./helpers/globals.ts"; | |
| 26 | ||
| 27 | quietLogs(); | |
| 28 | ||
| 29 | const PASSWORD = "a-long-chips-passphrase"; | |
| 30 | ||
| 31 | describe("feed chips", () => { | |
| 32 | let dataDir = ""; | |
| 33 | let app: App; | |
| 34 | let servers: Servers; | |
| 35 | let alice: User; | |
| 36 | let bob: User; | |
| 37 | let cookie = ""; | |
| 38 | let repo: Repo; | |
| 39 | ||
| 40 | const headers = (c: string) => ({ cookie: `${c}; forge_mode=studio`, origin: servers.url }); | |
| 41 | const post = (path: string, fields: [string, string][], c = cookie) => | |
| 42 | fetch(`${servers.url}${path}`, { | |
| 43 | method: "POST", | |
| 44 | headers: headers(c), | |
| 45 | body: new URLSearchParams(fields), | |
| 46 | redirect: "manual", | |
| 47 | }); | |
| 48 | const apiFeed = async (): Promise<{ kind: string; actor: string | null }[]> => | |
| 49 | ( | |
| 50 | (await fetch(`${servers.url}/api/v1/feed`, { headers: headers(cookie) }).then((r) => | |
| 51 | r.json(), | |
| 52 | )) as { items: { kind: string; actor: string | null }[] } | |
| 53 | ).items; | |
| 54 | const kinds = () => readFeed(app.db, alice).items.map((i) => i.kind); | |
| 55 | ||
| 56 | beforeAll(async () => { | |
| 57 | dataDir = await mkdtemp(join(tmpdir(), "forge-feed-chips-")); | |
| 58 | app = createApp({ dataDir, port: 0 }); | |
| 59 | servers = await serve(app); | |
| 60 | for (const name of ["alice", "bob"]) { | |
| 61 | const made = await createUser(app.db, { | |
| 62 | trusted: true, | |
| 63 | username: name, | |
| 64 | email: `${name}@forge.local`, | |
| 65 | password: PASSWORD, | |
| 66 | adminIfFirst: name === "alice", | |
| 67 | }); | |
| 68 | if (!made.ok) throw new Error(made.error); | |
| 69 | if (name === "alice") alice = made.value; | |
| 70 | else bob = made.value; | |
| 71 | } | |
| 72 | const res = await fetch(`${servers.url}/signin`, { | |
| 73 | method: "POST", | |
| 74 | body: new URLSearchParams({ username: "alice", password: PASSWORD }), | |
| 75 | redirect: "manual", | |
| 76 | }); | |
| 77 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 78 | await createAgent(app, alice, { username: "bot" }); | |
| 79 | const made = await createRepo(app, alice, { name: "chipped", visibility: "public" }); | |
| 80 | if (!made.ok) throw new Error(String(made.error)); | |
| 81 | repo = made.value; | |
| 82 | await commitToBranch(app, alice, repo, { | |
| 83 | message: "seed", | |
| 84 | files: [{ path: "README.md", content: "# chipped\n" }], | |
| 85 | }); | |
| 86 | addToCrew(app, alice, repo, "bot"); | |
| 87 | await commitToBranch(app, alice, repo, { | |
| 88 | branch: "bot/1", | |
| 89 | base: "main", | |
| 90 | message: "Chipped work", | |
| 91 | files: [{ path: "F.md", content: "1\n" }], | |
| 92 | }); | |
| 93 | const opened = await openPull(app, getUserByName(app.db, "bot")!, repo, { | |
| 94 | title: "Chipped work", | |
| 95 | body: "", | |
| 96 | baseBranch: "main", | |
| 97 | headBranch: "bot/1", | |
| 98 | }); | |
| 99 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 100 | const run = createRun(app.db, { | |
| 101 | repoId: repo.id, | |
| 102 | kind: "check", | |
| 103 | issueId: opened.value.id, | |
| 104 | env: `pull-${opened.value.number}`, | |
| 105 | commitOid: opened.value.headOid, | |
| 106 | reason: "pull", | |
| 107 | }); | |
| 108 | claimRun(app.db, run.id); | |
| 109 | finishRun(app.db, run.id, { state: "ok", summary: "ok" }); | |
| 110 | expect(assignIssue(app, alice, repo, getIssueById(app.db, opened.value.id)!, alice).ok).toBe(true); | |
| 111 | // A release: the app is serving a new version, by the owner. | |
| 112 | createAppRecord(app.db, { repoId: repo.id, slug: "chipped", access: "public" }); | |
| 113 | app.bus.append({ | |
| 114 | kind: "app.serving", | |
| 115 | repoId: repo.id, | |
| 116 | actorId: alice.id, | |
| 117 | payload: { env: "production" }, | |
| 118 | }); | |
| 119 | }, 30_000); | |
| 120 | ||
| 121 | afterAll(async () => { | |
| 122 | await servers.stop(); | |
| 123 | await rm(dataDir, { recursive: true, force: true }); | |
| 124 | }); | |
| 125 | ||
| 126 | test("with no chip the feed holds a change and a release", () => { | |
| 127 | expect(kinds()).toContain("pull"); | |
| 128 | expect(kinds()).toContain("release"); | |
| 129 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 130 | }); | |
| 131 | ||
| 132 | test("a kind chip removes that kind and nothing else, on the feed and the API", async () => { | |
| 133 | const before = kinds(); | |
| 134 | setKindChip(app.db, alice.id, "pulls", true); | |
| 135 | // A change that waits on alice is also an ask about a pull: both go. | |
| 136 | expect(kinds()).toEqual(before.filter((k) => k !== "pull" && k !== "ask")); | |
| 137 | expect((await apiFeed()).map((i) => i.kind)).not.toContain("pull"); | |
| 138 | expect((await apiFeed()).map((i) => i.kind)).toContain("release"); | |
| 139 | setKindChip(app.db, alice.id, "pulls", false); | |
| 140 | expect(kinds()).toEqual(before); | |
| 141 | setKindChip(app.db, alice.id, "releases", true); | |
| 142 | expect(kinds()).toEqual(before.filter((k) => k !== "release")); | |
| 143 | setKindChip(app.db, alice.id, "releases", false); | |
| 144 | // A kind nothing here is of removes nothing. | |
| 145 | setKindChip(app.db, alice.id, "words", true); | |
| 146 | setKindChip(app.db, alice.id, "decisions", true); | |
| 147 | expect(kinds()).toEqual(before); | |
| 148 | setKindChip(app.db, alice.id, "words", false); | |
| 149 | setKindChip(app.db, alice.id, "decisions", false); | |
| 150 | }); | |
| 151 | ||
| 152 | test("a people chip removes that person's posts and nothing else; yourself and strangers are refused", async () => { | |
| 153 | const before = readFeed(app.db, alice).items; | |
| 154 | expect(before.some((i) => i.actor === "bot")).toBe(true); | |
| 155 | expect(setPersonChip(app.db, alice.id, "bob", true)).toBe("ok"); | |
| 156 | expect(readFeed(app.db, alice).items.map((i) => i.id)).toEqual(before.map((i) => i.id)); | |
| 157 | expect(setPersonChip(app.db, alice.id, "bob", false)).toBe("ok"); | |
| 158 | expect(setPersonChip(app.db, alice.id, "bot", true)).toBe("ok"); | |
| 159 | expect(readFeed(app.db, alice).items.map((i) => i.id)).toEqual( | |
| 160 | // The change's own post and the ask about it, both the bot's. | |
| 161 | before.filter((i) => i.actor !== "bot" && i.kind !== "ask").map((i) => i.id), | |
| 162 | ); | |
| 163 | expect(askLinesFor(app.db, alice)).toHaveLength(0); | |
| 164 | expect((await apiFeed()).some((i) => i.actor === "bot")).toBe(false); | |
| 165 | expect(setPersonChip(app.db, alice.id, "alice", true)).toBe("self"); | |
| 166 | expect(setPersonChip(app.db, alice.id, "nobody", true)).toBe("unknown"); | |
| 167 | setPersonChip(app.db, alice.id, "bot", false); | |
| 168 | }); | |
| 169 | ||
| 170 | test("the inbox: a hidden person's or kind's asks leave it, others stay", async () => { | |
| 171 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 172 | setKindChip(app.db, alice.id, "decisions", true); | |
| 173 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 174 | setKindChip(app.db, alice.id, "decisions", false); | |
| 175 | setPersonChip(app.db, alice.id, "bob", true); | |
| 176 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 177 | setPersonChip(app.db, alice.id, "bob", false); | |
| 178 | setPersonChip(app.db, alice.id, "bot", true); | |
| 179 | expect(askLinesFor(app.db, alice)).toHaveLength(0); | |
| 180 | setPersonChip(app.db, alice.id, "bot", false); | |
| 181 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 182 | setKindChip(app.db, alice.id, "pulls", true); | |
| 183 | expect(askLinesFor(app.db, alice)).toHaveLength(0); | |
| 184 | const api = (await fetch(`${servers.url}/api/v1/asks`, { headers: headers(cookie) }).then((r) => | |
| 185 | r.json(), | |
| 186 | )) as { rows: unknown[] }; | |
| 187 | expect(api.rows).toHaveLength(0); | |
| 188 | setKindChip(app.db, alice.id, "pulls", false); | |
| 189 | expect(askLinesFor(app.db, alice)).toHaveLength(1); | |
| 190 | }); | |
| 191 | ||
| 192 | test("the apps rail drops a hidden person's apps", async () => { | |
| 193 | const live = () => | |
| 194 | fetch(`${servers.url}/live/apps`, { headers: headers(cookie) }).then((r) => r.text()); | |
| 195 | expect(await live()).toContain("alice/chipped"); | |
| 196 | // Alice is the forge's admin, so bob's app is on her rail too. | |
| 197 | const bobRepo = await createRepo(app, bob, { name: "theirs", visibility: "public" }); | |
| 198 | if (!bobRepo.ok) throw new Error(String(bobRepo.error)); | |
| 199 | expect(createAppRecord(app.db, { repoId: bobRepo.value.id, slug: "theirs", access: "public" }).ok).toBe(true); | |
| 200 | expect(await live()).toContain("bob/theirs"); | |
| 201 | setPersonChip(app.db, alice.id, "bob", true); | |
| 202 | const hidden = await live(); | |
| 203 | expect(hidden).not.toContain("bob/theirs"); | |
| 204 | expect(hidden).toContain("alice/chipped"); | |
| 205 | setPersonChip(app.db, alice.id, "bob", false); | |
| 206 | }); | |
| 207 | ||
| 208 | test("the home form and the API save per viewer; foreign origins and bad input are refused", async () => { | |
| 209 | const page = await fetch(`${servers.url}/`, { headers: headers(cookie) }).then((r) => r.text()); | |
| 210 | expect(page).toContain('action="/feed/chips"'); | |
| 211 | expect((await post("/feed/chips", [["kind", "pulls"], ["hide", "1"]])).status).toBe(303); | |
| 212 | expect(chipsFor(app.db, alice.id).kinds.has("pulls")).toBe(true); | |
| 213 | expect(chipsFor(app.db, bob.id).kinds.size).toBe(0); | |
| 214 | const saved = (await fetch(`${servers.url}/api/v1/feed/chips`, { headers: headers(cookie) }).then((r) => | |
| 215 | r.json(), | |
| 216 | )) as { kinds: string[]; people: string[] }; | |
| 217 | expect(saved.kinds).toEqual(["pulls"]); | |
| 218 | expect((await post("/feed/chips", [["kind", "pulls"], ["hide", "0"]])).status).toBe(303); | |
| 219 | expect(chipsFor(app.db, alice.id).kinds.size).toBe(0); | |
| 220 | expect((await post("/feed/chips", [["person", "bob"]])).status).toBe(303); | |
| 221 | expect([...chipsFor(app.db, alice.id).people]).toEqual([bob.id]); | |
| 222 | expect((await post("/feed/chips", [["kind", "evil"]])).status).toBe(400); | |
| 223 | expect((await post("/feed/chips", [["person", "../x"]])).status).toBe(400); | |
| 224 | expect((await post("/feed/chips", [])).status).toBe(400); | |
| 225 | const cross = await fetch(`${servers.url}/feed/chips`, { | |
| 226 | method: "POST", | |
| 227 | headers: { cookie, origin: "https://evil.example" }, | |
| 228 | body: new URLSearchParams({ kind: "words" }), | |
| 229 | redirect: "manual", | |
| 230 | }); | |
| 231 | expect(cross.status).toBe(403); | |
| 232 | expect(chipsFor(app.db, alice.id).kinds.size).toBe(0); | |
| 233 | const api = await fetch(`${servers.url}/api/v1/feed/chips`, { | |
| 234 | method: "POST", | |
| 235 | headers: { ...headers(cookie), "content-type": "application/json" }, | |
| 236 | body: JSON.stringify({ kind: "words" }), | |
| 237 | }); | |
| 238 | expect(api.status).toBe(201); | |
| 239 | expect(chipsFor(app.db, alice.id).kinds.has("words")).toBe(true); | |
| 240 | const bad = await fetch(`${servers.url}/api/v1/feed/chips`, { | |
| 241 | method: "POST", | |
| 242 | headers: { ...headers(cookie), "content-type": "application/json" }, | |
| 243 | body: JSON.stringify({ kind: "nope" }), | |
| 244 | }); | |
| 245 | expect(bad.status).toBe(422); | |
| 246 | }); | |
| 247 | }); |
new above
offline