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 #201

Parked opened this
Closed without merging

smith/arm:smith/190-moderators-not-only-the-owner-can-remove → main 4 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +129 −21
Software/README.md +8
@@ -524,6 +524,14 @@ or from the **Admin** tab that only the owner sees:
524524 `/admin` and read the settings and the log, and change nothing. A moderator
525525 is matched on the forge-signed identity (the forge username), never on a
526526 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.
527535 Settings are web-only because this app has no terminal (`forge.json` has no
528536 `tty`); the `so101` command line drives the arm hardware and does not run
529537 the relay.
Software/so101/src/lobby.test.ts +51
@@ -278,6 +278,57 @@ describe("lobby", () => {
278278 expect((await late.first((m) => m.type === "chat")).messages).toEqual([]);
279279 });
280280
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
281332 test("chat history is lazy: a new page gets the last lines, anyone asks for older pages, oldest first", async () => {
282333 const chat = new Chat();
283334 for (let i = 0; i < 100; i++) chat.system(`line ${i}`);
Software/so101/src/lobby.ts +39 −12
@@ -11,14 +11,14 @@
1111 * {"type": "leave"} hand back the turn, or leave the line
1212 * {"type": "post", "text": "…"} say something
1313 * {"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
1616 * {"type": "pause", "on": true|false} owner: maintenance; nobody new gets a turn
1717 * {"type": "settings", "set": {…}} owner: any of src/admin.ts's settings
1818 * {"type": "crew", "settings": {"enabled", "rules", "maxPerMinute"}} owner: the moderator's settings
1919 * {"type": "restore", "id": 12} owner: put back a line the crew hid
2020 * {"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
2222 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
2323 * {"type": "pong"} the answer to {"type": "ping"}
2424 * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one
@@ -70,9 +70,13 @@ interface Seat {
7070 who: Who;
7171 from: Visitor;
7272 heard: number;
73 /** Signed in through the forge: only such a seat can be a moderator. */
74 verified: boolean;
7375 }
7476
7577 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"]);
7680 /** Lines of chat a new page is sent: enough to fill a phone's chat, little enough for a slow link. */
7781 export const CHAT_FIRST = 30;
7882 /** Lines per "history" answer, when someone scrolls up for more. */
@@ -232,7 +236,8 @@ export class Lobby {
232236 upgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void {
233237 const who = this.who(req);
234238 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));
236241 }
237242
238243 close(): void {
@@ -244,16 +249,18 @@ export class Lobby {
244249 }
245250
246251 // ------------------------------------------------------------------ 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 };
249254 this.seats.add(seat);
250255 if (who) this.turns.connect(who.id);
251256 this.send(seat, this.hello(seat));
252257 this.send(seat, this.state());
253258 this.send(seat, this.chatLog());
254 if (who?.owner) {
259 if (this.moderates(seat)) {
255260 this.send(seat, this.mutedList());
256261 this.send(seat, this.bannedList());
262 }
263 if (who?.owner) {
257264 this.send(seat, this.wordsList());
258265 this.send(seat, this.adminState());
259266 this.send(seat, this.crewState());
@@ -314,7 +321,7 @@ export class Lobby {
314321 return;
315322 }
316323 }
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" });
318325 switch (m.type) {
319326 case "end":
320327 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 {
329336 return this.configure(who, m.set);
330337 case "delete":
331338 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 }
333344 return;
334345 case "words": {
335346 const words = this.filter.set(m.words);
@@ -341,12 +352,20 @@ export class Lobby {
341352 case "mute": {
342353 if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" });
343354 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 }
344361 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;
345362 this.chat.mute(id, name, m.on);
346363 for (const s of this.seats) {
347364 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());
349366 }
367 this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`);
368 this.broadcastAdmin();
350369 this.moderator?.muted(id);
351370 return;
352371 }
@@ -360,6 +379,7 @@ export class Lobby {
360379 const person = known ?? (said ? { id, name: said.name, owner: false } : null);
361380 if (!person) return this.send(seat, { type: "error", error: "nobody here by that id" });
362381 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" });
363383 this.accounts.ban({ id: person.id, name: person.name, owner: false }, target?.from ?? null, who.name);
364384 this.turns.end(id); // their turn ends, or they leave the line
365385 this.broadcast({ type: "said", message: this.chat.system(`${person.name} was banned`) });
@@ -372,7 +392,7 @@ export class Lobby {
372392 this.broadcastAdmin();
373393 for (const s of this.seats) {
374394 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());
376396 }
377397 return;
378398 }
@@ -437,7 +457,14 @@ export class Lobby {
437457
438458 private hello(seat: Seat) {
439459 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)));
441468 }
442469
443470 private bannedList() {
Software/so101/src/terminal.test.ts +3
@@ -80,6 +80,9 @@ describe("terminal lines", () => {
8080 expect(typed(room, "/end Bob")).toEqual({ send: { type: "end", id: "u-bob" } });
8181 expect(typed(room, "/mute alice")).toEqual({ send: { type: "mute", id: "u-alice", on: true } });
8282 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>" });
8386 expect(typed(room, "/mute")).toEqual({ print: "usage: /mute <name>" });
8487 expect(typed(room, "/mute a.b!")).toEqual({ print: "error: who is a.b!?" });
8588 expect(typed(room, "/who")).toEqual({ print: "alice has control · 1. bob" });
Software/so101/src/terminal.ts +9 −2
@@ -20,7 +20,8 @@
2020 * /stop the driver or an admin: halt the arm and hold it, whatever drives it, until /resume
2121 * /resume the driver or an admin: end the hold; the arm stays put until a new command
2222 * /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
2425 * /table here admin: put the table under the arm's lowest link now (POST /settings/table-here)
2526 * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm)
2627 * /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 => {
130131 return from ? `${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`;
131132 };
132133
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";
134135
135136 /** The Machine feed's socket beside the lobby's: same app, same port. */
136137 export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine");
@@ -266,6 +267,12 @@ export function typed(room: Room, input: string): Typed {
266267 const id = target(room, arg);
267268 return id ? { send: { type: "mute", id, on: verb.toLowerCase() === "mute" } } : { print: `error: who is ${plain(arg)}?` };
268269 }
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 }
269276 case "stop":
270277 case "resume":
271278 return { post: `/${verb.toLowerCase()}` as "/stop" | "/resume" };
Software/so101/src/ui/lobby-panel.js +9 −7
@@ -31,6 +31,8 @@ export function createRoom(page) {
3131 const lobby = { ws: null, hello: null, state: null, offset: 0, retry: 500, mutedList: [], bannedList: [], admin: null, statusSeen: false };
3232 function mayDrive() { return !relayed || (lobby.hello !== null && (!lobby.hello.signin || (lobby.hello.you !== null && lobby.state?.driver?.id === lobby.hello.you.id))); }
3333 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);
3436 const say = (m) => { if (lobby.ws?.readyState === 1) lobby.ws.send(JSON.stringify(m)); };
3537 const serverNow = () => Date.now() + lobby.offset;
3638 function tickClock() {
@@ -122,12 +124,12 @@ export function createRoom(page) {
122124 $("pauseBtn").className = st.paused ? "go" : "";
123125 $("pauseBtn").onclick = () => say({ type: "pause", on: !st.paused });
124126 }
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 })))) : []));
127129 renderCrew(me);
128130 $("say").hidden = !me || lobby.hello.muted || lobby.hello.banned;
129131 $("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());
131133 tickClock();
132134 page.applyLock();
133135 }
@@ -169,7 +171,7 @@ export function createRoom(page) {
169171 line.dataset.id = m.id;
170172 if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); }
171173 line.append(el("span", { textContent: m.text }));
172 if (m.from && me?.owner) line.tabIndex = 0;
174 if (m.from && moderates()) line.tabIndex = 0;
173175 return line;
174176 }
175177 // 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) {
199201 {
200202 let press = null, held = null;
201203 const open = (line) => {
202 if (!lobby.hello?.you?.owner || !line?.dataset.from) return;
204 if (!moderates() || !line?.dataset.from) return;
203205 held = line; line.classList.add("held");
204206 $("modTitle").textContent = `${line.dataset.name} said`;
205207 $("modText").textContent = line.querySelector("span")?.textContent ?? "";
@@ -210,13 +212,13 @@ export function createRoom(page) {
210212 nav.vibrate?.(10);
211213 };
212214 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;
214216 press = { x: e.clientX, y: e.clientY, timer: setTimeout(() => { press = null; open(line); }, 480) };
215217 });
216218 const cancel = () => { if (press) { clearTimeout(press.timer); press = null; } };
217219 chatBox.addEventListener("pointermove", (e) => { if (press && Math.hypot(e.clientX - press.x, e.clientY - press.y) > 10) cancel(); });
218220 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); } });
220222 chatBox.addEventListener("keydown", (e) => { if (e.key === "Enter" && e.target.classList?.contains("line")) open(e.target); });
221223 $("modDlg").addEventListener("close", () => {
222224 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", () => {
6464 } finally { Object.assign(g, saved); }
6565 });
6666 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs