From adac0c353cab2faa78b2c0c51e066935583382a7 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 22 Nov 2019 15:56:06 +0000 Subject: [PATCH 1/2] Fix calls in e2e rooms Events will be decrypted after the sync event, so we were having to wait until the next sync event before they got processed. --- src/client.js | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/client.js b/src/client.js index 2a7654bfc..5803e6807 100644 --- a/src/client.js +++ b/src/client.js @@ -4644,8 +4644,11 @@ function setupCallEventHandler(client) { // This happens quite often, eg. replaying sync from storage, catchup sync // after loading and after we've been offline for a bit. let callEventBuffer = []; - function onSync(state) { - if (state === "SYNCING") { + function evaluateEventBuffer() { + if (client.getSyncState() === "SYNCING") { + // don't process any events until they are all decrypted + if (callEventBuffer.some((e) => e.isBeingDecrypted())) return; + const ignoreCallIds = {}; // Set // inspect the buffer and mark all calls which have been answered // or hung up before passing them to the call event handler. @@ -4670,20 +4673,31 @@ function setupCallEventHandler(client) { callEventBuffer = []; } } - client.on("sync", onSync); + client.on("sync", evaluateEventBuffer); 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; + // any call events or ones that might be once they're decrypted + if (event.getType().indexOf("m.call.") === 0 || event.isBeingDecrypted()) { + // queue up for processing once all events from this sync have been + // processed (see above). + callEventBuffer.push(event); + } + + if (event.isBeingDecrypted() || event.isDecryptionFailure()) { + // add an event listener for once the event is decrypted. + event.once("Event.decrypted", () => { + if (event.getType().indexOf("m.call.") === -1) return; + + if (callEventBuffer.includes(event)) { + // we were waiting for that event to decrypt, so recheck the buffer + evaluateEventBuffer(); + } else { + // This one wasn't buffered so just run the event handler for it + // straight away + callEventHandler(e); + } + }); } - // queue up for processing once all events from this sync have been - // processed (see above). - callEventBuffer.push(event); } client.on("event", onEvent); From d8337d703d61ad9300da763cf4e1f2a579366828 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 22 Nov 2019 15:59:36 +0000 Subject: [PATCH 2/2] Use the right variable name --- src/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index 5803e6807..c62c8fca0 100644 --- a/src/client.js +++ b/src/client.js @@ -4694,7 +4694,7 @@ function setupCallEventHandler(client) { } else { // This one wasn't buffered so just run the event handler for it // straight away - callEventHandler(e); + callEventHandler(event); } }); }