From 406a2bb001380eb01758c9fd3920515913996cbe Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 15 Dec 2015 15:13:41 +0000 Subject: [PATCH] Do not erroneously emit Call.incoming events on startup Check to see if the call was answered or hung up in addition to having a valid lifetime before emitting the event. Fixes vector-im/vector-web#344 --- lib/client.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 5091a5828..cbcb25e0d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -2224,7 +2224,48 @@ function setupCallEventHandler(client) { var candidatesByCall = { // callId: [Candidate] }; + + // Maintain a buffer of events before the client has synced for the first time. + // This buffer will be inspected to see if we should send incoming call + // notifications. It needs to be buffered to correctly determine if an + // incoming call has had a matching answer/hangup. + var callEventBuffer = []; + var isClientPrepared = false; + client.on("sync", function(state) { + if (state === "PREPARED") { + isClientPrepared = true; + var ignoreCallIds = {}; // Set + // inspect the buffer and mark all calls which have been answered + // or hung up before passing them to the call event handler. + for (var i = callEventBuffer.length - 1; i >= 0; i--) { + var ev = callEventBuffer[i]; + if (ev.getType() === "m.call.answer" || + ev.getType() === "m.call.hangup") { + ignoreCallIds[ev.getContent().call_id] = "yep"; + } + } + // now loop through the buffer chronologically and inject them + callEventBuffer.forEach(function(e) { + if (ignoreCallIds[e.getContent().call_id]) { + return; + } + callEventHandler(e); + }); + callEventBuffer = []; + } + }); + client.on("event", function(event) { + if (!isClientPrepared) { + if (event.getType().indexOf("m.call.") === 0) { + callEventBuffer.push(event); + } + return; + } + callEventHandler(event); + }); + + function callEventHandler(event) { if (event.getType().indexOf("m.call.") !== 0) { return; // not a call event } @@ -2372,7 +2413,7 @@ function setupCallEventHandler(client) { } } } - }); + } } function checkTurnServers(client) {