fixes and cleanup

This commit is contained in:
Bruno Windels 2022-01-31 16:26:14 +01:00
parent 06a1421e97
commit 830786b2fd
2 changed files with 20 additions and 13 deletions

View File

@ -208,6 +208,8 @@ export class Session {
await this._writeSSSSKey(key, log); await this._writeSSSSKey(key, log);
this._keyBackup.get().flush(log); this._keyBackup.get().flush(log);
return key; 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. // 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 // 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 // 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([ const writeTxn = await this._storage.readWriteTxn([
this._storage.storeNames.session, this._storage.storeNames.session,
this._storage.storeNames.inboundGroupSessions, this._storage.storeNames.inboundGroupSessions,
@ -225,8 +231,8 @@ export class Session {
const previousBackupVersion = await ssssWriteKey(key, backupVersion, writeTxn); const previousBackupVersion = await ssssWriteKey(key, backupVersion, writeTxn);
log.set("previousBackupVersion", previousBackupVersion); log.set("previousBackupVersion", previousBackupVersion);
log.set("backupVersion", backupVersion); log.set("backupVersion", backupVersion);
if (typeof previousBackupVersion === "number" && previousBackupVersion !== backupVersion) { if (!!previousBackupVersion && previousBackupVersion !== backupVersion) {
const amountMarked = await this._keyBackup.markAllForBackup(writeTxn); const amountMarked = await keyBackup.markAllForBackup(writeTxn);
log.set("amountMarkedForBackup", amountMarked); log.set("amountMarkedForBackup", amountMarked);
} }
} catch (err) { } catch (err) {
@ -473,7 +479,7 @@ export class Session {
if (ssssKey) { if (ssssKey) {
// txn will end here as this does a network request // txn will end here as this does a network request
await this._createKeyBackup(ssssKey, txn, log); await this._createKeyBackup(ssssKey, txn, log);
this._keyBackup.get()?.flush(); this._keyBackup.get()?.flush(log);
} }
} }
// restore unfinished operations, like sending out room keys // restore unfinished operations, like sending out room keys

View File

@ -39,8 +39,9 @@ const KEYS_PER_REQUEST = 20;
export class KeyBackup { export class KeyBackup {
public readonly operationInProgress = new ObservableValue<AbortableOperation<Promise<void>, Progress> | undefined>(undefined); public readonly operationInProgress = new ObservableValue<AbortableOperation<Promise<void>, Progress> | undefined>(undefined);
private _cancelled = false; private _stopped = false;
private _needsNewKey = false; private _needsNewKey = false;
private _hasBackedUpAllKeys = false;
private _error?: Error; private _error?: Error;
constructor( constructor(
@ -52,9 +53,11 @@ export class KeyBackup {
private readonly platform: Platform, private readonly platform: Platform,
) {} ) {}
get cancelled(): boolean { return this._cancelled; } get hasStopped(): boolean { return this._stopped; }
get needsNewKey(): boolean { return this._needsNewKey; }
get error(): Error | undefined { return this._error; } 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<IncomingRoomKey | undefined> { async getRoomKey(roomId: string, sessionId: string, log: ILogItem): Promise<IncomingRoomKey | undefined> {
const sessionResponse = await this.hsApi.roomKeyForRoomAndSession(this.backupInfo.version, roomId, sessionId, {log}).response(); 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); log.set("needsNewKey", this._needsNewKey);
return; return;
} }
this._cancelled = false; this._stopped = false;
this._error = undefined; this._error = undefined;
this._hasBackedUpAllKeys = false;
const operation = this._runFlushOperation(log); const operation = this._runFlushOperation(log);
this.operationInProgress.set(operation); this.operationInProgress.set(operation);
try { try {
await operation.result; await operation.result;
this._hasBackedUpAllKeys = true;
} catch (err) { } catch (err) {
this._stopped = true;
if (err.name === "HomeServerError" && err.errcode === "M_WRONG_ROOM_KEYS_VERSION") { if (err.name === "HomeServerError" && err.errcode === "M_WRONG_ROOM_KEYS_VERSION") {
log.set("wrong_version", true); log.set("wrong_version", true);
this._needsNewKey = true; this._needsNewKey = true;
} else { } else {
this._cancelled = true;
// TODO should really also use AbortError in storage // TODO should really also use AbortError in storage
if (err.name !== "AbortError" || (err.name === "StorageError" && err.errcode === "AbortError")) { if (err.name !== "AbortError" || (err.name === "StorageError" && err.errcode === "AbortError")) {
this._error = err; this._error = err;
@ -176,10 +181,6 @@ export class KeyBackup {
}); });
} }
get version(): string {
return this.backupInfo.version;
}
dispose() { dispose() {
this.crypto.dispose(); this.crypto.dispose();
} }