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

Commit 29c1932

Browse files
Stop broadcasts on logout (#9978)
Co-authored-by: Andy Balaam <[email protected]>
1 parent 0b1fcd2 commit 29c1932

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

src/components/structures/MatrixChat.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ import { ValidatedServerConfig } from "../../utils/ValidatedServerConfig";
134134
import { isLocalRoom } from "../../utils/localRoom/isLocalRoom";
135135
import { SdkContextClass, SDKContext } from "../../contexts/SDKContext";
136136
import { viewUserDeviceSettings } from "../../actions/handlers/viewUserDeviceSettings";
137-
import { VoiceBroadcastResumer } from "../../voice-broadcast";
137+
import { cleanUpBroadcasts, VoiceBroadcastResumer } from "../../voice-broadcast";
138138
import GenericToast from "../views/toasts/GenericToast";
139139
import { Linkify } from "../views/elements/Linkify";
140140
import RovingSpotlightDialog, { Filter } from "../views/dialogs/spotlight/SpotlightDialog";
@@ -591,9 +591,10 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
591591
break;
592592
case "logout":
593593
LegacyCallHandler.instance.hangupAllCalls();
594-
Promise.all([...CallStore.instance.activeCalls].map((call) => call.disconnect())).finally(() =>
595-
Lifecycle.logout(),
596-
);
594+
Promise.all([
595+
...[...CallStore.instance.activeCalls].map((call) => call.disconnect()),
596+
cleanUpBroadcasts(this.stores),
597+
]).finally(() => Lifecycle.logout());
597598
break;
598599
case "require_registration":
599600
startAnyRegistrationFlow(payload as any);

src/voice-broadcast/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export * from "./stores/VoiceBroadcastPlaybacksStore";
4646
export * from "./stores/VoiceBroadcastPreRecordingStore";
4747
export * from "./stores/VoiceBroadcastRecordingsStore";
4848
export * from "./utils/checkVoiceBroadcastPreConditions";
49+
export * from "./utils/cleanUpBroadcasts";
4950
export * from "./utils/doClearCurrentVoiceBroadcastPlaybackIfStopped";
5051
export * from "./utils/doMaybeSetCurrentVoiceBroadcastPlayback";
5152
export * from "./utils/getChunkLength";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import { SdkContextClass } from "../../contexts/SDKContext";
18+
19+
export const cleanUpBroadcasts = async (stores: SdkContextClass): Promise<void> => {
20+
stores.voiceBroadcastPlaybacksStore.getCurrent()?.stop();
21+
stores.voiceBroadcastPlaybacksStore.clearCurrent();
22+
23+
await stores.voiceBroadcastRecordingsStore.getCurrent()?.stop();
24+
stores.voiceBroadcastRecordingsStore.clearCurrent();
25+
26+
stores.voiceBroadcastPreRecordingStore.getCurrent()?.cancel();
27+
stores.voiceBroadcastPreRecordingStore.clearCurrent();
28+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import {
18+
cleanUpBroadcasts,
19+
VoiceBroadcastPlayback,
20+
VoiceBroadcastPreRecording,
21+
VoiceBroadcastRecording,
22+
} from "../../../src/voice-broadcast";
23+
import { stubClient } from "../../test-utils";
24+
import { TestSdkContext } from "../../TestSdkContext";
25+
import { mkVoiceBroadcastPlayback, mkVoiceBroadcastPreRecording, mkVoiceBroadcastRecording } from "./test-utils";
26+
27+
describe("cleanUpBroadcasts", () => {
28+
let playback: VoiceBroadcastPlayback;
29+
let recording: VoiceBroadcastRecording;
30+
let preRecording: VoiceBroadcastPreRecording;
31+
let stores: TestSdkContext;
32+
33+
beforeEach(() => {
34+
stores = new TestSdkContext();
35+
stores.client = stubClient();
36+
37+
playback = mkVoiceBroadcastPlayback(stores);
38+
jest.spyOn(playback, "stop").mockReturnValue();
39+
stores.voiceBroadcastPlaybacksStore.setCurrent(playback);
40+
41+
recording = mkVoiceBroadcastRecording(stores);
42+
jest.spyOn(recording, "stop").mockResolvedValue();
43+
stores.voiceBroadcastRecordingsStore.setCurrent(recording);
44+
45+
preRecording = mkVoiceBroadcastPreRecording(stores);
46+
jest.spyOn(preRecording, "cancel").mockReturnValue();
47+
stores.voiceBroadcastPreRecordingStore.setCurrent(preRecording);
48+
});
49+
50+
it("should stop and clear all broadcast related stuff", async () => {
51+
await cleanUpBroadcasts(stores);
52+
expect(playback.stop).toHaveBeenCalled();
53+
expect(stores.voiceBroadcastPlaybacksStore.getCurrent()).toBeNull();
54+
expect(recording.stop).toHaveBeenCalled();
55+
expect(stores.voiceBroadcastRecordingsStore.getCurrent()).toBeNull();
56+
expect(preRecording.cancel).toHaveBeenCalled();
57+
expect(stores.voiceBroadcastPreRecordingStore.getCurrent()).toBeNull();
58+
});
59+
});

test/voice-broadcast/utils/test-utils.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ limitations under the License.
1515
*/
1616

1717
import { Optional } from "matrix-events-sdk";
18-
import { EventType, MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix";
18+
import { EventType, MatrixEvent, MsgType, RelationType, Room, RoomMember } from "matrix-js-sdk/src/matrix";
1919

20+
import { SdkContextClass } from "../../../src/contexts/SDKContext";
21+
import {
22+
VoiceBroadcastPlayback,
23+
VoiceBroadcastPreRecording,
24+
VoiceBroadcastRecording,
25+
} from "../../../src/voice-broadcast";
2026
import {
2127
VoiceBroadcastChunkEventType,
2228
VoiceBroadcastInfoEventType,
@@ -97,3 +103,37 @@ export const mkVoiceBroadcastChunkEvent = (
97103
ts: timestamp,
98104
});
99105
};
106+
107+
export const mkVoiceBroadcastPlayback = (stores: SdkContextClass): VoiceBroadcastPlayback => {
108+
const infoEvent = mkVoiceBroadcastInfoStateEvent(
109+
"!room:example.com",
110+
VoiceBroadcastInfoState.Started,
111+
"@user:example.com",
112+
"ASD123",
113+
);
114+
return new VoiceBroadcastPlayback(infoEvent, stores.client!, stores.voiceBroadcastRecordingsStore);
115+
};
116+
117+
export const mkVoiceBroadcastRecording = (stores: SdkContextClass): VoiceBroadcastRecording => {
118+
const infoEvent = mkVoiceBroadcastInfoStateEvent(
119+
"!room:example.com",
120+
VoiceBroadcastInfoState.Started,
121+
"@user:example.com",
122+
"ASD123",
123+
);
124+
return new VoiceBroadcastRecording(infoEvent, stores.client!);
125+
};
126+
127+
export const mkVoiceBroadcastPreRecording = (stores: SdkContextClass): VoiceBroadcastPreRecording => {
128+
const roomId = "!room:example.com";
129+
const userId = "@user:example.com";
130+
const room = new Room(roomId, stores.client!, userId);
131+
const roomMember = new RoomMember(roomId, userId);
132+
return new VoiceBroadcastPreRecording(
133+
room,
134+
roomMember,
135+
stores.client!,
136+
stores.voiceBroadcastPlaybacksStore,
137+
stores.voiceBroadcastRecordingsStore,
138+
);
139+
};

0 commit comments

Comments
 (0)