Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit de16d34

Browse files
authored
Apply strictNullChecks to src/LegacyCallHandler.tsx (#10690)
1 parent f6f5d44 commit de16d34

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

src/LegacyCallHandler.tsx

+35-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
*/
1919

2020
import React from "react";
21+
import { MatrixError } from "matrix-js-sdk/src/matrix";
2122
import {
2223
CallError,
2324
CallErrorCode,
@@ -29,7 +30,7 @@ import {
2930
} from "matrix-js-sdk/src/webrtc/call";
3031
import { logger } from "matrix-js-sdk/src/logger";
3132
import EventEmitter from "events";
32-
import { RuleId, TweakName, Tweaks } from "matrix-js-sdk/src/@types/PushRules";
33+
import { RuleId, TweakName } from "matrix-js-sdk/src/@types/PushRules";
3334
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
3435
import { SyncState } from "matrix-js-sdk/src/sync";
3536
import { CallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/callEventHandler";
@@ -65,6 +66,7 @@ import { getJoinedNonFunctionalMembers } from "./utils/room/getJoinedNonFunction
6566
import { localNotificationsAreSilenced } from "./utils/notifications";
6667
import { SdkContextClass } from "./contexts/SDKContext";
6768
import { showCantStartACallDialog } from "./voice-broadcast/utils/showCantStartACallDialog";
69+
import { isNotNull } from "./Typeguards";
6870

6971
export const PROTOCOL_PSTN = "m.protocol.pstn";
7072
export const PROTOCOL_PSTN_PREFIXED = "im.vector.protocol.pstn";
@@ -582,7 +584,9 @@ export default class LegacyCallHandler extends EventEmitter {
582584
call.on(CallEvent.Hangup, () => {
583585
if (!mappedRoomId || !this.matchesCallForThisRoom(call)) return;
584586

585-
this.removeCallForRoom(mappedRoomId);
587+
if (isNotNull(mappedRoomId)) {
588+
this.removeCallForRoom(mappedRoomId);
589+
}
586590
});
587591
call.on(CallEvent.State, (newState: CallState, oldState: CallState) => {
588592
this.onCallStateChanged(newState, oldState, call);
@@ -598,8 +602,10 @@ export default class LegacyCallHandler extends EventEmitter {
598602
this.pause(AudioID.Ringback);
599603
}
600604

601-
this.removeCallForRoom(mappedRoomId);
602-
this.addCallForRoom(mappedRoomId, newCall);
605+
if (isNotNull(mappedRoomId)) {
606+
this.removeCallForRoom(mappedRoomId);
607+
this.addCallForRoom(mappedRoomId, newCall);
608+
}
603609
this.setCallListeners(newCall);
604610
this.setCallState(newCall, newCall.state);
605611
});
@@ -635,7 +641,7 @@ export default class LegacyCallHandler extends EventEmitter {
635641

636642
const newMappedRoomId = this.roomIdForCall(call);
637643
logger.log(`Old room ID: ${mappedRoomId}, new room ID: ${newMappedRoomId}`);
638-
if (newMappedRoomId && newMappedRoomId !== mappedRoomId) {
644+
if (newMappedRoomId !== mappedRoomId && isNotNull(mappedRoomId) && isNotNull(newMappedRoomId)) {
639645
this.removeCallForRoom(mappedRoomId);
640646
mappedRoomId = newMappedRoomId;
641647
logger.log("Moving call to room " + mappedRoomId);
@@ -675,8 +681,11 @@ export default class LegacyCallHandler extends EventEmitter {
675681
RuleId.IncomingCall,
676682
);
677683
const pushRuleEnabled = incomingCallPushRule?.enabled;
684+
// actions can be either Tweaks | PushRuleActionName, ie an object or a string type enum
685+
// and we want to only run this check on the Tweaks
678686
const tweakSetToRing = incomingCallPushRule?.actions.some(
679-
(action: Tweaks) => action.set_tweak === TweakName.Sound && action.value === "ring",
687+
(action) =>
688+
typeof action !== "string" && action.set_tweak === TweakName.Sound && action.value === "ring",
680689
);
681690

682691
if (pushRuleEnabled && tweakSetToRing && !this.isForcedSilent()) {
@@ -692,7 +701,10 @@ export default class LegacyCallHandler extends EventEmitter {
692701
}
693702
case CallState.Ended: {
694703
const hangupReason = call.hangupReason;
695-
this.removeCallForRoom(mappedRoomId);
704+
if (isNotNull(mappedRoomId)) {
705+
this.removeCallForRoom(mappedRoomId);
706+
}
707+
696708
if (oldState === CallState.InviteSent && call.hangupParty === CallParty.Remote) {
697709
this.play(AudioID.Busy);
698710

@@ -724,7 +736,9 @@ export default class LegacyCallHandler extends EventEmitter {
724736
this.play(AudioID.CallEnd);
725737
}
726738

727-
this.logCallStats(call, mappedRoomId);
739+
if (isNotNull(mappedRoomId)) {
740+
this.logCallStats(call, mappedRoomId);
741+
}
728742
break;
729743
}
730744
}
@@ -1081,13 +1095,15 @@ export default class LegacyCallHandler extends EventEmitter {
10811095

10821096
const roomId = await ensureDMExists(MatrixClientPeg.get(), nativeUserId);
10831097

1084-
dis.dispatch<ViewRoomPayload>({
1085-
action: Action.ViewRoom,
1086-
room_id: roomId,
1087-
metricsTrigger: "WebDialPad",
1088-
});
1098+
if (isNotNull(roomId)) {
1099+
dis.dispatch<ViewRoomPayload>({
1100+
action: Action.ViewRoom,
1101+
room_id: roomId,
1102+
metricsTrigger: "WebDialPad",
1103+
});
10891104

1090-
await this.placeMatrixCall(roomId, CallType.Voice, transferee);
1105+
await this.placeMatrixCall(roomId, CallType.Voice, transferee);
1106+
}
10911107
}
10921108

10931109
public async startTransferToPhoneNumber(
@@ -1183,17 +1199,19 @@ export default class LegacyCallHandler extends EventEmitter {
11831199
// Prevent double clicking the call button
11841200
const widget = WidgetStore.instance.getApps(roomId).find((app) => WidgetType.JITSI.matches(app.type));
11851201
if (widget) {
1186-
const room = client.getRoom(roomId);
11871202
// If there already is a Jitsi widget, pin it
1188-
if (room) WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top);
1203+
const room = client.getRoom(roomId);
1204+
if (isNotNull(room)) {
1205+
WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top);
1206+
}
11891207
return;
11901208
}
11911209

11921210
try {
11931211
await WidgetUtils.addJitsiWidget(roomId, type, "Jitsi", false);
11941212
logger.log("Jitsi widget added");
11951213
} catch (e) {
1196-
if (e.errcode === "M_FORBIDDEN") {
1214+
if (e instanceof MatrixError && e.errcode === "M_FORBIDDEN") {
11971215
Modal.createDialog(ErrorDialog, {
11981216
title: _t("Permission Required"),
11991217
description: _t("You do not have permission to start a conference call in this room"),

src/Typeguards.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
export function isNotNull<T>(arg: T): arg is Exclude<T, null> {
18+
return arg !== null;
19+
}

0 commit comments

Comments
 (0)