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 #339

Parked opened this
Closed without merging

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

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +94 −1
Software/so101/src/ui/lobby-panel.js +21
@@ -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,16 @@ 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 {number} id */
184 function markReported(id) {
185 reported.add(id);
186 const b = chatBox.querySelector(`[data-id="${id}"] .report`);
187 if (!b) return;
188 b.disabled = true; b.textContent = "Reported";
189 b.setAttribute("aria-label", `Reported ${b.closest(".line")?.dataset.name}'s message`);
190 }
178191 /** @param {any} m */
179192 function chatLine(m) {
180193 const me = lobby.hello?.you;
@@ -183,6 +196,13 @@ export function createRoom(page) {
183196 if (m.from) { line.dataset.from = m.from.id; line.dataset.name = m.from.name; line.append(el("b", { textContent: m.from.name })); }
184197 line.append(el("span", { textContent: m.text }));
185198 if (m.from && canDelete()) line.tabIndex = 0;
199 if (mayReport(m, me)) { // always drawn, so the line never shifts when it is reported
200 const done = reported.has(Number(m.id));
201 const b = el("button", { type: "button", className: "report", textContent: done ? "Reported" : "Report", disabled: done, onclick: () => say({ type: "report", id: Number(m.id) }) });
202 b.setAttribute("aria-label", `${done ? "Reported" : "Report"} ${m.from.name}'s message`);
203 b.setAttribute("aria-live", "polite");
204 line.classList.add("rep"); line.append(b);
205 }
186206 return line;
187207 }
188208 // A new page gets the last lines of the chat; older ones come a page at a time, when asked for.
@@ -265,6 +285,7 @@ export function createRoom(page) {
265285 else if (m.type === "history") return addOlder(m.messages, m.more);
266286 else if (m.type === "said") return addChat([m.message]);
267287 else if (m.type === "unsaid") return chatBox.querySelector(`[data-id="${Number(m.id)}"]`)?.remove();
288 else if (m.type === "reported") return markReported(Number(m.id));
268289 else if (m.type === "muted") lobby.mutedList = m.muted;
269290 else if (m.type === "banned") lobby.bannedList = m.banned;
270291 else if (m.type === "words") page.words(m);
Software/so101/src/ui/lobby-panel.test.ts +71 −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,73 @@ 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 if (tag === "button") e.closest = () => ({ dataset: { name: "bob" } });
230 return e;
231 },
232 relayed: true, tab: () => "chat", showTab() {}, applyLock() {}, applyStatus() {}, noStatus() {}, words() {}, admin() {}, msg() {},
233 };
234 const room = createRoom(page);
235 room.lobbyConnect();
236 const ws = sockets[0];
237 const recv = (m: object) => ws.onmessage({ data: JSON.stringify(m) });
238 recv({ type: "hello", signin: true, you });
239 recv({ type: "chat", more: false, messages: [{ id: 1, at: 0, text: "hi", from: bob }, { id: 2, at: 0, text: "yo", from: ann }] });
240 return { ws, recv, lines, restore: () => Object.assign(g, saved) };
241 }
242
243 test("the button is on the other person's line only, and not for guests", () => {
244 const t = setup(ann);
245 try {
246 expect(t.lines.map((l) => l.btn?.tag === "button")).toEqual([true, false]);
247 expect(t.lines[0].btn.attrs["aria-label"]).toContain("bob");
248 } finally { t.restore(); }
249 const g = setup(null);
250 try { expect(g.lines.map((l) => l.btn?.tag === "button")).toEqual([false, false]); } finally { g.restore(); }
251 });
252
253 test("a click sends {type:report,id}; only a reported frame changes the button, an error leaves it", () => {
254 const t = setup(ann);
255 try {
256 const b = t.lines[0].btn;
257 b.onclick();
258 expect(t.ws.sent).toEqual([JSON.stringify({ type: "report", id: 1 })]);
259 t.recv({ type: "error", error: "too many reports" });
260 expect([b.disabled, b.textContent]).toEqual([false, "Report"]);
261 t.recv({ type: "reported", id: 1 });
262 expect([b.disabled, b.textContent, b.attrs["aria-live"]]).toEqual([true, "Reported", "polite"]);
263 } finally { t.restore(); }
264 });
265 });
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 .line.rep { display: flex; align-items: center; gap: 8px; min-height: 44px; } #chat .line.rep > span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
141 #chat .line .report { flex: none; 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