Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/live accepts a report from a signed-in, unbanned user for a line that exists, acks only the reporter, and sends owners and moderators the reports list #341
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/318-live-accepts-a-report-from-a-signed-in-u → main 1 commit fork
4 files
changed +95
−5
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/lobby.test.ts +56
| @@ -452,6 +452,62 @@ describe("lobby", () => { | ||
| 452 | 452 | expect(admin.log.length).toBe(1); |
| 453 | 453 | }); |
| 454 | 454 | |
| 455 | test("report: a signed-in user reports a line; only they hear it, owner and moderators get the list, a guest and a banned user are refused", async () => { | |
| 456 | const admin = new Admin(); | |
| 457 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); | |
| 458 | const port = await start(undefined, undefined, admin); | |
| 459 | const alice = await join(port, ALICE); | |
| 460 | const carol = await join(port, CAROL); | |
| 461 | const bob = await join(port, BOB); | |
| 462 | const owner = await join(port, OWNER); | |
| 463 | const guest = await join(port); | |
| 464 | expect((await bob.first((m) => m.type === "reports")).reports).toEqual([]); | |
| 465 | expect((await owner.first((m) => m.type === "reports")).reports).toEqual([]); | |
| 466 | carol.send({ type: "post", text: "rude" }); | |
| 467 | const id = (await alice.next((m) => m.type === "said")).message.id; | |
| 468 | for (const c of [alice, carol, bob, owner, guest]) c.heard.length = 0; | |
| 469 | ||
| 470 | alice.send({ type: "report", id }); | |
| 471 | expect(await alice.next((m) => m.type === "reported")).toEqual({ type: "reported", id }); | |
| 472 | for (const c of [bob, owner]) { | |
| 473 | expect((await c.first((m) => m.type === "reports")).reports).toEqual([expect.objectContaining({ id: String(id), text: "rude", author: "carol", reporter: "u-alice" })]); | |
| 474 | } | |
| 475 | alice.send({ type: "report", id }); | |
| 476 | await alice.next((m) => m.type === "reported"); | |
| 477 | alice.send({ type: "report", id: 9999 }); | |
| 478 | expect((await alice.next((m) => m.type === "error")).error).toBe("no such line to report"); | |
| 479 | alice.send({ type: "report", id: "1" }); | |
| 480 | expect((await alice.next((m) => m.type === "error")).error).toBe("no such line to report"); | |
| 481 | guest.send({ type: "report", id }); | |
| 482 | expect((await guest.next((m) => m.type === "error")).error).toBe("join to take part"); | |
| 483 | await sleep(50); | |
| 484 | expect(lobby!.lineReports.list().length).toBe(1); | |
| 485 | for (const c of [carol, guest]) expect(c.heard.some((m) => m.type === "reports" || m.type === "reported")).toBe(false); | |
| 486 | expect(alice.heard.filter((m) => m.type === "reports").length).toBe(0); | |
| 487 | expect(bob.heard.filter((m) => m.type === "reports").length).toBe(1); | |
| 488 | ||
| 489 | owner.send({ type: "ban", id: "u-alice", on: true }); | |
| 490 | await alice.next((m) => m.type === "hello" && m.banned); | |
| 491 | alice.send({ type: "report", id }); | |
| 492 | expect((await alice.next((m) => m.type === "error")).error).toContain("banned"); | |
| 493 | }); | |
| 494 | ||
| 495 | test("report: making someone a moderator sends them the list; a viewer's timings still pass", async () => { | |
| 496 | const port = await start(); | |
| 497 | const alice = await join(port, ALICE); | |
| 498 | const carol = await join(port, CAROL); | |
| 499 | const owner = await join(port, OWNER); | |
| 500 | carol.send({ type: "post", text: "x" }); | |
| 501 | const id = (await alice.next((m) => m.type === "said")).message.id; | |
| 502 | alice.send({ type: "report", id }); | |
| 503 | await alice.next((m) => m.type === "reported"); | |
| 504 | expect(alice.heard.some((m) => m.type === "reports")).toBe(false); | |
| 505 | owner.send({ type: "settings", set: { moderators: ["alice"] } }); | |
| 506 | expect((await alice.first((m) => m.type === "reports")).reports.length).toBe(1); | |
| 507 | alice.send({ type: "report", rttP50: -1 }); | |
| 508 | expect((await alice.next((m) => m.type === "error")).error).toContain("numbers in range"); | |
| 509 | }); | |
| 510 | ||
| 455 | 511 | test("a guest who picked a moderator's name is not a moderator", async () => { |
| 456 | 512 | const admin = new Admin(); |
| 457 | 513 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["zed"] }); |
Software/so101/src/lobby.ts +35 −4
| @@ -21,13 +21,15 @@ | ||
| 21 | 21 | * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion |
| 22 | 22 | * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch |
| 23 | 23 | * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused |
| 24 | * {"type": "report", …} anyone: the page's own timings (src/viewer-reports.ts), every 5 s | |
| 24 | * {"type": "report", "id": 12} signed in, not banned: report a chat line; answered to you alone with {"type": "reported", "id"}, a repeat too | |
| 25 | * {"type": "report", …} anyone: the page's own timings (src/viewer-reports.ts), every 5 s (no "id") | |
| 25 | 26 | * {"type": "pong"} the answer to {"type": "ping"} |
| 26 | 27 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 27 | 28 | * Server → client: hello (who you are, and whether you may moderate: delete a line), state (the turn, the line, pause, the announcement and the speed cap), |
| 28 | 29 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 29 | 30 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 30 | * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay | |
| 31 | * settings, hidden lines, mute suggestions), banned and words (owners only), reports (the owner and verified moderators: the | |
| 32 | * reported lines, newest first, on join, on each new report and when someone is made a moderator), reported, ping, error; and whatever the relay | |
| 31 | 33 | * `publish`es to everyone: the arm's status, so a page needs this one socket for the pose, the chat and the queue. |
| 32 | 34 | */ |
| 33 | 35 | |
| @@ -39,6 +41,7 @@ import { Admin, AdminError } from "./admin.ts"; | ||
| 39 | 41 | import { frameBytes, TOO_BIG_CLOSE } from "./frame-limit.ts"; |
| 40 | 42 | import { Chat, ChatError } from "./chat.ts"; |
| 41 | 43 | import { Moderator } from "./moderator.ts"; |
| 44 | import { Reports } from "./reports.ts"; | |
| 42 | 45 | import { WordFilter } from "./word-filter.ts"; |
| 43 | 46 | import { identify, LOCAL, type Person, type Who } from "./identity.ts"; |
| 44 | 47 | import { Turns, type TurnEvent } from "./turns.ts"; |
| @@ -60,6 +63,8 @@ export interface LobbyOptions { | ||
| 60 | 63 | chatFile?: string; |
| 61 | 64 | /** The word filter's file; none keeps it in memory. */ |
| 62 | 65 | wordsFile?: string; |
| 66 | /** The chat reports' file; none keeps them in memory. */ | |
| 67 | reportsFile?: string; | |
| 63 | 68 | /** The admin settings' file; none keeps them in memory. */ |
| 64 | 69 | adminFile?: string; |
| 65 | 70 | /** The line and the current turn's file; none keeps them in memory. */ |
| @@ -99,6 +104,8 @@ export class Lobby { | ||
| 99 | 104 | readonly filter: WordFilter; |
| 100 | 105 | /** What each open page last reported about its own link. */ |
| 101 | 106 | readonly reports: ViewerReports; |
| 107 | /** Chat lines people reported, for the owner and moderators. */ | |
| 108 | readonly lineReports: Reports; | |
| 102 | 109 | readonly local: boolean; |
| 103 | 110 | readonly moderator: Moderator | undefined; |
| 104 | 111 | private readonly key: string; |
| @@ -124,6 +131,7 @@ export class Lobby { | ||
| 124 | 131 | this.accounts = options.accounts ?? new Accounts(); |
| 125 | 132 | this.filter = new WordFilter(options.wordsFile); |
| 126 | 133 | this.reports = new ViewerReports(this.now); |
| 134 | this.lineReports = new Reports(options.reportsFile); | |
| 127 | 135 | this.silentMs = options.silentMs ?? 15_000; |
| 128 | 136 | this.moderator = options.moderator; |
| 129 | 137 | if (this.moderator) { |
| @@ -205,7 +213,10 @@ export class Lobby { | ||
| 205 | 213 | } |
| 206 | 214 | this.broadcastState(); |
| 207 | 215 | this.broadcastAdmin(); |
| 208 | if (changed.includes("moderators")) for (const s of this.seats) if (s.who && !s.who.owner) this.send(s, this.hello(s)); | |
| 216 | if (changed.includes("moderators")) { | |
| 217 | for (const s of this.seats) if (s.who && !s.who.owner) this.send(s, this.hello(s)); | |
| 218 | for (const s of this.seats) if (this.mayHearMuted(s)) this.send(s, this.reportsList()); | |
| 219 | } | |
| 209 | 220 | if (changed.length) for (const listener of this.changeListeners) listener(); |
| 210 | 221 | } |
| 211 | 222 | |
| @@ -270,7 +281,10 @@ export class Lobby { | ||
| 270 | 281 | this.send(seat, this.hello(seat)); |
| 271 | 282 | this.send(seat, this.state()); |
| 272 | 283 | this.send(seat, this.chatLog()); |
| 273 | if (this.mayHearMuted(seat)) this.send(seat, this.mutedList()); | |
| 284 | if (this.mayHearMuted(seat)) { | |
| 285 | this.send(seat, this.mutedList()); | |
| 286 | this.send(seat, this.reportsList()); | |
| 287 | } | |
| 274 | 288 | if (who?.owner) { |
| 275 | 289 | this.send(seat, this.bannedList()); |
| 276 | 290 | this.send(seat, this.wordsList()); |
| @@ -309,6 +323,7 @@ export class Lobby { | ||
| 309 | 323 | const m = message as Record<string, unknown>; |
| 310 | 324 | const who = seat.who; |
| 311 | 325 | if (m.type === "pong") return; |
| 326 | if (m.type === "report" && "id" in m) return this.reportLine(seat, m.id); | |
| 312 | 327 | if (m.type === "report") { |
| 313 | 328 | const report = parseReport(m); |
| 314 | 329 | if (!report) return this.send(seat, { type: "error", error: "that report is not numbers in range" }); |
| @@ -444,6 +459,22 @@ export class Lobby { | ||
| 444 | 459 | return Boolean(s.who && (s.who.owner || (s.verified && this.admin.isModerator(s.who.name)))); |
| 445 | 460 | } |
| 446 | 461 | |
| 462 | /** 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. */ | |
| 463 | private reportLine(seat: Seat, id: unknown): void { | |
| 464 | const who = seat.who; | |
| 465 | if (!who) return this.send(seat, { type: "error", error: "join to take part" }); | |
| 466 | if (this.barred(who, seat.from)) return this.send(seat, { type: "error", error: "you are banned: you can still watch" }); | |
| 467 | const line = Number.isSafeInteger(id) ? this.chat.messages.find((x) => x.id === id) : undefined; | |
| 468 | if (!line?.from) return this.send(seat, { type: "error", error: "no such line to report" }); | |
| 469 | const added = this.lineReports.add({ id: String(line.id), text: line.text, author: line.from.name, reporter: who.id, at: this.now() }); | |
| 470 | this.send(seat, { type: "reported", id: line.id }); | |
| 471 | if (added) for (const s of this.seats) if (this.mayHearMuted(s)) this.send(s, this.reportsList()); | |
| 472 | } | |
| 473 | ||
| 474 | private reportsList() { | |
| 475 | return { type: "reports", reports: this.lineReports.list() }; | |
| 476 | } | |
| 477 | ||
| 447 | 478 | /** Delete a chat line as the owner or a moderator; the admin log says who. */ |
| 448 | 479 | private deleteLine(who: Person, id: unknown): void { |
| 449 | 480 | if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?"); |
Software/so101/src/reports.ts +3 −1
| @@ -26,7 +26,8 @@ export class Reports { | ||
| 26 | 26 | /** Oldest first. */ |
| 27 | 27 | private items: Report[] = []; |
| 28 | 28 | |
| 29 | constructor(private readonly file: string) { | |
| 29 | constructor(private readonly file?: string) { | |
| 30 | if (!file) return; | |
| 30 | 31 | let raw: string; |
| 31 | 32 | try { |
| 32 | 33 | raw = readFileSync(file, "utf8"); |
| @@ -61,6 +62,7 @@ export class Reports { | ||
| 61 | 62 | } |
| 62 | 63 | |
| 63 | 64 | private save(): void { |
| 65 | if (!this.file) return; | |
| 64 | 66 | const tmp = `${this.file}.tmp`; |
| 65 | 67 | writeFileSync(tmp, JSON.stringify(this.items)); |
| 66 | 68 | renameSync(tmp, this.file); |
new above
offline