"use server"; // THE /cookies SCREEN'S BUTTONS (plans/cookie-screen.md). Each row has ONE, // and it runs the action that already exists for that service — nothing here // refreshes anything itself: // // x · refresh refreshXCookiesAction (/settings' Refresh cookies); when // X's alert is up, the recheck after it (X's sends no // request — the login on disk), so the alert clears at once // x · connect connectXAccountAction (a headed window on the editor's machine) // * · recheck recheckServiceCookiesAction (the dashboard's Recheck) // archive.today connectArchiveSessionAction (headed) // a forum host connectForumAt, at the thread the app last met there, else // the host's first thread channel (headed) // // Every one of them goes through lib/cookieRefreshGuard.ts: a second press // while one runs is refused with a sentence, and the outcome is the row's // "last result". Each answer carries the screen's rows read again, so the // row's age and state change in place, without a reload. import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { displayText } from "yt-dlp-transcript-common/lib/privacy/privacy-server"; import { ARCHIVE_TODAY_SERVICE, X_SERVICE, isServiceKey, serviceFamily, serviceLabel, } from "yt-dlp-transcript-common/lib/serviceCookies"; import { readServiceCookieState, raisedServiceAlert } from "yt-dlp-transcript-common/lib/serviceCookies-server"; import { forumThreadFor, type CookieStatusPayload } from "yt-dlp-transcript-common/lib/cookieStatus"; import { recheckServiceCookies } from "yt-dlp-transcript-common/controller/recheckServiceCookies"; import { connectXAccountAction, refreshXCookiesAction } from "../settings/xSessionActions"; import { connectArchiveSessionAction } from "../settings/archiveSessionActions"; import { recheckServiceCookiesAction } from "../operations/serviceCookiesActions"; import { connectForumAt } from "../lib/connectForum"; import { listForumChannels } from "../lib/forumChannels"; import { loadCookieStatus } from "../lib/cookieStatus"; import { loadCookieControls, type CookieControls } from "../lib/cookieControls"; import { writeXAccountState } from "yt-dlp-transcript-common/social/xAccountState"; import { clearXStop } from "yt-dlp-transcript-common/jobs/downloadBackoff"; export type CookiePressResult = { ok: boolean; message: string; // Refused because a press for the service is already running. busy?: boolean; status: CookieStatusPayload; }; type Answer = { ok: boolean; message: string; busy?: boolean }; function answerOf(r: { ok: true } | { ok: false; error: string; busy?: true }, okMessage: string): Answer { return r.ok ? { ok: true, message: okMessage } : { ok: false, message: r.error, ...("busy" in r && r.busy ? { busy: true } : {}) }; } async function press(service: string, action: string): Promise { if (!isServiceKey(service)) return { ok: false, message: `Not a service: ${service}` }; const family = serviceFamily(service); const paths = getPaths(); if (action === "recheck" && (family === "ytdlp" || family === "x")) { const r = await recheckServiceCookiesAction(service); return r.ok ? { ok: r.result === "clean", message: r.message } : answerOf(r, ""); } if (service === X_SERVICE && action === "refresh") { const r = await refreshXCookiesAction(); if (!r.ok) return answerOf(r, ""); // The jar is new; an X alert up since before it is rechecked from disk // (no request to X) so it clears now, not at the next X job. if (await raisedServiceAlert(paths, X_SERVICE)) { const o = await recheckServiceCookies({ paths, service: X_SERVICE }).catch(() => null); if (o) return { ok: true, message: `Cookies refreshed. ${o.message}` }; } return { ok: true, message: "Cookies refreshed." }; } if (service === X_SERVICE && action === "connect") { const r = await connectXAccountAction(); return answerOf(r, r.ok ? `Connected${r.browser ? ` with ${r.browser}` : ""}. Cookies exported.` : ""); } if (service === ARCHIVE_TODAY_SERVICE && action === "connect") { const r = await connectArchiveSessionAction(); return answerOf(r, r.ok ? (r.record.cleared ? "Connected: the check is passed." : "The window closed on a check page: not cleared.") : ""); } if (family === "forum" && action === "connect") { const thread = forumThreadFor(await readServiceCookieState(paths), service, await listForumChannels(paths).catch(() => [])); if (!thread) return { ok: false, message: `No thread on ${service} is recorded yet: connect from the thread's channel page.` }; const r = await connectForumAt(thread.url, service, thread.channel); return answerOf(r, r.ok ? (r.record.cleared ? "Connected: the thread showed." : "The window closed before the thread showed: not cleared.") : ""); } return { ok: false, message: `${serviceLabel(service)} has no "${action}" here.` }; } // The full controls' own slices, read again after a row's press changed what // they show (a Connect made a profile, a Refresh a jar): the same reads the // page made, never the whole settings object. export async function cookieControlsAction(): Promise { return loadCookieControls(getPaths()); } export async function pressCookieButtonAction(service: string, action: string): Promise { let answer: Answer; try { answer = await press(String(service ?? ""), String(action ?? "")); } catch (e) { answer = { ok: false, message: (e as Error).message }; } // An error may carry a path: drawn as the page draws one. return { ...answer, message: displayText(answer.message), status: await loadCookieStatus(getPaths(), { fresh: action === "recheck" }) }; } // THE X ACCOUNT'S STATE, MARKED BY HAND (lane x-sittings): "suspended" refuses // the X jobs that record posts as deleted or unavailable; "ok" lifts that. The // same write as `pnpm ops x-session --json '{"accountState":…}'`. Nothing is // asked of X. Answers the screen's rows read again. export async function setXAccountStateAction(state: string, note?: string): Promise { const paths = getPaths(); let answer: Answer; if (state !== "suspended" && state !== "ok") { answer = { ok: false, message: `Not an account state: ${String(state)}` }; } else { try { const r = await writeXAccountState(paths, state, typeof note === "string" && note.trim() ? { note } : {}); answer = { ok: true, message: r.state === "suspended" ? "Marked suspended." : "Marked ok." }; } catch (e) { answer = { ok: false, message: (e as Error).message }; } } return { ...answer, message: displayText(answer.message), status: await loadCookieStatus(paths) }; } // "CLEAR THE X STOP" on /cookies (common/jobs/downloadBackoff.ts clearXStop): // the page asks the operator to confirm what it risks first; logged. export async function clearXStopAction(confirm: boolean): Promise { const paths = getPaths(); let answer: Answer; try { const r = await clearXStop(paths, { confirm: confirm === true, by: "/cookies" }); answer = { ok: r.ok, message: r.message }; } catch (e) { answer = { ok: false, message: (e as Error).message }; } return { ...answer, message: displayText(answer.message), status: await loadCookieStatus(paths) }; }