// Which sites does this cut's QR links depend on, and is each one's deploy // recent enough to hold what the cut cites? (UI notes 2026-10-30, #9) // // On the Ember cut anilyzer's publish policy was "build" only: nothing said // the video being made cited a site that had not been deployed since 9 Oct. // The link check (linkCheck.mjs, #8) already reads each site's corpus.json and // records its `generatedAt` -- the day the deployed bundle's data was built -- // in out/links.json. This module reads that record (it never fetches): // // - the SITES: every origin a clip QR the cut draws opens (linksOf), each // named by the site whose site.json `siteUrl` it is (SITES_DIR, read only) // and dated by the recorded corpus.json; // - the cited videos NEWER than that deploy: a clip on that site whose // recording was uploaded after it, or whose transcript landed here after // it, and whose link the check did not find open (a link the check opened // is on the site, whatever its dates say). // // The check row `site-deploy-behind` (lib/projects/report.mjs) is one per site // with such clips, its settle "Publish " asking the editor to build and // deploy that site (POST /api/report/publish-site -> the editor's ops // build-deploy). Plain ESM: the CLI runs it under bare node. import { readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { archiveLinkOf, linksOf } from "./linkCheck.mjs"; /** `https://x.pages.dev/` -> `https://x.pages.dev`; null for anything not http(s). */ export function originOf(url) { try { const u = new URL(String(url)); return u.protocol === "http:" || u.protocol === "https:" ? u.origin : null; } catch { return null; } } /** * Every site with a `siteUrl`, by its origin: `{ siteId, title }`. Read once * per call (a handful of small files); a site.json that does not parse is * skipped. * @param {string} sitesDir * @returns {Map} */ export function sitesByOrigin(sitesDir) { const out = new Map(); let ids = []; try { ids = readdirSync(/* turbopackIgnore: true */ sitesDir); } catch { return out; } for (const id of ids.sort()) { let doc; try { doc = JSON.parse(readFileSync(/* turbopackIgnore: true */ path.join(sitesDir, id, "site.json"), "utf8")); } catch { continue; } const origin = originOf(doc?.siteUrl); if (!origin || out.has(origin)) continue; out.set(origin, { siteId: id, title: typeof doc?.siteTitle === "string" ? doc.siteTitle : typeof doc?.title === "string" ? doc.title : null }); } return out; } /** `20261020` / `2026-10-20` / an ISO time -> `2026-10-20`, else null. */ export function dayOf(v) { const s = String(v ?? "").trim(); if (/^\d{8}$/.test(s)) return `${s.slice(0, 4)}-${s.slice(4, 6)}-${s.slice(6, 8)}`; const m = /^(\d{4}-\d{2}-\d{2})/.exec(s); return m ? m[1] : null; } /** * The sites the cut's links depend on, with each one's deploy and the cited * videos newer than it. Pure over its inputs. * * @param {Record} m the manifest * @param {{ origins?: Record, links?: Array<{ clip: string, url: string, state: string }>, checkedAt?: string } | null} linkDoc out/links.json * @param {{ sites?: Map, recordOf?: (clipId: string) => { uploadDate?: string | null, landedAt?: string | null } | null }} [opts] * `recordOf`: what is known of the clip's recording here -- its upload date * and when its transcript landed (the cue file's mtime, ISO). * @returns {Array<{ * origin: string, host: string, siteId: string | null, title: string | null, * deployedAt: string | null, state: "ok" | "unreachable" | "no-corpus" | "unchecked", * clips: string[], links: number, * newer: Array<{ clip: string, why: string, day: string }>, * }>} */ export function projectSites(m, linkDoc, { sites = new Map(), recordOf = () => null } = {}) { /** @type {Map, links: Array<{ clip: string, url: string }> }>} */ const by = new Map(); for (const l of linksOf(m)) { const a = archiveLinkOf(l.url); if (!a) continue; if (!by.has(a.origin)) by.set(a.origin, { clips: new Set(), links: [] }); const g = /** @type {{ clips: Set, links: Array<{ clip: string, url: string }> }} */ (by.get(a.origin)); g.clips.add(l.clip); g.links.push(l); } const checked = new Map((linkDoc?.links ?? []).map((l) => [`${l.clip}\u0000${l.url}`, l.state])); const out = []; for (const [origin, g] of by) { const o = linkDoc?.origins?.[origin] ?? null; const deployedAt = typeof o?.generatedAt === "string" ? o.generatedAt : null; const deployDay = dayOf(deployedAt); const site = sites.get(origin) ?? null; const newer = []; if (deployDay) { const seen = new Set(); for (const l of g.links) { if (seen.has(l.clip)) continue; // The check opened it: the record is on the site. if (checked.get(`${l.clip}\u0000${l.url}`) === "ok") continue; const r = recordOf(l.clip) ?? {}; const up = dayOf(r.uploadDate); const landed = dayOf(r.landedAt); if (up && up > deployDay) { seen.add(l.clip); newer.push({ clip: l.clip, why: `uploaded ${up}`, day: up }); } else if (landed && landed > deployDay) { seen.add(l.clip); newer.push({ clip: l.clip, why: `transcribed here ${landed}`, day: landed }); } } } let host = origin; try { host = new URL(origin).host; } catch { /* the origin as written */ } out.push({ origin, host, siteId: site?.siteId ?? null, title: site?.title ?? (typeof o?.title === "string" ? o.title : null), deployedAt, state: o ? o.state : "unchecked", clips: [...g.clips], links: g.links.length, newer, }); } return out.sort((a, b) => b.links - a.links || a.host.localeCompare(b.host)); } /** The check row's sentence for one site with newer videos. */ export function deployBehindWhy(s) { const list = s.newer.slice(0, 6).map((n) => `${n.clip} (${n.why})`).join(", "); const more = s.newer.length > 6 ? ` and ${s.newer.length - 6} more` : ""; return ( `${s.siteId ?? s.host}'s deploy is from ${dayOf(s.deployedAt)}; ${s.newer.length} cited video${s.newer.length === 1 ? " is" : "s are"} newer: ${list}${more}` + ` — publish the site before the video ships, or link elsewhere` ); }