// MANIFEST MIGRATION (release 39): a manifest written before the modules, // rewritten into the modules' homes -- PURE, and the render of what it // returns is the render of what it was given, frame for frame. // // One migration so far, `counter-series`: the fact-check tally becomes the // COUNTER's named series (counter.mjs). // // render.chrome.factcheck.verdicts -> render.chrome.deck.counter.series // every verdict a series, in VERDICTS order, its id the verdict (so every // mark's `verdict` names it as it always did, and the pages it draws are // the bytes they were), carrying the relabels it had: label, colour, // stampLabel, tallyLabel. // render.chrome.factcheck.tally -> counter.show / position / showAt // (one showAt for all is the counter's; one per verdict is the series'). // render.chrome.deck.claims[].tally -> the series id when it named a label. // // The stamp's settings (`factcheck.stamp`) stay where they are: they are the // stamp module's. A manifest with nothing to count, or already migrated, is // returned as it was (`changes` empty). Idempotent. import { VERDICTS } from "yt-dlp-transcript-common/lib/report/verdicts.mjs"; import { resolveCounter } from "../counter.mjs"; import { tallyVerdictOf } from "../claims.mjs"; import { rawMarksOf } from "../factcheck.mjs"; /** The migrations, in the order they run, each `{ id, summary }`. */ export const MIGRATIONS = Object.freeze([ Object.freeze({ id: "counter-series", summary: "the fact-check verdicts and tally become the counter's named series (render.chrome.deck.counter)" }), ]); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const clone = (v) => JSON.parse(JSON.stringify(v)); /** Does this manifest count anything the counter would: a relabel, the tally's settings, a mark, a deck claim? */ function counts(m) { const chrome = m?.render?.chrome; if (!isObj(chrome) || chrome.layout !== "deck") return false; if (isObj(chrome.deck?.counter)) return false; const f = isObj(chrome.factcheck) ? chrome.factcheck : {}; if (f.verdicts !== undefined || f.tally !== undefined) return true; if ((m.timeline ?? []).some((e) => rawMarksOf(e).length)) return true; return Array.isArray(chrome.deck?.claims) && chrome.deck.claims.some((c) => isObj(c) && c.kind === "count"); } function counterSeries(m, changes) { const chrome = m.render.chrome; const f = isObj(chrome.factcheck) ? chrome.factcheck : {}; const resolved = resolveCounter(m.render); const given = isObj(f.verdicts) ? f.verdicts : {}; const tally = isObj(f.tally) ? f.tally : {}; const perSeries = isObj(tally.showAt); const series = VERDICTS.map((id) => { const own = isObj(given[id]) ? given[id] : {}; return { id, ...own, ...(perSeries && tally.showAt[id] === "first" ? { showAt: "first" } : {}), }; }); const counter = { series, ...(tally.show === false ? { show: false } : {}), ...(tally.position === "left" ? { position: "left" } : {}), ...(!perSeries && tally.showAt === "first" ? { showAt: "first" } : {}), }; const deck = isObj(chrome.deck) ? { ...chrome.deck } : {}; // A count claim names its series by id from here on. if (Array.isArray(deck.claims)) { deck.claims = deck.claims.map((c, n) => { if (!isObj(c) || c.kind !== "count" || typeof c.tally !== "string") return c; const id = tallyVerdictOf(c.tally, resolved.byId); if (!id || id === c.tally) return c; changes.push(`render.chrome.deck.claims[${n}].tally: "${c.tally}" -> "${id}" (the series by id)`); return { ...c, tally: id }; }); } deck.counter = counter; const { verdicts: _v, tally: _t, ...rest } = f; const next = { ...chrome, deck }; if (chrome.factcheck !== undefined) next.factcheck = rest; m.render.chrome = next; const relabelled = Object.keys(given).filter((k) => VERDICTS.includes(k)); changes.push( `render.chrome.factcheck.verdicts -> render.chrome.deck.counter.series (${VERDICTS.length} series` + `${relabelled.length ? `; ${relabelled.join(", ")} relabelled` : ""})`, ); if (f.tally !== undefined) changes.push("render.chrome.factcheck.tally -> render.chrome.deck.counter (show, position, showAt)"); } /** * The manifest migrated into the modules' homes: `{ manifest, changes }`, * `manifest` a new object (the one given is not touched) and `changes` a * sentence per move, empty when there was nothing to do. * * @param {Record} manifest * @returns {{ manifest: Record, changes: string[] }} */ export function migrateManifest(manifest) { if (!isObj(manifest)) throw new Error("migrateManifest: a manifest is an object"); const m = clone(manifest); const changes = []; if (counts(m)) counterSeries(m, changes); return { manifest: changes.length ? m : clone(manifest), changes }; }