2021-04-23 21:39:39 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-04-28 01:56:22 +08:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-04-23 21:39:39 +08:00
|
|
|
*/
|
|
|
|
|
2022-10-17 17:16:04 +08:00
|
|
|
import {
|
|
|
|
IProtocol,
|
|
|
|
LOCAL_NOTIFICATION_SETTINGS_PREFIX,
|
|
|
|
MatrixEvent,
|
|
|
|
PushRuleKind,
|
2022-12-19 16:44:19 +08:00
|
|
|
Room,
|
2022-10-17 17:16:04 +08:00
|
|
|
RuleId,
|
|
|
|
TweakName,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-10-17 17:16:04 +08:00
|
|
|
import { CallEvent, CallState, CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call";
|
2021-12-09 17:10:23 +08:00
|
|
|
import EventEmitter from "events";
|
2022-08-02 01:28:33 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2022-10-17 17:16:04 +08:00
|
|
|
import { CallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/callEventHandler";
|
2024-07-05 02:08:06 +08:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { waitFor } from "jest-matrix-react";
|
2021-12-09 17:10:23 +08:00
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
import LegacyCallHandler, {
|
2022-12-01 12:08:09 +08:00
|
|
|
AudioID,
|
2024-07-05 02:08:06 +08:00
|
|
|
LegacyCallHandlerEvent,
|
2022-12-01 12:08:09 +08:00
|
|
|
PROTOCOL_PSTN,
|
|
|
|
PROTOCOL_PSTN_PREFIXED,
|
|
|
|
PROTOCOL_SIP_NATIVE,
|
|
|
|
PROTOCOL_SIP_VIRTUAL,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../src/LegacyCallHandler";
|
|
|
|
import { mkStubRoom, stubClient, untilDispatch } from "../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
|
|
|
import SdkConfig from "../../src/SdkConfig";
|
|
|
|
import { Action } from "../../src/dispatcher/actions";
|
|
|
|
import { getFunctionalMembers } from "../../src/utils/room/getFunctionalMembers";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { UIFeature } from "../../src/settings/UIFeature";
|
|
|
|
import { VoiceBroadcastInfoState, VoiceBroadcastPlayback, VoiceBroadcastRecording } from "../../src/voice-broadcast";
|
2022-12-19 16:44:19 +08:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "./voice-broadcast/utils/test-utils";
|
2024-10-15 21:57:26 +08:00
|
|
|
import { SdkContextClass } from "../../src/contexts/SDKContext";
|
|
|
|
import Modal from "../../src/Modal";
|
|
|
|
import { createAudioContext } from "../../src/audio/compat";
|
|
|
|
import * as ManagedHybrid from "../../src/widgets/ManagedHybrid";
|
2022-12-19 16:44:19 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/Modal");
|
2022-12-19 16:44:19 +08:00
|
|
|
|
|
|
|
// mock VoiceRecording because it contains all the audio APIs
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/audio/VoiceRecording", () => ({
|
2022-12-19 16:44:19 +08:00
|
|
|
VoiceRecording: jest.fn().mockReturnValue({
|
|
|
|
disableMaxLength: jest.fn(),
|
|
|
|
liveData: {
|
|
|
|
onUpdate: jest.fn(),
|
|
|
|
},
|
|
|
|
off: jest.fn(),
|
|
|
|
on: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
stop: jest.fn(),
|
|
|
|
destroy: jest.fn(),
|
|
|
|
contentType: "audio/ogg",
|
|
|
|
}),
|
|
|
|
}));
|
2022-08-02 01:28:33 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/utils/room/getFunctionalMembers", () => ({
|
2022-08-02 01:28:33 +08:00
|
|
|
getFunctionalMembers: jest.fn(),
|
|
|
|
}));
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/audio/compat", () => ({
|
|
|
|
...jest.requireActual("../../src/audio/compat"),
|
2024-07-05 02:08:06 +08:00
|
|
|
createAudioContext: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// The Matrix IDs that the user sees when talking to Alice & Bob
|
|
|
|
const NATIVE_ALICE = "@alice:example.org";
|
|
|
|
const NATIVE_BOB = "@bob:example.org";
|
|
|
|
const NATIVE_CHARLIE = "@charlie:example.org";
|
|
|
|
|
|
|
|
// Virtual user for Bob
|
|
|
|
const VIRTUAL_BOB = "@virtual_bob:example.org";
|
|
|
|
|
|
|
|
//const REAL_ROOM_ID = "$room1:example.org";
|
|
|
|
// The rooms the user sees when they're communicating with these users
|
|
|
|
const NATIVE_ROOM_ALICE = "$alice_room:example.org";
|
|
|
|
const NATIVE_ROOM_BOB = "$bob_room:example.org";
|
|
|
|
const NATIVE_ROOM_CHARLIE = "$charlie_room:example.org";
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
const FUNCTIONAL_USER = "@bot:example.com";
|
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// The room we use to talk to virtual Bob (but that the user does not see)
|
|
|
|
// Bob has a virtual room, but Alice doesn't
|
|
|
|
const VIRTUAL_ROOM_BOB = "$virtual_bob_room:example.org";
|
|
|
|
|
|
|
|
// Bob's phone number
|
|
|
|
const BOB_PHONE_NUMBER = "01818118181";
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2022-12-19 16:44:19 +08:00
|
|
|
function mkStubDM(roomId: string, userId: string) {
|
2023-06-06 01:12:23 +08:00
|
|
|
const room = mkStubRoom(roomId, "room", MatrixClientPeg.safeGet());
|
2021-04-23 21:39:39 +08:00
|
|
|
room.getJoinedMembers = jest.fn().mockReturnValue([
|
|
|
|
{
|
|
|
|
userId: "@me:example.org",
|
|
|
|
name: "Member",
|
|
|
|
rawDisplayName: "Member",
|
|
|
|
roomId: roomId,
|
2024-03-12 22:52:54 +08:00
|
|
|
membership: KnownMembership.Join,
|
2021-04-23 21:39:39 +08:00
|
|
|
getAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
getMxcAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: userId,
|
|
|
|
name: "Member",
|
|
|
|
rawDisplayName: "Member",
|
|
|
|
roomId: roomId,
|
2024-03-12 22:52:54 +08:00
|
|
|
membership: KnownMembership.Join,
|
2021-04-23 21:39:39 +08:00
|
|
|
getAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
getMxcAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
},
|
2022-08-02 01:28:33 +08:00
|
|
|
{
|
|
|
|
userId: FUNCTIONAL_USER,
|
|
|
|
name: "Bot user",
|
|
|
|
rawDisplayName: "Bot user",
|
|
|
|
roomId: roomId,
|
2024-03-12 22:52:54 +08:00
|
|
|
membership: KnownMembership.Join,
|
2022-08-02 01:28:33 +08:00
|
|
|
getAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
getMxcAvatarUrl: () => "mxc://avatar.url/image.png",
|
|
|
|
},
|
2021-04-23 21:39:39 +08:00
|
|
|
]);
|
|
|
|
room.currentState.getMembers = room.getJoinedMembers;
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FakeCall extends EventEmitter {
|
|
|
|
roomId: string;
|
|
|
|
callId = "fake call id";
|
|
|
|
|
2022-07-01 20:43:42 +08:00
|
|
|
constructor(roomId: string) {
|
2021-04-23 21:39:39 +08:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.roomId = roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRemoteOnHold() {}
|
|
|
|
setRemoteAudioElement() {}
|
|
|
|
|
|
|
|
placeVoiceCall() {
|
|
|
|
this.emit(CallEvent.State, CallState.Connected, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
function untilCallHandlerEvent(callHandler: LegacyCallHandler, event: LegacyCallHandlerEvent): Promise<void> {
|
2021-12-01 02:09:13 +08:00
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
callHandler.addListener(event, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
describe("LegacyCallHandler", () => {
|
2021-04-23 21:39:39 +08:00
|
|
|
let dmRoomMap;
|
2022-12-19 16:44:19 +08:00
|
|
|
let callHandler: LegacyCallHandler;
|
2022-07-01 20:43:42 +08:00
|
|
|
let audioElement: HTMLAudioElement;
|
2022-12-19 16:44:19 +08:00
|
|
|
let fakeCall: MatrixCall | null;
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// what addresses the app has looked up via pstn and native lookup
|
2022-12-19 16:44:19 +08:00
|
|
|
let pstnLookup: string | null;
|
|
|
|
let nativeLookup: string | null;
|
2022-10-17 17:16:04 +08:00
|
|
|
const deviceId = "my-device";
|
2022-02-21 20:17:09 +08:00
|
|
|
|
2022-07-01 20:43:42 +08:00
|
|
|
beforeEach(async () => {
|
2021-04-23 21:39:39 +08:00
|
|
|
stubClient();
|
2022-12-19 16:44:19 +08:00
|
|
|
fakeCall = null;
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().createCall = (roomId: string): MatrixCall | null => {
|
2021-04-23 21:39:39 +08:00
|
|
|
if (fakeCall && fakeCall.roomId !== roomId) {
|
|
|
|
throw new Error("Only one call is supported!");
|
|
|
|
}
|
2022-12-19 16:44:19 +08:00
|
|
|
fakeCall = new FakeCall(roomId) as unknown as MatrixCall;
|
|
|
|
return fakeCall as unknown as MatrixCall;
|
2021-04-23 21:39:39 +08:00
|
|
|
};
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().deviceId = deviceId;
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThirdpartyProtocols = () => {
|
2022-02-21 20:17:09 +08:00
|
|
|
return Promise.resolve({
|
|
|
|
"m.id.phone": {} as IProtocol,
|
|
|
|
"im.vector.protocol.sip_native": {} as IProtocol,
|
|
|
|
"im.vector.protocol.sip_virtual": {} as IProtocol,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
callHandler = new LegacyCallHandler();
|
2021-04-23 21:39:39 +08:00
|
|
|
callHandler.start();
|
|
|
|
|
2022-08-02 01:28:33 +08:00
|
|
|
mocked(getFunctionalMembers).mockReturnValue([FUNCTIONAL_USER]);
|
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
const nativeRoomAlice = mkStubDM(NATIVE_ROOM_ALICE, NATIVE_ALICE);
|
|
|
|
const nativeRoomBob = mkStubDM(NATIVE_ROOM_BOB, NATIVE_BOB);
|
|
|
|
const nativeRoomCharie = mkStubDM(NATIVE_ROOM_CHARLIE, NATIVE_CHARLIE);
|
|
|
|
const virtualBobRoom = mkStubDM(VIRTUAL_ROOM_BOB, VIRTUAL_BOB);
|
2021-06-03 00:39:13 +08:00
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getRoom = (roomId: string): Room | null => {
|
2021-06-03 00:39:13 +08:00
|
|
|
switch (roomId) {
|
2022-02-21 20:17:09 +08:00
|
|
|
case NATIVE_ROOM_ALICE:
|
|
|
|
return nativeRoomAlice;
|
|
|
|
case NATIVE_ROOM_BOB:
|
|
|
|
return nativeRoomBob;
|
|
|
|
case NATIVE_ROOM_CHARLIE:
|
|
|
|
return nativeRoomCharie;
|
|
|
|
case VIRTUAL_ROOM_BOB:
|
|
|
|
return virtualBobRoom;
|
2021-06-03 00:39:13 +08:00
|
|
|
}
|
2022-12-19 16:44:19 +08:00
|
|
|
|
|
|
|
return null;
|
2021-06-03 00:39:13 +08:00
|
|
|
};
|
|
|
|
|
2021-04-23 21:39:39 +08:00
|
|
|
dmRoomMap = {
|
2022-07-01 20:43:42 +08:00
|
|
|
getUserIdForRoomId: (roomId: string) => {
|
2022-02-21 20:17:09 +08:00
|
|
|
if (roomId === NATIVE_ROOM_ALICE) {
|
|
|
|
return NATIVE_ALICE;
|
|
|
|
} else if (roomId === NATIVE_ROOM_BOB) {
|
|
|
|
return NATIVE_BOB;
|
|
|
|
} else if (roomId === NATIVE_ROOM_CHARLIE) {
|
|
|
|
return NATIVE_CHARLIE;
|
|
|
|
} else if (roomId === VIRTUAL_ROOM_BOB) {
|
|
|
|
return VIRTUAL_BOB;
|
2021-04-23 21:39:39 +08:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2022-07-01 20:43:42 +08:00
|
|
|
getDMRoomsForUserId: (userId: string) => {
|
2022-02-21 20:17:09 +08:00
|
|
|
if (userId === NATIVE_ALICE) {
|
|
|
|
return [NATIVE_ROOM_ALICE];
|
|
|
|
} else if (userId === NATIVE_BOB) {
|
|
|
|
return [NATIVE_ROOM_BOB];
|
|
|
|
} else if (userId === NATIVE_CHARLIE) {
|
|
|
|
return [NATIVE_ROOM_CHARLIE];
|
|
|
|
} else if (userId === VIRTUAL_BOB) {
|
|
|
|
return [VIRTUAL_ROOM_BOB];
|
2021-04-23 21:39:39 +08:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
2023-02-13 15:46:53 +08:00
|
|
|
} as unknown as DMRoomMap;
|
2021-04-23 21:39:39 +08:00
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
pstnLookup = null;
|
|
|
|
nativeLookup = null;
|
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThirdpartyUser = (proto: string, params: any) => {
|
2022-02-21 20:17:09 +08:00
|
|
|
if ([PROTOCOL_PSTN, PROTOCOL_PSTN_PREFIXED].includes(proto)) {
|
|
|
|
pstnLookup = params["m.id.phone"];
|
|
|
|
return Promise.resolve([
|
|
|
|
{
|
|
|
|
userid: VIRTUAL_BOB,
|
|
|
|
protocol: "m.id.phone",
|
|
|
|
fields: {
|
|
|
|
is_native: true,
|
|
|
|
lookup_success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
} else if (proto === PROTOCOL_SIP_NATIVE) {
|
|
|
|
nativeLookup = params["virtual_mxid"];
|
|
|
|
if (params["virtual_mxid"] === VIRTUAL_BOB) {
|
|
|
|
return Promise.resolve([
|
2022-12-12 19:24:14 +08:00
|
|
|
{
|
2022-02-21 20:17:09 +08:00
|
|
|
userid: NATIVE_BOB,
|
|
|
|
protocol: "im.vector.protocol.sip_native",
|
|
|
|
fields: {
|
|
|
|
is_native: true,
|
|
|
|
lookup_success: true,
|
2022-12-12 19:24:14 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2022-02-21 20:17:09 +08:00
|
|
|
}
|
|
|
|
return Promise.resolve([]);
|
|
|
|
} else if (proto === PROTOCOL_SIP_VIRTUAL) {
|
|
|
|
if (params["native_mxid"] === NATIVE_BOB) {
|
|
|
|
return Promise.resolve([
|
|
|
|
{
|
|
|
|
userid: VIRTUAL_BOB,
|
|
|
|
protocol: "im.vector.protocol.sip_virtual",
|
|
|
|
fields: {
|
|
|
|
is_virtual: true,
|
|
|
|
lookup_success: true,
|
2022-12-12 19:24:14 +08:00
|
|
|
},
|
2022-02-21 20:17:09 +08:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2022-12-19 16:44:19 +08:00
|
|
|
|
|
|
|
return Promise.resolve([]);
|
2022-02-21 20:17:09 +08:00
|
|
|
};
|
|
|
|
|
2021-04-23 21:39:39 +08:00
|
|
|
audioElement = document.createElement("audio");
|
|
|
|
audioElement.id = "remoteAudio";
|
|
|
|
document.body.appendChild(audioElement);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
callHandler.stop();
|
2022-12-19 16:44:19 +08:00
|
|
|
// @ts-ignore
|
2021-04-23 21:39:39 +08:00
|
|
|
DMRoomMap.setShared(null);
|
|
|
|
// @ts-ignore
|
2022-08-31 03:13:39 +08:00
|
|
|
window.mxLegacyCallHandler = null;
|
2021-04-23 21:39:39 +08:00
|
|
|
MatrixClientPeg.unset();
|
|
|
|
|
|
|
|
document.body.removeChild(audioElement);
|
2023-04-26 17:36:00 +08:00
|
|
|
SdkConfig.reset();
|
2024-07-15 17:08:34 +08:00
|
|
|
jest.restoreAllMocks();
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
|
|
|
|
2021-08-04 16:46:39 +08:00
|
|
|
it("should look up the correct user and start a call in the room when a phone number is dialled", async () => {
|
2022-02-21 20:17:09 +08:00
|
|
|
await callHandler.dialNumber(BOB_PHONE_NUMBER);
|
|
|
|
|
|
|
|
expect(pstnLookup).toEqual(BOB_PHONE_NUMBER);
|
|
|
|
expect(nativeLookup).toEqual(VIRTUAL_BOB);
|
|
|
|
|
|
|
|
// we should have switched to the native room for Bob
|
|
|
|
const viewRoomPayload = await untilDispatch(Action.ViewRoom);
|
|
|
|
expect(viewRoomPayload.room_id).toEqual(NATIVE_ROOM_BOB);
|
|
|
|
|
|
|
|
// Check that a call was started: its room on the protocol level
|
|
|
|
// should be the virtual room
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(fakeCall).not.toBeNull();
|
|
|
|
expect(fakeCall?.roomId).toEqual(VIRTUAL_ROOM_BOB);
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// but it should appear to the user to be in thw native room for Bob
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(callHandler.roomIdForCall(fakeCall!)).toEqual(NATIVE_ROOM_BOB);
|
2022-02-21 20:17:09 +08:00
|
|
|
});
|
2021-06-03 00:39:13 +08:00
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
it("should look up the correct user and start a call in the room when a call is transferred", async () => {
|
|
|
|
// we can pass a very minimal object as as the call since we pass consultFirst=true:
|
|
|
|
// we don't need to actually do any transferring
|
2022-12-19 16:44:19 +08:00
|
|
|
const mockTransferreeCall = { type: CallType.Voice } as unknown as MatrixCall;
|
2022-02-21 20:17:09 +08:00
|
|
|
await callHandler.startTransferToPhoneNumber(mockTransferreeCall, BOB_PHONE_NUMBER, true);
|
|
|
|
|
|
|
|
// same checks as above
|
2021-11-26 04:49:43 +08:00
|
|
|
const viewRoomPayload = await untilDispatch(Action.ViewRoom);
|
2022-02-21 20:17:09 +08:00
|
|
|
expect(viewRoomPayload.room_id).toEqual(NATIVE_ROOM_BOB);
|
|
|
|
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(fakeCall).not.toBeNull();
|
|
|
|
expect(fakeCall!.roomId).toEqual(VIRTUAL_ROOM_BOB);
|
2021-08-04 16:46:39 +08:00
|
|
|
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(callHandler.roomIdForCall(fakeCall!)).toEqual(NATIVE_ROOM_BOB);
|
2021-06-03 00:39:13 +08:00
|
|
|
});
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2021-06-03 00:39:13 +08:00
|
|
|
it("should move calls between rooms when remote asserted identity changes", async () => {
|
2022-02-21 20:17:09 +08:00
|
|
|
callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);
|
2021-12-01 02:09:13 +08:00
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
await untilCallHandlerEvent(callHandler, LegacyCallHandlerEvent.CallState);
|
2021-04-23 21:39:39 +08:00
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// We placed the call in Alice's room so it should start off there
|
|
|
|
expect(callHandler.getCallForRoom(NATIVE_ROOM_ALICE)).toBe(fakeCall);
|
2021-04-23 21:39:39 +08:00
|
|
|
|
|
|
|
let callRoomChangeEventCount = 0;
|
|
|
|
const roomChangePromise = new Promise<void>((resolve) => {
|
2022-08-31 03:13:39 +08:00
|
|
|
callHandler.addListener(LegacyCallHandlerEvent.CallChangeRoom, () => {
|
2021-04-28 17:49:07 +08:00
|
|
|
++callRoomChangeEventCount;
|
|
|
|
resolve();
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-21 20:17:09 +08:00
|
|
|
// Now emit an asserted identity for Bob: this should be ignored
|
2021-04-23 21:39:39 +08:00
|
|
|
// because we haven't set the config option to obey asserted identity
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(fakeCall).not.toBeNull();
|
|
|
|
fakeCall!.getRemoteAssertedIdentity = jest.fn().mockReturnValue({
|
2022-02-21 20:17:09 +08:00
|
|
|
id: NATIVE_BOB,
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
2023-04-04 18:41:46 +08:00
|
|
|
fakeCall!.emit(CallEvent.AssertedIdentityChanged, fakeCall!);
|
2021-04-23 21:39:39 +08:00
|
|
|
|
|
|
|
// Now set the config option
|
2022-03-23 18:27:28 +08:00
|
|
|
SdkConfig.add({
|
2021-04-28 02:33:53 +08:00
|
|
|
voip: {
|
2022-03-23 18:27:28 +08:00
|
|
|
obey_asserted_identity: true,
|
2021-04-28 02:33:53 +08:00
|
|
|
},
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// ...and send another asserted identity event for a different user
|
2022-12-19 16:44:19 +08:00
|
|
|
fakeCall!.getRemoteAssertedIdentity = jest.fn().mockReturnValue({
|
2022-02-21 20:17:09 +08:00
|
|
|
id: NATIVE_CHARLIE,
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
2023-04-04 18:41:46 +08:00
|
|
|
fakeCall!.emit(CallEvent.AssertedIdentityChanged, fakeCall!);
|
2021-04-23 21:39:39 +08:00
|
|
|
|
|
|
|
await roomChangePromise;
|
2021-04-28 19:33:15 +08:00
|
|
|
callHandler.removeAllListeners();
|
2021-04-23 21:39:39 +08:00
|
|
|
|
|
|
|
// If everything's gone well, we should have seen only one room change
|
2022-02-21 20:17:09 +08:00
|
|
|
// event and the call should now be in Charlie's room.
|
|
|
|
// If it's not obeying any, the call will still be in NATIVE_ROOM_ALICE.
|
2021-04-23 21:39:39 +08:00
|
|
|
// If it incorrectly obeyed both asserted identity changes, either it will
|
|
|
|
// have just processed one and the call will be in the wrong room, or we'll
|
|
|
|
// have seen two room change dispatches.
|
|
|
|
expect(callRoomChangeEventCount).toEqual(1);
|
2022-02-21 20:17:09 +08:00
|
|
|
expect(callHandler.getCallForRoom(NATIVE_ROOM_BOB)).toBeNull();
|
|
|
|
expect(callHandler.getCallForRoom(NATIVE_ROOM_CHARLIE)).toBe(fakeCall);
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
2022-12-19 16:44:19 +08:00
|
|
|
|
2024-07-15 17:08:34 +08:00
|
|
|
it("should place calls using managed hybrid widget if enabled", async () => {
|
|
|
|
const spy = jest.spyOn(ManagedHybrid, "addManagedHybridWidget");
|
|
|
|
jest.spyOn(ManagedHybrid, "isManagedHybridWidgetEnabled").mockReturnValue(true);
|
|
|
|
await callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);
|
|
|
|
expect(spy).toHaveBeenCalledWith(MatrixClientPeg.safeGet().getRoom(NATIVE_ROOM_ALICE));
|
|
|
|
});
|
|
|
|
|
2022-12-19 16:44:19 +08:00
|
|
|
describe("when listening to a voice broadcast", () => {
|
|
|
|
let voiceBroadcastPlayback: VoiceBroadcastPlayback;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastPlayback = new VoiceBroadcastPlayback(
|
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
"!room:example.com",
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getSafeUserId(),
|
2022-12-19 16:44:19 +08:00
|
|
|
"d42",
|
|
|
|
),
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet(),
|
2023-01-02 20:21:33 +08:00
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore,
|
2022-12-19 16:44:19 +08:00
|
|
|
);
|
|
|
|
SdkContextClass.instance.voiceBroadcastPlaybacksStore.setCurrent(voiceBroadcastPlayback);
|
|
|
|
jest.spyOn(voiceBroadcastPlayback, "pause").mockImplementation();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and placing a call should pause the broadcast", async () => {
|
|
|
|
callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);
|
|
|
|
await untilCallHandlerEvent(callHandler, LegacyCallHandlerEvent.CallState);
|
|
|
|
|
|
|
|
expect(voiceBroadcastPlayback.pause).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when recording a voice broadcast", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore.setCurrent(
|
|
|
|
new VoiceBroadcastRecording(
|
|
|
|
mkVoiceBroadcastInfoStateEvent(
|
|
|
|
"!room:example.com",
|
|
|
|
VoiceBroadcastInfoState.Started,
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getSafeUserId(),
|
2022-12-19 16:44:19 +08:00
|
|
|
"d42",
|
|
|
|
),
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet(),
|
2022-12-19 16:44:19 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and placing a call should show the info dialog", async () => {
|
|
|
|
callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);
|
|
|
|
expect(Modal.createDialog).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2021-04-23 21:39:39 +08:00
|
|
|
});
|
2022-07-01 20:43:42 +08:00
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
describe("LegacyCallHandler without third party protocols", () => {
|
2022-07-01 20:43:42 +08:00
|
|
|
let dmRoomMap;
|
2022-08-31 03:13:39 +08:00
|
|
|
let callHandler: LegacyCallHandler;
|
2022-07-01 20:43:42 +08:00
|
|
|
let audioElement: HTMLAudioElement;
|
2022-12-19 16:44:19 +08:00
|
|
|
let fakeCall: MatrixCall | null;
|
2022-07-01 20:43:42 +08:00
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
const mockAudioBufferSourceNode = {
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
connect: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
stop: jest.fn(),
|
|
|
|
};
|
|
|
|
const mockAudioContext = {
|
|
|
|
decodeAudioData: jest.fn().mockResolvedValue({}),
|
|
|
|
suspend: jest.fn(),
|
|
|
|
resume: jest.fn(),
|
|
|
|
createBufferSource: jest.fn().mockReturnValue(mockAudioBufferSourceNode),
|
|
|
|
currentTime: 1337,
|
|
|
|
};
|
|
|
|
|
2022-07-01 20:43:42 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2022-12-19 16:44:19 +08:00
|
|
|
fakeCall = null;
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().createCall = (roomId) => {
|
2022-07-01 20:43:42 +08:00
|
|
|
if (fakeCall && fakeCall.roomId !== roomId) {
|
|
|
|
throw new Error("Only one call is supported!");
|
|
|
|
}
|
2022-12-19 16:44:19 +08:00
|
|
|
fakeCall = new FakeCall(roomId) as unknown as MatrixCall;
|
2022-07-01 20:43:42 +08:00
|
|
|
return fakeCall;
|
|
|
|
};
|
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThirdpartyProtocols = () => {
|
2022-07-01 20:43:42 +08:00
|
|
|
throw new Error("Endpoint unsupported.");
|
|
|
|
};
|
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
mocked(createAudioContext).mockReturnValue(mockAudioContext as unknown as AudioContext);
|
2022-08-31 03:13:39 +08:00
|
|
|
callHandler = new LegacyCallHandler();
|
2022-07-01 20:43:42 +08:00
|
|
|
callHandler.start();
|
|
|
|
|
|
|
|
const nativeRoomAlice = mkStubDM(NATIVE_ROOM_ALICE, NATIVE_ALICE);
|
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getRoom = (roomId: string): Room | null => {
|
2022-07-01 20:43:42 +08:00
|
|
|
switch (roomId) {
|
|
|
|
case NATIVE_ROOM_ALICE:
|
|
|
|
return nativeRoomAlice;
|
|
|
|
}
|
2022-12-19 16:44:19 +08:00
|
|
|
|
|
|
|
return null;
|
2022-07-01 20:43:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
dmRoomMap = {
|
|
|
|
getUserIdForRoomId: (roomId: string) => {
|
|
|
|
if (roomId === NATIVE_ROOM_ALICE) {
|
|
|
|
return NATIVE_ALICE;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getDMRoomsForUserId: (userId: string) => {
|
|
|
|
if (userId === NATIVE_ALICE) {
|
|
|
|
return [NATIVE_ROOM_ALICE];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
2022-12-19 16:44:19 +08:00
|
|
|
} as DMRoomMap;
|
2022-07-01 20:43:42 +08:00
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThirdpartyUser = (_proto, _params) => {
|
2022-07-01 20:43:42 +08:00
|
|
|
throw new Error("Endpoint unsupported.");
|
|
|
|
};
|
|
|
|
|
|
|
|
audioElement = document.createElement("audio");
|
|
|
|
audioElement.id = "remoteAudio";
|
|
|
|
document.body.appendChild(audioElement);
|
2022-12-19 16:44:19 +08:00
|
|
|
|
|
|
|
SdkContextClass.instance.voiceBroadcastPlaybacksStore.clearCurrent();
|
|
|
|
SdkContextClass.instance.voiceBroadcastRecordingsStore.clearCurrent();
|
2024-07-05 02:08:06 +08:00
|
|
|
|
|
|
|
fetchMock.get(
|
|
|
|
"/media/ring.mp3",
|
|
|
|
{ body: new Blob(["1", "2", "3", "4"], { type: "audio/mpeg" }) },
|
|
|
|
{ sendAsJson: false },
|
|
|
|
);
|
2022-07-01 20:43:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
callHandler.stop();
|
2022-12-19 16:44:19 +08:00
|
|
|
// @ts-ignore
|
2022-07-01 20:43:42 +08:00
|
|
|
DMRoomMap.setShared(null);
|
|
|
|
// @ts-ignore
|
2022-08-31 03:13:39 +08:00
|
|
|
window.mxLegacyCallHandler = null;
|
2022-07-01 20:43:42 +08:00
|
|
|
MatrixClientPeg.unset();
|
|
|
|
|
|
|
|
document.body.removeChild(audioElement);
|
2023-04-26 17:36:00 +08:00
|
|
|
SdkConfig.reset();
|
2022-07-01 20:43:42 +08:00
|
|
|
});
|
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
it("should cache sounds between playbacks", async () => {
|
|
|
|
await callHandler.play(AudioID.Ring);
|
|
|
|
expect(mockAudioBufferSourceNode.start).toHaveBeenCalled();
|
|
|
|
expect(fetchMock.calls("/media/ring.mp3")).toHaveLength(1);
|
|
|
|
await callHandler.play(AudioID.Ring);
|
|
|
|
expect(fetchMock.calls("/media/ring.mp3")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow silencing an incoming call ring", async () => {
|
|
|
|
await callHandler.play(AudioID.Ring);
|
|
|
|
await callHandler.silenceCall("call123");
|
|
|
|
expect(mockAudioBufferSourceNode.stop).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-07-01 20:43:42 +08:00
|
|
|
it("should still start a native call", async () => {
|
|
|
|
callHandler.placeCall(NATIVE_ROOM_ALICE, CallType.Voice);
|
|
|
|
|
2022-08-31 03:13:39 +08:00
|
|
|
await untilCallHandlerEvent(callHandler, LegacyCallHandlerEvent.CallState);
|
2022-07-01 20:43:42 +08:00
|
|
|
|
|
|
|
// Check that a call was started: its room on the protocol level
|
|
|
|
// should be the virtual room
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(fakeCall).not.toBeNull();
|
|
|
|
expect(fakeCall!.roomId).toEqual(NATIVE_ROOM_ALICE);
|
2022-07-01 20:43:42 +08:00
|
|
|
|
|
|
|
// but it should appear to the user to be in thw native room for Bob
|
2022-12-19 16:44:19 +08:00
|
|
|
expect(callHandler.roomIdForCall(fakeCall!)).toEqual(NATIVE_ROOM_ALICE);
|
2022-07-01 20:43:42 +08:00
|
|
|
});
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
describe("incoming calls", () => {
|
|
|
|
const roomId = "test-room-id";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === UIFeature.Voip);
|
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
jest.spyOn(MatrixClientPeg.safeGet(), "supportsVoip").mockReturnValue(true);
|
2022-10-17 17:16:04 +08:00
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().isFallbackICEServerAllowed = jest.fn();
|
2022-10-17 17:16:04 +08:00
|
|
|
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().pushRules = {
|
2022-10-17 17:16:04 +08:00
|
|
|
global: {
|
|
|
|
[PushRuleKind.Override]: [
|
|
|
|
{
|
|
|
|
rule_id: RuleId.IncomingCall,
|
|
|
|
default: false,
|
|
|
|
enabled: true,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
set_tweak: TweakName.Sound,
|
|
|
|
value: "ring",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// silence local notifications by default
|
2023-06-06 01:12:23 +08:00
|
|
|
jest.spyOn(MatrixClientPeg.safeGet(), "getAccountData").mockImplementation((eventType) => {
|
2022-10-17 17:16:04 +08:00
|
|
|
if (eventType.includes(LOCAL_NOTIFICATION_SETTINGS_PREFIX.name)) {
|
|
|
|
return new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
content: {
|
|
|
|
is_silenced: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("listens for incoming call events when voip is enabled", () => {
|
|
|
|
const call = new MatrixCall({
|
2023-06-06 01:12:23 +08:00
|
|
|
client: MatrixClientPeg.safeGet(),
|
2022-10-17 17:16:04 +08:00
|
|
|
roomId,
|
|
|
|
});
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
cli.emit(CallEventHandlerEvent.Incoming, call);
|
|
|
|
|
|
|
|
// call added to call map
|
|
|
|
expect(callHandler.getCallForRoom(roomId)).toEqual(call);
|
|
|
|
});
|
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
it("rings when incoming call state is ringing and notifications set to ring", async () => {
|
2022-10-17 17:16:04 +08:00
|
|
|
// remove local notification silencing mock for this test
|
2023-06-06 01:12:23 +08:00
|
|
|
jest.spyOn(MatrixClientPeg.safeGet(), "getAccountData").mockReturnValue(undefined);
|
2022-10-17 17:16:04 +08:00
|
|
|
const call = new MatrixCall({
|
2023-06-06 01:12:23 +08:00
|
|
|
client: MatrixClientPeg.safeGet(),
|
2022-10-17 17:16:04 +08:00
|
|
|
roomId,
|
|
|
|
});
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
cli.emit(CallEventHandlerEvent.Incoming, call);
|
|
|
|
|
|
|
|
// call added to call map
|
|
|
|
expect(callHandler.getCallForRoom(roomId)).toEqual(call);
|
2023-04-04 18:41:46 +08:00
|
|
|
call.emit(CallEvent.State, CallState.Ringing, CallState.Connected, fakeCall!);
|
2022-10-17 17:16:04 +08:00
|
|
|
|
2024-07-05 02:08:06 +08:00
|
|
|
// ringer audio started
|
|
|
|
await waitFor(() => expect(mockAudioBufferSourceNode.start).toHaveBeenCalled());
|
2022-10-17 17:16:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does not ring when incoming call state is ringing but local notifications are silenced", () => {
|
|
|
|
const call = new MatrixCall({
|
2023-06-06 01:12:23 +08:00
|
|
|
client: MatrixClientPeg.safeGet(),
|
2022-10-17 17:16:04 +08:00
|
|
|
roomId,
|
|
|
|
});
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
cli.emit(CallEventHandlerEvent.Incoming, call);
|
|
|
|
|
|
|
|
// call added to call map
|
|
|
|
expect(callHandler.getCallForRoom(roomId)).toEqual(call);
|
2023-04-04 18:41:46 +08:00
|
|
|
call.emit(CallEvent.State, CallState.Ringing, CallState.Connected, fakeCall!);
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
// ringer audio element started
|
2024-07-05 02:08:06 +08:00
|
|
|
expect(mockAudioBufferSourceNode.start).not.toHaveBeenCalled();
|
2022-10-17 17:16:04 +08:00
|
|
|
expect(callHandler.isCallSilenced(call.callId)).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should force calls to silent when local notifications are silenced", async () => {
|
|
|
|
const call = new MatrixCall({
|
2023-06-06 01:12:23 +08:00
|
|
|
client: MatrixClientPeg.safeGet(),
|
2022-10-17 17:16:04 +08:00
|
|
|
roomId,
|
|
|
|
});
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-10-17 17:16:04 +08:00
|
|
|
|
|
|
|
cli.emit(CallEventHandlerEvent.Incoming, call);
|
|
|
|
|
|
|
|
expect(callHandler.isForcedSilent()).toEqual(true);
|
|
|
|
expect(callHandler.isCallSilenced(call.callId)).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not unsilence calls when local notifications are silenced", async () => {
|
|
|
|
const call = new MatrixCall({
|
2023-06-06 01:12:23 +08:00
|
|
|
client: MatrixClientPeg.safeGet(),
|
2022-10-17 17:16:04 +08:00
|
|
|
roomId,
|
|
|
|
});
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-10-17 17:16:04 +08:00
|
|
|
const callHandlerEmitSpy = jest.spyOn(callHandler, "emit");
|
|
|
|
|
|
|
|
cli.emit(CallEventHandlerEvent.Incoming, call);
|
|
|
|
// reset emit call count
|
|
|
|
callHandlerEmitSpy.mockClear();
|
|
|
|
|
|
|
|
callHandler.unSilenceCall(call.callId);
|
|
|
|
expect(callHandlerEmitSpy).not.toHaveBeenCalled();
|
|
|
|
// call still silenced
|
|
|
|
expect(callHandler.isCallSilenced(call.callId)).toEqual(true);
|
|
|
|
// ringer not played
|
2024-07-05 02:08:06 +08:00
|
|
|
expect(mockAudioBufferSourceNode.start).not.toHaveBeenCalled();
|
2022-10-17 17:16:04 +08:00
|
|
|
});
|
|
|
|
});
|
2022-07-01 20:43:42 +08:00
|
|
|
});
|