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

Commit

Permalink
Stop voice broadcast on delete (#9629)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Nov 28, 2022
1 parent d6ea92f commit 3c7781a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/voice-broadcast/models/VoiceBroadcastPlayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EventType,
MatrixClient,
MatrixEvent,
MatrixEventEvent,
MsgType,
RelationType,
} from "matrix-js-sdk/src/matrix";
Expand Down Expand Up @@ -88,6 +89,7 @@ export class VoiceBroadcastPlayback
) {
super();
this.addInfoEvent(this.infoEvent);
this.infoEvent.on(MatrixEventEvent.BeforeRedaction, this.onBeforeRedaction);
this.setUpRelationsHelper();
}

Expand Down Expand Up @@ -169,6 +171,14 @@ export class VoiceBroadcastPlayback
this.setInfoState(state);
};

private onBeforeRedaction = () => {
if (this.getState() !== VoiceBroadcastPlaybackState.Stopped) {
this.stop();
// destroy cleans up everything
this.destroy();
}
};

private async enqueueChunks(): Promise<void> {
const promises = this.chunkEvents.getEvents().reduce((promises, event: MatrixEvent) => {
if (!this.playbacks.has(event.getId() || "")) {
Expand Down
13 changes: 13 additions & 0 deletions test/voice-broadcast/models/VoiceBroadcastPlayback-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe("VoiceBroadcastPlayback", () => {
const mkPlayback = async () => {
const playback = new VoiceBroadcastPlayback(infoEvent, client);
jest.spyOn(playback, "removeAllListeners");
jest.spyOn(playback, "destroy");
playback.on(VoiceBroadcastPlaybackEvent.StateChanged, onStateChanged);
await flushPromises();
return playback;
Expand Down Expand Up @@ -273,6 +274,7 @@ describe("VoiceBroadcastPlayback", () => {
startPlayback();

it("should play the last chunk", () => {
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Playing);
// assert that the last chunk is played first
expect(chunk2Playback.play).toHaveBeenCalled();
expect(chunk1Playback.play).not.toHaveBeenCalled();
Expand All @@ -299,6 +301,17 @@ describe("VoiceBroadcastPlayback", () => {
});
});
});

describe("and the info event is deleted", () => {
beforeEach(() => {
infoEvent.makeRedacted(new MatrixEvent({}));
});

it("should stop and destroy the playback", () => {
expect(playback.getState()).toBe(VoiceBroadcastPlaybackState.Stopped);
expect(playback.destroy).toHaveBeenCalled();
});
});
});
});

Expand Down

0 comments on commit 3c7781a

Please sign in to comment.