"use server"; // A CHANNEL'S STREAM CHATS AFTER ITS DOWNLOADS (2026-10-26): the Download // stage's "Fetch chat backlog" and `pnpm ops fetch-chat-backlog`, over the one // job controller/chatBacklog.ts makes (the download lane makes the same one // for a `chat: "later"` channel with nothing left to download). // // NOTHING BUT FUNCTIONS IS DECLARED HERE (a "use server" file). import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { resolveCookiePolicy } from "yt-dlp-transcript-common/lib/cookiePolicy"; import { isValidChannelSlug, readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; import { planChatBacklog, startChatBacklogJob } from "yt-dlp-transcript-common/controller/chatBacklog"; import type { StreamActionResult } from "yt-dlp-transcript-common/jobs/streamCommand"; export async function fetchChatBacklogAction( slug: string, opts: { limit?: number; queueKey?: string } = {}, ): Promise { if (!isValidChannelSlug(slug)) return { ok: false, error: `"${slug}" is not a channel slug` }; if (opts.limit !== undefined && !(Number.isInteger(opts.limit) && opts.limit > 0)) { return { ok: false, error: '"limit" must be a whole number above 0' }; } const paths = getPaths(); const config = await readChannelConfig(paths, slug); if (!config) return { ok: false, error: `Channel "${slug}" not found` }; return startChatBacklogJob({ paths, slug, config, cookiePolicy: resolveCookiePolicy(getSettings(), config), ...(opts.limit ? { limit: opts.limit } : {}), ...(opts.queueKey ? { queueKey: opts.queueKey } : {}), }); } export type ChatBacklogPreview = | { ok: true; chats: number; normalizeOnly: number; have: number; settled: number; notStreams: number; // The first 50, newest first. ids: string[]; } | { ok: false; error: string }; // What a run would do, from the disk alone: no request, no job. export async function previewChatBacklogAction(slug: string): Promise { if (!isValidChannelSlug(slug)) return { ok: false, error: `"${slug}" is not a channel slug` }; const paths = getPaths(); const config = await readChannelConfig(paths, slug); if (!config) return { ok: false, error: `Channel "${slug}" not found` }; const plan = await planChatBacklog(paths, slug, config); return { ok: true, chats: plan.candidates.length, normalizeOnly: plan.candidates.filter((c) => c.action === "normalize").length, have: plan.have, settled: plan.settled, notStreams: plan.notStreams, ids: plan.candidates.slice(0, 50).map((c) => c.id), }; }