import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { isValidChannelSlug } from "yt-dlp-transcript-common/controller/channels"; import { xSessionReport } from "yt-dlp-transcript-common/jobs/networkGuard"; import { NextResponse } from "next/server"; import { writeXAccountState } from "yt-dlp-transcript-common/social/xAccountState"; import { clearXStop } from "yt-dlp-transcript-common/jobs/downloadBackoff"; import { readRoute } from "../_read"; import { oneOf, ops, opsFail, optString } from "../_lib"; export const dynamic = "force-dynamic"; // GET /api/ops/x-session[?slug=] // -> { ok, source, sourceChosen, browserSpec?, profileDir, cookieFile, // hasProfile, hasCookies, looksAuthenticated, cookiesUpdatedAt, // ageMinutes, maxAgeMinutes, ageChecked, alert, cap, sitting, account, // cooldown, allowed, reason, refusedBy?, note? } // // The X login an X job would use and whether one would start NOW: the // fresh-jar rule's inputs (the source, the exported jar's age, the limit // social.x.maxCookieAgeMinutes) and its verdict, from xSessionReport — the // record the network guard refuses an X job with (common/jobs/networkGuard.ts). // `slug` names a channel whose own cookiesFromBrowser may decide the source. // Nothing is asked of X and nothing is written: a refresh is a human click on // /settings (Refresh cookies / Connect X account). export async function GET(request: Request) { return readRoute(request, ["slug"], async (q) => { const slug = q.get("slug")?.trim() || undefined; if (slug && !isValidChannelSlug(slug)) return opsFail(`"${slug}" is not a valid channel slug`); return { ...(await xSessionReport({ paths: getPaths(), channelSlug: slug })) }; }); } // POST /api/ops/x-session {"accountState": "suspended" | "ok", "note"?} // -> { ok, account: {state, note?, at, by} } // // The operator's word on the X account (social/xAccountState.ts): while it is // "suspended" the network guard refuses the X jobs that record posts as // deleted or unavailable, and every other X job logs it. Nothing is asked of // X; nothing else is written. /cookies has the same control. // // POST {"clearStop": true, "confirm"?: true, "note"?} — "Clear the X stop" // (common/jobs/downloadBackoff.ts clearXStop): the one way X's 15-minute stop // after a rate limit or a bot check ends before its time. Without "confirm" it // clears nothing and answers 409 with what it risks; each clear is logged. export async function POST(request: Request) { return ops(request, ["accountState", "note", "clearStop", "confirm"], async (body) => { if (body.clearStop !== undefined) { if (body.clearStop !== true) return opsFail('"clearStop" must be true'); if (body.accountState !== undefined) return opsFail('"clearStop" and "accountState" are two acts: send one'); const note = optString(body, "note"); const r = await clearXStop(getPaths(), { confirm: body.confirm === true, by: "pnpm ops x-session", ...(note ? { note } : {}) }); if (!r.ok) return opsFail(r.message, 409, { confirmNeeded: true }); return NextResponse.json({ ok: true, cleared: r.cleared, message: r.message }); } const state = oneOf(body, "accountState", ["suspended", "ok"] as const); const note = optString(body, "note"); const account = await writeXAccountState(getPaths(), state, note ? { note } : {}); return NextResponse.json({ ok: true, account }); }); }