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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

/admin shows the moderation actions from the admin log, newest first, with who and when - read-only #254

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/237-admin-shows-the-moderation-actions-from → main 5 commits fork 2 threads · 2 unresolved

  • Merged. smith/237-admin-shows-the-moderation-actions-from was merged into main as 4ba6e72.

Review

Sign in to review this change.
5 files changed +55 −3
Software/README.md +3
@@ -534,6 +534,9 @@ or from the **Admin** tab that only the owner sees:
534534 Settings are web-only because this app has no terminal (`forge.json` has no
535535 `tty`); the `so101` command line drives the arm hardware and does not run
536536 the relay.
537 The **Moderation** list on `/admin` (bans, mutes, chat-line deletions,
538 removals from the line, newest first, with who and when) is page-only: it
539 reads the admin log, which `src/tty.ts` cannot reach.
537540
538541 The settings take effect at once and survive a restart: they are stored in
539542 `$FORGE_APP_DATA/so101/admin.json`. Every change and every owner action
Software/so101/src/admin-page.ts +10 −3
@@ -95,12 +95,19 @@ const time = (at: number) => {
9595 return `<time datetime="${iso}">${iso.slice(5, 10)} ${iso.slice(11, 16)}</time>`;
9696 };
9797
98 /** Entries newest first, each with when, who and what; empty says so. */
99 const entries = (list: readonly LogEntry[], none: string): string =>
100 list.length ? `<ol>${[...list].reverse().map((e) => `<li>${time(e.at)}<span><b>${esc(e.by.name)}</b> ${esc(e.what)}</span></li>`).join("")}</ol>` : `<p class="empty">${none}</p>`;
101
98102 const field = (name: keyof AdminSettings, label: string, hint: string, value: number, min: number, max: number, off: string) =>
99103 `<label>${label}<small>${hint}</small><input name="${name}" type="number" inputmode="numeric" required min="${min}" max="${max}" step="1" value="${value}"${off}></label>`;
100104
101105 const action = (name: string, text: string, cls = "", disabled = false) =>
outdated

Regex misses deleted chat line N (lobby.ts:408); removed is never logged. Use deleted chat line instead and test it with a real delete.

102106 `<form method="post" action="/admin"><input type="hidden" name="action" value="${name}"><button class="${cls}"${disabled ? " disabled" : ""}>${text}</button></form>`;
103107
108 /** 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);
110
104111 export function renderAdmin(p: AdminPage): string {
105112 const s = p.settings;
106113 const d = p.snapshot.driver;
@@ -109,9 +116,8 @@ export function renderAdmin(p: AdminPage): string {
109116 const mods = `<ul>${s.moderators.map((m) => `<li>${esc(m)}${p.canEdit ? ` <form method="post" action="/admin" class="inline"><input type="hidden" name="action" value="mod-remove"><input type="hidden" name="name" value="${esc(m)}"><button aria-label="Remove moderator ${esc(m)}">Remove</button></form>` : ""}</li>`).join("") || "<li class=\"empty\">none</li>"}</ul>` +
110117 (p.canEdit ? `<form method="post" action="/admin"><input type="hidden" name="action" value="mod-add"><label>Add a moderator<small>a username; moderators can look at this page and change nothing</small><input name="name" required maxlength="64"></label><div class="save"><button>Add</button></div></form>` : "");
111118 const flash = p.error ? `<p class="flash bad" role="alert">${esc(p.error)}</p>` : p.done ? `<p class="flash" role="status">${DONE[p.done]}</p>` : "";
112 const log = p.log.length
113 ? `<ol>${[...p.log].reverse().map((e) => `<li>${time(e.at)}<span><b>${esc(e.by.name)}</b> ${esc(e.what)}</span></li>`).join("")}</ol>`
114 : `<p class="empty">Nothing yet.</p>`;
119 const actions = entries(p.log.filter(isModeration), "No moderation actions yet.");
120 const log = entries(p.log, "Nothing yet.");
115121 return page(
116122 "Admin · SO-101",
117123 `<header>
@@ -141,6 +147,7 @@ ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div>
141147 </form>
142148 </section>
143149 <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section>
150 <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section>
144151 <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`,
145152 );
146153 }
Software/so101/src/admin-restart.test.ts +21
@@ -57,3 +57,24 @@ test("a setting, a moderator and the log entries survive a relay restart on the
5757 expect(after).toContain("back at five");
5858 expect(after).not.toContain("hijacked");
5959 });
60
61 test("/admin lists moderation actions newest first, escaped, and a plain user gets no page", async () => {
62 const owner = as("trevato", "admin");
63 const base = await boot();
64 const evil = "<img src=x onerror=alert(1)>";
65 const who = { id: "id-trevato", name: "trevato", owner: true };
66 lobby!.admin.record(who, "announcement → \"hi\"");
67 lobby!.admin.record(who, "banned first");
68 lobby!.admin.record({ ...who, name: evil }, `muted ${evil}`);
69 lobby!.admin.record(who, "unbanned first");
70
71 const html = await (await fetch(`${base}/admin`, { headers: owner })).text();
72 const section = html.slice(html.indexOf('id="moderation"'), html.indexOf('id="log"'));
73 expect(section).not.toContain("<img");
74 expect(section).toContain("&#60;img src=x onerror=alert(1)&#62;");
75 expect(section).not.toContain("announcement");
76 expect(section.indexOf("unbanned first")).toBeLessThan(section.indexOf("muted"));
77 expect(section.indexOf("muted")).toBeLessThan(section.indexOf("</b> banned first"));
78
79 expect((await fetch(`${base}/admin`, { headers: as("bob", "user") })).status).not.toBe(200);
80 });
Software/so101/src/lobby.test.ts +19
@@ -278,6 +278,22 @@ describe("lobby", () => {
278278 expect((await late.first((m) => m.type === "chat")).messages).toEqual([]);
279279 });
280280
281 test("a mute and an unmute are logged and listed under Moderation on /admin, and open admin views hear of them", async () => {
282 const port = await start();
283 const alice = await join(port, ALICE);
284 const owner = await join(port, OWNER);
285 alice.send({ type: "post", text: "hi" });
286 await owner.next((m) => m.type === "said");
287 owner.send({ type: "mute", id: "u-alice", on: true });
288 expect((await owner.next((m) => m.type === "admin" && m.log.some((e: Msg) => e.what === "muted alice"))).log.at(-1).by.name).toBe("trevato");
289 owner.send({ type: "mute", id: "u-alice", on: false });
290 await owner.next((m) => m.type === "admin" && m.log.some((e: Msg) => e.what === "unmuted alice"));
291 const html = await (await fetch(`http://127.0.0.1:${port}/admin`, { headers: OWNER })).text();
292 const section = html.slice(html.indexOf('id="moderation"'), html.indexOf('id="log"'));
293 expect(section.indexOf("unmuted alice")).toBeGreaterThan(0);
294 expect(section.indexOf("unmuted alice")).toBeLessThan(section.indexOf("</b> muted alice"));
295 });
296
281297 test("a 10-minute mute reaches the owner's list with its end, refuses a bad length, and ends on its own", async () => {
282298 let clock = 1_000_000;
283299 const chat = new Chat({ now: () => clock });
@@ -325,6 +341,9 @@ describe("lobby", () => {
325341 expect((await alice.next((m) => m.type === "unsaid")).id).toBe(said.message.id);
326342 expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${said.message.id}` });
327343 expect((await owner.next((m) => m.type === "admin" && m.log.at(-1).by.name === "bob")).log.at(-1).what).toContain("deleted chat line");
344 const html = await (await fetch(`http://127.0.0.1:${port}/admin`, { headers: OWNER })).text();
345 const section = html.slice(html.indexOf('id="moderation"'), html.indexOf('id="log"'));
346 expect(section).toContain(`<b>bob</b> deleted chat line ${said.message.id}`);
328347 });
329348
330349 test("the owner's delete is logged with the owner as who did it, and a delete without an integer id is refused", async () => {
Software/so101/src/lobby.ts +2
@@ -356,6 +356,8 @@ export class Lobby {
356356 const span = MUTE_FOR[m.for ?? "forever"];
357357 if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span);
358358 else this.chat.unmute(id);
359 this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`);
360 this.broadcastAdmin();
359361 for (const s of this.seats) {
360362 if (s.who?.id === id) this.send(s, this.hello(s));
361363 if (s.who?.owner) this.send(s, this.mutedList());

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs