Files
cinny/src/client/initMatrix.ts
T

82 lines
2.0 KiB
TypeScript
Raw Normal View History

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';
import { clearNavToActivePathStore } from '../app/state/navToActivePath';
import { pushSessionToSW } from '../sw-session';
2024-07-22 16:17:19 +05:30
type Session = {
baseUrl: string;
accessToken: string;
userId: string;
deviceId: string;
};
export const initClient = async (session: Session): Promise<MatrixClient> => {
const indexedDBStore = new IndexedDBStore({
indexedDB: global.indexedDB,
localStorage: global.localStorage,
dbName: 'web-sync-store',
});
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,
cryptoStore: legacyCryptoStore,
2024-07-22 16:17:19 +05:30
deviceId: session.deviceId,
timelineSupport: true,
cryptoCallbacks: cryptoCallbacks as any,
verificationMethods: ['m.sas.v1'],
});
2024-09-07 21:45:55 +08:00
await indexedDBStore.startup();
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();
clearNavToActivePathStore(mx.getSafeUserId());
2024-07-22 16:17:19 +05:30
await mx.store.deleteAllData();
window.location.reload();
};
export const logoutClient = async (mx: MatrixClient) => {
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();
};
export const clearLoginData = async () => {
const dbs = await window.indexedDB.databases();
dbs.forEach((idbInfo) => {
const { name } = idbInfo;
if (name) {
window.indexedDB.deleteDatabase(name);
}
});
window.localStorage.clear();
window.location.reload();
};