diff --git a/src/matrix/Session.js b/src/matrix/Session.js index a5270476..17c809bd 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -208,6 +208,8 @@ export class Session { await this._writeSSSSKey(key, log); this._keyBackup.get().flush(log); return key; + } else { + throw new Error("Could not read key backup with the given key"); } }); } @@ -216,7 +218,11 @@ export class Session { // we're going to write the 4S key, and also the backup version. // this way, we can detect when we enter a key for a new backup version // and mark all inbound sessions to be backed up again - const backupVersion = this._keyBackup.get()?.version; + const keyBackup = this._keyBackup.get(); + if (!keyBackup) { + return; + } + const backupVersion = keyBackup.version; const writeTxn = await this._storage.readWriteTxn([ this._storage.storeNames.session, this._storage.storeNames.inboundGroupSessions, @@ -225,8 +231,8 @@ export class Session { const previousBackupVersion = await ssssWriteKey(key, backupVersion, writeTxn); log.set("previousBackupVersion", previousBackupVersion); log.set("backupVersion", backupVersion); - if (typeof previousBackupVersion === "number" && previousBackupVersion !== backupVersion) { - const amountMarked = await this._keyBackup.markAllForBackup(writeTxn); + if (!!previousBackupVersion && previousBackupVersion !== backupVersion) { + const amountMarked = await keyBackup.markAllForBackup(writeTxn); log.set("amountMarkedForBackup", amountMarked); } } catch (err) { @@ -473,7 +479,7 @@ export class Session { if (ssssKey) { // txn will end here as this does a network request await this._createKeyBackup(ssssKey, txn, log); - this._keyBackup.get()?.flush(); + this._keyBackup.get()?.flush(log); } } // restore unfinished operations, like sending out room keys diff --git a/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts b/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts index de4a978b..e20ccc1a 100644 --- a/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts +++ b/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts @@ -39,8 +39,9 @@ const KEYS_PER_REQUEST = 20; export class KeyBackup { public readonly operationInProgress = new ObservableValue, Progress> | undefined>(undefined); - private _cancelled = false; + private _stopped = false; private _needsNewKey = false; + private _hasBackedUpAllKeys = false; private _error?: Error; constructor( @@ -52,9 +53,11 @@ export class KeyBackup { private readonly platform: Platform, ) {} - get cancelled(): boolean { return this._cancelled; } - get needsNewKey(): boolean { return this._needsNewKey; } + get hasStopped(): boolean { return this._stopped; } get error(): Error | undefined { return this._error; } + get version(): string { return this.backupInfo.version; } + get needsNewKey(): boolean { return this._needsNewKey; } + get hasBackedUpAllKeys(): boolean { return this._hasBackedUpAllKeys; } async getRoomKey(roomId: string, sessionId: string, log: ILogItem): Promise { const sessionResponse = await this.hsApi.roomKeyForRoomAndSession(this.backupInfo.version, roomId, sessionId, {log}).response(); @@ -80,18 +83,20 @@ export class KeyBackup { log.set("needsNewKey", this._needsNewKey); return; } - this._cancelled = false; + this._stopped = false; this._error = undefined; + this._hasBackedUpAllKeys = false; const operation = this._runFlushOperation(log); this.operationInProgress.set(operation); try { await operation.result; + this._hasBackedUpAllKeys = true; } catch (err) { + this._stopped = true; if (err.name === "HomeServerError" && err.errcode === "M_WRONG_ROOM_KEYS_VERSION") { log.set("wrong_version", true); this._needsNewKey = true; } else { - this._cancelled = true; // TODO should really also use AbortError in storage if (err.name !== "AbortError" || (err.name === "StorageError" && err.errcode === "AbortError")) { this._error = err; @@ -176,10 +181,6 @@ export class KeyBackup { }); } - get version(): string { - return this.backupInfo.version; - } - dispose() { this.crypto.dispose(); }