From 04387e78ccecda2e5b86ad5c785dbbc7c79600a1 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 10 Mar 2020 15:56:33 -0400 Subject: [PATCH] some cleanups --- spec/unit/crypto/algorithms/megolm.spec.js | 38 +++++++++------------- src/crypto/algorithms/megolm.js | 10 +++--- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/spec/unit/crypto/algorithms/megolm.spec.js b/spec/unit/crypto/algorithms/megolm.spec.js index 0dd005feb..3b7fc91e1 100644 --- a/spec/unit/crypto/algorithms/megolm.spec.js +++ b/spec/unit/crypto/algorithms/megolm.spec.js @@ -516,21 +516,22 @@ describe("MegolmDecryption", function() { }; }; - let run = false; - aliceClient.sendToDevice = async (msgtype, contentMap) => { - run = true; - expect(msgtype).toBe("org.matrix.room_key.withheld"); - expect(contentMap).toStrictEqual({ - '@bob:example.com': { - bobdevice: { - algorithm: "m.megolm.v1.aes-sha2", - code: 'm.no_olm', - reason: 'Unable to establish a secure channel.', - sender_key: aliceDevice.deviceCurve25519Key, + const sendPromise = new Promise((resolve, reject) => { + aliceClient.sendToDevice = async (msgtype, contentMap) => { + expect(msgtype).toBe("org.matrix.room_key.withheld"); + expect(contentMap).toStrictEqual({ + '@bob:example.com': { + bobdevice: { + algorithm: "m.megolm.v1.aes-sha2", + code: 'm.no_olm', + reason: 'Unable to establish a secure channel.', + sender_key: aliceDevice.deviceCurve25519Key, + }, }, - }, - }); - }; + }); + resolve(); + }; + }); const event = new MatrixEvent({ type: "m.room.message", @@ -540,14 +541,7 @@ describe("MegolmDecryption", function() { content: {}, }); await aliceClient._crypto.encryptEvent(event, aliceRoom); - await new Promise((resolve) => { - // encryptMessage retries senders in the background before giving - // up and telling them that there's no olm channel, so we need to - // wait a bit before checking that we got the message - setTimeout(resolve, 100); - }); - - expect(run).toBe(true); + await sendPromise; }); it("throws an error describing why it doesn't have a key", async function() { diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 51a25e6cc..95cf43e3e 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -244,8 +244,6 @@ MegolmEncryption.prototype._ensureOutboundSession = async function( } } - const errorDevices = []; - const key = this._olmDevice.getOutboundGroupSessionKey(session.sessionId); const payload = { type: "m.room_key", @@ -269,6 +267,8 @@ MegolmEncryption.prototype._ensureOutboundSession = async function( ); })(), (async () => { + const errorDevices = []; + // meanwhile, establish olm sessions for devices that we don't // already have a session for, and share keys with them. Use a // shorter timeout when fetching one-time keys. @@ -769,11 +769,12 @@ MegolmEncryption.prototype._notifyBlockedDevices = async function( * @param {module:models/room} room the room the event is in */ MegolmEncryption.prototype.prepareToEncrypt = function(room) { - logger.log(`Preparing to encrypt events for ${this._roomId}`); + logger.debug(`Preparing to encrypt events for ${this._roomId}`); if (this.encryptionPreparation) { // We're already preparing something, so don't do anything else. // FIXME: check if we need to restart + // (https://github.com/matrix-org/matrix-js-sdk/issues/1255) return; } @@ -807,7 +808,8 @@ MegolmEncryption.prototype.encryptMessage = async function(room, eventType, cont if (this.encryptionPreparation) { // If we started sending keys, wait for it to be done. - // FIXME: check if we need to restart + // FIXME: check if we need to cancel + // (https://github.com/matrix-org/matrix-js-sdk/issues/1255) try { await this.encryptionPreparation; } catch (e) {