Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
The long-press moderation dialog offers Mute (with its length) and Ban to a verified moderator on a plain user's line, not only to the owner #412
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/400-the-long-press-moderation-dialog-offers → main 4 commits fork 3 threads · 3 unresolved
5 files
changed +77
−9
Software/so101/src/chat.ts +2 −2
| @@ -16,7 +16,7 @@ export interface Message { | ||
| 16 | 16 | id: number; |
| 17 | 17 | at: number; |
| 18 | 18 | /** null for a system line. */ |
| 19 | from: { id: string; name: string } | null; | |
| 19 | from: { id: string; name: string; /** set when the owner said it, from the same identity that makes them the owner on /live */ owner?: true } | null; | |
| 20 | 20 | text: string; |
| 21 | 21 | } |
| 22 | 22 | |
| @@ -89,7 +89,7 @@ export class Chat { | ||
| 89 | 89 | const last = this.lastPost.get(who.id); |
| 90 | 90 | if (last !== undefined && now - last < this.rateMs) throw new ChatError(`one message every ${this.rateMs / 1000} s`); |
| 91 | 91 | this.lastPost.set(who.id, now); |
| 92 | return this.add({ id: who.id, name: who.name }, clean); | |
| 92 | return this.add(who.owner ? { id: who.id, name: who.name, owner: true } : { id: who.id, name: who.name }, clean); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | system(text: string): Message { |
Software/so101/src/lobby.test.ts +28
| @@ -368,6 +368,34 @@ describe("lobby", () => { | ||
| 368 | 368 | expect((await alice.next((m) => m.type === "said")).message.text).toBe("back"); |
| 369 | 369 | }); |
| 370 | 370 | |
| 371 | test("chat, history and said frames mark the owner's and a moderator's lines with their role, a plain user's with none, even after the owner has left", async () => { | |
| 372 | const admin = new Admin(); | |
| 373 | admin.set({ id: "u-owner", name: "trevato", owner: true }, { moderators: ["bob"] }); | |
| 374 | const port = await start(undefined, new Chat(), admin); | |
| 375 | const owner = await join(port, OWNER); | |
| 376 | const bob = await join(port, BOB); | |
| 377 | const alice = await join(port, ALICE); | |
| 378 | const roleOf = (m: Msg | undefined, name: string) => (m ? [m.message ?? m].flatMap((x) => x.messages ?? [x]).find((x: Msg) => x.from?.name === name)?.from.role : undefined); | |
| 379 | for (const [who, text] of [[owner, "from owner"], [bob, "from mod"], [alice, "from user"]] as const) { | |
| 380 | who.send({ type: "post", text }); | |
| 381 | const said = await alice.next((m) => m.type === "said" && m.message.text === text); | |
| 382 | expect(roleOf(said, said.message.from.name)).toBe(who === owner ? "owner" : who === bob ? "moderator" : undefined); | |
| 383 | expect("owner" in said.message.from).toBe(false); | |
| 384 | } | |
| 385 | owner.ws.close(); | |
| 386 | await sleep(100); | |
| 387 | const carol = await join(port, CAROL); | |
| 388 | const chat = await carol.first((m) => m.type === "chat"); | |
| 389 | expect(roleOf(chat, "trevato")).toBe("owner"); | |
| 390 | expect(roleOf(chat, "bob")).toBe("moderator"); | |
| 391 | expect(roleOf(chat, "alice")).toBeUndefined(); | |
| 392 | carol.send({ type: "history", before: 1_000_000 }); | |
| 393 | const history = await carol.next((m) => m.type === "history"); | |
| 394 | expect(roleOf(history, "trevato")).toBe("owner"); | |
| 395 | expect(roleOf(history, "bob")).toBe("moderator"); | |
| 396 | expect(roleOf(history, "alice")).toBeUndefined(); | |
| 397 | }); | |
| 398 | ||
| 371 | 399 | test("slow mode holds a second line until slowModeSeconds have passed; owner and moderators are never held; 0 holds nobody", async () => { |
| 372 | 400 | let clock = 1_000_000; |
| 373 | 401 | const admin = new Admin(); |
Software/so101/src/lobby.ts +11 −3
| @@ -44,7 +44,7 @@ import { Accounts, visitor, type Visitor } from "./accounts.ts"; | ||
| 44 | 44 | import { browserId } from "./browser-id.ts"; |
| 45 | 45 | import { Admin, AdminError } from "./admin.ts"; |
| 46 | 46 | import { frameBytes, TOO_BIG_CLOSE } from "./frame-limit.ts"; |
| 47 | import { Chat, ChatError } from "./chat.ts"; | |
| 47 | import { Chat, ChatError, type Message } from "./chat.ts"; | |
| 48 | 48 | import { nameFor, type GuestName } from "./guest-names.ts"; |
| 49 | 49 | import { Moderator } from "./moderator.ts"; |
| 50 | 50 | import { ChatRules } from "./chat-rules.ts"; |
| @@ -413,7 +413,7 @@ export class Lobby { | ||
| 413 | 413 | if (held > 0) throw new ChatError(`slow mode: wait ${Math.ceil(held / 1000)}s`); |
| 414 | 414 | const said = this.chat.post(who, m.text); |
| 415 | 415 | this.lastPosted.set(who.id, this.now()); |
| 416 | this.broadcast({ type: "said", message: said }); | |
| 416 | this.broadcast({ type: "said", message: this.marked(said) }); | |
| 417 | 417 | void this.moderator?.check(said); |
| 418 | 418 | return; |
| 419 | 419 | } |
| @@ -701,13 +701,21 @@ export class Lobby { | ||
| 701 | 701 | } |
| 702 | 702 | } |
| 703 | 703 | |
| 704 | /** A line with its author's standing (the owner is remembered on the line itself), so a moderator's page can tell whose line it may mute or ban. */ | |
| 705 | private marked(m: Message): Message & { from: { role?: "owner" | "moderator" } | null } { | |
| 706 | if (!m.from) return m; | |
| 707 | const { owner, ...from } = m.from; | |
| 708 | const role = owner ? "owner" : this.admin.isModerator(from.name) ? "moderator" : undefined; | |
| 709 | return { ...m, from: role ? { ...from, role } : from }; | |
| 710 | } | |
| 711 | ||
| 704 | 712 | /** Up to `count` lines before the one with id `before` (all of them with no `before`), oldest first, and whether older ones remain. */ |
| 705 | 713 | private page(before: number | null, count: number) { |
| 706 | 714 | const all = this.chat.messages; |
| 707 | 715 | const end = before === null ? all.length : all.findIndex((m) => m.id >= before); |
| 708 | 716 | const stop = end < 0 ? all.length : end; |
| 709 | 717 | const start = Math.max(0, stop - count); |
| 710 | return { messages: all.slice(start, stop), more: start > 0 }; | |
| 718 | return { messages: all.slice(start, stop).map((m) => this.marked(m)), more: start > 0 }; | |
| 711 | 719 | } |
| 712 | 720 | |
| 713 | 721 | private chatLog() { |
Software/so101/src/ui/lobby-panel.js +16 −3
| @@ -22,6 +22,18 @@ export function queueEntry(el, x, mine) { | ||
| 22 | 22 | return el("li", { className: mine ? "me" : "" }, el("span", { className: "place", textContent: `${x.place}.` }), el("span", { textContent: mine ? `${x.name} (you)` : x.name }), el("span", { className: "wait", textContent: `≤ ${waitMinutes(x.waitMs)} min` })); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | /** | |
| 26 | * Which moderation actions the long-press dialog offers on a line: Delete to the owner and moderators; Mute and Ban to the owner | |
| 27 | * on anyone else's line, and to a moderator on a plain user's (not their own, the owner's or another moderator's). The server checks again. | |
| 28 | * @param {{ owner?: boolean, id?: string } | null | undefined} you @param {boolean} moderator @param {{ id: string, role?: string } | null | undefined} author | |
| 29 | */ | |
| 30 | export function modActions(you, moderator, author) { | |
| 31 | const staff = Boolean(you?.owner || moderator); | |
| 32 | if (!staff || !author) return { delete: false, mute: false, ban: false }; | |
| 33 | const other = author.id !== you?.id && (Boolean(you?.owner) || !author.role); | |
| 34 | return { delete: true, mute: other, ban: other }; | |
| 35 | } | |
| 36 | ||
| 25 | 37 | /** Only a signed-in person reports, and only what someone else said. @param {any} m a chat message @param {any} me hello.you */ |
| 26 | 38 | export const mayReport = (m, me) => Boolean(me && m.from && m.from.id !== me.id); |
| 27 | 39 | |
| @@ -194,7 +206,7 @@ export function createRoom(page) { | ||
| 194 | 206 | const me = lobby.hello?.you; |
| 195 | 207 | const line = el("div", { className: m.from ? `line${me && m.from.id === me.id ? " me" : ""}` : "line sys", title: new Date(m.at).toLocaleTimeString() }); |
| 196 | 208 | line.dataset.id = m.id; |
| 197 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); } | |
| 209 | if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; if (m.from.role) line.dataset.role = m.from.role; line.append(el("b", { textContent: m.from.name })); } | |
| 198 | 210 | line.append(el("span", { textContent: m.text })); |
| 199 | 211 | if (m.from && canDelete()) line.tabIndex = 0; |
| 200 | 212 | if (mayReport(m, me)) { // always drawn, so the line never shifts when it is reported |
| @@ -237,7 +249,7 @@ export function createRoom(page) { | ||
| 237 | 249 | else if (lines.length) pill.hidden = false; // the reader is up in the history: keep the place, offer the way down |
| 238 | 250 | if (!replace && page.tab() !== "chat") $("unread").textContent = String(Math.min(99, Number($("unread").textContent || 0) + lines.length)); |
| 239 | 251 | } |
| 240 | // 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). | |
| 252 | // Moderation on a phone: long-press a line (or right-click it, or Enter on it) for delete (and mute and ban, per modActions). | |
| 241 | 253 | { |
| 242 | 254 | /** @type {any} */ |
| 243 | 255 | let press = null; |
| @@ -250,7 +262,8 @@ export function createRoom(page) { | ||
| 250 | 262 | $("modText").textContent = line.querySelector("span")?.textContent ?? ""; |
| 251 | 263 | $("modMute").textContent = `Mute ${line.dataset.name}`; |
| 252 | 264 | $("modBan").textContent = `Ban ${line.dataset.name}`; |
| 253 | $("modMute").hidden = $("modBan").hidden = !lobby.hello.you.owner || line.dataset.from === lobby.hello.you.id; | |
| 265 | const may = modActions(lobby.hello.you, Boolean(lobby.hello.moderator), { id: line.dataset.from, role: line.dataset.role }); | |
| 266 | $("modMute").hidden = !may.mute; $("modBan").hidden = !may.ban; | |
| 254 | 267 | $("modDlg").showModal(); |
| 255 | 268 | nav.vibrate?.(10); |
| 256 | 269 | }; |
Software/so101/src/ui/lobby-panel.test.ts +20 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { backoff, createRoom, mayReport, minutes, ordinal, presenceText, queueEntry, waitMinutes } from "./lobby-panel.js"; | |
| 2 | import { backoff, createRoom, mayReport, modActions, minutes, ordinal, presenceText, queueEntry, waitMinutes } from "./lobby-panel.js"; | |
| 3 | 3 | |
| 4 | 4 | /** A stand-in for the page's el(): elements as plain trees, so the test needs no DOM. */ |
| 5 | 5 | const el = (tag: string, props: Record<string, unknown> = {}, ...children: unknown[]) => ({ tag, ...props, children }); |
| @@ -299,3 +299,22 @@ describe("the presence line", () => { | ||
| 299 | 299 | expect(presenceText(null)).toBeNull(); |
| 300 | 300 | }); |
| 301 | 301 | }); |
| 302 | ||
| 303 | describe("the long-press dialog's actions", () => { | |
| 304 | const owner = { id: "o", owner: true }; | |
| 305 | const mod = { id: "m", owner: false }; | |
| 306 | test("the owner sees all three on anyone else's line", () => { | |
| 307 | expect(modActions(owner, false, { id: "u" })).toEqual({ delete: true, mute: true, ban: true }); | |
| 308 | expect(modActions(owner, false, { id: "m2", role: "moderator" })).toEqual({ delete: true, mute: true, ban: true }); | |
| 309 | }); | |
| 310 | test("a moderator may mute and ban a plain user, not the owner or another moderator", () => { | |
| 311 | expect(modActions(mod, true, { id: "u" })).toEqual({ delete: true, mute: true, ban: true }); | |
| 312 | expect(modActions(mod, true, { id: "o", role: "owner" })).toEqual({ delete: true, mute: false, ban: false }); | |
| 313 | expect(modActions(mod, true, { id: "m2", role: "moderator" })).toEqual({ delete: true, mute: false, ban: false }); | |
| 314 | }); | |
| 315 | test("nobody may mute or ban themselves, and a plain user gets nothing", () => { | |
| 316 | expect(modActions(owner, false, { id: "o" })).toEqual({ delete: true, mute: false, ban: false }); | |
| 317 | expect(modActions(mod, true, { id: "m" })).toEqual({ delete: true, mute: false, ban: false }); | |
| 318 | expect(modActions({ id: "u", owner: false }, false, { id: "x" })).toEqual({ delete: false, mute: false, ban: false }); | |
| 319 | }); | |
| 320 | }); |
new above
offline
The
mayReportdoc comment ("Only a signed-in person reports…") now sits abovemodActions's block. Put it back directly abovemayReport.