"use server"; // THE ARCHIVE SESSION (release 37 lane F, common/lib/archives/archiveTodaySession.ts): // archive.today's check, passed once by the operator in a HEADED window on the // archive profile, so the headless reads behind a check go through. Only this // explicit action opens a window; an import that meets a check it cannot pass // stops and says to come here. Nothing is ever solved by code. // // And settings.archives (host, pace, the browser fallback), saved as a patch. import { revalidatePath } from "next/cache"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { connectArchiveSession, readArchiveSessionStatus, type ArchiveSessionRecord, type ArchiveSessionStatus, } from "yt-dlp-transcript-common/lib/archives/archiveTodaySession"; import { isArchiveTodayUrl } from "yt-dlp-transcript-common/lib/archives/mementos"; import { sanitizeArchives, type ArchivesSettings } from "yt-dlp-transcript-common/lib/archives/settings"; import { saveSettings } from "./saveSettings"; import { withCookieAction } from "../lib/cookieRefreshGuard"; import { noteServiceCookieClean } from "yt-dlp-transcript-common/lib/serviceCookies-server"; import { ARCHIVE_TODAY_SERVICE } from "yt-dlp-transcript-common/lib/serviceCookies"; export async function archiveSessionStatusAction(): Promise { return readArchiveSessionStatus(getPaths()); } // Open the window at `url` (an archive.today copy the check stood in front // of), else at archive.today's front page on the canonical host. export type ConnectArchiveResult = { ok: true; record: ArchiveSessionRecord } | { ok: false; error: string }; // Through the one-press-per-service guard (lib/cookieRefreshGuard.ts). export async function connectArchiveSessionAction(url?: string): Promise { return withCookieAction(ARCHIVE_TODAY_SERVICE, "connect", () => connectArchiveNow(url), (r) => r.ok ? { ok: true, message: r.record.cleared ? `Connected: the window closed past the check (${r.record.cookies} cookies).` : "The window closed on a check page: not cleared.", } : { ok: false, message: r.error }, ); } async function connectArchiveNow(url?: string): Promise { const host = getSettings().archives.archiveTodayHost; const at = url?.trim() && isArchiveTodayUrl(url.trim()) ? url.trim() : `https://${host}/`; try { const record = await connectArchiveSession(getPaths(), at, { onLog: (line) => console.log(`[archive-session] ${line}`), }); // A window that closed on a page that is not a check clears the cookie // refresh alert for archive.today (the dashboard's banner). if (record.cleared) { await noteServiceCookieClean(getPaths(), ARCHIVE_TODAY_SERVICE, undefined, { by: "connect" }); } revalidatePath("/settings"); return { ok: true, record }; } catch (e) { return { ok: false, error: (e as Error).message }; } } export async function saveArchivesSettingsAction( patch: Partial, ): Promise<{ ok: true; archives: ArchivesSettings } | { ok: false; error: string }> { try { const archives = sanitizeArchives({ ...getSettings().archives, ...patch }); await saveSettings({ archives }); revalidatePath("/settings"); return { ok: true, archives }; } catch (e) { return { ok: false, error: (e as Error).message }; } }