import { NextResponse } from "next/server"; import { fetchChatBacklogAction, previewChatBacklogAction, } from "../../../channels/[slug]/chatBacklogActions"; import { jobResponse, OpsInputError, ops, opsFail, optBool, optString, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, dryRun?, limit?, queueKey? } -> { ok: true, jobId } // | { ok: true, dryRun: true, chats, … } // // A channel's stream chats, after its downloads (2026-10-26; // controller/chatBacklog.ts): every held video whose metadata says it was a // stream (`live_status` was_live / post_live) and whose chat is not here, one // at a time through fetch-chat's pass — the channel's cookies, the platform's // live pace and the batch downloads' gap between two — on the platform's // download queue. A re-run skips every chat here and every stream known to // have none (`chat-backlog.json`). `limit` caps the chats this run asks for. // The download lane queues the same job by itself for a channel whose `chat` // is "later" once it has nothing left to download. // // `dryRun` answers from the disk alone, no job: `chats` (to get, newest // first; `ids` the first 50), `normalizeOnly` (raw replays already here), // `have`, `settled` (known to have none), `notStreams`. // // Answered without a job, `info: true`: a channel on a platform with no chat // fetcher, a backlog already queued or running, a held platform or one in a // rate-limit cooldown. export async function POST(request: Request) { return ops(request, ["slug", "dryRun", "limit", "queueKey"], async (body) => { const slug = reqSlug(body, "slug"); const limit = body.limit; if (limit !== undefined && !(typeof limit === "number" && Number.isInteger(limit) && limit > 0)) { throw new OpsInputError('"limit" must be a whole number above 0'); } if (optBool(body, "dryRun")) { if (limit !== undefined || body.queueKey !== undefined) { throw new OpsInputError('a dry run starts no job: "limit" and "queueKey" go with a real run'); } const preview = await previewChatBacklogAction(slug); if (!preview.ok) return opsFail(preview.error); return NextResponse.json({ ...preview, dryRun: true }); } return jobResponse( await fetchChatBacklogAction(slug, { ...(typeof limit === "number" ? { limit } : {}), ...(optString(body, "queueKey") ? { queueKey: optString(body, "queueKey") } : {}), }), ); }); }