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
don't always do second phase of olm creation
don't need to do the shorter timeout when doing preparation to encrypt, and skip the second phase if the first phase already took longer than a normal otk claim
This commit is contained in:
@@ -185,12 +185,14 @@ utils.inherits(MegolmEncryption, EncryptionAlgorithm);
|
|||||||
*
|
*
|
||||||
* @param {Object} devicesInRoom The devices in this room, indexed by user ID
|
* @param {Object} devicesInRoom The devices in this room, indexed by user ID
|
||||||
* @param {Object} blocked The devices that are blocked, indexed by user ID
|
* @param {Object} blocked The devices that are blocked, indexed by user ID
|
||||||
|
* @param {boolean} [singleOlmCreationPhase] Only perform one round of olm
|
||||||
|
* session creation
|
||||||
*
|
*
|
||||||
* @return {Promise} Promise which resolves to the
|
* @return {Promise} Promise which resolves to the
|
||||||
* OutboundSessionInfo when setup is complete.
|
* OutboundSessionInfo when setup is complete.
|
||||||
*/
|
*/
|
||||||
MegolmEncryption.prototype._ensureOutboundSession = async function(
|
MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||||
devicesInRoom, blocked,
|
devicesInRoom, blocked, singleOlmCreationPhase,
|
||||||
) {
|
) {
|
||||||
let session;
|
let session;
|
||||||
|
|
||||||
@@ -270,56 +272,66 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
|||||||
const errorDevices = [];
|
const errorDevices = [];
|
||||||
|
|
||||||
// meanwhile, establish olm sessions for devices that we don't
|
// meanwhile, establish olm sessions for devices that we don't
|
||||||
// already have a session for, and share keys with them. Use a
|
// already have a session for, and share keys with them. If
|
||||||
// shorter timeout when fetching one-time keys.
|
// we're doing two phases of olm session creation, use a
|
||||||
|
// shorter timeout when fetching one-time keys for the first
|
||||||
|
// phase.
|
||||||
|
const start = Date.now();
|
||||||
await this._shareKeyWithDevices(
|
await this._shareKeyWithDevices(
|
||||||
session, key, payload, devicesWithoutSession, errorDevices, 2000,
|
session, key, payload, devicesWithoutSession, errorDevices,
|
||||||
|
singleOlmCreationPhase ? 10000 : 2000,
|
||||||
);
|
);
|
||||||
|
|
||||||
(async () => {
|
if (!singleOlmCreationPhase && (Date.now() - start < 10000)) {
|
||||||
// Retry sending keys to devices that we were unable to establish
|
// perform the second phase of olm session creation if requested,
|
||||||
// an olm session for. This time, we use a longer timeout, but we
|
// and if the first phase didn't take too long
|
||||||
// do this in the background and don't block anything else while we
|
(async () => {
|
||||||
// do this.
|
// Retry sending keys to devices that we were unable to establish
|
||||||
const retryDevices = {};
|
// an olm session for. This time, we use a longer timeout, but we
|
||||||
for (const {userId, deviceInfo} of errorDevices) {
|
// do this in the background and don't block anything else while we
|
||||||
retryDevices[userId] = retryDevices[userId] || [];
|
// do this.
|
||||||
retryDevices[userId].push(deviceInfo);
|
const retryDevices = {};
|
||||||
}
|
for (const {userId, deviceInfo} of errorDevices) {
|
||||||
|
retryDevices[userId] = retryDevices[userId] || [];
|
||||||
|
retryDevices[userId].push(deviceInfo);
|
||||||
|
}
|
||||||
|
|
||||||
const failedDevices = [];
|
const failedDevices = [];
|
||||||
await this._shareKeyWithDevices(
|
await this._shareKeyWithDevices(
|
||||||
session, key, payload, retryDevices, failedDevices,
|
session, key, payload, retryDevices, failedDevices,
|
||||||
);
|
|
||||||
|
|
||||||
const blockedMap = {};
|
|
||||||
const filteredFailedDevices =
|
|
||||||
await this._olmDevice.filterOutNotifiedErrorDevices(
|
|
||||||
failedDevices,
|
|
||||||
);
|
);
|
||||||
for (const {userId, deviceInfo} of filteredFailedDevices) {
|
|
||||||
blockedMap[userId] = blockedMap[userId] || {};
|
|
||||||
// we use a similar format to what
|
|
||||||
// olmlib.ensureOlmSessionsForDevices returns, so that
|
|
||||||
// we can use the same function to split
|
|
||||||
blockedMap[userId][deviceInfo.deviceId] = {
|
|
||||||
device: {
|
|
||||||
code: "m.no_olm",
|
|
||||||
reason: WITHHELD_MESSAGES["m.no_olm"],
|
|
||||||
deviceInfo,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const deviceId = deviceInfo.deviceId;
|
const blockedMap = {};
|
||||||
|
const filteredFailedDevices =
|
||||||
|
await this._olmDevice.filterOutNotifiedErrorDevices(
|
||||||
|
failedDevices,
|
||||||
|
);
|
||||||
|
for (const {userId, deviceInfo} of filteredFailedDevices) {
|
||||||
|
blockedMap[userId] = blockedMap[userId] || {};
|
||||||
|
// we use a similar format to what
|
||||||
|
// olmlib.ensureOlmSessionsForDevices returns, so that
|
||||||
|
// we can use the same function to split
|
||||||
|
blockedMap[userId][deviceInfo.deviceId] = {
|
||||||
|
device: {
|
||||||
|
code: "m.no_olm",
|
||||||
|
reason: WITHHELD_MESSAGES["m.no_olm"],
|
||||||
|
deviceInfo,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// mark this device as "handled" because we don't want to try
|
const deviceId = deviceInfo.deviceId;
|
||||||
// to claim a one-time-key for dead devices on every message.
|
|
||||||
session.markSharedWithDevice(userId, deviceId, key.chain_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
// notify devices that we couldn't get an olm session
|
// mark this device as "handled" because we don't want to try
|
||||||
await this._notifyBlockedDevices(session, blockedMap);
|
// to claim a one-time-key for dead devices on every message.
|
||||||
})();
|
session.markSharedWithDevice(
|
||||||
|
userId, deviceId, key.chain_index,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// notify devices that we couldn't get an olm session
|
||||||
|
await this._notifyBlockedDevices(session, blockedMap);
|
||||||
|
})();
|
||||||
|
}
|
||||||
})(),
|
})(),
|
||||||
(async () => {
|
(async () => {
|
||||||
// also, notify blocked devices that they're blocked
|
// also, notify blocked devices that they're blocked
|
||||||
@@ -786,7 +798,7 @@ MegolmEncryption.prototype.prepareToEncrypt = function(room) {
|
|||||||
this._removeUnknownDevices(devicesInRoom);
|
this._removeUnknownDevices(devicesInRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this._ensureOutboundSession(devicesInRoom, blocked);
|
await this._ensureOutboundSession(devicesInRoom, blocked, true);
|
||||||
|
|
||||||
delete this.encryptionPreparation;
|
delete this.encryptionPreparation;
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user