Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/admin's Moderation section shows an Unmute or Unban button beside each mute or ban in force, posting to /admin/lift #377
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/357-admin-s-moderation-section-shows-an-unmu → main 2 commits fork
4 files
changed +48
−5
Software/so101/src/admin-page.test.ts +28
| @@ -33,3 +33,31 @@ test("the Table height field is the owner's alone", () => { | ||
| 33 | 33 | expect(mod).not.toContain("table-mm"); |
| 34 | 34 | expect(mod).not.toContain("tableOffsetMm"); |
| 35 | 35 | }); |
| 36 | ||
| 37 | test("a mute or ban in force gets an Unmute or Unban form to /admin/lift; none, none", () => { | |
| 38 | const base = { who: { id: "o", name: "owner", owner: true }, canEdit: true, settings: new Admin().settings, snapshot: new Turns().snapshot(), log: [] }; | |
| 39 | const html = renderAdmin({ ...base, muted: [{ id: "u2", name: "<b>mo</b>" }], banned: [{ id: "u3", name: "bo" }] }); | |
| 40 | expect(html).toContain('action="/admin/lift"'); | |
| 41 | expect(html).toContain('name="kind" value="mute"><input type="hidden" name="id" value="u2"><button aria-label="Unmute <b>mo</b>">Unmute</button>'); | |
| 42 | expect(html).toContain('name="kind" value="ban"><input type="hidden" name="id" value="u3"><button aria-label="Unban bo">Unban</button>'); | |
| 43 | expect(html).not.toContain("<b>mo</b>"); | |
| 44 | expect(html.match(/<script/g)?.length).toBe(1); | |
| 45 | const none = renderAdmin(base); | |
| 46 | expect(none).not.toContain("Unmute"); | |
| 47 | expect(none).not.toContain("Unban"); | |
| 48 | expect(none).not.toContain("/admin/lift"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("a moderator sees no Unmute or Unban beside a moderator's mute or ban; the owner does", () => { | |
| 52 | const settings = { ...new Admin().settings, moderators: ["bob"] }; | |
| 53 | const base = { settings, snapshot: new Turns().snapshot(), log: [], muted: [{ id: "u2", name: "Bob" }, { id: "u4", name: "cy" }], banned: [{ id: "u3", name: "bob" }] }; | |
| 54 | const mod = renderAdmin({ ...base, who: { id: "m", name: "bob", owner: false }, canEdit: false }); | |
| 55 | expect(mod.match(/Unmute<\/button>/g)?.length).toBe(1); | |
| 56 | expect(mod).toContain('value="u4"'); | |
| 57 | expect(mod).not.toContain('value="u2"'); | |
| 58 | expect(mod).not.toContain("Unban"); | |
| 59 | expect(mod).toContain("Bob"); | |
| 60 | const owner = renderAdmin({ ...base, who: { id: "o", name: "owner", owner: true }, canEdit: true }); | |
| 61 | expect(owner.match(/Unmute<\/button>/g)?.length).toBe(2); | |
| 62 | expect(owner).toContain("Unban"); | |
| 63 | }); |
Software/so101/src/admin-page.ts +17 −2
| @@ -124,9 +124,11 @@ export const adminCsp = (canEdit: boolean): string => | ||
| 124 | 124 | `default-src 'none'; style-src 'sha256-${sha(STYLE)}'; script-src 'sha256-${sha(scriptFor(canEdit))}'; connect-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`; |
| 125 | 125 | |
| 126 | 126 | /** The fixed lines a redirect back may ask for; nothing from the request is echoed. */ |
| 127 | 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; | |
| 127 | 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.", lifted: "Lifted." } as const; | |
| 128 | 128 | export type Done = keyof typeof DONE; |
| 129 | 129 | |
| 130 | export interface Target { id: string; name: string } | |
| 131 | ||
| 130 | 132 | export interface AdminPage { |
| 131 | 133 | who: Who; |
| 132 | 134 | /** The owner may change things; a moderator only looks. */ |
| @@ -138,6 +140,9 @@ export interface AdminPage { | ||
| 138 | 140 | log: readonly LogEntry[]; |
| 139 | 141 | /** Chat lines a person removed, newest first. */ |
| 140 | 142 | removed?: readonly Removed[]; |
| 143 | /** Mutes and bans in force, each with a button to lift it. */ | |
| 144 | muted?: readonly Target[]; | |
| 145 | banned?: readonly Target[]; | |
| 141 | 146 | done?: Done | null; |
| 142 | 147 | error?: string | null; |
| 143 | 148 | } |
| @@ -193,6 +198,14 @@ const removedLines = (list: readonly Removed[], canEdit: boolean): string => | ||
| 193 | 198 | ? `<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>` |
| 194 | 199 | : `<p class="empty">No removed lines.</p>`; |
| 195 | 200 | |
| 201 | /** Who is muted or banned now, each with a form button posting to /admin/lift. */ | |
| 202 | const inForce = (kind: "mute" | "ban", list: readonly Target[], canLift: (name: string) => boolean): string => { | |
| 203 | const verb = kind === "mute" ? "Unmute" : "Unban"; | |
| 204 | return list.length | |
| 205 | ? `<ul>${list.map((t) => `<li>${esc(t.name)}${canLift(t.name) ? ` <form method="post" action="/admin/lift" class="inline"><input type="hidden" name="kind" value="${kind}"><input type="hidden" name="id" value="${esc(t.id)}"><button aria-label="${verb} ${esc(t.name)}">${verb}</button></form>` : ""}</li>`).join("")}</ul>` | |
| 206 | : ""; | |
| 207 | }; | |
| 208 | ||
| 196 | 209 | export function renderAdmin(p: AdminPage): string { |
| 197 | 210 | const s = p.settings; |
| 198 | 211 | const d = p.snapshot.driver; |
| @@ -201,6 +214,8 @@ export function renderAdmin(p: AdminPage): string { | ||
| 201 | 214 | 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>` + |
| 202 | 215 | (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>` : ""); |
| 203 | 216 | 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>` : ""; |
| 217 | // only the owner can lift a mute or ban on a moderator: /admin/lift refuses anyone else | |
| 218 | const canLift = (name: string) => p.canEdit || !s.moderators.includes(name.toLowerCase()); | |
| 204 | 219 | const actions = entries(p.log.filter(isModeration), "No moderation actions yet."); |
| 205 | 220 | const log = entries(p.log, "Nothing yet."); |
| 206 | 221 | return page( |
| @@ -237,7 +252,7 @@ ${p.canEdit ? `<section aria-labelledby="table"><h2 id="table">Table height</h2> | ||
| 237 | 252 | </section>` : ""} |
| 238 | 253 | <section aria-labelledby="health"><h2 id="health">Health</h2>${health(p.health ?? [])}</section> |
| 239 | 254 | <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section> |
| 240 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section> | |
| 255 | <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${inForce("mute", p.muted ?? [], canLift)}${inForce("ban", p.banned ?? [], canLift)}${actions}</section> | |
| 241 | 256 | <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section> |
| 242 | 257 | <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`, |
| 243 | 258 | p.canEdit, |
Software/so101/src/admin-relay.test.ts +1 −1
| @@ -142,7 +142,7 @@ test("POST /admin/lift lifts a mute or ban for the owner or a moderator, logged; | ||
| 142 | 142 | expect((await lift(bob, { kind: "mute", id: "u2" })).status).toBe(403); // a moderator's own mute stays with the owner |
| 143 | 143 | const ok = await lift(bob, { kind: "ban", id: "u3" }); |
| 144 | 144 | expect(ok.status).toBe(303); |
| 145 | expect(ok.headers.get("location")).toBe("/admin"); | |
| 145 | expect(ok.headers.get("location")).toBe("/admin?done=lifted"); | |
| 146 | 146 | expect(lobby!.accounts.bans().map((b) => b.id)).toEqual(["u4"]); |
| 147 | 147 | expect(lobby!.admin.log.at(-1)).toMatchObject({ what: "unbanned dave", by: { name: "bob" } }); |
| 148 | 148 | expect((await lift(bob, { kind: "ban", id: "u3" })).status).toBe(404); |
Software/so101/src/admin-routes.ts +2 −2
| @@ -45,7 +45,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe | ||
| 45 | 45 | return null; |
| 46 | 46 | } |
| 47 | 47 | const show = (status: number, done: Done | null, error: string | null) => |
| 48 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, health: lobby.reports.rows(), done, error }), canEdit); | |
| 48 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), done, error }), canEdit); | |
| 49 | 49 | if (req.method === "GET" || req.method === "HEAD") { |
| 50 | 50 | const done = url.searchParams.get("done"); |
| 51 | 51 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
| @@ -162,6 +162,6 @@ export async function handleLift(lobby: Lobby, req: IncomingMessage, res: Server | ||
| 162 | 162 | if (kind === "mute") lobby.chat.unmute(id); |
| 163 | 163 | else lobby.accounts.unban(id); |
| 164 | 164 | lobby.admin.record(who, `${kind === "mute" ? "unmuted" : "unbanned"} ${name}`); |
| 165 | res.writeHead(303, { location: "/admin", "cache-control": "no-store" }); | |
| 165 | res.writeHead(303, { location: "/admin?done=lifted", "cache-control": "no-store" }); | |
| 166 | 166 | res.end(); |
| 167 | 167 | } |
new above
offline