diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 0bf77cd29..f2552ed1c 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -1692,7 +1692,7 @@ MegolmDecryption.prototype._retryDecryption = async function(senderKey, sessionI await Promise.all([...pending].map(async (ev) => { try { - await ev.attemptDecryption(this._crypto, true); + await ev.attemptDecryption(this._crypto, { isRetry: true }); } catch (e) { // don't die if something goes wrong } diff --git a/src/models/event.js b/src/models/event.js index 58cb49da4..401d0fd03 100644 --- a/src/models/event.js +++ b/src/models/event.js @@ -413,12 +413,13 @@ utils.extend(MatrixEvent.prototype, { * @internal * * @param {module:crypto} crypto crypto module - * @param {bool} isRetry True if this is a retry (enables more logging) + * @param {bool} options.isRetry True if this is a retry (enables more logging) + * @param {bool} options.emit Emits "event.decrypted" if set to true * * @returns {Promise} promise which resolves (to undefined) when the decryption * attempt is completed. */ - attemptDecryption: async function(crypto, isRetry) { + attemptDecryption: async function(crypto, options = {}) { // start with a couple of sanity checks. if (!this.isEncrypted()) { throw new Error("Attempt to decrypt event which isn't encrypted"); @@ -448,7 +449,7 @@ utils.extend(MatrixEvent.prototype, { return this._decryptionPromise; } - this._decryptionPromise = this._decryptionLoop(crypto, isRetry); + this._decryptionPromise = this._decryptionLoop(crypto, options); return this._decryptionPromise; }, @@ -493,7 +494,7 @@ utils.extend(MatrixEvent.prototype, { return recipients; }, - _decryptionLoop: async function(crypto, isRetry) { + _decryptionLoop: async function(crypto, options = {}) { // make sure that this method never runs completely synchronously. // (doing so would mean that we would clear _decryptionPromise *before* // it is set in attemptDecryption - and hence end up with a stuck @@ -510,7 +511,7 @@ utils.extend(MatrixEvent.prototype, { res = this._badEncryptedMessage("Encryption not enabled"); } else { res = await crypto.decryptEvent(this); - if (isRetry) { + if (options.isRetry === true) { logger.info(`Decrypted event on retry (id=${this.getId()})`); } } @@ -518,7 +519,7 @@ utils.extend(MatrixEvent.prototype, { if (e.name !== "DecryptionError") { // not a decryption error: log the whole exception as an error // (and don't bother with a retry) - const re = isRetry ? 're' : ''; + const re = options.isRetry ? 're' : ''; logger.error( `Error ${re}decrypting event ` + `(id=${this.getId()}): ${e.stack || e}`, @@ -584,7 +585,9 @@ utils.extend(MatrixEvent.prototype, { // pick up the wrong contents. this.setPushActions(null); - this.emit("Event.decrypted", this, err); + if (options.emit !== false) { + this.emit("Event.decrypted", this, err); + } return; } diff --git a/src/models/room.js b/src/models/room.js index 634ebd025..5621cff08 100644 --- a/src/models/room.js +++ b/src/models/room.js @@ -239,7 +239,7 @@ Room.prototype.decryptCriticalEvents = function() { .slice(readReceiptTimelineIndex) .filter(event => event.shouldAttemptDecryption()) .reverse() - .map(event => event.attemptDecryption(this._client._crypto, true)); + .map(event => event.attemptDecryption(this._client._crypto, { isRetry: true })); return Promise.allSettled(decryptionPromises); } @@ -251,7 +251,7 @@ Room.prototype.decryptAllEvents = function() { .getEvents() .filter(event => event.shouldAttemptDecryption()) .reverse() - .map(event => event.attemptDecryption(this._client._crypto, true)); + .map(event => event.attemptDecryption(this._client._crypto, { isRetry: true })); return Promise.allSettled(decryptionPromises); }