"use server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { isValidSiteId, listSiteIds } from "yt-dlp-transcript-common/lib/site"; import { runManagedFunction, type StreamActionResult, } from "yt-dlp-transcript-common/jobs/streamCommand"; import { formatReportMediaProblems, prepareReportMedia, } from "yt-dlp-transcript-common/publish/reportMedia"; import { exportAfterPrepare, formatReportExportProblems, } from "yt-dlp-transcript-common/publish/reportExports"; import { REPORTS_PREPARE_QUEUE } from "./reportsQueue"; // PREPARE A REPORT SITE'S EVIDENCE MEDIA as a job: every clip its published // reports cite, cut from the media on disk, and every cited post capture, // copied into `.export-index/sites//report-media/` before its build. // The work is publish/reportMedia.ts's, the same `archilyzer reports prepare` // runs. The job FAILS when any citation lacks its media — the log names each // one — and the manifest it writes carries the same list. When nothing is // missing it ends by exporting the reports as files (reportsExportAction's // work, publish/reportExports.ts), and fails when an export does. // // The spec's `slug` is the SITE id (a spec needs one, and this job belongs to // no channel); the replay handler reads `params.siteId`. export async function reportsPrepareAction( siteId: string, ): Promise { const paths = getPaths(); const id = siteId.trim(); if (!isValidSiteId(id)) { return { ok: false, error: `"${id}" is not a valid site id` }; } if (!listSiteIds(paths).includes(id)) { return { ok: false, error: `No site "${id}"` }; } return runManagedFunction({ kind: "reports-prepare", queueKey: REPORTS_PREPARE_QUEUE, paths, spec: { kind: "reports-prepare", slug: id, params: { siteId: id } }, fn: async (onLog, signal) => { const index = await prepareReportMedia({ siteId: id, paths, onLog, signal }); if (index.problems.length > 0) { for (const line of formatReportMediaProblems(index.problems)) onLog(line); throw new Error( `${index.problems.length} problem(s): the site's evidence media is not complete`, ); } const exported = await exportAfterPrepare(index, { siteId: id, paths, onLog, signal }); for (const r of exported?.exported ?? []) { for (const note of r.manifest.notes) onLog(`note: ${r.reportId}: ${note}`); } if (!exported || exported.problems.length === 0) return; for (const line of formatReportExportProblems(exported.problems)) onLog(line); throw new Error( `${exported.problems.length} problem(s): the reports were not all exported`, ); }, }); }