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 #228
smith/arm:smith/220-admin-shows-the-moderation-actions-from → main 2 commits fork
2 files
changed +44
−0
Software/so101/src/admin-page.test.ts added +33
| @@ -0,0 +1,33 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { renderAdmin } from "./admin-page.ts"; | |
| 3 | import { DEFAULT_ADMIN, type LogEntry } from "./admin.ts"; | |
| 4 | import { Turns } from "./turns.ts"; | |
| 5 | ||
| 6 | const entry = (at: number, name: string, what: string): LogEntry => ({ at, by: { id: `u-${name}`, name }, what }); | |
| 7 | const render = (log: LogEntry[]) => | |
| 8 | renderAdmin({ who: { id: "u-o", name: "owner", owner: true }, canEdit: true, settings: { ...DEFAULT_ADMIN, moderators: [] }, snapshot: new Turns().snapshot(), log }); | |
| 9 | const section = (html: string) => html.split('id="modlog"')[1]!.split("</section>")[0]!; | |
| 10 | ||
| 11 | test("moderation lists bans and their reversals newest first, with who and when, and nothing else", () => { | |
| 12 | const html = section(render([ | |
| 13 | entry(Date.parse("2026-01-02T03:04:00Z"), "ann", "banned bob"), | |
| 14 | entry(Date.parse("2026-01-02T05:06:00Z"), "cy", "took control"), | |
| 15 | entry(Date.parse("2026-01-03T07:08:00Z"), "dee", "unbanned bob"), | |
| 16 | ])); | |
| 17 | expect(html.indexOf("unbanned bob")).toBeGreaterThan(-1); | |
| 18 | expect(html.indexOf("unbanned bob")).toBeLessThan(html.indexOf("banned bob</span>")); | |
| 19 | expect(html).toContain("<b>ann</b>"); | |
| 20 | expect(html).toContain("01-03 07:08"); | |
| 21 | expect(html).not.toContain("took control"); | |
| 22 | }); | |
| 23 | ||
| 24 | test("names and text in the moderation list are escaped", () => { | |
| 25 | const html = section(render([entry(1, "<img onerror=x>", "banned <script>alert(1)</script>")])); | |
| 26 | expect(html).not.toContain("<img"); | |
| 27 | expect(html).not.toContain("<script>"); | |
| 28 | expect(html).toContain("<img onerror=x>"); | |
| 29 | }); | |
| 30 | ||
| 31 | test("an empty moderation log says so", () => { | |
| 32 | expect(section(render([]))).toContain("No moderation yet."); | |
| 33 | }); |
Software/so101/src/admin-page.ts +11
| @@ -95,6 +95,16 @@ const time = (at: number) => { | ||
| 95 | 95 | return `<time datetime="${iso}">${iso.slice(5, 10)} ${iso.slice(11, 16)}</time>`; |
| 96 | 96 | }; |
| 97 | 97 | |
| 98 | /** The log lines that are moderation: removing, muting, banning, and undoing any of them. */ | |
| 99 | const MODERATION = /^(removed|deleted|(un)?muted|(un)?banned)\b/; | |
| 100 | ||
| 101 | const moderation = (log: readonly LogEntry[]): string => { | |
| 102 | const rows = log.filter((e) => MODERATION.test(e.what)); | |
| 103 | return rows.length | |
| 104 | ? `<ol>${[...rows].reverse().map((e) => `<li>${time(e.at)}<span><b>${esc(e.by.name)}</b> ${esc(e.what)}</span></li>`).join("")}</ol>` | |
| 105 | : `<p class="empty">No moderation yet.</p>`; | |
| 106 | }; | |
| 107 | ||
| 98 | 108 | const field = (name: keyof AdminSettings, label: string, hint: string, value: number, min: number, max: number, off: string) => |
| 99 | 109 | `<label>${label}<small>${hint}</small><input name="${name}" type="number" inputmode="numeric" required min="${min}" max="${max}" step="1" value="${value}"${off}></label>`; |
| 100 | 110 | |
| @@ -140,6 +150,7 @@ ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div> | ||
| 140 | 150 | </form> |
| 141 | 151 | </section> |
| 142 | 152 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 153 | <section aria-labelledby="modlog"><h2 id="modlog">Moderation</h2>${moderation(p.log)}</section> | |
| 143 | 154 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 144 | 155 | ); |
| 145 | 156 | } |
new above
offline