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

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/318-live-accepts-a-report-from-a-signed-in-u → main 1 commit fork

  • Merged. smith/318-live-accepts-a-report-from-a-signed-in-u was merged into main as f21ce60.

Review

Sign in to review this change.
4 files changed +95 −5
Software/so101/bench/fake-serve.ts +1
@@ -54,6 +54,7 @@ const lobby = new Lobby({
5454 adminFile: join(dir, "admin.json"),
5555 turnsFile: join(dir, "turns.json"),
5656 wordsFile: join(dir, "words.json"),
57 reportsFile: join(dir, "reports.json"),
5758 accounts: new Accounts({ file: join(dir, "accounts.sqlite") }),
5859 moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }),
5960 });
Software/so101/src/lobby.test.ts +56
@@ -452,6 +452,62 @@ describe("lobby", () => {
452452 expect(admin.log.length).toBe(1);
453453 });
454454
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
455511 test("a guest who picked a moderator's name is not a moderator", async () => {
456512 const admin = new Admin();
457513 admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["zed"] });
Software/so101/src/lobby.ts +35 −4
@@ -21,13 +21,15 @@
2121 * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion
2222 * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch
2323 * {"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")
2526 * {"type": "pong"} the answer to {"type": "ping"}
2627 * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one
2728 * 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),
2829 * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way),
2930 * 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
3133 * `publish`es to everyone: the arm's status, so a page needs this one socket for the pose, the chat and the queue.
3234 */
3335
@@ -39,6 +41,7 @@ import { Admin, AdminError } from "./admin.ts";
3941 import { frameBytes, TOO_BIG_CLOSE } from "./frame-limit.ts";
4042 import { Chat, ChatError } from "./chat.ts";
4143 import { Moderator } from "./moderator.ts";
44 import { Reports } from "./reports.ts";
4245 import { WordFilter } from "./word-filter.ts";
4346 import { identify, LOCAL, type Person, type Who } from "./identity.ts";
4447 import { Turns, type TurnEvent } from "./turns.ts";
@@ -60,6 +63,8 @@ export interface LobbyOptions {
6063 chatFile?: string;
6164 /** The word filter's file; none keeps it in memory. */
6265 wordsFile?: string;
66 /** The chat reports' file; none keeps them in memory. */
67 reportsFile?: string;
6368 /** The admin settings' file; none keeps them in memory. */
6469 adminFile?: string;
6570 /** The line and the current turn's file; none keeps them in memory. */
@@ -99,6 +104,8 @@ export class Lobby {
99104 readonly filter: WordFilter;
100105 /** What each open page last reported about its own link. */
101106 readonly reports: ViewerReports;
107 /** Chat lines people reported, for the owner and moderators. */
108 readonly lineReports: Reports;
102109 readonly local: boolean;
103110 readonly moderator: Moderator | undefined;
104111 private readonly key: string;
@@ -124,6 +131,7 @@ export class Lobby {
124131 this.accounts = options.accounts ?? new Accounts();
125132 this.filter = new WordFilter(options.wordsFile);
126133 this.reports = new ViewerReports(this.now);
134 this.lineReports = new Reports(options.reportsFile);
127135 this.silentMs = options.silentMs ?? 15_000;
128136 this.moderator = options.moderator;
129137 if (this.moderator) {
@@ -205,7 +213,10 @@ export class Lobby {
205213 }
206214 this.broadcastState();
207215 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 }
209220 if (changed.length) for (const listener of this.changeListeners) listener();
210221 }
211222
@@ -270,7 +281,10 @@ export class Lobby {
270281 this.send(seat, this.hello(seat));
271282 this.send(seat, this.state());
272283 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 }
274288 if (who?.owner) {
275289 this.send(seat, this.bannedList());
276290 this.send(seat, this.wordsList());
@@ -309,6 +323,7 @@ export class Lobby {
309323 const m = message as Record<string, unknown>;
310324 const who = seat.who;
311325 if (m.type === "pong") return;
326 if (m.type === "report" && "id" in m) return this.reportLine(seat, m.id);
312327 if (m.type === "report") {
313328 const report = parseReport(m);
314329 if (!report) return this.send(seat, { type: "error", error: "that report is not numbers in range" });
@@ -444,6 +459,22 @@ export class Lobby {
444459 return Boolean(s.who && (s.who.owner || (s.verified && this.admin.isModerator(s.who.name))));
445460 }
446461
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
447478 /** Delete a chat line as the owner or a moderator; the admin log says who. */
448479 private deleteLine(who: Person, id: unknown): void {
449480 if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?");
Software/so101/src/reports.ts +3 −1
@@ -26,7 +26,8 @@ export class Reports {
2626 /** Oldest first. */
2727 private items: Report[] = [];
2828
29 constructor(private readonly file: string) {
29 constructor(private readonly file?: string) {
30 if (!file) return;
3031 let raw: string;
3132 try {
3233 raw = readFileSync(file, "utf8");
@@ -61,6 +62,7 @@ export class Reports {
6162 }
6263
6364 private save(): void {
65 if (!this.file) return;
6466 const tmp = `${this.file}.tmp`;
6567 writeFileSync(tmp, JSON.stringify(this.items));
6668 renameSync(tmp, this.file);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs