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
Rewrite olmlib.ensureOlmSessionsForDevices as async
This is non-functional. It just looks a lot prettier.
This commit is contained in:
@@ -121,7 +121,7 @@ module.exports.encryptMessageForDevice = async function(
|
|||||||
* an Object mapping from userId to deviceId to
|
* an Object mapping from userId to deviceId to
|
||||||
* {@link module:crypto~OlmSessionResult}
|
* {@link module:crypto~OlmSessionResult}
|
||||||
*/
|
*/
|
||||||
module.exports.ensureOlmSessionsForDevices = function(
|
module.exports.ensureOlmSessionsForDevices = async function(
|
||||||
olmDevice, baseApis, devicesByUser,
|
olmDevice, baseApis, devicesByUser,
|
||||||
) {
|
) {
|
||||||
const devicesWithoutSession = [
|
const devicesWithoutSession = [
|
||||||
@@ -151,7 +151,7 @@ module.exports.ensureOlmSessionsForDevices = function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (devicesWithoutSession.length === 0) {
|
if (devicesWithoutSession.length === 0) {
|
||||||
return Promise.resolve(result);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this has a race condition - if we try to send another message
|
// TODO: this has a race condition - if we try to send another message
|
||||||
@@ -161,52 +161,54 @@ module.exports.ensureOlmSessionsForDevices = function(
|
|||||||
// That should eventually resolve itself, but it's poor form.
|
// That should eventually resolve itself, but it's poor form.
|
||||||
|
|
||||||
const oneTimeKeyAlgorithm = "signed_curve25519";
|
const oneTimeKeyAlgorithm = "signed_curve25519";
|
||||||
return baseApis.claimOneTimeKeys(
|
const res = await baseApis.claimOneTimeKeys(
|
||||||
devicesWithoutSession, oneTimeKeyAlgorithm,
|
devicesWithoutSession, oneTimeKeyAlgorithm,
|
||||||
).then(function(res) {
|
);
|
||||||
const otk_res = res.one_time_keys || {};
|
|
||||||
const promises = [];
|
const otk_res = res.one_time_keys || {};
|
||||||
for (const userId in devicesByUser) {
|
const promises = [];
|
||||||
if (!devicesByUser.hasOwnProperty(userId)) {
|
for (const userId in devicesByUser) {
|
||||||
|
if (!devicesByUser.hasOwnProperty(userId)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const userRes = otk_res[userId] || {};
|
||||||
|
const devices = devicesByUser[userId];
|
||||||
|
for (let j = 0; j < devices.length; j++) {
|
||||||
|
const deviceInfo = devices[j];
|
||||||
|
const deviceId = deviceInfo.deviceId;
|
||||||
|
if (result[userId][deviceId].sessionId) {
|
||||||
|
// we already have a result for this device
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const userRes = otk_res[userId] || {};
|
|
||||||
const devices = devicesByUser[userId];
|
|
||||||
for (let j = 0; j < devices.length; j++) {
|
|
||||||
const deviceInfo = devices[j];
|
|
||||||
const deviceId = deviceInfo.deviceId;
|
|
||||||
if (result[userId][deviceId].sessionId) {
|
|
||||||
// we already have a result for this device
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const deviceRes = userRes[deviceId] || {};
|
const deviceRes = userRes[deviceId] || {};
|
||||||
let oneTimeKey = null;
|
let oneTimeKey = null;
|
||||||
for (const keyId in deviceRes) {
|
for (const keyId in deviceRes) {
|
||||||
if (keyId.indexOf(oneTimeKeyAlgorithm + ":") === 0) {
|
if (keyId.indexOf(oneTimeKeyAlgorithm + ":") === 0) {
|
||||||
oneTimeKey = deviceRes[keyId];
|
oneTimeKey = deviceRes[keyId];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!oneTimeKey) {
|
|
||||||
console.warn(
|
|
||||||
"No one-time keys (alg=" + oneTimeKeyAlgorithm +
|
|
||||||
") for device " + userId + ":" + deviceId,
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
promises.push(
|
|
||||||
_verifyKeyAndStartSession(
|
|
||||||
olmDevice, oneTimeKey, userId, deviceInfo,
|
|
||||||
).then((sid) => {
|
|
||||||
result[userId][deviceId].sessionId = sid;
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!oneTimeKey) {
|
||||||
|
console.warn(
|
||||||
|
"No one-time keys (alg=" + oneTimeKeyAlgorithm +
|
||||||
|
") for device " + userId + ":" + deviceId,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
promises.push(
|
||||||
|
_verifyKeyAndStartSession(
|
||||||
|
olmDevice, oneTimeKey, userId, deviceInfo,
|
||||||
|
).then((sid) => {
|
||||||
|
result[userId][deviceId].sessionId = sid;
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return Promise.all(promises).return(result);
|
}
|
||||||
});
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function _verifyKeyAndStartSession(olmDevice, oneTimeKey, userId, deviceInfo) {
|
async function _verifyKeyAndStartSession(olmDevice, oneTimeKey, userId, deviceInfo) {
|
||||||
|
|||||||
Reference in New Issue
Block a user