import { errorResponse, EditorError } from "@/lib/articles/generation"; import { captureImportStatus, ImportCaptureError, startCaptureImport, useImportedRecord, webpageChannels, } from "@/lib/articles/importCapture"; import { isReportHost, readReportFile } from "@/lib/articles/sites"; import { clearPiecesCache } from "@/lib/pieces"; import { clearRailCounts } from "@/lib/rail"; export const dynamic = "force-dynamic"; // IMPORT THIS CAPTURE (release 37 S4; lib/articles/importCapture.ts): a page // citation's Wayback copy imported as a page record, then cited as one. // // GET the Wayback pages channels a capture can go into // GET ?job=&slug= the import job's state, and its records once done // POST {action: "start", site, report, cite, slug | newChannel: {name}} // create-channel (when asked) + import-page → {slug, jobId} // POST {action: "use", site, report, cite, slug, id} // the citation rewritten (a store article, a hand // edit) or, for a site report, the replacement for its draft const NO_STORE = { "cache-control": "no-store" }; function fail(err: unknown): Response { if (err instanceof ImportCaptureError) { return Response.json({ error: err.message, problems: err.problems }, { status: err.status, headers: NO_STORE }); } if (err instanceof EditorError) return errorResponse(err); return Response.json({ error: (err as Error).message }, { status: 500, headers: NO_STORE }); } export async function GET(request: Request) { const q = new URL(request.url).searchParams; try { const job = q.get("job"); if (job) return Response.json(await captureImportStatus(job, q.get("slug") ?? ""), { headers: NO_STORE }); return Response.json({ channels: await webpageChannels() }, { headers: NO_STORE }); } catch (err) { return fail(err); } } export async function POST(request: Request) { let body: Record; try { body = (await request.json()) as Record; } catch { return Response.json({ error: "the body is JSON" }, { status: 400, headers: NO_STORE }); } const s = (k: string) => (typeof body[k] === "string" ? (body[k] as string).trim() : ""); const site = s("site"); const reportId = s("report"); const cite = s("cite"); try { if (!isReportHost(site)) throw new ImportCaptureError(`no site ${site}`, 404); const read = await readReportFile(site, reportId); if (!read.report) throw new ImportCaptureError(read.problems[0]?.message ?? "no report", 404); if (body.action === "start") { const nc = body.newChannel as { name?: unknown } | undefined; const into = nc && typeof nc === "object" ? { create: { name: String(nc.name ?? "") } } : { slug: s("slug") }; if ("slug" in into && !into.slug) throw new ImportCaptureError("pick a Wayback pages channel, or name a new one"); return Response.json(await startCaptureImport(read.report, cite, into), { headers: NO_STORE }); } if (body.action === "use") { const out = await useImportedRecord(site, read.report, cite, s("slug"), s("id")); if (out.written) { clearPiecesCache(); clearRailCounts(); } return Response.json(out, { headers: NO_STORE }); } throw new ImportCaptureError('action is "start" or "use"'); } catch (err) { return fail(err); } }