diff --git a/spec/integ/matrix-client-crypto.spec.js b/spec/integ/matrix-client-crypto.spec.js index fe5f63eac..d74009177 100644 --- a/spec/integ/matrix-client-crypto.spec.js +++ b/spec/integ/matrix-client-crypto.spec.js @@ -203,7 +203,7 @@ function aliSendsFirstMessage() { expectAliQueryKeys() .then(expectAliClaimKeys) .then(expectAliSendMessageRequest), - ]).spread(function(_, ciphertext) { + ]).then(function([_, ciphertext]) { return ciphertext; }); } @@ -218,7 +218,7 @@ function aliSendsMessage() { return Promise.all([ sendMessage(aliTestClient.client), expectAliSendMessageRequest(), - ]).spread(function(_, ciphertext) { + ]).then(function([_, ciphertext]) { return ciphertext; }); } @@ -234,7 +234,7 @@ function bobSendsReplyMessage() { sendMessage(bobTestClient.client), expectBobQueryKeys() .then(expectBobSendMessageRequest), - ]).spread(function(_, ciphertext) { + ]).then(function([_, ciphertext]) { return ciphertext; }); } @@ -279,16 +279,17 @@ function sendMessage(client) { function expectSendMessageRequest(httpBackend) { const path = "/send/m.room.encrypted/"; - const deferred = Promise.defer(); - httpBackend.when("PUT", path).respond(200, function(path, content) { - deferred.resolve(content); - return { - event_id: "asdfgh", - }; + const prom = new Promise((resolve) => { + httpBackend.when("PUT", path).respond(200, function(path, content) { + resolve(content); + return { + event_id: "asdfgh", + }; + }); }); // it can take a while to process the key query - return httpBackend.flush(path, 1).then(() => deferred.promise); + return httpBackend.flush(path, 1).then(() => prom); } function aliRecvMessage() { @@ -491,7 +492,7 @@ describe("MatrixClient crypto", function() { aliTestClient.client.getStoredDevicesForUser(bobUserId), aliTestClient.client.getStoredDevicesForUser(eveUserId), ]); - }).spread((bobDevices, eveDevices) => { + }).then(([bobDevices, eveDevices]) => { // should get an empty list expect(bobDevices).toEqual([]); expect(eveDevices).toEqual([]); diff --git a/spec/integ/matrix-client-event-timeline.spec.js b/spec/integ/matrix-client-event-timeline.spec.js index 96b28e58f..86436c27d 100644 --- a/spec/integ/matrix-client-event-timeline.spec.js +++ b/spec/integ/matrix-client-event-timeline.spec.js @@ -83,18 +83,19 @@ function startClient(httpBackend, client) { client.startClient(); // set up a promise which will resolve once the client is initialised - const deferred = Promise.defer(); - client.on("sync", function(state) { - logger.log("sync", state); - if (state != "SYNCING") { - return; - } - deferred.resolve(); + const prom = new Promise((resolve) => { + client.on("sync", function(state) { + logger.log("sync", state); + if (state != "SYNCING") { + return; + } + resolve(); + }); }); return Promise.all([ httpBackend.flushAllExpected(), - deferred.promise, + prom, ]); } @@ -343,25 +344,25 @@ describe("MatrixClient event timelines", function() { }; }); - const deferred = Promise.defer(); - client.on("sync", function() { - client.getEventTimeline(timelineSet, EVENTS[2].event_id, - ).then(function(tl) { - expect(tl.getEvents().length).toEqual(4); - expect(tl.getEvents()[0].event).toEqual(EVENTS[1]); - expect(tl.getEvents()[1].event).toEqual(EVENTS[2]); - expect(tl.getEvents()[3].event).toEqual(EVENTS[3]); - expect(tl.getPaginationToken(EventTimeline.BACKWARDS)) - .toEqual("start_token"); - // expect(tl.getPaginationToken(EventTimeline.FORWARDS)) - // .toEqual("s_5_4"); - }).done(() => deferred.resolve(), - (e) => deferred.reject(e)); + const prom = new Promise((resolve, reject) => { + client.on("sync", function() { + client.getEventTimeline(timelineSet, EVENTS[2].event_id, + ).then(function(tl) { + expect(tl.getEvents().length).toEqual(4); + expect(tl.getEvents()[0].event).toEqual(EVENTS[1]); + expect(tl.getEvents()[1].event).toEqual(EVENTS[2]); + expect(tl.getEvents()[3].event).toEqual(EVENTS[3]); + expect(tl.getPaginationToken(EventTimeline.BACKWARDS)) + .toEqual("start_token"); + // expect(tl.getPaginationToken(EventTimeline.FORWARDS)) + // .toEqual("s_5_4"); + }).done(resolve, reject); + }); }); return Promise.all([ httpBackend.flushAllExpected(), - deferred.promise, + prom, ]); }); diff --git a/spec/integ/matrix-client-syncing.spec.js b/spec/integ/matrix-client-syncing.spec.js index 1294b6a5d..f015943c7 100644 --- a/spec/integ/matrix-client-syncing.spec.js +++ b/spec/integ/matrix-client-syncing.spec.js @@ -691,12 +691,12 @@ describe("MatrixClient syncing", function() { include_leave: true }}); }).respond(200, { filter_id: "another_id" }); - const defer = Promise.defer(); - - httpBackend.when("GET", "/sync").check(function(req) { - expect(req.queryParams.filter).toEqual("another_id"); - defer.resolve(); - }).respond(200, {}); + const prom = new Promise((resolve) => { + httpBackend.when("GET", "/sync").check(function(req) { + expect(req.queryParams.filter).toEqual("another_id"); + resolve(); + }).respond(200, {}); + }); client.syncLeftRooms(); @@ -707,7 +707,7 @@ describe("MatrixClient syncing", function() { // flush the syncs return httpBackend.flushAllExpected(); }), - defer.promise, + prom, ]); }); diff --git a/spec/unit/crypto.spec.js b/spec/unit/crypto.spec.js index acbc08731..67ed8db19 100644 --- a/spec/unit/crypto.spec.js +++ b/spec/unit/crypto.spec.js @@ -25,8 +25,8 @@ describe("Crypto", function() { return; } - beforeEach(function(done) { - Olm.init().then(done); + beforeAll(function() { + return Olm.init(); }); it("Crypto exposes the correct olm library version", function() { diff --git a/spec/unit/crypto/algorithms/megolm.spec.js b/spec/unit/crypto/algorithms/megolm.spec.js index eae11da59..ad5e7f20a 100644 --- a/spec/unit/crypto/algorithms/megolm.spec.js +++ b/spec/unit/crypto/algorithms/megolm.spec.js @@ -25,14 +25,16 @@ describe("MegolmDecryption", function() { return; } + beforeAll(function() { + return Olm.init(); + }); + let megolmDecryption; let mockOlmLib; let mockCrypto; let mockBaseApis; beforeEach(async function() { - await Olm.init(); - mockCrypto = testUtils.mock(Crypto, 'Crypto'); mockBaseApis = {}; diff --git a/spec/unit/crypto/algorithms/olm.spec.js b/spec/unit/crypto/algorithms/olm.spec.js index 1b26a6d22..04bd0c322 100644 --- a/spec/unit/crypto/algorithms/olm.spec.js +++ b/spec/unit/crypto/algorithms/olm.spec.js @@ -48,12 +48,14 @@ describe("OlmDecryption", function() { return; } + beforeAll(function() { + return global.Olm.init(); + }); + let aliceOlmDevice; let bobOlmDevice; beforeEach(async function() { - await global.Olm.init(); - aliceOlmDevice = makeOlmDevice(); bobOlmDevice = makeOlmDevice(); await aliceOlmDevice.init(); diff --git a/spec/unit/crypto/backup.spec.js b/spec/unit/crypto/backup.spec.js index c13287f22..7413ee0da 100644 --- a/spec/unit/crypto/backup.spec.js +++ b/spec/unit/crypto/backup.spec.js @@ -128,6 +128,10 @@ describe("MegolmBackup", function() { return; } + beforeAll(function() { + return Olm.init(); + }); + let olmDevice; let mockOlmLib; let mockCrypto; @@ -136,7 +140,6 @@ describe("MegolmBackup", function() { let cryptoStore; let megolmDecryption; beforeEach(async function() { - await Olm.init(); mockCrypto = testUtils.mock(Crypto, 'Crypto'); mockCrypto.backupKey = new Olm.PkEncryption(); mockCrypto.backupKey.set_recipient_key( diff --git a/spec/unit/crypto/cross-signing.spec.js b/spec/unit/crypto/cross-signing.spec.js index 2023bc970..06840b74a 100644 --- a/spec/unit/crypto/cross-signing.spec.js +++ b/spec/unit/crypto/cross-signing.spec.js @@ -55,8 +55,8 @@ describe("Cross Signing", function() { return; } - beforeEach(async function() { - await global.Olm.init(); + beforeAll(function() { + return global.Olm.init(); }); it("should sign the master key with the device key", async function() { diff --git a/spec/unit/crypto/secrets.spec.js b/spec/unit/crypto/secrets.spec.js index 0a348e937..845f756d3 100644 --- a/spec/unit/crypto/secrets.spec.js +++ b/spec/unit/crypto/secrets.spec.js @@ -40,8 +40,8 @@ describe("Secrets", function() { return; } - beforeEach(async function() { - await global.Olm.init(); + beforeAll(function() { + return global.Olm.init(); }); it("should store and retrieve a secret", async function() { diff --git a/spec/unit/crypto/verification/qr_code.spec.js b/spec/unit/crypto/verification/qr_code.spec.js index 5a1507729..b59bddb72 100644 --- a/spec/unit/crypto/verification/qr_code.spec.js +++ b/spec/unit/crypto/verification/qr_code.spec.js @@ -33,8 +33,8 @@ describe("QR code verification", function() { return; } - beforeEach(async function() { - await Olm.init(); + beforeAll(function() { + return Olm.init(); }); describe("showing", function() { diff --git a/spec/unit/crypto/verification/request.spec.js b/spec/unit/crypto/verification/request.spec.js index 761edc8b7..4fb103457 100644 --- a/spec/unit/crypto/verification/request.spec.js +++ b/spec/unit/crypto/verification/request.spec.js @@ -35,8 +35,8 @@ describe("verification request", function() { return; } - beforeEach(async function() { - await Olm.init(); + beforeAll(function() { + return Olm.init(); }); it("should request and accept a verification", async function() { diff --git a/spec/unit/crypto/verification/sas.spec.js b/spec/unit/crypto/verification/sas.spec.js index d5d5c1a9f..bf41fefa8 100644 --- a/spec/unit/crypto/verification/sas.spec.js +++ b/spec/unit/crypto/verification/sas.spec.js @@ -45,8 +45,8 @@ describe("SAS verification", function() { return; } - beforeEach(async function() { - await Olm.init(); + beforeAll(function() { + return Olm.init(); }); it("should error on an unexpected event", async function() { @@ -169,6 +169,12 @@ describe("SAS verification", function() { } }); }); + afterEach(async function() { + await Promise.all([ + alice.stop(), + bob.stop(), + ]); + }); it("should verify a key", async function() { let macMethod; diff --git a/src/client.js b/src/client.js index 6fc6a3223..715580d01 100644 --- a/src/client.js +++ b/src/client.js @@ -73,7 +73,7 @@ function keysFromRecoverySession(sessions, decryptionKey, roomId) { decrypted.room_id = roomId; keys.push(decrypted); } catch (e) { - logger.log("Failed to decrypt session from backup"); + logger.log("Failed to decrypt megolm session from backup", e); } } return keys; @@ -1626,7 +1626,7 @@ MatrixClient.prototype._restoreKeyBackup = function( key.session_id = targetSessionId; keys.push(key); } catch (e) { - logger.log("Failed to decrypt session from backup"); + logger.log("Failed to decrypt megolm session from backup", e); } } @@ -1872,33 +1872,33 @@ MatrixClient.prototype.joinRoom = function(roomIdOrAlias, opts, callback) { const reqOpts = {qsStringifyOptions: {arrayFormat: 'repeat'}}; - const defer = Promise.defer(); - const self = this; - sign_promise.then(function(signed_invite_object) { - const data = {}; - if (signed_invite_object) { - data.third_party_signed = signed_invite_object; - } + const prom = new Promise((resolve, reject) => { + sign_promise.then(function(signed_invite_object) { + const data = {}; + if (signed_invite_object) { + data.third_party_signed = signed_invite_object; + } - const path = utils.encodeUri("/join/$roomid", { $roomid: roomIdOrAlias}); - return self._http.authedRequest( - undefined, "POST", path, queryString, data, reqOpts); - }).then(function(res) { - const roomId = res.room_id; - const syncApi = new SyncApi(self, self._clientOpts); - const room = syncApi.createRoom(roomId); - if (opts.syncRoom) { - // v2 will do this for us - // return syncApi.syncRoom(room); - } - return Promise.resolve(room); - }).done(function(room) { - _resolve(callback, defer, room); - }, function(err) { - _reject(callback, defer, err); + const path = utils.encodeUri("/join/$roomid", { $roomid: roomIdOrAlias}); + return self._http.authedRequest( + undefined, "POST", path, queryString, data, reqOpts); + }).then(function(res) { + const roomId = res.room_id; + const syncApi = new SyncApi(self, self._clientOpts); + const room = syncApi.createRoom(roomId); + if (opts.syncRoom) { + // v2 will do this for us + // return syncApi.syncRoom(room); + } + return Promise.resolve(room); + }).done(function(room) { + _resolve(callback, resolve, room); + }, function(err) { + _reject(callback, reject, err); + }); }); - return defer.promise; + return prom; }; /** @@ -3214,42 +3214,45 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) { // reduce the required number of events appropriately limit = limit - numAdded; - const defer = Promise.defer(); + const self = this; + const prom = new Promise((resolve, reject) => { + // wait for a time before doing this request + // (which may be 0 in order not to special case the code paths) + sleep(timeToWaitMs).then(function() { + return self._createMessagesRequest( + room.roomId, + room.oldState.paginationToken, + limit, + 'b'); + }).done(function(res) { + const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self)); + if (res.state) { + const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self)); + room.currentState.setUnknownStateEvents(stateEvents); + } + room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline()); + room.oldState.paginationToken = res.end; + if (res.chunk.length === 0) { + room.oldState.paginationToken = null; + } + self.store.storeEvents(room, matrixEvents, res.end, true); + self._ongoingScrollbacks[room.roomId] = null; + _resolve(callback, resolve, room); + }, function(err) { + self._ongoingScrollbacks[room.roomId] = { + errorTs: Date.now(), + }; + _reject(callback, reject, err); + }); + }); + info = { - promise: defer.promise, + promise: prom, errorTs: null, }; - const self = this; - // wait for a time before doing this request - // (which may be 0 in order not to special case the code paths) - sleep(timeToWaitMs).then(function() { - return self._createMessagesRequest( - room.roomId, - room.oldState.paginationToken, - limit, - 'b'); - }).done(function(res) { - const matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper(self)); - if (res.state) { - const stateEvents = utils.map(res.state, _PojoToMatrixEventMapper(self)); - room.currentState.setUnknownStateEvents(stateEvents); - } - room.addEventsToTimeline(matrixEvents, true, room.getLiveTimeline()); - room.oldState.paginationToken = res.end; - if (res.chunk.length === 0) { - room.oldState.paginationToken = null; - } - self.store.storeEvents(room, matrixEvents, res.end, true); - self._ongoingScrollbacks[room.roomId] = null; - _resolve(callback, defer, room); - }, function(err) { - self._ongoingScrollbacks[room.roomId] = { - errorTs: Date.now(), - }; - _reject(callback, defer, err); - }); + this._ongoingScrollbacks[room.roomId] = info; - return defer.promise; + return prom; }; /** @@ -3867,7 +3870,7 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) { } else if (!hasDontNotifyRule) { // Remove the existing one before setting the mute push rule // This is a workaround to SYN-590 (Push rule update fails) - deferred = Promise.defer(); + deferred = utils.defer(); this.deletePushRule(scope, "room", roomPushRule.rule_id) .done(function() { self.addPushRule(scope, "room", roomId, { @@ -3886,26 +3889,26 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) { } if (deferred) { - // Update this.pushRules when the operation completes - const ruleRefreshDeferred = Promise.defer(); - deferred.done(function() { - self.getPushRules().done(function(result) { - self.pushRules = result; - ruleRefreshDeferred.resolve(); + return new Promise((resolve, reject) => { + // Update this.pushRules when the operation completes + deferred.done(function() { + self.getPushRules().done(function(result) { + self.pushRules = result; + resolve(); + }, function(err) { + reject(err); + }); }, function(err) { - ruleRefreshDeferred.reject(err); - }); - }, function(err) { - // Update it even if the previous operation fails. This can help the - // app to recover when push settings has been modifed from another client - self.getPushRules().done(function(result) { - self.pushRules = result; - ruleRefreshDeferred.reject(err); - }, function(err2) { - ruleRefreshDeferred.reject(err); + // Update it even if the previous operation fails. This can help the + // app to recover when push settings has been modifed from another client + self.getPushRules().done(function(result) { + self.pushRules = result; + reject(err); + }, function(err2) { + reject(err); + }); }); }); - return ruleRefreshDeferred.promise; } }; @@ -4705,7 +4708,7 @@ function setupCallEventHandler(client) { const content = event.getContent(); let call = content.call_id ? client.callList[content.call_id] : undefined; let i; - //console.log("RECV %s content=%s", event.getType(), JSON.stringify(content)); + //console.info("RECV %s content=%s", event.getType(), JSON.stringify(content)); if (event.getType() === "m.call.invite") { if (event.getSender() === client.credentials.userId) { @@ -4876,18 +4879,18 @@ function checkTurnServers(client) { }); } -function _reject(callback, defer, err) { +function _reject(callback, reject, err) { if (callback) { callback(err); } - defer.reject(err); + reject(err); } -function _resolve(callback, defer, res) { +function _resolve(callback, resolve, res) { if (callback) { callback(null, res); } - defer.resolve(res); + resolve(res); } function _PojoToMatrixEventMapper(client) { diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 7060074a7..b86b4f858 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -462,7 +462,7 @@ OlmDevice.prototype.createInboundSession = async function( */ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) { if (this._sessionsInProgress[theirDeviceIdentityKey]) { - logger.log("waiting for session to be created"); + logger.log("waiting for olm session to be created"); try { await this._sessionsInProgress[theirDeviceIdentityKey]; } catch (e) { @@ -543,7 +543,7 @@ OlmDevice.prototype.getSessionIdForDevice = async function( */ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) { if (this._sessionsInProgress[deviceIdentityKey] && !nowait) { - logger.log("waiting for session to be created"); + logger.log("waiting for olm session to be created"); try { await this._sessionsInProgress[deviceIdentityKey]; } catch (e) { @@ -595,8 +595,8 @@ OlmDevice.prototype.encryptMessage = async function( (txn) => { this._getSession(theirDeviceIdentityKey, sessionId, txn, (sessionInfo) => { const sessionDesc = sessionInfo.session.describe(); - console.log( - "Session ID " + sessionId + " to " + + logger.log( + "encryptMessage: Olm Session ID " + sessionId + " to " + theirDeviceIdentityKey + ": " + sessionDesc, ); res = sessionInfo.session.encrypt(payloadString); @@ -627,8 +627,8 @@ OlmDevice.prototype.decryptMessage = async function( (txn) => { this._getSession(theirDeviceIdentityKey, sessionId, txn, (sessionInfo) => { const sessionDesc = sessionInfo.session.describe(); - console.log( - "Session ID " + sessionId + " to " + + logger.log( + "decryptMessage: Olm Session ID " + sessionId + " from " + theirDeviceIdentityKey + ": " + sessionDesc, ); payloadString = sessionInfo.session.decrypt(messageType, ciphertext); @@ -740,6 +740,8 @@ OlmDevice.prototype.createOutboundGroupSession = function() { OlmDevice.prototype.encryptGroupMessage = function(sessionId, payloadString) { const self = this; + logger.log(`encrypting msg with megolm session ${sessionId}`); + checkPayloadLength(payloadString); return this._getOutboundGroupSession(sessionId, function(session) { @@ -886,7 +888,9 @@ OlmDevice.prototype.addInboundGroupSession = async function( <= session.first_known_index()) { // existing session has lower index (i.e. can // decrypt more), so keep it - logger.log("Keeping existing session"); + logger.log( + `Keeping existing megolm session ${sessionId}`, + ); return; } } diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 4262c2cee..286c29804 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -104,7 +104,7 @@ OutboundSessionInfo.prototype.sharedWithTooManyDevices = function( } if (!devicesInRoom.hasOwnProperty(userId)) { - logger.log("Starting new session because we shared with " + userId); + logger.log("Starting new megolm session because we shared with " + userId); return true; } @@ -115,7 +115,7 @@ OutboundSessionInfo.prototype.sharedWithTooManyDevices = function( if (!devicesInRoom[userId].hasOwnProperty(deviceId)) { logger.log( - "Starting new session because we shared with " + + "Starting new megolm session because we shared with " + userId + ":" + deviceId, ); return true; @@ -200,6 +200,8 @@ MegolmEncryption.prototype._ensureOutboundSession = function(devicesInRoom) { if (!session) { logger.log(`Starting new megolm session for room ${self._roomId}`); session = await self._prepareNewSession(); + logger.log(`Started new megolm session ${session.sessionId} ` + + `for room ${self._roomId}`); self._outboundSessions[session.sessionId] = session; } @@ -278,7 +280,7 @@ MegolmEncryption.prototype._prepareNewSession = async function() { ).catch((e) => { // This throws if the upload failed, but this is fine // since it will have written it to the db and will retry. - logger.log("Failed to back up group session", e); + logger.log("Failed to back up megolm session", e); }); } @@ -440,19 +442,19 @@ MegolmEncryption.prototype.reshareKeyWithDevice = async function( ) { const obSessionInfo = this._outboundSessions[sessionId]; if (!obSessionInfo) { - logger.debug("Session ID " + sessionId + " not found: not re-sharing keys"); + logger.debug(`megolm session ${sessionId} not found: not re-sharing keys`); return; } // The chain index of the key we previously sent this device if (obSessionInfo.sharedWithDevices[userId] === undefined) { - logger.debug("Session ID " + sessionId + " never shared with user " + userId); + logger.debug(`megolm session ${sessionId} never shared with user ${userId}`); return; } const sentChainIndex = obSessionInfo.sharedWithDevices[userId][device.deviceId]; if (sentChainIndex === undefined) { logger.debug( - "Session ID " + sessionId + " never shared with device " + + "megolm session ID " + sessionId + " never shared with device " + userId + ":" + device.deviceId, ); return; @@ -466,7 +468,7 @@ MegolmEncryption.prototype.reshareKeyWithDevice = async function( if (!key) { logger.warn( - "No outbound session key found for " + sessionId + ": not re-sharing keys", + `No inbound session key found for megolm ${sessionId}: not re-sharing keys`, ); return; } @@ -513,9 +515,8 @@ MegolmEncryption.prototype.reshareKeyWithDevice = async function( [device.deviceId]: encryptedContent, }, }); - logger.debug( - `Re-shared key for session ${sessionId} with ${userId}:${device.deviceId}`, - ); + logger.debug(`Re-shared key for megolm session ${sessionId} ` + + `with ${userId}:${device.deviceId}`); }; /** @@ -550,10 +551,10 @@ MegolmEncryption.prototype._shareKeyWithDevices = async function(session, device await this._encryptAndSendKeysToDevices( session, key.chain_index, userDeviceMaps[i], payload, ); - logger.log(`Completed megolm keyshare in ${this._roomId} ` - + `(slice ${i + 1}/${userDeviceMaps.length})`); + logger.log(`Completed megolm keyshare for ${session.sessionId} ` + + `in ${this._roomId} (slice ${i + 1}/${userDeviceMaps.length})`); } catch (e) { - logger.log(`megolm keyshare in ${this._roomId} ` + logger.log(`megolm keyshare for ${session.sessionId} in ${this._roomId} ` + `(slice ${i + 1}/${userDeviceMaps.length}) failed`); throw e; @@ -922,7 +923,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) { keysClaimed = event.getKeysClaimed(); } - logger.log(`Adding key for megolm session ${senderKey}|${sessionId}`); + logger.log(`Received and adding key for megolm session ${senderKey}|${sessionId}`); return this._olmDevice.addInboundGroupSession( content.room_id, senderKey, forwardingKeyChain, sessionId, content.session_key, keysClaimed, @@ -955,7 +956,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) { ).catch((e) => { // This throws if the upload failed, but this is fine // since it will have written it to the db and will retry. - logger.log("Failed to back up group session", e); + logger.log("Failed to back up megolm session", e); }); } }).catch((e) => { @@ -1088,7 +1089,7 @@ MegolmDecryption.prototype.importRoomKey = function(session) { ).catch((e) => { // This throws if the upload failed, but this is fine // since it will have written it to the db and will retry. - logger.log("Failed to back up group session", e); + logger.log("Failed to back up megolm session", e); }); } // have another go at decrypting events sent with this session. diff --git a/src/crypto/algorithms/olm.js b/src/crypto/algorithms/olm.js index f233e8f9f..93d651d49 100644 --- a/src/crypto/algorithms/olm.js +++ b/src/crypto/algorithms/olm.js @@ -139,7 +139,7 @@ OlmEncryption.prototype.encryptMessage = async function(room, eventType, content } } - return await Promise.all(promises).return(encryptedContent); + return await Promise.all(promises).then(() => encryptedContent); }; /** diff --git a/src/crypto/index.js b/src/crypto/index.js index 20ec1a2dd..d749d1d3f 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1638,7 +1638,7 @@ Crypto.prototype.setRoomEncryption = async function(roomId, config, inhibitDevic // It would otherwise just throw later as an unknown algorithm would, but we may // as well catch this here if (!config.algorithm) { - console.log("Ignoring setRoomEncryption with no algorithm"); + logger.log("Ignoring setRoomEncryption with no algorithm"); return; } @@ -1824,17 +1824,15 @@ Crypto.prototype.exportRoomKeys = async function() { * @return {module:client.Promise} a promise which resolves once the keys have been imported */ Crypto.prototype.importRoomKeys = function(keys) { - return Promise.map( - keys, (key) => { - if (!key.room_id || !key.algorithm) { - logger.warn("ignoring room key entry with missing fields", key); - return null; - } + return Promise.all(keys.map((key) => { + if (!key.room_id || !key.algorithm) { + logger.warn("ignoring room key entry with missing fields", key); + return null; + } - const alg = this._getRoomDecryptor(key.room_id, key.algorithm); - return alg.importRoomKey(key); - }, - ); + const alg = this._getRoomDecryptor(key.room_id, key.algorithm); + return alg.importRoomKey(key); + })); }; /** @@ -2296,6 +2294,9 @@ Crypto.prototype._getTrackedE2eRooms = function() { Crypto.prototype._onToDeviceEvent = function(event) { try { + logger.log(`received to_device ${event.getType()} from: ` + + `${event.getSender()} id: ${event.getId()}`); + if (event.getType() == "m.room_key" || event.getType() == "m.forwarded_room_key") { this._onRoomKeyEvent(event); @@ -2858,14 +2859,10 @@ Crypto.prototype._processReceivedRoomKeyRequests = async function() { // cancellation (and end up with a cancelled request), rather than the // cancellation before the request (and end up with an outstanding // request which should have been cancelled.) - await Promise.map( - requests, (req) => - this._processReceivedRoomKeyRequest(req), - ); - await Promise.map( - cancellations, (cancellation) => - this._processReceivedRoomKeyRequestCancellation(cancellation), - ); + await Promise.all(requests.map((req) => + this._processReceivedRoomKeyRequest(req))); + await Promise.all(cancellations.map((cancellation) => + this._processReceivedRoomKeyRequestCancellation(cancellation))); } catch (e) { logger.error(`Error processing room key requsts: ${e}`); } finally { diff --git a/src/crypto/olmlib.js b/src/crypto/olmlib.js index 93a492e26..4af8e035d 100644 --- a/src/crypto/olmlib.js +++ b/src/crypto/olmlib.js @@ -287,12 +287,12 @@ async function _verifyKeyAndStartSession(olmDevice, oneTimeKey, userId, deviceIn ); } catch (e) { // possibly a bad key - logger.error("Error starting session with device " + + logger.error("Error starting olm session with device " + userId + ":" + deviceId + ": " + e); return null; } - logger.log("Started new sessionid " + sid + + logger.log("Started new olm sessionid " + sid + " for device " + userId + ":" + deviceId); return sid; } diff --git a/src/crypto/store/indexeddb-crypto-store-backend.js b/src/crypto/store/indexeddb-crypto-store-backend.js index 98c5d8945..ede785d00 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.js +++ b/src/crypto/store/indexeddb-crypto-store-backend.js @@ -58,35 +58,34 @@ export class Backend { getOrAddOutgoingRoomKeyRequest(request) { const requestBody = request.requestBody; - const deferred = Promise.defer(); - const txn = this._db.transaction("outgoingRoomKeyRequests", "readwrite"); - txn.onerror = deferred.reject; + return new Promise((resolve, reject) => { + const txn = this._db.transaction("outgoingRoomKeyRequests", "readwrite"); + txn.onerror = reject; - // first see if we already have an entry for this request. - this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => { - if (existing) { - // this entry matches the request - return it. + // first see if we already have an entry for this request. + this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => { + if (existing) { + // this entry matches the request - return it. + logger.log( + `already have key request outstanding for ` + + `${requestBody.room_id} / ${requestBody.session_id}: ` + + `not sending another`, + ); + resolve(existing); + return; + } + + // we got to the end of the list without finding a match + // - add the new request. logger.log( - `already have key request outstanding for ` + - `${requestBody.room_id} / ${requestBody.session_id}: ` + - `not sending another`, + `enqueueing key request for ${requestBody.room_id} / ` + + requestBody.session_id, ); - deferred.resolve(existing); - return; - } - - // we got to the end of the list without finding a match - // - add the new request. - logger.log( - `enqueueing key request for ${requestBody.room_id} / ` + - requestBody.session_id, - ); - txn.oncomplete = () => { deferred.resolve(request); }; - const store = txn.objectStore("outgoingRoomKeyRequests"); - store.add(request); + txn.oncomplete = () => { resolve(request); }; + const store = txn.objectStore("outgoingRoomKeyRequests"); + store.add(request); + }); }); - - return deferred.promise; } /** @@ -100,15 +99,14 @@ export class Backend { * not found */ getOutgoingRoomKeyRequest(requestBody) { - const deferred = Promise.defer(); + return new Promise((resolve, reject) => { + const txn = this._db.transaction("outgoingRoomKeyRequests", "readonly"); + txn.onerror = reject; - const txn = this._db.transaction("outgoingRoomKeyRequests", "readonly"); - txn.onerror = deferred.reject; - - this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => { - deferred.resolve(existing); + this._getOutgoingRoomKeyRequest(txn, requestBody, (existing) => { + resolve(existing); + }); }); - return deferred.promise; } /** diff --git a/src/crypto/store/indexeddb-crypto-store.js b/src/crypto/store/indexeddb-crypto-store.js index b2c2fe4d0..2919fb9ca 100644 --- a/src/crypto/store/indexeddb-crypto-store.js +++ b/src/crypto/store/indexeddb-crypto-store.js @@ -287,7 +287,9 @@ export default class IndexedDBCryptoStore { * @param {function(string)} func Called with the account pickle */ getAccount(txn, func) { - this._backendPromise.value().getAccount(txn, func); + this._backendPromise.then(backend => { + backend.getAccount(txn, func); + }); } /** @@ -298,7 +300,9 @@ export default class IndexedDBCryptoStore { * @param {string} newData The new account pickle to store. */ storeAccount(txn, newData) { - this._backendPromise.value().storeAccount(txn, newData); + this._backendPromise.then(backend => { + backend.storeAccount(txn, newData); + }); } /** @@ -310,7 +314,9 @@ export default class IndexedDBCryptoStore { * { key_type: base64 encoded seed } where key type = user_signing_key_seed or self_signing_key_seed */ getCrossSigningKeys(txn, func) { - this._backendPromise.value().getCrossSigningKeys(txn, func); + this._backendPromise.then(backend => { + backend.getCrossSigningKeys(txn, func); + }); } /** @@ -320,7 +326,9 @@ export default class IndexedDBCryptoStore { * @param {string} keys keys object as getCrossSigningKeys() */ storeCrossSigningKeys(txn, keys) { - this._backendPromise.value().storeCrossSigningKeys(txn, keys); + this._backendPromise.then(backend => { + backend.storeCrossSigningKeys(txn, keys); + }); } // Olm sessions @@ -331,7 +339,9 @@ export default class IndexedDBCryptoStore { * @param {function(int)} func Called with the count of sessions */ countEndToEndSessions(txn, func) { - this._backendPromise.value().countEndToEndSessions(txn, func); + this._backendPromise.then(backend => { + backend.countEndToEndSessions(txn, func); + }); } /** @@ -347,7 +357,9 @@ export default class IndexedDBCryptoStore { * a message. */ getEndToEndSession(deviceKey, sessionId, txn, func) { - this._backendPromise.value().getEndToEndSession(deviceKey, sessionId, txn, func); + this._backendPromise.then(backend => { + backend.getEndToEndSession(deviceKey, sessionId, txn, func); + }); } /** @@ -362,7 +374,9 @@ export default class IndexedDBCryptoStore { * a message. */ getEndToEndSessions(deviceKey, txn, func) { - this._backendPromise.value().getEndToEndSessions(deviceKey, txn, func); + this._backendPromise.then(backend => { + backend.getEndToEndSessions(deviceKey, txn, func); + }); } /** @@ -373,7 +387,9 @@ export default class IndexedDBCryptoStore { * and session keys. */ getAllEndToEndSessions(txn, func) { - this._backendPromise.value().getAllEndToEndSessions(txn, func); + this._backendPromise.then(backend => { + backend.getAllEndToEndSessions(txn, func); + }); } /** @@ -384,12 +400,14 @@ export default class IndexedDBCryptoStore { * @param {*} txn An active transaction. See doTxn(). */ storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) { - this._backendPromise.value().storeEndToEndSession( - deviceKey, sessionId, sessionInfo, txn, - ); + this._backendPromise.then(backend => { + backend.storeEndToEndSession( + deviceKey, sessionId, sessionInfo, txn, + ); + }); } - // Inbound group saessions + // Inbound group sessions /** * Retrieve the end-to-end inbound group session for a given @@ -401,9 +419,11 @@ export default class IndexedDBCryptoStore { * to Base64 end-to-end session. */ getEndToEndInboundGroupSession(senderCurve25519Key, sessionId, txn, func) { - this._backendPromise.value().getEndToEndInboundGroupSession( - senderCurve25519Key, sessionId, txn, func, - ); + this._backendPromise.then(backend => { + backend.getEndToEndInboundGroupSession( + senderCurve25519Key, sessionId, txn, func, + ); + }); } /** @@ -414,7 +434,9 @@ export default class IndexedDBCryptoStore { * sessionData}, then once with null to indicate the end of the list. */ getAllEndToEndInboundGroupSessions(txn, func) { - this._backendPromise.value().getAllEndToEndInboundGroupSessions(txn, func); + this._backendPromise.then(backend => { + backend.getAllEndToEndInboundGroupSessions(txn, func); + }); } /** @@ -427,9 +449,11 @@ export default class IndexedDBCryptoStore { * @param {*} txn An active transaction. See doTxn(). */ addEndToEndInboundGroupSession(senderCurve25519Key, sessionId, sessionData, txn) { - this._backendPromise.value().addEndToEndInboundGroupSession( - senderCurve25519Key, sessionId, sessionData, txn, - ); + this._backendPromise.then(backend => { + backend.addEndToEndInboundGroupSession( + senderCurve25519Key, sessionId, sessionData, txn, + ); + }); } /** @@ -442,9 +466,11 @@ export default class IndexedDBCryptoStore { * @param {*} txn An active transaction. See doTxn(). */ storeEndToEndInboundGroupSession(senderCurve25519Key, sessionId, sessionData, txn) { - this._backendPromise.value().storeEndToEndInboundGroupSession( - senderCurve25519Key, sessionId, sessionData, txn, - ); + this._backendPromise.then(backend => { + backend.storeEndToEndInboundGroupSession( + senderCurve25519Key, sessionId, sessionData, txn, + ); + }); } // End-to-end device tracking @@ -460,7 +486,9 @@ export default class IndexedDBCryptoStore { * @param {*} txn An active transaction. See doTxn(). */ storeEndToEndDeviceData(deviceData, txn) { - this._backendPromise.value().storeEndToEndDeviceData(deviceData, txn); + this._backendPromise.then(backend => { + backend.storeEndToEndDeviceData(deviceData, txn); + }); } /** @@ -471,7 +499,9 @@ export default class IndexedDBCryptoStore { * device data */ getEndToEndDeviceData(txn, func) { - this._backendPromise.value().getEndToEndDeviceData(txn, func); + this._backendPromise.then(backend => { + backend.getEndToEndDeviceData(txn, func); + }); } // End to End Rooms @@ -483,7 +513,9 @@ export default class IndexedDBCryptoStore { * @param {*} txn An active transaction. See doTxn(). */ storeEndToEndRoom(roomId, roomInfo, txn) { - this._backendPromise.value().storeEndToEndRoom(roomId, roomInfo, txn); + this._backendPromise.then(backend => { + backend.storeEndToEndRoom(roomId, roomInfo, txn); + }); } /** @@ -492,7 +524,9 @@ export default class IndexedDBCryptoStore { * @param {function(Object)} func Function called with the end to end encrypted rooms */ getEndToEndRooms(txn, func) { - this._backendPromise.value().getEndToEndRooms(txn, func); + this._backendPromise.then(backend => { + backend.getEndToEndRooms(txn, func); + }); } // session backups diff --git a/src/store/indexeddb-remote-backend.js b/src/store/indexeddb-remote-backend.js index 920739b6e..270bef961 100644 --- a/src/store/indexeddb-remote-backend.js +++ b/src/store/indexeddb-remote-backend.js @@ -17,6 +17,7 @@ limitations under the License. import Promise from 'bluebird'; import logger from '../logger'; +import {defer} from '../utils'; /** * An IndexedDB store backend where the actual backend sits in a web @@ -152,7 +153,7 @@ RemoteIndexedDBStoreBackend.prototype = { // the promise automatically gets rejected return Promise.resolve().then(() => { const seq = this._nextSeq++; - const def = Promise.defer(); + const def = defer(); this._inFlight[seq] = def; diff --git a/src/utils.js b/src/utils.js index e3ca7dd81..49863ecd8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -718,3 +718,16 @@ module.exports.sleep = (ms, value) => new Promise((resolve => { module.exports.isNullOrUndefined = function(val) { return val === null || val === undefined; }; + +// Returns a Deferred +module.exports.defer = () => { + let resolve; + let reject; + + const promise = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + + return {resolve, reject, promise}; +};