From 17ec7daf23dc0820bc12096f1a6fae56a6330d81 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 6 Jun 2016 14:31:36 +0100 Subject: [PATCH] MatrixClient: refactor uploadKeys rewrite uploadKeys to require less looping and not to use deferreds. --- lib/client.js | 80 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/lib/client.js b/lib/client.js index ccef2626c..5b8b71f75 100644 --- a/lib/client.js +++ b/lib/client.js @@ -308,22 +308,54 @@ MatrixClient.prototype.isCryptoEnabled = function() { * @return {object} A promise that will resolve when the keys are uploaded. */ MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) { - if (!CRYPTO_ENABLED || this.sessionStore === null) { + var self = this; + return _doKeyUpload(this).then(function(res) { + var keyCount = res.one_time_key_counts.curve25519 || 0; + + var pickled = self.sessionStore.getEndToEndAccount(); + + var numberToGenerate; + var account = new Olm.Account(); + try { + account.unpickle(self.accountKey, pickled); + + var maxOneTimeKeys = account.max_number_of_one_time_keys(); + var keyLimit = Math.floor(maxOneTimeKeys / 2); + numberToGenerate = Math.max(keyLimit - keyCount, 0); + if (maxKeys !== undefined) { + numberToGenerate = Math.min(numberToGenerate, maxKeys); + } + if (numberToGenerate > 0) { + account.generate_one_time_keys(numberToGenerate); + } + pickled = account.pickle(self.accountKey); + self.sessionStore.storeEndToEndAccount(pickled); + } finally { + account.free(); + } + if (numberToGenerate > 0) { + return _doKeyUpload(self); + } else { + return; + } + }); +}; + +// build the upload request, and return a promise which resolves to the response +function _doKeyUpload(client) { + if (!CRYPTO_ENABLED || client.sessionStore === null) { return q.reject(new Error("End-to-end encryption disabled")); } - var first_time = deferred === undefined; - deferred = deferred || q.defer(); - var path = "/keys/upload/" + this.deviceId; - var pickled = this.sessionStore.getEndToEndAccount(); + + var pickled = client.sessionStore.getEndToEndAccount(); if (!pickled) { return q.reject(new Error("End-to-end account not found")); } var account = new Olm.Account(); var oneTimeKeys; try { - account.unpickle(this.accountKey, pickled); + account.unpickle(client.accountKey, pickled); oneTimeKeys = JSON.parse(account.one_time_keys()); - var maxOneTimeKeys = account.max_number_of_one_time_keys(); } finally { account.free(); } @@ -335,42 +367,26 @@ MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) { } } var content = { - device_keys: this.deviceKeys, + device_keys: client.deviceKeys, one_time_keys: oneTimeJson }; - var self = this; - this._http.authedRequestWithPrefix( + var path = "/keys/upload/" + client.deviceId; + return client._http.authedRequestWithPrefix( undefined, "POST", path, undefined, content, httpApi.PREFIX_UNSTABLE ).then(function(res) { - var keyLimit = Math.floor(maxOneTimeKeys / 2); - var keyCount = res.one_time_key_counts.curve25519 || 0; - var generateKeys = (keyCount < keyLimit); - var pickled = self.sessionStore.getEndToEndAccount(); - var account = new Olm.Account(); try { - account.unpickle(self.accountKey, pickled); + account.unpickle(client.accountKey, pickled); account.mark_keys_as_published(); - if (generateKeys) { - var numberToGenerate = keyLimit - keyCount; - if (maxKeys) { - numberToGenerate = Math.min(numberToGenerate, maxKeys); - } - account.generate_one_time_keys(numberToGenerate); - } - pickled = account.pickle(self.accountKey); - self.sessionStore.storeEndToEndAccount(pickled); + pickled = account.pickle(client.accountKey); + client.sessionStore.storeEndToEndAccount(pickled); } finally { account.free(); } - if (generateKeys && first_time) { - self.uploadKeys(maxKeys, deferred); - } else { - deferred.resolve(); - } + return res; }); - return deferred.promise; -}; +} + /**