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
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/35-moderation-pause-ban-and-word-filter → main 3 commits fork 1 thread · 1 unresolved
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. | ||
| 287 | 287 | the queue, delete messages, mute people and ban them (long-press or |
| 288 | 288 | right-click a chat line). |
| 289 | 289 | 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. | |
| 290 | 294 | - A ban bars the person's account, their name and the address they were |
| 291 | 295 | last seen at: their turn ends, they leave the queue, and they cannot |
| 292 | 296 | 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({ | ||
| 51 | 51 | local, |
| 52 | 52 | chatFile: join(dir, "chat.json"), |
| 53 | 53 | adminFile: join(dir, "admin.json"), |
| 54 | wordsFile: join(dir, "words.json"), | |
| 54 | 55 | accounts: new Accounts({ file: join(dir, "accounts.sqlite") }), |
| 55 | 56 | moderator: new Moderator({ file: join(dir, "crew.json"), model: modelFromEnv(process.env) }), |
| 56 | 57 | }); |
Software/so101/src/lobby.test.ts +20
| @@ -295,6 +295,26 @@ describe("lobby", () => { | ||
| 295 | 295 | expect((await torque(port, forged)).status).toBe(403); |
| 296 | 296 | }); |
| 297 | 297 | |
| 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 | ||
| 298 | 318 | test("ban: the turn ends, chat and queue are refused, watching goes on, and no new name comes from that address", async () => { |
| 299 | 319 | const port = await start(); |
| 300 | 320 | const troll = await enter(port, "troll", "203.0.113.66"); |
Software/so101/src/lobby.ts +20 −1
| @@ -17,10 +17,11 @@ | ||
| 17 | 17 | * {"type": "restore", "id": 12} owner: put back a line the crew hid |
| 18 | 18 | * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion |
| 19 | 19 | * {"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 | |
| 20 | 21 | * {"type": "pong"} the answer to {"type": "ping"} |
| 21 | 22 | * Server → client: hello (who you are), state (the turn, the line, pause, the announcement and the speed cap), |
| 22 | 23 | * 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. | |
| 24 | 25 | */ |
| 25 | 26 | |
| 26 | 27 | import type { IncomingMessage } from "node:http"; |
| @@ -30,6 +31,7 @@ import { Accounts, visitor, type Visitor } from "./accounts.ts"; | ||
| 30 | 31 | import { Admin, AdminError } from "./admin.ts"; |
| 31 | 32 | import { Chat, ChatError } from "./chat.ts"; |
| 32 | 33 | import { Moderator } from "./moderator.ts"; |
| 34 | import { WordFilter } from "./word-filter.ts"; | |
| 33 | 35 | import { identify, LOCAL, type Person, type Who } from "./identity.ts"; |
| 34 | 36 | import { Turns, type TurnEvent } from "./turns.ts"; |
| 35 | 37 | |
| @@ -42,6 +44,8 @@ export interface LobbyOptions { | ||
| 42 | 44 | accounts?: Accounts; |
| 43 | 45 | /** The chat's file; none keeps it in memory. */ |
| 44 | 46 | chatFile?: string; |
| 47 | /** The word filter's file; none keeps it in memory. */ | |
| 48 | wordsFile?: string; | |
| 45 | 49 | /** The admin settings' file; none keeps them in memory. */ |
| 46 | 50 | adminFile?: string; |
| 47 | 51 | /** 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 { | ||
| 70 | 74 | readonly chat: Chat; |
| 71 | 75 | readonly accounts: Accounts; |
| 72 | 76 | readonly admin: Admin; |
| 77 | readonly filter: WordFilter; | |
| 73 | 78 | readonly local: boolean; |
| 74 | 79 | readonly moderator: Moderator | undefined; |
| 75 | 80 | private readonly key: string; |
| @@ -89,6 +94,7 @@ export class Lobby { | ||
| 89 | 94 | this.turns.setPaused(a.paused); |
| 90 | 95 | this.chat = options.chat ?? new Chat({ file: options.chatFile }); |
| 91 | 96 | this.accounts = options.accounts ?? new Accounts(); |
| 97 | this.filter = new WordFilter(options.wordsFile); | |
| 92 | 98 | this.silentMs = options.silentMs ?? 15_000; |
| 93 | 99 | this.moderator = options.moderator; |
| 94 | 100 | if (this.moderator) { |
| @@ -218,6 +224,7 @@ export class Lobby { | ||
| 218 | 224 | if (who?.owner) { |
| 219 | 225 | this.send(seat, this.mutedList()); |
| 220 | 226 | this.send(seat, this.bannedList()); |
| 227 | this.send(seat, this.wordsList()); | |
| 221 | 228 | this.send(seat, this.adminState()); |
| 222 | 229 | this.send(seat, this.crewState()); |
| 223 | 230 | } |
| @@ -261,6 +268,7 @@ export class Lobby { | ||
| 261 | 268 | this.turns.leave(who.id); |
| 262 | 269 | return; |
| 263 | 270 | case "post": { |
| 271 | if (!who.owner && typeof m.text === "string" && this.filter.blocks(m.text)) throw new ChatError("that word is not allowed here"); | |
| 264 | 272 | const said = this.chat.post(who, m.text); |
| 265 | 273 | this.broadcast({ type: "said", message: said }); |
| 266 | 274 | void this.moderator?.check(said); |
| @@ -284,6 +292,13 @@ export class Lobby { | ||
| 284 | 292 | if (!Number.isSafeInteger(m.id)) return this.send(seat, { type: "error", error: "delete which line?" }); |
| 285 | 293 | if (this.chat.delete(m.id as number)) this.broadcast({ type: "unsaid", id: m.id }); |
| 286 | 294 | 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 | } | |
| 287 | 302 | case "mute": { |
| 288 | 303 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
| 289 | 304 | const id = m.id; |
| @@ -390,6 +405,10 @@ export class Lobby { | ||
| 390 | 405 | return { type: "banned", banned: this.accounts.bans().map(({ id, name, at, by }) => ({ id, name, at, by })) }; |
| 391 | 406 | } |
| 392 | 407 | |
| 408 | private wordsList() { | |
| 409 | return { type: "words", words: this.filter.words }; | |
| 410 | } | |
| 411 | ||
| 393 | 412 | private mutedList() { |
| 394 | 413 | return { type: "muted", muted: [...this.chat.muted].map(([id, name]) => ({ id, name })) }; |
| 395 | 414 | } |
Software/so101/src/viewer.html +10
| @@ -310,6 +310,13 @@ | ||
| 310 | 310 | <div class="row wide"><button class="go">Save</button></div> |
| 311 | 311 | </form> |
| 312 | 312 | </div> |
| 313 | <div class="card"> | |
| 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> | |
| 313 | 320 | <div class="card"> |
| 314 | 321 | <h3>Crew moderator</h3> |
| 315 | 322 | <div id="crewLine" aria-live="polite"></div> |
| @@ -1203,6 +1210,8 @@ | ||
| 1203 | 1210 | // ---------------------------------------------------------------- the room: the turn, the queue and the chat over /live |
| 1204 | 1211 | // Behind the hosted app's relay only the person whose turn it is may drive, and the server refuses everyone else. |
| 1205 | 1212 | // 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) }); }); | |
| 1206 | 1215 | const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null }; |
| 1207 | 1216 | function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); } |
| 1208 | 1217 | function isOwner() { return !relayed || Boolean(lobby.hello?.you?.owner); } |
| @@ -1384,6 +1393,7 @@ | ||
| 1384 | 1393 | else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove(); |
| 1385 | 1394 | else if (m.type === "muted") lobby.mutedList = m.muted; |
| 1386 | 1395 | 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"}.`); } } | |
| 1387 | 1397 | else if (m.type === "admin") { const saved = lobby.admin !== null; lobby.admin = m; fillAdmin(saved); } |
| 1388 | 1398 | else if (m.type === "crew") lobby.crew = m; |
| 1389 | 1399 | 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 | } |
new above
offline
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.