From 37f51c7536d878ad93767155037d6f9264bac50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 3 Aug 2021 14:48:39 +0200 Subject: [PATCH 1/8] Don't ring if it's disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/CallHandler.tsx | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index e7c1dda54f..d4a29a11b9 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -1,7 +1,8 @@ /* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017, 2018 New Vector Ltd -Copyright 2019, 2020 The Matrix.org Foundation C.I.C. +Copyright 2019 - 2021 The Matrix.org Foundation C.I.C. +Copyright 2021 Šimon Brandner Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -86,6 +87,8 @@ import { randomUppercaseString, randomLowercaseString } from "matrix-js-sdk/src/ import EventEmitter from 'events'; import SdkConfig from './SdkConfig'; import { ensureDMExists, findDMForUser } from './createRoom'; +import { IPushRule, RuleId, TweakName, Tweaks } from "matrix-js-sdk/src/@types/PushRules"; +import { PushProcessor } from 'matrix-js-sdk/src/pushprocessor'; export const PROTOCOL_PSTN = 'm.protocol.pstn'; export const PROTOCOL_PSTN_PREFIXED = 'im.vector.protocol.pstn'; @@ -475,9 +478,22 @@ export default class CallHandler extends EventEmitter { this.silencedCalls.delete(call.callId); } + const incomingCallPushRule = ( + new PushProcessor(MatrixClientPeg.get()).getPushRuleById(RuleId.IncomingCall) as IPushRule + ); switch (newState) { case CallState.Ringing: - this.play(AudioID.Ring); + if ( + incomingCallPushRule?.enabled && + incomingCallPushRule.actions.some((a: Tweaks) => ( + a.set_tweak === TweakName.Sound && + a.value === "ring" + )) + ) { + this.play(AudioID.Ring); + } else { + this.silenceCall(call.callId); + } break; case CallState.InviteSent: this.play(AudioID.Ringback); From e787d14cde2783e3e83613a40355a0afba477191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 3 Aug 2021 14:58:50 +0200 Subject: [PATCH 2/8] Set silenced state as soon as we get the call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/views/voip/IncomingCallBox.tsx | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/components/views/voip/IncomingCallBox.tsx b/src/components/views/voip/IncomingCallBox.tsx index 95e97f1080..66fc2847f7 100644 --- a/src/components/views/voip/IncomingCallBox.tsx +++ b/src/components/views/voip/IncomingCallBox.tsx @@ -63,16 +63,12 @@ export default class IncomingCallBox extends React.Component { private onAction = (payload: ActionPayload) => { switch (payload.action) { case 'call_state': { - const call = CallHandler.sharedInstance().getCallForRoom(payload.room_id); - if (call && call.state === CallState.Ringing) { - this.setState({ - incomingCall: call, - silenced: false, // Reset silenced state for new call - }); + const incomingCall = CallHandler.sharedInstance().getCallForRoom(payload.room_id); + const silenced = CallHandler.sharedInstance().isCallSilenced(incomingCall.callId); + if (incomingCall && incomingCall.state === CallState.Ringing) { + this.setState({ incomingCall, silenced }); } else { - this.setState({ - incomingCall: null, - }); + this.setState({ incomingCall: null }); } } } From 75948587e43cf539dbef39c1f09a37ff7ed1d155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 3 Aug 2021 15:13:40 +0200 Subject: [PATCH 3/8] Avoid calling with undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/views/voip/IncomingCallBox.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/views/voip/IncomingCallBox.tsx b/src/components/views/voip/IncomingCallBox.tsx index 66fc2847f7..4479c02e30 100644 --- a/src/components/views/voip/IncomingCallBox.tsx +++ b/src/components/views/voip/IncomingCallBox.tsx @@ -64,9 +64,11 @@ export default class IncomingCallBox extends React.Component { switch (payload.action) { case 'call_state': { const incomingCall = CallHandler.sharedInstance().getCallForRoom(payload.room_id); - const silenced = CallHandler.sharedInstance().isCallSilenced(incomingCall.callId); if (incomingCall && incomingCall.state === CallState.Ringing) { - this.setState({ incomingCall, silenced }); + this.setState({ + incomingCall, + silenced: CallHandler.sharedInstance().isCallSilenced(incomingCall.callId), + }); } else { this.setState({ incomingCall: null }); } From 2a378f30b7f3b71deddcba306078c78332c647a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 3 Aug 2021 15:38:12 +0200 Subject: [PATCH 4/8] Attempt to fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- test/test-utils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test-utils.js b/test/test-utils.js index 5e29fd242e..6d0d930d93 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -48,6 +48,7 @@ export function createTestClient() { getDomain: jest.fn().mockReturnValue("matrix.rog"), getUserId: jest.fn().mockReturnValue("@userId:matrix.rog"), + getPushRuleById: jest.fn().mockReturnValue(null), getPushActionsForEvent: jest.fn(), getRoom: jest.fn().mockImplementation(mkStubRoom), getRooms: jest.fn().mockReturnValue([]), From a18f41ceed92de96a2df3d21ac23799660142a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 3 Aug 2021 15:43:56 +0200 Subject: [PATCH 5/8] Fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- test/test-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-utils.js b/test/test-utils.js index 6d0d930d93..fb2d866962 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -48,7 +48,6 @@ export function createTestClient() { getDomain: jest.fn().mockReturnValue("matrix.rog"), getUserId: jest.fn().mockReturnValue("@userId:matrix.rog"), - getPushRuleById: jest.fn().mockReturnValue(null), getPushActionsForEvent: jest.fn(), getRoom: jest.fn().mockImplementation(mkStubRoom), getRooms: jest.fn().mockReturnValue([]), @@ -96,6 +95,7 @@ export function createTestClient() { getItem: jest.fn(), }, }, + pushRules: {}, decryptEventIfNeeded: () => Promise.resolve(), isUserIgnored: jest.fn().mockReturnValue(false), getCapabilities: jest.fn().mockResolvedValue({}), From 79e4a95b13bded8accd3a6a4b7e6002ed246fde9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 4 Aug 2021 16:25:44 +0200 Subject: [PATCH 6/8] Move stuff out of if statement for better readability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/CallHandler.tsx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index d4a29a11b9..8d862498d2 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -481,15 +481,14 @@ export default class CallHandler extends EventEmitter { const incomingCallPushRule = ( new PushProcessor(MatrixClientPeg.get()).getPushRuleById(RuleId.IncomingCall) as IPushRule ); + const pushRuleEnabled = incomingCallPushRule?.enabled; + const tweakSetToRing = incomingCallPushRule.actions.some((action: Tweaks) => ( + action.set_tweak === TweakName.Sound && + action.value === "ring" + )); switch (newState) { case CallState.Ringing: - if ( - incomingCallPushRule?.enabled && - incomingCallPushRule.actions.some((a: Tweaks) => ( - a.set_tweak === TweakName.Sound && - a.value === "ring" - )) - ) { + if (pushRuleEnabled && tweakSetToRing) { this.play(AudioID.Ring); } else { this.silenceCall(call.callId); From 023d8749499605da27d949308e91d58e25cb7183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 4 Aug 2021 16:34:59 +0200 Subject: [PATCH 7/8] Add missing ? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/CallHandler.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index 0de6723d33..0cb916578e 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -485,7 +485,7 @@ export default class CallHandler extends EventEmitter { new PushProcessor(MatrixClientPeg.get()).getPushRuleById(RuleId.IncomingCall) as IPushRule ); const pushRuleEnabled = incomingCallPushRule?.enabled; - const tweakSetToRing = incomingCallPushRule.actions.some((action: Tweaks) => ( + const tweakSetToRing = incomingCallPushRule?.actions.some((action: Tweaks) => ( action.set_tweak === TweakName.Sound && action.value === "ring" )); From 74e1342fa8266c97e6d4dcae664b9a1d4447f0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 4 Aug 2021 16:35:46 +0200 Subject: [PATCH 8/8] Wrap cases in {} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/CallHandler.tsx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index 0cb916578e..77569711df 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -481,27 +481,29 @@ export default class CallHandler extends EventEmitter { this.silencedCalls.delete(call.callId); } - const incomingCallPushRule = ( - new PushProcessor(MatrixClientPeg.get()).getPushRuleById(RuleId.IncomingCall) as IPushRule - ); - const pushRuleEnabled = incomingCallPushRule?.enabled; - const tweakSetToRing = incomingCallPushRule?.actions.some((action: Tweaks) => ( - action.set_tweak === TweakName.Sound && - action.value === "ring" - )); switch (newState) { - case CallState.Ringing: + case CallState.Ringing: { + const incomingCallPushRule = ( + new PushProcessor(MatrixClientPeg.get()).getPushRuleById(RuleId.IncomingCall) as IPushRule + ); + const pushRuleEnabled = incomingCallPushRule?.enabled; + const tweakSetToRing = incomingCallPushRule?.actions.some((action: Tweaks) => ( + action.set_tweak === TweakName.Sound && + action.value === "ring" + )); + if (pushRuleEnabled && tweakSetToRing) { this.play(AudioID.Ring); } else { this.silenceCall(call.callId); } break; - case CallState.InviteSent: + } + case CallState.InviteSent: { this.play(AudioID.Ringback); break; - case CallState.Ended: - { + } + case CallState.Ended: { const hangupReason = call.hangupReason; Analytics.trackEvent('voip', 'callEnded', 'hangupReason', hangupReason); this.removeCallForRoom(mappedRoomId);