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
This commit is contained in:
Bruno Windels 2020-10-23 12:22:52 +02:00
parent 08645c8bc0
commit df72e829bf
2 changed files with 21 additions and 20 deletions

View file

@ -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
]);

View file

@ -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