Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Moderation: pause, ban and word filter #42

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/35-moderation-pause-ban-and-word-filter → main 3 commits fork 1 thread · 1 unresolved

  • Merged. smith/35-moderation-pause-ban-and-word-filter was merged into main as 649a7c3.

Review

Sign in to review this change.
7 files changed +158 −1
Software/README.md +4
@@ -287,6 +287,10 @@ its live pose and the cameras. Driving needs a name and a turn.
287287 the queue, delete messages, mute people and ban them (long-press or
288288 right-click a chat line).
289289 The owner can also press **Stop** during anyone's turn.
290 - The owner's **word filter** (under Admin in the page) lists words and
291 phrases; a chat line holding one whole is refused with an error and never
292 posted. Case, accents and look-alike digits (`h3ck`) are folded; the owner is
293 not held to it. The list is in `words.json` beside the arm's data.
290294 - A ban bars the person's account, their name and the address they were
291295 last seen at: their turn ends, they leave the queue, and they cannot
292296 chat, queue or make a new name from that address (or from the browser
Software/so101/bench/fake-serve.ts +1
@@ -51,6 +51,7 @@ const lobby = new Lobby({
5151 local,
5252 chatFile: join(dir, "chat.json"),
5353 adminFile: join(dir, "admin.json"),
54 wordsFile: join(dir, "words.json"),
5455 accounts: new Accounts({ file: join(dir, "accounts.sqlite") }),
5556 moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }),
5657 });
Software/so101/src/lobby.test.ts +20
@@ -295,6 +295,26 @@ describe("lobby", () => {
295295 expect((await torque(port, forged)).status).toBe(403);
296296 });
297297
298 test("word filter: only the owner sets it, a listed word is refused in chat, the owner is not held to it", async () => {
299 const port = await start();
300 const owner = await join(port, OWNER);
301 const alice = await join(port, ALICE);
302 await alice.first((m) => m.type === "hello");
303 alice.send({ type: "words", words: ["heck"] });
304 expect((await alice.next((m) => m.type === "error")).error).toContain("only the owner");
305 owner.send({ type: "words", words: "heck" });
306 expect((await owner.next((m) => m.type === "error")).error).toContain("list");
307 owner.send({ type: "words", words: ["Heck"] });
308 expect(await owner.next((m) => m.type === "words" && m.words.length === 1)).toMatchObject({ words: ["heck"] });
309 expect((await owner.first((m) => m.type === "admin" && m.log.at(-1)?.what.startsWith("word filter"))).log.at(-1).what).toContain("1 word");
310 alice.send({ type: "post", text: "oh h3ck!" });
311 expect((await alice.next((m) => m.type === "error")).error).toContain("not allowed");
312 alice.send({ type: "post", text: "a heckle is fine" });
313 expect((await alice.next((m) => m.type === "said")).message.text).toBe("a heckle is fine");
314 owner.send({ type: "post", text: "heck" });
315 expect((await alice.next((m) => m.type === "said" && m.message.text === "heck")).message.from.name).toBe("trevato");
316 });
317
298318 test("ban: the turn ends, chat and queue are refused, watching goes on, and no new name comes from that address", async () => {
299319 const port = await start();
300320 const troll = await enter(port, "troll", "203.0.113.66");
Software/so101/src/lobby.ts +20 −1
@@ -17,10 +17,11 @@
1717 * {"type": "restore", "id": 12} owner: put back a line the crew hid
1818 * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion
1919 * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch
20 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
2021 * {"type": "pong"} the answer to {"type": "ping"}
2122 * Server → client: hello (who you are), state (the turn, the line, pause, the announcement and the speed cap),
2223 * chat (the log), said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only:
23 * settings, hidden lines, mute suggestions), banned (owners only), ping, error.
24 * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error.
2425 */
2526
2627 import type { IncomingMessage } from "node:http";
@@ -30,6 +31,7 @@ import { Accounts, visitor, type Visitor } from "./accounts.ts";
3031 import { Admin, AdminError } from "./admin.ts";
3132 import { Chat, ChatError } from "./chat.ts";
3233 import { Moderator } from "./moderator.ts";
34 import { WordFilter } from "./word-filter.ts";
3335 import { identify, LOCAL, type Person, type Who } from "./identity.ts";
3436 import { Turns, type TurnEvent } from "./turns.ts";
3537
@@ -42,6 +44,8 @@ export interface LobbyOptions {
4244 accounts?: Accounts;
4345 /** The chat's file; none keeps it in memory. */
4446 chatFile?: string;
47 /** The word filter's file; none keeps it in memory. */
48 wordsFile?: string;
4549 /** The admin settings' file; none keeps them in memory. */
4650 adminFile?: string;
4751 /** Given turns keep their own limits until the owner changes one; made here, they start from the admin settings. */
@@ -70,6 +74,7 @@ export class Lobby {
7074 readonly chat: Chat;
7175 readonly accounts: Accounts;
7276 readonly admin: Admin;
77 readonly filter: WordFilter;
7378 readonly local: boolean;
7479 readonly moderator: Moderator | undefined;
7580 private readonly key: string;
@@ -89,6 +94,7 @@ export class Lobby {
8994 this.turns.setPaused(a.paused);
9095 this.chat = options.chat ?? new Chat({ file: options.chatFile });
9196 this.accounts = options.accounts ?? new Accounts();
97 this.filter = new WordFilter(options.wordsFile);
9298 this.silentMs = options.silentMs ?? 15_000;
9399 this.moderator = options.moderator;
94100 if (this.moderator) {
@@ -218,6 +224,7 @@ export class Lobby {
218224 if (who?.owner) {
219225 this.send(seat, this.mutedList());
220226 this.send(seat, this.bannedList());
227 this.send(seat, this.wordsList());
221228 this.send(seat, this.adminState());
222229 this.send(seat, this.crewState());
223230 }
@@ -261,6 +268,7 @@ export class Lobby {
261268 this.turns.leave(who.id);
262269 return;
263270 case "post": {
271 if (!who.owner && typeof m.text === "string" && this.filter.blocks(m.text)) throw new ChatError("that word is not allowed here");
264272 const said = this.chat.post(who, m.text);
265273 this.broadcast({ type: "said", message: said });
266274 void this.moderator?.check(said);
@@ -284,6 +292,13 @@ export class Lobby {
284292 if (!Number.isSafeInteger(m.id)) return this.send(seat, { type: "error", error: "delete which line?" });
285293 if (this.chat.delete(m.id as number)) this.broadcast({ type: "unsaid", id: m.id });
286294 return;
295 case "words": {
296 const words = this.filter.set(m.words);
297 this.admin.record(who, `word filter → ${words.length} word${words.length === 1 ? "" : "s"}`);
298 this.broadcastAdmin();
299 for (const s of this.seats) if (s.who?.owner) this.send(s, this.wordsList());
300 return;
301 }
287302 case "mute": {
288303 if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" });
289304 const id = m.id;
@@ -390,6 +405,10 @@ export class Lobby {
390405 return { type: "banned", banned: this.accounts.bans().map(({ id, name, at, by }) => ({ id, name, at, by })) };
391406 }
392407
408 private wordsList() {
409 return { type: "words", words: this.filter.words };
410 }
411
393412 private mutedList() {
394413 return { type: "muted", muted: [...this.chat.muted].map(([id, name]) => ({ id, name })) };
395414 }
Software/so101/src/viewer.html +10
@@ -310,6 +310,13 @@
310310 <div class="row wide"><button class="go">Save</button></div>
311311 </form>
312312 </div>
313 <div class="card">

On a 360px phone a long comma-separated list in a single-line input scrolls out of sight. Use a textarea with rows=3 so the owner can read and edit it.

314 <h3>Word filter</h3>
315 <form id="wordsForm">
316 <label class="wide">Words nobody may say, comma separated<input name="words" type="text" autocomplete="off" placeholder="none"></label>
317 <div class="row wide"><button class="go">Save</button></div>
318 </form>
319 </div>
313320 <div class="card">
314321 <h3>Crew moderator</h3>
315322 <div id="crewLine" aria-live="polite"></div>
@@ -1203,6 +1210,8 @@
12031210 // ---------------------------------------------------------------- the room: the turn, the queue and the chat over /live
12041211 // Behind the hosted app's relay only the person whose turn it is may drive, and the server refuses everyone else.
12051212 // The page follows suit: the Controls tab appears on your turn. Everything here arrives on one socket.
1213 let wordsSent = false;
1214 $("wordsForm").addEventListener("submit", (e) => { e.preventDefault(); wordsSent = true; say({ type: "words", words: $("wordsForm").elements.words.value.split(",").map((w) => w.trim()).filter(Boolean) }); });
12061215 const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null };
12071216 function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); }
12081217 function isOwner() { return !relayed || Boolean(lobby.hello?.you?.owner); }
@@ -1384,6 +1393,7 @@
13841393 else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove();
13851394 else if (m.type === "muted") lobby.mutedList = m.muted;
13861395 else if (m.type === "banned") lobby.bannedList = m.banned;
1396 else if (m.type === "words") { const f = $("wordsForm"); if (document.activeElement !== f.elements.words) f.elements.words.value = m.words.join(", "); if (wordsSent) { wordsSent = false; msg(`Word filter saved: ${m.words.length} word${m.words.length === 1 ? "" : "s"}.`); } }
13871397 else if (m.type === "admin") { const saved = lobby.admin !== null; lobby.admin = m; fillAdmin(saved); }
13881398 else if (m.type === "crew") lobby.crew = m;
13891399 else if (m.type === "error") return msg(m.error, true);
Software/so101/src/word-filter.test.ts added +31
@@ -0,0 +1,31 @@
1 import { describe, expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { WordFilter } from "./word-filter.ts";
6
7 describe("word filter", () => {
8 test("whole words, case, accents and look-alikes", () => {
9 const f = new WordFilter();
10 f.set(["Bad", "very rude"]);
11 expect(f.words).toEqual(["bad", "very rude"]);
12 for (const t of ["BAD", "bad!", "b4d", "bád", "so very rude."]) expect(f.blocks(t)).toBe(true);
13 for (const t of ["badge", "baddie", "very nice", "hello"]) expect(f.blocks(t)).toBe(false);
14 });
15
16 test("nothing listed blocks nothing; bad lists are refused", () => {
17 const f = new WordFilter();
18 expect(f.blocks("anything")).toBe(false);
19 expect(() => f.set("bad")).toThrow(/list/);
20 expect(() => f.set([1])).toThrow(/text/);
21 expect(() => f.set(["x".repeat(41)])).toThrow(/40/);
22 expect(() => f.set(Array(201).fill("a"))).toThrow(/200/);
23 expect(f.set(["", " ", "ok", "OK"])).toEqual(["ok"]);
24 });
25
26 test("the list survives a restart", () => {
27 const file = join(mkdtempSync(join(tmpdir(), "so101-words-")), "words.json");
28 new WordFilter(file).set(["nope"]);
29 expect(new WordFilter(file).blocks("Nope!")).toBe(true);
30 });
31 });
Software/so101/src/word-filter.ts added +72
@@ -0,0 +1,72 @@
1 /**
2 * The chat's word filter: the owner keeps a list of words, and a line that contains one is refused before it is
3 * posted. Matching is on whole words after folding case, accents and common look-alike digits, so "B4D" meets "bad"
4 * but "badge" does not. Kept in one JSON file beside the arm's data.
5 */
6
7 import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
8 import { dirname } from "node:path";
9 import { ChatError } from "./chat.ts";
10
11 export const MAX_WORDS = 200;
12 export const MAX_WORD_CHARS = 40;
13 const LOOKALIKE: Record<string, string> = { "0": "o", "1": "i", "3": "e", "4": "a", "5": "s", "7": "t", "@": "a", $: "s" };
14
15 /** Lowercase, accents off, look-alikes folded; anything else that is not a letter or digit becomes a space. */
16 export function fold(text: string): string {
17 return text
18 .normalize("NFKD")
19 .replace(/\p{M}/gu, "")
20 .toLowerCase()
21 .replace(/[01345$7@]/g, (c) => LOOKALIKE[c]!)
22 .replace(/[^\p{L}\p{N}]+/gu, " ")
23 .trim();
24 }
25
26 export class WordFilter {
27 private list: string[] = [];
28 private readonly file: string | undefined;
29
30 constructor(file?: string) {
31 this.file = file;
32 if (!file) return;
33 try {
34 const stored = JSON.parse(readFileSync(file, "utf8"));
35 if (Array.isArray(stored)) this.list = stored.filter((w): w is string => typeof w === "string" && fold(w) !== "").slice(0, MAX_WORDS);
36 } catch {
37 /* none yet, or unreadable: an empty list */
38 }
39 }
40
41 get words(): string[] {
42 return [...this.list];
43 }
44
45 /** Replace the list. Throws ChatError for anything that is not a short list of short words. */
46 set(input: unknown): string[] {
47 if (!Array.isArray(input) || input.length > MAX_WORDS) throw new ChatError(`the filter is a list of at most ${MAX_WORDS} words`);
48 const seen = new Set<string>();
49 for (const w of input) {
50 if (typeof w !== "string" || [...w].length > MAX_WORD_CHARS) throw new ChatError(`each word is text of at most ${MAX_WORD_CHARS} characters`);
51 const folded = fold(w);
52 if (folded) seen.add(folded);
53 }
54 this.list = [...seen];
55 this.save();
56 return this.words;
57 }
58
59 /** Whether the text holds a listed word (or phrase), whole. */
60 blocks(text: string): boolean {
61 if (!this.list.length) return false;
62 const padded = ` ${fold(text)} `;
63 return this.list.some((w) => padded.includes(` ${w} `));
64 }
65
66 private save(): void {
67 if (!this.file) return;
68 mkdirSync(dirname(this.file), { recursive: true });
69 writeFileSync(`${this.file}.tmp`, JSON.stringify(this.list));
70 renameSync(`${this.file}.tmp`, this.file);
71 }
72 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs