You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-29 16:43:09 +03:00
MatrixClient: refactor uploadKeys
rewrite uploadKeys to require less looping and not to use deferreds.
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user