// A project's LINK POLICY (UI notes 2026-10-30, #10): "cite via site X; these // channels direct". On the Ember cut Ember's and Side Scrollers' clips linked // anilyzer while Harmful Opinions' linked YouTube, and the citeUrls were // rewritten by hand twice. The policy lives in the manifest, beside the other // link setting it qualifies (`provenance.siteOrigin`): // // provenance.linkPolicy = { via?: "", direct?: ["", …] } // // via the archive every clip's QR opens (default: provenance.siteOrigin) // direct the channels whose clips link the platform's own page at the cite // (YouTube `t=s`, Twitch `t=hms`, Rumble `start=` -- // common/lib/clips/assist.mjs watchUrlAt over the record's webpage_url) // // Enforced in two places, both through the existing writers: // // - THE INSERTERS (manifest.mjs addClip: the strip's selection, the clip // finder, a live share; `umtool timeline insert`): the citeUrl a new clip // is written with is the policy's (`policyCiteUrl`), never a neighbour's // link that breaks it. // - THE CHECK (lib/projects/report.mjs): `clip-link-policy`, one row per clip // whose QR breaks the policy, its settle the window writer's citeUrl patch // ("Link via site.test" / "Link its YouTube page"). // // A clip with its own media (`src`) is not judged. Plain ESM: the CLI runs it // under bare node. import { readFile } from "node:fs/promises"; import path from "node:path"; import { watchUrlAt } from "yt-dlp-transcript-common/lib/clips/assist.mjs"; import { resolveVideoDir } from "yt-dlp-transcript-common/lib/videoDir-server.mjs"; import { archiveMomentUrl } from "umtool-report-to-video/site-links"; import { archiveLinkOf } from "./linkCheck.mjs"; import { originOf } from "./siteDeploys.mjs"; export const LINK_POLICY_KEY = "linkPolicy"; const SLUG_RE = /^[a-z0-9][a-z0-9._-]{0,127}$/i; /** * A policy as written, checked and normalised: `{ via, direct }` (via an * origin or null, direct sorted and unique), or null for "no policy". Throws * on a policy the check could not apply. * @param {unknown} raw * @returns {{ via: string | null, direct: string[] } | null} */ export function normalizeLinkPolicy(raw) { if (raw == null) return null; if (typeof raw !== "object" || Array.isArray(raw)) throw new Error("provenance.linkPolicy is { via?, direct? }"); const r = /** @type {Record} */ (raw); for (const k of Object.keys(r)) if (k !== "via" && k !== "direct") throw new Error(`provenance.linkPolicy has no key "${k}" (via, direct)`); let via = null; if (r.via != null && String(r.via).trim() !== "") { via = originOf(String(r.via).trim()); if (!via) throw new Error(`linkPolicy.via is a site's http(s) address, not ${JSON.stringify(r.via)}`); } const list = r.direct == null ? [] : Array.isArray(r.direct) ? r.direct : String(r.direct).split(","); const direct = [...new Set(list.map((s) => String(s).trim()).filter(Boolean))].sort(); for (const s of direct) if (!SLUG_RE.test(s)) throw new Error(`linkPolicy.direct names channel slugs, not ${JSON.stringify(s)}`); return via || direct.length ? { via, direct } : null; } /** The manifest's policy, or null (none, or one that does not read). */ export function linkPolicyOf(m) { try { return normalizeLinkPolicy(m?.provenance?.[LINK_POLICY_KEY]); } catch { return null; } } const chanOf = (m, e) => e?.channel ?? m?.provenance?.channelSlug ?? null; const citeOf = (e) => Math.floor(Number(e?.cite ?? e?.cutStart ?? e?.start ?? 0) || 0); /** * Where a clip's QR points now -- build-video's rule (lib/projects/report.mjs * citeUrlFor): its own citeUrl, else its moment on the provenance's origin. */ export function currentLinkOf(m, e) { if (typeof e?.citeUrl === "string" && e.citeUrl) return e.citeUrl; return archiveMomentUrl({ ...e, channel: chanOf(m, e) }, m?.provenance, e?.start ?? 0, { origin: m?.provenance?.siteOrigin ?? "" }); } /** * What the policy wants of one clip: `{ want: "direct" | "via", url }`, the * url null when it cannot be built (a direct clip whose record names no * webpage_url, a via policy with no origin). Null when the policy has nothing * to say about the clip. * @param {Record} m * @param {Record} e * @param {{ webpageUrl?: string | null, policy?: ReturnType }} [o] */ export function policyLinkFor(m, e, { webpageUrl = null, policy = linkPolicyOf(m) } = {}) { if (!policy || e?.type !== "clip" || e.src != null) return null; const chan = chanOf(m, e); const cite = citeOf(e); if (chan && policy.direct.includes(chan)) return { want: /** @type {const} */ ("direct"), url: watchUrlAt(webpageUrl, cite) }; const via = policy.via ?? originOf(m?.provenance?.siteOrigin); if (!via) return null; return { want: /** @type {const} */ ("via"), url: archiveMomentUrl({ ...e, channel: chan }, m?.provenance, cite, { origin: via }), via }; } /** * Whether the clip's QR keeps the policy: a direct channel's is not an * archive link; any other's is an archive link on `via`. `null` when the * policy says nothing about the clip. */ export function keepsPolicy(m, e, o = {}) { const want = policyLinkFor(m, e, o); if (!want) return null; const a = archiveLinkOf(currentLinkOf(m, e)); if (want.want === "direct") return !a; return !!a && a.origin === /** @type {any} */ (want).via; } /** * Every clip whose QR breaks the policy, with the citeUrl that keeps it (null * when it cannot be built). Pure over `webpageOf` (the record's webpage_url). * @param {Record} m * @param {{ webpageOf?: (e: Record) => string | null }} [o] */ export function policyBreaks(m, { webpageOf = () => null } = {}) { const policy = linkPolicyOf(m); if (!policy) return []; const out = []; for (const e of m?.timeline ?? []) { if (e?.type !== "clip" || e.src != null) continue; const webpageUrl = webpageOf(e); const want = policyLinkFor(m, e, { webpageUrl, policy }); if (!want || keepsPolicy(m, e, { webpageUrl, policy }) !== false) continue; const current = currentLinkOf(m, e); let host = ""; try { host = new URL(current).host; } catch { host = current; } out.push({ clip: e.id, channel: chanOf(m, e), current, want: want.want, fix: want.url, why: want.want === "direct" ? `${chanOf(m, e)} links direct by the project's link policy, but its QR opens ${host}${want.url ? "" : " — and its record names no platform page to link (no webpage_url)"}` : `the project's link policy cites via ${new URL(/** @type {any} */ (want).via).host}, but its QR opens ${host}`, }); } return out; } /** The settle button's words for a fix. */ export function policyFixLabel(b) { if (b.want === "direct") { let host = "the platform"; try { host = new URL(String(b.fix)).hostname.replace(/^(?:www|m)\./, ""); } catch { /* keep the generic word */ } const name = /youtu/.test(host) ? "YouTube" : /twitch/.test(host) ? "Twitch" : /rumble/.test(host) ? "Rumble" : host; return `Link its ${name} page`; } try { return `Link via ${new URL(String(b.fix)).host}`; } catch { return "Link via the site"; } } /** * The record's webpage_url, read once per recording: the cue file's * `webpageUrl`, else metadata.info.json's `webpage_url`. Null when neither * says (or there is no channels dir). * @param {string | null} channelsDir */ export function webpageReader(channelsDir) { const memo = new Map(); return async (slug, video) => { if (!channelsDir || !slug || !video) return null; const key = `${slug}/${video}`; if (!memo.has(key)) { memo.set(key, (async () => { const dir = await resolveVideoDir(channelsDir, slug, video).catch(() => path.join(channelsDir, slug, "data", video)); const read = (name) => readFile(path.join(/* turbopackIgnore: true */ dir, name), "utf8").then(JSON.parse, () => null); const cues = await read("transcript.cues.json"); if (typeof cues?.webpageUrl === "string" && cues.webpageUrl) return cues.webpageUrl; const meta = await read("metadata.info.json"); return typeof meta?.webpage_url === "string" && meta.webpage_url ? meta.webpage_url : null; })()); } return memo.get(key); }; } /** * The citeUrl an inserter writes a NEW clip with under the policy: the * policy's link when the clip's link would break it (a neighbour's copied * citeUrl, or the default origin when `via` is another site), `undefined` * when the clip keeps it as it is (so nothing is written), `null` when the * link the policy wants cannot be built (the clip's citeUrl is dropped so the * check names it, rather than a copied link that breaks the policy). * @param {Record} m * @param {Record} entry * @param {string | null} webpageUrl */ export function policyCiteUrl(m, entry, webpageUrl) { const policy = linkPolicyOf(m); const want = policyLinkFor(m, entry, { webpageUrl, policy }); if (!want) return undefined; if (keepsPolicy(m, entry, { webpageUrl, policy }) === true) return undefined; return want.url; }