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
smith/arm:smith/225-a-moderator-can-delete-a-chat-line-over → main 2 commits fork
2 files
changed +56
−7
Software/so101/src/lobby.test.ts +38
| @@ -278,6 +278,44 @@ describe("lobby", () => { | ||
| 278 | 278 | expect((await late.first((m) => m.type === "chat")).messages).toEqual([]); |
| 279 | 279 | }); |
| 280 | 280 | |
| 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 | ||
| 281 | 319 | test("chat history is lazy: a new page gets the last lines, anyone asks for older pages, oldest first", async () => { |
| 282 | 320 | const chat = new Chat(); |
| 283 | 321 | for (let i = 0; i < 100; i++) chat.system(`line ${i}`); |
Software/so101/src/lobby.ts +18 −7
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | * {"type": "leave"} hand back the turn, or leave the line |
| 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 | * {"type": "delete", "id": 12} owner: delete a chat line | |
| 14 | * {"type": "delete", "id": 12} owner or a moderator: delete a chat line | |
| 15 | 15 | * {"type": "mute", "id": "<user id>", "on": true|false} owner |
| 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 |
| @@ -69,6 +69,8 @@ interface Seat { | ||
| 69 | 69 | ws: WebSocket; |
| 70 | 70 | who: Who; |
| 71 | 71 | from: Visitor; |
| 72 | /** The forge signed this identity (a guest's chosen name never counts as a moderator). */ | |
| 73 | verified: boolean; | |
| 72 | 74 | heard: number; |
| 73 | 75 | } |
| 74 | 76 | |
| @@ -232,7 +234,8 @@ export class Lobby { | ||
| 232 | 234 | upgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void { |
| 233 | 235 | const who = this.who(req); |
| 234 | 236 | 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)); | |
| 236 | 239 | } |
| 237 | 240 | |
| 238 | 241 | close(): void { |
| @@ -244,8 +247,8 @@ export class Lobby { | ||
| 244 | 247 | } |
| 245 | 248 | |
| 246 | 249 | // ------------------------------------------------------------------ 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() }; | |
| 249 | 252 | this.seats.add(seat); |
| 250 | 253 | if (who) this.turns.connect(who.id); |
| 251 | 254 | this.send(seat, this.hello(seat)); |
| @@ -314,6 +317,7 @@ export class Lobby { | ||
| 314 | 317 | return; |
| 315 | 318 | } |
| 316 | 319 | } |
| 320 | if (!who.owner && m.type === "delete" && seat.verified && this.admin.isModerator(who.name)) return this.deleteLine(who, m.id); | |
| 317 | 321 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); |
| 318 | 322 | switch (m.type) { |
| 319 | 323 | case "end": |
| @@ -328,9 +332,7 @@ export class Lobby { | ||
| 328 | 332 | case "settings": |
| 329 | 333 | return this.configure(who, m.set); |
| 330 | 334 | 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); | |
| 334 | 336 | case "words": { |
| 335 | 337 | const words = this.filter.set(m.words); |
| 336 | 338 | this.admin.record(who, `word filter → ${words.length} word${words.length === 1 ? "" : "s"}`); |
| @@ -398,6 +400,15 @@ export class Lobby { | ||
| 398 | 400 | this.send(seat, { type: "error", error: "unknown request" }); |
| 399 | 401 | } |
| 400 | 402 | |
| 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 | ||
| 401 | 412 | private turnChanged(event: TurnEvent): void { |
| 402 | 413 | if (event.kind === "turn") { |
| 403 | 414 | const { ended, started } = event; |
new above
offline