From 3a0f27fa7eaba6d21899e9770e0ae940e84c4954 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 5 Jul 2019 13:50:11 -0600 Subject: [PATCH] Add a function to flag keys for backup without scheduling a backup For https://github.com/vector-im/riot-web/issues/10263 Starting/scheduling the backup won't help us because the token would be invalid from a server perspective. Instead, we should update what needs to be done and return a count. --- src/client.js | 13 +++++++++++++ src/crypto/index.js | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/client.js b/src/client.js index 9ef9395d8a0..8975ee46773 100644 --- a/src/client.js +++ b/src/client.js @@ -1208,6 +1208,19 @@ MatrixClient.prototype.scheduleAllGroupSessionsForBackup = async function() { await this._crypto.scheduleAllGroupSessionsForBackup(); }; +/** + * Marks all group sessions as needing to be backed up without scheduling + * them to upload in the background. + * @returns {Promise} Resolves to the number of sessions requiring a backup. + */ +MatrixClient.prototype.flagAllGroupSessionsForBackup = function() { + if (this._crypto === null) { + throw new Error("End-to-end encryption disabled"); + } + + return this._crypto.flagAllGroupSessionsForBackup(); +}; + MatrixClient.prototype.isValidRecoveryKey = function(recoveryKey) { try { decodeRecoveryKey(recoveryKey); diff --git a/src/crypto/index.js b/src/crypto/index.js index a49a375aa53..903e770e6bf 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1288,6 +1288,18 @@ Crypto.prototype.backupGroupSession = async function( * upload in the background as soon as possible. */ Crypto.prototype.scheduleAllGroupSessionsForBackup = async function() { + await this.flagAllGroupSessionsForBackup(); + + // Schedule keys to upload in the background as soon as possible. + this.scheduleKeyBackupSend(0 /* maxDelay */); +}; + +/** + * Marks all group sessions as needing to be backed up without scheduling + * them to upload in the background. + * @returns {Promise} Resolves to the number of sessions requiring a backup. + */ +Crypto.prototype.flagAllGroupSessionsForBackup = async function() { await this._cryptoStore.doTxn( 'readwrite', [ @@ -1305,9 +1317,7 @@ Crypto.prototype.scheduleAllGroupSessionsForBackup = async function() { const remaining = await this._cryptoStore.countSessionsNeedingBackup(); this.emit("crypto.keyBackupSessionsRemaining", remaining); - - // Schedule keys to upload in the background as soon as possible. - this.scheduleKeyBackupSend(0 /* maxDelay */); + return remaining; }; /* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307