2024-07-22 16:17:19 +05:30
|
|
|
import { createClient, MatrixClient, IndexedDBStore, IndexedDBCryptoStore } from 'matrix-js-sdk';
|
|
|
|
|
|
2025-08-29 15:04:52 +05:30
|
|
|
import { cryptoCallbacks } from './secretStorageKeys';
|
2025-06-10 10:14:17 +05:30
|
|
|
import { clearNavToActivePathStore } from '../app/state/navToActivePath';
|
2026-02-14 11:41:36 +05:30
|
|
|
import { pushSessionToSW } from '../sw-session';
|
2024-07-22 16:17:19 +05:30
|
|
|
|
|
|
|
|
type Session = {
|
|
|
|
|
baseUrl: string;
|
|
|
|
|
accessToken: string;
|
|
|
|
|
userId: string;
|
|
|
|
|
deviceId: string;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-21 23:50:24 -04:00
|
|
|
// Thrown when the local IndexedDB has a higher schema version than this SDK expects.
|
|
|
|
|
// This happens after a downgrade (e.g. matrix-js-sdk was briefly upgraded and then reverted).
|
|
|
|
|
export const IDB_VERSION_CONFLICT = 'IDB_VERSION_CONFLICT';
|
|
|
|
|
|
2024-07-22 16:17:19 +05:30
|
|
|
export const initClient = async (session: Session): Promise<MatrixClient> => {
|
|
|
|
|
const indexedDBStore = new IndexedDBStore({
|
|
|
|
|
indexedDB: global.indexedDB,
|
|
|
|
|
localStorage: global.localStorage,
|
|
|
|
|
dbName: 'web-sync-store',
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-10 16:49:47 +11:00
|
|
|
const legacyCryptoStore = new IndexedDBCryptoStore(global.indexedDB, 'crypto-store');
|
|
|
|
|
|
2024-07-22 16:17:19 +05:30
|
|
|
const mx = createClient({
|
|
|
|
|
baseUrl: session.baseUrl,
|
|
|
|
|
accessToken: session.accessToken,
|
|
|
|
|
userId: session.userId,
|
|
|
|
|
store: indexedDBStore,
|
2025-02-10 16:49:47 +11:00
|
|
|
cryptoStore: legacyCryptoStore,
|
2024-07-22 16:17:19 +05:30
|
|
|
deviceId: session.deviceId,
|
|
|
|
|
timelineSupport: true,
|
|
|
|
|
cryptoCallbacks: cryptoCallbacks as any,
|
|
|
|
|
verificationMethods: ['m.sas.v1'],
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-21 23:50:24 -04:00
|
|
|
try {
|
|
|
|
|
await indexedDBStore.startup();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// IDB VersionError = local DB was written by a newer SDK version (schema downgrade).
|
|
|
|
|
if (e instanceof DOMException && e.name === 'VersionError') {
|
|
|
|
|
throw new Error(IDB_VERSION_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2025-02-10 16:49:47 +11:00
|
|
|
await mx.initRustCrypto();
|
2024-07-22 16:17:19 +05:30
|
|
|
|
|
|
|
|
mx.setMaxListeners(50);
|
|
|
|
|
|
|
|
|
|
return mx;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const startClient = async (mx: MatrixClient) => {
|
|
|
|
|
await mx.startClient({
|
|
|
|
|
lazyLoadMembers: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const clearCacheAndReload = async (mx: MatrixClient) => {
|
|
|
|
|
mx.stopClient();
|
2025-06-10 10:14:17 +05:30
|
|
|
clearNavToActivePathStore(mx.getSafeUserId());
|
2024-07-22 16:17:19 +05:30
|
|
|
await mx.store.deleteAllData();
|
|
|
|
|
window.location.reload();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const logoutClient = async (mx: MatrixClient) => {
|
2026-02-14 11:41:36 +05:30
|
|
|
pushSessionToSW();
|
2024-07-22 16:17:19 +05:30
|
|
|
mx.stopClient();
|
|
|
|
|
try {
|
|
|
|
|
await mx.logout();
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore if failed to logout
|
|
|
|
|
}
|
|
|
|
|
await mx.clearStores();
|
|
|
|
|
window.localStorage.clear();
|
|
|
|
|
window.location.reload();
|
|
|
|
|
};
|
2025-02-10 16:49:47 +11:00
|
|
|
|
|
|
|
|
export const clearLoginData = async () => {
|
|
|
|
|
const dbs = await window.indexedDB.databases();
|
|
|
|
|
|
|
|
|
|
dbs.forEach((idbInfo) => {
|
|
|
|
|
const { name } = idbInfo;
|
|
|
|
|
if (name) {
|
|
|
|
|
window.indexedDB.deleteDatabase(name);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-22 00:19:11 -04:00
|
|
|
// Unregister service workers so stale caches don't interfere after a reset
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
const regs = await navigator.serviceWorker.getRegistrations();
|
|
|
|
|
await Promise.all(regs.map((r) => r.unregister()));
|
|
|
|
|
const cacheNames = await caches.keys();
|
|
|
|
|
await Promise.all(cacheNames.map((c) => caches.delete(c)));
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-10 16:49:47 +11:00
|
|
|
window.localStorage.clear();
|
|
|
|
|
window.location.reload();
|
|
|
|
|
};
|