import { withEditNotes } from "@/lib/report/guard";
import { StaleToken, StructureRefused, manifestToken } from "@/lib/report/manifest.mjs";
import { embedIdFixes, embedIdRows } from "@/lib/report/embedIds.mjs";
import { setSiteVideos } from "@/lib/report/inspectorEdits.mjs";
import { readLinkCheck } from "@/lib/report/linkCheck.mjs";
import { readManifest } from "@/lib/projects/report.mjs";
import { resolveReport } from "@/lib/report/serve.mjs";
import { refuseCaller } from "@/lib/report/caller";
import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs";
export const dynamic = "force-dynamic";
// THE PUBLISHED EMBED ID (umtool redesign L6, the Links panel's "Use the published embed id"; 4 and 8 Nov: a
// Rumble clip's QR named the URL-slug folder the corpus keeps it under, and the archive serves the EMBED id):
// every clip of the cut citing such a folder, its embed id read from the folder's metadata.info.json through L1's
// one mapping (shareLine.mjs siteVideoFor), with what the last link check (out/links.json) said of its QR. The
// fix writes `siteVideo` (and a citeUrl that named the folder) -- one write, one undo point. No network: the
// check is `umtool links
`'s; `umtool links
--embed-ids` is this route's twin.
//
// GET ?project= { rows: [{ clip, channel, video, siteVideo, url, fixedUrl, citeUrl, state }], checkedAt, token }
// POST { project, token, clips?: [id…] } those rows fixed (no clips: every row)
const noStore = { "cache-control": "no-store" };
export async function GET(request: Request) {
const url = new URL(request.url);
const r = await resolveReport(url.searchParams.get("project") ?? "");
if ("error" in r) return Response.json({ error: r.error }, { status: r.status });
const doc = await readLinkCheck(r.project.dir);
const rows = await embedIdRows(r.project.dir, r.manifest as Record, { doc });
return Response.json({ rows, checkedAt: doc?.checkedAt ?? null, token: await manifestToken(r.project.dir) }, { headers: noStore });
}
export async function POST(request: Request) {
const refused = refuseCaller(request, "/api/report/embed-ids");
if (refused) return refused;
const cross = crossSiteRefusal(request.headers);
if (cross) return Response.json({ error: cross }, { status: 403 });
let body: Record;
try {
body = await request.json();
} catch {
return Response.json({ error: "expected JSON" }, { status: 400 });
}
const r = await resolveReport(String(body.project ?? ""));
if ("error" in r) return Response.json({ error: r.error }, { status: r.status });
if (typeof body.token !== "string" || !body.token) return Response.json({ error: "token is required: GET the route first (a write over a newer draft is refused, never silent)" }, { status: 400 });
const clips = Array.isArray(body.clips) ? body.clips.map(String) : null;
try {
const { result, editNotes } = await withEditNotes(r.project, async () => {
// The rows as the manifest is NOW (read inside the request, the writer checks the token).
const m = (await readManifest(r.project.dir)) as Record;
const rows = await embedIdRows(r.project.dir, m, { doc: await readLinkCheck(r.project.dir) });
return setSiteVideos(r.project.dir, embedIdFixes(rows, clips), { token: body.token as string });
});
return Response.json({ ok: true, fixed: result.fixed, token: result.token, editNotes }, { headers: noStore });
} catch (e) {
if (e instanceof StaleToken) return Response.json({ error: e.message, stale: true }, { status: 409 });
if (e instanceof StructureRefused) return Response.json({ error: e.message, errors: e.errors }, { status: 400 });
return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 });
}
}