Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Bad words never show: a word filter and a report button, both editable by the admin #26
smith/arm:smith/14-bad-words-never-show-a-word-filter-and-a → main 10 commits fork 9 threads · 9 unresolved
11 files
changed +657
−18
Software/README.md +19
| @@ -473,6 +473,25 @@ the gripper first and then walks down to id 1, one servo on the bus at a time. | ||
| 473 | 473 | | 5 | wrist_roll | 1/147 | |
| 474 | 474 | | 6 | gripper | 1/147 | |
| 475 | 475 | |
| 476 | ## Moderating the hosted room | |
| 477 | ||
| 478 | The owner (the forge's admin role; on a local run, everyone) opens `/admin` from the viewer's Admin panel. It is a | |
| 479 | server-rendered page of plain forms, no script; the signed-out get a sign-in link (401) and everyone else 403. | |
| 480 | Every action below is a form post to `/admin` (same-origin checked, under the page's CSP) and is written to the | |
| 481 | admin log shown at the bottom of the page. | |
| 482 | ||
| 483 | - **Reports.** Signed-in people can press "report" on another person's chat line. The line is kept as it was, so | |
| 484 | it shows on `/admin` even if it is deleted. Remove deletes it, Mute silences its author, Dismiss drops the | |
| 485 | report; any of them settles every report on that line. A person may have at most 10 open reports and gets a | |
| 486 | plain message past that (or "already reported" for a repeat); at most 200 are kept. A muted person cannot report. | |
| 487 | - **Word filter.** Every chat line is checked before it is stored or broadcast: first muted ("you are muted"), | |
| 488 | then the filter, then the chat, then the crew moderator. A refused line is explained to its sender only. The | |
| 489 | page edits the settings, live: blocked words (matched through case, accents, look-alike digits, invisible | |
| 490 | characters and stretched letters), allow links and allowed hosts, longest run of one character (0 = off) and | |
| 491 | most capitals (100 = off). | |
| 492 | - **State.** `chat.json`, `filter.json`, `reports.json` and `admin.json` in the app's data directory (`FORGE_APP_DATA`). | |
| 493 | - **Terminal.** This app has no terminal (`forge.json` declares no `tty`), so these actions are on the web page only. | |
| 494 | ||
| 476 | 495 | ## References |
| 477 | 496 | |
| 478 | 497 | The methods behind the vision kit, so you can read the originals: |
Software/so101/bench/fake-serve.ts +3
| @@ -51,9 +51,12 @@ const lobby = new Lobby({ | ||
| 51 | 51 | local, |
| 52 | 52 | chatFile: join(dir, "chat.json"), |
| 53 | 53 | adminFile: join(dir, "admin.json"), |
| 54 | filterFile: join(dir, "filter.json"), | |
| 55 | reportsFile: join(dir, "reports.json"), | |
| 54 | 56 | accounts: new Accounts({ file: join(dir, "accounts.sqlite") }), |
| 55 | 57 | moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }), |
| 56 | 58 | }); |
| 59 | ||
| 57 | 60 | const cameras = new CameraHub({ file: join(dir, "cameras.json"), seed: remote ? [{ name: "Camera 1", source: remote.cameras[0]! }] : [] }); |
| 58 | 61 | |
| 59 | 62 | const leaders = new LeaderCalibrations({ file: join(dir, "leader-calibrations.json") }); |
Software/so101/src/admin-page.ts +39 −1
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { createHash } from "node:crypto"; |
| 8 | 8 | import { ANNOUNCEMENT_CHARS, type AdminSettings, type LogEntry } from "./admin.ts"; |
| 9 | import type { FilterSettings } from "./filter.ts"; | |
| 10 | import type { Report } from "./reports.ts"; | |
| 9 | 11 | import type { Who } from "./identity.ts"; |
| 10 | 12 | import type { Snapshot } from "./turns.ts"; |
| 11 | 13 | |
| @@ -45,6 +47,10 @@ textarea { min-height: 5.5rem; resize: vertical; } | ||
| 45 | 47 | ol { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; font-size: .95rem; } |
| 46 | 48 | ol li { display: grid; grid-template-columns: 7.5rem 1fr; gap: 1rem; padding-bottom: .5rem; border-bottom: 1px solid var(--line); } |
| 47 | 49 | time { color: var(--dim); font-variant-numeric: tabular-nums; } |
| 50 | .card { border: 1px solid var(--line); border-radius: 8px; padding: .75rem 1rem; display: grid; gap: .5rem; overflow-wrap: anywhere; } | |
| 51 | .cards { display: grid; gap: .75rem; } | |
| 52 | .card p { margin: 0; } .card .actions form { display: inline; } | |
| 53 | .check { display: flex; align-items: center; gap: .6rem; } .check input { width: auto; min-width: 1.25rem; min-height: 1.25rem; } | |
| 48 | 54 | .empty { color: var(--dim); } |
| 49 | 55 | @media (prefers-reduced-motion: reduce) { * { transition: none !important; } } |
| 50 | 56 | `; |
| @@ -52,7 +58,7 @@ time { color: var(--dim); font-variant-numeric: tabular-nums; } | ||
| 52 | 58 | export const ADMIN_CSP = `default-src 'none'; style-src 'sha256-${createHash("sha256").update(STYLE).digest("base64")}'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`; |
| 53 | 59 | |
| 54 | 60 | /** The fixed lines a redirect back may ask for; nothing from the request is echoed. */ |
| 55 | export const DONE = { saved: "Saved.", paused: "Paused: nobody new gets a turn.", resumed: "Open again: the next in line has control.", ended: "Turn ended.", taken: "You have control." } as const; | |
| 61 | export const DONE = { saved: "Saved.", paused: "Paused: nobody new gets a turn.", resumed: "Open again: the next in line has control.", ended: "Turn ended.", taken: "You have control.", removed: "Line removed.", muted: "Muted.", unmuted: "Unmuted.", dismissed: "Report dismissed.", filtered: "Word filter saved. Live now." } as const; | |
| 56 | 62 | export type Done = keyof typeof DONE; |
| 57 | 63 | |
| 58 | 64 | export interface AdminPage { |
| @@ -60,6 +66,9 @@ export interface AdminPage { | ||
| 60 | 66 | settings: AdminSettings; |
| 61 | 67 | snapshot: Snapshot; |
| 62 | 68 | log: readonly LogEntry[]; |
| 69 | reports: readonly Report[]; | |
| 70 | muted: ReadonlyArray<{ id: string; name: string }>; | |
| 71 | filter: FilterSettings; | |
| 63 | 72 | done?: Done | null; |
| 64 | 73 | error?: string | null; |
| 65 | 74 | } |
| @@ -97,6 +106,32 @@ const time = (at: number) => { | ||
| 97 | 106 | const field = (name: keyof AdminSettings, label: string, hint: string, value: number, min: number, max: number) => |
| 98 | 107 | `<label>${label}<small>${hint}</small><input name="${name}" type="number" inputmode="numeric" required min="${min}" max="${max}" step="1" value="${value}"></label>`; |
| 99 | 108 | |
| 109 | const hidden = (name: string, value: string | number) => `<input type="hidden" name="${name}" value="${esc(String(value))}">`; | |
| 110 | const post = (fields: Record<string, string | number>, text: string, cls = "") => | |
| 111 | `<form method="post" action="/admin">${Object.entries(fields).map(([k, v]) => hidden(k, v)).join("")}<button class="${cls}">${text}</button></form>`; | |
| 112 | ||
| 113 | const reportsHtml = (reports: readonly Report[]) => | |
| 114 | reports.length | |
| 115 | ? `<div class="cards">${[...reports].reverse().map((r) => `<div class="card"><p><b>${esc(r.message.from.name)}</b>: ${esc(r.message.text)}</p><p class="empty">reported by ${esc(r.by.name)} · ${time(r.at)}</p><div class="actions">${post({ action: "remove", report: r.id }, "Remove the line", "danger")}${post({ action: "mutefrom", report: r.id }, `Mute ${esc(r.message.from.name)}`)}${post({ action: "dismissreport", report: r.id }, "Dismiss")}</div></div>`).join("")}</div>` | |
| 116 | : `<p class="empty">No reports.</p>`; | |
| 117 | ||
| 118 | const mutedHtml = (muted: ReadonlyArray<{ id: string; name: string }>) => | |
| 119 | muted.length | |
| 120 | ? `<div class="cards">${muted.map((m) => `<div class="card"><p><b>${esc(m.name)}</b></p><div class="actions">${post({ action: "unmute", user: m.id }, "Unmute")}</div></div>`).join("")}</div>` | |
| 121 | : `<p class="empty">Nobody.</p>`; | |
| 122 | ||
| 123 | const filterHtml = (f: FilterSettings) => | |
| 124 | `<form method="post" action="/admin">${hidden("action", "filter")} | |
| 125 | <div class="grid"> | |
| 126 | <label class="wide">Blocked words<small>one per line; matched through case, accents and look-alike characters</small><textarea name="words" rows="8">${esc(f.words.join("\n"))}</textarea></label> | |
| 127 | <label class="check wide"><input type="checkbox" name="allowLinks"${f.allowLinks ? " checked" : ""}> Allow links</label> | |
| 128 | <label class="wide">Links to these hosts are always allowed<small>one per line</small><textarea name="allowedHosts" rows="3">${esc(f.allowedHosts.join("\n"))}</textarea></label> | |
| 129 | <label>Longest run of one character<small>0 turns it off</small><input name="maxRepeat" type="number" inputmode="numeric" required min="0" max="500" step="1" value="${f.maxRepeat}"></label> | |
| 130 | <label>Most capitals<small>percent of the letters; 100 turns it off</small><input name="maxCapsPercent" type="number" inputmode="numeric" required min="0" max="100" step="1" value="${f.maxCapsPercent}"></label> | |
| 131 | </div> | |
| 132 | <div class="save"><button class="go">Save word filter</button></div> | |
| 133 | </form>`; | |
| 134 | ||
| 100 | 135 | const action = (name: string, text: string, cls = "", disabled = false) => |
| 101 | 136 | `<form method="post" action="/admin"><input type="hidden" name="action" value="${name}"><button class="${cls}"${disabled ? " disabled" : ""}>${text}</button></form>`; |
| 102 | 137 | |
| @@ -134,6 +169,9 @@ ${field("maxQueue", "Max queue length", "people waiting, 0–100", s.maxQueue, 0 | ||
| 134 | 169 | <div class="save"><button class="go">Save settings</button></div> |
| 135 | 170 | </form> |
| 136 | 171 | </section> |
| 172 | <section aria-labelledby="rep"><h2 id="rep">Reports</h2>${reportsHtml(p.reports)}</section> | |
| 173 | <section aria-labelledby="mut"><h2 id="mut">Muted</h2>${mutedHtml(p.muted)}</section> | |
| 174 | <section aria-labelledby="flt"><h2 id="flt">Word filter</h2>${filterHtml(p.filter)}</section> | |
| 137 | 175 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 138 | 176 | ); |
| 139 | 177 | } |
Software/so101/src/admin.test.ts +188 −1
| @@ -1,7 +1,14 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 1 | import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | 2 | import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | import WebSocket from "ws"; | |
| 6 | import { FeetechBus } from "./bus.ts"; | |
| 7 | import { FakeBus } from "./fake-bus.ts"; | |
| 8 | import { sign } from "./identity.ts"; | |
| 9 | import { Lobby } from "./lobby.ts"; | |
| 10 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 11 | import { startServer, type RunningServer } from "./server.ts"; | |
| 5 | 12 | import { Admin, AdminError, changeFromForm, DEFAULT_ADMIN, validateChange } from "./admin.ts"; |
| 6 | 13 | |
| 7 | 14 | const owner = { id: "o", name: "trevato", owner: true }; |
| @@ -49,3 +56,183 @@ describe("admin settings", () => { | ||
| 49 | 56 | expect(readFileSync(file, "utf8")).toBe("{not json"); // left for the owner to look at |
| 50 | 57 | }); |
| 51 | 58 | }); |
| 59 | ||
| 60 | // ---------------------------------------------------------------------------- reports, mutes and the word filter, over /admin | |
| 61 | const KEY = "test-identity-key"; | |
| 62 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 63 | const shut = (x: { close(): Promise<void> } | undefined) => (x ? Promise.race([x.close(), sleep(500)]) : undefined); | |
| 64 | const as = (id: string, name: string, role = "user") => { | |
| 65 | const identity = `${id}|${name}|${role}|${Date.now() + 3_600_000}`; | |
| 66 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 67 | }; | |
| 68 | const ALICE = as("u-alice", "alice"); | |
| 69 | const BOB = as("u-bob", "bob"); | |
| 70 | const OWNER = as("u-owner", "trevato", "admin"); | |
| 71 | ||
| 72 | type Msg = Record<string, any>; | |
| 73 | async function client(port: number, headers: Record<string, string>) { | |
| 74 | const ws = new WebSocket(`ws://127.0.0.1:${port}/live`, { headers }); | |
| 75 | const heard: Msg[] = []; | |
| 76 | ws.on("message", (data) => heard.push(JSON.parse(String(data)))); | |
| 77 | await new Promise((r, j) => (ws.once("open", r), ws.once("error", j))); | |
| 78 | const until = async (match: (m: Msg) => boolean, ms = 2000) => { | |
| 79 | for (const end = Date.now() + ms; Date.now() < end; await sleep(10)) { | |
| 80 | const found = heard.find(match); | |
| 81 | if (found) return found; | |
| 82 | } | |
| 83 | throw new Error(`nothing matched; heard ${JSON.stringify(heard.slice(-5))}`); | |
| 84 | }; | |
| 85 | return { ws, heard, until, send: (m: Msg) => ws.send(JSON.stringify(m)) }; | |
| 86 | } | |
| 87 | ||
| 88 | let simulation: RunningServer; | |
| 89 | beforeAll(async () => { | |
| 90 | simulation = await startServer(new FeetechBus(new FakeBus([1, 2, 6])), { host: "127.0.0.1", port: 0, ids: [1, 2, 6], posesFile: join(dir(), "poses.json") }); | |
| 91 | }); | |
| 92 | afterAll(() => shut(simulation)); | |
| 93 | ||
| 94 | describe("moderation on /admin", () => { | |
| 95 | let relay: RunningRelay | undefined; | |
| 96 | let lobby: Lobby | undefined; | |
| 97 | const sockets: WebSocket[] = []; | |
| 98 | afterEach(async () => { | |
| 99 | for (const x of sockets.splice(0)) x.terminate(); | |
| 100 | await shut(relay); | |
| 101 | lobby?.close(); | |
| 102 | }); | |
| 103 | const start = async () => { | |
| 104 | lobby = new Lobby({ identityKey: KEY }); | |
| 105 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby }); | |
| 106 | return relay.port; | |
| 107 | }; | |
| 108 | const enter = async (port: number, headers: Record<string, string>) => { | |
| 109 | const c = await client(port, headers); | |
| 110 | sockets.push(c.ws); | |
| 111 | return c; | |
| 112 | }; | |
| 113 | const page = (port: number, headers: Record<string, string>) => fetch(`http://127.0.0.1:${port}/admin`, { headers }); | |
| 114 | const form = (port: number, headers: Record<string, string>, fields: Record<string, string>) => | |
| 115 | fetch(`http://127.0.0.1:${port}/admin`, { method: "POST", redirect: "manual", headers: { "content-type": "application/x-www-form-urlencoded", ...headers }, body: new URLSearchParams(fields).toString() }); | |
| 116 | ||
| 117 | test("a blocked word is refused to its sender only and never broadcast", async () => { | |
| 118 | const port = await start(); | |
| 119 | const alice = await enter(port, ALICE); | |
| 120 | const bob = await enter(port, BOB); | |
| 121 | alice.send({ type: "post", text: "you fucking idiot" }); | |
| 122 | expect((await alice.until((m) => m.type === "error")).error).toMatch(/not allowed/); | |
| 123 | alice.send({ type: "post", text: "hello" }); // a refused line does not use up the rate limit | |
| 124 | await bob.until((m) => m.type === "said" && m.message.text === "hello"); | |
| 125 | expect(bob.heard.some((m) => JSON.stringify(m).includes("fucking"))).toBe(false); | |
| 126 | expect(lobby!.chat.messages.some((m) => m.text.includes("fucking"))).toBe(false); | |
| 127 | }); | |
| 128 | ||
| 129 | test("a muted person is told they are muted, not that their language is not allowed, and cannot report", async () => { | |
| 130 | const port = await start(); | |
| 131 | const alice = await enter(port, ALICE); | |
| 132 | const bob = await enter(port, BOB); | |
| 133 | bob.send({ type: "post", text: "hello there" }); | |
| 134 | const said = await alice.until((m) => m.type === "said"); | |
| 135 | lobby!.mute("u-alice", true); | |
| 136 | alice.send({ type: "post", text: "you fucking idiot" }); | |
| 137 | expect((await alice.until((m) => m.type === "error")).error).toBe("you are muted"); | |
| 138 | alice.send({ type: "report", id: said.message.id }); | |
| 139 | await alice.until((m) => m.type === "error" && m.error === "you are muted" && alice.heard.filter((x) => x.type === "error").length === 2); | |
| 140 | expect(lobby!.reports.list).toEqual([]); | |
| 141 | }); | |
| 142 | ||
| 143 | test("the 11th open report from one person is refused with the truth, and not acknowledged", async () => { | |
| 144 | const port = await start(); | |
| 145 | const bob = await enter(port, BOB); | |
| 146 | const lines = Array.from({ length: 11 }, (_, i) => lobby!.chat.post({ id: `u-x${i}`, name: `x${i}`, owner: false }, `line ${i}`)); | |
| 147 | for (const line of lines.slice(0, 10)) bob.send({ type: "report", id: line.id }); | |
| 148 | for (const line of lines.slice(0, 10)) await bob.until((m) => m.type === "reported" && m.id === line.id); | |
| 149 | const mark = bob.heard.length; | |
| 150 | bob.send({ type: "report", id: lines[10]!.id }); | |
| 151 | expect((await bob.until((m) => m.type === "error" && m.error.startsWith("too many open reports"))).error).toBe("too many open reports; wait for the owner to look at them"); | |
| 152 | expect(bob.heard.slice(mark).some((m) => m.type === "reported")).toBe(false); | |
| 153 | expect(lobby!.reports.list).toHaveLength(10); | |
| 154 | }); | |
| 155 | ||
| 156 | test("the page is for the owner: a readable sign-in for the signed-out, 403 for others, and no script", async () => { | |
| 157 | const port = await start(); | |
| 158 | const out = await page(port, {}); | |
| 159 | expect(out.status).toBe(401); | |
| 160 | expect(await out.text()).toContain("Sign in"); | |
| 161 | expect((await page(port, ALICE)).status).toBe(403); | |
| 162 | expect((await form(port, ALICE, { action: "filter" })).status).toBe(403); | |
| 163 | const ok = await page(port, OWNER); | |
| 164 | expect(ok.status).toBe(200); | |
| 165 | const html = await ok.text(); | |
| 166 | expect(html).toContain("Word filter"); | |
| 167 | expect(html).not.toContain("<script"); | |
| 168 | }); | |
| 169 | ||
| 170 | test("the word list is editable from the page, takes effect live, and is logged", async () => { | |
| 171 | const port = await start(); | |
| 172 | const alice = await enter(port, ALICE); | |
| 173 | const settings = lobby!.filter.settings; | |
| 174 | const fields = { action: "filter", words: [...settings.words, "pineapple"].join("\n"), allowedHosts: "", maxRepeat: "6", maxCapsPercent: "70" }; | |
| 175 | expect((await form(port, ALICE, fields)).status).toBe(403); | |
| 176 | const saved = await form(port, OWNER, fields); | |
| 177 | expect(saved.status).toBe(303); | |
| 178 | expect(saved.headers.get("location")).toBe("/admin?done=filtered"); | |
| 179 | expect((await form(port, OWNER, { ...fields, maxRepeat: "-4" })).status).toBe(400); | |
| 180 | expect(lobby!.admin.log.map((e) => e.what)).toEqual(["changed the word filter"]); | |
| 181 | alice.send({ type: "post", text: "more pineapple" }); | |
| 182 | expect((await alice.until((m) => m.type === "error")).error).toMatch(/not allowed/); | |
| 183 | }); | |
| 184 | ||
| 185 | test("a report reaches the page, where it is removed, muted or dismissed, each logged", async () => { | |
| 186 | const port = await start(); | |
| 187 | const owner = await enter(port, OWNER); | |
| 188 | const alice = await enter(port, ALICE); | |
| 189 | const bob = await enter(port, BOB); | |
| 190 | alice.send({ type: "post", text: "rude <b>thing</b>" }); | |
| 191 | const id = (await bob.until((m) => m.type === "said")).message.id; | |
| 192 | alice.send({ type: "report", id }); | |
| 193 | expect((await alice.until((m) => m.type === "error")).error).toMatch(/your own/); | |
| 194 | bob.send({ type: "report", id }); | |
| 195 | await bob.until((m) => m.type === "reported"); | |
| 196 | bob.send({ type: "report", id }); // twice is still one report, and the sender is told so | |
| 197 | expect((await bob.until((m) => m.type === "error")).error).toBe("already reported"); | |
| 198 | bob.send({ type: "report", id: 999 }); | |
| 199 | expect((await bob.until((m) => m.type === "error" && m.error !== "already reported")).error).toBe("that line is gone"); | |
| 200 | const html = await (await page(port, OWNER)).text(); | |
| 201 | expect(html).toContain("rude <b>thing</b>"); // escaped | |
| 202 | expect(html).not.toContain("<b>thing</b>"); | |
| 203 | ||
| 204 | const reportId = String(lobby!.reports.list[0]!.id); | |
| 205 | expect((await form(port, OWNER, { action: "remove", report: reportId })).status).toBe(303); | |
| 206 | await bob.until((m) => m.type === "unsaid" && m.id === id); | |
| 207 | expect(lobby!.chat.messages.find((m) => m.id === id)).toBeUndefined(); | |
| 208 | expect(lobby!.reports.list).toEqual([]); | |
| 209 | expect((await form(port, OWNER, { action: "remove", report: reportId })).status).toBe(400); // already settled | |
| 210 | ||
| 211 | await sleep(2100); | |
| 212 | alice.send({ type: "post", text: "again" }); | |
| 213 | const again = (await bob.until((m) => m.type === "said" && m.message.text === "again")).message.id; | |
| 214 | bob.send({ type: "report", id: again }); | |
| 215 | await bob.until((m) => m.type === "reported" && m.id === again); | |
| 216 | expect((await form(port, OWNER, { action: "mutefrom", report: String(lobby!.reports.list[0]!.id) })).status).toBe(303); | |
| 217 | expect(lobby!.chat.muted.has("u-alice")).toBe(true); | |
| 218 | await owner.until((m) => m.type === "muted" && m.muted.length === 1); | |
| 219 | expect(await (await page(port, OWNER)).text()).toContain("Unmute"); | |
| 220 | expect((await form(port, OWNER, { action: "unmute", user: "u-alice" })).status).toBe(303); | |
| 221 | expect(lobby!.chat.muted.size).toBe(0); | |
| 222 | ||
| 223 | await sleep(2100); | |
| 224 | alice.send({ type: "post", text: "fine" }); | |
| 225 | const fine = (await bob.until((m) => m.type === "said" && m.message.text === "fine")).message.id; | |
| 226 | bob.send({ type: "report", id: fine }); | |
| 227 | await bob.until((m) => m.type === "reported" && m.id === fine); | |
| 228 | expect((await form(port, OWNER, { action: "dismissreport", report: String(lobby!.reports.list[0]!.id) })).status).toBe(303); | |
| 229 | expect(lobby!.reports.list).toEqual([]); | |
| 230 | expect(lobby!.chat.messages.some((m) => m.text === "fine")).toBe(true); | |
| 231 | expect(lobby!.admin.log.map((e) => e.what)).toEqual([ | |
| 232 | `removed alice's line "rude <b>thing</b>" after a report`, | |
| 233 | "muted alice after a report", | |
| 234 | "unmuted alice", | |
| 235 | "dismissed a report on alice's line", | |
| 236 | ]); | |
| 237 | }); | |
| 238 | }); |
Software/so101/src/filter.test.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { Filter, FilterError, linkHosts, parseSettings } from "./filter.ts"; | |
| 6 | ||
| 7 | describe("filter", () => { | |
| 8 | test("blocks listed words, whole words only, through disguises", () => { | |
| 9 | const f = new Filter(); | |
| 10 | for (const bad of ["what the fuck", "FUCK", "f.u.c.k", "fuuuck you", "sh1t", "$hit happens", "fück", "fu\u200Bck"]) expect(f.check(bad)).not.toBeNull(); | |
| 11 | for (const ok of ["hello there", "classic Scunthorpe", "assassin", "move the arm left", "I like 3 of them"]) expect(f.check(ok)).toBeNull(); | |
| 12 | }); | |
| 13 | ||
| 14 | test("the list is editable and takes effect on the next line", () => { | |
| 15 | const f = new Filter(); | |
| 16 | expect(f.check("banana")).toBeNull(); | |
| 17 | f.update({ ...f.settings, words: ["banana", "two words"] }); | |
| 18 | expect(f.check("a Banana!")).not.toBeNull(); | |
| 19 | expect(f.check("say two words now")).not.toBeNull(); | |
| 20 | expect(f.check("fuck")).toBeNull(); | |
| 21 | }); | |
| 22 | ||
| 23 | test("links are refused unless allowed, or the host is", () => { | |
| 24 | const f = new Filter(); | |
| 25 | for (const t of ["see http://evil.example/x", "www.spam.com", "go to spam.io now", "https://a.b.org:8080/p"]) expect(f.check(t)).toMatch(/links/); | |
| 26 | expect(f.check("e.g. this, i.e. that. Hello.World")).toBeNull(); | |
| 27 | f.update({ ...f.settings, allowedHosts: ["github.com"] }); | |
| 28 | expect(f.check("https://github.com/x and docs.github.com")).toBeNull(); | |
| 29 | expect(f.check("https://evilgithub.com")).toMatch(/links/); | |
| 30 | f.update({ ...f.settings, allowLinks: true }); | |
| 31 | expect(f.check("http://evil.example")).toBeNull(); | |
| 32 | expect(linkHosts("x https://WWW.Foo.com/a")).toEqual(["foo.com"]); | |
| 33 | }); | |
| 34 | ||
| 35 | test("repeats and caps", () => { | |
| 36 | const f = new Filter(); | |
| 37 | expect(f.check("aaaaaaa")).toMatch(/in a row/); | |
| 38 | expect(f.check("aaaaaa")).toBeNull(); | |
| 39 | expect(f.check("STOP SHOUTING AT ME")).toMatch(/shout/); | |
| 40 | expect(f.check("OK GO")).toBeNull(); // too short to count | |
| 41 | f.update({ ...f.settings, maxRepeat: 0, maxCapsPercent: 100 }); | |
| 42 | expect(f.check("aaaaaaaaaa STOP SHOUTING AT ME")).toBeNull(); | |
| 43 | }); | |
| 44 | ||
| 45 | test("settings are validated, and survive a restart", () => { | |
| 46 | const file = join(mkdtempSync(join(tmpdir(), "so101-filter-")), "filter.json"); | |
| 47 | const f = new Filter(file); | |
| 48 | const good = { ...f.settings }; | |
| 49 | for (const bad of [null, [], { ...good, words: "x" }, { ...good, words: [1] }, { ...good, allowLinks: "yes" }, { ...good, maxRepeat: -1 }, { ...good, maxCapsPercent: 101 }, { ...good, allowedHosts: ["not a host"] }, { ...good, words: ["x".repeat(41)] }]) { | |
| 50 | expect(() => parseSettings(bad)).toThrow(FilterError); | |
| 51 | expect(() => f.update(bad)).toThrow(FilterError); | |
| 52 | } | |
| 53 | expect(f.settings).toEqual(good); | |
| 54 | f.update({ ...good, words: ["Zebra", " zebra "] }); | |
| 55 | expect(new Filter(file).settings.words).toEqual(["zebra"]); | |
| 56 | }); | |
| 57 | }); |
Software/so101/src/filter.ts added +146
| @@ -0,0 +1,146 @@ | ||
| 1 | /** | |
| 2 | * The word filter: a deterministic check on a chat line before it is broadcast. The admin edits the settings | |
| 3 | * (blocked words, links, repeats, caps) and they take effect on the next line; they live in one JSON file. | |
| 4 | * A refused line gets a short reason, shown to its sender only. | |
| 5 | */ | |
| 6 | ||
| 7 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 8 | import { dirname } from "node:path"; | |
| 9 | ||
| 10 | export interface FilterSettings { | |
| 11 | /** Lower-case words and phrases that may not appear. */ | |
| 12 | words: string[]; | |
| 13 | /** Links are refused unless true, or their host is in `allowedHosts`. */ | |
| 14 | allowLinks: boolean; | |
| 15 | allowedHosts: string[]; | |
| 16 | /** The longest run of one character; 0 turns the check off. */ | |
| 17 | maxRepeat: number; | |
| 18 | /** The most capitals, in percent of the letters, in a line of 8 letters or more; 100 turns the check off. */ | |
| 19 | maxCapsPercent: number; | |
| 20 | } | |
| 21 | ||
| 22 | export const DEFAULT_WORDS = [ | |
| 23 | "fuck", "fucking", "fucker", "shit", "bitch", "bastard", "asshole", "dick", "cunt", "slut", "whore", | |
| 24 | "nigger", "nigga", "faggot", "retard", "rape", "nazi", | |
| 25 | ]; | |
| 26 | ||
| 27 | export const DEFAULTS: FilterSettings = { words: DEFAULT_WORDS, allowLinks: false, allowedHosts: [], maxRepeat: 6, maxCapsPercent: 70 }; | |
| 28 | ||
| 29 | export class FilterError extends Error {} | |
| 30 | ||
| 31 | const LEET: Record<string, string> = { "0": "o", "1": "i", "3": "e", "4": "a", "5": "s", "7": "t", "@": "a", $: "s" }; | |
| 32 | /** Lower case, accents off, look-alike digits and symbols back to letters, runs of a letter squeezed to one. */ | |
| 33 | const fold = (text: string): string => | |
| 34 | text | |
| 35 | .normalize("NFKD") | |
| 36 | .replace(/[\u0300-\u036f\p{Cf}]/gu, "") // accents, and zero-width and other invisible format characters | |
| 37 | .toLowerCase() | |
| 38 | .replace(/[013457@$]/g, (c) => LEET[c] ?? c) | |
| 39 | .replace(/([a-z])\1+/g, "$1"); | |
| 40 | /** The words of a line; letters spelled out one by one ("f u c k") are put back together. */ | |
| 41 | const tokens = (text: string): string[] => { | |
| 42 | const out: string[] = []; | |
| 43 | let spelled = ""; | |
| 44 | for (const t of [...fold(text).split(/[^a-z]+/), ""]) { | |
| 45 | if (t.length === 1) spelled += t; | |
| 46 | else { | |
| 47 | if (spelled) out.push(spelled); | |
| 48 | spelled = ""; | |
| 49 | if (t) out.push(t); | |
| 50 | } | |
| 51 | } | |
| 52 | return out; | |
| 53 | }; | |
| 54 | ||
| 55 | const LINK = /\b(?:[a-z][a-z0-9+.-]*:\/\/|www\.)([^\s/?#:]+)|\b((?:[a-z0-9-]+\.)+(?:com|net|org|io|dev|app|xyz|me|co|gg|ly|tv|info|biz|ru|cn|tk|ml|ga|cf|gq|to|cc|sh|link|site|online|top|club|shop))\b/gi; | |
| 56 | ||
| 57 | export function linkHosts(text: string): string[] { | |
| 58 | return [...text.matchAll(LINK)].map((m) => (m[1] ?? m[2])!.toLowerCase().replace(/^www\./, "")); | |
| 59 | } | |
| 60 | ||
| 61 | /** Validate what an admin sent into settings, naming the first thing wrong. */ | |
| 62 | export function parseSettings(input: unknown): FilterSettings { | |
| 63 | if (typeof input !== "object" || input === null || Array.isArray(input)) throw new FilterError("settings are an object"); | |
| 64 | const i = input as Record<string, unknown>; | |
| 65 | const list = (value: unknown, what: string, max: number, pattern: RegExp): string[] => { | |
| 66 | if (!Array.isArray(value) || value.length > max) throw new FilterError(`${what}: a list of at most ${max}`); | |
| 67 | const out = value.map((v) => { | |
| 68 | if (typeof v !== "string") throw new FilterError(`${what}: text only`); | |
| 69 | return v.trim().toLowerCase(); | |
| 70 | }).filter(Boolean); | |
| 71 | for (const v of out) if (!pattern.test(v)) throw new FilterError(`${what}: "${v.slice(0, 40)}" is not allowed`); | |
| 72 | return [...new Set(out)]; | |
| 73 | }; | |
| 74 | const number = (value: unknown, what: string, min: number, max: number): number => { | |
| 75 | if (!Number.isInteger(value) || (value as number) < min || (value as number) > max) throw new FilterError(`${what}: a whole number from ${min} to ${max}`); | |
| 76 | return value as number; | |
| 77 | }; | |
| 78 | if (typeof i.allowLinks !== "boolean") throw new FilterError("allowLinks: true or false"); | |
| 79 | return { | |
| 80 | words: list(i.words, "words", 1000, /^.{1,40}$/u), | |
| 81 | allowLinks: i.allowLinks, | |
| 82 | allowedHosts: list(i.allowedHosts, "allowedHosts", 100, /^[a-z0-9]([a-z0-9.-]{0,251}[a-z0-9])?$/), | |
| 83 | maxRepeat: number(i.maxRepeat, "maxRepeat", 0, 500), | |
| 84 | maxCapsPercent: number(i.maxCapsPercent, "maxCapsPercent", 0, 100), | |
| 85 | }; | |
| 86 | } | |
| 87 | ||
| 88 | /** The word-filter form's fields (a line per word, a checkbox, numbers as text) as settings to validate. */ | |
| 89 | export function settingsFromForm(form: URLSearchParams): unknown { | |
| 90 | const lines = (name: string) => (form.get(name) ?? "").split(/\r?\n/); | |
| 91 | const whole = (name: string) => { | |
| 92 | const v = form.get(name) ?? ""; | |
| 93 | return /^\s*\d{1,6}\s*$/.test(v) ? Number(v) : NaN; | |
| 94 | }; | |
| 95 | return { words: lines("words"), allowLinks: form.get("allowLinks") === "on", allowedHosts: lines("allowedHosts"), maxRepeat: whole("maxRepeat"), maxCapsPercent: whole("maxCapsPercent") }; | |
| 96 | } | |
| 97 | ||
| 98 | export class Filter { | |
| 99 | private current: FilterSettings = { ...DEFAULTS, words: [...DEFAULTS.words] }; | |
| 100 | private folded: string[] = []; | |
| 101 | private readonly file: string | undefined; | |
| 102 | ||
| 103 | constructor(file?: string) { | |
| 104 | this.file = file; | |
| 105 | if (file) { | |
| 106 | try { | |
| 107 | this.current = parseSettings(JSON.parse(readFileSync(file, "utf8"))); | |
| 108 | } catch { | |
| 109 | // none yet, or unreadable: the defaults | |
| 110 | } | |
| 111 | } | |
| 112 | this.index(); | |
| 113 | } | |
| 114 | ||
| 115 | get settings(): FilterSettings { | |
| 116 | return this.current; | |
| 117 | } | |
| 118 | ||
| 119 | update(input: unknown): FilterSettings { | |
| 120 | this.current = parseSettings(input); | |
| 121 | this.index(); | |
| 122 | if (this.file) { | |
| 123 | mkdirSync(dirname(this.file), { recursive: true }); | |
| 124 | writeFileSync(`${this.file}.tmp`, JSON.stringify(this.current)); | |
| 125 | renameSync(`${this.file}.tmp`, this.file); | |
| 126 | } | |
| 127 | return this.current; | |
| 128 | } | |
| 129 | ||
| 130 | /** Why this line may not be sent, or null. */ | |
| 131 | check(text: string): string | null { | |
| 132 | const s = this.current; | |
| 133 | const found = tokens(text); | |
| 134 | const joined = ` ${found.join(" ")} `; | |
| 135 | if (this.folded.some((w) => joined.includes(` ${w} `))) return "that language is not allowed here"; | |
| 136 | if (!s.allowLinks && linkHosts(text).some((h) => !s.allowedHosts.some((a) => h === a || h.endsWith(`.${a}`)))) return "links are not allowed here"; | |
| 137 | if (s.maxRepeat > 0 && new RegExp(`(.)\\1{${s.maxRepeat},}`, "su").test(text)) return `no more than ${s.maxRepeat} of the same character in a row`; | |
| 138 | const letters = text.replace(/[^\p{L}]/gu, ""); | |
| 139 | if (s.maxCapsPercent < 100 && letters.length >= 8 && (letters.replace(/[^\p{Lu}]/gu, "").length * 100) / letters.length > s.maxCapsPercent) return "please do not shout"; | |
| 140 | return null; | |
| 141 | } | |
| 142 | ||
| 143 | private index(): void { | |
| 144 | this.folded = this.current.words.map((w) => tokens(w).join(" ")).filter(Boolean); | |
| 145 | } | |
| 146 | } |
Software/so101/src/lobby.ts +89 −14
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * {"type": "leave"} hand back the turn, or leave the line |
| 10 | 10 | * {"type": "post", "text": "…"} say something |
| 11 | 11 | * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line |
| 12 | * {"type": "report", "id": 12} report a chat line to the owner (answered with {"type": "reported"}) | |
| 12 | 13 | * {"type": "delete", "id": 12} owner: delete a chat line |
| 13 | 14 | * {"type": "mute", "id": "<user id>", "on": true|false} owner |
| 14 | 15 | * {"type": "pause", "on": true|false} owner: maintenance; nobody new gets a turn |
| @@ -28,8 +29,10 @@ import type { Duplex } from "node:stream"; | ||
| 28 | 29 | import WebSocket, { WebSocketServer } from "ws"; |
| 29 | 30 | import { Accounts, visitor, type Visitor } from "./accounts.ts"; |
| 30 | 31 | import { Admin, AdminError } from "./admin.ts"; |
| 31 | import { Chat, ChatError } from "./chat.ts"; | |
| 32 | import { Chat, ChatError, cleanText } from "./chat.ts"; | |
| 33 | import { Filter, FilterError } from "./filter.ts"; | |
| 32 | 34 | import { Moderator } from "./moderator.ts"; |
| 35 | import { Reports } from "./reports.ts"; | |
| 33 | 36 | import { identify, LOCAL, type Person, type Who } from "./identity.ts"; |
| 34 | 37 | import { Turns, type TurnEvent } from "./turns.ts"; |
| 35 | 38 | |
| @@ -48,6 +51,11 @@ export interface LobbyOptions { | ||
| 48 | 51 | turns?: Turns; |
| 49 | 52 | chat?: Chat; |
| 50 | 53 | admin?: Admin; |
| 54 | /** The word filter's settings and the reports; none keeps them in memory. */ | |
| 55 | filterFile?: string; | |
| 56 | reportsFile?: string; | |
| 57 | filter?: Filter; | |
| 58 | reports?: Reports; | |
| 51 | 59 | /** Checks each posted line in the background; none leaves the chat unchecked. */ |
| 52 | 60 | moderator?: Moderator; |
| 53 | 61 | /** How often each socket is pinged, and how long it may stay silent before it counts as dropped. */ |
| @@ -68,6 +76,8 @@ const USER_ID = /^[A-Za-z0-9_-]{1,64}$/; | ||
| 68 | 76 | export class Lobby { |
| 69 | 77 | readonly turns: Turns; |
| 70 | 78 | readonly chat: Chat; |
| 79 | readonly filter: Filter; | |
| 80 | readonly reports: Reports; | |
| 71 | 81 | readonly accounts: Accounts; |
| 72 | 82 | readonly admin: Admin; |
| 73 | 83 | readonly local: boolean; |
| @@ -88,6 +98,8 @@ export class Lobby { | ||
| 88 | 98 | this.turns = options.turns ?? new Turns({ turnMs: a.turnSeconds * 1000, idleMs: a.idleSeconds * 1000, maxQueue: a.maxQueue }); |
| 89 | 99 | this.turns.setPaused(a.paused); |
| 90 | 100 | this.chat = options.chat ?? new Chat({ file: options.chatFile }); |
| 101 | this.filter = options.filter ?? new Filter(options.filterFile); | |
| 102 | this.reports = options.reports ?? new Reports(options.reportsFile); | |
| 91 | 103 | this.accounts = options.accounts ?? new Accounts(); |
| 92 | 104 | this.silentMs = options.silentMs ?? 15_000; |
| 93 | 105 | this.moderator = options.moderator; |
| @@ -261,11 +273,26 @@ export class Lobby { | ||
| 261 | 273 | this.turns.leave(who.id); |
| 262 | 274 | return; |
| 263 | 275 | case "post": { |
| 264 | const said = this.chat.post(who, m.text); | |
| 276 | // Order matters: muted, then the word filter (both answer the sender only), then store, then the moderator. | |
| 277 | if (this.chat.muted.has(who.id)) throw new ChatError("you are muted"); | |
| 278 | const text = cleanText(m.text); | |
| 279 | const refused = this.filter.check(text); | |
| 280 | if (refused) throw new ChatError(refused); | |
| 281 | const said = this.chat.post(who, text); | |
| 265 | 282 | this.broadcast({ type: "said", message: said }); |
| 266 | 283 | void this.moderator?.check(said); |
| 267 | 284 | return; |
| 268 | 285 | } |
| 286 | case "report": { | |
| 287 | if (this.chat.muted.has(who.id)) throw new ChatError("you are muted"); | |
| 288 | const line = this.chat.messages.find((x) => x.id === m.id); | |
| 289 | if (!line?.from) throw new ChatError("that line is gone"); | |
| 290 | if (line.from.id === who.id) throw new ChatError("that is your own line"); | |
| 291 | const added = this.reports.add({ id: line.id, text: line.text, from: line.from }, { id: who.id, name: who.name }); | |
| 292 | if (added === "duplicate") throw new ChatError("already reported"); | |
| 293 | if (added === "limit") throw new ChatError("too many open reports; wait for the owner to look at them"); | |
| 294 | return this.send(seat, { type: "reported", id: line.id }); | |
| 295 | } | |
| 269 | 296 | } |
| 270 | 297 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); |
| 271 | 298 | switch (m.type) { |
| @@ -282,20 +309,12 @@ export class Lobby { | ||
| 282 | 309 | return this.configure(who, m.set); |
| 283 | 310 | case "delete": |
| 284 | 311 | if (!Number.isSafeInteger(m.id)) return this.send(seat, { type: "error", error: "delete which line?" }); |
| 285 | if (this.chat.delete(m.id as number)) this.broadcast({ type: "unsaid", id: m.id }); | |
| 312 | this.removeLine(m.id as number); | |
| 286 | 313 | return; |
| 287 | case "mute": { | |
| 314 | case "mute": | |
| 288 | 315 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
| 289 | const id = m.id; | |
| 290 | const name = [...this.seats].find((s) => s.who?.id === id)?.who?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.chat.muted.get(id) ?? id; | |
| 291 | this.chat.mute(id, name, m.on); | |
| 292 | for (const s of this.seats) { | |
| 293 | if (s.who?.id === id) this.send(s, this.hello(s)); | |
| 294 | if (s.who?.owner) this.send(s, this.mutedList()); | |
| 295 | } | |
| 296 | this.moderator?.muted(id); | |
| 316 | this.mute(m.id, m.on); | |
| 297 | 317 | return; |
| 298 | } | |
| 299 | 318 | case "ban": { |
| 300 | 319 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "ban whom?" }); |
| 301 | 320 | const id = m.id; |
| @@ -344,6 +363,57 @@ export class Lobby { | ||
| 344 | 363 | this.send(seat, { type: "error", error: "unknown request" }); |
| 345 | 364 | } |
| 346 | 365 | |
| 366 | /** Delete a chat line for everyone; true when it existed. */ | |
| 367 | removeLine(id: number): boolean { | |
| 368 | const gone = this.chat.delete(id); | |
| 369 | if (gone) this.broadcast({ type: "unsaid", id }); | |
| 370 | return gone; | |
| 371 | } | |
| 372 | ||
| 373 | /** Mute or unmute a person, and tell them and the owners. */ | |
| 374 | mute(id: string, on: boolean): void { | |
| 375 | const name = [...this.seats].find((s) => s.who?.id === id)?.who?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.reports.list.find((r) => r.message.from.id === id)?.message.from.name ?? this.chat.muted.get(id) ?? id; | |
| 376 | this.chat.mute(id, name, on); | |
| 377 | for (const s of this.seats) { | |
| 378 | if (s.who?.id === id) this.send(s, this.hello(s)); | |
| 379 | if (s.who?.owner) this.send(s, this.mutedList()); | |
| 380 | } | |
| 381 | this.moderator?.muted(id); | |
| 382 | } | |
| 383 | ||
| 384 | /** The owner's decision on a report from /admin; every report on the line is settled. Logged. */ | |
| 385 | settleReport(who: Person, reportId: number, action: "remove" | "mute" | "dismiss"): void { | |
| 386 | const report = this.reports.get(reportId); | |
| 387 | if (!report) throw new AdminError("that report is already settled"); | |
| 388 | const line = report.message; | |
| 389 | if (action === "remove") { | |
| 390 | this.removeLine(line.id); | |
| 391 | this.admin.record(who, `removed ${line.from.name}'s line "${line.text.slice(0, 60)}" after a report`); | |
| 392 | } else if (action === "mute") { | |
| 393 | this.mute(line.from.id, true); | |
| 394 | this.admin.record(who, `muted ${line.from.name} after a report`); | |
| 395 | } else this.admin.record(who, `dismissed a report on ${line.from.name}'s line`); | |
| 396 | this.reports.resolveLine(line.id); | |
| 397 | } | |
| 398 | ||
| 399 | /** Let a muted person speak again; logged. */ | |
| 400 | unmute(who: Person, id: string): void { | |
| 401 | const name = this.chat.muted.get(id); | |
| 402 | if (name === undefined) throw new AdminError("that person is not muted"); | |
| 403 | this.mute(id, false); | |
| 404 | this.admin.record(who, `unmuted ${name}`); | |
| 405 | } | |
| 406 | ||
| 407 | /** Replace the word filter's settings; live at once, logged. */ | |
| 408 | setFilter(who: Person, input: unknown): void { | |
| 409 | try { | |
| 410 | this.filter.update(input); | |
| 411 | } catch (err) { | |
| 412 | throw err instanceof FilterError ? new AdminError(err.message) : err; | |
| 413 | } | |
| 414 | this.admin.record(who, "changed the word filter"); | |
| 415 | } | |
| 416 | ||
| 347 | 417 | private turnChanged(event: TurnEvent): void { |
| 348 | 418 | if (event.kind === "turn") { |
| 349 | 419 | const { ended, started } = event; |
| @@ -381,6 +451,11 @@ export class Lobby { | ||
| 381 | 451 | for (const s of this.seats) if (s.who?.owner) this.send(s, this.crewState()); |
| 382 | 452 | } |
| 383 | 453 | |
| 454 | /** Who is muted, for the lists the owner sees. */ | |
| 455 | mutedPeople(): Array<{ id: string; name: string }> { | |
| 456 | return [...this.chat.muted].map(([id, name]) => ({ id, name })); | |
| 457 | } | |
| 458 | ||
| 384 | 459 | private hello(seat: Seat) { |
| 385 | 460 | const who = seat.who; |
| 386 | 461 | return { type: "hello", you: who, signin: !this.local, muted: who ? this.chat.muted.has(who.id) : false, banned: who ? this.barred(who, seat.from) : false }; |
| @@ -391,7 +466,7 @@ export class Lobby { | ||
| 391 | 466 | } |
| 392 | 467 | |
| 393 | 468 | private mutedList() { |
| 394 | return { type: "muted", muted: [...this.chat.muted].map(([id, name]) => ({ id, name })) }; | |
| 469 | return { type: "muted", muted: this.mutedPeople() }; | |
| 395 | 470 | } |
| 396 | 471 | |
| 397 | 472 | private ping(): void { |
Software/so101/src/relay.ts +26 −2
| @@ -15,7 +15,9 @@ | ||
| 15 | 15 | * WS /live the lobby: the turn, the queue, the chat |
| 16 | 16 | * POST /join {"name": "…"}: become someone here, no forge account needed (src/accounts.ts); the answer |
| 17 | 17 | * sets the HttpOnly session cookie and names you: {"you": {"id", "name", "owner"}} |
| 18 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log | |
| 18 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, reports, the muted | |
| 19 | * and the word filter, the log | |
| 20 | ||
| 19 | 21 | * GET /link {"mode": "live"|"simulated", "host": "<host:port>"|null, "problem": "…"|null} |
| 20 | 22 | * GET /cameras the listed cameras: [{"id", "name", "online"}] |
| 21 | 23 | * GET /camera/<id>/<stream|state|snapshot> one listed camera, one upstream connection shared by all its viewers (src/cameras.ts, src/camera-feed.ts); |
| @@ -36,6 +38,7 @@ import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts"; | ||
| 36 | 38 | import { AccountError, sessionCookie, visitor } from "./accounts.ts"; |
| 37 | 39 | import { AdminError, changeFromForm } from "./admin.ts"; |
| 38 | 40 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; |
| 41 | import { settingsFromForm } from "./filter.ts"; | |
| 39 | 42 | import type { FeedOptions } from "./camera-feed.ts"; |
| 40 | 43 | import { CameraError, CameraHub } from "./cameras.ts"; |
| 41 | 44 | import { LOCAL as LOCAL_PERSON } from "./identity.ts"; |
| @@ -316,7 +319,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 316 | 319 | }; |
| 317 | 320 | if (!who?.owner) return html(who ? 403 : 401, renderRefusal(who)); |
| 318 | 321 | const show = (status: number, done: Done | null, error: string | null) => |
| 319 | html(status, renderAdmin({ who, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error })); | |
| 322 | html(status, renderAdmin({ who, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, reports: lobby.reports.list, muted: lobby.mutedPeople(), filter: lobby.filter.settings, done, error })); | |
| 320 | 323 | if (req.method === "GET" || req.method === "HEAD") { |
| 321 | 324 | const done = url.searchParams.get("done"); |
| 322 | 325 | return show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
| @@ -350,6 +353,27 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 350 | 353 | lobby.takeControl(who); |
| 351 | 354 | done = "taken"; |
| 352 | 355 | break; |
| 356 | case "remove": | |
| 357 | case "mutefrom": | |
| 358 | case "dismissreport": { | |
| 359 | const id = Number(form.get("report")); | |
| 360 | if (!Number.isSafeInteger(id)) throw new AdminError("no such report"); | |
| 361 | const verb = { remove: "remove", mutefrom: "mute", dismissreport: "dismiss" } as const; | |
| 362 | lobby.settleReport(who, id, verb[form.get("action") as "remove" | "mutefrom" | "dismissreport"]); | |
| 363 | done = form.get("action") === "remove" ? "removed" : form.get("action") === "mutefrom" ? "muted" : "dismissed"; | |
| 364 | break; | |
| 365 | } | |
| 366 | case "unmute": { | |
| 367 | const user = form.get("user") ?? ""; | |
| 368 | if (!/^[A-Za-z0-9_-]{1,64}$/.test(user)) throw new AdminError("no such person"); | |
| 369 | lobby.unmute(who, user); | |
| 370 | done = "unmuted"; | |
| 371 | break; | |
| 372 | } | |
| 373 | case "filter": | |
| 374 | lobby.setFilter(who, settingsFromForm(form)); | |
| 375 | done = "filtered"; | |
| 376 | break; | |
| 353 | 377 | default: |
| 354 | 378 | throw new AdminError("no such action"); |
| 355 | 379 | } |
Software/so101/src/reports.test.ts added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { KEEP_REPORTS, MAX_OPEN_PER_REPORTER, Reports } from "./reports.ts"; | |
| 3 | ||
| 4 | const line = (id: number) => ({ id, text: "x", from: { id: "u-a", name: "a" } }); | |
| 5 | const bob = { id: "u-bob", name: "bob" }; | |
| 6 | ||
| 7 | test("one reporter cannot hold more than the cap, and cannot evict anyone else's reports", () => { | |
| 8 | const reports = new Reports(); | |
| 9 | expect(reports.add(line(1), { id: "u-carol", name: "carol" })).toBe("ok"); | |
| 10 | for (let i = 0; i < MAX_OPEN_PER_REPORTER; i++) expect(reports.add(line(100 + i), bob)).toBe("ok"); | |
| 11 | expect(reports.add(line(500), bob)).toBe("limit"); | |
| 12 | expect(reports.add(line(100), bob)).toBe("duplicate"); | |
| 13 | expect(reports.list.filter((r) => r.by.id === bob.id)).toHaveLength(MAX_OPEN_PER_REPORTER); | |
| 14 | expect(reports.list.some((r) => r.by.id === "u-carol")).toBe(true); | |
| 15 | reports.resolveLine(100); // the owner settles one; room for another | |
| 16 | expect(reports.add(line(500), bob)).toBe("ok"); | |
| 17 | expect(KEEP_REPORTS).toBeGreaterThan(MAX_OPEN_PER_REPORTER); | |
| 18 | }); |
Software/so101/src/reports.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** | |
| 2 | * Reports: a signed-in person flags a chat line; the owner sees the line as it was (it may be deleted by then) | |
| 3 | * on /admin and removes it, mutes its author, or dismisses the report. Kept in one JSON file. | |
| 4 | */ | |
| 5 | ||
| 6 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 7 | import { dirname } from "node:path"; | |
| 8 | ||
| 9 | export const KEEP_REPORTS = 200; | |
| 10 | /** Open reports one person may have at a time, so one account cannot push everyone else's off the list. */ | |
| 11 | export const MAX_OPEN_PER_REPORTER = 10; | |
| 12 | ||
| 13 | export interface Report { | |
| 14 | id: number; | |
| 15 | at: number; | |
| 16 | /** The reported chat line. */ | |
| 17 | message: { id: number; text: string; from: { id: string; name: string } }; | |
| 18 | by: { id: string; name: string }; | |
| 19 | } | |
| 20 | ||
| 21 | const isPerson = (p: any): boolean => typeof p?.id === "string" && typeof p.name === "string"; | |
| 22 | ||
| 23 | export class Reports { | |
| 24 | list: Report[] = []; | |
| 25 | private next = 1; | |
| 26 | private readonly file: string | undefined; | |
| 27 | ||
| 28 | constructor(file?: string) { | |
| 29 | this.file = file; | |
| 30 | if (!file) return; | |
| 31 | try { | |
| 32 | const stored = JSON.parse(readFileSync(file, "utf8")); | |
| 33 | if (Array.isArray(stored)) this.list = stored.filter((r) => typeof r?.id === "number" && typeof r.at === "number" && typeof r.message?.id === "number" && typeof r.message.text === "string" && isPerson(r.message.from) && isPerson(r.by)).slice(-KEEP_REPORTS); | |
| 34 | } catch { | |
| 35 | // none yet, or unreadable | |
| 36 | } | |
| 37 | this.next = Math.max(0, ...this.list.map((r) => r.id)) + 1; | |
| 38 | } | |
| 39 | ||
| 40 | /** "duplicate" when this person already reported this line; "limit" when they have too many open reports. */ | |
| 41 | add(message: Report["message"], by: Report["by"], at = Date.now()): "ok" | "duplicate" | "limit" { | |
| 42 | if (this.list.some((r) => r.message.id === message.id && r.by.id === by.id)) return "duplicate"; | |
outdated
Add a per-reporter cap on open reports here (count list entries with by.id === by.id, return false or throw past ~10). Without it the KEEP_REPORTS eviction on the next lines lets one account erase everyone else's reports. |
||
| 43 | if (this.list.filter((r) => r.by.id === by.id).length >= MAX_OPEN_PER_REPORTER) return "limit"; | |
| 44 | this.list.push({ id: this.next++, at, message, by }); | |
| 45 | if (this.list.length > KEEP_REPORTS) this.list.splice(0, this.list.length - KEEP_REPORTS); | |
| 46 | this.save(); | |
| 47 | return "ok"; | |
| 48 | } | |
| 49 | ||
| 50 | get(id: number): Report | undefined { | |
| 51 | return this.list.find((r) => r.id === id); | |
| 52 | } | |
| 53 | ||
| 54 | /** Drop every report on this line (one decision settles them all); false when none. */ | |
| 55 | resolveLine(messageId: number): boolean { | |
| 56 | const before = this.list.length; | |
| 57 | this.list = this.list.filter((r) => r.message.id !== messageId); | |
| 58 | if (this.list.length !== before) this.save(); | |
| 59 | return this.list.length !== before; | |
| 60 | } | |
| 61 | ||
| 62 | private save(): void { | |
| 63 | if (!this.file) return; | |
| 64 | mkdirSync(dirname(this.file), { recursive: true }); | |
| 65 | writeFileSync(`${this.file}.tmp`, JSON.stringify(this.list)); | |
| 66 | renameSync(`${this.file}.tmp`, this.file); | |
| 67 | } | |
| 68 | } |
Software/so101/src/viewer.html +4
| @@ -182,6 +182,8 @@ | ||
| 182 | 182 | #adminForm { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; } |
| 183 | 183 | #adminForm label { display: grid; gap: 4px; color: var(); font-size: 13px; } |
| 184 | 184 | #adminForm input { width: 100%; min-width: 0; font-variant-numeric: tabular-nums; } |
| 185 | #chat .rep { visibility: hidden; margin-left: 6px; min-height: 22px; padding: 0 6px; font-size: 11px; } #chat .line:hover .rep, #chat .line:focus-within .rep { visibility: visible; } | |
| 186 | @media (hover: none), (pointer: coarse) { #chat .rep { visibility: visible; min-height: 44px; min-width: 44px; } } | |
| 185 | 187 | #adminForm .wide { grid-column: 1 / -1; } |
| 186 | 188 | #crewLine { display: grid; gap: 8px; font-size: 14px; } #crewLine:empty { display: none; } |
| 187 | 189 | #crewLine textarea { width: 100%; font: inherit; font-size: 16px; color: var(); background: var(); border: 1px solid var(); border-radius: 10px; padding: 8px 10px; } |
| @@ -1329,6 +1331,7 @@ | ||
| 1329 | 1331 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); } |
| 1330 | 1332 | line.append(el("span", { textContent: m.text })); |
| 1331 | 1333 | if (m.from && me?.owner) line.tabIndex = 0; |
| 1334 | if (m.from && me && m.from.id !== me.id && !me.owner) line.append(el("button", { className: "rep", textContent: "report", title: "Tell the owner about this line", onclick: () => say({ type: "report", id: m.id }) })); | |
| 1332 | 1335 | return line; |
| 1333 | 1336 | } |
| 1334 | 1337 | function addChat(lines, replace = false) { |
| @@ -1384,6 +1387,7 @@ | ||
| 1384 | 1387 | else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove(); |
| 1385 | 1388 | else if (m.type === "muted") lobby.mutedList = m.muted; |
| 1386 | 1389 | else if (m.type === "banned") lobby.bannedList = m.banned; |
| 1390 | else if (m.type === "reported") return msg("Reported. The owner will take a look."); | |
| 1387 | 1391 | else if (m.type === "admin") { const saved = lobby.admin !== null; lobby.admin = m; fillAdmin(saved); } |
| 1388 | 1392 | else if (m.type === "crew") lobby.crew = m; |
| 1389 | 1393 | else if (m.type === "error") return msg(m.error, true); |
new above
offline
This only waits for any error. Assert the text ('already reported'), and add the 11th-open-report case through the lobby.