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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

A moderator can delete a chat line over /live - the first moderator action, server-checked and logged #239

Parked opened this
Closed without merging

smith/arm:smith/225-a-moderator-can-delete-a-chat-line-over → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +56 −7
Software/so101/src/lobby.test.ts +38
@@ -278,6 +278,44 @@ describe("lobby", () => {
278278 expect((await late.first((m) => m.type === "chat")).messages).toEqual([]);
279279 });
280280
281 test("a moderator deletes a chat line and it is logged; a plain user is refused; settings and pause stay the owner's", async () => {
282 const admin = new Admin();
283 admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] });
284 const port = await start(undefined, undefined, admin);
285 const alice = await join(port, ALICE);
286 const bob = await join(port, BOB);
287 const owner = await join(port, OWNER);
288 alice.send({ type: "post", text: "hello" });
289 const said = await bob.next((m) => m.type === "said");
290
291 alice.send({ type: "delete", id: said.message.id });
292 expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that");
293 bob.send({ type: "settings", set: { paused: true } });
294 expect((await bob.next((m) => m.type === "error")).error).toBe("only the owner can do that");
295 bob.send({ type: "pause", on: true });
296 expect((await bob.next((m) => m.type === "error" && m !== bob.heard.find((x) => x.type === "error"))).error).toBe("only the owner can do that");
297 expect(admin.settings.paused).toBe(false);
298
299 bob.send({ type: "delete", id: said.message.id });
300 expect((await alice.next((m) => m.type === "unsaid")).id).toBe(said.message.id);
301 expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${said.message.id}` });
302 expect((await owner.next((m) => m.type === "admin" && m.log.at(-1).by.name === "bob")).log.at(-1).what).toContain("deleted chat line");
303 });
304
305 test("a guest who picked a moderator's name is not a moderator", async () => {
306 const admin = new Admin();
307 admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["zed"] });
308 const accounts = new Accounts();
309 const port = await start(undefined, undefined, admin, undefined, accounts);
310 const alice = await join(port, ALICE);
311 alice.send({ type: "post", text: "hi" });
312 const said = await alice.next((m) => m.type === "said");
313 const guest = await join(port);
314 guest.send({ type: "delete", id: said.message.id });
315 expect((await guest.next((m) => m.type === "error")).error).toBeTruthy();
316 expect(admin.log.length).toBe(1);
317 });
318
281319 test("chat history is lazy: a new page gets the last lines, anyone asks for older pages, oldest first", async () => {
282320 const chat = new Chat();
283321 for (let i = 0; i < 100; i++) chat.system(`line ${i}`);
Software/so101/src/lobby.ts +18 −7
@@ -11,7 +11,7 @@
1111 * {"type": "leave"} hand back the turn, or leave the line
1212 * {"type": "post", "text": "…"} say something
1313 * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line
14 * {"type": "delete", "id": 12} owner: delete a chat line
14 * {"type": "delete", "id": 12} owner or a moderator: delete a chat line
1515 * {"type": "mute", "id": "<user id>", "on": true|false} owner
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
@@ -69,6 +69,8 @@ interface Seat {
6969 ws: WebSocket;
7070 who: Who;
7171 from: Visitor;
72 /** The forge signed this identity (a guest's chosen name never counts as a moderator). */
73 verified: boolean;
7274 heard: number;
7375 }
7476
@@ -232,7 +234,8 @@ export class Lobby {
232234 upgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void {
233235 const who = this.who(req);
234236 const from = visitor(req);
235 this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from));
237 const verified = Boolean(who) && this.verified(req)?.id === who?.id;
238 this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from, verified));
236239 }
237240
238241 close(): void {
@@ -244,8 +247,8 @@ export class Lobby {
244247 }
245248
246249 // ------------------------------------------------------------------ sockets
247 private seat(ws: WebSocket, who: Who, from: Visitor): void {
248 const seat: Seat = { ws, who, from, heard: Date.now() };
250 private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void {
251 const seat: Seat = { ws, who, from, verified, heard: Date.now() };
249252 this.seats.add(seat);
250253 if (who) this.turns.connect(who.id);
251254 this.send(seat, this.hello(seat));
@@ -314,6 +317,7 @@ export class Lobby {
314317 return;
315318 }
316319 }
320 if (!who.owner && m.type === "delete" && seat.verified && this.admin.isModerator(who.name)) return this.deleteLine(who, m.id);
317321 if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" });
318322 switch (m.type) {
319323 case "end":
@@ -328,9 +332,7 @@ export class Lobby {
328332 case "settings":
329333 return this.configure(who, m.set);
330334 case "delete":
331 if (!Number.isSafeInteger(m.id)) return this.send(seat, { type: "error", error: "delete which line?" });
332 if (this.chat.delete(m.id as number)) this.broadcast({ type: "unsaid", id: m.id });
333 return;
335 return this.deleteLine(who, m.id);
334336 case "words": {
335337 const words = this.filter.set(m.words);
336338 this.admin.record(who, `word filter → ${words.length} word${words.length === 1 ? "" : "s"}`);
@@ -398,6 +400,15 @@ export class Lobby {
398400 this.send(seat, { type: "error", error: "unknown request" });
399401 }
400402
403 /** Delete a chat line as the owner or a moderator; the admin log says who. */
404 private deleteLine(who: Person, id: unknown): void {
405 if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?");
406 if (!this.chat.delete(id as number)) return;
407 this.admin.record(who, `deleted chat line ${id}`);
408 this.broadcast({ type: "unsaid", id });
409 this.broadcastAdmin();
410 }
411
401412 private turnChanged(event: TurnEvent): void {
402413 if (event.kind === "turn") {
403414 const { ended, started } = event;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs