// The editor's doors the intake specs knock on (UI notes 2026-10-30, items 13 // and 16), stubbed beside editor-stub.mjs: // // GET /api/ops/channel/ a channel's snapshot counts (testchan: a backlog, // 1,454/2,798 transcribed; backlog-done: nothing left; // any other: 404 "Channel … not found") // GET /api/ops/settings?key=channelPriority { value: { focus, channels } } // POST /api/ops/channel-priority {focus} the focus replaced, recorded // POST /api/ops/capture-posts {slug, ids} job cp-: running at once, done after ~1 s, when it // has written posts-media//shot.png + capture.json, and // each attachment the post names (media[].name) -- shared by // Add post and a research case's forum evidence (lane C) // GET /api/ops/job/cp- that job // POST /__stub/channel-counts {slug, totals?, undownloaded?, untranscribed?} (no token) a spec's // counts for a channel; {} resets import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { postShotPng } from "./intake-fixture.mjs"; const DEFAULT_COUNTS = () => ({ testchan: { totals: { videos: 2751, transcribed: 1454, downloaded: 2700 }, undownloaded: 47, untranscribed: 1297 }, "backlog-done": { totals: { videos: 120, transcribed: 120, downloaded: 120 }, undownloaded: 0, untranscribed: 0 }, }); let counts = DEFAULT_COUNTS(); const priority = { focus: { kind: "none" }, channels: {} }; const jobs = new Map(); /** The file names of a post's named attachments, read from the channel's posts/*.jsonl (none when unreadable). */ function attachmentNames(channels, slug, id) { const dir = path.join(channels, slug, "posts"); let files = []; try { files = readdirSync(dir).filter((f) => f.endsWith(".jsonl")); } catch { return []; } for (const f of files) { for (const line of readFileSync(path.join(dir, f), "utf8").split("\n")) { if (!line.includes(`"id":"${id}"`)) continue; try { const p = JSON.parse(line); if (p.id === id) return (p.media ?? []).map((m) => m?.name).filter((n) => typeof n === "string" && /^[A-Za-z0-9._-]+$/.test(n)); } catch { /* not a post */ } } } return []; } let nextJob = 1; /** The control door (before the token check). Answers true when it handled the request. */ export function intakeControl(req, res, url, { json, readBody }) { if (req.method === "POST" && url.pathname === "/__stub/channel-counts") { readBody(req, (body) => { if (!body?.slug) { counts = DEFAULT_COUNTS(); priority.focus = { kind: "none" }; } else counts[body.slug] = { totals: body.totals ?? counts[body.slug]?.totals ?? { videos: 0, transcribed: 0, downloaded: 0 }, undownloaded: body.undownloaded ?? 0, untranscribed: body.untranscribed ?? 0 }; json(res, 200, { ok: true, counts }); }); return true; } return false; } /** The token-gated doors. Answers true when it handled the request. */ export function intakeRoute(req, res, url, { json, readBody, push, channels }) { const ch = /^\/api\/ops\/channel\/([^/]+)$/.exec(url.pathname); if (req.method === "GET" && ch) { req.resume(); const slug = decodeURIComponent(ch[1]); const c = counts[slug]; if (!c) { json(res, 404, { ok: false, error: `Channel "${slug}" not found` }); return true; } json(res, 200, { ok: true, slug, config: { name: slug }, priority: { tier: "normal", overrides: {}, rank: null }, counts: null, media: { status: "ok" }, report: { generatedAt: "2026-11-01T00:00:00.000Z", totals: c.totals, buckets: { downloadedNoTranscript: c.untranscribed, noTranscript: c.untranscribed }, undownloaded: c.undownloaded }, }); return true; } if (req.method === "GET" && url.pathname === "/api/ops/settings" && url.searchParams.get("key") === "channelPriority") { req.resume(); json(res, 200, { ok: true, key: "channelPriority", value: priority }); return true; } if (req.method === "POST" && url.pathname === "/api/ops/channel-priority") { readBody(req, (body) => { push({ route: "channel-priority", ...(body ?? {}) }); if (!body?.focus || Object.keys(body).length !== 1) return json(res, 400, { ok: false, error: "the stub takes a focus body alone" }); priority.focus = body.focus; json(res, 200, { ok: true }); }); return true; } if (req.method === "POST" && url.pathname === "/api/ops/capture-posts") { readBody(req, (body) => { push({ route: "capture-posts", ...(body ?? {}) }); const slug = String(body?.slug ?? ""); const ids = Array.isArray(body?.ids) ? body.ids.map(String) : []; if (!/^[A-Za-z0-9._-]+$/.test(slug) || !ids.length) return json(res, 400, { ok: false, error: '"slug" and "ids"' }); const id = `cp-${nextJob++}`; const job = { id, kind: "capture-posts", status: "running", tail: [`capturing ${ids.length} post(s) of ${slug}`] }; jobs.set(id, job); setTimeout(() => { for (const pid of ids) { const d = path.join(channels, slug, "posts-media", pid); mkdirSync(d, { recursive: true }); writeFileSync(path.join(d, "shot.png"), postShotPng()); // The post's named attachments (release 38 lane C's forum post, case-stages.spec), as the real // capture saves its media beside the shot. for (const name of attachmentNames(channels, slug, pid)) writeFileSync(path.join(d, name), postShotPng()); writeFileSync(path.join(d, "capture.json"), JSON.stringify({ version: 1, id: pid, url: `https://x.com/i/status/${pid}`, capturedAt: new Date().toISOString(), state: "captured", mediaState: "skipped", media: [] }) + "\n"); } job.status = "done"; job.tail.push(`Done: ${ids.length} captured, 0 deleted, 0 unavailable, 0 failed.`); }, 1000); json(res, 200, { ok: true, jobId: id }); }); return true; } const jm = /^\/api\/ops\/job\/(cp-\d+)$/.exec(url.pathname); if (req.method === "GET" && jm) { req.resume(); const job = jobs.get(jm[1]); if (!job) json(res, 404, { ok: false, error: "no such job" }); else json(res, 200, { ok: true, job: { id: job.id, kind: job.kind, status: job.status }, tail: job.tail }); return true; } return false; }