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
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/237-admin-shows-the-moderation-actions-from → main 5 commits fork 2 threads · 2 unresolved
5 files
changed +55
−3
Software/README.md +3
| @@ -534,6 +534,9 @@ or from the **Admin** tab that only the owner sees: | ||
| 534 | 534 | Settings are web-only because this app has no terminal (`forge.json` has no |
| 535 | 535 | `tty`); the `so101` command line drives the arm hardware and does not run |
| 536 | 536 | 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. | |
| 537 | 540 | |
| 538 | 541 | The settings take effect at once and survive a restart: they are stored in |
| 539 | 542 | `$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) => { | ||
| 95 | 95 | return `<time datetime="${iso}">${iso.slice(5, 10)} ${iso.slice(11, 16)}</time>`; |
| 96 | 96 | }; |
| 97 | 97 | |
| 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 | ||
| 98 | 102 | const field = (name: keyof AdminSettings, label: string, hint: string, value: number, min: number, max: number, off: string) => |
| 99 | 103 | `<label>${label}<small>${hint}</small><input name="${name}" type="number" inputmode="numeric" required min="${min}" max="${max}" step="1" value="${value}"${off}></label>`; |
| 100 | 104 | |
| 101 | 105 | const action = (name: string, text: string, cls = "", disabled = false) => |
| 102 | 106 | `<form method="post" action="/admin"><input type="hidden" name="action" value="${name}"><button class="${cls}"${disabled ? " disabled" : ""}>${text}</button></form>`; |
| 103 | 107 | |
| 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 | ||
| 104 | 111 | export function renderAdmin(p: AdminPage): string { |
| 105 | 112 | const s = p.settings; |
| 106 | 113 | const d = p.snapshot.driver; |
| @@ -109,9 +116,8 @@ export function renderAdmin(p: AdminPage): string { | ||
| 109 | 116 | 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>` + |
| 110 | 117 | (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>` : ""); |
| 111 | 118 | 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."); | |
| 115 | 121 | return page( |
| 116 | 122 | "Admin · SO-101", |
| 117 | 123 | `<header> |
| @@ -141,6 +147,7 @@ ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div> | ||
| 141 | 147 | </form> |
| 142 | 148 | </section> |
| 143 | 149 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 150 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section> | |
| 144 | 151 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 145 | 152 | ); |
| 146 | 153 | } |
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 | ||
| 57 | 57 | expect(after).toContain("back at five"); |
| 58 | 58 | expect(after).not.toContain("hijacked"); |
| 59 | 59 | }); |
| 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("<img src=x onerror=alert(1)>"); | |
| 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", () => { | ||
| 278 | 278 | expect((await late.first((m) => m.type === "chat")).messages).toEqual([]); |
| 279 | 279 | }); |
| 280 | 280 | |
| 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 | ||
| 281 | 297 | test("a 10-minute mute reaches the owner's list with its end, refuses a bad length, and ends on its own", async () => { |
| 282 | 298 | let clock = 1_000_000; |
| 283 | 299 | const chat = new Chat({ now: () => clock }); |
| @@ -325,6 +341,9 @@ describe("lobby", () => { | ||
| 325 | 341 | expect((await alice.next((m) => m.type === "unsaid")).id).toBe(said.message.id); |
| 326 | 342 | expect(admin.log.at(-1)).toMatchObject({ by: { id: "u-bob", name: "bob" }, what: `deleted chat line ${said.message.id}` }); |
| 327 | 343 | 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}`); | |
| 328 | 347 | }); |
| 329 | 348 | |
| 330 | 349 | 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 { | ||
| 356 | 356 | const span = MUTE_FOR[m.for ?? "forever"]; |
| 357 | 357 | if (m.on) this.chat.mute(id, name, span === null ? null : this.now() + span); |
| 358 | 358 | else this.chat.unmute(id); |
| 359 | this.admin.record(who, `${m.on ? "muted" : "unmuted"} ${name}`); | |
| 360 | this.broadcastAdmin(); | |
| 359 | 361 | for (const s of this.seats) { |
| 360 | 362 | if (s.who?.id === id) this.send(s, this.hello(s)); |
| 361 | 363 | if (s.who?.owner) this.send(s, this.mutedList()); |
new above
offline
Regex misses
deleted chat line N(lobby.ts:408);removedis never logged. Usedeleted chat lineinstead and test it with a real delete.