// THE FORUM-THREAD CHANNELS, BY HOST — for the /cookies screen's forum rows // (one per host, with each thread channel's own Connect) and for its Connect // when no thread of the host is in the cookie state yet. One config read per // channel (listChannelConfigs: ~40 ms over 231 channels), never a channel's // data/; remembered for a minute, and read again at once when channels/ // itself changes (a channel created or removed moves its mtime) or `fresh`. import { stat } from "node:fs/promises"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; import { listChannelConfigs } from "yt-dlp-transcript-common/controller/channels"; import { parseXenforoThreadUrl } from "yt-dlp-transcript-common/social/xenforoParse"; export type ForumChannel = { host: string; slug: string; url: string }; export const FORUM_CHANNELS_TTL_MS = 60_000; type Memo = { key: string; at: number; rows: ForumChannel[] }; const KEY = Symbol.for("archilyzer.cookies.forumChannels"); function memo(): { last?: Memo } { const g = globalThis as unknown as Record; return (g[KEY] ??= {}); } export async function listForumChannels( paths: Paths, opts: { fresh?: boolean; now?: number } = {}, ): Promise { const now = opts.now ?? Date.now(); const mtime = await stat(paths.channelsDir).then((s) => s.mtimeMs, () => 0); const key = `${paths.channelsDir}:${mtime}`; const m = memo(); if (!opts.fresh && m.last && m.last.key === key && now - m.last.at < FORUM_CHANNELS_TTL_MS) return m.last.rows; const rows: ForumChannel[] = []; for (const { slug, config } of await listChannelConfigs(paths)) { if (config.platform !== "xenforo") continue; const thread = parseXenforoThreadUrl(config.url ?? ""); if (thread) rows.push({ host: thread.host, slug, url: thread.base }); } m.last = { key, at: now, rows }; return rows; }