import "server-only"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; import { readJsonFile } from "yt-dlp-transcript-common/lib/jsonFile-server"; import { articleAudience, articleFile, listArticleDirs, readArticlesIndex, } from "yt-dlp-transcript-common/lib/report/registry"; import type { ReportAudience } from "yt-dlp-transcript-common/lib/report/schema"; import { articleReportStore } from "yt-dlp-transcript-common/lib/report/store"; import { readStoreMediaIndex, type ReportMediaIndex } from "yt-dlp-transcript-common/publish/reportMedia"; import { publishableStoreExports, readStoreExportManifest, } from "yt-dlp-transcript-common/publish/reportExportFiles"; import { getRegistry } from "yt-dlp-transcript-common/jobs/registry"; import { listAllJobs, type JobListEntry } from "yt-dlp-transcript-common/jobs/listJobs"; import { readJobMeta } from "yt-dlp-transcript-common/jobs/jobMeta"; import { reportRows, type ReportFileRead, type ReportRow } from "./reportList"; import type { ReportExportsRow } from "./reportListServer"; // THE ARTICLES TAB, read: every article directory under // `transcripts/reports/`, published (reports/index.json, in order) then // drafts, each read and validated as a site's report is (reportList.ts), with // its audience; the articles' prepared media; their exports; the last jobs. // `video`: the article's own video (report.json `video.src`), or null. export type ArticleRow = ReportRow & { audience: ReportAudience; video: string | null }; // An index.json that cannot be read is the tab's error to show, not an empty // list (lib/report/registry.ts readArticlesIndex). export async function readArticleRows(paths: Paths): Promise<{ rows: ArticleRow[]; indexError: string | null }> { let published: string[] = []; let indexError: string | null = null; try { published = (await readArticlesIndex(paths)).published; } catch (err) { indexError = (err as Error).message; } const dirIds = await listArticleDirs(paths); const ids = [...new Set([...published, ...dirIds])]; const reads = new Map( await Promise.all(ids.map(async (id) => [id, await readJsonFile(articleFile(paths, id))] as const)), ); const rows = reportRows(published, dirIds, reads).map((row) => { const read = reads.get(row.id); const value = read?.ok ? (read.value as { audience?: unknown; video?: { src?: unknown } }) : null; const video = typeof value?.video?.src === "string" ? value.video.src : null; return { ...row, audience: articleAudience(value), video }; }); return { rows, indexError }; } export async function readArticlesMediaIndex(paths: Paths): Promise { return readStoreMediaIndex(articleReportStore(paths)); } // Each published article's exports: the last export's manifest and which // files the homepage's compose would publish now. export async function readArticleExports(paths: Paths, published: readonly string[]): Promise { const store = articleReportStore(paths); return Promise.all( published.map(async (reportId) => ({ reportId, manifest: await readStoreExportManifest(store, reportId), publishable: await publishableStoreExports(store, reportId), })), ); } const JOB_LOOKBACK = 100; // The newest articles job of `kind` (its spec says `params.articles`), live or // from an earlier server lifetime, or null. export async function latestArticlesJob( paths: Paths, kind: "reports-prepare" | "reports-export", ): Promise { const page = await listAllJobs(paths, { limit: JOB_LOOKBACK }); const registry = getRegistry(); for (const entry of page.entries) { if (entry.kind !== kind) continue; const spec = registry.get(entry.id)?.spec ?? (await readJobMeta(paths, entry.id))?.spec; if ((spec?.params as { articles?: unknown } | undefined)?.articles === true) return entry; } return null; }