From df72e829bf567fb9858f879ac2bf64bf264967d1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 23 Oct 2020 12:22:52 +0200 Subject: [PATCH] setup session backup as part of start method, so we know we're online also don't upload OTKs in case of existing account until catchup sync has happened --- src/matrix/Session.js | 35 +++++++++++++++++----------------- src/matrix/SessionContainer.js | 6 ++++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 80f20d0e..d2d65cf9 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -62,6 +62,7 @@ export class Session { this._getSyncToken = () => this.syncToken; this._olmWorker = olmWorker; this._cryptoDriver = cryptoDriver; + this._sessionBackup = null; if (olm) { this._olmUtil = new olm.Utility(); @@ -211,12 +212,9 @@ export class Session { return this._sessionBackup; } - // called after load - async beforeFirstSync(isNewLogin) { + /** @internal */ + async createIdentity() { if (this._olm) { - if (isNewLogin && this._e2eeAccount) { - throw new Error("there should not be an e2ee account already on a fresh login"); - } if (!this._e2eeAccount) { this._e2eeAccount = await E2EEAccount.create({ hsApi: this._hsApi, @@ -231,18 +229,6 @@ export class Session { } await this._e2eeAccount.generateOTKsIfNeeded(this._storage); await this._e2eeAccount.uploadKeys(this._storage); - await this._deviceMessageHandler.decryptPending(this.rooms); - - const txn = this._storage.readTxn([ - this._storage.storeNames.session, - this._storage.storeNames.accountData, - ]); - // try set up session backup if we stored the ssss key - const ssssKey = await ssssReadKey(txn); - if (ssssKey) { - // txn will end here as this does a network request - await this._createSessionBackup(ssssKey, txn); - } } } @@ -299,7 +285,20 @@ export class Session { // TODO: what can we do if this throws? await txn.complete(); } - + // enable session backup, this requests the latest backup version + if (!this._sessionBackup) { + const txn = this._storage.readTxn([ + this._storage.storeNames.session, + this._storage.storeNames.accountData, + ]); + // try set up session backup if we stored the ssss key + const ssssKey = await ssssReadKey(txn); + if (ssssKey) { + // txn will end here as this does a network request + await this._createSessionBackup(ssssKey, txn); + } + } + // restore unfinished operations, like sending out room keys const opsTxn = this._storage.readWriteTxn([ this._storage.storeNames.operations ]); diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index a9a6dea6..d59d6c49 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -179,8 +179,10 @@ export class SessionContainer { mediaRepository: new MediaRepository(sessionInfo.homeServer) }); await this._session.load(); - this._status.set(LoadStatus.SessionSetup); - await this._session.beforeFirstSync(isNewLogin); + if (isNewLogin) { + this._status.set(LoadStatus.SessionSetup); + await this._session.createIdentity(); + } this._sync = new Sync({hsApi: this._requestScheduler.hsApi, storage: this._storage, session: this._session}); // notify sync and session when back online