mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 03:46:25 -08:00
fix(crypto): re-check key backup after the first sync
This commit is contained in:
2 files changed
+84
-1
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
import type { ClientEvent, MatrixClient } from '$types/matrix-sdk';
|
||||
import { SyncState } from '$types/matrix-sdk';
|
||||
import type { Session } from '$state/sessions';
|
||||
import type * as PlatformModule from '$utils/platform';
|
||||
import { ACTIVE_SESSION_KEY, MATRIX_SESSIONS_KEY } from '$state/sessions';
|
||||
@@ -17,6 +18,7 @@ import {
|
||||
installSlidingSyncRequestPatch,
|
||||
newSlidingSyncConnId,
|
||||
ownsActiveMediaSession,
|
||||
recheckKeyBackupAfterInitialSync,
|
||||
resolvePollTimeoutMs,
|
||||
supportsSlidingSync,
|
||||
} from './initMatrix';
|
||||
@@ -279,3 +281,62 @@ describe('resolvePollTimeoutMs', () => {
|
||||
expect(resolvePollTimeoutMs(5000)).toBe(5000);
|
||||
});
|
||||
});
|
||||
|
||||
const makeKeyBackupMx = (syncState: SyncState | null) => {
|
||||
const checkKeyBackupAndEnable = vi.fn<() => Promise<null>>().mockResolvedValue(null);
|
||||
const listeners = new Set<(state: SyncState) => void>();
|
||||
const mx = {
|
||||
getSyncState: () => syncState,
|
||||
getCrypto: () => ({ checkKeyBackupAndEnable }),
|
||||
on: (_event: ClientEvent, cb: (state: SyncState) => void) => listeners.add(cb),
|
||||
removeListener: (_event: ClientEvent, cb: (state: SyncState) => void) => listeners.delete(cb),
|
||||
} as unknown as MatrixClient;
|
||||
|
||||
const emitSync = (state: SyncState) => {
|
||||
listeners.forEach((cb) => cb(state));
|
||||
};
|
||||
|
||||
return { mx, checkKeyBackupAndEnable, emitSync, listeners };
|
||||
};
|
||||
|
||||
describe('recheckKeyBackupAfterInitialSync', () => {
|
||||
it('re-checks key backup once the first sync completes', () => {
|
||||
const { mx, checkKeyBackupAndEnable, emitSync } = makeKeyBackupMx(null);
|
||||
|
||||
recheckKeyBackupAfterInitialSync(mx);
|
||||
expect(checkKeyBackupAndEnable).not.toHaveBeenCalled();
|
||||
|
||||
emitSync(SyncState.Prepared);
|
||||
|
||||
expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('re-checks immediately when the client has already synced', () => {
|
||||
const { mx, checkKeyBackupAndEnable } = makeKeyBackupMx(SyncState.Syncing);
|
||||
|
||||
recheckKeyBackupAfterInitialSync(mx);
|
||||
|
||||
expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('re-checks only once and stops listening', () => {
|
||||
const { mx, checkKeyBackupAndEnable, emitSync, listeners } = makeKeyBackupMx(null);
|
||||
|
||||
recheckKeyBackupAfterInitialSync(mx);
|
||||
emitSync(SyncState.Prepared);
|
||||
emitSync(SyncState.Syncing);
|
||||
|
||||
expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1);
|
||||
expect(listeners.size).toBe(0);
|
||||
});
|
||||
|
||||
it('waits for a usable sync state', () => {
|
||||
const { mx, checkKeyBackupAndEnable, emitSync } = makeKeyBackupMx(null);
|
||||
|
||||
recheckKeyBackupAfterInitialSync(mx);
|
||||
emitSync(SyncState.Reconnecting);
|
||||
emitSync(SyncState.Error);
|
||||
|
||||
expect(checkKeyBackupAndEnable).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -119,6 +119,27 @@ const startPresenceAfterInitialSync = (
|
||||
return cleanup;
|
||||
};
|
||||
|
||||
// The SDK's only check runs inside `initRustCrypto`, before the client starts, and latches off.
|
||||
export const recheckKeyBackupAfterInitialSync = (mx: MatrixClient): void => {
|
||||
const recheck = () => {
|
||||
mx.removeListener(ClientEvent.Sync, onSync);
|
||||
const crypto = mx.getCrypto();
|
||||
if (!crypto) return;
|
||||
crypto.checkKeyBackupAndEnable().catch((error: unknown) => {
|
||||
debugLog.warn('sync', 'Failed to re-check key backup after initial sync', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const onSync = (state: SyncState) => {
|
||||
if (isInitialSyncReady(state)) recheck();
|
||||
};
|
||||
|
||||
if (isInitialSyncReady(mx.getSyncState())) recheck();
|
||||
else mx.on(ClientEvent.Sync, onSync);
|
||||
};
|
||||
|
||||
type StartupPhase = 'sync_store' | 'rust_crypto' | 'client_init' | 'client_start';
|
||||
|
||||
const measureStartupPhase = async <T>(
|
||||
@@ -566,6 +587,7 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig):
|
||||
{ transport: useSliding ? 'sliding' : 'classic' }
|
||||
);
|
||||
if (!useSliding) installSyncStorePersistence(mx);
|
||||
recheckKeyBackupAfterInitialSync(mx);
|
||||
if (manager && (await manager.waitForSidebarCacheHydration())) {
|
||||
config?.onCachedRoomsLoaded?.();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user