// THE /cookies SCREEN'S FULL CONTROLS — each service's whole session block, as // /settings and a forum channel's page draw it (XSessionSection, // ArchiveSourcesSection, the yt-dlp cookie fields, ForumSessionConnect), fed // ONLY its own slice: the same reads /settings' page makes for those sections // (the X profile's status, the login source in use, social.x's visibility and // age limit; settings.archives and the archive session; cookiesFromBrowser and // cookieMode), and each forum host's session status. Never the whole settings // object, never a corpus walk. The rows themselves are lib/cookieStatus.ts. import { xPacingOf } from "./xPacing"; import { getPaths, type Paths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings, type SiteSettings } from "yt-dlp-transcript-common/lib/settings"; import { displayText } from "yt-dlp-transcript-common/lib/privacy/privacy-server"; import { readXSessionStatus, type XSessionStatus } from "yt-dlp-transcript-common/social/xSessionBroker"; import { resolveXCookieSourceFor } from "yt-dlp-transcript-common/social/xBrowserLogin"; import { xCookieSourceView, xMaxCookieAgeMinutes, type XCookieSourceView, type XPostsVisibility, } from "yt-dlp-transcript-common/social/xCookieSource"; import { xPostsVisibility } from "yt-dlp-transcript-common/lib/postsVisibility"; import { readArchiveSessionStatus, type ArchiveSessionStatus } from "yt-dlp-transcript-common/lib/archives/archiveTodaySession"; import type { ArchivesSettings } from "yt-dlp-transcript-common/lib/archives/settings"; import { readForumSessionStatus, type ForumSessionStatus } from "yt-dlp-transcript-common/social/forumSession"; import { forumServiceKey } from "yt-dlp-transcript-common/lib/serviceCookies"; import { DEFAULT_COOKIE_MODE } from "yt-dlp-transcript-common/lib/cookiePolicy"; import { listForumChannels } from "./forumChannels"; import { xSourceForDisplay } from "./xSourceDisplay"; import { sectionFailed, type CookieControls, type ForumControls, type SectionRead } from "./cookieControlsTypes"; export { sectionFailed, type CookieControls } from "./cookieControlsTypes"; async function read(what: string, fn: () => Promise): Promise> { try { return await fn(); } catch (e) { return { error: displayText(`Could not read ${what}: ${(e as Error).message.split("\n")[0]}`) }; } } export async function loadCookieControls( paths: Paths = getPaths(), // Injectable for the tests: a settings read that throws. deps: { getSettings?: () => SiteSettings } = {}, ): Promise { // One parse, shared; a settings read that throws fails the three sections // that need it, each saying so. const settings = await read("settings.json", async () => (deps.getSettings ?? getSettings)()); const need = (fn: (s: SiteSettings) => Promise) => async (): Promise => { if (sectionFailed(settings)) throw new Error(settings.error); return fn(settings); }; const [x, archive, ytdlp, forums] = await Promise.all([ read( "X's session", need(async (st) => { const [initial, resolved] = await Promise.all([readXSessionStatus(paths), resolveXCookieSourceFor(paths, st)]); return { initial, // Drawn by the client: its spec and label through settings.privacy. initialSource: xSourceForDisplay(xCookieSourceView(resolved)), initialVisibility: xPostsVisibility(st), initialMaxCookieAge: xMaxCookieAgeMinutes(st.social.x), initialPacing: xPacingOf(st.social.x), }; }), ), read( "archive.today's session", need(async (st) => ({ initial: st.archives, session: await readArchiveSessionStatus(paths) })), ), read( "yt-dlp's cookie settings", need(async (st) => ({ cookiesFromBrowser: st.cookiesFromBrowser ?? "", cookieMode: st.cookieMode ?? DEFAULT_COOKIE_MODE, })), ), read("the forum sessions", async () => { const out: ForumControls = {}; for (const c of await listForumChannels(paths)) { const key = forumServiceKey(c.host); if (!out[key]) out[key] = { status: await readForumSessionStatus(paths, c.host), channels: [] }; out[key]!.channels.push(c.slug); } for (const f of Object.values(out)) f.channels.sort(); return out; }), ]); return { x, archive, ytdlp, forums }; }