You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Make olmlib.encryptMessageForDevice async
This commit is contained in:
@@ -60,7 +60,8 @@ describe("MegolmDecryption", function() {
|
||||
// we stub out the olm encryption bits
|
||||
mockOlmLib = {};
|
||||
mockOlmLib.ensureOlmSessionsForDevices = expect.createSpy();
|
||||
mockOlmLib.encryptMessageForDevice = expect.createSpy();
|
||||
mockOlmLib.encryptMessageForDevice =
|
||||
expect.createSpy().andReturn(Promise.resolve());
|
||||
megolmDecryption.olmlib = mockOlmLib;
|
||||
});
|
||||
|
||||
|
||||
@@ -288,7 +288,7 @@ MegolmEncryption.prototype._shareKeyWithDevices = function(session, devicesByUse
|
||||
return olmlib.ensureOlmSessionsForDevices(
|
||||
this._olmDevice, this._baseApis, devicesByUser,
|
||||
).then(function(devicemap) {
|
||||
let haveTargets = false;
|
||||
const promises = [];
|
||||
|
||||
for (const userId in devicesByUser) {
|
||||
if (!devicesByUser.hasOwnProperty(userId)) {
|
||||
@@ -328,6 +328,13 @@ MegolmEncryption.prototype._shareKeyWithDevices = function(session, devicesByUse
|
||||
ciphertext: {},
|
||||
};
|
||||
|
||||
if (!contentMap[userId]) {
|
||||
contentMap[userId] = {};
|
||||
}
|
||||
|
||||
contentMap[userId][deviceId] = encryptedContent;
|
||||
|
||||
promises.push(
|
||||
olmlib.encryptMessageForDevice(
|
||||
encryptedContent.ciphertext,
|
||||
self._userId,
|
||||
@@ -336,23 +343,20 @@ MegolmEncryption.prototype._shareKeyWithDevices = function(session, devicesByUse
|
||||
userId,
|
||||
deviceInfo,
|
||||
payload,
|
||||
),
|
||||
);
|
||||
|
||||
if (!contentMap[userId]) {
|
||||
contentMap[userId] = {};
|
||||
}
|
||||
|
||||
contentMap[userId][deviceId] = encryptedContent;
|
||||
haveTargets = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!haveTargets) {
|
||||
if (promises.length === 0) {
|
||||
// no devices to send to
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
// TODO: retries
|
||||
return self._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||
});
|
||||
}).then(function() {
|
||||
console.log(`Completed megolm keyshare in ${self._roomId}`);
|
||||
|
||||
@@ -753,7 +757,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
||||
//
|
||||
// ensureOlmSessionsForUsers has already done the logging,
|
||||
// so just skip it.
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(
|
||||
@@ -772,7 +776,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
||||
ciphertext: {},
|
||||
};
|
||||
|
||||
this.olmlib.encryptMessageForDevice(
|
||||
return this.olmlib.encryptMessageForDevice(
|
||||
encryptedContent.ciphertext,
|
||||
this._userId,
|
||||
this._deviceId,
|
||||
@@ -780,8 +784,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
||||
userId,
|
||||
deviceInfo,
|
||||
payload,
|
||||
);
|
||||
|
||||
).then(() => {
|
||||
const contentMap = {
|
||||
[userId]: {
|
||||
[deviceId]: encryptedContent,
|
||||
@@ -790,6 +793,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
||||
|
||||
// TODO: retries
|
||||
return this._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||
});
|
||||
}).done();
|
||||
};
|
||||
|
||||
|
||||
@@ -107,6 +107,8 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
|
||||
ciphertext: {},
|
||||
};
|
||||
|
||||
const promises = [];
|
||||
|
||||
for (let i = 0; i < users.length; ++i) {
|
||||
const userId = users[i];
|
||||
const devices = self._crypto.getStoredDevicesForUser(userId);
|
||||
@@ -123,15 +125,17 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
promises.push(
|
||||
olmlib.encryptMessageForDevice(
|
||||
encryptedContent.ciphertext,
|
||||
self._userId, self._deviceId, self._olmDevice,
|
||||
userId, deviceInfo, payloadFields,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return encryptedContent;
|
||||
return Promise.all(promises).return(encryptedContent);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -48,8 +48,11 @@ module.exports.MEGOLM_ALGORITHM = "m.megolm.v1.aes-sha2";
|
||||
* @param {string} recipientUserId
|
||||
* @param {module:crypto/deviceinfo} recipientDevice
|
||||
* @param {object} payloadFields fields to include in the encrypted payload
|
||||
*
|
||||
* Returns a promise which resolves (to undefined) when the payload
|
||||
* has been encrypted into `resultsObject`
|
||||
*/
|
||||
module.exports.encryptMessageForDevice = function(
|
||||
module.exports.encryptMessageForDevice = async function(
|
||||
resultsObject,
|
||||
ourUserId, ourDeviceId, olmDevice, recipientUserId, recipientDevice,
|
||||
payloadFields,
|
||||
|
||||
Reference in New Issue
Block a user