Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A moderator can mute and unmute over /live, never the owner or another moderator, and receives the muted list #298
smith/arm:smith/226-a-moderator-can-mute-and-unmute-over-liv → main 1 commit fork
2 files
changed +66
−21
Software/so101/src/lobby.test.ts +30
| @@ -346,6 +346,36 @@ describe("lobby", () => { | ||
| 346 | 346 | expect(section).toContain(`<b>bob</b> deleted chat line ${said.message.id}`); |
| 347 | 347 | }); |
| 348 | 348 | |
| 349 | test("a moderator mutes and unmutes, is logged and sees the muted list; never the owner or another moderator; a plain user is refused", async () => { | |
| 350 | const admin = new Admin(); | |
| 351 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob", "carol"] }); | |
| 352 | const port = await start(undefined, undefined, admin); | |
| 353 | const alice = await join(port, ALICE); | |
| 354 | const owner = await join(port, OWNER); | |
| 355 | const bob = await join(port, BOB); | |
| 356 | await join(port, CAROL); | |
| 357 | expect((await bob.first((m) => m.type === "muted")).muted).toEqual([]); | |
| 358 | ||
| 359 | bob.send({ type: "mute", id: "u-alice", on: true, for: "1h" }); | |
| 360 | expect((await alice.next((m) => m.type === "hello")).muted).toBe(true); | |
| 361 | expect((await bob.next((m) => m.type === "muted")).muted).toMatchObject([{ id: "u-alice", name: "alice" }]); | |
| 362 | expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: "muted alice" }); | |
| 363 | bob.send({ type: "mute", id: "u-alice", on: false }); | |
| 364 | expect((await bob.next((m) => m.type === "muted")).muted).toEqual([]); | |
| 365 | expect(admin.log.at(-1)).toMatchObject({ by: { name: "bob" }, what: "unmuted alice" }); | |
| 366 | ||
| 367 | const logged = admin.log.length; | |
| 368 | bob.send({ type: "mute", id: "u-owner", on: true }); | |
| 369 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner can mute"); | |
| 370 | bob.send({ type: "mute", id: "u-carol", on: true }); | |
| 371 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner can mute"); | |
| 372 | expect(admin.log.length).toBe(logged); | |
| 373 | ||
| 374 | alice.send({ type: "mute", id: "u-bob", on: true }); | |
| 375 | expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that"); | |
| 376 | expect(admin.log.length).toBe(logged); | |
| 377 | }); | |
| 378 | ||
| 349 | 379 | test("the owner's delete is logged with the owner as who did it, and a delete without an integer id is refused", async () => { |
| 350 | 380 | const admin = new Admin(); |
| 351 | 381 | const port = await start(undefined, undefined, admin); |
Software/so101/src/lobby.ts +36 −21
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | * {"type": "post", "text": "…"} say something |
| 13 | 13 | * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line |
| 14 | 14 | * {"type": "delete", "id": 12} owner or a moderator: delete a chat line |
| 15 | * {"type": "mute", "id": "<user id>", "on": true|false, "for": "10m"|"1h"|"24h"|"forever"} owner | |
| 15 | * {"type": "mute", "id": "<user id>", "on": true|false, "for": "10m"|"1h"|"24h"|"forever"} owner or a moderator (never the owner or a moderator) | |
| 16 | 16 | * {"type": "pause", "on": true|false} owner: maintenance; nobody new gets a turn |
| 17 | 17 | * {"type": "settings", "set": {…}} owner: any of src/admin.ts's settings |
| 18 | 18 | * {"type": "crew", "settings": {"enabled", "rules", "maxPerMinute"}} owner: the moderator's settings |
| @@ -94,6 +94,8 @@ export class Lobby { | ||
| 94 | 94 | readonly filter: WordFilter; |
| 95 | 95 | readonly local: boolean; |
| 96 | 96 | readonly moderator: Moderator | undefined; |
| 97 | /** Ids that have joined as the owner, so a moderator cannot mute one who has gone. */ | |
| 98 | private readonly ownerIds = new Set<string>(); | |
| 97 | 99 | private readonly key: string; |
| 98 | 100 | private readonly wss = new WebSocketServer({ noServer: true, maxPayload: MAX_FRAME }); |
| 99 | 101 | private readonly seats = new Set<Seat>(); |
| @@ -258,12 +260,13 @@ export class Lobby { | ||
| 258 | 260 | private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void { |
| 259 | 261 | const seat: Seat = { ws, who, from, verified, heard: Date.now() }; |
| 260 | 262 | this.seats.add(seat); |
| 263 | if (who?.owner) this.ownerIds.add(who.id); | |
| 261 | 264 | if (who) this.turns.connect(who.id); |
| 262 | 265 | this.send(seat, this.hello(seat)); |
| 263 | 266 | this.send(seat, this.state()); |
| 264 | 267 | this.send(seat, this.chatLog()); |
| 268 | if (this.mayHearMuted(seat)) this.send(seat, this.mutedList()); | |
| 265 | 269 | if (who?.owner) { |
| 266 | this.send(seat, this.mutedList()); | |
| 267 | 270 | this.send(seat, this.bannedList()); |
| 268 | 271 | this.send(seat, this.wordsList()); |
| 269 | 272 | this.send(seat, this.adminState()); |
| @@ -325,7 +328,10 @@ export class Lobby { | ||
| 325 | 328 | return; |
| 326 | 329 | } |
| 327 | 330 | } |
| 328 | if (!who.owner && m.type === "delete" && seat.verified && this.admin.isModerator(who.name)) return this.deleteLine(who, m.id); | |
| 331 | if (!who.owner && seat.verified && this.admin.isModerator(who.name)) { | |
| 332 | if (m.type === "delete") return this.deleteLine(who, m.id); | |
| 333 | if (m.type === "mute") return this.muteUser(seat, who, m); | |
| 334 | } | |
| 329 | 335 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); |
| 330 | 336 | switch (m.type) { |
| 331 | 337 | case "end": |
| @@ -348,23 +354,8 @@ export class Lobby { | ||
| 348 | 354 | for (const s of this.seats) if (s.who?.owner) this.send(s, this.wordsList()); |
| 349 | 355 | return; |
| 350 | 356 | } |
| 351 | case "mute": { | |
| 352 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); | |
| 353 | if (m.for !== undefined && (typeof m.for !== "string" || !Object.hasOwn(MUTE_FOR, m.for))) return this.send(seat, { type: "error", error: "mute for 10m, 1h, 24h or forever" }); | |
| 354 | const id = m.id; | |
| 355 | const name = [...this.seats].find((s) => s.who?.id === id)?.who?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.chat.muted.get(id) ?? id; | |
| 356 | const span = MUTE_FOR[m.for ?? "forever"]; | |
| 357 | if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span); | |
| 358 | else this.chat.unmute(id); | |
| 359 | this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`); | |
| 360 | this.broadcastAdmin(); | |
| 361 | for (const s of this.seats) { | |
| 362 | if (s.who?.id === id) this.send(s, this.hello(s)); | |
| 363 | if (s.who?.owner) this.send(s, this.mutedList()); | |
| 364 | } | |
| 365 | this.moderator?.muted(id); | |
| 366 | return; | |
| 367 | } | |
| 357 | case "mute": | |
| 358 | return this.muteUser(seat, who, m); | |
| 368 | 359 | case "ban": { |
| 369 | 360 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "ban whom?" }); |
| 370 | 361 | const id = m.id; |
| @@ -413,6 +404,30 @@ export class Lobby { | ||
| 413 | 404 | this.send(seat, { type: "error", error: "unknown request" }); |
| 414 | 405 | } |
| 415 | 406 | |
| 407 | /** Mute or unmute as the owner or a moderator; a moderator may not touch the owner or another moderator. The admin log says who. */ | |
| 408 | private muteUser(seat: Seat, who: Person, m: Record<string, unknown>): void { | |
| 409 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); | |
| 410 | if (m.for !== undefined && (typeof m.for !== "string" || !Object.hasOwn(MUTE_FOR, m.for))) return this.send(seat, { type: "error", error: "mute for 10m, 1h, 24h or forever" }); | |
| 411 | const id = m.id; | |
| 412 | const name = [...this.seats].find((s) => s.who?.id === id)?.who?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.chat.muted.get(id) ?? id; | |
| 413 | if (!who.owner && (this.ownerIds.has(id) || this.admin.isModerator(name))) return this.send(seat, { type: "error", error: "only the owner can mute the owner or a moderator" }); | |
| 414 | const span = MUTE_FOR[(m.for as string | undefined) ?? "forever"]; | |
| 415 | if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span); | |
| 416 | else this.chat.unmute(id); | |
| 417 | this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`); | |
| 418 | this.broadcastAdmin(); | |
| 419 | for (const s of this.seats) { | |
| 420 | if (s.who?.id === id) this.send(s, this.hello(s)); | |
| 421 | if (this.mayHearMuted(s)) this.send(s, this.mutedList()); | |
| 422 | } | |
| 423 | this.moderator?.muted(id); | |
| 424 | } | |
| 425 | ||
| 426 | /** The owner and a verified moderator are sent the muted list. */ | |
| 427 | private mayHearMuted(s: Seat): boolean { | |
| 428 | return Boolean(s.who && (s.who.owner || (s.verified && this.admin.isModerator(s.who.name)))); | |
| 429 | } | |
| 430 | ||
| 416 | 431 | /** Delete a chat line as the owner or a moderator; the admin log says who. */ |
| 417 | 432 | private deleteLine(who: Person, id: unknown): void { |
| 418 | 433 | if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?"); |
| @@ -491,7 +506,7 @@ export class Lobby { | ||
| 491 | 506 | if (!ended.length) return; |
| 492 | 507 | for (const s of this.seats) { |
| 493 | 508 | if (s.who && ended.includes(s.who.id)) this.send(s, this.hello(s)); |
| 494 | if (s.who?.owner) this.send(s, this.mutedList()); | |
| 509 | if (this.mayHearMuted(s)) this.send(s, this.mutedList()); | |
| 495 | 510 | } |
| 496 | 511 | } |
| 497 | 512 |
new above
offline