2021-07-28 16:15:52 +03:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import * as sdk from 'matrix-js-sdk';
|
2021-12-03 15:02:10 +02:00
|
|
|
// import { logger } from 'matrix-js-sdk/lib/logger';
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
import { secret } from './state/auth';
|
|
|
|
import RoomList from './state/RoomList';
|
|
|
|
import RoomsInput from './state/RoomsInput';
|
2021-09-11 16:57:35 +03:00
|
|
|
import Notifications from './state/Notifications';
|
2021-12-11 07:20:34 +02:00
|
|
|
import { initHotkeys } from './event/hotkeys';
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2021-08-03 16:57:09 +03:00
|
|
|
global.Olm = require('@matrix-org/olm');
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2021-12-03 15:02:10 +02:00
|
|
|
// logger.disableAll();
|
2021-11-18 10:02:12 +02:00
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
class InitMatrix extends EventEmitter {
|
|
|
|
async init() {
|
|
|
|
await this.startClient();
|
|
|
|
this.setupSync();
|
|
|
|
this.listenEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
async startClient() {
|
|
|
|
const indexedDBStore = new sdk.IndexedDBStore({
|
|
|
|
indexedDB: global.indexedDB,
|
|
|
|
localStorage: global.localStorage,
|
|
|
|
dbName: 'web-sync-store',
|
|
|
|
});
|
|
|
|
await indexedDBStore.startup();
|
|
|
|
|
|
|
|
this.matrixClient = sdk.createClient({
|
|
|
|
baseUrl: secret.baseUrl,
|
|
|
|
accessToken: secret.accessToken,
|
|
|
|
userId: secret.userId,
|
|
|
|
store: indexedDBStore,
|
|
|
|
sessionStore: new sdk.WebStorageSessionStore(global.localStorage),
|
|
|
|
cryptoStore: new sdk.IndexedDBCryptoStore(global.indexedDB, 'crypto-store'),
|
|
|
|
deviceId: secret.deviceId,
|
2021-11-14 08:01:22 +02:00
|
|
|
timelineSupport: true,
|
2021-07-28 16:15:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await this.matrixClient.initCrypto();
|
|
|
|
|
|
|
|
await this.matrixClient.startClient({
|
|
|
|
lazyLoadMembers: true,
|
|
|
|
});
|
|
|
|
this.matrixClient.setGlobalErrorOnUnknownDevices(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSync() {
|
|
|
|
const sync = {
|
|
|
|
NULL: () => {
|
|
|
|
console.log('NULL state');
|
|
|
|
},
|
|
|
|
SYNCING: () => {
|
|
|
|
console.log('SYNCING state');
|
|
|
|
},
|
|
|
|
PREPARED: (prevState) => {
|
|
|
|
console.log('PREPARED state');
|
|
|
|
console.log('previous state: ', prevState);
|
|
|
|
// TODO: remove global.initMatrix at end
|
|
|
|
global.initMatrix = this;
|
|
|
|
if (prevState === null) {
|
|
|
|
this.roomList = new RoomList(this.matrixClient);
|
|
|
|
this.roomsInput = new RoomsInput(this.matrixClient);
|
2021-09-11 16:57:35 +03:00
|
|
|
this.notifications = new Notifications(this.roomList);
|
2021-12-11 07:20:34 +02:00
|
|
|
initHotkeys();
|
2021-07-28 16:15:52 +03:00
|
|
|
this.emit('init_loading_finished');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RECONNECTING: () => {
|
|
|
|
console.log('RECONNECTING state');
|
|
|
|
},
|
|
|
|
CATCHUP: () => {
|
|
|
|
console.log('CATCHUP state');
|
|
|
|
},
|
|
|
|
ERROR: () => {
|
|
|
|
console.log('ERROR state');
|
|
|
|
},
|
|
|
|
STOPPED: () => {
|
|
|
|
console.log('STOPPED state');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
this.matrixClient.on('sync', (state, prevState) => sync[state](prevState));
|
|
|
|
}
|
|
|
|
|
|
|
|
listenEvents() {
|
|
|
|
this.matrixClient.on('Session.logged_out', () => {
|
2021-08-10 09:40:02 +03:00
|
|
|
this.matrixClient.stopClient();
|
2021-07-28 16:15:52 +03:00
|
|
|
this.matrixClient.clearStores();
|
|
|
|
window.localStorage.clear();
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const initMatrix = new InitMatrix();
|
|
|
|
|
|
|
|
export default initMatrix;
|