Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Moderators, not only the owner, can remove messages, mute and ban over /live - server-checked #219
smith/arm:smith/190-moderators-not-only-the-owner-can-remove → main 4 commits fork
7 files
changed +129
−21
Software/README.md +8
| @@ -524,6 +524,14 @@ or from the **Admin** tab that only the owner sees: | ||
| 524 | 524 | `/admin` and read the settings and the log, and change nothing. A moderator |
| 525 | 525 | is matched on the forge-signed identity (the forge username), never on a |
| 526 | 526 | name a guest typed: a guest who picks a moderator's name still gets a 403. |
| 527 | A signed-in moderator can also **delete a chat line, mute and ban** in the | |
| 528 | room, from the page (long-press, right-click or Enter on a line, the same | |
| 529 | menu the owner has; the server tells the page it moderates in `hello`) and | |
| 530 | at the terminal (`/mute`, `/unmute`, `/ban`, `/unban`). The server checks | |
| 531 | the seat and re-reads the moderator list on each action; each one is | |
| 532 | logged under the moderator's name. A moderator cannot mute or ban the owner | |
| 533 | or another moderator, and everything else (pause, settings, words, crew, | |
| 534 | ending turns) stays the owner's. Deleting a line has no terminal command yet. | |
| 527 | 535 | Settings are web-only because this app has no terminal (`forge.json` has no |
| 528 | 536 | `tty`); the `so101` command line drives the arm hardware and does not run |
| 529 | 537 | the relay. |
Software/so101/src/lobby.test.ts +51
| @@ -278,6 +278,57 @@ describe("lobby", () => { | ||
| 278 | 278 | expect((await late.first((m) => m.type === "chat")).messages).toEqual([]); |
| 279 | 279 | }); |
| 280 | 280 | |
| 281 | test("moderators named on /admin delete, mute and ban over /live, logged; plain users and moderators against the owner are refused", async () => { | |
| 282 | const admin = new Admin(); | |
| 283 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["carol", "bob"] }); | |
| 284 | const port = await start(undefined, undefined, admin); | |
| 285 | const owner = await join(port, OWNER); | |
| 286 | const carol = await join(port, CAROL); | |
| 287 | const bob = await join(port, BOB); | |
| 288 | const alice = await join(port, { ...ALICE, "x-forwarded-for": "198.51.100.9" }); // its own address: a ban reaches everyone at one | |
| 289 | expect((await carol.first((m) => m.type === "muted")).muted).toEqual([]); | |
| 290 | expect((await carol.first((m) => m.type === "banned")).banned).toEqual([]); | |
| 291 | ||
| 292 | alice.send({ type: "post", text: "hi" }); | |
| 293 | const said = await carol.next((m) => m.type === "said"); | |
| 294 | alice.send({ type: "mute", id: "u-carol", on: true }); | |
| 295 | expect((await alice.next((m) => m.type === "error")).error).toContain("only the owner"); | |
| 296 | alice.send({ type: "delete", id: said.message.id }); | |
| 297 | expect((await alice.next((m) => m.type === "error" && m !== undefined && m.error.includes("owner"))).error).toContain("only the owner"); | |
| 298 | ||
| 299 | carol.send({ type: "delete", id: said.message.id }); | |
| 300 | expect((await alice.next((m) => m.type === "unsaid")).id).toBe(said.message.id); | |
| 301 | carol.send({ type: "mute", id: "u-alice", on: true }); | |
| 302 | expect((await alice.next((m) => m.type === "hello")).muted).toBe(true); | |
| 303 | expect((await carol.next((m) => m.type === "muted" && m.muted.length === 1)).muted).toEqual([{ id: "u-alice", name: "alice" }]); | |
| 304 | carol.send({ type: "ban", id: "u-alice", on: true }); | |
| 305 | expect((await carol.next((m) => m.type === "banned" && m.banned.length === 1)).banned[0]).toMatchObject({ id: "u-alice", by: "carol" }); | |
| 306 | carol.send({ type: "ban", id: "u-alice", on: false }); | |
| 307 | await carol.next((m) => m.type === "banned" && m.banned.length === 0); | |
| 308 | carol.send({ type: "mute", id: "u-alice", on: false }); | |
| 309 | await carol.next((m) => m.type === "muted" && m.muted.length === 0); | |
| 310 | const log = (await owner.first((m) => m.type === "admin" && m.log.some((e: Msg) => e.what === "unmuted alice"))).log.map((e: Msg) => `${e.by.name}: ${e.what}`); | |
| 311 | expect(log).toContain("carol: deleted line " + said.message.id); | |
| 312 | expect(log).toContain("carol: banned alice"); | |
| 313 | ||
| 314 | carol.send({ type: "ban", id: "u-owner", on: true }); | |
| 315 | expect((await carol.next((m) => m.type === "error")).error).toContain("owner"); | |
| 316 | carol.send({ type: "mute", id: "u-owner", on: true }); | |
| 317 | expect((await carol.next((m) => m.type === "error" && m.error.includes("moderators cannot mute"))).error).toContain("owner or another moderator"); | |
| 318 | carol.send({ type: "mute", id: "u-bob", on: true }); | |
| 319 | expect((await carol.next((m) => m.type === "error" && m.error.includes("moderators cannot mute"))).error).toContain("another moderator"); | |
| 320 | carol.send({ type: "ban", id: "u-bob", on: true }); | |
| 321 | expect((await carol.next((m) => m.type === "error" && m.error.includes("moderators cannot ban"))).error).toContain("another moderator"); | |
| 322 | carol.send({ type: "pause", on: true }); | |
| 323 | expect((await carol.next((m) => m.type === "error" && m.error.includes("only the owner"))).error).toContain("only the owner"); | |
| 324 | bob.send({ type: "ban", id: "u-owner", on: true }); | |
| 325 | expect((await bob.next((m) => m.type === "error")).error).toContain("owner"); | |
| 326 | // hello tells the page it moderates: a moderator and not the owner, nobody else | |
| 327 | expect((await carol.first((m) => m.type === "hello")).moderator).toBe(true); | |
| 328 | expect((await owner.first((m) => m.type === "hello")).moderator).toBeUndefined(); | |
| 329 | expect((await alice.first((m) => m.type === "hello")).moderator).toBeUndefined(); | |
| 330 | }); | |
| 331 | ||
| 281 | 332 | test("chat history is lazy: a new page gets the last lines, anyone asks for older pages, oldest first", async () => { |
| 282 | 333 | const chat = new Chat(); |
| 283 | 334 | for (let i = 0; i < 100; i++) chat.system(`line ${i}`); |
Software/so101/src/lobby.ts +39 −12
| @@ -11,14 +11,14 @@ | ||
| 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 | |
| 15 | * {"type": "mute", "id": "<user id>", "on": true|false} owner | |
| 14 | * {"type": "delete", "id": 12} owner or moderator: delete a chat line | |
| 15 | * {"type": "mute", "id": "<user id>", "on": true|false} owner or 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 |
| 19 | 19 | * {"type": "restore", "id": 12} owner: put back a line the crew hid |
| 20 | 20 | * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion |
| 21 | * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch | |
| 21 | * {"type": "ban", "id": "<user id>", "on": true|false} owner or moderator: bars their account, name and address; they still watch | |
| 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 |
| @@ -70,9 +70,13 @@ interface Seat { | ||
| 70 | 70 | who: Who; |
| 71 | 71 | from: Visitor; |
| 72 | 72 | heard: number; |
| 73 | /** Signed in through the forge: only such a seat can be a moderator. */ | |
| 74 | verified: boolean; | |
| 73 | 75 | } |
| 74 | 76 | |
| 75 | 77 | const MAX_FRAME = 4096; |
| 78 | /** What a moderator may send besides the owner's own: the rest stays the owner's. */ | |
| 79 | const MODERATOR_ACTIONS = new Set(["delete", "mute", "ban"]); | |
| 76 | 80 | /** Lines of chat a new page is sent: enough to fill a phone's chat, little enough for a slow link. */ |
| 77 | 81 | export const CHAT_FIRST = 30; |
| 78 | 82 | /** Lines per "history" answer, when someone scrolls up for more. */ |
| @@ -232,7 +236,8 @@ export class Lobby { | ||
| 232 | 236 | upgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void { |
| 233 | 237 | const who = this.who(req); |
| 234 | 238 | const from = visitor(req); |
| 235 | this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from)); | |
| 239 | const verified = this.verified(req) !== null; | |
| 240 | this.wss.handleUpgrade(req, socket, head, (ws) => this.seat(ws, who, from, verified)); | |
| 236 | 241 | } |
| 237 | 242 | |
| 238 | 243 | close(): void { |
| @@ -244,16 +249,18 @@ export class Lobby { | ||
| 244 | 249 | } |
| 245 | 250 | |
| 246 | 251 | // ------------------------------------------------------------------ sockets |
| 247 | private seat(ws: WebSocket, who: Who, from: Visitor): void { | |
| 248 | const seat: Seat = { ws, who, from, heard: Date.now() }; | |
| 252 | private seat(ws: WebSocket, who: Who, from: Visitor, verified: boolean): void { | |
| 253 | const seat: Seat = { ws, who, from, heard: Date.now(), verified }; | |
| 249 | 254 | this.seats.add(seat); |
| 250 | 255 | if (who) this.turns.connect(who.id); |
| 251 | 256 | this.send(seat, this.hello(seat)); |
| 252 | 257 | this.send(seat, this.state()); |
| 253 | 258 | this.send(seat, this.chatLog()); |
| 254 | if (who?.owner) { | |
| 259 | if (this.moderates(seat)) { | |
| 255 | 260 | this.send(seat, this.mutedList()); |
| 256 | 261 | this.send(seat, this.bannedList()); |
| 262 | } | |
| 263 | if (who?.owner) { | |
| 257 | 264 | this.send(seat, this.wordsList()); |
| 258 | 265 | this.send(seat, this.adminState()); |
| 259 | 266 | this.send(seat, this.crewState()); |
| @@ -314,7 +321,7 @@ export class Lobby { | ||
| 314 | 321 | return; |
| 315 | 322 | } |
| 316 | 323 | } |
| 317 | if (!who.owner) return this.send(seat, { type: "error", error: "only the owner can do that" }); | |
| 324 | if (!who.owner && !(this.moderates(seat) && MODERATOR_ACTIONS.has(String(m.type)))) return this.send(seat, { type: "error", error: "only the owner can do that" }); | |
| 318 | 325 | switch (m.type) { |
| 319 | 326 | case "end": |
| 320 | 327 | if (typeof m.id !== "string" || !USER_ID.test(m.id)) return this.send(seat, { type: "error", error: "end whom?" }); |
| @@ -329,7 +336,11 @@ export class Lobby { | ||
| 329 | 336 | return this.configure(who, m.set); |
| 330 | 337 | case "delete": |
| 331 | 338 | 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 }); | |
| 339 | if (this.chat.delete(m.id as number)) { | |
| 340 | this.broadcast({ type: "unsaid", id: m.id }); | |
| 341 | this.admin.record(who, `deleted line ${m.id}`); | |
| 342 | this.broadcastAdmin(); | |
| 343 | } | |
| 333 | 344 | return; |
| 334 | 345 | case "words": { |
| 335 | 346 | const words = this.filter.set(m.words); |
| @@ -341,12 +352,20 @@ export class Lobby { | ||
| 341 | 352 | case "mute": { |
| 342 | 353 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
| 343 | 354 | const id = m.id; |
| 355 | if (m.on && !who.owner) { | |
| 356 | const seated = [...this.seats].find((s) => s.who?.id === id)?.who; | |
| 357 | const said = [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from; | |
| 358 | const target = seated ?? said; | |
| 359 | if (seated?.owner || (target && this.admin.isModerator(target.name))) return this.send(seat, { type: "error", error: "moderators cannot mute the owner or another moderator" }); | |
| 360 | } | |
| 344 | 361 | 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; |
| 345 | 362 | this.chat.mute(id, name, m.on); |
| 346 | 363 | for (const s of this.seats) { |
| 347 | 364 | if (s.who?.id === id) this.send(s, this.hello(s)); |
| 348 | if (s.who?.owner) this.send(s, this.mutedList()); | |
| 365 | if (this.moderates(s)) this.send(s, this.mutedList()); | |
| 349 | 366 | } |
| 367 | this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`); | |
| 368 | this.broadcastAdmin(); | |
| 350 | 369 | this.moderator?.muted(id); |
| 351 | 370 | return; |
| 352 | 371 | } |
| @@ -360,6 +379,7 @@ export class Lobby { | ||
| 360 | 379 | const person = known ?? (said ? { id, name: said.name, owner: false } : null); |
| 361 | 380 | if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" }); |
| 362 | 381 | if (person.owner) return this.send(seat, { type: "error", error: "the owner cannot be banned" }); |
| 382 | if (!who.owner && this.admin.isModerator(person.name)) return this.send(seat, { type: "error", error: "moderators cannot ban another moderator" }); | |
| 363 | 383 | this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name); |
| 364 | 384 | this.turns.end(id); // their turn ends, or they leave the line |
| 365 | 385 | this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) }); |
| @@ -372,7 +392,7 @@ export class Lobby { | ||
| 372 | 392 | this.broadcastAdmin(); |
| 373 | 393 | for (const s of this.seats) { |
| 374 | 394 | if (s.who && !s.who.owner) this.send(s, this.hello(s)); // an address ban reaches everyone there |
| 375 | if (s.who?.owner) this.send(s, this.bannedList()); | |
| 395 | if (this.moderates(s)) this.send(s, this.bannedList()); | |
| 376 | 396 | } |
| 377 | 397 | return; |
| 378 | 398 | } |
| @@ -437,7 +457,14 @@ export class Lobby { | ||
| 437 | 457 | |
| 438 | 458 | private hello(seat: Seat) { |
| 439 | 459 | 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 }; | |
| 460 | const moderator = !!who && !who.owner && this.moderates(seat); | |
| 461 | 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, ...(moderator ? { moderator } : {}) }; | |
| 462 | } | |
| 463 | ||
| 464 | /** Whether this seat may delete, mute and ban: the owner, or a forge-verified moderator named on /admin. */ | |
| 465 | private moderates(seat: Seat): boolean { | |
| 466 | const who = seat.who; | |
| 467 | return !!who && (who.owner || (seat.verified && this.admin.isModerator(who.name))); | |
| 441 | 468 | } |
| 442 | 469 | |
| 443 | 470 | private bannedList() { |
Software/so101/src/terminal.test.ts +3
| @@ -80,6 +80,9 @@ describe("terminal lines", () => { | ||
| 80 | 80 | expect(typed(room, "/end Bob")).toEqual({ send: { type: "end", id: "u-bob" } }); |
| 81 | 81 | expect(typed(room, "/mute alice")).toEqual({ send: { type: "mute", id: "u-alice", on: true } }); |
| 82 | 82 | expect(typed(room, "/unmute u-zed")).toEqual({ send: { type: "mute", id: "u-zed", on: false } }); |
| 83 | expect(typed(room, "/ban alice")).toEqual({ send: { type: "ban", id: "u-alice", on: true } }); | |
| 84 | expect(typed(room, "/unban u-zed")).toEqual({ send: { type: "ban", id: "u-zed", on: false } }); | |
| 85 | expect(typed(room, "/ban")).toEqual({ print: "usage: /ban <name>" }); | |
| 83 | 86 | expect(typed(room, "/mute")).toEqual({ print: "usage: /mute <name>" }); |
| 84 | 87 | expect(typed(room, "/mute a.b!")).toEqual({ print: "error: who is a.b!?" }); |
| 85 | 88 | expect(typed(room, "/who")).toEqual({ print: "alice has control · 1. bob" }); |
Software/so101/src/terminal.ts +9 −2
| @@ -20,7 +20,8 @@ | ||
| 20 | 20 | * /stop the driver or an admin: halt the arm and hold it, whatever drives it, until /resume |
| 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 | * /mute <who> /unmute <who> owner | |
| 23 | * /mute <who> /unmute <who> owner or moderator | |
| 24 | * /ban <who> /unban <who> owner or moderator | |
| 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, |
| @@ -130,7 +131,7 @@ const said = (m: unknown): string => { | ||
| 130 | 131 | return from ? `${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 131 | 132 | }; |
| 132 | 133 | |
| 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"; | |
| 134 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /mute <who> /unmute <who> /ban <who> /unban <who> (owner, moderator) · /table here|<mm> (owner) · /machine [off] · /who · /quit"; | |
| 134 | 135 | |
| 135 | 136 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 136 | 137 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -266,6 +267,12 @@ export function typed(room: Room, input: string): Typed { | ||
| 266 | 267 | const id = target(room, arg); |
| 267 | 268 | return id ? { send: { type: "mute", id, on: verb.toLowerCase() === "mute" } } : { print: `error: who is ${plain(arg)}?` }; |
| 268 | 269 | } |
| 270 | case "ban": | |
| 271 | case "unban": { | |
| 272 | if (!arg) return { print: `usage: /${verb.toLowerCase()} <name>` }; | |
| 273 | const id = target(room, arg); | |
| 274 | return id ? { send: { type: "ban", id, on: verb.toLowerCase() === "ban" } } : { print: `error: who is ${plain(arg)}?` }; | |
| 275 | } | |
| 269 | 276 | case "stop": |
| 270 | 277 | case "resume": |
| 271 | 278 | return { post: `/${verb.toLowerCase()}` as "/stop" | "/resume" }; |
Software/so101/src/ui/lobby-panel.js +9 −7
| @@ -31,6 +31,8 @@ export function createRoom(page) { | ||
| 31 | 31 | const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null, statusSeen: false }; |
| 32 | 32 | function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); } |
| 33 | 33 | function isOwner() { return !relayed || Boolean(lobby.hello?.you?.owner); } |
| 34 | /** The owner, or a moderator the server vouches for in hello: the chat-line menu and the unmute/unban lists. */ | |
| 35 | const moderates = () => Boolean(lobby.hello?.you?.owner || lobby.hello?.moderator); | |
| 34 | 36 | const say = (m) => { if (lobby.ws?.readyState === 1) lobby.ws.send(JSON.stringify(m)); }; |
| 35 | 37 | const serverNow = () => Date.now() + lobby.offset; |
| 36 | 38 | function tickClock() { |
| @@ -122,12 +124,12 @@ export function createRoom(page) { | ||
| 122 | 124 | $("pauseBtn").className = st.paused ? "go" : ""; |
| 123 | 125 | $("pauseBtn").onclick = () => say({ type: "pause", on: !st.paused }); |
| 124 | 126 | } |
| 125 | $("mutedLine").replaceChildren(...(me?.owner ? lobby.mutedList.map((m) => el("div", { className: "item" }, el("span", { textContent: `${m.name} is muted` }), button("Unmute", "", () => say({ type: "mute", id: m.id, on: false })))) : [])); | |
| 126 | $("bannedLine").replaceChildren(...(me?.owner ? lobby.bannedList.map((b) => el("div", { className: "item" }, el("span", { textContent: `${b.name} is banned` }), button("Unban", "", () => say({ type: "ban", id: b.id, on: false })))) : [])); | |
| 127 | $("mutedLine").replaceChildren(...(moderates() ? lobby.mutedList.map((m) => el("div", { className: "item" }, el("span", { textContent: `${m.name} is muted` }), button("Unmute", "", () => say({ type: "mute", id: m.id, on: false })))) : [])); | |
| 128 | $("bannedLine").replaceChildren(...(moderates() ? lobby.bannedList.map((b) => el("div", { className: "item" }, el("span", { textContent: `${b.name} is banned` }), button("Unban", "", () => say({ type: "ban", id: b.id, on: false })))) : [])); | |
| 127 | 129 | renderCrew(me); |
| 128 | 130 | $("say").hidden = !me || lobby.hello.muted || lobby.hello.banned; |
| 129 | 131 | $("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." : ""; |
| 130 | chatBox.classList.toggle("moderated", Boolean(me?.owner)); | |
| 132 | chatBox.classList.toggle("moderated", moderates()); | |
| 131 | 133 | tickClock(); |
| 132 | 134 | page.applyLock(); |
| 133 | 135 | } |
| @@ -169,7 +171,7 @@ export function createRoom(page) { | ||
| 169 | 171 | line.dataset.id = m.id; |
| 170 | 172 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); } |
| 171 | 173 | line.append(el("span", { textContent: m.text })); |
| 172 | if (m.from && me?.owner) line.tabIndex = 0; | |
| 174 | if (m.from && moderates()) line.tabIndex = 0; | |
| 173 | 175 | return line; |
| 174 | 176 | } |
| 175 | 177 | // A new page gets the last lines of the chat; older ones come a page at a time, when asked for. |
| @@ -199,7 +201,7 @@ export function createRoom(page) { | ||
| 199 | 201 | { |
| 200 | 202 | let press = null, held = null; |
| 201 | 203 | const open = (line) => { |
| 202 | if (!lobby.hello?.you?.owner || !line?.dataset.from) return; | |
| 204 | if (!moderates() || !line?.dataset.from) return; | |
| 203 | 205 | held = line; line.classList.add("held"); |
| 204 | 206 | $("modTitle").textContent = `${line.dataset.name} said`; |
| 205 | 207 | $("modText").textContent = line.querySelector("span")?.textContent ?? ""; |
| @@ -210,13 +212,13 @@ export function createRoom(page) { | ||
| 210 | 212 | nav.vibrate?.(10); |
| 211 | 213 | }; |
| 212 | 214 | chatBox.addEventListener("pointerdown", (e) => { |
| 213 | const line = e.target.closest(".line"); if (!line?.dataset.from || !lobby.hello?.you?.owner) return; | |
| 215 | const line = e.target.closest(".line"); if (!line?.dataset.from || !moderates()) return; | |
| 214 | 216 | press = { x: e.clientX, y: e.clientY, timer: setTimeout(() => { press = null; open(line); }, 480) }; |
| 215 | 217 | }); |
| 216 | 218 | const cancel = () => { if (press) { clearTimeout(press.timer); press = null; } }; |
| 217 | 219 | chatBox.addEventListener("pointermove", (e) => { if (press && Math.hypot(e.clientX - press.x, e.clientY - press.y) > 10) cancel(); }); |
| 218 | 220 | for (const ev of ["pointerup", "pointercancel", "scroll"]) chatBox.addEventListener(ev, cancel); |
| 219 | chatBox.addEventListener("contextmenu", (e) => { const line = e.target.closest(".line"); if (line?.dataset.from && lobby.hello?.you?.owner) { e.preventDefault(); cancel(); open(line); } }); | |
| 221 | chatBox.addEventListener("contextmenu", (e) => { const line = e.target.closest(".line"); if (line?.dataset.from && moderates()) { e.preventDefault(); cancel(); open(line); } }); | |
| 220 | 222 | chatBox.addEventListener("keydown", (e) => { if (e.key === "Enter" && e.target.classList?.contains("line")) open(e.target); }); |
| 221 | 223 | $("modDlg").addEventListener("close", () => { |
| 222 | 224 | const line = held; held = null; line?.classList.remove("held"); |
Software/so101/src/ui/lobby-panel.test.ts +10
| @@ -64,3 +64,13 @@ describe("the room over /live", () => { | ||
| 64 | 64 | } finally { Object.assign(g, saved); } |
| 65 | 65 | }); |
| 66 | 66 | }); |
| 67 | ||
| 68 | describe("moderators on the page", () => { | |
| 69 | test("the chat-line menu follows hello's moderator flag as well as the owner, and the owner's panels stay the owner's", async () => { | |
| 70 | const src = await Bun.file(new URL("./lobby-panel.js", import.meta.url)).text(); | |
| 71 | expect(src).toContain("lobby.hello?.moderator"); | |
| 72 | for (const line of src.split("\n").filter((l) => /type: "(delete|mute|ban)"|modDlg"\)\.showModal|contextmenu|pointerdown|tabIndex/.test(l))) expect(line).not.toContain("you?.owner"); | |
| 73 | expect(src).toContain('if (me?.owner) {\n $("pauseBtn")'); | |
| 74 | expect(src).toContain("me?.owner && d && d.id !== me.id"); | |
| 75 | }); | |
| 76 | }); |
new above
offline