import { atom, createStore } from 'jotai'; import { MatrixClient } from 'matrix-js-sdk'; import { collectOwnEventIds, loadRedactJob, redactPending, RedactJobState, saveRedactJob, } from '../utils/redactOwnMessages'; /** * [Gitea #169] One background "delete all my messages" job per room, kept * outside React so it survives the settings dialog closing. State is mirrored * into `redactJobsAtom` for the UI and persisted for resume-after-reload. */ export const redactJobsAtom = atom>({}); const controllers = new Map(); type Store = ReturnType; const publish = (store: Store, state: RedactJobState) => { saveRedactJob(state); store.set(redactJobsAtom, (prev) => ({ ...prev, [state.roomId]: { ...state } })); }; export const isRedactJobRunning = (roomId: string): boolean => controllers.has(roomId); export const cancelRedactJob = (roomId: string): void => { controllers.get(roomId)?.abort(); }; export const clearRedactJob = (store: Store, roomId: string): void => { saveRedactJob({ roomId, phase: 'cancelled', pending: [], found: 0, redacted: 0, skipped: 0, leaveAfter: false, }); store.set(redactJobsAtom, (prev) => { const next = { ...prev }; delete next[roomId]; return next; }); }; /** Pick up a persisted, unfinished job (e.g. after a reload). */ export const hydrateRedactJob = (store: Store, roomId: string): RedactJobState | null => { const saved = loadRedactJob(roomId); if (saved && saved.pending.length > 0 && saved.phase !== 'done') { // Nothing is running after a reload — surface it as paused/resumable. const state: RedactJobState = { ...saved, phase: 'cancelled' }; store.set(redactJobsAtom, (prev) => ({ ...prev, [roomId]: state })); return state; } return null; }; export type RedactJobOptions = { leaveAfter: boolean; /** Called once everything is redacted (and the room left, if asked). */ onDone?: (state: RedactJobState) => void; }; /** * Start (or resume) the job. Resolves when it stops for any reason; errors * and cancellation are reported through the state, not thrown. */ export async function runRedactJob( store: Store, mx: MatrixClient, roomId: string, opts: RedactJobOptions, resume?: RedactJobState, ): Promise { if (controllers.has(roomId)) return store.get(redactJobsAtom)[roomId]; const ctrl = new AbortController(); controllers.set(roomId, ctrl); const state: RedactJobState = resume ?? { roomId, phase: 'collecting', pending: [], found: 0, redacted: 0, skipped: 0, leaveAfter: opts.leaveAfter, }; state.leaveAfter = opts.leaveAfter; publish(store, state); try { if (!resume) { const ids = await collectOwnEventIds( mx, roomId, mx.getSafeUserId(), (found) => { state.found = found; publish(store, state); }, ctrl.signal, ); state.pending = ids; state.found = ids.length; } state.phase = 'redacting'; publish(store, state); await redactPending(mx, state, (s) => publish(store, s), ctrl.signal); state.phase = 'done'; if (state.leaveAfter) { await mx.leave(roomId); } publish(store, state); opts.onDone?.(state); } catch (e) { if ((e as { name?: string })?.name === 'AbortError') { state.phase = 'cancelled'; } else { state.phase = 'error'; state.error = e instanceof Error ? e.message : String(e); } publish(store, state); } finally { controllers.delete(roomId); } return state; }