import type { CryptoCallbacks, MatrixClient, MSC3575SlidingSyncRequest, MSC3575SlidingSyncResponse, } from '$types/matrix-sdk'; import { ClientEvent, createClient, IndexedDBStore, IndexedDBCryptoStore, SyncState, } from '$types/matrix-sdk'; import { clearNavToActivePathStore } from '$state/navToActivePath'; import type { Session, Sessions, SessionStoreName } from '$state/sessions'; import { getSessionStoreName, MATRIX_SESSIONS_KEY } from '$state/sessions'; import { getLocalStorageItem } from '$state/utils/atomWithLocalStorage'; import { createLogger } from '$utils/debug'; import { createDebugLogger } from '$utils/debugLogger'; import * as Sentry from '@sentry/react'; import { pushSessionToSW } from '../sw-session'; import { cryptoCallbacks } from './secretStorageKeys'; import type { SlidingSyncDiagnostics } from './slidingSync'; import { SlidingSyncManager } from './slidingSync'; import { PresenceSyncManager } from './presenceSync'; const log = createLogger('initMatrix'); const debugLog = createDebugLogger('initMatrix'); const slidingSyncByClient = new WeakMap(); const presenceSyncByClient = new WeakMap(); const presenceStartCleanupByClient = new WeakMap void>(); const SLIDING_SYNC_POLL_TIMEOUT_MS = 20000; const isInitialSyncReady = (state: string | null): boolean => state === SyncState.Prepared || state === SyncState.Syncing || state === SyncState.Catchup; const startPresenceAfterInitialSync = ( mx: MatrixClient, manager: PresenceSyncManager ): (() => void) => { let started = false; let startTimer: ReturnType | undefined; const start = () => { if (started) return; started = true; presenceStartCleanupByClient.delete(mx); mx.removeListener(ClientEvent.Sync, onSync); manager.start(); }; const scheduleStart = () => { if (startTimer !== undefined) return; startTimer = globalThis.setTimeout(() => { startTimer = undefined; start(); }, 0); }; const onSync = (state: SyncState) => { if (isInitialSyncReady(state)) scheduleStart(); }; if (isInitialSyncReady(mx.getSyncState())) scheduleStart(); else mx.on(ClientEvent.Sync, onSync); const cleanup = () => { if (startTimer !== undefined) globalThis.clearTimeout(startTimer); mx.removeListener(ClientEvent.Sync, onSync); presenceStartCleanupByClient.delete(mx); }; presenceStartCleanupByClient.set(mx, cleanup); return cleanup; }; type StartupPhase = 'sync_store' | 'rust_crypto' | 'client_init' | 'client_start'; const measureStartupPhase = async ( phase: StartupPhase, task: () => Promise, attributes?: Record ): Promise => { const startTime = performance.now(); try { const result = await task(); Sentry.metrics.distribution('sable.startup.phase_ms', performance.now() - startTime, { attributes: { phase, outcome: 'success', ...attributes }, }); return result; } catch (error) { Sentry.metrics.distribution('sable.startup.phase_ms', performance.now() - startTime, { attributes: { phase, outcome: 'error', ...attributes }, }); throw error; } }; type FetchRoomEventResult = Awaited>; type MatrixClientWithWritableFetchRoomEvent = MatrixClient & { fetchRoomEvent: (roomId: string, eventId: string) => Promise; }; const fetchRoomEventStartupCleanupByClient = new WeakMap void>(); const slidingSyncConnIdCleanupByClient = new WeakMap void>(); type SlidingSyncMethod = ( reqBody: MSC3575SlidingSyncRequest, baseUrl?: string, abortSignal?: AbortSignal ) => Promise; type MatrixClientWithWritableSlidingSync = MatrixClient & { slidingSync: SlidingSyncMethod; }; type SlidingSyncRequestWithConnId = MSC3575SlidingSyncRequest & { conn_id?: string }; const SLIDING_SYNC_CONN_ID = 'sable-main'; function installSlidingSyncConnId(mx: MatrixClient): void { slidingSyncConnIdCleanupByClient.get(mx)?.(); const mxWritable = mx as MatrixClientWithWritableSlidingSync; const original = mx.slidingSync.bind(mx) as SlidingSyncMethod; mxWritable.slidingSync = (reqBody, baseUrl, abortSignal) => { const req = reqBody as SlidingSyncRequestWithConnId; if (req.conn_id === undefined) { req.conn_id = SLIDING_SYNC_CONN_ID; } return original(reqBody, baseUrl, abortSignal); }; slidingSyncConnIdCleanupByClient.set(mx, () => { slidingSyncConnIdCleanupByClient.delete(mx); mxWritable.slidingSync = original; }); } function installStartupFetchRoomEventPatch( mx: MatrixClient, slidingSyncManager: SlidingSyncManager ): void { fetchRoomEventStartupCleanupByClient.get(mx)?.(); const mxWritable = mx as MatrixClientWithWritableFetchRoomEvent; const origFetchRoomEvent = mx.fetchRoomEvent.bind(mx); const restore = () => { fetchRoomEventStartupCleanupByClient.delete(mx); mxWritable.fetchRoomEvent = origFetchRoomEvent; }; mxWritable.fetchRoomEvent = (roomId: string, eventId: string) => { if (slidingSyncManager.isRoomActive(roomId)) { return origFetchRoomEvent(roomId, eventId); } const cachedEvent = mx.getRoom(roomId)?.findEventById(eventId); const payload: FetchRoomEventResult = cachedEvent?.event ?? { event_id: eventId, room_id: roomId, }; return Promise.resolve(payload); }; fetchRoomEventStartupCleanupByClient.set(mx, restore); } const deleteDatabase = (name: string): Promise => new Promise((resolve) => { const req = window.indexedDB.deleteDatabase(name); req.addEventListener('success', () => resolve()); req.addEventListener('error', () => resolve()); // resolve anyway — we tried req.addEventListener('blocked', () => resolve()); }); const deleteSyncStoreGroup = async (syncStoreName: string): Promise => { await Promise.all([ deleteDatabase(syncStoreName), deleteDatabase(syncStoreName.replace(/^sync/, 'crypto')), deleteDatabase(`${syncStoreName}::matrix-sdk-crypto`), ]); }; const deleteSessionStores = async (storeName: SessionStoreName): Promise => { await Promise.all([ deleteDatabase(storeName.sync), deleteDatabase(storeName.crypto), deleteDatabase(`${storeName.rustCryptoPrefix}::matrix-sdk-crypto`), ]); }; /** * Reads the account stored in an IndexedDB sync store without opening a full MatrixClient. * Returns undefined if the database doesn't exist or has no account record. */ const readStoredAccount = (dbName: string): Promise => new Promise((resolve) => { let settled = false; const finish = (value: string | undefined) => { if (settled) return; settled = true; resolve(value); }; const req = window.indexedDB.open(dbName); req.addEventListener('error', () => finish(undefined)); req.addEventListener('success', () => { const db = req.result; try { if (!db.objectStoreNames.contains('account')) { db.close(); finish(undefined); } else { const tx = db.transaction('account', 'readonly'); const store = tx.objectStore('account'); const getReq = store.get('account'); getReq.addEventListener('success', () => { db.close(); const record = getReq.result; if (!record?.account_data) { finish(undefined); } else { try { const data = JSON.parse(record.account_data); finish(data?.user_id ?? undefined); } catch { finish(undefined); } } }); getReq.addEventListener('error', () => { db.close(); finish(undefined); }); } } catch { try { db.close(); } catch { /* ignore */ } finish(undefined); } }); }); const isMismatch = (err: unknown): boolean => { const msg = err instanceof Error ? err.message : String(err); return ( msg.includes("doesn't match") || msg.includes('does not match') || msg.includes('account in the store') || msg.includes('account in the constructor') ); }; /** * Pre-flight check: scans every IndexedDB database and deletes any that * belong to a userId not present in the stored sessions list, or whose * sync-store data contradicts the expected session userId. * Call this once on startup before initClient. */ export const clearMismatchedStores = async (): Promise => { const sessions = getLocalStorageItem(MATRIX_SESSIONS_KEY, []); const knownUserIds = new Set(sessions.map((s) => s.userId)); const knownStoreNames = new Set( sessions.flatMap((s) => { const sn = getSessionStoreName(s); return [sn.sync, sn.crypto, `${sn.rustCryptoPrefix}::matrix-sdk-crypto`]; }) ); let allDbs: IDBDatabaseInfo[] = []; try { allDbs = await window.indexedDB.databases(); } catch { // databases() not supported in all browsers } await Promise.all( allDbs.map(async ({ name }) => { if (!name) return; const containsKnownUser = Array.from(knownUserIds).some((uid) => name.includes(uid)); const looksLikeUserDb = name.includes('@'); if (looksLikeUserDb && !containsKnownUser && !knownStoreNames.has(name)) { log.warn(`clearMismatchedStores: "${name}" has unknown user — deleting`); await deleteDatabase(name); return; } if (!name.startsWith('sync')) return; const storedUserId = await readStoredAccount(name); if (!storedUserId) return; if (!knownUserIds.has(storedUserId)) { log.warn(`clearMismatchedStores: "${name}" has unknown user ${storedUserId} — deleting`); await deleteSyncStoreGroup(name); return; } const expectedStore = `sync${storedUserId}`; if (name !== expectedStore && !knownStoreNames.has(name)) { log.warn(`clearMismatchedStores: "${name}" is misplaced for ${storedUserId} — deleting`); await deleteSyncStoreGroup(name); } }) ); await Promise.all( sessions.map(async (session) => { const sn = getSessionStoreName(session); const storedUserId = await readStoredAccount(sn.sync); if (storedUserId && storedUserId !== session.userId) { log.warn( `clearMismatchedStores: "${sn.sync}" has ${storedUserId} but session is ${session.userId} — deleting` ); await deleteSessionStores(sn); } }) ); }; type BuiltClient = { mx: MatrixClient; indexedDBStore: IndexedDBStore; }; const buildClient = (session: Session): BuiltClient => { const storeName = getSessionStoreName(session); const indexedDBStore = new IndexedDBStore({ indexedDB: global.indexedDB, localStorage: global.localStorage, dbName: storeName.sync, }); const legacyCryptoStore = new IndexedDBCryptoStore(global.indexedDB, storeName.crypto); const mx = createClient({ baseUrl: session.baseUrl, accessToken: session.accessToken, userId: session.userId, store: indexedDBStore, cryptoStore: legacyCryptoStore, deviceId: session.deviceId, timelineSupport: true, cryptoCallbacks: cryptoCallbacks as unknown as CryptoCallbacks, verificationMethods: ['m.sas.v1'], }); return { mx, indexedDBStore }; }; type ClientInitializationResult = | { ok: true; mx: MatrixClient } | { ok: false; error: unknown; phase: 'sync_store' | 'rust_crypto' }; const initializeClient = async ( session: Session, cryptoDatabasePrefix: string ): Promise => { let builtClient: BuiltClient; try { builtClient = buildClient(session); } catch (error) { return { ok: false, error, phase: 'sync_store' }; } const { mx, indexedDBStore } = builtClient; void mx.getVersions().catch(() => undefined); const syncStorePromise = measureStartupPhase('sync_store', () => indexedDBStore.startup()); const cryptoPromise = measureStartupPhase('rust_crypto', () => mx.initRustCrypto({ cryptoDatabasePrefix }) ); const [syncStoreResult, cryptoResult] = await Promise.allSettled([ syncStorePromise, cryptoPromise, ]); if (syncStoreResult.status === 'rejected') { mx.stopClient(); return { ok: false, error: syncStoreResult.reason, phase: 'sync_store' }; } if (cryptoResult.status === 'rejected') { mx.stopClient(); return { ok: false, error: cryptoResult.reason, phase: 'rust_crypto' }; } return { ok: true, mx }; }; export const initClient = async (session: Session): Promise => { const storeName = getSessionStoreName(session); debugLog.info('sync', 'Initializing Matrix client', { userId: session.userId, baseUrl: session.baseUrl, }); const wipeAllStores = async () => { log.warn('initClient: wiping all stores for', session.userId); debugLog.warn('sync', 'Wiping all stores due to mismatch', { userId: session.userId, }); Sentry.addBreadcrumb({ category: 'crypto', message: 'Crypto store mismatch — wiping local stores and retrying', level: 'warning', }); Sentry.metrics.count('sable.crypto.store_wipe', 1); await deleteSessionStores(storeName); try { const allDbs = await window.indexedDB.databases(); await Promise.all( allDbs.map(async ({ name }) => { if (name && name.includes(session.userId)) { log.warn('initClient: also wiping db', name); await deleteDatabase(name); } }) ); } catch { // databases() not available in all browsers } }; const initStartTime = performance.now(); let initOutcome = 'success'; try { let result = await initializeClient(session, storeName.rustCryptoPrefix); if (!result.ok) { if (!isMismatch(result.error)) { debugLog.error('sync', 'Failed to initialize client', { phase: result.phase, error: result.error, }); throw result.error; } log.warn(`initClient: mismatch during ${result.phase} — wiping and retrying:`, result.error); debugLog.warn('sync', 'Client initialization mismatch - wiping stores and retrying', { phase: result.phase, error: result.error, }); await wipeAllStores(); result = await initializeClient(session, storeName.rustCryptoPrefix); if (!result.ok) { debugLog.error('sync', 'Failed to initialize client after store reset', { phase: result.phase, error: result.error, }); throw result.error; } } result.mx.setMaxListeners(50); return result.mx; } catch (error) { initOutcome = 'error'; throw error; } finally { Sentry.metrics.distribution('sable.startup.phase_ms', performance.now() - initStartTime, { attributes: { phase: 'client_init', outcome: initOutcome }, }); } }; export type StartClientConfig = { baseUrl?: string; sessionSlidingSyncOptIn?: boolean; pollTimeoutMs?: number; timelineLimit?: number; }; export type ClientSyncDiagnostics = { transport: 'sliding' | 'classic'; syncState: string | null; sliding?: SlidingSyncDiagnostics; }; const disposeSlidingSync = (mx: MatrixClient): void => { const manager = slidingSyncByClient.get(mx); if (!manager) return; manager.dispose(); slidingSyncByClient.delete(mx); }; const disposePresenceSync = (mx: MatrixClient): void => { presenceStartCleanupByClient.get(mx)?.(); const manager = presenceSyncByClient.get(mx); if (!manager) return; manager.dispose(); presenceSyncByClient.delete(mx); }; export const getSlidingSyncManager = (mx: MatrixClient): SlidingSyncManager | undefined => slidingSyncByClient.get(mx); export const getPresenceSyncManager = (mx: MatrixClient): PresenceSyncManager | undefined => presenceSyncByClient.get(mx); export const startClient = async (mx: MatrixClient, config?: StartClientConfig): Promise => { disposeSlidingSync(mx); disposePresenceSync(mx); const baseUrl = config?.baseUrl ?? mx.baseUrl; const useSliding = config?.sessionSlidingSyncOptIn === true; debugLog.info('sync', 'Starting Matrix client', { userId: mx.getUserId() }); const presenceManager = new PresenceSyncManager(mx); presenceSyncByClient.set(mx, presenceManager); startPresenceAfterInitialSync(mx, presenceManager); let manager: SlidingSyncManager | undefined; if (useSliding) { manager = new SlidingSyncManager(mx, baseUrl, { pollTimeoutMs: config?.pollTimeoutMs ?? SLIDING_SYNC_POLL_TIMEOUT_MS, timelineLimit: config?.timelineLimit, }); installStartupFetchRoomEventPatch(mx, manager); installSlidingSyncConnId(mx); manager.attach(); slidingSyncByClient.set(mx, manager); } try { await measureStartupPhase( 'client_start', () => mx.startClient({ lazyLoadMembers: true, slidingSync: manager?.slidingSync, threadSupport: true, }), { transport: useSliding ? 'sliding' : 'classic' } ); } catch (err) { debugLog.error('network', 'Failed to start client with sliding sync', { error: err instanceof Error ? err.message : String(err), userId: mx.getUserId(), baseUrl: useSliding ? baseUrl : undefined, stack: err instanceof Error ? err.stack : undefined, }); disposeSlidingSync(mx); disposePresenceSync(mx); throw err; } }; export const stopClient = (mx: MatrixClient): void => { log.log('stopClient', mx.getUserId()); debugLog.info('sync', 'Stopping client', { userId: mx.getUserId() }); slidingSyncConnIdCleanupByClient.get(mx)?.(); disposeSlidingSync(mx); disposePresenceSync(mx); mx.stopClient(); }; export const clearCacheAndReload = async (mx: MatrixClient) => { log.log('clearCacheAndReload', mx.getUserId()); stopClient(mx); clearNavToActivePathStore(mx.getSafeUserId()); await mx.store.deleteAllData(); window.location.reload(); }; export const getClientSyncDiagnostics = (mx: MatrixClient): ClientSyncDiagnostics => { const slidingManager = slidingSyncByClient.get(mx); return { transport: slidingManager ? 'sliding' : 'classic', syncState: mx.getSyncState(), sliding: slidingManager?.getDiagnostics(), }; }; /** * Logs out a Matrix client and cleans up its SDK stores + IndexedDB databases. * Does NOT touch the Jotai sessions atom — callers must do that themselves * so the correct Jotai Provider store is used. */ export const logoutClient = async (mx: MatrixClient, session?: Session) => { log.log('logoutClient', { userId: mx.getUserId(), sessionUserId: session?.userId, }); debugLog.info('general', 'Logging out client', { userId: mx.getUserId() }); pushSessionToSW(); stopClient(mx); try { await mx.logout(); debugLog.info('general', 'Logout successful', { userId: mx.getUserId() }); } catch { // ignore } if (session) { const storeName: SessionStoreName = getSessionStoreName(session); await mx.clearStores({ cryptoDatabasePrefix: storeName.rustCryptoPrefix }); await deleteDatabase(storeName.sync); await deleteDatabase(storeName.crypto); await deleteDatabase(`${storeName.rustCryptoPrefix}::matrix-sdk-crypto`); } else { await mx.clearStores(); window.localStorage.clear(); } }; export const clearLoginData = async () => { debugLog.info('general', 'Clearing all login data and reloading'); const dbs = await window.indexedDB.databases(); dbs.forEach((idbInfo) => { const { name } = idbInfo; if (name) window.indexedDB.deleteDatabase(name); }); window.localStorage.clear(); window.location.reload(); };