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 #240
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/225-a-moderator-can-delete-a-chat-line-over → main 6 commits fork
6 files
changed +148
−27
Software/README.md +7
| @@ -421,6 +421,13 @@ its live pose and the cameras. Driving needs a name and a turn. | ||
| 421 | 421 | any time (this skips the queue), end anyone's turn, take people out of |
| 422 | 422 | the queue, delete messages, mute people and ban them (long-press or |
| 423 | 423 | right-click a chat line). |
| 424 | - Moderators (names the owner lists under `moderators` on `/admin`) can delete | |
| 425 | chat lines and do nothing else of the owner's. They must be signed in through | |
| 426 | the forge: a guest who picked a listed name is not one. In the page, long-press | |
| 427 | or right-click a chat line and choose **Delete message**. In the terminal, chat | |
| 428 | lines show their number (`#12 bob: …`) to moderators and the owner: `/delete | |
| 429 | <name>` removes that person's latest line, `/delete #12` that line. Everyone | |
| 430 | watching sees it go, and the admin log records who deleted it. | |
| 424 | 431 | The owner can also press **Stop** during anyone's turn. |
| 425 | 432 | - The red **STOP** button (or Esc) is the emergency stop. |
| 426 | 433 | The driver and the owner can press it. Only the owner can clear it, and |
Software/so101/src/lobby.test.ts +58
| @@ -278,6 +278,64 @@ 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 | expect((await bob.first((m) => m.type === "hello")).moderator).toBe(true); | |
| 289 | expect((await alice.first((m) => m.type === "hello")).moderator).toBe(false); | |
| 290 | alice.send({ type: "post", text: "hello" }); | |
| 291 | const said = await bob.next((m) => m.type === "said"); | |
| 292 | ||
| 293 | alice.send({ type: "delete", id: said.message.id }); | |
| 294 | expect((await alice.next((m) => m.type === "error")).error).toBe("only the owner can do that"); | |
| 295 | bob.send({ type: "settings", set: { paused: true } }); | |
| 296 | expect((await bob.next((m) => m.type === "error")).error).toBe("only the owner can do that"); | |
| 297 | bob.heard.length = 0; | |
| 298 | bob.send({ type: "pause", on: true }); | |
| 299 | expect((await bob.next((m) => m.type === "error")).error).toBe("only the owner can do that"); | |
| 300 | expect(admin.settings.paused).toBe(false); | |
| 301 | ||
| 302 | bob.send({ type: "delete", id: said.message.id }); | |
| 303 | expect((await alice.next((m) => m.type === "unsaid")).id).toBe(said.message.id); | |
| 304 | expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${said.message.id}` }); | |
| 305 | expect((await owner.next((m) => m.type === "admin" && m.log.at(-1).by.name === "bob")).log.at(-1).what).toContain("deleted chat line"); | |
| 306 | }); | |
| 307 | ||
| 308 | test("the owner's delete is logged with the owner as who did it, and a delete without an integer id is refused", async () => { | |
| 309 | const admin = new Admin(); | |
| 310 | const port = await start(undefined, undefined, admin); | |
| 311 | const alice = await join(port, ALICE); | |
| 312 | const owner = await join(port, OWNER); | |
| 313 | alice.send({ type: "post", text: "hello" }); | |
| 314 | const said = await owner.next((m) => m.type === "said"); | |
| 315 | owner.send({ type: "delete", id: said.message.id }); | |
| 316 | await alice.next((m) => m.type === "unsaid"); | |
| 317 | expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-owner", name: "trevato" }, what: `deleted chat line ${said.message.id}` }); | |
| 318 | for (const id of ["3", 1.5, null]) { | |
| 319 | owner.heard.length = 0; | |
| 320 | owner.send({ type: "delete", id }); | |
| 321 | expect((await owner.next((m) => m.type === "error")).error).toBe("delete which line?"); | |
| 322 | } | |
| 323 | expect(admin.log.length).toBe(1); | |
| 324 | }); | |
| 325 | ||
| 326 | test("a guest who picked a moderator's name is not a moderator", async () => { | |
| 327 | const admin = new Admin(); | |
| 328 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["zed"] }); | |
| 329 | const port = await start(undefined, undefined, admin); | |
| 330 | const alice = await join(port, ALICE); | |
| 331 | alice.send({ type: "post", text: "hi" }); | |
| 332 | const said = await alice.next((m) => m.type === "said"); | |
| 333 | const guest = await join(port); | |
| 334 | guest.send({ type: "delete", id: said.message.id }); | |
| 335 | expect((await guest.next((m) => m.type === "error")).error).toBeTruthy(); | |
| 336 | expect(admin.log.length).toBe(1); | |
| 337 | }); | |
| 338 | ||
| 281 | 339 | test("chat history is lazy: a new page gets the last lines, anyone asks for older pages, oldest first", async () => { |
| 282 | 340 | const chat = new Chat(); |
| 283 | 341 | for (let i = 0; i < 100; i++) chat.system(`line ${i}`); |
Software/so101/src/lobby.ts +21 −9
| @@ -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 |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused |
| 23 | 23 | * {"type": "pong"} the answer to {"type": "ping"} |
| 24 | 24 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 25 | * Server → client: hello (who you are), state (the turn, the line, pause, the announcement and the speed cap), | |
| 25 | * 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), | |
| 26 | 26 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 27 | 27 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 28 | 28 | * settings, hidden lines, mute suggestions), banned and words (owners only), ping, error; and whatever the relay |
| @@ -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 | |
| @@ -188,6 +190,7 @@ export class Lobby { | ||
| 188 | 190 | } |
| 189 | 191 | this.broadcastState(); |
| 190 | 192 | this.broadcastAdmin(); |
| 193 | if (changed.includes("moderators")) for (const s of this.seats) if (s.who && !s.who.owner) this.send(s, this.hello(s)); | |
| 191 | 194 | if (changed.length) for (const listener of this.changeListeners) listener(); |
| 192 | 195 | } |
| 193 | 196 | |
| @@ -232,7 +235,8 @@ export class Lobby { | ||
| 232 | 235 | upgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void { |
| 233 | 236 | const who = this.who(req); |
| 234 | 237 | const from = visitor(req); |
| 235 | this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from)); | |
| 238 | const verified = Boolean(who) && this.verified(req)?.id === who?.id; | |
| 239 | this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from, verified)); | |
| 236 | 240 | } |
| 237 | 241 | |
| 238 | 242 | close(): void { |
| @@ -244,8 +248,8 @@ export class Lobby { | ||
| 244 | 248 | } |
| 245 | 249 | |
| 246 | 250 | // ------------------------------------------------------------------ sockets |
| 247 | private seat(ws: WebSocket, who: Who, from: Visitor): void { | |
| 248 | const seat: Seat = { ws, who, from, heard: Date.now() }; | |
| 251 | private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void { | |
| 252 | const seat: Seat = { ws, who, from, verified, heard: Date.now() }; | |
| 249 | 253 | this.seats.add(seat); |
| 250 | 254 | if (who) this.turns.connect(who.id); |
| 251 | 255 | this.send(seat, this.hello(seat)); |
| @@ -314,6 +318,7 @@ export class Lobby { | ||
| 314 | 318 | return; |
| 315 | 319 | } |
| 316 | 320 | } |
| 321 | if (!who.owner && m.type === "delete" && seat.verified && this.admin.isModerator(who.name)) return this.deleteLine(who, m.id); | |
| 317 | 322 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); |
| 318 | 323 | switch (m.type) { |
| 319 | 324 | case "end": |
| @@ -328,9 +333,7 @@ export class Lobby { | ||
| 328 | 333 | case "settings": |
| 329 | 334 | return this.configure(who, m.set); |
| 330 | 335 | 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; | |
| 336 | return this.deleteLine(who, m.id); | |
| 334 | 337 | case "words": { |
| 335 | 338 | const words = this.filter.set(m.words); |
| 336 | 339 | this.admin.record(who, `word filter → ${words.length} word${words.length === 1 ? "" : "s"}`); |
| @@ -398,6 +401,15 @@ export class Lobby { | ||
| 398 | 401 | this.send(seat, { type: "error", error: "unknown request" }); |
| 399 | 402 | } |
| 400 | 403 | |
| 404 | /** Delete a chat line as the owner or a moderator; the admin log says who. */ | |
| 405 | private deleteLine(who: Person, id: unknown): void { | |
| 406 | if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?"); | |
| 407 | if (!this.chat.delete(id as number)) return; | |
| 408 | this.admin.record(who, `deleted chat line ${id}`); | |
| 409 | this.broadcast({ type: "unsaid", id }); | |
| 410 | this.broadcastAdmin(); | |
| 411 | } | |
| 412 | ||
| 401 | 413 | private turnChanged(event: TurnEvent): void { |
| 402 | 414 | if (event.kind === "turn") { |
| 403 | 415 | const { ended, started } = event; |
| @@ -437,7 +449,7 @@ export class Lobby { | ||
| 437 | 449 | |
| 438 | 450 | private hello(seat: Seat) { |
| 439 | 451 | const who = seat.who; |
| 440 | return { type: "hello", you: who, signin: !this.local, muted: who ? this.chat.muted.has(who.id) : false, banned: who ? this.barred(who, seat.from) : false }; | |
| 452 | return { type: "hello", you: who, moderator: Boolean(who && !who.owner && seat.verified && this.admin.isModerator(who.name)), signin: !this.local, muted: who ? this.chat.muted.has(who.id) : false, banned: who ? this.barred(who, seat.from) : false }; | |
| 441 | 453 | } |
| 442 | 454 | |
| 443 | 455 | private bannedList() { |
Software/so101/src/terminal.test.ts +30 −1
| @@ -82,6 +82,15 @@ describe("terminal lines", () => { | ||
| 82 | 82 | expect(typed(room, "/unmute u-zed")).toEqual({ send: { type: "mute", id: "u-zed", on: false } }); |
| 83 | 83 | expect(typed(room, "/mute")).toEqual({ print: "usage: /mute <name>" }); |
| 84 | 84 | expect(typed(room, "/mute a.b!")).toEqual({ print: "error: who is a.b!?" }); |
| 85 | hear(room, { type: "said", message: { id: 7, from: { id: "u-alice", name: "alice" }, text: "hi" } }); | |
| 86 | expect(typed(room, "/delete Alice")).toEqual({ send: { type: "delete", id: 7 } }); | |
| 87 | expect(typed(room, "/delete #12")).toEqual({ send: { type: "delete", id: 12 } }); | |
| 88 | expect(typed(room, "/delete")).toEqual({ print: "usage: /delete <name> | /delete #<line number>" }); | |
| 89 | expect(typed(room, "/delete bob")).toEqual({ print: "error: no line from bob seen" }); | |
| 90 | expect(hear(emptyRoom(), { type: "said", message: { id: 7, from: { id: "u-alice", name: "alice" }, text: "hi" } })).toEqual(["alice: hi"]); | |
| 91 | const mod = emptyRoom(); | |
| 92 | hear(mod, { type: "hello", you: { id: "u-bob", name: "bob" }, moderator: true }); | |
| 93 | expect(hear(mod, { type: "said", message: { id: 7, from: { id: "u-alice", name: "alice" }, text: "hi" } })).toEqual(["#7 alice: hi"]); | |
| 85 | 94 | expect(typed(room, "/who")).toEqual({ print: "alice has control · 1. bob" }); |
| 86 | 95 | expect(typed(room, "/quit")).toEqual({ quit: true }); |
| 87 | 96 | expect(typed(room, " ")).toBeNull(); |
| @@ -218,7 +227,7 @@ describe("terminal door", () => { | ||
| 218 | 227 | const port = await start(); |
| 219 | 228 | const page = await viewer(port); |
| 220 | 229 | const alice = await terminal(port, ttyEnv("u-alice", "alice", "user")); |
| 221 | expect(alice.door.room.you).toEqual({ id: "u-alice", name: "alice", owner: false }); | |
| 230 | expect(alice.door.room.you).toEqual({ id: "u-alice", name: "alice", owner: false, moderator: false }); | |
| 222 | 231 | alice.door.input("hi from ssh <b>bold</b>"); |
| 223 | 232 | const m = await page.heardOne((m) => m.type === "said" && m.message.from?.id === "u-alice", "terminal line on the page"); |
| 224 | 233 | expect(m.message).toMatchObject({ from: { id: "u-alice", name: "alice" }, text: "hi from ssh <b>bold</b>" }); |
| @@ -337,6 +346,26 @@ describe("terminal door", () => { | ||
| 337 | 346 | await alicePage.heardOne((m) => m.type === "said" && m.message.text === "thanks", "alice's line once unmuted"); |
| 338 | 347 | }); |
| 339 | 348 | |
| 349 | test("delete: a moderator's terminal removes a line and web viewers hear unsaid; a plain user's is refused", async () => { | |
| 350 | const port = await start(); | |
| 351 | lobby!.admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); | |
| 352 | const page = await viewer(port); | |
| 353 | const alicePage = await viewer(port, web("u-alice", "alice")); | |
| 354 | alicePage.send({ type: "post", text: "one" }); | |
| 355 | const one = await page.heardOne((m) => m.type === "said" && m.message.text === "one", "the line"); | |
| 356 | const bob = await terminal(port, ttyEnv("u-bob", "bob", "user")); | |
| 357 | const carol = await terminal(port, ttyEnv("u-carol", "carol", "user")); | |
| 358 | await bob.printedLine(/^you are bob \(moderator\)$/); | |
| 359 | await bob.printedLine(new RegExp(`^#${one.message.id} alice: one$`)); | |
| 360 | await carol.printedLine(/^alice: one$/); | |
| 361 | ||
| 362 | carol.door.input("/delete alice"); | |
| 363 | await carol.printedLine(/^error: only the owner can do that$/); | |
| 364 | bob.door.input("/delete alice"); | |
| 365 | expect((await page.heardOne((m) => m.type === "unsaid", "the line going")).id).toBe(one.message.id); | |
| 366 | expect(lobby!.admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${one.message.id}` }); | |
| 367 | }); | |
| 368 | ||
| 340 | 369 | test("end-turn: only the owner's terminal may; the page sees the turn pass on", async () => { |
| 341 | 370 | const port = await start(); |
| 342 | 371 | const alicePage = await viewer(port, web("u-alice", "alice")); |
Software/so101/src/terminal.ts +23 −10
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | * /resume the driver or an admin: end the hold; the arm stays put until a new command |
| 22 | 22 | * /end [who] owner: end that turn (none: whoever drives), or take them out of the line |
| 23 | 23 | * /mute <who> /unmute <who> owner |
| 24 | * /delete <who|#id> owner or moderator: remove that person's latest chat line, or the line numbered #id (shown to those who may) | |
| 24 | 25 | * /table here admin: put the table under the arm's lowest link now (POST /settings/table-here) |
| 25 | 26 | * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm) |
| 26 | 27 | * /machine [off] the Machine panel (/machine, src/machine-feed.ts): the joints, the link and the server as they are, |
| @@ -96,7 +97,7 @@ interface Someone { | ||
| 96 | 97 | |
| 97 | 98 | /** What the door knows of the room, from what the server told it. */ |
| 98 | 99 | export interface Room { |
| 99 | you: (Someone & { owner: boolean }) | null; | |
| 100 | you: (Someone & { owner: boolean; moderator: boolean }) | null; | |
| 100 | 101 | muted: boolean; |
| 101 | 102 | banned: boolean; |
| 102 | 103 | driver: Someone | null; |
| @@ -106,11 +107,13 @@ export interface Room { | ||
| 106 | 107 | held: string | null | undefined; |
| 107 | 108 | /** Whether the call to confirm, and the "you are next" cue, were already printed (each is said once). */ |
| 108 | 109 | told: { called: boolean; next: boolean }; |
| 110 | /** The latest chat line of each author, by user id: what `/delete <who>` removes. */ | |
| 111 | latest: Map<string, number>; | |
| 109 | 112 | /** Everyone seen by name, most recent last: to turn a typed name into an id. */ |
| 110 | 113 | seen: Map<string, Someone>; |
| 111 | 114 | } |
| 112 | 115 | |
| 113 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map() }); | |
| 116 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map() }); | |
| 114 | 117 | |
| 115 | 118 | const someone = (x: unknown): Someone | null => { |
| 116 | 119 | if (typeof x !== "object" || x === null) return null; |
| @@ -124,13 +127,17 @@ function see(room: Room, who: Someone | null): void { | ||
| 124 | 127 | room.seen.set(who.name.toLowerCase(), who); |
| 125 | 128 | } |
| 126 | 129 | |
| 127 | const said = (m: unknown): string => { | |
| 128 | const message = m as { from?: unknown; text?: unknown } | null; | |
| 130 | /** One chat line; those who may delete see its number, and the author's latest line is remembered for /delete. */ | |
| 131 | const said = (room: Room, m: unknown): string => { | |
| 132 | const message = m as { id?: unknown; from?: unknown; text?: unknown } | null; | |
| 129 | 133 | const from = someone(message?.from); |
| 130 | return from ? `${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; | |
| 134 | const id = typeof message?.id === "number" && Number.isSafeInteger(message.id) ? message.id : null; | |
| 135 | if (from && id !== null) room.latest.set(from.id, id); | |
| 136 | const tag = id !== null && (room.you?.owner || room.you?.moderator) ? `#${id} ` : ""; | |
| 137 | return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; | |
| 131 | 138 | }; |
| 132 | 139 | |
| 133 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /machine [off] · /who · /quit"; | |
| 140 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /table here|<mm> (owner) · /delete <who|#id> (owner, moderator) · /machine [off] · /who · /quit"; | |
| 134 | 141 | |
| 135 | 142 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 136 | 143 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -181,12 +188,12 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 181 | 188 | case "hello": { |
| 182 | 189 | const you = someone(frame.you); |
| 183 | 190 | const was = { you: room.you, muted: room.muted, banned: room.banned }; |
| 184 | room.you = you ? { ...you, owner: (frame.you as { owner?: unknown }).owner === true } : null; | |
| 191 | room.you = you ? { ...you, owner: (frame.you as { owner?: unknown }).owner === true, moderator: frame.moderator === true } : null; | |
| 185 | 192 | room.muted = frame.muted === true; |
| 186 | 193 | room.banned = frame.banned === true; |
| 187 | 194 | see(room, you); |
| 188 | 195 | const out: string[] = []; |
| 189 | if (!was.you && room.you) out.push(`you are ${plain(room.you.name)}${room.you.owner ? " (owner)" : ""}`); | |
| 196 | if (!was.you && room.you) out.push(`you are ${plain(room.you.name)}${room.you.owner ? " (owner)" : room.you.moderator ? " (moderator)" : ""}`); | |
| 190 | 197 | if (!room.you && frame.signin === true) out.push("you are watching: connect with a key your forge account knows to chat and drive"); |
| 191 | 198 | if (room.muted !== was.muted && (room.muted || was.you)) out.push(room.muted ? "you are muted" : "you may chat again"); |
| 192 | 199 | if (room.banned && !was.banned) out.push("you are banned: you can still watch"); |
| @@ -203,11 +210,11 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 203 | 210 | case "chat": { |
| 204 | 211 | const messages = Array.isArray(frame.messages) ? frame.messages : []; |
| 205 | 212 | for (const m of messages) see(room, someone((m as { from?: unknown } | null)?.from)); |
| 206 | return messages.slice(-10).map(said); | |
| 213 | return messages.slice(-10).map((m) => said(room, m)); | |
| 207 | 214 | } |
| 208 | 215 | case "said": |
| 209 | 216 | see(room, someone((frame.message as { from?: unknown } | null)?.from)); |
| 210 | return [said(frame.message)]; | |
| 217 | return [said(room, frame.message)]; | |
| 211 | 218 | case "status": { |
| 212 | 219 | // Said when it changes, not on every push: a stop made here or on a page reaches every terminal once. |
| 213 | 220 | const h = (frame.status as { held?: unknown } | null)?.held as { by?: unknown; at?: unknown } | null | undefined; |
| @@ -266,6 +273,12 @@ export function typed(room: Room, input: string): Typed { | ||
| 266 | 273 | const id = target(room, arg); |
| 267 | 274 | return id ? { send: { type: "mute", id, on: verb.toLowerCase() === "mute" } } : { print: `error: who is ${plain(arg)}?` }; |
| 268 | 275 | } |
| 276 | case "delete": { | |
| 277 | if (!arg) return { print: "usage: /delete <name> | /delete #<line number>" }; | |
| 278 | const number = /^#(\d{1,15})$/.exec(arg); | |
| 279 | const id = number ? Number(number[1]) : room.latest.get(target(room, arg) ?? ""); | |
| 280 | return id === undefined ? { print: `error: no line from ${plain(arg)} seen` } : { send: { type: "delete", id } }; | |
| 281 | } | |
| 269 | 282 | case "stop": |
| 270 | 283 | case "resume": |
| 271 | 284 | return { post: `/${verb.toLowerCase()}` as "/stop" | "/resume" }; |
Software/so101/src/ui/lobby-panel.js +9 −7
| @@ -33,6 +33,8 @@ export function createRoom(page) { | ||
| 33 | 33 | const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null, statusSeen: false }; |
| 34 | 34 | function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); } |
| 35 | 35 | function isOwner() { return !relayed || Boolean(lobby.hello?.you?.owner); } |
| 36 | /** The owner, or a forge-verified moderator: who may delete a chat line (the server checks again). */ | |
| 37 | function canDelete() { return Boolean(lobby.hello?.you?.owner || lobby.hello?.moderator); } | |
| 36 | 38 | const say = (/** @type {Record<string, unknown>} */ m) => { if (lobby.ws?.readyState === 1) lobby.ws.send(JSON.stringify(m)); }; |
| 37 | 39 | const serverNow = () => Date.now() + lobby.offset; |
| 38 | 40 | function tickClock() { |
| @@ -131,7 +133,7 @@ export function createRoom(page) { | ||
| 131 | 133 | renderCrew(me); |
| 132 | 134 | $("say").hidden = !me || lobby.hello.muted || lobby.hello.banned; |
| 133 | 135 | $("sayNote").textContent = !lobby.hello.signin ? "" : !me ? "Join (Queue tab) to chat." : lobby.hello.banned ? "You are banned: you can still watch." : lobby.hello.muted ? "The owner has muted you." : ""; |
| 134 | chatBox.classList.toggle("moderated", Boolean(me?.owner)); | |
| 136 | chatBox.classList.toggle("moderated", canDelete()); | |
| 135 | 137 | tickClock(); |
| 136 | 138 | page.applyLock(); |
| 137 | 139 | } |
| @@ -174,7 +176,7 @@ export function createRoom(page) { | ||
| 174 | 176 | line.dataset.id = m.id; |
| 175 | 177 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); } |
| 176 | 178 | line.append(el("span", { textContent: m.text })); |
| 177 | if (m.from && me?.owner) line.tabIndex = 0; | |
| 179 | if (m.from && canDelete()) line.tabIndex = 0; | |
| 178 | 180 | return line; |
| 179 | 181 | } |
| 180 | 182 | // A new page gets the last lines of the chat; older ones come a page at a time, when asked for. |
| @@ -203,31 +205,31 @@ export function createRoom(page) { | ||
| 203 | 205 | if (atEnd || replace) chatBox.scrollTop = chatBox.scrollHeight; |
| 204 | 206 | if (!replace && page.tab() !== "chat") $("unread").textContent = String(Math.min(99, Number($("unread").textContent || 0) + lines.length)); |
| 205 | 207 | } |
| 206 | // Moderation on a phone: long-press a line (or right-click it, or Enter on it) for delete and mute. | |
| 208 | // Moderation on a phone: long-press a line (or right-click it, or Enter on it) for delete (and mute and ban, for the owner). | |
| 207 | 209 | { |
| 208 | 210 | /** @type {any} */ |
| 209 | 211 | let press = null; |
| 210 | 212 | /** @type {any} */ |
| 211 | 213 | let held = null; |
| 212 | 214 | const open = (/** @type {any} */ line) => { |
| 213 | if (!lobby.hello?.you?.owner || !line?.dataset.from) return; | |
| 215 | if (!canDelete() || !line?.dataset.from) return; | |
| 214 | 216 | held = line; line.classList.add("held"); |
| 215 | 217 | $("modTitle").textContent = `${line.dataset.name} said`; |
| 216 | 218 | $("modText").textContent = line.querySelector("span")?.textContent ?? ""; |
| 217 | 219 | $("modMute").textContent = `Mute ${line.dataset.name}`; |
| 218 | 220 | $("modBan").textContent = `Ban ${line.dataset.name}`; |
| 219 | $("modMute").hidden = $("modBan").hidden = line.dataset.from === lobby.hello.you.id; | |
| 221 | $("modMute").hidden = $("modBan").hidden = !lobby.hello.you.owner || line.dataset.from === lobby.hello.you.id; | |
| 220 | 222 | $("modDlg").showModal(); |
| 221 | 223 | nav.vibrate?.(10); |
| 222 | 224 | }; |
| 223 | 225 | chatBox.addEventListener("pointerdown", (/** @type {any} */ e) => { |
| 224 | const line = e.target.closest(".line"); if (!line?.dataset.from || !lobby.hello?.you?.owner) return; | |
| 226 | const line = e.target.closest(".line"); if (!line?.dataset.from || !canDelete()) return; | |
| 225 | 227 | press = { x: e.clientX, y: e.clientY, timer: setTimeout(() => { press = null; open(line); }, 480) }; |
| 226 | 228 | }); |
| 227 | 229 | const cancel = () => { if (press) { clearTimeout(press.timer); press = null; } }; |
| 228 | 230 | chatBox.addEventListener("pointermove", (/** @type {any} */ e) => { if (press && Math.hypot(e.clientX - press.x, e.clientY - press.y) > 10) cancel(); }); |
| 229 | 231 | for (const ev of ["pointerup", "pointercancel", "scroll"]) chatBox.addEventListener(ev, cancel); |
| 230 | chatBox.addEventListener("contextmenu", (/** @type {any} */ e) => { const line = e.target.closest(".line"); if (line?.dataset.from && lobby.hello?.you?.owner) { e.preventDefault(); cancel(); open(line); } }); | |
| 232 | chatBox.addEventListener("contextmenu", (/** @type {any} */ e) => { const line = e.target.closest(".line"); if (line?.dataset.from && canDelete()) { e.preventDefault(); cancel(); open(line); } }); | |
| 231 | 233 | chatBox.addEventListener("keydown", (/** @type {any} */ e) => { if (e.key === "Enter" && e.target.classList?.contains("line")) open(e.target); }); |
| 232 | 234 | $("modDlg").addEventListener("close", () => { |
| 233 | 235 | const line = held; held = null; line?.classList.remove("held"); |
new above
offline