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

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/357-admin-s-moderation-section-shows-an-unmu → main 2 commits fork

  • Merged. smith/357-admin-s-moderation-section-shows-an-unmu was merged into main as 5906f01.

Review

Sign in to review this change.
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", () => {
3333 expect(mod).not.toContain("table-mm");
3434 expect(mod).not.toContain("tableOffsetMm");
3535 });
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 &#60;b&#62;mo&#60;/b&#62;">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 =>
124124 `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'`;
125125
126126 /** 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;
128128 export type Done = keyof typeof DONE;
129129
130 export interface Target { id: string; name: string }
131
130132 export interface AdminPage {
131133 who: Who;
132134 /** The owner may change things; a moderator only looks. */
@@ -138,6 +140,9 @@ export interface AdminPage {
138140 log: readonly LogEntry[];
139141 /** Chat lines a person removed, newest first. */
140142 removed?: readonly Removed[];
143 /** Mutes and bans in force, each with a button to lift it. */
144 muted?: readonly Target[];
145 banned?: readonly Target[];
141146 done?: Done | null;
142147 error?: string | null;
143148 }
@@ -193,6 +198,14 @@ const removedLines = (list: readonly Removed[], canEdit: boolean): string =>
193198 ? `<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>`
194199 : `<p class="empty">No removed lines.</p>`;
195200
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
196209 export function renderAdmin(p: AdminPage): string {
197210 const s = p.settings;
198211 const d = p.snapshot.driver;
@@ -201,6 +214,8 @@ export function renderAdmin(p: AdminPage): string {
201214 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>` +
202215 (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>` : "");
203216 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());
204219 const actions = entries(p.log.filter(isModeration), "No moderation actions yet.");
205220 const log = entries(p.log, "Nothing yet.");
206221 return page(
@@ -237,7 +252,7 @@ ${p.canEdit ? `<section aria-labelledby="table"><h2 id="table">Table height</h2>
237252 </section>` : ""}
238253 <section aria-labelledby="health"><h2 id="health">Health</h2>${health(p.health ?? [])}</section>
239254 <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>
241256 <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section>
242257 <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`,
243258 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;
142142 expect((await lift(bob, { kind: "mute", id: "u2" })).status).toBe(403); // a moderator's own mute stays with the owner
143143 const ok = await lift(bob, { kind: "ban", id: "u3" });
144144 expect(ok.status).toBe(303);
145 expect(ok.headers.get("location")).toBe("/admin");
145 expect(ok.headers.get("location")).toBe("/admin?done=lifted");
146146 expect(lobby!.accounts.bans().map((b) => b.id)).toEqual(["u4"]);
147147 expect(lobby!.admin.log.at(-1)).toMatchObject({ what: "unbanned dave", by: { name: "bob" } });
148148 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
4545 return null;
4646 }
4747 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);
4949 if (req.method === "GET" || req.method === "HEAD") {
5050 const done = url.searchParams.get("done");
5151 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
162162 if (kind === "mute") lobby.chat.unmute(id);
163163 else lobby.accounts.unban(id);
164164 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" });
166166 res.end();
167167 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs