Skip to content

Commit

Permalink
Merge pull request #1698 from matrix-org/gsouquet/fix-call-handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
germain-gg authored May 19, 2021
2 parents 4b0db6c + 0c47412 commit f346cd6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ export function MatrixClient(opts) {
if (call) {
this._callEventHandler = new CallEventHandler(this);
this._supportsVoip = true;
// Start listening for calls after the initial sync is done
// We do not need to backfill the call event buffer
// with encrypted events that might never get decrypted
this.on("sync", this._startCallEventHandler);
} else {
this._callEventHandler = null;
}
Expand Down Expand Up @@ -4986,6 +4990,13 @@ MatrixClient.prototype.getOpenIdToken = function() {
// VoIP operations
// ===============

MatrixClient.prototype._startCallEventHandler = function() {
if (this.isInitialSyncComplete()) {
this._callEventHandler.start();
this.off("sync", this._startCallEventHandler);
}
};

/**
* @param {module:client.callback} callback Optional.
* @return {Promise} Resolves: TODO
Expand Down
19 changes: 10 additions & 9 deletions src/webrtc/callEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class CallEventHandler {
// after loading and after we've been offline for a bit.
this.callEventBuffer = [];
this.candidateEventsByCall = new Map<string, Array<MatrixEvent>>();
}

public start() {
this.client.on("sync", this.evaluateEventBuffer);
this.client.on("event", this.onEvent);
}
Expand All @@ -52,12 +55,11 @@ export class CallEventHandler {
this.client.removeListener("event", this.onEvent);
}

private evaluateEventBuffer = () => {
private evaluateEventBuffer = async () => {
if (this.client.getSyncState() === "SYNCING") {
// don't process any events until they are all decrypted
if (this.callEventBuffer.some((e) => {
return e.isBeingDecrypted() || e.shouldAttemptDecryption()
})) return;
await Promise.all(this.callEventBuffer.map(event => {
this.client.decryptEventIfNeeded(event);
}));

const ignoreCallIds = new Set<String>();
// inspect the buffer and mark all calls which have been answered
Expand Down Expand Up @@ -88,20 +90,19 @@ export class CallEventHandler {
}

private onEvent = (event: MatrixEvent) => {
this.client.decryptEventIfNeeded(event);
// any call events or ones that might be once they're decrypted
const isBeingDecrypted = event.isBeingDecrypted();
const shouldAttemptDecryption = event.shouldAttemptDecryption();
if (
event.getType().indexOf("m.call.") === 0 ||
event.getType().indexOf("org.matrix.call.") === 0
|| isBeingDecrypted || shouldAttemptDecryption
|| event.isBeingDecrypted()
) {
// queue up for processing once all events from this sync have been
// processed (see above).
this.callEventBuffer.push(event);
}

if (event.isDecryptionFailure() || isBeingDecrypted || shouldAttemptDecryption) {
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;
Expand Down

0 comments on commit f346cd6

Please sign in to comment.