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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Each other person's chat line has an accessible 44 px Report button that sends {type:'report',id} and turns disabled 'Reported' only on the server's 'reported' frame - page module and test only #394

Closed opened this
Closed without merging

smith/arm:smith/317-each-other-person-s-chat-line-has-an-acc → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +88 −1
Software/so101/src/ui/lobby-panel.js +16
@@ -22,6 +22,9 @@ export function queueEntry(el, x, mine) {
2222 return el("li", { className: mine ? "me" : "" }, el("span", { className: "place", textContent: `${x.place}.` }), el("span", { textContent: mine ? `${x.name} (you)` : x.name }), el("span", { className: "wait", textContent: `≤ ${waitMinutes(x.waitMs)} min` }));
2323 }
2424
25 /** Only a signed-in person reports, and only what someone else said. @param {any} m a chat message @param {any} me hello.you */
26 export const mayReport = (m, me) => Boolean(me && m.from && m.from.id !== me.id);
27
2528 /** @param {number} attempt 1 for the first retry @returns {number} ms to wait: 250, 500, 1000, then 2000 at most */
2629 export const backoff = (attempt) => Math.min(250 * 2 ** Math.max(0, attempt - 1), 2000);
2730
@@ -175,6 +178,12 @@ export function createRoom(page) {
175178 button.disabled = false;
176179 };
177180 const chatBox = $("chat");
181 /** Ids the server confirmed as reported: only its "reported" frame changes a button. @type {Set<number>} */
182 const reported = new Set();
183 /** @param {any} b */
184 const done = (b) => { b.disabled = true; b.textContent = "Reported"; b.setAttribute("aria-label", `Reported ${b.name}`); };
185 /** @param {number} id */
186 const markReported = (id) => { reported.add(id); const b = chatBox.querySelector(`[data-id="${id}"] .report`); if (b) done(b); };
178187 /** @param {any} m */
179188 function chatLine(m) {
180189 const me = lobby.hello?.you;
@@ -183,6 +192,12 @@ export function createRoom(page) {
183192 if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); }
184193 line.append(el("span", { textContent: m.text }));
185194 if (m.from && canDelete()) line.tabIndex = 0;
195 if (mayReport(m, me)) { // always drawn, so the line never shifts when it is reported
196 const b = el("button", { type: "button", className: "report", textContent: "Report", name: m.from.name, onclick: () => say({ type: "report", id: Number(m.id) }) });
197 b.setAttribute("aria-label", `Report ${m.from.name}`); b.setAttribute("aria-live", "polite");
198 line.classList.add("rep"); line.append(b);
199 if (reported.has(Number(m.id))) done(b);
200 }
186201 return line;
187202 }
188203 // A new page gets the last lines of the chat; older ones come a page at a time, when asked for.
@@ -265,6 +280,7 @@ export function createRoom(page) {
265280 else if (m.type === "history") return addOlder(m.messages, m.more);
266281 else if (m.type === "said") return addChat([m.message]);
267282 else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove();
283 else if (m.type === "reported") return markReported(Number(m.id));
268284 else if (m.type === "muted") lobby.mutedList = m.muted;
269285 else if (m.type === "banned") lobby.bannedList = m.banned;
270286 else if (m.type === "words") page.words(m);
Software/so101/src/ui/lobby-panel.test.ts +70 −1
@@ -1,5 +1,5 @@
11 import { describe, expect, test } from "bun:test";
2 import { backoff, createRoom, minutes, ordinal, queueEntry, waitMinutes } from "./lobby-panel.js";
2 import { backoff, createRoom, mayReport, minutes, ordinal, queueEntry, waitMinutes } from "./lobby-panel.js";
33
44 /** A stand-in for the page's el(): elements as plain trees, so the test needs no DOM. */
55 const el = (tag: string, props: Record<string, unknown> = {}, ...children: unknown[]) => ({ tag, ...props, children });
@@ -193,3 +193,72 @@ describe("the chat's scroll", () => {
193193 } finally { t.restore(); }
194194 });
195195 });
196
197 describe("reporting a chat line", () => {
198 const ann = { id: "a", name: "ann" }, bob = { id: "b", name: "bob" };
199 test("only a signed-in person reports, and only someone else's line", () => {
200 expect(mayReport({ from: bob }, ann)).toBe(true);
201 expect(mayReport({ from: ann }, ann)).toBe(false);
202 expect(mayReport({ from: bob }, null)).toBe(false);
203 expect(mayReport({ id: 1, text: "system" }, ann)).toBe(false);
204 });
205
206 function setup(you: object | null) {
207 const sockets: any[] = [];
208 const g = globalThis as any, saved = { WebSocket: g.WebSocket, setInterval: g.setInterval, location: g.location };
209 g.WebSocket = class { readyState = 1; sent: string[] = []; constructor() { sockets.push(this); } send(m: string) { this.sent.push(m); } };
210 g.setInterval = () => 0;
211 g.location = { protocol: "http:", host: "x", pathname: "/", search: "" };
212 const nodes = new Map<string, any>();
213 const lines: any[] = [];
214 const n = (): any => new Proxy({ children: [], classList: { toggle() {}, add() {}, remove() {}, contains: () => false }, style: { setProperty() {} }, dataset: {}, attrs: {} }, {
215 get: (t: any, k) => (k in t ? t[k] : (() => n())), set: (t: any, k, v) => ((t[k] = v), true),
216 });
217 const box = Object.assign(n(), {
218 append: (...x: any[]) => void lines.push(...x), replaceChildren: () => void (lines.length = 0), prepend() {},
219 querySelectorAll: () => lines, scrollHeight: 0, clientHeight: 0, scrollTop: 0,
220 querySelector: (q: string) => lines.find((l) => q === `[data-id="${l.dataset.id}"] .report`)?.btn,
221 });
222 nodes.set("chat", box);
223 const page = {
224 $: (id: string) => nodes.get(id) ?? (nodes.set(id, n()), nodes.get(id)),
225 el: (tag: string, props: object = {}) => {
226 const e = Object.assign(n(), { tag, btn: null }, props);
227 e.setAttribute = (k: string, v: string) => (e.attrs[k] = v);
228 if (tag === "div") e.append = (...c: any[]) => { for (const x of c) if (x.tag === "button") e.btn = x; };
229 return e;
230 },
231 relayed: true, tab: () => "chat", showTab() {}, applyLock() {}, applyStatus() {}, noStatus() {}, words() {}, admin() {}, msg() {},
232 };
233 const room = createRoom(page);
234 room.lobbyConnect();
235 const ws = sockets[0];
236 const recv = (m: object) => ws.onmessage({ data: JSON.stringify(m) });
237 recv({ type: "hello", signin: true, you });
238 recv({ type: "chat", more: false, messages: [{ id: 1, at: 0, text: "hi", from: bob }, { id: 2, at: 0, text: "yo", from: ann }] });
239 return { ws, recv, lines, restore: () => Object.assign(g, saved) };
240 }
241
242 test("the button is on the other person's line only, and not for guests", () => {
243 const t = setup(ann);
244 try {
245 expect(t.lines.map((l) => l.btn?.tag === "button")).toEqual([true, false]);
246 expect(t.lines[0].btn.attrs["aria-label"]).toContain("bob");
247 } finally { t.restore(); }
248 const g = setup(null);
249 try { expect(g.lines.map((l) => l.btn?.tag === "button")).toEqual([false, false]); } finally { g.restore(); }
250 });
251
252 test("a click sends {type:report,id}; only a reported frame changes the button, an error leaves it", () => {
253 const t = setup(ann);
254 try {
255 const b = t.lines[0].btn;
256 b.onclick();
257 expect(t.ws.sent).toEqual([JSON.stringify({ type: "report", id: 1 })]);
258 t.recv({ type: "error", error: "too many reports" });
259 expect([b.disabled === true, b.textContent]).toEqual([false, "Report"]);
260 t.recv({ type: "reported", id: 1 });
261 expect([b.disabled, b.textContent, b.attrs["aria-live"]]).toEqual([true, "Reported", "polite"]);
262 } finally { t.restore(); }
263 });
264 });
Software/so101/src/viewer.html +2
@@ -137,6 +137,8 @@
137137 #chat { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; padding: 10px calc(12px + var(--safe-r)) 10px calc(12px + var(--safe-l)); display: flex; flex-direction: column; gap: 4px; overflow-wrap: anywhere; }
138138 #chat .line { line-height: 1.45; padding: 4px 8px; margin: 0 -8px; border-radius: 8px; }
139139 #chat .line b { color: var(--fg2); font-weight: 650; margin-right: 6px; } #chat .line.me b { color: var(--link); }
140 #chat .rep { display: flex; align-items: center; gap: 8px; min-height: 44px; } #chat .rep span { flex: 1; min-width: 0; }
141 #chat .report { min-width: 44px; min-height: 44px; }
140142 #chat .line.held { background: var(--blue-soft); }
141143 #chat.moderated .line:not(.sys) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: context-menu; }
142144 #newMsgs { align-self: center; margin: -38px 0 8px; position: relative; z-index: 1; border-radius: 999px; padding: 5px 14px; font-size: 13px; background: var(--link); color: #fff; border: 0; box-shadow: 0 2px 8px rgba(0,0,0,.4); }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs