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 #300

Parked opened this
Closed without merging

smith/arm:smith/226-a-moderator-can-mute-and-unmute-over-liv → main 2 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +66 −21
Software/so101/src/lobby.test.ts +30
@@ -346,6 +346,36 @@ describe("lobby", () => {
346346 expect(section).toContain(`<b>bob</b> deleted chat line ${said.message.id}`);
347347 });
348348
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
349379 test("a removed line is kept with who removed it, and Restore on /admin or over /live brings it back for everyone, logged", async () => {
350380 const admin = new Admin();
351381 const port = await start(undefined, undefined, admin);
Software/so101/src/lobby.ts +36 −21
@@ -12,7 +12,7 @@
1212 * {"type": "post", "text": "…"} say something
1313 * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line
1414 * {"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)
1616 * {"type": "pause", "on": true|false} owner: maintenance; nobody new gets a turn
1717 * {"type": "settings", "set": {…}} owner: any of src/admin.ts's settings
1818 * {"type": "crew", "settings": {"enabled", "rules", "maxPerMinute"}} owner: the moderator's settings
@@ -95,6 +95,8 @@ export class Lobby {
9595 readonly filter: WordFilter;
9696 readonly local: boolean;
9797 readonly moderator: Moderator | undefined;
98 /** Ids that have joined as the owner, so a moderator cannot mute one who has gone. */
99 private readonly ownerIds = new Set<string>();
98100 private readonly key: string;
99101 private readonly wss = new WebSocketServer({ noServer: true, maxPayload: MAX_FRAME });
100102 private readonly seats = new Set<Seat>();
@@ -259,12 +261,13 @@ export class Lobby {
259261 private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void {
260262 const seat: Seat = { ws, who, from, verified, heard: Date.now() };
261263 this.seats.add(seat);
264 if (who?.owner) this.ownerIds.add(who.id);
262265 if (who) this.turns.connect(who.id);
263266 this.send(seat, this.hello(seat));
264267 this.send(seat, this.state());
265268 this.send(seat, this.chatLog());
269 if (this.mayHearMuted(seat)) this.send(seat, this.mutedList());
266270 if (who?.owner) {
267 this.send(seat, this.mutedList());
268271 this.send(seat, this.bannedList());
269272 this.send(seat, this.wordsList());
270273 this.send(seat, this.adminState());
@@ -326,7 +329,10 @@ export class Lobby {
326329 return;
327330 }
328331 }
329 if (!who.owner && m.type === "delete" && seat.verified && this.admin.isModerator(who.name)) return this.deleteLine(who, m.id);
332 if (!who.owner && seat.verified && this.admin.isModerator(who.name)) {
333 if (m.type === "delete") return this.deleteLine(who, m.id);
334 if (m.type === "mute") return this.muteUser(seat, who, m);
335 }
330336 if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" });
331337 switch (m.type) {
332338 case "end":
@@ -349,23 +355,8 @@ export class Lobby {
349355 for (const s of this.seats) if (s.who?.owner) this.send(s, this.wordsList());
350356 return;
351357 }
352 case "mute": {
353 if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" });
354 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" });
355 const id = m.id;
356 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;
357 const span = MUTE_FOR[m.for ?? "forever"];
358 if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span);
359 else this.chat.unmute(id);
360 this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`);
361 this.broadcastAdmin();
362 for (const s of this.seats) {
363 if (s.who?.id === id) this.send(s, this.hello(s));
364 if (s.who?.owner) this.send(s, this.mutedList());
365 }
366 this.moderator?.muted(id);
367 return;
368 }
358 case "mute":
359 return this.muteUser(seat, who, m);
369360 case "ban": {
370361 if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "ban whom?" });
371362 const id = m.id;
@@ -413,6 +404,30 @@ export class Lobby {
413404 this.send(seat, { type: "error", error: "unknown request" });
414405 }
415406
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}`);

Fails open: ownerIds only knows owners who have joined, and an unresolved name falls back to the id, so isModerator(name) misses a moderator who has not joined. Deny when the target is not positively known to be a plain user.

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
416431 /** Delete a chat line as the owner or a moderator; the admin log says who. */
417432 private deleteLine(who: Person, id: unknown): void {
418433 if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?");
@@ -504,7 +519,7 @@ export class Lobby {
504519 if (!ended.length) return;
505520 for (const s of this.seats) {
506521 if (s.who && ended.includes(s.who.id)) this.send(s, this.hello(s));
507 if (s.who?.owner) this.send(s, this.mutedList());
522 if (this.mayHearMuted(s)) this.send(s, this.mutedList());
508523 }
509524 }
510525

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs