From ee5b8748b58e8b94aef81c61c14a89c9d4227bc4 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 24 Aug 2017 13:32:19 +0100 Subject: [PATCH 1/2] Add MatrixEvent.isDecryptionFailure() --- src/models/event.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/models/event.js b/src/models/event.js index 89f574851..3a4e87f59 100644 --- a/src/models/event.js +++ b/src/models/event.js @@ -318,6 +318,19 @@ utils.extend(module.exports.MatrixEvent.prototype, { return this._decryptionPromise != null; }, + /** + * Check if this event is an encrypted event which we failed to decrypt + * + * (This implies that we might retry decryption at some point in the future) + * + * @return {boolean} True if this event is an encrypted event which we + * couldn't decrypt. + */ + isDecryptionFailure: function() { + return this._clearEvent && this._clearEvent.content && + this._clearEvent.content.msgtype === "m.bad.encrypted"; + }, + /** * Start the process of trying to decrypt this event. * From ec5f9a28920a859ec42c990fb1769e5f0860ab56 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 24 Aug 2017 13:24:02 +0100 Subject: [PATCH 2/2] Handle m.call.* events which are decrypted asynchronously Handle the case where received m.call.* events are not decrypted at the point of the 'event' notification by adding an 'Event.decrypted' listener for them. --- src/client.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/client.js b/src/client.js index 59718f6f1..f8126e8e7 100644 --- a/src/client.js +++ b/src/client.js @@ -3053,6 +3053,10 @@ function setupCallEventHandler(client) { // now loop through the buffer chronologically and inject them callEventBuffer.forEach(function(e) { if (ignoreCallIds[e.getContent().call_id]) { + console.log( + 'Ignoring previously answered/hungup call ' + + e.getContent().call_id, + ); return; } callEventHandler(e); @@ -3061,20 +3065,25 @@ function setupCallEventHandler(client) { } }); - client.on("event", function(event) { - if (!isClientPrepared) { - if (event.getType().indexOf("m.call.") === 0) { - callEventBuffer.push(event); + client.on("event", onEvent); + + function onEvent(event) { + if (event.getType().indexOf("m.call.") !== 0) { + // not a call event + if (event.isBeingDecrypted() || event.isDecryptionFailure()) { + // not *yet* a call event, but might become one... + event.once("Event.decrypted", onEvent); } return; } + if (!isClientPrepared) { + callEventBuffer.push(event); + return; + } callEventHandler(event); - }); + } function callEventHandler(event) { - if (event.getType().indexOf("m.call.") !== 0) { - return; // not a call event - } const content = event.getContent(); let call = content.call_id ? client.callList[content.call_id] : undefined; let i;