Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A moderator can ban and unban over /live, never the owner or another moderator, and receives the banned list #327
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/227-a-moderator-can-ban-and-unban-over-live → main 2 commits fork
2 files
changed +72
−29
Software/so101/src/lobby.test.ts +36
| @@ -452,6 +452,42 @@ describe("lobby", () => { | ||
| 452 | 452 | expect(admin.log.length).toBe(1); |
| 453 | 453 | }); |
| 454 | 454 | |
| 455 | test("a moderator bans and unbans, is logged and sees the banned list; never the owner or another moderator; a plain user is refused", async () => { | |
| 456 | const admin = new Admin(); | |
| 457 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob", "carol"] }); | |
| 458 | const port = await start(undefined, undefined, admin); | |
| 459 | const alice = await join(port, { ...ALICE, "x-forwarded-for": "198.51.100.9" }); // its own address: the ban bars no one else | |
| 460 | const owner = await join(port, OWNER); | |
| 461 | const bob = await join(port, BOB); | |
| 462 | const carol = await join(port, { ...CAROL, "x-forwarded-for": "198.51.100.10" }); | |
| 463 | expect((await bob.first((m) => m.type === "banned")).banned).toEqual([]); | |
| 464 | ||
| 465 | bob.send({ type: "ban", id: "u-alice", on: true }); | |
| 466 | expect((await alice.next((m) => m.type === "hello")).banned).toBe(true); | |
| 467 | expect((await bob.next((m) => m.type === "banned")).banned).toEqual([expect.objectContaining({ id: "u-alice", name: "alice", by: "bob" })]); | |
| 468 | expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: "banned alice" }); | |
| 469 | alice.send({ type: "say", text: "hi" }); | |
| 470 | expect((await alice.next((m) => m.type === "error")).error).toContain("banned"); | |
| 471 | ||
| 472 | bob.send({ type: "ban", id: "u-alice", on: false }); | |
| 473 | expect((await bob.next((m) => m.type === "banned")).banned).toEqual([]); | |
| 474 | expect(admin.log.at(-1)).toMatchObject({ by: { name: "bob" }, what: "unbanned alice" }); | |
| 475 | ||
| 476 | const logged = admin.log.length; | |
| 477 | for (const id of ["u-owner", "u-carol"]) { | |
| 478 | bob.send({ type: "ban", id, on: true }); | |
| 479 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner can ban"); | |
| 480 | } | |
| 481 | expect(admin.log.length).toBe(logged); | |
| 482 | owner.send({ type: "ban", id: "u-carol", on: true }); | |
| 483 | await carol.first((m) => m.type === "hello" && m.banned); | |
| 484 | bob.send({ type: "ban", id: "u-carol", on: false }); | |
| 485 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner can ban"); | |
| 486 | ||
| 487 | alice.send({ type: "ban", id: "u-bob", on: true }); | |
| 488 | expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that"); | |
| 489 | }); | |
| 490 | ||
| 455 | 491 | 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 | 492 | const admin = new Admin(); |
| 457 | 493 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); |
Software/so101/src/lobby.ts +36 −29
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | * {"type": "removed"} owner: the latest removed lines, answered with {"type": "removed", "lines": [...]} |
| 20 | 20 | * {"type": "restore", "id": 12} owner: put back a removed line, or one the crew hid |
| 21 | 21 | * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion |
| 22 | * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch | |
| 22 | * {"type": "ban", "id": "<user id>", "on": true|false} owner or a moderator (never the owner or a moderator): 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 | 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 | 25 | * {"type": "report", …} anyone: the page's own timings (src/viewer-reports.ts), every 5 s (no "id") |
| @@ -28,7 +28,7 @@ | ||
| 28 | 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), |
| 29 | 29 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 30 | 30 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 31 | * settings, hidden lines, mute suggestions), banned and words (owners only), reports (the owner and verified moderators: the | |
| 31 | * settings, hidden lines, mute suggestions), banned (owners and moderators), words (owners only), reports (the owner and verified moderators: the | |
| 32 | 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 |
| 33 | 33 | * `publish`es to everyone: the arm's status, so a page needs this one socket for the pose, the chat and the queue. |
| 34 | 34 | */ |
| @@ -284,9 +284,9 @@ export class Lobby { | ||
| 284 | 284 | if (this.mayHearMuted(seat)) { |
| 285 | 285 | this.send(seat, this.mutedList()); |
| 286 | 286 | this.send(seat, this.reportsList()); |
| 287 | this.send(seat, this.bannedList()); | |
| 287 | 288 | } |
| 288 | 289 | if (who?.owner) { |
| 289 | this.send(seat, this.bannedList()); | |
| 290 | 290 | this.send(seat, this.wordsList()); |
| 291 | 291 | this.send(seat, this.adminState()); |
| 292 | 292 | this.send(seat, this.crewState()); |
| @@ -358,6 +358,7 @@ export class Lobby { | ||
| 358 | 358 | if (!who.owner && seat.verified && this.admin.isModerator(who.name)) { |
| 359 | 359 | if (m.type === "delete") return this.deleteLine(who, m.id); |
| 360 | 360 | if (m.type === "mute") return this.muteUser(seat, who, m); |
| 361 | if (m.type === "ban") return this.banUser(seat, who, m); | |
| 361 | 362 | } |
| 362 | 363 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); |
| 363 | 364 | switch (m.type) { |
| @@ -383,32 +384,8 @@ export class Lobby { | ||
| 383 | 384 | } |
| 384 | 385 | case "mute": |
| 385 | 386 | return this.muteUser(seat, who, m); |
| 386 | case "ban": { | |
| 387 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "ban whom?" }); | |
| 388 | const id = m.id; | |
| 389 | if (m.on) { | |
| 390 | const target = [...this.seats].find((s) => s.who?.id === id); | |
| 391 | const known = target?.who ?? this.turns.queue.find((p) => p.id === id) ?? (this.turns.driver?.id === id ? this.turns.driver : null); | |
| 392 | const said = [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from; | |
| 393 | const person = known ?? (said ? { id, name: said.name, owner: false } : null); | |
| 394 | if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" }); | |
| 395 | if (person.owner) return this.send(seat, { type: "error", error: "the owner cannot be banned" }); | |
| 396 | this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name); | |
| 397 | this.turns.end(id); // their turn ends, or they leave the line | |
| 398 | this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) }); | |
| 399 | this.admin.record(who, `banned ${person.name}`); | |
| 400 | } else { | |
| 401 | const name = this.accounts.bans().find((b) => b.id === id)?.name; | |
| 402 | if (!this.accounts.unban(id)) return this.send(seat, { type: "error", error: "not banned" }); | |
| 403 | this.admin.record(who, `unbanned ${name ?? id}`); | |
| 404 | } | |
| 405 | this.broadcastAdmin(); | |
| 406 | for (const s of this.seats) { | |
| 407 | if (s.who && !s.who.owner) this.send(s, this.hello(s)); // an address ban reaches everyone there | |
| 408 | if (s.who?.owner) this.send(s, this.bannedList()); | |
| 409 | } | |
| 410 | return; | |
| 411 | } | |
| 387 | case "ban": | |
| 388 | return this.banUser(seat, who, m); | |
| 412 | 389 | case "crew": |
| 413 | 390 | if (!this.moderator) return this.send(seat, { type: "error", error: "no moderator here" }); |
| 414 | 391 | try { |
| @@ -430,6 +407,36 @@ export class Lobby { | ||
| 430 | 407 | this.send(seat, { type: "error", error: "unknown request" }); |
| 431 | 408 | } |
| 432 | 409 | |
| 410 | /** Ban or unban as the owner or a moderator; a moderator may not touch the owner or another moderator. The admin log says who. */ | |
| 411 | private banUser(seat: Seat, who: Person, m: Record<string, unknown>): void { | |
| 412 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "ban whom?" }); | |
| 413 | const id = m.id; | |
| 414 | const refused = "only the owner can ban the owner or a moderator"; | |
| 415 | if (m.on) { | |
| 416 | const target = [...this.seats].find((s) => s.who?.id === id); | |
| 417 | const known = target?.who ?? this.turns.queue.find((p) => p.id === id) ?? (this.turns.driver?.id === id ? this.turns.driver : null); | |
| 418 | const said = [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from; | |
| 419 | const person = known ?? (said ? { id, name: said.name, owner: false } : null); | |
| 420 | if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" }); | |
| 421 | if (person.owner) return this.send(seat, { type: "error", error: who.owner ? "the owner cannot be banned" : refused }); | |
| 422 | if (!who.owner && this.admin.isModerator(person.name)) return this.send(seat, { type: "error", error: refused }); | |
| 423 | this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name); | |
| 424 | this.turns.end(id); // their turn ends, or they leave the line | |
| 425 | this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) }); | |
| 426 | this.admin.record(who, `banned ${person.name}`); | |
| 427 | } else { | |
| 428 | const name = this.accounts.bans().find((b) => b.id === id)?.name; | |
| 429 | if (!who.owner && name !== undefined && this.admin.isModerator(name)) return this.send(seat, { type: "error", error: refused }); | |
| 430 | if (!this.accounts.unban(id)) return this.send(seat, { type: "error", error: "not banned" }); | |
| 431 | this.admin.record(who, `unbanned ${name ?? id}`); | |
| 432 | } | |
| 433 | this.broadcastAdmin(); | |
| 434 | for (const s of this.seats) { | |
| 435 | if (s.who && !s.who.owner) this.send(s, this.hello(s)); // an address ban reaches everyone there | |
| 436 | if (this.mayHearMuted(s)) this.send(s, this.bannedList()); | |
| 437 | } | |
| 438 | } | |
| 439 | ||
| 433 | 440 | /** Mute or unmute as the owner or a moderator; a moderator may not touch the owner or another moderator. The admin log says who. */ |
| 434 | 441 | private muteUser(seat: Seat, who: Person, m: Record<string, unknown>): void { |
| 435 | 442 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
new above
offline