import type { XTrendingListing, XTrendingPostRow, XTrendingTopicRow } from "yt-dlp-transcript-common/lib/xTrending/store"; import { askEditor, EditorError, editorUrl } from "@/lib/articles/generation"; import { listLibraries, listLibrary } from "@/lib/library/client.mjs"; import { listingParamsOf, PAGE_SIZE, parseLibraryView } from "@/lib/library/filters.mjs"; import { seenOf } from "@/lib/library/seen.mjs"; import { REPORTS_ROOT } from "@/lib/paths.mjs"; // WHAT THE POSTS LIBRARY PAGE SHOWS (lane posts-library), read THROUGH THE // EDITOR (GET /api/ops/trending): the libraries, one library's topics with // their counts and watches, and one page of posts under the view's filters. // umtool opens no file of a library here. The one thing it adds is its own: // each topic's Mark seen time, handed to the listing, which answers how many // posts of each topic are new in the same read. // // TWO READS OF THE EDITOR PER VIEW, whatever the number of topics: the // libraries, and one listing (the topics, their counts and new posts, the // page of posts). // // SERVER-ONLY. export type LibraryView = ReturnType; export type LibrarySummary = { slug: string; name: string; topics: number; posts: number; unjudged: number }; // `published` (lane x-trending-publish): the channel the topic's kept posts were published into, when they were. export type LibraryTopic = XTrendingTopicRow & { seenAt: string | null; fresh: number; published?: { channel: string; firstAt: string; lastAt: string; posts: number } }; export type LibraryPost = XTrendingPostRow; export type LibraryData = | { ok: false; sentence: string; reason: string } | { ok: true; libraries: LibrarySummary[]; lib: string; name: string; view: LibraryView; topics: LibraryTopic[]; posts: LibraryPost[]; total: number; authors: Array<{ author: string; posts: number }>; pageSize: number; }; // ALWAYS ASKED AGAIN (`fresh`): a judge view shows what a press just wrote, and askEditor's two-second // memo may hold an answer another render asked for while that press was still on its way to the editor. const ask = (route: string, init?: { method?: "GET" | "POST"; body?: unknown }) => askEditor>(route, { ...init, fresh: true }); /** Why the library cannot be shown, as one sentence. */ export function unavailableSentence(err: unknown): { sentence: string; reason: string } { if (err instanceof EditorError) { if (err.reason === "no-token") return { reason: err.reason, sentence: "umtool has no WORKER_TOKEN, so the editor cannot be asked. The library is read and judged through the editor: set WORKER_TOKEN to the value the editor runs with." }; if (err.reason === "unreachable") return { reason: err.reason, sentence: `The editor is not answering at ${editorUrl()}. The library is read and judged through the editor: start it, then reload.` }; if (err.reason === "editor-token") return { reason: err.reason, sentence: "The editor's own WORKER_TOKEN is not set, so its ops surface is off. The library is read and judged through it." }; if (err.reason === "denied") return { reason: err.reason, sentence: "The editor refused umtool's WORKER_TOKEN: the two tokens differ. The library is read and judged through the editor." }; return { reason: err.reason, sentence: err.message }; } return { reason: "error", sentence: err instanceof Error ? err.message : String(err) }; } export async function loadLibrary(view: LibraryView): Promise { try { const libraries = (await listLibraries(ask)) as LibrarySummary[]; if (libraries.length === 0) { return { ok: false, reason: "no-library", sentence: "The editor has no X trending library. One is made in the editor (a channel with the platform x-trending), and its topics come from reading a trending page there." }; } const lib = view.lib ?? libraries[0].slug; if (!libraries.some((l) => l.slug === lib)) { return { ok: false, reason: "no-library", sentence: `The editor has no library "${lib}". Its libraries: ${libraries.map((l) => l.slug).join(", ")}.` }; } const seen = (await seenOf(REPORTS_ROOT, lib)) as Record; const shown = { ...view, lib }; const listing = (await listLibrary(ask, { ...listingParamsOf(shown, { seenAt: view.topic ? (seen[view.topic] ?? null) : null }), limit: PAGE_SIZE, offset: view.offset, detail: true, // New since Mark seen, per topic, answered by this same read (a topic never marked is new in full). seen, })) as unknown as XTrendingListing & { name?: string }; const topics: LibraryTopic[] = listing.topics.map((t) => ({ ...t, seenAt: seen[t.id] ?? null, fresh: t.fresh ?? t.posts })); return { ok: true, libraries, lib, name: listing.name ?? lib, view: shown, topics, posts: listing.posts, total: listing.total, authors: listing.authors ?? [], pageSize: PAGE_SIZE, }; } catch (err) { return { ok: false, ...unavailableSentence(err) }; } }