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
|
// we stub out the olm encryption bits
|
||||||
mockOlmLib = {};
|
mockOlmLib = {};
|
||||||
mockOlmLib.ensureOlmSessionsForDevices = expect.createSpy();
|
mockOlmLib.ensureOlmSessionsForDevices = expect.createSpy();
|
||||||
mockOlmLib.encryptMessageForDevice = expect.createSpy();
|
mockOlmLib.encryptMessageForDevice =
|
||||||
|
expect.createSpy().andReturn(Promise.resolve());
|
||||||
megolmDecryption.olmlib = mockOlmLib;
|
megolmDecryption.olmlib = mockOlmLib;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -288,7 +288,7 @@ MegolmEncryption.prototype._shareKeyWithDevices = function(session, devicesByUse
|
|||||||
return olmlib.ensureOlmSessionsForDevices(
|
return olmlib.ensureOlmSessionsForDevices(
|
||||||
this._olmDevice, this._baseApis, devicesByUser,
|
this._olmDevice, this._baseApis, devicesByUser,
|
||||||
).then(function(devicemap) {
|
).then(function(devicemap) {
|
||||||
let haveTargets = false;
|
const promises = [];
|
||||||
|
|
||||||
for (const userId in devicesByUser) {
|
for (const userId in devicesByUser) {
|
||||||
if (!devicesByUser.hasOwnProperty(userId)) {
|
if (!devicesByUser.hasOwnProperty(userId)) {
|
||||||
@@ -328,31 +328,35 @@ MegolmEncryption.prototype._shareKeyWithDevices = function(session, devicesByUse
|
|||||||
ciphertext: {},
|
ciphertext: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
olmlib.encryptMessageForDevice(
|
|
||||||
encryptedContent.ciphertext,
|
|
||||||
self._userId,
|
|
||||||
self._deviceId,
|
|
||||||
self._olmDevice,
|
|
||||||
userId,
|
|
||||||
deviceInfo,
|
|
||||||
payload,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!contentMap[userId]) {
|
if (!contentMap[userId]) {
|
||||||
contentMap[userId] = {};
|
contentMap[userId] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
contentMap[userId][deviceId] = encryptedContent;
|
contentMap[userId][deviceId] = encryptedContent;
|
||||||
haveTargets = true;
|
|
||||||
|
promises.push(
|
||||||
|
olmlib.encryptMessageForDevice(
|
||||||
|
encryptedContent.ciphertext,
|
||||||
|
self._userId,
|
||||||
|
self._deviceId,
|
||||||
|
self._olmDevice,
|
||||||
|
userId,
|
||||||
|
deviceInfo,
|
||||||
|
payload,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!haveTargets) {
|
if (promises.length === 0) {
|
||||||
|
// no devices to send to
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: retries
|
return Promise.all(promises).then(() => {
|
||||||
return self._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
// TODO: retries
|
||||||
|
return self._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||||
|
});
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
console.log(`Completed megolm keyshare in ${self._roomId}`);
|
console.log(`Completed megolm keyshare in ${self._roomId}`);
|
||||||
|
|
||||||
@@ -753,7 +757,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
|||||||
//
|
//
|
||||||
// ensureOlmSessionsForUsers has already done the logging,
|
// ensureOlmSessionsForUsers has already done the logging,
|
||||||
// so just skip it.
|
// so just skip it.
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
@@ -772,7 +776,7 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
|||||||
ciphertext: {},
|
ciphertext: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
this.olmlib.encryptMessageForDevice(
|
return this.olmlib.encryptMessageForDevice(
|
||||||
encryptedContent.ciphertext,
|
encryptedContent.ciphertext,
|
||||||
this._userId,
|
this._userId,
|
||||||
this._deviceId,
|
this._deviceId,
|
||||||
@@ -780,16 +784,16 @@ MegolmDecryption.prototype.shareKeysWithDevice = function(keyRequest) {
|
|||||||
userId,
|
userId,
|
||||||
deviceInfo,
|
deviceInfo,
|
||||||
payload,
|
payload,
|
||||||
);
|
).then(() => {
|
||||||
|
const contentMap = {
|
||||||
|
[userId]: {
|
||||||
|
[deviceId]: encryptedContent,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const contentMap = {
|
// TODO: retries
|
||||||
[userId]: {
|
return this._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||||
[deviceId]: encryptedContent,
|
});
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: retries
|
|
||||||
return this._baseApis.sendToDevice("m.room.encrypted", contentMap);
|
|
||||||
}).done();
|
}).done();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -107,6 +107,8 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
|
|||||||
ciphertext: {},
|
ciphertext: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
for (let i = 0; i < users.length; ++i) {
|
for (let i = 0; i < users.length; ++i) {
|
||||||
const userId = users[i];
|
const userId = users[i];
|
||||||
const devices = self._crypto.getStoredDevicesForUser(userId);
|
const devices = self._crypto.getStoredDevicesForUser(userId);
|
||||||
@@ -123,15 +125,17 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
olmlib.encryptMessageForDevice(
|
promises.push(
|
||||||
encryptedContent.ciphertext,
|
olmlib.encryptMessageForDevice(
|
||||||
self._userId, self._deviceId, self._olmDevice,
|
encryptedContent.ciphertext,
|
||||||
userId, deviceInfo, payloadFields,
|
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 {string} recipientUserId
|
||||||
* @param {module:crypto/deviceinfo} recipientDevice
|
* @param {module:crypto/deviceinfo} recipientDevice
|
||||||
* @param {object} payloadFields fields to include in the encrypted payload
|
* @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,
|
resultsObject,
|
||||||
ourUserId, ourDeviceId, olmDevice, recipientUserId, recipientDevice,
|
ourUserId, ourDeviceId, olmDevice, recipientUserId, recipientDevice,
|
||||||
payloadFields,
|
payloadFields,
|
||||||
|
|||||||
Reference in New Issue
Block a user