import { projectRef } from "@/lib/projects"; import { readManifest } from "@/lib/projects/report.mjs"; import { linkVerdicts, readLinkCheck, runLinkCheck } from "@/lib/report/linkCheck.mjs"; import { refuseCaller } from "@/lib/report/caller"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; import { applyLinkFixes } from "@/lib/report/linksCli.mjs"; export const dynamic = "force-dynamic"; // DO THE QR LINKS OPEN (lib/report/linkCheck.mjs; UI notes #8): every clip QR // the cut draws, resolved against the site it links -- corpus.json, the // channel's manifest, slugToPage, the shard's record. One request at a time; // a site that does not answer leaves its links unverified. The project page's // Check links (the link-dead / links-unverified / links-unchecked rows), and // `umtool links `. // // GET ?project= the recorded check (out/links.json) read against the manifest as it is now // POST { project, fix? } check now, record it, and answer the same; `fix`: first // rewrite every dead link the last check knows the fix for // (a Twitch id the site serves as v; a Rumble folder id as the // embed id the archive serves, `siteVideo` -- "Use embed ids") // through the window writer, after one undo point (linksCli.mjs // applyLinkFixes, `umtool links

--fix`'s own) const headers = { "cache-control": "no-store" }; async function answer(dir: string, doc: Awaited>) { const m = await readManifest(dir); const v = linkVerdicts(m ?? {}, doc); return { doc, verdicts: { ...v, unchecked: v.unchecked.length } }; } export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); return Response.json(await answer(project.dir, await readLinkCheck(project.dir)), { headers }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/links"); if (refused) return refused; // Another site's page may not write here (lib/report/sameOrigin.mjs: Sec-Fetch-Site, Origin, a loopback Host). const cross = crossSiteRefusal(request.headers); if (cross) return Response.json({ error: cross }, { status: 403 }); const b = (await request.json().catch(() => null)) as { project?: unknown; fix?: unknown } | null; if (!b || typeof b !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const project = await projectRef(String(b.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); let m = await readManifest(project.dir); if (!m) return Response.json({ error: "the project has no video.manifest.json" }, { status: 400, headers }); try { let fixed: { clip: string; from: string; to: string; siteVideo?: string }[] = []; if (b.fix === true) { // Only a clip still as the recorded check saw it: an edit since is somebody's decision. fixed = (await applyLinkFixes(project, m, await readLinkCheck(project.dir), { guard: true })).fixed; if (fixed.length) m = await readManifest(project.dir); } const doc = await runLinkCheck(project.dir, m); return Response.json({ ok: true, fixed, ...(await answer(project.dir, doc)) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500, headers }); } }