"use server"; import { revalidatePath } from "next/cache"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { ArticleRegistryError, configureArticle, isReportAudience, readArticlesIndex, setArticlesOrder, type ArticleConfigChange, } from "yt-dlp-transcript-common/lib/report/registry"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { REPORT_EXPORT_FORMATS, type ReportExportFormat, } from "yt-dlp-transcript-common/lib/report/views"; import { runManagedFunction, type StreamActionResult, } from "yt-dlp-transcript-common/jobs/streamCommand"; import { formatReportMediaProblems, prepareReportMedia } from "yt-dlp-transcript-common/publish/reportMedia"; import { exportAfterPrepare, exportSiteReports, formatReportExportProblems, } from "yt-dlp-transcript-common/publish/reportExports"; import { articlesWorkHost } from "yt-dlp-transcript-common/publish/reportHost"; import { withReportMoved } from "./reportList"; import { REPORTS_PREPARE_QUEUE } from "./reportsQueue"; // THE ARTICLES TAB'S WRITES AND JOBS (release 26 A6): the article store, // `transcripts/reports/` — published by the project homepage, not a site. // // Every write goes through the article registry (lib/report/registry.ts: // locked, validated, atomic), the one writer of `reports/index.json` and an // article's `audience`, as `pnpm ops articles-config` does. The jobs are a // site's prepare and export over the articles' host (publish/reportHost.ts // articlesWorkHost: every published article, plus a named draft), on the // same queue: an export reads the media cache a prepare writes and prunes. // A job's spec carries `params.articles: true`; its slug is "articles" (a // spec needs one, and the job belongs to no channel and no site). export type ArticlesActionResult = { ok: true } | { ok: false; error: string }; async function configure(id: string, change: ArticleConfigChange): Promise { if (!isReportId(id)) return { ok: false, error: `"${id}" is not a valid article id` }; try { await configureArticle(id, change, getPaths()); } catch (err) { return { ok: false, error: err instanceof ArticleRegistryError || err instanceof Error ? err.message : String(err) }; } revalidatePath("/sites/articles"); return { ok: true }; } // Publish a draft (last in the list). Refused by the registry unless its // report.json is sound — read now, not when the tab rendered. export async function publishArticleAction(id: string): Promise { return configure(id, { published: true }); } // Unpublish: off the list, back to a draft. Its directory is not touched. export async function unpublishArticleAction(id: string): Promise { return configure(id, { published: false }); } // Who may read it: the public homepage build carries only "public" articles. export async function setArticleAudienceAction(id: string, audience: string): Promise { if (!isReportAudience(audience)) return { ok: false, error: `"${audience}" is not an audience` }; return configure(id, { audience }); } // One place up (-1) or down (1) in the published order, applied to the order // as it is on disk inside the registry's lock. export async function moveArticleAction(id: string, dir: -1 | 1): Promise { if (dir !== -1 && dir !== 1) return { ok: false, error: "An article moves one place up or down." }; if (!isReportId(id)) return { ok: false, error: `"${id}" is not a valid article id` }; const paths = getPaths(); try { const index = await readArticlesIndex(paths); if (!index.published.includes(id)) return { ok: false, error: `"${id}" is not published` }; const position = withReportMoved(index.published, id, dir).indexOf(id); await configureArticle(id, { position }, paths); } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } revalidatePath("/sites/articles"); return { ok: true }; } // The whole published order at once (a caller that reorders by dragging). export async function setArticlesOrderAction(ids: string[]): Promise { try { await setArticlesOrder(ids, getPaths()); } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } revalidatePath("/sites/articles"); return { ok: true }; } // PREPARE THE ARTICLES' EVIDENCE MEDIA as a job: every clip the published // articles (plus `article`, a draft being readied) cite, cut from the media on // disk, and every cited post capture, into `.export-index/articles/report-media/`; // then, when nothing is missing, their exports. As the site's prepare: the job // FAILS when a citation lacks its media, the log naming each. export async function articlesPrepareAction(article?: string): Promise { const paths = getPaths(); const include = article?.trim() ? [article.trim()] : []; let host; try { host = await articlesWorkHost(paths, include); } catch (err) { return { ok: false, error: (err as Error).message }; } return runManagedFunction({ kind: "reports-prepare", queueKey: REPORTS_PREPARE_QUEUE, paths, spec: { kind: "reports-prepare", slug: "articles", params: { articles: true, ...(include[0] ? { article: include[0] } : {}) } }, fn: async (onLog, signal) => { const index = await prepareReportMedia({ host, 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 articles' evidence media is not complete`); } const exported = await exportAfterPrepare(index, { host, 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 articles were not all exported`); }, }); } // EXPORT THE ARTICLES as a job: each published article (or the one named, // published or a draft) as files, into `.export-index/articles/report-exports/`, // where the homepage's compose publishes them from. export async function articlesExportAction( opts: { reportId?: string; formats?: string[] } = {}, ): Promise { const paths = getPaths(); if (opts.reportId !== undefined && !isReportId(opts.reportId)) { return { ok: false, error: `"${opts.reportId}" is not an article id` }; } let formats: ReportExportFormat[] | undefined; if (opts.formats !== undefined) { const bad = opts.formats.filter((f) => !(REPORT_EXPORT_FORMATS as readonly string[]).includes(f)); if (bad.length > 0 || opts.formats.length === 0) { return { ok: false, error: `formats are some of ${REPORT_EXPORT_FORMATS.join(", ")}${bad.length ? ` (not ${bad.join(", ")})` : ""}`, }; } formats = REPORT_EXPORT_FORMATS.filter((f) => opts.formats!.includes(f)); } let host; try { host = await articlesWorkHost(paths, opts.reportId ? [opts.reportId] : []); } catch (err) { return { ok: false, error: (err as Error).message }; } return runManagedFunction({ kind: "reports-export", queueKey: REPORTS_PREPARE_QUEUE, paths, spec: { kind: "reports-export", slug: "articles", params: { articles: true, ...(opts.reportId ? { reportId: opts.reportId } : {}), ...(formats ? { formats } : {}), }, }, fn: async (onLog, signal) => { const result = await exportSiteReports({ host, paths, onLog, signal, ...(opts.reportId ? { reportId: opts.reportId } : {}), ...(formats ? { formats } : {}), }); for (const r of result.exported) { for (const note of r.manifest.notes) onLog(`note: ${r.reportId}: ${note}`); } if (result.problems.length === 0) return; for (const line of formatReportExportProblems(result.problems)) onLog(line); throw new Error(`${result.problems.length} problem(s): the articles were not all exported`); }, }); }