import { DeleteRefusedError, deleteArticle, deleteIteration, planArticleDelete, planIterationDelete, type DeleteOptions, type DeletePlan, } from "yt-dlp-transcript-common/lib/report/delete"; import { ArticleRegistryError } from "yt-dlp-transcript-common/lib/report/registry"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { chainKind, errorResponse, projectsRootOf, readPolicy } from "@/lib/articles/generation"; import { sitesPaths } from "@/lib/articles/sites"; import { clearLinksCache, videoProjects } from "@/lib/articles/links.mjs"; import { configuredProjectsRoot } from "@/lib/report/chain.mjs"; import { answerDelete } from "@/lib/delete-plan"; import { invalidateProjects, pruneIndex } from "@/lib/projects"; import { runningJob } from "@/lib/jobs"; import { ARTICLES_KEY, MEDIA_ROOT, MEDIA_TIERED, REPORTS_ROOT } from "@/lib/paths"; export const dynamic = "force-dynamic"; // DELETE AN ARTICLE, OR ONE ITERATION OF IT (release 27 follow-up). // // POST { article, n?, dryRun?, force? } // // Common's lib/report/delete.ts — the article registry's delete, under its // lock — as `archilyzer reports delete` and the editor's ops run it: the // article's directory, its iterations' video projects under the generation // projects root (the policy's, as "Make the video" reads it), its staged // exports; or one iteration and its project. umtool adds what only it knows: // a chain job of this article running here refuses, and a report-video // project whose manifest names the article is named (kept: delete it on its // own page). `force` (an article only) unpublishes it first. The answer is // lib/delete-plan.ts's. export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as { article?: unknown; n?: unknown; dryRun?: unknown; force?: unknown }; const article = String(body.article ?? ""); if (!isReportId(article)) return Response.json({ error: `"${article}" is not an article id` }, { status: 400 }); const n = body.n === undefined || body.n === null ? null : Number(body.n); if (n !== null && (!Number.isInteger(n) || n < 1)) return Response.json({ error: "n is an iteration number" }, { status: 400 }); const force = body.force === true && n === null; let projectsRoot: string; try { projectsRoot = projectsRootOf((await readPolicy()).generation); } catch { projectsRoot = configuredProjectsRoot(); // the editor unreachable: settings.json, as the chain reads it } const opts: DeleteOptions = { paths: sitesPaths(), projectsRoot, media: MEDIA_TIERED ? { reportsRoot: REPORTS_ROOT, mediaRoot: MEDIA_ROOT } : null, force, }; try { const plan: DeletePlan = n === null ? await planArticleDelete(article, opts) : await planIterationDelete(article, n, opts); const job = runningJob(); if ( job && (n === null ? job.kind.startsWith(`chain ${article} `) || job.kind.startsWith(`draft-all ${article} `) : job.kind === chainKind(article, n) || job.kind === chainKind(article, n, "draft-all")) ) { plan.refusals.push({ reason: `${job.kind} is running here: cancel the chain, or wait for it`, forceable: false }); } if (n === null) { for (const p of await videoProjects(REPORTS_ROOT).catch(() => [])) { if (p.article !== `${ARTICLES_KEY}/${article}`) continue; if (plan.remove.some((e) => e.path === p.dir)) continue; plan.kept.push({ what: `the project ${p.id}`, path: p.dir, why: "its manifest names this article: kept (delete it on its own page)" }); } } const dryRun = body.dryRun === true; return await answerDelete(plan, { dryRun, force }, async () => { const r = n === null ? await deleteArticle(article, opts) : await deleteIteration(article, n, opts); invalidateProjects(); clearLinksCache(); await pruneIndex().catch(() => 0); return { ...r, plan }; }); } catch (err) { if (err instanceof DeleteRefusedError) { return Response.json({ error: `refused: ${err.problems.join("; ")}`, plan: err.plan }, { status: 409 }); } if (err instanceof ArticleRegistryError) { return Response.json({ error: err.message }, { status: /^no article|has no iteration/.test(err.message) ? 404 : 400 }); } return errorResponse(err); } }