// UNDO OF THE LAST PRESS (lane posts-library): the judge-trending set that // puts back what each judged id (a post, or a thread) held before the press // — its OWN entry's state and note, or "undecided" when it had none. // // It is sent with `archive: false`: an undo starts no capture, and it does not // cancel one a keep already queued (the capture job asks, before each // download, whether the post is still kept). The entry it restores is written // by the operator's hand, whoever wrote the one before. // // Plain ESM, no imports. export const UNDO_SAYS = "Undo puts the judgements back, written as the operator's whoever wrote them before. It does not cancel a capture already queued."; const before = (j) => (j && typeof j.state === "string" ? { state: j.state, ...(typeof j.note === "string" && j.note ? { note: j.note } : {}) } : { state: "undecided" }); /** * The set that undoes a press on `targets`, read from the rows as they were * BEFORE it (the listing's detail rows: `detail.ownJudgement`, `detail.threadJudgement`). * A target no row speaks for goes back to undecided. * * @param {Array<{id: string, thread: string, detail?: {ownJudgement?: object, threadJudgement?: object}}>} rows * @param {Array<{id?: string, thread?: string}>} targets * @returns {Array<{id?: string, thread?: string, state: string, note?: string}>} */ export function undoSetOf(rows, targets) { const byId = new Map(rows.map((r) => [r.id, r])); const byThread = new Map(); for (const r of rows) if (!byThread.has(r.thread)) byThread.set(r.thread, r); return targets.map((t) => t.thread !== undefined ? { thread: t.thread, ...before(byThread.get(t.thread)?.detail?.threadJudgement) } : { id: t.id, ...before(byId.get(t.id)?.detail?.ownJudgement) }, ); } /** The set a press sends: every target with the state (and the note, for a judgement that is not removed). */ export function pressSetOf(targets, state, note = "") { return targets.map((t) => ({ ...(t.thread !== undefined ? { thread: t.thread } : { id: t.id }), state, ...(note && state !== "undecided" ? { note } : {}), })); }