Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

A removed chat message can be restored from /admin, and it reappears for everyone live #294

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/197-a-removed-chat-message-can-be-restored-f → main 2 commits fork

  • Merged. smith/197-a-removed-chat-message-can-be-restored-f was merged into main as b047c80.

Review

Sign in to review this change.
9 files changed +171 −21
Software/README.md +8
@@ -417,6 +417,13 @@ its live pose and the cameras. Driving needs a name and a turn.
417417 everyone and logs it for **restore**; `mute-suggest` shows the owner a
418418 one-click mute. A model that is down or over budget leaves the line standing.
419419 Settings and log: `$FORGE_APP_DATA/so101/crew.json`.
420 - **Removed lines.** A chat line the owner or a moderator removes is hidden for
421 everyone live but kept, with who removed it and when (last 200, in the chat
422 file). `/admin` lists them under **Removed lines**; the owner restores one
423 with its Restore button, with `{"type":"restore","id":N}` over `/live`, or
424 with `/removed` and `/restore #N` in the terminal. All go through
425 `Lobby.restoreLine`: the line reappears for every open page and the restore
426 is logged (`restored chat line N`). Moderators can see the list, not restore.
420427 - The owner (anyone who may write this repository) can take control at
421428 any time (this skips the queue), end anyone's turn, take people out of
422429 the queue, delete messages, mute people and ban them (long-press or
@@ -471,6 +478,7 @@ watches. A line you type is said in the chat; commands start with `/`:
471478 `resumed` when it is resumed.
472479 - `/end [name]`, owner: end that turn (no name: whoever drives), or take
473480 them out of the line.
481 - `/removed`, `/restore #<n>`, owner: list the latest removed chat lines, and put one back for everyone.
474482 - `/mute <name> [10m|1h|24h|forever]`, `/unmute <name>`, owner. A mute without a length is for good; a timed one ends on its own and the owner's list says when.
475483 - `/table here`, owner: the page's "Table is here", the same
476484 `POST /settings/table-here`: the table plane goes under the arm's lowest
Software/so101/src/admin-page.ts +12 −2
@@ -6,6 +6,7 @@
66
77 import { createHash } from "node:crypto";
88 import { ANNOUNCEMENT_CHARS, type AdminSettings, type LogEntry } from "./admin.ts";
9 import type { Removed } from "./chat.ts";
910 import type { Person, Who } from "./identity.ts";
1011 import type { Snapshot } from "./turns.ts";
1112
@@ -53,7 +54,7 @@ time { color: var(--dim); font-variant-numeric: tabular-nums; }
5354 export const ADMIN_CSP = `default-src 'none'; style-src 'sha256-${createHash("sha256").update(STYLE).digest("base64")}'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`;
5455
5556 /** The fixed lines a redirect back may ask for; nothing from the request is echoed. */
56 export const DONE = { saved: "Saved.", paused: "Paused: nobody new gets a turn.", resumed: "Open again: the next in line has control.", ended: "Turn ended.", taken: "You have control." } as const;
57 export const DONE = { saved: "Saved.", paused: "Paused: nobody new gets a turn.", resumed: "Open again: the next in line has control.", ended: "Turn ended.", taken: "You have control.", restored: "Line restored." } as const;
5758 export type Done = keyof typeof DONE;
5859
5960 export interface AdminPage {
@@ -63,6 +64,8 @@ export interface AdminPage {
6364 settings: AdminSettings;
6465 snapshot: Snapshot;
6566 log: readonly LogEntry[];
67 /** Chat lines a person removed, newest first. */
68 removed?: readonly Removed[];
6669 done?: Done | null;
6770 error?: string | null;
6871 }
@@ -106,7 +109,13 @@ const action = (name: string, text: string, cls = "", disabled = false) =>
106109 `<form method="post" action="/admin"><input type="hidden" name="action" value="${name}"><button class="${cls}"${disabled ? " disabled" : ""}>${text}</button></form>`;
107110
108111 /** The log lines that are moderation: chat-line deletions, removals from the line, mutes, bans and their reversals. */
109 export const isModeration = (e: LogEntry): boolean => /^(banned|unbanned|muted|unmuted|deleted chat line|took .+ out of the line)/.test(e.what);
112 export const isModeration = (e: LogEntry): boolean => /^(banned|unbanned|muted|unmuted|deleted chat line|restored chat line|took .+ out of the line)/.test(e.what);
113
114 /** Removed chat lines, each with who took it down and, for the owner, a Restore button. */
115 const removedLines = (list: readonly Removed[], canEdit: boolean): string =>
116 list.length
117 ? `<ol>${list.map((r) => `<li>${time(r.at)}<span><b>${esc(r.by.name)}</b> removed ${esc(r.message.from?.name ?? "system")}: ${esc(r.message.text)}${canEdit ? ` <form method="post" action="/admin" class="inline"><input type="hidden" name="action" value="restore"><input type="hidden" name="id" value="${r.message.id}"><button aria-label="Restore line ${r.message.id}: ${esc([...r.message.text].slice(0, 40).join(""))}">Restore</button></form>` : ""}</span></li>`).join("")}</ol>`
118 : `<p class="empty">No removed lines.</p>`;
110119
111120 export function renderAdmin(p: AdminPage): string {
112121 const s = p.settings;
@@ -148,6 +157,7 @@ ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div>
148157 </section>
149158 <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section>
150159 <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section>
160 <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section>
151161 <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`,
152162 );
153163 }
Software/so101/src/admin-routes.ts +7 −1
@@ -34,7 +34,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe
3434 return null;
3535 }
3636 const show = (status: number, done: Done | null, error: string | null) =>
37 html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error }));
37 html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, done, error }));
3838 if (req.method === "GET" || req.method === "HEAD") {
3939 const done = url.searchParams.get("done");
4040 show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null);
@@ -96,6 +96,12 @@ export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: Serve
9696 lobby.takeControl(who);
9797 done = "taken";
9898 break;
99 case "restore": {
100 const id = Number(form.get("id"));
101 if (!lobby.restoreLine(who, id)) throw new AdminError("that line is not in the log");
102 done = "restored";
103 break;
104 }
99105 default:
100106 throw new AdminError("no such action");
101107 }
Software/so101/src/chat.test.ts +17
@@ -81,4 +81,21 @@ describe("chat", () => {
8181 const chat = new Chat({ file: old, now: () => 1e12 });
8282 expect(chat.muted.get("c")).toBe("carol");
8383 });
84
85 test("a removed line is kept with who removed it, survives a restart, and goes back in order", () => {
86 const file = join(mkdtempSync(join(tmpdir(), "so101-chat-")), "chat.json");
87 let clock = 0;
88 const chat = new Chat({ file, now: () => (clock += 5000) });
89 const first = chat.post(alice, "one");
90 chat.post(bob, "two");
91 chat.delete(first.id, bob);
92 expect(chat.messages.map((m) => m.text)).toEqual(["two"]);
93 const again = new Chat({ file });
94 expect(again.removed[0]).toMatchObject({ message: { text: "one" }, by: { id: "b", name: "bob" } });
95 expect(again.system("x").id).toBe(3);
96 expect(again.unremove(first.id)?.message.text).toBe("one");
97 expect(again.messages.map((m) => m.text)).toEqual(["one", "two", "x"]);
98 expect(again.unremove(first.id)).toBeNull();
99 expect(new Chat({ file }).removed).toEqual([]);
100 });
84101 });
Software/so101/src/chat.ts +36 −11
@@ -20,6 +20,13 @@ export interface Message {
2020 text: string;
2121 }
2222
23 /** A line taken down by a person: kept, with who and when, so it can be put back. */
24 export interface Removed {
25 message: Message;
26 by: { id: string; name: string };
27 at: number;
28 }
29
2330 export class ChatError extends Error {}
2431
2532 export interface ChatOptions {
@@ -42,6 +49,8 @@ export function cleanText(text: unknown): string {
4249
4350 export class Chat {
4451 messages: Message[] = [];
52 /** Lines a person removed, newest first; the last `KEEP` are kept. */
53 removed: Removed[] = [];
4554 /** id → mute; `until` null is for good. */
4655 private readonly mutes = new Map<string, { name: string; until: number | null }>();
4756 /** id → name of everyone muted; kept in step with `mutes`, expired ones pruned on read. */
@@ -87,14 +96,28 @@ export class Chat {
8796 return this.add(null, text);
8897 }
8998
90 delete(id: number): boolean {
91 const before = this.messages.length;
92 this.messages = this.messages.filter((m) => m.id !== id);
93 if (this.messages.length === before) return false;
99 /** Takes a line down; with `by` it is kept as removed so `unremove` can bring it back. */
100 delete(id: number, by?: { id: string; name: string }): boolean {
101 const message = this.messages.find((m) => m.id === id);
102 if (!message) return false;
103 this.messages = this.messages.filter((m) => m !== message);
104 if (by) {
105 this.removed.unshift({ message, by: { id: by.id, name: by.name }, at: this.now() });
106 this.removed.length = Math.min(this.removed.length, KEEP);
107 }
94108 this.save();
95109 return true;
96110 }
97111
112 /** Puts a removed line back where its id puts it; null when it was not removed. */
113 unremove(id: number): Removed | null {
114 const i = this.removed.findIndex((r) => r.message.id === id);
115 if (i < 0) return null;
116 const [entry] = this.removed.splice(i, 1);
117 this.restore(entry!.message);
118 return entry!;
119 }
120
98121 /** Puts a removed line back where its id puts it. */
99122 restore(message: Message): void {
100123 if (this.messages.some((m) => m.id === message.id)) return;
@@ -143,16 +166,18 @@ export class Chat {
143166 }
144167
145168 private load(file: string): void {
146 let stored: { messages?: unknown; muted?: unknown };
169 let stored: { messages?: unknown; removed?: unknown; muted?: unknown };
147170 try {
148171 stored = JSON.parse(readFileSync(file, "utf8"));
149172 } catch {
150173 return; // none yet, or unreadable: start over rather than refuse to start
151174 }
152 if (Array.isArray(stored.messages)) {
153 this.messages = stored.messages
154 .filter((m): m is Message => typeof m?.id === "number" && typeof m.at === "number" && typeof m.text === "string" && (m.from === null || (typeof m.from?.id === "string" && typeof m.from?.name === "string")))
155 .slice(-KEEP);
175 const valid = (m: any): m is Message => typeof m?.id === "number" && typeof m.at === "number" && typeof m.text === "string" && (m.from === null || (typeof m.from?.id === "string" && typeof m.from?.name === "string"));
176 if (Array.isArray(stored.messages)) this.messages = stored.messages.filter(valid).slice(-KEEP);
177 if (Array.isArray(stored.removed)) {
178 this.removed = stored.removed
179 .filter((r): r is Removed => valid(r?.message) && typeof r.by?.id === "string" && typeof r.by?.name === "string" && typeof r.at === "number")
180 .slice(0, KEEP);
156181 }
157182 if (stored.muted && typeof stored.muted === "object") {
158183 // a name string is the old format: muted for good
@@ -165,13 +190,13 @@ export class Chat {
165190 }
166191 }
167192 for (const [id, m] of this.mutes) this.names.set(id, m.name);
168 this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1;
193 this.next = Math.max(0, ...this.messages.map((m) => m.id), ...this.removed.map((r) => r.message.id)) + 1;
169194 }
170195
171196 private save(): void {
172197 if (!this.file) return;
173198 mkdirSync(dirname(this.file), { recursive: true });
174 writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, muted: Object.fromEntries([...this.mutes].map(([id, m]) => [id, m.until === null ? m.name : m])) }));
199 writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, removed: this.removed, muted: Object.fromEntries([...this.mutes].map(([id, m]) => [id, m.until === null ? m.name : m])) }));
175200 renameSync(`${this.file}.tmp`, this.file);
176201 }
177202 }
Software/so101/src/lobby.test.ts +31
@@ -346,6 +346,37 @@ describe("lobby", () => {
346346 expect(section).toContain(`<b>bob</b> deleted chat line ${said.message.id}`);
347347 });
348348
349 test("a removed line is kept with who removed it, and Restore on /admin or over /live brings it back for everyone, logged", async () => {
350 const admin = new Admin();
351 const port = await start(undefined, undefined, admin);
352 const alice = await join(port, ALICE);
353 const owner = await join(port, OWNER);
354 alice.send({ type: "post", text: "oops <i>x</i>" });
355 const id = (await owner.next((m) => m.type === "said")).message.id;
356 owner.send({ type: "delete", id });
357 await alice.next((m) => m.type === "unsaid");
358 const admin_ = `http://127.0.0.1:${port}/admin`;
359 const html = await (await fetch(admin_, { headers: OWNER })).text();
360 expect(html).toContain("<b>trevato</b> removed alice: oops &#60;i&#62;x&#60;/i&#62;");
361 expect(html).toContain("Restore");
362
363 owner.send({ type: "restore", id });
364 for (const c of [alice, owner]) expect((await c.next((m) => m.type === "chat")).messages.map((x: any) => x.text)).toEqual(["oops <i>x</i>"]);
365 expect(admin.log.at(-1)).toMatchObject({ by: { name: "trevato" }, what: `restored chat line ${id}` });
366 owner.send({ type: "restore", id });
367 expect((await owner.next((m) => m.type === "error")).error).toBe("that line is not in the log");
368
369 // and the same through the admin form, which a plain user may not post
370 owner.send({ type: "delete", id });
371 await alice.next((m) => m.type === "unsaid");
372 const post = (headers: Record<string, string>) => fetch(admin_, { method: "POST", headers: { ...headers, "content-type": "application/x-www-form-urlencoded" }, body: `action=restore&id=${id}`, redirect: "manual" });
373 expect((await post(ALICE)).status).toBe(404);
374 const back = alice.next((m) => m.type === "chat");
375 expect((await post(OWNER)).status).toBe(303);
376 expect((await back).messages.map((x: any) => x.id)).toEqual([id]);
377 expect((await post(OWNER)).status).toBe(400);
378 });
379
349380 test("the owner's delete is logged with the owner as who did it, and a delete without an integer id is refused", async () => {
350381 const admin = new Admin();
351382 const port = await start(undefined, undefined, admin);
Software/so101/src/lobby.ts +19 −6
@@ -16,7 +16,8 @@
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
19 * {"type": "restore", "id": 12} owner: put back a line the crew hid
19 * {"type": "removed"} owner: the latest removed lines, answered with {"type": "removed", "lines": [...]}
20 * {"type": "restore", "id": 12} owner: put back a removed line, or one the crew hid
2021 * {"type": "dismiss", "id": 12} owner: dismiss a mute suggestion
2122 * {"type": "ban", "id": "<user id>", "on": true|false} owner: bars their account, name and address; they still watch
2223 * {"type": "words", "words": ["…"]} owner: the chat's word filter; a line with a listed word is refused
@@ -399,11 +400,10 @@ export class Lobby {
399400 this.send(seat, { type: "error", error: (err as Error).message });
400401 }
401402 return;
403 case "removed":
404 return this.send(seat, { type: "removed", lines: this.chat.removed.slice(0, 20).map((r) => ({ id: r.message.id, by: r.by.name, from: r.message.from?.name ?? null, text: r.message.text })) });
402405 case "restore": {
403 const back = Number.isSafeInteger(m.id) ? this.moderator?.restore(m.id as number) : null;
404 if (!back) return this.send(seat, { type: "error", error: "that line is not in the log" });
405 this.chat.restore(back);
406 this.broadcast(this.chatLog());
406 if (!this.restoreLine(who, m.id)) this.send(seat, { type: "error", error: "that line is not in the log" });
407407 return;
408408 }
409409 case "dismiss":
@@ -416,12 +416,25 @@ export class Lobby {
416416 /** Delete a chat line as the owner or a moderator; the admin log says who. */
417417 private deleteLine(who: Person, id: unknown): void {
418418 if (!Number.isSafeInteger(id)) throw new ChatError("delete which line?");
419 if (!this.chat.delete(id as number)) return;
419 if (!this.chat.delete(id as number, who)) return;
420420 this.admin.record(who, `deleted chat line ${id}`);
421421 this.broadcast({ type: "unsaid", id });
422422 this.broadcastAdmin();
423423 }
424424
425 /** Put a removed chat line back for everyone, from the owner's removals or the moderator's log; the admin log says who. */
426 restoreLine(who: Person, id: unknown): boolean {
427 if (!Number.isSafeInteger(id)) return false;
428 const line = this.chat.unremove(id as number);
429 const back = line?.message ?? this.moderator?.restore(id as number);
430 if (!back) return false;
431 if (!line) this.chat.restore(back);
432 this.admin.record(who, `restored chat line ${id}`);
433 this.broadcast(this.chatLog());
434 this.broadcastAdmin();
435 return true;
436 }
437
425438 private turnChanged(event: TurnEvent): void {
426439 if (event.kind === "turn") {
427440 const { ended, started } = event;
Software/so101/src/terminal.test.ts +27
@@ -368,6 +368,33 @@ describe("terminal door", () => {
368368 expect(lobby!.admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${one.message.id}` });
369369 });
370370
371 test("removed and restore: the owner's terminal lists removals and restores one for web viewers; anyone else's gets nothing", async () => {
372 const port = await start();
373 const page = await viewer(port);
374 const alicePage = await viewer(port, web("u-alice", "alice"));
375 alicePage.send({ type: "post", text: "one" });
376 const one = await page.heardOne((m) => m.type === "said" && m.message.text === "one", "the line");
377 const owner = await terminal(port, ttyEnv("u-owner", "trevato", "admin"));
378 const carol = await terminal(port, ttyEnv("u-carol", "carol", "user"));
379 await owner.printedLine(/^you are trevato \(owner\)$/);
380 owner.door.input("/removed");
381 await owner.printedLine(/^no removed lines$/);
382 owner.door.input(`/delete #${one.message.id}`);
383 await page.heardOne((m) => m.type === "unsaid", "the line going");
384 owner.door.input("/removed");
385 await owner.printedLine(new RegExp(`^removed #${one.message.id} by trevato, alice: one$`));
386 carol.door.input("/removed");
387 await carol.printedLine(/^error: only the owner can do that$/);
388 carol.door.input(`/restore #${one.message.id}`);
389 await carol.printedLine(/^error: only the owner can do that$/);
390 expect(lobby!.chat.messages.length).toBe(0);
391 owner.door.input(`/restore #${one.message.id}`);
392 expect((await page.heardOne((m) => m.type === "chat" && m.messages.length === 1, "the line back")).messages[0].text).toBe("one");
393 expect(lobby!.admin.log.at(-1)).toMatchObject({ what: `restored chat line ${one.message.id}` });
394 owner.door.input("/restore x");
395 await owner.printedLine(/^usage: \/restore/);
396 });
397
371398 test("end-turn: only the owner's terminal may; the page sees the turn pass on", async () => {
372399 const port = await start();
373400 const alicePage = await viewer(port, web("u-alice", "alice"));
Software/so101/src/terminal.ts +14 −1
@@ -22,6 +22,8 @@
2222 * /end [who] owner: end that turn (none: whoever drives), or take them out of the line
2323 * /mute <who> [10m|1h|24h|forever] /unmute <who> owner
2424 * /delete <who|#id> owner or moderator: remove that person's latest chat line, or the line numbered #id (shown to those who may)
25 * /removed owner: the latest removed chat lines, with their numbers and who removed them
26 * /restore #<n> owner: put a removed line back for everyone (Lobby.restoreLine; it is logged)
2527 * /table here admin: put the table under the arm's lowest link now (POST /settings/table-here)
2628 * /table <mm> admin: set the table offset, -100..100 mm (PUT /settings tableOffsetMm)
2729 * /machine [off] the Machine panel (/machine, src/machine-feed.ts): the joints, the link and the server as they are,
@@ -137,7 +139,7 @@ const said = (room: Room, m: unknown): string => {
137139 return from ? `${tag}${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`;
138140 };
139141
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";
142 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) · /removed /restore #<id> (owner) · /machine [off] · /who · /quit";
141143
142144 /** The Machine feed's socket beside the lobby's: same app, same port. */
143145 export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine");
@@ -228,6 +230,11 @@ export function hear(room: Room, frame: Frame): string[] {
228230 }
229231 case "unsaid":
230232 return ["· a line was removed"];
233 case "removed": {
234 const lines = Array.isArray(frame.lines) ? frame.lines : [];
235 if (!lines.length) return ["no removed lines"];
236 return lines.map((l: { id?: unknown; by?: unknown; from?: unknown; text?: unknown }) => `removed #${plain(l?.id)} by ${plain(l?.by)}, ${l?.from ? plain(l.from) : "system"}: ${plain(l?.text)}`);
237 }
231238 case "error":
232239 return [`error: ${plain(frame.error)}`];
233240 default:
@@ -283,6 +290,12 @@ export function typed(room: Room, input: string): Typed {
283290 const id = number ? Number(number[1]) : room.latest.get(target(room, arg) ?? "");
284291 return id === undefined ? { print: `error: no line from ${plain(arg)} seen` } : { send: { type: "delete", id } };
285292 }
293 case "removed":
294 return { send: { type: "removed" } };
295 case "restore": {
296 const number = /^#?(\d{1,15})$/.exec(arg);
297 return number ? { send: { type: "restore", id: Number(number[1]) } } : { print: "usage: /restore #<line number>" };
298 }
286299 case "stop":
287300 case "resume":
288301 return { post: `/${verb.toLowerCase()}` as "/stop" | "/resume" };

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs