Skip to content

Commit 1044a95

Browse files
defaultdinot3chguy
andauthored
fix(call): leave call along with room (#33162)
* make sure to disconnect from possibly active calls for a room when leaving the room * log error on log call * Update apps/web/src/utils/leave-behaviour.ts Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> * fix wrong logger import * hang up calls properly on empty rooms for both legacy and element calls (listen for room event and leave call if only one member left). add tests for both legacy and element calls. * format Call-test.ts * revert async on function def * revert Call.ts and Call-test.ts. Wrap legacy call hangup in try --------- Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent abae870 commit 1044a95

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

apps/web/src/utils/leave-behaviour.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details.
99
import { sleep } from "matrix-js-sdk/src/utils";
1010
import React, { type ReactNode } from "react";
1111
import { EventStatus, MatrixEventEvent, type Room, type MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
12+
import { logger } from "matrix-js-sdk/src/logger";
1213

1314
import Modal, { type IHandle } from "../Modal";
1415
import Spinner from "../components/views/elements/Spinner";
@@ -25,6 +26,8 @@ import { type AfterLeaveRoomPayload } from "../dispatcher/payloads/AfterLeaveRoo
2526
import { bulkSpaceBehaviour } from "./space";
2627
import { SdkContextClass } from "../contexts/SDKContext";
2728
import SettingsStore from "../settings/SettingsStore";
29+
import { CallStore } from "../stores/CallStore";
30+
import LegacyCallHandler from "../LegacyCallHandler";
2831

2932
export async function leaveRoomBehaviour(
3033
matrixClient: MatrixClient,
@@ -59,6 +62,23 @@ export async function leaveRoomBehaviour(
5962
throw new Error(`Expected to find room for id ${roomId}`);
6063
}
6164

65+
// attempt to hang up legacy based calls
66+
try {
67+
LegacyCallHandler.instance.hangupOrReject(roomId);
68+
} catch (e) {
69+
logger.warn("Failed to hangup call before leaving room: ", e);
70+
}
71+
72+
// hang up widget based calls
73+
const activeCall = CallStore.instance.getActiveCall(roomId);
74+
if (activeCall) {
75+
try {
76+
await activeCall.disconnect();
77+
} catch (e) {
78+
logger.warn("Failed to disconnect call before leaving room: ", e);
79+
}
80+
}
81+
6282
// await any queued messages being sent so that they do not fail
6383
await Promise.all(
6484
room

apps/web/test/unit-tests/utils/leave-behaviour-test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import SpaceStore from "../../../src/stores/spaces/SpaceStore";
2222
import { MetaSpace } from "../../../src/stores/spaces";
2323
import { type ActionPayload } from "../../../src/dispatcher/payloads";
2424
import SettingsStore from "../../../src/settings/SettingsStore";
25+
import { CallStore } from "../../../src/stores/CallStore";
26+
import { type Call } from "../../../src/models/Call";
27+
import LegacyCallHandler from "../../../src/LegacyCallHandler";
2528

2629
describe("leaveRoomBehaviour", () => {
2730
SdkContextClass.instance.constructEagerStores(); // Initialize RoomViewStore
@@ -76,6 +79,28 @@ describe("leaveRoomBehaviour", () => {
7679
defaultDispatcher.unregister(dispatcherRef);
7780
};
7881

82+
it("hangs up legacy calls when leaving a room", async () => {
83+
const hangupSpy = jest.spyOn(LegacyCallHandler.instance, "hangupOrReject").mockImplementation(() => {});
84+
85+
viewRoom(room);
86+
await leaveRoomBehaviour(client, room.roomId);
87+
88+
expect(hangupSpy).toHaveBeenCalledWith(room.roomId);
89+
});
90+
91+
it("disconnects widget-based calls when leaving a room", async () => {
92+
const mockCall = {
93+
disconnect: jest.fn().mockResolvedValue(undefined),
94+
} as unknown as Call;
95+
96+
jest.spyOn(CallStore.instance, "getActiveCall").mockReturnValue(mockCall);
97+
98+
viewRoom(room);
99+
await leaveRoomBehaviour(client, room.roomId);
100+
101+
expect(mockCall.disconnect).toHaveBeenCalled();
102+
});
103+
79104
it("returns to the home page after leaving a room outside of a space that was being viewed", async () => {
80105
viewRoom(room);
81106

0 commit comments

Comments
 (0)