// Abuse reports in the browser: the notice on a flagged listing, the form a // visitor uses to tip off the node's operator, and the operator's own report // form and queue of tips. Nothing here hides anything: a node's rules do, and // they are the operator's (see "Abuse reports" in docs/PROTOCOL.md). import { HttpError, getReportSummary, postSignal, type ReportSummary } from "./api.ts"; import { append, clear, h } from "./dom.ts"; import { t, type Key } from "./i18n.ts"; import { canonicalize } from "./jcs.ts"; import { DEFAULT_MESSAGE_DIFFICULTY, mine } from "./message.ts"; import { renderLogin } from "./owner.ts"; import { Unauthorized, dismissTips, getToken, getTips, signReport, type Tip } from "./ownerApi.ts"; import { nowSeconds, state } from "./state.ts"; import type { Listing } from "./verify.ts"; /** The reasons a visitor or an operator can give, in the order they are offered. */ const REASONS = ["scam", "illegal", "category", "other"] as const; const MAX_NOTE_BYTES = 300; const reasonLabel = (c: string): string => (REASONS as readonly string[]).includes(c) ? t(`rep.cat.${c}` as Key) : c; const utf8 = (s: string) => new TextEncoder().encode(s).length; /** * Tells the node's operator about a listing. A visitor has no key, so the * request is not signed: it is backed by proof-of-work, mined here like the * proof of a message, and the node keeps it for its operator only. */ async function sendTip(author: string, id: string, category: string, note: string): Promise { const bits = state.node?.message_difficulty ?? DEFAULT_MESSAGE_DIFFICULTY; // Keys in canonical order; `pow` must be followed by another key (see mine()). const body = { category, created_at: nowSeconds(), listing: { author, id }, note, pow: 0, v: 1 }; await mine(body, bits); await postSignal(canonicalize(body)); } function describe(e: unknown): string { if (e instanceof Unauthorized) return t("reg.signedOut"); if (e instanceof HttpError) return e.detail; return t("send.network"); } /** A select of reasons and a note box, with the way to read them back. */ function reasonFields(): { nodes: HTMLElement[]; read: () => { category: string; note: string } } { const select = h("select", { name: "category" }, ...REASONS.map((c) => h("option", { value: c }, reasonLabel(c)))); const note = h("textarea", { name: "note", rows: 3, maxlength: MAX_NOTE_BYTES, "aria-label": t("rep.note"), placeholder: t("rep.note") }); return { nodes: [h("label", null, t("rep.reason"), " ", select), note], read: () => ({ category: select.value, note: note.value.trim() }), }; } /** What this node makes of the reports about the listing, as a few lines of text. */ function summaryLines(sum: ReportSummary): HTMLElement[] { const out: HTMLElement[] = []; if (sum.hidden) out.push(h("p", { class: "note warn", role: "status" }, t("rep.hiddenNotice"))); const counted = Object.entries(sum.trusted).filter(([, n]) => n > 0); if (counted.length) { out.push(h("p", { class: "muted" }, t("rep.counts", { list: counted.map(([c, n]) => `${reasonLabel(c)} (${n})`).join(", ") }))); } if (sum.others > 0) out.push(h("p", { class: "muted small" }, t("rep.others", { n: sum.others }))); if (sum.own) out.push(h("p", { class: "muted" }, t("rep.mine", { what: reasonLabel(sum.own) }))); return out; } /** * The report section of a listing page. It fills in once the node has said what * it makes of the reports, and works without that: the forms are always there. */ export function reportPanel(l: Listing): HTMLElement { const lines = h("div", null); const root = h("section", { class: "panel report" }, lines, visitorForm(l)); const refresh = () => void getReportSummary(l.author, l.id).then((sum) => { if (!sum) return; clear(lines); append(lines, summaryLines(sum)); }); if (getToken()) root.append(operatorForm(l, refresh)); refresh(); return root; } function visitorForm(l: Listing): HTMLElement { const fields = reasonFields(); const status = h("p", { class: "muted", role: "status" }); const problem = h("p", { class: "error", role: "alert" }); const button = h("button", { type: "submit", class: "btn" }, t("rep.send")); const form = h( "form", { class: "compose", onsubmit: (ev: Event) => { ev.preventDefault(); const { category, note } = fields.read(); problem.textContent = ""; if (utf8(note) > MAX_NOTE_BYTES) { problem.textContent = t("rep.fail", { detail: t("rep.note") }); return; } button.disabled = true; status.textContent = t("rep.working"); void sendTip(l.author, l.id, category, note) .then(() => { clear(form); form.append(h("p", { role: "status" }, t("rep.thanks"))); }) .catch((e: unknown) => { button.disabled = false; status.textContent = ""; problem.textContent = t("rep.fail", { detail: describe(e) }); }); }, }, h("p", { class: "muted" }, t("rep.intro")), ...fields.nodes, h("div", { class: "actions" }, button), status, problem, ); return h("details", { class: "report-form" }, h("summary", null, t("rep.report")), form); } /** The operator's own signed report (or taking it back). Only offered with the admin token. */ function operatorForm(l: Listing, changed: () => void): HTMLElement { const fields = reasonFields(); const status = h("p", { class: "muted", role: "status" }); const problem = h("p", { class: "error", role: "alert" }); const act = (category: string, note: string, done: Key) => { problem.textContent = ""; void signReport(l.author, l.id, category, note) .then(() => { status.textContent = t(done); changed(); }) .catch((e: unknown) => { problem.textContent = t("rep.fail", { detail: describe(e) }); }); }; return h( "details", { class: "report-form" }, h("summary", null, t("rep.operator")), h( "form", { class: "compose", onsubmit: (ev: Event) => { ev.preventDefault(); const { category, note } = fields.read(); act(category, note, "rep.done"); }, }, h("p", { class: "muted" }, t("rep.operatorIntro")), ...fields.nodes, h( "div", { class: "actions" }, h("button", { type: "submit", class: "btn primary" }, t("rep.operatorSend")), h("button", { type: "button", class: "btn", onclick: () => act("withdrawn", "", "rep.withdrawn") }, t("rep.withdraw")), ), status, problem, ), ); } // --- the operator's queue ----------------------------------------------------------- /** The tips visitors left on this node, one card per listing. */ export async function showReports(): Promise { const root = document.getElementById("app")!; clear(root); if (!getToken()) { renderLogin(false); return; } const list = h("div", null); const problem = h("p", { class: "error", role: "alert" }); append(root, [ h("a", { class: "back", href: "#/mine" }, t("rep.back")), h("h1", null, t("rep.title")), h("p", { class: "muted" }, t("rep.tipsIntro")), problem, list, ]); const load = async (): Promise => { let tips: Tip[]; try { tips = await getTips(); } catch (e) { if (e instanceof Unauthorized) return renderLogin(true); problem.textContent = describe(e); return; } clear(list); if (tips.length === 0) { list.append(h("p", { class: "muted" }, t("rep.none"))); return; } const byListing = new Map(); for (const tip of tips) byListing.set(`${tip.author}/${tip.listing}`, [...(byListing.get(`${tip.author}/${tip.listing}`) ?? []), tip]); for (const group of byListing.values()) list.append(card(group, load, problem)); }; await load(); } function card(group: Tip[], reload: () => Promise, problem: HTMLElement): HTMLElement { const first = group[0]!; const fail = (e: unknown) => { problem.textContent = describe(e); }; const categories = [...new Set(group.map((g) => g.category))]; return h( "section", { class: "panel" }, h("h2", null, h("a", { href: `#/l/${first.author}/${first.listing}` }, first.title || t("rep.open"))), h( "ul", null, ...group.map((g) => h("li", null, `${reasonLabel(g.category)}: ${g.note || "–"} `, h("span", { class: "muted small" }, `(${t("rep.work", { n: g.work })})`)), ), ), h( "div", { class: "actions" }, ...categories.map((c) => h( "button", { type: "button", class: "btn", onclick: () => void signReport(first.author, first.listing, c, group.find((g) => g.category === c)?.note ?? "").then(reload).catch(fail) }, t("rep.sign", { what: reasonLabel(c) }), ), ), h("button", { type: "button", class: "btn", onclick: () => void dismissTips(first.author, first.listing).then(reload).catch(fail) }, t("rep.dismiss")), ), ); }