Files
Sable/src/client/initMatrix.ts
T

624 lines
20 KiB
TypeScript

import type {
CryptoCallbacks,
MatrixClient,
MSC3575SlidingSyncRequest,
MSC3575SlidingSyncResponse,
} from '$types/matrix-sdk';
import {
ClientEvent,
createClient,
IndexedDBStore,
IndexedDBCryptoStore,
KnownMembership,
SyncState,
} from '$types/matrix-sdk';
import { fetch } from '$utils/fetch';
import { clearMediaCache } from '$utils/mediaCache';
import { clearNavToActivePathStore } from '$state/navToActivePath';
import type { Session, SessionStoreName } from '$state/sessions';
import { getSessionStoreName } from '$state/sessions';
import { createLogger } from '$utils/debug';
import { createDebugLogger } from '$utils/debugLogger';
import * as Sentry from '@sentry/react';
import { pushSessionToSW } from '../sw-session';
import { SessionOidcTokenRefresher } from './oidcTokenRefresher';
import { cryptoCallbacks } from './secretStorageKeys';
import type { SlidingSyncDiagnostics } from './slidingSync';
import { scopeEphemeralExtensions, SlidingSyncManager } from './slidingSync';
import { PresenceSyncManager } from './presenceSync';
import { SlidingSyncSidebarCache } from './slidingSyncSidebarCache';
import { hydrateRoomMember } from './roomMemberHydration';
const log = createLogger('initMatrix');
const debugLog = createDebugLogger('initMatrix');
const slidingSyncByClient = new WeakMap<MatrixClient, SlidingSyncManager>();
const membershipActionCleanupByClient = new WeakMap<MatrixClient, () => void>();
const presenceSyncByClient = new WeakMap<MatrixClient, PresenceSyncManager>();
const presenceStartCleanupByClient = new WeakMap<MatrixClient, () => void>();
const SLIDING_SYNC_POLL_TIMEOUT_MS = 45000;
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<typeof globalThis.setTimeout> | 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 <T>(
phase: StartupPhase,
task: () => Promise<T>,
attributes?: Record<string, string>
): Promise<T> => {
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<ReturnType<MatrixClient['fetchRoomEvent']>>;
type MatrixClientWithWritableFetchRoomEvent = MatrixClient & {
fetchRoomEvent: (roomId: string, eventId: string) => Promise<FetchRoomEventResult>;
};
const fetchRoomEventStartupCleanupByClient = new WeakMap<MatrixClient, () => void>();
const slidingSyncRequestCleanupByClient = new WeakMap<MatrixClient, () => void>();
type SlidingSyncMethod = (
reqBody: MSC3575SlidingSyncRequest,
baseUrl?: string,
abortSignal?: AbortSignal
) => Promise<MSC3575SlidingSyncResponse>;
type MatrixClientWithWritableSlidingSync = MatrixClient & {
slidingSync: SlidingSyncMethod;
};
type SlidingSyncRequestWithConnId = MSC3575SlidingSyncRequest & {
conn_id?: string;
};
const SLIDING_SYNC_CONN_ID = 'sable-main';
function installSlidingSyncRequestPatch(mx: MatrixClient, manager: SlidingSyncManager): void {
slidingSyncRequestCleanupByClient.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;
}
const roomIds = manager.getActiveRoomSubscriptionIds();
scopeEphemeralExtensions(req.extensions, roomIds);
return original(reqBody, baseUrl, abortSignal);
};
slidingSyncRequestCleanupByClient.set(mx, () => {
slidingSyncRequestCleanupByClient.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);
let restored = false;
const restore = () => {
if (restored) return;
restored = true;
fetchRoomEventStartupCleanupByClient.delete(mx);
mx.removeListener(ClientEvent.Sync, onSync);
mxWritable.fetchRoomEvent = origFetchRoomEvent;
};
const onSync = (state: SyncState) => {
if (isInitialSyncReady(state)) restore();
};
mxWritable.fetchRoomEvent = (roomId: string, eventId: string) => {
if (slidingSyncManager.isRoomActive(roomId)) {
return origFetchRoomEvent(roomId, eventId).then((event) => {
if (typeof event.sender === 'string') {
void hydrateRoomMember(mx, roomId, event.sender);
}
return event;
});
}
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);
mx.on(ClientEvent.Sync, onSync);
}
const deleteDatabase = (name: string): Promise<void> =>
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 deleteSessionStores = async (storeName: SessionStoreName): Promise<void> => {
await Promise.all([
deleteDatabase(storeName.sync),
deleteDatabase(storeName.crypto),
deleteDatabase(`${storeName.rustCryptoPrefix}::matrix-sdk-crypto`),
]);
};
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')
);
};
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 tokenRefresher =
session.oidc && session.refreshToken ? new SessionOidcTokenRefresher(session) : undefined;
const mx = createClient({
baseUrl: session.baseUrl,
fetchFn: fetch,
accessToken: session.accessToken,
refreshToken: session.refreshToken,
userId: session.userId,
store: indexedDBStore,
cryptoStore: legacyCryptoStore,
deviceId: session.deviceId,
timelineSupport: true,
cryptoCallbacks: cryptoCallbacks as unknown as CryptoCallbacks,
verificationMethods: ['m.sas.v1'],
tokenRefreshFunction: tokenRefresher
? (refreshToken) => tokenRefresher.doRefreshAccessToken(refreshToken)
: undefined,
});
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<ClientInitializationResult> => {
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<MatrixClient> => {
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;
initialRoomIds?: Iterable<string>;
onCachedRoomsLoaded?: () => void;
};
export type ClientSyncDiagnostics = {
transport: 'sliding' | 'classic';
syncState: string | null;
sliding?: SlidingSyncDiagnostics;
};
const disposeSlidingSync = (mx: MatrixClient): void => {
membershipActionCleanupByClient.get(mx)?.();
const manager = slidingSyncByClient.get(mx);
if (!manager) return;
manager.dispose();
slidingSyncByClient.delete(mx);
};
type MatrixClientWithWritableMembershipActions = MatrixClient & {
joinRoom: MatrixClient['joinRoom'];
leave: MatrixClient['leave'];
};
const installMembershipActionReconciliation = (
mx: MatrixClient,
manager: SlidingSyncManager
): void => {
membershipActionCleanupByClient.get(mx)?.();
const writableMx = mx as MatrixClientWithWritableMembershipActions;
// Keep the exact methods so cleanup restores the client without leaving bound replacements behind.
// oxlint-disable-next-line typescript/unbound-method
const originalJoinRoom = mx.joinRoom;
// oxlint-disable-next-line typescript/unbound-method
const originalLeave = mx.leave;
writableMx.joinRoom = async (...args) => {
const room = await originalJoinRoom.apply(mx, args);
manager.reconcileRoomMembership(room.roomId, KnownMembership.Join);
return room;
};
writableMx.leave = async (...args) => {
const result = await originalLeave.apply(mx, args);
manager.reconcileRoomMembership(args[0], KnownMembership.Leave);
return result;
};
membershipActionCleanupByClient.set(mx, () => {
membershipActionCleanupByClient.delete(mx);
writableMx.joinRoom = originalJoinRoom;
writableMx.leave = originalLeave;
});
};
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<void> => {
disposeSlidingSync(mx);
disposePresenceSync(mx);
const baseUrl = config?.baseUrl ?? mx.baseUrl;
const useSliding = config?.sessionSlidingSyncOptIn === true;
debugLog.info('sync', 'Starting Matrix client', { userId: mx.getUserId() });
let manager: SlidingSyncManager | undefined;
if (useSliding) {
const presenceManager = new PresenceSyncManager(mx);
presenceSyncByClient.set(mx, presenceManager);
startPresenceAfterInitialSync(mx, presenceManager);
manager = new SlidingSyncManager(mx, baseUrl, {
pollTimeoutMs: config?.pollTimeoutMs ?? SLIDING_SYNC_POLL_TIMEOUT_MS,
timelineLimit: config?.timelineLimit,
initialRoomIds: config?.initialRoomIds,
});
installStartupFetchRoomEventPatch(mx, manager);
installSlidingSyncRequestPatch(mx, manager);
manager.attach();
manager.prepareSidebarCacheHydration();
installMembershipActionReconciliation(mx, manager);
slidingSyncByClient.set(mx, manager);
}
try {
await measureStartupPhase(
'client_start',
() =>
mx.startClient({
lazyLoadMembers: true,
slidingSync: manager?.slidingSync,
threadSupport: true,
}),
{ transport: useSliding ? 'sliding' : 'classic' }
);
if (manager && (await manager.waitForSidebarCacheHydration())) {
config?.onCachedRoomsLoaded?.();
}
} 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,
});
fetchRoomEventStartupCleanupByClient.get(mx)?.();
slidingSyncRequestCleanupByClient.get(mx)?.();
membershipActionCleanupByClient.get(mx)?.();
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() });
fetchRoomEventStartupCleanupByClient.get(mx)?.();
slidingSyncRequestCleanupByClient.get(mx)?.();
disposeSlidingSync(mx);
disposePresenceSync(mx);
mx.stopClient();
};
export const clearCacheAndReload = async (mx: MatrixClient) => {
log.log('clearCacheAndReload', mx.getUserId());
stopClient(mx);
clearNavToActivePathStore(mx.getSafeUserId());
SlidingSyncSidebarCache.clear(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(),
};
};
const revokeOidcToken = async (
endpoint: string,
token: string,
tokenTypeHint: 'access_token' | 'refresh_token',
clientId: string
): Promise<void> => {
const res = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ token, token_type_hint: tokenTypeHint, client_id: clientId }),
});
if (!res.ok) {
throw new Error(`OIDC token revocation failed (${res.status})`);
}
};
const revokeOidcSession = async (mx: MatrixClient, session: Session): Promise<void> => {
const clientId = session.oidc?.clientId;
if (!clientId) return;
const metadata = await mx.getAuthMetadata();
const endpoint = metadata.revocation_endpoint;
if (!endpoint) return;
const accessToken = mx.getAccessToken() ?? undefined;
const results = await Promise.allSettled([
session.refreshToken
? revokeOidcToken(endpoint, session.refreshToken, 'refresh_token', clientId)
: Promise.resolve(),
accessToken
? revokeOidcToken(endpoint, accessToken, 'access_token', clientId)
: Promise.resolve(),
]);
if (results.some((r) => r.status === 'rejected')) {
debugLog.warn('general', 'OIDC token revocation had failures', { userId: session.userId });
}
};
/**
* 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 {
if (session?.oidc) {
await revokeOidcSession(mx, session);
} else {
await mx.logout();
}
debugLog.info('general', 'Logout successful', { userId: mx.getUserId() });
} catch {
// ignore
}
if (session) {
SlidingSyncSidebarCache.clear(session.userId);
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();
}
await clearMediaCache();
};
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();
await clearMediaCache();
window.location.reload();
};