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

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/400-the-long-press-moderation-dialog-offers → main 4 commits fork 3 threads · 3 unresolved

  • Merged. smith/400-the-long-press-moderation-dialog-offers was merged into main as 3358b0b.

Review

Sign in to review this change.
5 files changed +77 −9
Software/so101/src/chat.ts +2 −2
@@ -16,7 +16,7 @@ export interface Message {
1616 id: number;
1717 at: number;
1818 /** 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;
2020 text: string;
2121 }
2222
@@ -89,7 +89,7 @@ export class Chat {
8989 const last = this.lastPost.get(who.id);
9090 if (last !== undefined && now - last < this.rateMs) throw new ChatError(`one message every ${this.rateMs / 1000} s`);
9191 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);
9393 }
9494
9595 system(text: string): Message {
Software/so101/src/lobby.test.ts +28
@@ -368,6 +368,34 @@ describe("lobby", () => {
368368 expect((await alice.next((m) => m.type === "said")).message.text).toBe("back");
369369 });
370370
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
371399 test("slow mode holds a second line until slowModeSeconds have passed; owner and moderators are never held; 0 holds nobody", async () => {
372400 let clock = 1_000_000;
373401 const admin = new Admin();
Software/so101/src/lobby.ts +11 −3
@@ -44,7 +44,7 @@ import { Accounts, visitor, type Visitor } from "./accounts.ts";
4444 import { browserId } from "./browser-id.ts";
4545 import { Admin, AdminError } from "./admin.ts";
4646 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";
4848 import { nameFor, type GuestName } from "./guest-names.ts";
4949 import { Moderator } from "./moderator.ts";
5050 import { ChatRules } from "./chat-rules.ts";
@@ -413,7 +413,7 @@ export class Lobby {
413413 if (held > 0) throw new ChatError(`slow mode: wait ${Math.ceil(held / 1000)}s`);
414414 const said = this.chat.post(who, m.text);
415415 this.lastPosted.set(who.id, this.now());
416 this.broadcast({ type: "said", message: said });
416 this.broadcast({ type: "said", message: this.marked(said) });
417417 void this.moderator?.check(said);
418418 return;
419419 }
@@ -701,13 +701,21 @@ export class Lobby {
701701 }
702702 }
703703
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
704712 /** Up to `count` lines before the one with id `before` (all of them with no `before`), oldest first, and whether older ones remain. */
705713 private page(before: number | null, count: number) {
706714 const all = this.chat.messages;
707715 const end = before === null ? all.length : all.findIndex((m) => m.id >= before);
708716 const stop = end < 0 ? all.length : end;
709717 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 };
711719 }
712720
713721 private chatLog() {
Software/so101/src/ui/lobby-panel.js +16 −3
@@ -22,6 +22,18 @@ export function queueEntry(el, x, mine) {
2222 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` }));
2323 }

The mayReport doc comment ("Only a signed-in person reports…") now sits above modActions's block. Put it back directly above mayReport.

2424
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
2537 /** Only a signed-in person reports, and only what someone else said. @param {any} m a chat message @param {any} me hello.you */
2638 export const mayReport = (m, me) => Boolean(me && m.from && m.from.id !== me.id);
2739
@@ -194,7 +206,7 @@ export function createRoom(page) {
194206 const me = lobby.hello?.you;
195207 const line = el("div", { className: m.from ? `line${me && m.from.id === me.id ? " me" : ""}` : "line sys", title: new Date(m.at).toLocaleTimeString() });
196208 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 })); }
198210 line.append(el("span", { textContent: m.text }));
199211 if (m.from && canDelete()) line.tabIndex = 0;
200212 if (mayReport(m, me)) { // always drawn, so the line never shifts when it is reported
@@ -237,7 +249,7 @@ export function createRoom(page) {
237249 else if (lines.length) pill.hidden = false; // the reader is up in the history: keep the place, offer the way down
238250 if (!replace && page.tab() !== "chat") $("unread").textContent = String(Math.min(99, Number($("unread").textContent || 0) + lines.length));
239251 }
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).
241253 {
242254 /** @type {any} */
243255 let press = null;
@@ -250,7 +262,8 @@ export function createRoom(page) {
250262 $("modText").textContent = line.querySelector("span")?.textContent ?? "";
251263 $("modMute").textContent = `Mute ${line.dataset.name}`;
252264 $("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;
254267 $("modDlg").showModal();
255268 nav.vibrate?.(10);
256269 };
Software/so101/src/ui/lobby-panel.test.ts +20 −1
@@ -1,5 +1,5 @@
11 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";
33
44 /** A stand-in for the page's el(): elements as plain trees, so the test needs no DOM. */
55 const el = (tag: string, props: Record<string, unknown> = {}, ...children: unknown[]) => ({ tag, ...props, children });
@@ -299,3 +299,22 @@ describe("the presence line", () => {
299299 expect(presenceText(null)).toBeNull();
300300 });
301301 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs