// THE X ACCOUNT'S STATE, AS THE OPERATOR MARKED IT (lane x-sittings): whether // the account X answers as is suspended (read-only). `/.x-session/account.json`: // { "version": 1, "state": "suspended" | "ok", "note"?: "…", "at": , "by": "operator" } // // Marked BY HAND only (`pnpm ops x-session --json '{"accountState":…}'`, the // control on /cookies). The fetchers were searched for an explicit signal of // OUR account's suspension in X's own answers and carry none (gallery-dl's // "account suspended" is about the account being read, not ours — // social/xGalleryDlSignals.ts), so nothing here guesses one. // // While it is "suspended" the network guard refuses the X jobs that record // posts as deleted or unavailable (lib/xSitting.ts xSuspendedRefuses), and // every other X job's log says the account is suspended. // // A file that cannot be read is taken as SUSPENDED (fail closed: the jobs it // refuses are the ones that could write a false "deleted"). // // SERVER-ONLY (node:fs). import path from "node:path"; import type { Paths } from "../lib/paths"; import { readJsonFile, writeJsonAtomic } from "../lib/jsonFile-server"; import { isXAccountState, type XAccountState } from "../lib/xSitting"; export function xAccountStateFile(paths: Pick): string { return path.join(paths.transcriptsDir, ".x-session", "account.json"); } export type XAccountRecord = { state: XAccountState; note?: string; at: string; by: "operator"; // The file is there and could not be read: taken as suspended. unreadable?: true; }; export const X_ACCOUNT_NOTE_MAX = 300; // The state on record; absent means "ok" (nothing marked). export async function readXAccountState(paths: Pick): Promise { const read = await readJsonFile(xAccountStateFile(paths)); if (!read.ok) { if (read.reason === "absent") return null; return { state: "suspended", at: new Date(0).toISOString(), by: "operator", unreadable: true, note: "account.json cannot be read" }; } const v = read.value as Record | null; if (!v || typeof v !== "object" || !isXAccountState(v.state) || typeof v.at !== "string") { return { state: "suspended", at: new Date(0).toISOString(), by: "operator", unreadable: true, note: "account.json cannot be read" }; } return { state: v.state, at: v.at, by: "operator", ...(typeof v.note === "string" && v.note.trim() ? { note: v.note.trim().slice(0, X_ACCOUNT_NOTE_MAX) } : {}), }; } // The operator marks the account. Answers what is now on record. export async function writeXAccountState( paths: Pick, state: XAccountState, opts: { note?: string; now?: number } = {}, ): Promise { const note = opts.note?.trim().slice(0, X_ACCOUNT_NOTE_MAX); const record: XAccountRecord = { state, at: new Date(opts.now ?? Date.now()).toISOString(), by: "operator", ...(note ? { note } : {}), }; await writeJsonAtomic(xAccountStateFile(paths), { version: 1, ...record }, { mkdir: true }); return record; } // The alert line /cookies and `get x-session` show while it is suspended. export function xAccountAlertLine(r: XAccountRecord): string { if (r.unreadable) return "The X account's state cannot be read (.x-session/account.json): taken as suspended until it is marked again."; return `The X account is marked suspended (since ${r.at.slice(0, 16).replace("T", " ")} UTC${r.note ? `: ${r.note}` : ""}). Availability checks and captures that record deleted posts are refused.`; } // THE LAST LINE BEFORE A VERDICT IS WRITTEN (an X capture, an X availability // check): while the account is marked suspended — marked during the run, the // guard having let the job start — no post is recorded deleted or // unavailable from what it answered. Only "available" is kept. Answers the // observations to write (the same map when nothing is dropped). export async function keepVerdictsTrustedWhileSuspended( paths: Pick, observations: Map, log: (line: string) => void, ): Promise> { const acct = await readXAccountState(paths).catch(() => null); if (acct?.state !== "suspended") return observations; const kept = new Map(); let dropped = 0; for (const [id, v] of observations) { if (v === "available") kept.set(id, v); else dropped++; } if (dropped > 0) { log( `The X account is marked suspended: ${dropped} "deleted" or "unavailable" verdict(s) from its answers were NOT recorded ` + "(a suspended account's answers are thinned).", ); } return kept; }