Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/admin lists open reports with Remove, Mute and Dismiss beside each #406
smith/arm:smith/243-admin-lists-open-reports-with-remove-mut → main 10 commits fork
8 files
changed +229
−21
Software/README.md +2 −1
| @@ -530,7 +530,7 @@ watches. A line you type is said in the chat; commands start with `/`: | ||
| 530 | 530 | `resumed` when it is resumed. |
| 531 | 531 | - `/end [name]`, owner: end that turn (no name: whoever drives), or take |
| 532 | 532 | them out of the line. |
| 533 | - `/report <name|#n>`, anyone signed in: report that person's latest chat line, or line number n; prints `reported #n` when the server took it. `/reports`, owner or moderator: the reported lines, newest first (`report #n author: text (by reporter)`); anyone else is told it is for them. The owner and moderators are told `N reports to review: /reports` on join and get one line per new report. Reports are kept in `$FORGE_APP_DATA/so101/reports.json` (one per reporter per line, 200 at most). | |
| 533 | - `/report <name|#n>`, anyone signed in: report that person's latest chat line, or line number n; prints `reported #n` when the server took it. `/reports`, owner or moderator: the reported lines, newest first (`report #n author: text (by reporter)`); anyone else is told it is for them. `/dismiss #n`, owner or moderator: close that line's reports and nothing else (the page's Dismiss; logged, and every owner and moderator sees the list update). The owner and moderators are told `N reports to review: /reports` on join and get one line per new report. Reports are kept in `$FORGE_APP_DATA/so101/reports.json` (one per reporter per line, 200 at most). | |
| 534 | 534 | - `/removed`, `/restore #<n>`, owner: list the latest removed chat lines, and put one back for everyone. |
| 535 | 535 | - `/mute <name> [10m|1h|24h|forever]`, `/unmute <name>`, owner or moderator. A mute without a length is for good; a timed one ends on its own and the owner's list says when. |
| 536 | 536 | - `/table here`, owner: the page's "Table is here", the same |
| @@ -564,6 +564,7 @@ reaches it as the owner. | ||
| 564 | 564 | **Admin.** The owner runs the room from `/admin` (plain forms, and one pinned script for Health and the table height), |
| 565 | 565 | or from the **Admin** tab that only the owner sees: |
| 566 | 566 | |
| 567 | - **Reports** (owner and moderators; `/admin` is open to a moderator who holds a forge identity) lists the open chat reports, newest first, each with **Remove** (take the line down for everyone), **Mute** (mute its author for good) and **Dismiss** (close the reports, nothing else). Each closes the line's reports and is written to the admin log. `/delete` and `/mute` from either door close them too. | |
| 567 | 568 | - **Table height** (-100..100 mm, `tableOffsetMm`) is a number field under Table height on `/admin`: |
| 568 | 569 | Save sends `PUT /settings` and shows the saved value or the server's error. |
| 569 | 570 | - **Pause the queue** for maintenance. The driver's turn ends (unless it is |
Software/so101/src/admin-page.ts +14 −2
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | import { createHash } from "node:crypto"; |
| 8 | 8 | import { ANNOUNCEMENT_CHARS, type AdminSettings, type LogEntry } from "./admin.ts"; |
| 9 | 9 | import type { Removed } from "./chat.ts"; |
| 10 | import type { Report } from "./reports.ts"; | |
| 10 | 11 | import type { Person, Who } from "./identity.ts"; |
| 11 | 12 | import type { Snapshot } from "./turns.ts"; |
| 12 | 13 | import type { ViewerRow } from "./viewer-reports.ts"; |
| @@ -124,7 +125,7 @@ export const adminCsp = (canEdit: boolean): string => | ||
| 124 | 125 | `default-src 'none'; style-src 'sha256-${sha(STYLE)}'; script-src 'sha256-${sha(scriptFor(canEdit))}'; connect-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`; |
| 125 | 126 | |
| 126 | 127 | /** The fixed lines a redirect back may ask for; nothing from the request is echoed. */ |
| 127 | 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.", restored: "Line restored.", lifted: "Lifted." } as const; | |
| 128 | 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.", restored: "Line restored.", lifted: "Lifted.", reportRemoved: "Line removed; report closed.", reportMuted: "Author muted; report closed.", reportDismissed: "Report dismissed." } as const; | |
| 128 | 129 | export type Done = keyof typeof DONE; |
| 129 | 130 | |
| 130 | 131 | export interface Target { id: string; name: string } |
| @@ -140,6 +141,8 @@ export interface AdminPage { | ||
| 140 | 141 | log: readonly LogEntry[]; |
| 141 | 142 | /** Chat lines a person removed, newest first. */ |
| 142 | 143 | removed?: readonly Removed[]; |
| 144 | /** Open chat-line reports, newest first. */ | |
| 145 | reports?: readonly Report[]; | |
| 143 | 146 | /** Mutes and bans in force, each with a button to lift it. */ |
| 144 | 147 | muted?: readonly Target[]; |
| 145 | 148 | banned?: readonly Target[]; |
| @@ -190,7 +193,7 @@ const action = (name: string, text: string, cls = "", disabled = false) => | ||
| 190 | 193 | `<form method="post" action="/admin"><input type="hidden" name="action" value="${name}"><button class="${cls}"${disabled ? " disabled" : ""}>${text}</button></form>`; |
| 191 | 194 | |
| 192 | 195 | /** The log lines that are moderation: chat-line deletions, removals from the line, mutes, bans and their reversals. */ |
| 193 | export const isModeration = (e: LogEntry): boolean => /^(banned|unbanned|muted|unmuted|deleted chat line|restored chat line|took .+ out of the line)/.test(e.what); | |
| 196 | export const isModeration = (e: LogEntry): boolean => /^(banned|unbanned|muted|unmuted|deleted chat line|restored chat line|dismissed the reports|took .+ out of the line)/.test(e.what); | |
| 194 | 197 | |
| 195 | 198 | /** Removed chat lines, each with who took it down and, for the owner, a Restore button. */ |
| 196 | 199 | const removedLines = (list: readonly Removed[], canEdit: boolean): string => |
| @@ -198,6 +201,14 @@ const removedLines = (list: readonly Removed[], canEdit: boolean): string => | ||
| 198 | 201 | ? `<ol>${list.map((r) => `<li>${time(r.at)}<span><b>${esc(r.by.name)}</b> removed ${esc(r.message.from?.name ?? "system")}: ${esc(r.message.text)}${canEdit ? ` <form method="post" action="/admin" class="inline"><input type="hidden" name="action" value="restore"><input type="hidden" name="id" value="${r.message.id}"><button aria-label="Restore line ${r.message.id}: ${esc([...r.message.text].slice(0, 40).join(""))}">Restore</button></form>` : ""}</span></li>`).join("")}</ol>` |
| 199 | 202 | : `<p class="empty">No removed lines.</p>`; |
| 200 | 203 | |
| 204 | /** Open reports, newest first, each with Remove, Mute and Dismiss: the owner's and a moderator's alike. */ | |
| 205 | const reportList = (list: readonly Report[]): string => { | |
| 206 | if (!list.length) return `<p class="empty">No open reports.</p>`; | |
| 207 | const button = (r: Report, act: string, text: string, cls = "") => | |
| 208 | `<form method="post" action="/admin" class="inline"><input type="hidden" name="action" value="report-${act}"><input type="hidden" name="id" value="${esc(r.id)}"><button${cls ? ` class="${cls}"` : ""} aria-label="${text} line ${esc(r.id)} by ${esc(r.author)}">${text}</button></form>`; | |
| 209 | return `<ol>${list.map((r) => `<li>${time(r.at)}<span><b>${esc(r.author)}</b>: ${esc(r.text)} ${button(r, "remove", "Remove", "danger")} ${button(r, "mute", "Mute")} ${button(r, "dismiss", "Dismiss")}</span></li>`).join("")}</ol>`; | |
| 210 | }; | |
| 211 | ||
| 201 | 212 | /** Who is muted or banned now, each with a form button posting to /admin/lift. */ |
| 202 | 213 | const inForce = (kind: "mute" | "ban", list: readonly Target[], canLift: (name: string) => boolean): string => { |
| 203 | 214 | const verb = kind === "mute" ? "Unmute" : "Unban"; |
| @@ -253,6 +264,7 @@ ${p.canEdit ? `<section aria-labelledby="table"><h2 id="table">Table height</h2> | ||
| 253 | 264 | <section aria-labelledby="health"><h2 id="health">Health</h2>${health(p.health ?? [])}</section> |
| 254 | 265 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 255 | 266 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${inForce("mute", p.muted ?? [], canLift)}${inForce("ban", p.banned ?? [], canLift)}${actions}</section> |
| 267 | <section aria-labelledby="reports"><h2 id="reports">Reports</h2>${reportList(p.reports ?? [])}</section> | |
| 256 | 268 | <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section> |
| 257 | 269 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 258 | 270 | p.canEdit, |
Software/so101/src/admin-restart.test.ts +58
| @@ -78,3 +78,61 @@ test("/admin lists moderation actions newest first, escaped, and a plain user ge | ||
| 78 | 78 | |
| 79 | 79 | expect((await fetch(`${base}/admin`, { headers: as("bob", "user") })).status).not.toBe(200); |
| 80 | 80 | }); |
| 81 | ||
| 82 | test("a report reaches /admin; Remove, Mute and Dismiss close it, are logged, and a plain user's POST is refused", async () => { | |
| 83 | const owner = as("trevato", "admin"); | |
| 84 | const base = await boot(); | |
| 85 | const carol = { id: "u-carol", name: "carol", owner: false }; | |
| 86 | const lines = ["<b>rude</b>", "spam", "meh"].map((t, i) => lobby!.chat.post({ ...carol, id: `u-c${i}` }, t)); | |
| 87 | const [a, b, c] = lines.map((l) => l.id); | |
| 88 | lines.forEach((l, i) => lobby!.lineReports.add({ id: String(l.id), text: l.text, author: "carol", authorId: `u-c${i}`, reporter: `r${i}`, at: 1000 + i })); | |
| 89 | ||
| 90 | const html = await (await fetch(`${base}/admin`, { headers: owner })).text(); | |
| 91 | const section = html.slice(html.indexOf('id="reports"'), html.indexOf('id="removed"')); | |
| 92 | expect(section).not.toContain("<b>rude"); | |
| 93 | expect(section).toContain("<b>rude"); | |
| 94 | expect(section.indexOf("meh")).toBeLessThan(section.indexOf("spam")); | |
| 95 | expect(section.match(/value="report-remove"/g)?.length).toBe(3); | |
| 96 | ||
| 97 | const post = (headers: Record<string, string>, action: string, id: number) => fetch(`${base}/admin`, form(headers, { action, id: String(id) })); | |
| 98 | expect((await post(as("bob", "user"), "report-remove", a!)).status).toBe(404); | |
| 99 | expect(lobby!.chat.messages.some((m) => m.id === a)).toBe(true); | |
| 100 | expect((await fetch(`${base}/admin`, { ...form(owner, { action: "report-remove", id: String(a) }), headers: { ...owner, origin: "https://evil.example", "content-type": "application/x-www-form-urlencoded" } })).status).toBe(403); | |
| 101 | expect((await fetch(`${base}/admin`, { ...form(owner, { action: "report-remove", id: String(a) }), headers: { ...owner, "sec-fetch-site": "cross-site", "content-type": "application/x-www-form-urlencoded" } })).status).toBe(403); | |
| 102 | ||
| 103 | expect((await post(owner, "report-remove", a!)).status).toBe(303); | |
| 104 | expect(lobby!.chat.messages.some((m) => m.id === a)).toBe(false); | |
| 105 | expect((await post(owner, "report-dismiss", b!)).status).toBe(303); | |
| 106 | expect(lobby!.chat.messages.some((m) => m.id === b)).toBe(true); | |
| 107 | expect((await post(owner, "report-mute", c!)).status).toBe(303); | |
| 108 | expect(lobby!.chat.muted.has("u-c2")).toBe(true); | |
| 109 | expect(lobby!.lineReports.list()).toEqual([]); | |
| 110 | expect((await post(owner, "report-remove", a!)).status).toBe(400); | |
| 111 | const log = lobby!.admin.log.map((e) => `${e.by.name} ${e.what}`); | |
| 112 | expect(log).toContain(`trevato deleted chat line ${a}`); | |
| 113 | expect(log).toContain(`trevato dismissed the reports of chat line ${b}`); | |
| 114 | expect(log).toContain("trevato muted carol"); | |
| 115 | }); | |
| 116 | ||
| 117 | test("a verified moderator may act on reports but not on settings", async () => { | |
| 118 | const base = await boot(); | |
| 119 | lobby!.configure({ id: "id-trevato", name: "trevato", owner: true }, { moderators: ["mod"] }); | |
| 120 | const carol = { id: "u-carol", name: "carol", owner: false }; | |
| 121 | const line = lobby!.chat.post(carol, "rude"); | |
| 122 | lobby!.lineReports.add({ id: String(line.id), text: "rude", author: "carol", authorId: "u-carol", reporter: "r", at: 1 }); | |
| 123 | const mod = as("mod", "user"); | |
| 124 | expect((await fetch(`${base}/admin`, form(mod, { action: "pause" }))).status).toBe(403); | |
| 125 | expect((await fetch(`${base}/admin`, form(mod, { action: "report-remove", id: String(line.id) }))).status).toBe(303); | |
| 126 | expect(lobby!.chat.messages.some((m) => m.id === line.id)).toBe(false); | |
| 127 | }); | |
| 128 | ||
| 129 | test("a moderator's Mute on a report whose author is the owner or another moderator is refused and the report stays open", async () => { | |
| 130 | const base = await boot(); | |
| 131 | lobby!.configure({ id: "id-trevato", name: "trevato", owner: true }, { moderators: ["mod", "mod2"] }); | |
| 132 | const mod = as("mod", "user"); | |
| 133 | const lines = [{ id: "id-trevato", name: "trevato", owner: true }, { id: "id-mod2", name: "mod2", owner: false }].map((from) => lobby!.chat.post(from, "hi")); | |
| 134 | lines.forEach((l) => lobby!.lineReports.add({ id: String(l.id), text: "hi", author: l.from!.name, authorId: l.from!.id, reporter: "r", at: 1 })); | |
| 135 | for (const l of lines) expect((await fetch(`${base}/admin`, form(mod, { action: "report-mute", id: String(l.id) }))).status).toBe(400); | |
| 136 | expect(lobby!.chat.muted.has("id-trevato") || lobby!.chat.muted.has("id-mod2")).toBe(false); | |
| 137 | expect(lobby!.lineReports.list().length).toBe(2); | |
| 138 | }); |
Software/so101/src/admin-routes.ts +20 −5
| @@ -27,8 +27,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 27 | 27 | res.end(text); |
| 28 | 28 | }; |
| 29 | 29 | const canEdit = Boolean(who?.owner); |
| 30 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them | |
| 31 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) { | |
| 30 | if (!who || !lobby.moderates(req)) { | |
| 32 | 31 | // the page is no secret to a moderator, but to anyone else it does not exist: the answer is the ordinary not-found one |
| 33 | 32 | res.writeHead(404, { "content-type": "application/json", "cache-control": "no-store" }); |
| 34 | 33 | res.end(JSON.stringify({ error: "no such route" })); |
| @@ -45,7 +44,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 45 | 44 | return null; |
| 46 | 45 | } |
| 47 | 46 | const show = (status: number, done: Done | null, error: string | null) => |
| 48 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), done, error }), canEdit); | |
| 47 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), reports: lobby.lineReports.list(), done, error }), canEdit); | |
| 49 | 48 | if (req.method === "GET" || req.method === "HEAD") { |
| 50 | 49 | const done = url.searchParams.get("done"); |
| 51 | 50 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
| @@ -57,6 +56,9 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 57 | 56 | /** The largest form the page may post. */ |
| 58 | 57 | const MAX_FORM = 16_384; |
| 59 | 58 | |
| 59 | /** The actions a verified moderator may post; everything else on the page is the owner's. */ | |
| 60 | const REPORT_ACTIONS = new Set(["report-remove", "report-mute", "report-dismiss"]); | |
| 61 | ||
| 60 | 62 | /** Reads a request body, refusing one over the limit. */ |
| 61 | 63 | type ReadBody = (req: IncomingMessage, limit?: number) => Promise<Buffer>; |
| 62 | 64 | |
| @@ -70,7 +72,7 @@ export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: Serve | ||
| 70 | 72 | res.end(JSON.stringify({ error: "GET or POST" })); |
| 71 | 73 | return; |
| 72 | 74 | } |
| 73 | if (!canEdit) return session.refuse(403); | |
| 75 | if (!sameOrigin(req)) return session.refuse(403); | |
| 74 | 76 | let body: Buffer; |
| 75 | 77 | try { |
| 76 | 78 | body = await readBody(req, MAX_FORM); |
| @@ -79,6 +81,8 @@ export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: Serve | ||
| 79 | 81 | return show(413, null, "that form is too large"); |
| 80 | 82 | } |
| 81 | 83 | const form = new URLSearchParams(body.toString("utf8")); |
| 84 | const act = form.get("action") ?? ""; | |
| 85 | if (!canEdit && !REPORT_ACTIONS.has(act)) return session.refuse(403); | |
| 82 | 86 | let done: Done; |
| 83 | 87 | try { |
| 84 | 88 | switch (form.get("action")) { |
| @@ -113,6 +117,17 @@ export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: Serve | ||
| 113 | 117 | done = "restored"; |
| 114 | 118 | break; |
| 115 | 119 | } |
| 120 | case "report-remove": | |
| 121 | case "report-mute": | |
| 122 | case "report-dismiss": { | |
| 123 | const line = form.get("id") ?? ""; | |
| 124 | if (!/^[0-9]{1,15}$/.test(line)) throw new AdminError("that report is closed"); | |
| 125 | if (act === "report-remove") lobby.removeReported(who, line); | |
| 126 | else if (act === "report-mute") lobby.muteReported(who, line); | |
| 127 | else lobby.dismissReported(who, line); | |
| 128 | done = act === "report-remove" ? "reportRemoved" : act === "report-mute" ? "reportMuted" : "reportDismissed"; | |
| 129 | break; | |
| 130 | } | |
| 116 | 131 | default: |
| 117 | 132 | throw new AdminError("no such action"); |
| 118 | 133 | } |
| @@ -144,7 +159,7 @@ export async function handleLift(lobby: Lobby, req: IncomingMessage, res: Server | ||
| 144 | 159 | } |
| 145 | 160 | if (!sameOrigin(req)) return refuse(res, 403, "only this page may lift"); |
| 146 | 161 | const who = lobby.who(req); |
| 147 | if (!who || !(who.owner || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) return refuse(res, 403, "only the owner or a moderator may lift"); | |
| 162 | if (!who || !lobby.moderates(req)) return refuse(res, 403, "only the owner or a moderator may lift"); | |
| 148 | 163 | let body: Buffer; |
| 149 | 164 | try { |
| 150 | 165 | body = await readBody(req, MAX_FORM); |
Software/so101/src/lobby.ts +69 −9
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | * {"type": "hosts", "hosts": ["…"]} owner: hostnames chat may link to; any other link is refused |
| 23 | 23 | * {"type": "ban", "id": "<user id>", "on": true|false} owner or a moderator (never the owner or a moderator): bars their account, name and address; they still watch |
| 24 | 24 | * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused |
| 25 | * {"type": "dismissReport", "id": 12} owner or moderator: close a chat line's reports and nothing else | |
| 25 | 26 | * {"type": "report", "id": 12} signed in, not banned: report a chat line; answered to you alone with {"type": "reported", "id"}, a repeat too |
| 26 | 27 | * {"type": "report", …} anyone: the page's own timings (src/viewer-reports.ts), every 5 s (no "id") |
| 27 | 28 | * {"type": "pong"} the answer to {"type": "ping"} |
| @@ -178,6 +179,17 @@ export class Lobby { | ||
| 178 | 179 | return this.local ? LOCAL : identify(req.headers, this.key); |
| 179 | 180 | } |
| 180 | 181 | |
| 182 | /** The one moderator test: the owner, or a listed moderator who holds that forge identity (a guest who types the same name is not them). */ | |
| 183 | mayModerate(who: Person | null, verified: boolean): boolean { | |
| 184 | return Boolean(who && (who.owner || (verified && this.admin.isModerator(who.name)))); | |
| 185 | } | |
| 186 | ||
| 187 | /** Whether the caller of this request may moderate. */ | |
| 188 | moderates(req: IncomingMessage): boolean { | |
| 189 | const who = this.who(req); | |
| 190 | return this.mayModerate(who, this.verified(req)?.id === who?.id); | |
| 191 | } | |
| 192 | ||
| 181 | 193 | /** Whether this person, or anyone where they are, is banned. The owner never is. */ |
| 182 | 194 | barred(who: Person, from: Visitor | null): boolean { |
| 183 | 195 | return !who.owner && !this.local && this.accounts.banned(who.id, who.name, from?.ip ?? null); |
| @@ -373,6 +385,15 @@ export class Lobby { | ||
| 373 | 385 | return; |
| 374 | 386 | } |
| 375 | 387 | } |
| 388 | if (m.type === "dismissReport" && this.mayModerate(who, seat.verified)) { | |
| 389 | try { | |
| 390 | this.dismissReported(who, String(m.id)); | |
| 391 | } catch (err) { | |
| 392 | if (!(err instanceof AdminError)) throw err; | |
| 393 | this.send(seat, { type: "error", error: err.message }); | |
| 394 | } | |
| 395 | return; | |
| 396 | } | |
| 376 | 397 | if (!who.owner && seat.verified && this.admin.isModerator(who.name)) { |
| 377 | 398 | if (m.type === "delete") return this.deleteLine(who, m.id); |
| 378 | 399 | if (m.type === "mute") return this.muteUser(seat, who, m); |
| @@ -466,29 +487,67 @@ export class Lobby { | ||
| 466 | 487 | private muteUser(seat: Seat, who: Person, m: Record<string, unknown>): void { |
| 467 | 488 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
| 468 | 489 | if (m.for !== undefined && (typeof m.for !== "string" || !Object.hasOwn(MUTE_FOR, m.for))) return this.send(seat, { type: "error", error: "mute for 10m, 1h, 24h or forever" }); |
| 469 | const id = m.id; | |
| 490 | const refused = this.applyMute(who, m.id, m.on, MUTE_FOR[(m.for as string | undefined) ?? "forever"] ?? null); | |
| 491 | if (refused) this.send(seat, { type: "error", error: refused }); | |
| 492 | } | |
| 493 | ||
| 494 | /** Mute or unmute for `span` ms (null: for good); the refusal's words when `who` may not. */ | |
| 495 | private applyMute(who: Person, id: string, on: boolean, span: number | null): string | null { | |
| 470 | 496 | const there = [...this.seats].find((s) => s.who?.id === id)?.who; |
| 471 | 497 | const name = there?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.chat.muted.get(id) ?? id; |
| 472 | 498 | // A moderator fails closed: the target must be positively known as a plain user (here now and neither owner nor listed |
| 473 | 499 | // moderator), or be on the muted list already (to lift it). Anyone who has not joined cannot be told from the owner. |
| 474 | if (!who.owner && !(there ? !there.owner && !this.admin.isModerator(there.name) : !m.on && this.chat.muted.has(id) && !this.admin.isModerator(name))) { | |
| 475 | return this.send(seat, { type: "error", error: "only the owner can mute the owner, a moderator or anyone who is not here" }); | |
| 500 | if (!who.owner && !(there ? !there.owner && !this.admin.isModerator(there.name) : !on && this.chat.muted.has(id) && !this.admin.isModerator(name))) { | |
| 501 | return "only the owner can mute the owner, a moderator or anyone who is not here"; | |
| 476 | 502 | } |
| 477 | const span = MUTE_FOR[(m.for as string | undefined) ?? "forever"] ?? null; | |
| 478 | if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span); | |
| 479 | else this.chat.unmute(id); | |
| 480 | this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`); | |
| 503 | if (on) { | |
| 504 | this.chat.mute(id, name, span === null ? null : this.now() + span); | |
| 505 | if (this.lineReports.closeBy(id)) for (const s of this.seats) if (this.mayHearMuted(s)) this.send(s, this.reportsList()); | |
| 506 | } else this.chat.unmute(id); | |
| 507 | this.admin.record(who, `${on ? "muted" : "unmuted"} ${name}`); | |
| 481 | 508 | this.broadcastAdmin(); |
| 482 | 509 | for (const s of this.seats) { |
| 483 | 510 | if (s.who?.id === id) this.send(s, this.hello(s)); |
| 484 | 511 | if (this.mayHearMuted(s)) this.send(s, this.mutedList()); |
| 485 | 512 | } |
| 486 | 513 | this.moderator?.muted(id); |
| 514 | return null; | |
| 515 | } | |
| 516 | ||
| 517 | /** Close a line's reports and tell the owner and moderators. */ | |
| 518 | private closeReports(line: string): void { | |
| 519 | if (!this.lineReports.close(line)) return; | |
| 520 | for (const s of this.seats) if (this.mayHearMuted(s)) this.send(s, this.reportsList()); | |
| 521 | } | |
| 522 | ||
| 523 | /** From /admin: take the reported line down for everyone and close its reports. Throws AdminError when no report is open. */ | |
| 524 | removeReported(who: Person, line: string): void { | |
| 525 | if (!this.lineReports.find(line)) throw new AdminError("that report is closed"); | |
| 526 | this.deleteLine(who, Number(line)); | |
| 527 | this.closeReports(line); | |
| 528 | } | |
| 529 | ||
| 530 | /** From /admin: mute the reported line's author for good and close its reports. Throws AdminError when it may not. */ | |
| 531 | muteReported(who: Person, line: string): void { | |
| 532 | const report = this.lineReports.find(line); | |
| 533 | if (!report) throw new AdminError("that report is closed"); | |
| 534 | if (!report.authorId) throw new AdminError("this report predates author ids: mute from the chat"); | |
| 535 | const refused = this.applyMute(who, report.authorId, true, null); | |
| 536 | if (refused) throw new AdminError(refused); | |
| 537 | this.closeReports(line); | |
| 538 | } | |
| 539 | ||
| 540 | /** From /admin or the terminal: close a line's reports and nothing else. */ | |
| 541 | dismissReported(who: Person, line: string): void { | |
| 542 | if (!this.lineReports.find(line)) throw new AdminError("that report is closed"); | |
| 543 | this.admin.record(who, `dismissed the reports of chat line ${line}`); | |
| 544 | this.closeReports(line); | |
| 545 | this.broadcastAdmin(); | |
| 487 | 546 | } |
| 488 | 547 | |
| 489 | 548 | /** The owner and a verified moderator are sent the muted list. */ |
| 490 | 549 | private mayHearMuted(s: Seat): boolean { |
| 491 | return Boolean(s.who && (s.who.owner || (s.verified && this.admin.isModerator(s.who.name)))); | |
| 550 | return this.mayModerate(s.who, s.verified); | |
| 492 | 551 | } |
| 493 | 552 | |
| 494 | 553 | /** A signed-in, unbanned user reports a chat line by id; only they hear it was taken, and the owner and moderators get the list. */ |
| @@ -498,7 +557,7 @@ export class Lobby { | ||
| 498 | 557 | if (this.barred(who, seat.from)) return this.send(seat, { type: "error", error: "you are banned: you can still watch" }); |
| 499 | 558 | const line = Number.isSafeInteger(id) ? this.chat.messages.find((x) => x.id === id) : undefined; |
| 500 | 559 | if (!line?.from) return this.send(seat, { type: "error", error: "no such line to report" }); |
| 501 | const added = this.lineReports.add({ id: String(line.id), text: line.text, author: line.from.name, reporter: who.id, at: this.now() }); | |
| 560 | const added = this.lineReports.add({ id: String(line.id), text: line.text, author: line.from.name, authorId: line.from.id, reporter: who.id, at: this.now() }); | |
| 502 | 561 | this.send(seat, { type: "reported", id: line.id }); |
| 503 | 562 | if (added) for (const s of this.seats) if (this.mayHearMuted(s)) this.send(s, this.reportsList()); |
| 504 | 563 | } |
| @@ -512,6 +571,7 @@ export class Lobby { | ||
| 512 | 571 | if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?"); |
| 513 | 572 | if (!this.chat.delete(id as number, who)) return; |
| 514 | 573 | this.admin.record(who, `deleted chat line ${id}`); |
| 574 | this.closeReports(String(id)); | |
| 515 | 575 | this.broadcast({ type: "unsaid", id }); |
| 516 | 576 | this.broadcastAdmin(); |
| 517 | 577 | } |
Software/so101/src/reports.ts +34 −3
| @@ -11,6 +11,8 @@ export interface Report { | ||
| 11 | 11 | id: string; |
| 12 | 12 | text: string; |
| 13 | 13 | author: string; |
| 14 | /** Who wrote the line; absent in reports saved before it was kept. */ | |
| 15 | authorId?: string; | |
| 14 | 16 | reporter: string; |
| 15 | 17 | at: number; |
| 16 | 18 | } |
| @@ -19,7 +21,7 @@ function valid(r: unknown): r is Report { | ||
| 19 | 21 | if (!r || typeof r !== "object") return false; |
| 20 | 22 | const o = r as Record<string, unknown>; |
| 21 | 23 | return typeof o.id === "string" && typeof o.text === "string" && typeof o.author === "string" |
| 22 | && typeof o.reporter === "string" && typeof o.at === "number" && Number.isFinite(o.at); | |
| 24 | && (o.authorId === undefined || typeof o.authorId === "string") && typeof o.reporter === "string" && typeof o.at === "number" && Number.isFinite(o.at); | |
| 23 | 25 | } |
| 24 | 26 | |
| 25 | 27 | export class Reports { |
| @@ -41,7 +43,7 @@ export class Reports { | ||
| 41 | 43 | try { |
| 42 | 44 | const data: unknown = JSON.parse(raw); |
| 43 | 45 | if (!Array.isArray(data) || !data.every(valid)) throw new Error("not a list of reports"); |
| 44 | this.items = data.map(({ id, text, author, reporter, at }) => ({ id, text, author, reporter, at })); | |
| 46 | this.items = data.map((r) => this.copy(r)); | |
| 45 | 47 | } catch { |
| 46 | 48 | console.error(`reports: ${file} is not valid reports JSON; starting empty`); |
| 47 | 49 | } |
| @@ -51,7 +53,7 @@ export class Reports { | ||
| 51 | 53 | add(r: Report): boolean { |
| 52 | 54 | if (!valid(r)) throw new TypeError("invalid report"); |
| 53 | 55 | if (this.items.some((x) => x.reporter === r.reporter && x.id === r.id)) return false; |
| 54 | this.items.push({ id: r.id, text: r.text, author: r.author, reporter: r.reporter, at: r.at }); | |
| 56 | this.items.push(this.copy(r)); | |
| 55 | 57 | const mine = this.items.filter((x) => x.reporter === r.reporter); |
| 56 | 58 | if (mine.length > PER_REPORTER) this.items.splice(this.items.indexOf(mine[0]!), 1); |
| 57 | 59 | if (this.items.length > OVERALL) this.items.shift(); |
| @@ -64,6 +66,35 @@ export class Reports { | ||
| 64 | 66 | return this.items.map((r) => ({ ...r })).reverse(); |
| 65 | 67 | } |
| 66 | 68 | |
| 69 | /** The newest open report of this line, if any. */ | |
| 70 | find(id: string): Report | undefined { | |
| 71 | const r = this.items.findLast((x) => x.id === id); | |
| 72 | return r && { ...r }; | |
| 73 | } | |
| 74 | ||
| 75 | /** Closes every report of this line; false when none was open. */ | |
| 76 | close(id: string): boolean { | |
| 77 | const left = this.items.filter((x) => x.id !== id); | |
| 78 | if (left.length === this.items.length) return false; | |
| 79 | this.items = left; | |
| 80 | this.save(); | |
| 81 | return true; | |
| 82 | } | |
| 83 | ||
| 84 | /** Closes every report of lines this author wrote; false when none was open. */ | |
| 85 | closeBy(authorId: string): boolean { | |
| 86 | const left = this.items.filter((x) => x.authorId !== authorId); | |
| 87 | if (left.length === this.items.length) return false; | |
| 88 | this.items = left; | |
| 89 | this.save(); | |
| 90 | return true; | |
| 91 | } | |
| 92 | ||
| 93 | private copy(r: Report): Report { | |
| 94 | const { id, text, author, authorId, reporter, at } = r; | |
| 95 | return authorId === undefined ? { id, text, author, reporter, at } : { id, text, author, authorId, reporter, at }; | |
| 96 | } | |
| 97 | ||
| 67 | 98 | private save(): void { |
| 68 | 99 | if (!this.file) return; |
| 69 | 100 | const tmp = `${this.file}.tmp`; |
Software/so101/src/terminal.test.ts +26
| @@ -468,6 +468,32 @@ describe("terminal door", () => { | ||
| 468 | 468 | expect(hear(emptyRoom(), { type: "reports", reports: [] })).toEqual([]); |
| 469 | 469 | }); |
| 470 | 470 | |
| 471 | test("dismiss: a moderator's terminal closes a line's reports and the count falls; a plain user is refused; a closed report is an error", async () => { | |
| 472 | const port = await start(); | |
| 473 | lobby!.admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); | |
| 474 | const alicePage = await viewer(port, web("u-alice", "alice")); | |
| 475 | alicePage.send({ type: "post", text: "rude" }); | |
| 476 | const one = await alicePage.heardOne((m) => m.type === "said" && m.message.text === "rude", "the line"); | |
| 477 | const bob = await terminal(port, ttyEnv("u-bob", "bob", "user")); | |
| 478 | const carol = await terminal(port, ttyEnv("u-carol", "carol", "user")); | |
| 479 | await bob.printedLine(/^you are bob \(moderator\)$/); | |
| 480 | expect(typed(emptyRoom(), "/dismiss x")).toEqual({ print: "usage: /dismiss #<line number>" }); | |
| 481 | carol.door.input("/report alice"); | |
| 482 | await carol.printedLine(new RegExp(`^reported #${one.message.id}$`)); | |
| 483 | await bob.printedLine(new RegExp(`^report #${one.message.id} alice: rude`)); | |
| 484 | expect(bob.door.room.reports?.length).toBe(1); | |
| 485 | carol.door.input(`/dismiss #${one.message.id}`); | |
| 486 | await carol.printedLine(/^error: only the owner can do that$/); | |
| 487 | expect(lobby!.lineReports.list().length).toBe(1); | |
| 488 | bob.door.input(`/dismiss #${one.message.id}`); | |
| 489 | await waitFor(() => (bob.door.room.reports?.length === 0 ? true : undefined), "the count falling"); | |
| 490 | expect(lobby!.lineReports.list()).toEqual([]); | |
| 491 | expect(lobby!.chat.messages.some((m) => m.id === one.message.id)).toBe(true); | |
| 492 | expect(lobby!.admin.log.at(-1)).toMatchObject({ by: { name: "bob" }, what: `dismissed the reports of chat line ${one.message.id}` }); | |
| 493 | bob.door.input(`/dismiss #${one.message.id}`); | |
| 494 | await bob.printedLine(/^error: that report is closed$/); | |
| 495 | }); | |
| 496 | ||
| 471 | 497 | test("end-turn: only the owner's terminal may; the page sees the turn pass on", async () => { |
| 472 | 498 | const port = await start(); |
| 473 | 499 | const alicePage = await viewer(port, web("u-alice", "alice")); |
Software/so101/src/terminal.ts +6 −1
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | * /delete <who|#id> owner or moderator: remove that person's latest chat line, or the line numbered #id (shown to those who may) |
| 25 | 25 | * /report <who|#id> any signed-in person: report that person's latest chat line, or the line numbered #id; prints `reported #id` |
| 26 | 26 | * /reports owner or moderator: the reported lines, newest first (they also hear of each new one) |
| 27 | * /dismiss #<id> owner or moderator: close that line's reports and nothing else (Lobby.dismissReported; it is logged) | |
| 27 | 28 | * /removed owner: the latest removed chat lines, with their numbers and who removed them |
| 28 | 29 | * /restore #<n> owner: put a removed line back for everyone (Lobby.restoreLine; it is logged) |
| 29 | 30 | * /table here admin: put the table under the arm's lowest link now (POST /settings/table-here) |
| @@ -174,7 +175,7 @@ const said = (room: Room, m: unknown): string => { | ||
| 174 | 175 | return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 175 | 176 | }; |
| 176 | 177 | |
| 177 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|<mm> (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports (owner, moderator) · /removed /restore #<id> (owner) · /health (owner, moderator) · /machine [off] · /who · /quit"; | |
| 178 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|<mm> (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports /dismiss #<id> (owner, moderator) · /removed /restore #<id> (owner) · /health (owner, moderator) · /machine [off] · /who · /quit"; | |
| 178 | 179 | |
| 179 | 180 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 180 | 181 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -351,6 +352,10 @@ export function typed(room: Room, input: string): Typed { | ||
| 351 | 352 | case "reports": |
| 352 | 353 | if (!room.you?.owner && !room.you?.moderator) return { print: "reports are for the owner and moderators" }; |
| 353 | 354 | return room.reports?.length ? { lines: room.reports.map(reportLine) } : { print: "no reports" }; |
| 355 | case "dismiss": { | |
| 356 | const number = /^#?(\d{1,15})$/.exec(arg); | |
| 357 | return number ? { send: { type: "dismissReport", id: Number(number[1]) } } : { print: "usage: /dismiss #<line number>" }; | |
| 358 | } | |
| 354 | 359 | case "removed": |
| 355 | 360 | return { send: { type: "removed" } }; |
| 356 | 361 | case "restore": { |
new above
offline