Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Any signed-in user can report a chat line over /live; reports are kept under FORGE_APP_DATA, one per person per line #299
smith/arm:smith/242-any-signed-in-user-can-report-a-chat-lin → main 6 commits fork 2 threads · 2 unresolved
12 files
changed +245
−5
Software/README.md +11
| @@ -461,6 +461,14 @@ its live pose and the cameras. Driving needs a name and a turn. | ||
| 461 | 461 | with `/removed` and `/restore #N` in the terminal. All go through |
| 462 | 462 | `Lobby.restoreLine`: the line reappears for every open page and the restore |
| 463 | 463 | is logged (`restored chat line N`). Moderators can see the list, not restore. |
| 464 | - **Reports.** Any signed-in person can report a chat line: the page shows a | |
| 465 | **Report** button on other people's lines (it says **Reported** once the | |
| 466 | server has taken it), `{"type":"report","id":N}` does it over `/live`, and | |
| 467 | `/report` in the terminal. One report per person per line; each keeps the | |
| 468 | line's text and author, so it still reads after the line is deleted. Only the | |
| 469 | owner and moderators receive them, live and under **Reports** on `/admin`. | |
| 470 | They are kept in `$FORGE_APP_DATA/so101/reports.json`: the newest 200, and at | |
| 471 | most 20 from any one person. | |
| 464 | 472 | - The owner (anyone who may write this repository) can take control at |
| 465 | 473 | any time (this skips the queue), end anyone's turn, take people out of |
| 466 | 474 | the queue, delete messages, mute people and ban them (long-press or |
| @@ -517,6 +525,9 @@ watches. A line you type is said in the chat; commands start with `/`: | ||
| 517 | 525 | `resumed` when it is resumed. |
| 518 | 526 | - `/end [name]`, owner: end that turn (no name: whoever drives), or take |
| 519 | 527 | them out of the line. |
| 528 | - `/report <name|#n>`: report that person's latest line, or line `n`, to the | |
| 529 | owner and moderators (signed in; once per line). `/reports`, owner and | |
| 530 | moderators: list the reports. They are also told as each new one comes in. | |
| 520 | 531 | - `/removed`, `/restore #<n>`, owner: list the latest removed chat lines, and put one back for everyone. |
| 521 | 532 | - `/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. |
| 522 | 533 | - `/table here`, owner: the page's "Table is here", the same |
Software/so101/bench/fake-serve.ts +1
| @@ -54,6 +54,7 @@ const lobby = new Lobby({ | ||
| 54 | 54 | adminFile: join(dir, "admin.json"), |
| 55 | 55 | turnsFile: join(dir, "turns.json"), |
| 56 | 56 | wordsFile: join(dir, "words.json"), |
| 57 | reportsFile: join(dir, "reports.json"), | |
| 57 | 58 | accounts: new Accounts({ file: join(dir, "accounts.sqlite") }), |
| 58 | 59 | moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }), |
| 59 | 60 | }); |
Software/so101/src/admin-page.ts +9
| @@ -8,6 +8,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 | 10 | import type { Person, Who } from "./identity.ts"; |
| 11 | import type { Report } from "./reports.ts"; | |
| 11 | 12 | import type { Snapshot } from "./turns.ts"; |
| 12 | 13 | |
| 13 | 14 | export const esc = (text: string): string => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`); |
| @@ -64,6 +65,7 @@ export interface AdminPage { | ||
| 64 | 65 | settings: AdminSettings; |
| 65 | 66 | snapshot: Snapshot; |
| 66 | 67 | log: readonly LogEntry[]; |
| 68 | reports: readonly Report[]; | |
| 67 | 69 | /** Chat lines a person removed, newest first. */ |
| 68 | 70 | removed?: readonly Removed[]; |
| 69 | 71 | done?: Done | null; |
| @@ -102,6 +104,12 @@ const time = (at: number) => { | ||
| 102 | 104 | const entries = (list: readonly LogEntry[], none: string): string => |
| 103 | 105 | list.length ? `<ol>${[...list].reverse().map((e) => `<li>${time(e.at)}<span><b>${esc(e.by.name)}</b> ${esc(e.what)}</span></li>`).join("")}</ol>` : `<p class="empty">${none}</p>`; |
| 104 | 106 | |
| 107 | /** Reported chat lines, newest first: the line as it was, its author, and who reported it. */ | |
| 108 | const reported = (list: readonly Report[]): string => | |
| 109 | list.length | |
| 110 | ? `<ol>${[...list].reverse().map((r) => `<li>${time(r.at)}<span><b>${esc(r.author.name)}</b> ${esc(r.text)} <small>#${r.id}, reported by ${esc(r.reporter.name)}</small></span></li>`).join("")}</ol>` | |
| 111 | : `<p class="empty">Nobody has reported a line.</p>`; | |
| 112 | ||
| 105 | 113 | const field = (name: keyof AdminSettings, label: string, hint: string, value: number, min: number, max: number, off: string) => |
| 106 | 114 | `<label>${label}<small>${hint}</small><input name="${name}" type="number" inputmode="numeric" required min="${min}" max="${max}" step="1" value="${value}"${off}></label>`; |
| 107 | 115 | |
| @@ -157,6 +165,7 @@ ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div> | ||
| 157 | 165 | </section> |
| 158 | 166 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 159 | 167 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section> |
| 168 | <section aria-labelledby="reports"><h2 id="reports">Reports</h2>${reported(p.reports)}</section> | |
| 160 | 169 | <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section> |
| 161 | 170 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 162 | 171 | ); |
Software/so101/src/admin-routes.ts +1 −1
| @@ -34,7 +34,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 34 | 34 | return null; |
| 35 | 35 | } |
| 36 | 36 | const show = (status: number, done: Done | null, error: string | null) => |
| 37 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, done, error })); | |
| 37 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, reports: lobby.reports.list, removed: lobby.chat.removed, done, error })); | |
| 38 | 38 | if (req.method === "GET" || req.method === "HEAD") { |
| 39 | 39 | const done = url.searchParams.get("done"); |
| 40 | 40 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
Software/so101/src/lobby.test.ts +33
| @@ -645,6 +645,39 @@ describe("lobby", () => { | ||
| 645 | 645 | expect((await alice.next((m) => m.type === "said" && m.message.text === "heck")).message.from.name).toBe("trevato"); |
| 646 | 646 | }); |
| 647 | 647 | |
| 648 | test("report: stored once per person and line, shown to the owner, refused for anonymous, banned or missing lines", async () => { | |
| 649 | const chat = new Chat({ now: (() => { let t = 0; return () => (t += 5000); })() }); | |
| 650 | const port = await start(undefined, chat); | |
| 651 | const owner = await join(port, OWNER); | |
| 652 | const alice = await join(port, ALICE); | |
| 653 | const bob = await join(port, BOB); | |
| 654 | const anon = await join(port); | |
| 655 | alice.send({ type: "post", text: "rude" }); | |
| 656 | const line = (await bob.next((m) => m.type === "said")).message; | |
| 657 | expect((await owner.first((m) => m.type === "reports")).reports).toEqual([]); | |
| 658 | ||
| 659 | bob.send({ type: "report", id: line.id }); | |
| 660 | expect(await bob.next((m) => m.type === "reported")).toMatchObject({ id: line.id }); | |
| 661 | const list = await owner.first((m) => m.type === "reports" && m.reports.length === 1); | |
| 662 | expect(list.reports[0]).toMatchObject({ id: line.id, text: "rude", author: { name: "alice" }, reporter: { name: "bob" } }); | |
| 663 | bob.send({ type: "report", id: line.id }); // again: acknowledged, not stored twice | |
| 664 | await bob.next((m) => m.type === "reported"); | |
| 665 | expect(lobby!.reports.list.length).toBe(1); | |
| 666 | expect(alice.heard.some((m) => m.type === "reported" || m.type === "reports")).toBe(false); | |
| 667 | ||
| 668 | anon.send({ type: "report", id: line.id }); | |
| 669 | expect((await anon.next((m) => m.type === "error")).error).toMatch(/join/); | |
| 670 | bob.send({ type: "report", id: 9999 }); | |
| 671 | expect((await bob.next((m) => m.type === "error")).error).toMatch(/no such line/); | |
| 672 | bob.send({ type: "report", id: "1" }); // a non-numeric id is refused like a missing one | |
| 673 | expect((await bob.next((m) => m.type === "error")).error).toMatch(/no such line/); | |
| 674 | ||
| 675 | owner.send({ type: "ban", id: "u-bob", on: true }); | |
| 676 | await bob.next((m) => m.type === "hello" && m.banned); | |
| 677 | bob.send({ type: "report", id: line.id }); | |
| 678 | expect((await bob.next((m) => m.type === "error" && /banned/.test(m.error))).error).toMatch(/banned/); | |
| 679 | }); | |
| 680 | ||
| 648 | 681 | test("ban: the turn ends, chat and queue are refused, watching goes on, and no new name comes from that address", async () => { |
| 649 | 682 | const port = await start(); |
| 650 | 683 | const troll = await enter(port, "troll", "203.0.113.66"); |
Software/so101/src/lobby.ts +25 −1
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | * {"type": "leave"} hand back the turn, or leave the line |
| 12 | 12 | * {"type": "post", "text": "…"} say something |
| 13 | 13 | * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line |
| 14 | * {"type": "report", "id": 12} signed in: flag a chat line; answered "reported", kept in reports.json under FORGE_APP_DATA | |
| 14 | 15 | * {"type": "delete", "id": 12} owner or a moderator: delete a chat line |
| 15 | 16 | * {"type": "mute", "id": "<user id>", "on": true|false, "for": "10m"|"1h"|"24h"|"forever"} owner or a moderator (never the owner or a moderator) |
| 16 | 17 | * {"type": "pause", "on": true|false} owner: maintenance; nobody new gets a turn |
| @@ -38,6 +39,7 @@ import { Admin, AdminError } from "./admin.ts"; | ||
| 38 | 39 | import { frameBytes, TOO_BIG_CLOSE } from "./frame-limit.ts"; |
| 39 | 40 | import { Chat, ChatError } from "./chat.ts"; |
| 40 | 41 | import { Moderator } from "./moderator.ts"; |
| 42 | import { Reports } from "./reports.ts"; | |
| 41 | 43 | import { WordFilter } from "./word-filter.ts"; |
| 42 | 44 | import { identify, LOCAL, type Person, type Who } from "./identity.ts"; |
| 43 | 45 | import { Turns, type TurnEvent } from "./turns.ts"; |
| @@ -56,6 +58,8 @@ export interface LobbyOptions { | ||
| 56 | 58 | accounts?: Accounts; |
| 57 | 59 | /** The chat's file; none keeps it in memory. */ |
| 58 | 60 | chatFile?: string; |
| 61 | /** The reports' file; none keeps them in memory. */ | |
| 62 | reportsFile?: string; | |
| 59 | 63 | /** The word filter's file; none keeps it in memory. */ |
| 60 | 64 | wordsFile?: string; |
| 61 | 65 | /** The admin settings' file; none keeps them in memory. */ |
| @@ -95,6 +99,7 @@ export class Lobby { | ||
| 95 | 99 | readonly accounts: Accounts; |
| 96 | 100 | readonly admin: Admin; |
| 97 | 101 | readonly filter: WordFilter; |
| 102 | readonly reports: Reports; | |
| 98 | 103 | readonly local: boolean; |
| 99 | 104 | readonly moderator: Moderator | undefined; |
| 100 | 105 | private readonly key: string; |
| @@ -119,6 +124,7 @@ export class Lobby { | ||
| 119 | 124 | this.chat = options.chat ?? new Chat({ file: options.chatFile }); |
| 120 | 125 | this.accounts = options.accounts ?? new Accounts(); |
| 121 | 126 | this.filter = new WordFilter(options.wordsFile); |
| 127 | this.reports = new Reports({ file: options.reportsFile, now: this.now }); | |
| 122 | 128 | this.silentMs = options.silentMs ?? 15_000; |
| 123 | 129 | this.moderator = options.moderator; |
| 124 | 130 | if (this.moderator) { |
| @@ -200,7 +206,7 @@ export class Lobby { | ||
| 200 | 206 | } |
| 201 | 207 | this.broadcastState(); |
| 202 | 208 | this.broadcastAdmin(); |
| 203 | if (changed.includes("moderators")) for (const s of this.seats) if (s.who && !s.who.owner) this.send(s, this.hello(s)); | |
| 209 | if (changed.includes("moderators")) for (const s of this.seats) if (s.who && !s.who.owner) (this.send(s, this.hello(s)), this.mayReview(s) && this.send(s, this.reportsList())); | |
| 204 | 210 | if (changed.length) for (const listener of this.changeListeners) listener(); |
| 205 | 211 | } |
| 206 | 212 | |
| @@ -265,6 +271,7 @@ export class Lobby { | ||
| 265 | 271 | this.send(seat, this.hello(seat)); |
| 266 | 272 | this.send(seat, this.state()); |
| 267 | 273 | this.send(seat, this.chatLog()); |
| 274 | if (this.mayReview(seat)) this.send(seat, this.reportsList()); | |
| 268 | 275 | if (this.mayHearMuted(seat)) this.send(seat, this.mutedList()); |
| 269 | 276 | if (who?.owner) { |
| 270 | 277 | this.send(seat, this.bannedList()); |
| @@ -310,6 +317,7 @@ export class Lobby { | ||
| 310 | 317 | if (!who) return this.send(seat, { type: "error", error: "join to take part" }); |
| 311 | 318 | if (m.type === "leave") return void this.turns.leave(who.id); |
| 312 | 319 | if (this.barred(who, seat.from)) return this.send(seat, { type: "error", error: "you are banned: you can still watch" }); |
| 320 | if (m.type === "report") return this.report(seat, who, m.id); | |
| 313 | 321 | switch (m.type) { |
| 314 | 322 | case "join": |
| 315 | 323 | if (this.takeControl(who) === "full") this.send(seat, { type: "error", error: `the line is full (${this.turns.maxQueue}); try again in a little while` }); |
| @@ -403,6 +411,22 @@ export class Lobby { | ||
| 403 | 411 | this.send(seat, { type: "error", error: "unknown request" }); |
| 404 | 412 | } |
| 405 | 413 | |
| 414 | /** Files a report on a chat line for the owners and moderators; only the reporter is told. */ | |
| 415 | private report(seat: Seat, who: Person, id: unknown): void { | |
| 416 | const line = Number.isSafeInteger(id) ? this.chat.messages.find((x) => x.id === id) : undefined; | |
| 417 | if (!line?.from) return this.send(seat, { type: "error", error: "no such line to report" }); | |
| 418 | if (this.reports.add(line, who)) for (const s of this.seats) if (this.mayReview(s)) this.send(s, this.reportsList()); | |
| 419 | this.send(seat, { type: "reported", id: line.id }); | |
| 420 | } | |
| 421 | ||
| 422 | private mayReview(seat: Seat): boolean { | |
| 423 | return Boolean(seat.who && (seat.who.owner || (seat.verified && this.admin.isModerator(seat.who.name)))); | |
| 424 | } | |
| 425 | ||
| 426 | private reportsList() { | |
| 427 | return { type: "reports", reports: this.reports.list }; | |
| 428 | } | |
| 429 | ||
| 406 | 430 | /** Mute or unmute as the owner or a moderator; a moderator may not touch the owner or another moderator. The admin log says who. */ |
| 407 | 431 | private muteUser(seat: Seat, who: Person, m: Record<string, unknown>): void { |
| 408 | 432 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
Software/so101/src/reports.test.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | import { 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 { KEEP, PER_REPORTER, Reports } from "./reports.ts"; | |
| 6 | ||
| 7 | const line = (id: number) => ({ id, at: 1, from: { id: "a", name: "alice" }, text: `line ${id}` }); | |
| 8 | const bob = { id: "b", name: "bob", owner: false }; | |
| 9 | ||
| 10 | test("one report per person per line, capped, and kept across a restart", () => { | |
| 11 | const file = join(mkdtempSync(join(tmpdir(), "so101-reports-")), "reports.json"); | |
| 12 | const reports = new Reports({ file, now: () => 7 }); | |
| 13 | expect(reports.add(line(1), bob)).toBe(true); | |
| 14 | expect(reports.add(line(1), bob)).toBe(false); | |
| 15 | expect(reports.add({ ...line(2), from: null }, bob)).toBe(false); | |
| 16 | for (let r = 0; r < KEEP; r++) reports.add(line(100 + r), { id: `u${r}`, name: `u${r}`, owner: false }); | |
| 17 | expect(reports.list.length).toBe(KEEP); | |
| 18 | reports.add(line(999), bob); | |
| 19 | expect(reports.list.length).toBe(KEEP); | |
| 20 | expect(reports.list.some((r) => r.id === 1)).toBe(false); | |
| 21 | const again = new Reports({ file }); | |
| 22 | expect(again.list.length).toBe(KEEP); | |
| 23 | expect(again.list.at(-1)).toMatchObject({ text: "line 999", author: { name: "alice" }, reporter: { name: "bob" }, at: 7 }); | |
| 24 | }); | |
| 25 | ||
| 26 | test("one person's reports are capped, oldest first, so they cannot push out other people's", () => { | |
| 27 | const reports = new Reports({ now: () => 1 }); | |
| 28 | const carol = { id: "c", name: "carol", owner: false }; | |
| 29 | reports.add(line(1), carol); | |
| 30 | for (let i = 10; i < 10 + PER_REPORTER + 5; i++) reports.add(line(i), bob); | |
| 31 | const mine = reports.list.filter((r) => r.reporter.id === "b"); | |
| 32 | expect(mine.length).toBe(PER_REPORTER); | |
| 33 | expect(mine[0]!.id).toBe(15); | |
| 34 | expect(reports.list.some((r) => r.reporter.id === "c")).toBe(true); | |
| 35 | }); |
Software/so101/src/reports.ts added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | /** | |
| 2 | * Reports: a signed-in person flags a chat line for the owner and the moderators. One report per person per | |
| 3 | * line; each keeps the line's text and author as they were, so it still reads once the line is deleted. | |
| 4 | * The newest `KEEP` are kept, and at most `PER_REPORTER` from one person (their oldest go first), so one | |
| 5 | * person cannot push the rest out. One JSON file, rewritten whole on each change. | |
| 6 | */ | |
| 7 | ||
| 8 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 9 | import { dirname } from "node:path"; | |
| 10 | import type { Message } from "./chat.ts"; | |
| 11 | import type { Person } from "./identity.ts"; | |
| 12 | ||
| 13 | export const KEEP = 200; | |
| 14 | export const PER_REPORTER = 20; | |
| 15 | ||
| 16 | export interface Report { | |
| 17 | /** The chat line's id. */ | |
| 18 | id: number; | |
| 19 | text: string; | |
| 20 | author: { id: string; name: string }; | |
| 21 | reporter: { id: string; name: string }; | |
| 22 | at: number; | |
| 23 | } | |
| 24 | ||
| 25 | export class Reports { | |
| 26 | list: Report[] = []; | |
| 27 | private readonly file: string | undefined; | |
| 28 | private readonly now: () => number; | |
| 29 | ||
| 30 | constructor(options: { file?: string; now?: () => number } = {}) { | |
| 31 | this.file = options.file; | |
| 32 | this.now = options.now ?? Date.now; | |
| 33 | if (this.file) this.load(this.file); | |
| 34 | } | |
| 35 | ||
| 36 | /** Files a report; false when this person already reported this line. */ | |
| 37 | add(message: Message, reporter: Person): boolean { | |
| 38 | if (!message.from) return false; | |
| 39 | if (this.list.some((r) => r.id === message.id && r.reporter.id === reporter.id)) return false; | |
| 40 | this.list.push({ id: message.id, text: message.text, author: { ...message.from }, reporter: { id: reporter.id, name: reporter.name }, at: this.now() }); | |
| 41 | const mine = this.list.filter((r) => r.reporter.id === reporter.id); | |
| 42 | if (mine.length > PER_REPORTER) this.list.splice(this.list.indexOf(mine[0]!), 1); | |
| 43 | if (this.list.length > KEEP) this.list.splice(0, this.list.length - KEEP); | |
| 44 | this.save(); | |
| 45 | return true; | |
| 46 | } | |
| 47 | ||
| 48 | private load(file: string): void { | |
| 49 | let stored: unknown; | |
| 50 | try { | |
| 51 | stored = JSON.parse(readFileSync(file, "utf8")); | |
| 52 | } catch { | |
| 53 | return; | |
| 54 | } | |
| 55 | if (!Array.isArray(stored)) return; | |
| 56 | const person = (p: any) => typeof p?.id === "string" && typeof p.name === "string"; | |
| 57 | this.list = stored.filter((r) => typeof r?.id === "number" && typeof r.text === "string" && typeof r.at === "number" && person(r.author) && person(r.reporter)).slice(-KEEP); | |
| 58 | } | |
| 59 | ||
| 60 | private save(): void { | |
| 61 | if (!this.file) return; | |
| 62 | mkdirSync(dirname(this.file), { recursive: true }); | |
| 63 | writeFileSync(`${this.file}.tmp`, JSON.stringify(this.list)); | |
| 64 | renameSync(`${this.file}.tmp`, this.file); | |
| 65 | } | |
| 66 | } |
Software/so101/src/terminal.test.ts +25
| @@ -89,6 +89,12 @@ describe("terminal lines", () => { | ||
| 89 | 89 | expect(typed(room, "/delete #12")).toEqual({ send: { type: "delete", id: 12 } }); |
| 90 | 90 | expect(typed(room, "/delete")).toEqual({ print: "usage: /delete <name> | /delete #<line number>" }); |
| 91 | 91 | expect(typed(room, "/delete bob")).toEqual({ print: "error: no line from bob seen" }); |
| 92 | expect(typed(room, "/report #12")).toEqual({ send: { type: "report", id: 12 } }); | |
| 93 | expect(typed(room, "/report Alice")).toEqual({ send: { type: "report", id: 7 } }); | |
| 94 | expect(typed(room, "/report")).toEqual({ print: "usage: /report <name> | /report #<line number>" }); | |
| 95 | expect(typed(room, "/report bob")).toEqual({ print: "error: no line from bob seen" }); | |
| 96 | expect(typed(room, "/reports")).toEqual({ print: "only the owner and moderators can list reports" }); | |
| 97 | expect(hear(room, { type: "reported", id: 7 })).toEqual(["reported #7: the owner and moderators will see it"]); | |
| 92 | 98 | expect(hear(emptyRoom(), { type: "said", message: { id: 7, from: { id: "u-alice", name: "alice" }, text: "hi" } })).toEqual(["alice: hi"]); |
| 93 | 99 | const mod = emptyRoom(); |
| 94 | 100 | hear(mod, { type: "hello", you: { id: "u-bob", name: "bob" }, moderator: true }); |
| @@ -368,6 +374,25 @@ describe("terminal door", () => { | ||
| 368 | 374 | expect(lobby!.admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${one.message.id}` }); |
| 369 | 375 | }); |
| 370 | 376 | |
| 377 | test("report: a plain user's terminal reports a line; the owner's terminal is told and can list it", async () => { | |
| 378 | const port = await start(); | |
| 379 | const page = await viewer(port, web("u-alice", "alice")); | |
| 380 | page.send({ type: "post", text: "rude" }); | |
| 381 | const one = await page.heardOne((m) => m.type === "said" && m.message.text === "rude", "the line"); | |
| 382 | const owner = await terminal(port, ttyEnv("u-owner", "trevato", "admin")); | |
| 383 | const carol = await terminal(port, ttyEnv("u-carol", "carol", "user")); | |
| 384 | await carol.printedLine(/^alice: rude$/); | |
| 385 | await owner.printedLine(new RegExp(`^#${one.message.id} alice: rude$`)); // the owner sees the line, with its number | |
| 386 | carol.door.input("/report alice"); | |
| 387 | await carol.printedLine(new RegExp(`^reported #${one.message.id}: `)); | |
| 388 | await owner.printedLine(new RegExp(`^report: #${one.message.id} alice: rude \\(reported by carol\\)$`)); | |
| 389 | owner.door.input("/reports"); | |
| 390 | await owner.printedLine(new RegExp(`^#${one.message.id} alice: rude \\(reported by carol\\)$`)); | |
| 391 | carol.door.input("/reports"); | |
| 392 | await carol.printedLine(/^only the owner and moderators can list reports$/); | |
| 393 | expect(lobby!.reports.list).toHaveLength(1); | |
| 394 | }); | |
| 395 | ||
| 371 | 396 | test("removed and restore: the owner's terminal lists removals and restores one for web viewers; anyone else's gets nothing", async () => { |
| 372 | 397 | const port = await start(); |
| 373 | 398 | const page = await viewer(port); |
Software/so101/src/terminal.ts +27 −2
| @@ -111,11 +111,13 @@ export interface Room { | ||
| 111 | 111 | told: { called: boolean; next: boolean }; |
| 112 | 112 | /** The latest chat line of each author, by user id: what `/delete <who>` removes. */ |
| 113 | 113 | latest: Map<string, number>; |
| 114 | /** The reports the server sent, for those who may review them; null until the first list. */ | |
| 115 | reports: Array<{ id: number; text: string; author: string; reporter: string }> | null; | |
| 114 | 116 | /** Everyone seen by name, most recent last: to turn a typed name into an id. */ |
| 115 | 117 | seen: Map<string, Someone>; |
| 116 | 118 | } |
| 117 | 119 | |
| 118 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map() }); | |
| 120 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map(), reports: null }); | |
| 119 | 121 | |
| 120 | 122 | const someone = (x: unknown): Someone | null => { |
| 121 | 123 | if (typeof x !== "object" || x === null) return null; |
| @@ -139,7 +141,9 @@ const said = (room: Room, m: unknown): string => { | ||
| 139 | 141 | return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 140 | 142 | }; |
| 141 | 143 | |
| 142 | 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) · /removed /restore #<id> (owner) · /machine [off] · /who · /quit"; | |
| 144 | const reportLine = (r: { id: number; text: string; author: string; reporter: string }): string => plain(`#${r.id} ${r.author}: ${r.text} (reported by ${r.reporter})`); | |
| 145 | ||
| 146 | 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: the list) · /removed /restore #<id> (owner) · /machine [off] · /who · /quit"; | |
| 143 | 147 | |
| 144 | 148 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 145 | 149 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -228,6 +232,18 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 228 | 232 | if (now === was || (was === undefined && now === null)) return []; |
| 229 | 233 | return [now ?? "resumed: the arm may move again, from a new command"]; |
| 230 | 234 | } |
| 235 | case "reported": | |
| 236 | return [`reported${typeof frame.id === "number" ? ` #${frame.id}` : ""}: the owner and moderators will see it`]; | |
| 237 | case "reports": { | |
| 238 | const list = Array.isArray(frame.reports) ? frame.reports : []; | |
| 239 | const rows = list.map((r) => r as Record<string, unknown>).filter((r) => typeof r.id === "number" && typeof r.text === "string").map((r) => ({ id: r.id as number, text: r.text as string, author: String(someone(r.author)?.name ?? "?"), reporter: String(someone(r.reporter)?.name ?? "?") })); | |
| 240 | const was = room.reports; | |
| 241 | room.reports = rows; | |
| 242 | if (was === null) return rows.length ? [`${rows.length} report${rows.length === 1 ? "" : "s"} to review: /reports`] : []; | |
| 243 | const last = rows.at(-1); | |
| 244 | const key = (r?: { id: number; reporter: string }) => (r ? `${r.id}:${r.reporter}` : ""); | |
| 245 | return last && key(last) !== key(was.at(-1)) ? [`report: ${reportLine(last)}`] : []; | |
| 246 | } | |
| 231 | 247 | case "unsaid": |
| 232 | 248 | return ["· a line was removed"]; |
| 233 | 249 | case "removed": { |
| @@ -290,6 +306,15 @@ export function typed(room: Room, input: string): Typed { | ||
| 290 | 306 | const id = number ? Number(number[1]) : room.latest.get(target(room, arg) ?? ""); |
| 291 | 307 | return id === undefined ? { print: `error: no line from ${plain(arg)} seen` } : { send: { type: "delete", id } }; |
| 292 | 308 | } |
| 309 | case "report": { | |
| 310 | if (!arg) return { print: "usage: /report <name> | /report #<line number>" }; | |
| 311 | const number = /^#(\d{1,15})$/.exec(arg); | |
| 312 | const id = number ? Number(number[1]) : room.latest.get(target(room, arg) ?? ""); | |
| 313 | return id === undefined ? { print: `error: no line from ${plain(arg)} seen` } : { send: { type: "report", id } }; | |
| 314 | } | |
| 315 | case "reports": | |
| 316 | if (!room.you?.owner && !room.you?.moderator) return { print: "only the owner and moderators can list reports" }; | |
| 317 | return { print: room.reports?.length ? room.reports.map(reportLine).join("\n") : "no reports" }; | |
| 293 | 318 | case "removed": |
| 294 | 319 | return { send: { type: "removed" } }; |
| 295 | 320 | case "restore": { |
Software/so101/src/ui/lobby-panel.js +9
| @@ -172,6 +172,11 @@ export function createRoom(page) { | ||
| 172 | 172 | button.disabled = false; |
| 173 | 173 | }; |
| 174 | 174 | const chatBox = $("chat"); |
| 175 | /** The server took the report: only now does the button say so (a refusal leaves it as it was). */ | |
| 176 | function markReported(/** @type {unknown} */ id) { | |
| 177 | const b = /** @type {any} */ (chatBox.querySelector(`[data-id="${Number(id)}"] .report`)); | |
| 178 | if (b) { b.disabled = true; b.textContent = "Reported"; b.ariaLabel = `Reported ${b.closest(".line")?.dataset.name ?? ""}`; } | |
| 179 | } | |
| 175 | 180 | /** @param {any} m */ |
| 176 | 181 | function chatLine(m) { |
| 177 | 182 | const me = lobby.hello?.you; |
| @@ -180,6 +185,9 @@ export function createRoom(page) { | ||
| 180 | 185 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); } |
| 181 | 186 | line.append(el("span", { textContent: m.text })); |
| 182 | 187 | if (m.from && canDelete()) line.tabIndex = 0; |
| 188 | if (m.from && me && m.from.id !== me.id) { | |
| 189 | line.append(el("button", { className: "report", textContent: "Report", ariaLabel: `Report ${m.from.name}`, ariaLive: "polite", onclick: () => say({ type: "report", id: m.id }) })); | |
| 190 | } | |
| 183 | 191 | return line; |
| 184 | 192 | } |
| 185 | 193 | // A new page gets the last lines of the chat; older ones come a page at a time, when asked for. |
| @@ -256,6 +264,7 @@ export function createRoom(page) { | ||
| 256 | 264 | else if (m.type === "chat") return addChat(m.messages, true), showEarlier(m.more); |
| 257 | 265 | else if (m.type === "history") return addOlder(m.messages, m.more); |
| 258 | 266 | else if (m.type === "said") return addChat([m.message]); |
| 267 | else if (m.type === "reported") return markReported(m.id); | |
| 259 | 268 | else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove(); |
| 260 | 269 | else if (m.type === "muted") lobby.mutedList = m.muted; |
| 261 | 270 | else if (m.type === "banned") lobby.bannedList = m.banned; |
Software/so101/src/viewer.html +3 −1
| @@ -135,8 +135,10 @@ | ||
| 135 | 135 | /* chat */ |
| 136 | 136 | #tab-chat { padding: 0; gap: 0; } |
| 137 | 137 | #chat { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; padding: 10px calc(12px + var()) 10px calc(12px + var()); display: flex; flex-direction: column; gap: 4px; overflow-wrap: anywhere; } |
| 138 | #chat .line { line-height: 1.45; padding: 4px 8px; margin: 0 -8px; border-radius: 8px; } | |
| 138 | #chat .line { position: relative; line-height: 1.45; padding: 4px 8px; margin: 0 -8px; border-radius: 8px; } | |
| 139 | 139 | #chat .line b { color: var(); font-weight: 650; margin-right: 6px; } #chat .line.me b { color: var(); } |
| 140 | #chat .line:has(.report) { padding-right: 60px; } | |
| 141 | #chat .report { position: absolute; top: 50%; right: 0; transform: translateY(-50%); min-width: 56px; min-height: 44px; font-size: 12px; background: none; border: 0; } | |
| 140 | 142 | #chat .line.held { background: var(); } |
| 141 | 143 | #chat.moderated .line:not(.sys) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: context-menu; } |
| 142 | 144 | #chat .sys { color: var(); font-style: italic; } |
new above
offline
Reports are acknowledged and stored, but no terminal or page code consumes them (see the review body).