// A "DO NOT CLEAN" ASKED FOR BEFORE THE VIDEO IS HERE (plans/live-capture.md). // // The marker (`data//do-not-clean.json`, lib/doNotClean-server.ts) lives in // the video's dir, and nothing may make that dir but a fetch: every enumerator // reads "has a dir" as "was fetched". A live stream has none — the download // lane skips it while it is live — and the next sync may fetch its VOD before // an import of a recording runs. So `keep-videos {ids}` records an id the // channel does not hold yet as PENDING, in `channels//pending-keep.json` // ({entries: [{id, note, at}]}), and the marker is set the moment the record // is first written: after a managed download (downloadOneManaged), an // import-media landing, a record-live landing. Applied entries are removed. // // Written only through these functions (one atomic write per change). import path from "node:path"; import { stat } from "node:fs/promises"; import type { Paths } from "./paths"; import { readJsonFile, writeJsonAtomic } from "./jsonFile-server"; import { keepByPerson } from "./doNotClean-server"; import type { DoNotCleanSurface } from "./doNotClean"; export const PENDING_KEEP_FILENAME = "pending-keep.json"; // `surface`: where the person asked (the Keep matching control's button or // pnpm ops), carried onto the mark when it is applied. Absent on entries // written before it was recorded. export type PendingKeepEntry = { id: string; note?: string; at: string; surface?: DoNotCleanSurface }; function fileOf(paths: Pick, slug: string): string { return path.join(paths.channelsDir, slug, PENDING_KEEP_FILENAME); } export async function loadPendingKeep(paths: Pick, slug: string): Promise { const read = await readJsonFile(fileOf(paths, slug)); if (!read.ok) return []; const entries = (read.value as { entries?: unknown } | null)?.entries; if (!Array.isArray(entries)) return []; return entries.filter( (e): e is PendingKeepEntry => !!e && typeof e === "object" && typeof (e as PendingKeepEntry).id === "string" && typeof (e as PendingKeepEntry).at === "string", ); } async function save(paths: Pick, slug: string, entries: PendingKeepEntry[]): Promise { await writeJsonAtomic(fileOf(paths, slug), { entries }); } // Adds (or re-notes) pending entries. Returns the list after. export async function addPendingKeep( paths: Pick, slug: string, ids: readonly string[], note: string | undefined, now: Date = new Date(), surface?: DoNotCleanSurface, ): Promise { const entries = await loadPendingKeep(paths, slug); for (const id of ids) { const i = entries.findIndex((e) => e.id === id); const entry: PendingKeepEntry = { id, ...(note ? { note } : {}), at: now.toISOString(), ...(surface ? { surface } : {}) }; if (i >= 0) entries[i] = entry; else entries.push(entry); } await save(paths, slug, entries); return entries; } // THE HOOK: when `id` is pending and its dir now exists, set the marker (with // the pending note) and drop the entry. A no-op — one failed read — for the // common case of a channel with nothing pending. Never throws: a marker that // could not be set stays pending for the next landing. export async function applyPendingKeep( paths: Pick, slug: string, id: string, onLog?: (s: string) => void, // The landing job (an import, a live recording), recorded on the mark. jobId?: string, ): Promise { try { const entries = await loadPendingKeep(paths, slug); const entry = entries.find((e) => e.id === id); if (!entry) return false; const videoDir = path.join(paths.channelsDir, slug, "data", id); if (!(await stat(videoDir).then((s) => s.isDirectory(), () => false))) return false; // A person's ask: it takes over a mark another writer set meanwhile (the // availability check, an import, a live recording); a person's own mark // stays, gaining the note if one was asked with. await keepByPerson( videoDir, { via: "pending-keep", ...(entry.surface === "button" || entry.surface === "ops" ? { surface: entry.surface } : {}), ...(jobId ? { jobId } : {}), ...(entry.note ? { note: entry.note } : {}), }, { defaultNote: "keep-videos: asked before it was fetched" }, ); await save( paths, slug, entries.filter((e) => e.id !== id), ); onLog?.(`Marked "Do not clean" (asked for before it was here${entry.note ? `: ${entry.note}` : ""}).\n`); return true; } catch (err) { onLog?.(`The pending "Do not clean" for ${id} was not applied: ${(err as Error).message}\n`); return false; } }