// MUTES: "stop showing me this kind of call on this piece" (plans/umtool-redesign.md, // P3 follow-up). Beside the lifecycle, and the same shape of store: // // REPORTS_ROOT/.umtool/mutes.json // { format: "umtool-mutes", version: 1, // mutes: { "": { "": { at } } } } // // A mute is not a ruling: the decision stays in the API, one row per decision, // marked `hidden` with `hiddenBy: "muted"` (lib/inbox/groups.mjs annotate), and // the Inbox lists it under "Muted N" with Unmute. Unmuting removes the entry. // // STATE, NOT CACHE (the walk skips dot-dirs). One writer, serialised // in-process; every write is a whole file to a temp name and a rename. A file // that does not parse is never overwritten: a write refuses (409) until it is // fixed; a read treats it as "nothing muted" and says so in `error`. // // Plain ESM, so a CLI and a unit test read it the way the app does. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { isPieceId } from "./pieces/ids.mjs"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const MUTES_FORMAT = "umtool-mutes"; export const MUTES_VERSION = 1; /** A decision kind: lower-case words and dashes (`claim-unadjudicated`). */ export const KIND_RE = /^[a-z][a-z0-9-]{0,63}$/; export class MuteError extends Error { constructor(message, status = 400) { super(message); this.name = "MuteError"; this.status = status; } } /** The file under a reports root. */ export function mutesFile(reportsRoot) { return path.join(/* turbopackIgnore: true */ reportsRoot, ".umtool", "mutes.json"); } /** * Every mute: `{ mutes: { [piece]: { [kind]: { at } } }, error? }`. A missing * file is empty; an unreadable one is empty with `error`. Entries that are not a * piece id and a kind are skipped, never fatal. * * @param {string} reportsRoot */ export async function readMutes(reportsRoot) { let text; try { text = await readFile(/* turbopackIgnore: true */ mutesFile(reportsRoot), "utf8"); } catch (err) { if (err?.code === "ENOENT") return { mutes: {} }; return { mutes: {}, error: `mutes.json cannot be read: ${err?.message ?? err}` }; } let raw; try { raw = JSON.parse(text); } catch (err) { return { mutes: {}, error: `mutes.json is not JSON: ${err.message}` }; } if (!raw || typeof raw !== "object" || raw.format !== MUTES_FORMAT || typeof raw.mutes !== "object" || !raw.mutes) { return { mutes: {}, error: "mutes.json is not a umtool-mutes file" }; } const mutes = {}; for (const [piece, kinds] of Object.entries(raw.mutes)) { if (!isPieceId(piece) || !kinds || typeof kinds !== "object") continue; for (const [kind, e] of Object.entries(kinds)) { if (!KIND_RE.test(kind) || !e || typeof e !== "object") continue; (mutes[piece] ??= {})[kind] = { at: typeof e.at === "string" ? e.at : "" }; } } return { mutes }; } /** Is this kind of call muted on this piece? */ export function isMuted(read, piece, kind) { return !!(piece && read?.mutes?.[piece]?.[kind]); } /** How many (piece, kind) pairs are muted. */ export function muteCount(read) { return Object.values(read?.mutes ?? {}).reduce((n, kinds) => n + Object.keys(kinds).length, 0); } let chain = Promise.resolve(); /** * Mute or unmute one kind of call on one piece. Answers `{ piece, kind, muted, at }`. * * @param {string} reportsRoot * @param {string} piece a piece id (lib/pieces/ids.mjs) * @param {string} kind a decision kind * @param {boolean} muted * @param {{ now?: Date }} [opts] */ export function setMute(reportsRoot, piece, kind, muted, { now } = {}) { if (!isPieceId(piece)) return Promise.reject(new MuteError(`not a piece id: ${JSON.stringify(piece)}`)); if (typeof kind !== "string" || !KIND_RE.test(kind)) return Promise.reject(new MuteError(`not a decision kind: ${JSON.stringify(kind)}`)); if (typeof muted !== "boolean") return Promise.reject(new MuteError(`muted is true or false, not ${JSON.stringify(muted)}`)); const run = chain.then(() => write(reportsRoot, piece, kind, muted, now ?? new Date())); // The next write waits for this one, whether it failed or not. chain = run.catch(() => {}); return run; } async function write(reportsRoot, piece, kind, muted, now) { const file = mutesFile(reportsRoot); const read = await readMutes(reportsRoot); if (read.error) throw new MuteError(`${read.error}; fix or remove ${file} first`, 409); const mutes = structuredClone(read.mutes); const at = now.toISOString(); if (muted) (mutes[piece] ??= {})[kind] = { at }; else if (mutes[piece]) { delete mutes[piece][kind]; if (!Object.keys(mutes[piece]).length) delete mutes[piece]; } const sorted = (o) => Object.fromEntries(Object.entries(o).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))); const doc = { format: MUTES_FORMAT, version: MUTES_VERSION, mutes: sorted(Object.fromEntries(Object.entries(mutes).map(([p, kinds]) => [p, sorted(kinds)]))), }; await mkdir(/* turbopackIgnore: true */ path.dirname(file), { recursive: true }); const tmp = tmpPathFor(file); try { await writeFile(/* turbopackIgnore: true */ tmp, `${JSON.stringify(doc, null, 2)}\n`); await rename(/* turbopackIgnore: true */ tmp, file); } catch (err) { await rm(/* turbopackIgnore: true */ tmp, { force: true }).catch(() => {}); throw err; } return { piece, kind, muted, at }; }