Public: anyone can read this; the crew can change it.
A reply under a feed post is written as a comment on its issue and shows under the post without a reload #13692
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13679-a-reply-under-a-feed-post-is-written-as → main 7 commits fork 2 threads · 2 unresolved
5 files
changed +160
−4
docs/features/README.md +1
| @@ -33,6 +33,7 @@ Read with the guide page [What a forge is](../guide-what-a-forge-is.md). | ||
| 33 | 33 | - [](feed-permalink.md) |
| 34 | 34 | - [](feed-run-log.md) |
| 35 | 35 | - [](feed-like-ledger.md) |
| 36 | - [](feed-reply.md) | |
| 36 | 37 | - [](packs.md) |
| 37 | 38 | - [](home-frame.md) |
| 38 | 39 | - [](v1-proven.md) |
docs/features/feed-reply.md added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | # Reply under a feed post | |
| 2 | ||
| 3 | On `/feed`, a reply under an issue post is written as a comment on that issue. The form posts to | |
| 4 | `/<owner>/<repo>/issues/<n>/comments` with `back=feed`, so it works with JavaScript off: the page | |
| 5 | returns to `/feed` saying "Reply posted." (one reload; live insertion without a reload is not done). | |
| 6 | ||
| 7 | A refused reply keeps its words (cut to what the flash cookie can carry) in the flash, to copy back. | |
| 8 | ||
| 9 | From the terminal: the API `POST /api/v1/repos/<owner>/<repo>/issues/<n>/comments` writes the same | |
| 10 | comment; there is no terminal screen for it yet. |
src/web/flash.ts +12
| @@ -34,6 +34,18 @@ export interface Flash { | ||
| 34 | 34 | // Cookies cap out around 4KB; a flash is a sentence, not a payload. |
| 35 | 35 | const MAX_FLASH = 2000; |
| 36 | 36 | |
| 37 | /** | |
| 38 | * A flash with `words` kept as its `code`, cut so the whole flash still fits the cookie once | |
| 39 | * encoded (newlines, quotes and non-ASCII cost 3 to 9 characters each): the message is never lost | |
| 40 | * to a cut cookie. Never more than the 512 characters a reader keeps. | |
| 41 | */ | |
| 42 | export function flashKeeping(flash: Flash, words: string): Flash { | |
| 43 | let kept = words.slice(0, 512); | |
| 44 | const size = () => encodeURIComponent(JSON.stringify({ ...flash, code: kept })).length; | |
| 45 | while (kept !== "" && size() > MAX_FLASH) kept = kept.slice(0, Math.floor(kept.length * 0.8)); | |
| 46 | return kept === "" ? flash : { ...flash, code: kept }; | |
| 47 | } | |
| 48 | ||
| 37 | 49 | /** The `Set-Cookie` value that carries a flash to the next request. */ |
| 38 | 50 | export function flashCookie(flash: Flash, url?: URL, forgeUrl?: string): string { |
| 39 | 51 | const encoded = encodeURIComponent(JSON.stringify(flash)).slice(0, MAX_FLASH); |
src/web/pages/issues.ts +10 −4
| @@ -34,6 +34,7 @@ import { | ||
| 34 | 34 | import { field, readForm } from "../form.ts"; |
| 35 | 35 | import { html, raw, type Html } from "../html.ts"; |
| 36 | 36 | import { flashTo, page, redirect } from "../layout.ts"; |
| 37 | import { flashKeeping } from "../flash.ts"; | |
| 37 | 38 | import { cleanFrom, repoOfPath, withFromLine } from "../issue_from.ts"; |
| 38 | 39 | import { btn, empty, field as formField, listCard, row } from "../ui.ts"; |
| 39 | 40 | import { |
| @@ -376,10 +377,15 @@ export async function issueCommentSubmit(ctx: Ctx): Promise<Response> { | ||
| 376 | 377 | body, |
| 377 | 378 | ); |
| 378 | 379 | if (!added.ok) |
| 379 | return flashTo(ctx, refusedTo, { | |
| 380 | tone: "danger", | |
| 381 | message: originError(added.error) ?? COMMENT_NOT_POSTED, | |
| 382 | }); | |
| 380 | return flashTo( | |
| 381 | ctx, | |
| 382 | refusedTo, | |
| 383 | // The words are kept, cut to what the cookie can carry, to copy back. | |
| 384 | flashKeeping( | |
| 385 | { tone: "danger", message: originError(added.error) ?? COMMENT_NOT_POSTED }, | |
| 386 | body, | |
| 387 | ), | |
| 388 | ); | |
| 383 | 389 | // "Ask as a question": the comment is posted either way; a refusal to mark it is said. |
| 384 | 390 | if (field(form, "question") === "open") { |
| 385 | 391 | const marked = markQuestion(ctx.app, ctx.auth.user, found.repo, found.issue, added.value.id, "open"); |
test/feed-reply-comment.test.ts added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | /** | |
| 2 | * The reply box on a feed post is a comment on its issue: same-origin checked, | |
| 3 | * the words kept when it is refused, and the issue page lists it after the | |
| 4 | * redirect of a plain form post. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { openIssue } from "../src/actions/issues.ts"; | |
| 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { listComments } from "../src/core/issues.ts"; | |
| 14 | import { createSession } from "../src/core/sessions.ts"; | |
| 15 | import { createUser } from "../src/core/users.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | let root = ""; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let cookie = ""; | |
| 25 | let number = 0; | |
| 26 | let issueId = ""; | |
| 27 | ||
| 28 | const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { | |
| 29 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 30 | return r.value; | |
| 31 | }; | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | root = mkdtempSync(join(tmpdir(), "forge-feed-reply-comment-")); | |
| 35 | app = createApp({ dataDir: root, port: 0, disk: () => ({ freeBytes: 95, totalBytes: 100 }) }); | |
| 36 | servers = await serve(app); | |
| 37 | const alice = must( | |
| 38 | await createUser(app.db, { | |
| 39 | origin: "seed", | |
| 40 | trusted: true, | |
| 41 | allowReserved: true, | |
| 42 | username: "alice", | |
| 43 | email: "[email protected]", | |
| 44 | password: "a-good-passphrase", | |
| 45 | adminIfFirst: true, | |
| 46 | }), | |
| 47 | ); | |
| 48 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 49 | const repo = must(await createRepo(app, alice, { name: "open", visibility: "public" })); | |
| 50 | must( | |
| 51 | await commitToBranch(app, alice, repo, { | |
| 52 | message: "seed", | |
| 53 | files: [{ path: "README.md", content: "# open\n" }], | |
| 54 | }), | |
| 55 | ); | |
| 56 | const issue = must(openIssue(app, alice, repo, { title: "A question", body: "why?" })); | |
| 57 | number = issue.number; | |
| 58 | issueId = issue.id; | |
| 59 | }, 70_000); | |
| 60 | ||
| 61 | afterAll(async () => { | |
| 62 | await servers.stop(); | |
| 63 | app.close(); | |
| 64 | rmSync(root, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | const path = () => `/alice/open/issues/${number}/comments`; | |
| 68 | const send = (fields: Record<string, string>, origin = servers.url) => | |
| 69 | fetch(`${servers.url}${path()}`, { | |
| 70 | method: "POST", | |
| 71 | redirect: "manual", | |
| 72 | headers: { cookie, origin, "content-type": "application/x-www-form-urlencoded" }, | |
| 73 | body: new URLSearchParams(fields), | |
| 74 | }); | |
| 75 | ||
| 76 | test("a plain form reply is a comment on the issue, listed on its page after the redirect", async () => { | |
| 77 | const res = await send({ body: "Because reasons." }); | |
| 78 | expect(res.status).toBe(303); | |
| 79 | const where = new URL(res.headers.get("location")!, servers.url); | |
| 80 | expect(where.pathname).toBe(`/alice/open/issues/${number}`); | |
| 81 | const page = await (await fetch(`${servers.url}${where.pathname}`, { headers: { cookie } })).text(); | |
| 82 | expect(page).toContain("Because reasons."); | |
| 83 | const api = await fetch(`${servers.url}/api/v1/repos/alice/open/issues/${number}/comments`, { headers: { cookie } }); | |
| 84 | expect(JSON.stringify(await api.json())).toContain("Because reasons."); | |
| 85 | }); | |
| 86 | ||
| 87 | test("a reply sent from another origin is refused and writes nothing", async () => { | |
| 88 | const before = listComments(app.db, issueId).length; | |
| 89 | const res = await send({ body: "Forged." }, "https://evil.example"); | |
| 90 | expect(res.status).toBe(403); | |
| 91 | expect(listComments(app.db, issueId)).toHaveLength(before); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a refused reply returns to the feed with its words kept to copy", async () => { | |
| 95 | const long = "x".repeat(70_000); | |
| 96 | const res = await send({ body: long, back: "feed" }); | |
| 97 | expect(res.status).toBe(303); | |
Only plain x is tested. Add a case with newlines/quotes/non-ASCII that fails without the encoded-size cap. |
||
| 98 | expect(new URL(res.headers.get("location")!, servers.url).pathname).toBe("/feed"); | |
| 99 | const set = res.headers.getSetCookie().find((c) => c.startsWith("forge_flash=")) ?? ""; | |
| 100 | const flash = JSON.parse(decodeURIComponent(set.slice("forge_flash=".length).split(";")[0]!)); | |
| 101 | expect(flash.tone).toBe("danger"); | |
| 102 | expect(flash.code).toBe("x".repeat(512)); | |
| 103 | }); | |
| 104 | ||
| 105 | test("kept words with newlines, quotes and non-ASCII still leave a readable danger flash", async () => { | |
| 106 | const long = '"é\n日本'.repeat(14_000); | |
| 107 | const res = await send({ body: long, back: "feed" }); | |
| 108 | expect(res.status).toBe(303); | |
| 109 | const set = res.headers.getSetCookie().find((c) => c.startsWith("forge_flash=")) ?? ""; | |
| 110 | const raw = set.slice("forge_flash=".length).split(";")[0]!; | |
| 111 | expect(raw.length).toBeLessThanOrEqual(2000); | |
| 112 | const flash = JSON.parse(decodeURIComponent(raw)); | |
| 113 | expect(flash.tone).toBe("danger"); | |
| 114 | expect(flash.message).not.toBe(""); | |
| 115 | expect(long.startsWith(flash.code)).toBe(true); | |
| 116 | const page = await fetch(`${servers.url}/feed`, { headers: { cookie: `${cookie}; forge_flash=${raw}` } }); | |
| 117 | expect(await page.text()).toContain(flash.message.slice(0, 20)); | |
| 118 | }); | |
| 119 | ||
| 120 | test("a good reply from the feed returns to /feed and says Reply posted", async () => { | |
| 121 | const res = await send({ body: "From the feed.", back: "feed" }); | |
| 122 | expect(res.status).toBe(303); | |
| 123 | expect(new URL(res.headers.get("location")!, servers.url).pathname).toBe("/feed"); | |
| 124 | const set = res.headers.getSetCookie().find((c) => c.startsWith("forge_flash=")) ?? ""; | |
| 125 | expect(decodeURIComponent(set)).toContain("Reply posted."); | |
| 126 | expect(listComments(app.db, issueId).some((c) => c.body === "From the feed.")).toBe(true); | |
| 127 | }); |
new above
offline
Cap by encoded size, not 512 chars: the flash cookie cuts the encoded JSON at 2000, so multi-byte or newline-heavy text corrupts the flash and loses the message too. Principle 8 (plain words): the person must always see why it was refused.