Apply strictNullChecks to src/LegacyCallHandler.tsx (#10690)

This commit is contained in:
alunturner 2023-05-05 17:08:07 +01:00 committed by GitHub
parent f6f5d44bde
commit de16d347e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 17 deletions

View file

@ -18,6 +18,7 @@ limitations under the License.
*/
import React from "react";
import { MatrixError } from "matrix-js-sdk/src/matrix";
import {
CallError,
CallErrorCode,
@ -29,7 +30,7 @@ import {
} from "matrix-js-sdk/src/webrtc/call";
import { logger } from "matrix-js-sdk/src/logger";
import EventEmitter from "events";
import { RuleId, TweakName, Tweaks } from "matrix-js-sdk/src/@types/PushRules";
import { RuleId, TweakName } from "matrix-js-sdk/src/@types/PushRules";
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
import { SyncState } from "matrix-js-sdk/src/sync";
import { CallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/callEventHandler";
@ -65,6 +66,7 @@ import { getJoinedNonFunctionalMembers } from "./utils/room/getJoinedNonFunction
import { localNotificationsAreSilenced } from "./utils/notifications";
import { SdkContextClass } from "./contexts/SDKContext";
import { showCantStartACallDialog } from "./voice-broadcast/utils/showCantStartACallDialog";
import { isNotNull } from "./Typeguards";
export const PROTOCOL_PSTN = "m.protocol.pstn";
export const PROTOCOL_PSTN_PREFIXED = "im.vector.protocol.pstn";
@ -582,7 +584,9 @@ export default class LegacyCallHandler extends EventEmitter {
call.on(CallEvent.Hangup, () => {
if (!mappedRoomId || !this.matchesCallForThisRoom(call)) return;
this.removeCallForRoom(mappedRoomId);
if (isNotNull(mappedRoomId)) {
this.removeCallForRoom(mappedRoomId);
}
});
call.on(CallEvent.State, (newState: CallState, oldState: CallState) => {
this.onCallStateChanged(newState, oldState, call);
@ -598,8 +602,10 @@ export default class LegacyCallHandler extends EventEmitter {
this.pause(AudioID.Ringback);
}
this.removeCallForRoom(mappedRoomId);
this.addCallForRoom(mappedRoomId, newCall);
if (isNotNull(mappedRoomId)) {
this.removeCallForRoom(mappedRoomId);
this.addCallForRoom(mappedRoomId, newCall);
}
this.setCallListeners(newCall);
this.setCallState(newCall, newCall.state);
});
@ -635,7 +641,7 @@ export default class LegacyCallHandler extends EventEmitter {
const newMappedRoomId = this.roomIdForCall(call);
logger.log(`Old room ID: ${mappedRoomId}, new room ID: ${newMappedRoomId}`);
if (newMappedRoomId && newMappedRoomId !== mappedRoomId) {
if (newMappedRoomId !== mappedRoomId && isNotNull(mappedRoomId) && isNotNull(newMappedRoomId)) {
this.removeCallForRoom(mappedRoomId);
mappedRoomId = newMappedRoomId;
logger.log("Moving call to room " + mappedRoomId);
@ -675,8 +681,11 @@ export default class LegacyCallHandler extends EventEmitter {
RuleId.IncomingCall,
);
const pushRuleEnabled = incomingCallPushRule?.enabled;
// actions can be either Tweaks | PushRuleActionName, ie an object or a string type enum
// and we want to only run this check on the Tweaks
const tweakSetToRing = incomingCallPushRule?.actions.some(
(action: Tweaks) => action.set_tweak === TweakName.Sound && action.value === "ring",
(action) =>
typeof action !== "string" && action.set_tweak === TweakName.Sound && action.value === "ring",
);
if (pushRuleEnabled && tweakSetToRing && !this.isForcedSilent()) {
@ -692,7 +701,10 @@ export default class LegacyCallHandler extends EventEmitter {
}
case CallState.Ended: {
const hangupReason = call.hangupReason;
this.removeCallForRoom(mappedRoomId);
if (isNotNull(mappedRoomId)) {
this.removeCallForRoom(mappedRoomId);
}
if (oldState === CallState.InviteSent && call.hangupParty === CallParty.Remote) {
this.play(AudioID.Busy);
@ -724,7 +736,9 @@ export default class LegacyCallHandler extends EventEmitter {
this.play(AudioID.CallEnd);
}
this.logCallStats(call, mappedRoomId);
if (isNotNull(mappedRoomId)) {
this.logCallStats(call, mappedRoomId);
}
break;
}
}
@ -1081,13 +1095,15 @@ export default class LegacyCallHandler extends EventEmitter {
const roomId = await ensureDMExists(MatrixClientPeg.get(), nativeUserId);
dis.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: roomId,
metricsTrigger: "WebDialPad",
});
if (isNotNull(roomId)) {
dis.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: roomId,
metricsTrigger: "WebDialPad",
});
await this.placeMatrixCall(roomId, CallType.Voice, transferee);
await this.placeMatrixCall(roomId, CallType.Voice, transferee);
}
}
public async startTransferToPhoneNumber(
@ -1183,9 +1199,11 @@ export default class LegacyCallHandler extends EventEmitter {
// Prevent double clicking the call button
const widget = WidgetStore.instance.getApps(roomId).find((app) => WidgetType.JITSI.matches(app.type));
if (widget) {
const room = client.getRoom(roomId);
// If there already is a Jitsi widget, pin it
if (room) WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top);
const room = client.getRoom(roomId);
if (isNotNull(room)) {
WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top);
}
return;
}
@ -1193,7 +1211,7 @@ export default class LegacyCallHandler extends EventEmitter {
await WidgetUtils.addJitsiWidget(roomId, type, "Jitsi", false);
logger.log("Jitsi widget added");
} catch (e) {
if (e.errcode === "M_FORBIDDEN") {
if (e instanceof MatrixError && e.errcode === "M_FORBIDDEN") {
Modal.createDialog(ErrorDialog, {
title: _t("Permission Required"),
description: _t("You do not have permission to start a conference call in this room"),