Resume voice broadcast on load (#9475)
This commit is contained in:
parent
493ee425b7
commit
5f59ce182e
16 changed files with 554 additions and 22 deletions
111
test/voice-broadcast/utils/VoiceBroadcastResumer-test.ts
Normal file
111
test/voice-broadcast/utils/VoiceBroadcastResumer-test.ts
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { ClientEvent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import {
|
||||
findRoomLiveVoiceBroadcastFromUserAndDevice,
|
||||
resumeVoiceBroadcastInRoom,
|
||||
VoiceBroadcastInfoState,
|
||||
VoiceBroadcastResumer,
|
||||
} from "../../../src/voice-broadcast";
|
||||
import { stubClient } from "../../test-utils";
|
||||
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
||||
|
||||
jest.mock("../../../src/voice-broadcast/utils/findRoomLiveVoiceBroadcastFromUserAndDevice");
|
||||
jest.mock("../../../src/voice-broadcast/utils/resumeVoiceBroadcastInRoom");
|
||||
|
||||
describe("VoiceBroadcastResumer", () => {
|
||||
const roomId = "!room:example.com";
|
||||
let client: MatrixClient;
|
||||
let room: Room;
|
||||
let resumer: VoiceBroadcastResumer;
|
||||
let infoEvent: MatrixEvent;
|
||||
|
||||
beforeEach(() => {
|
||||
client = stubClient();
|
||||
jest.spyOn(client, "off");
|
||||
room = new Room(roomId, client, client.getUserId());
|
||||
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
||||
if (getRoomId === roomId) return room;
|
||||
});
|
||||
resumer = new VoiceBroadcastResumer(client);
|
||||
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Started,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("when there is no info event", () => {
|
||||
beforeEach(() => {
|
||||
client.emit(ClientEvent.Room, room);
|
||||
});
|
||||
|
||||
it("should not resume a broadcast", () => {
|
||||
expect(resumeVoiceBroadcastInRoom).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there is an info event", () => {
|
||||
beforeEach(() => {
|
||||
mocked(findRoomLiveVoiceBroadcastFromUserAndDevice).mockImplementation((
|
||||
findRoom: Room,
|
||||
userId: string,
|
||||
deviceId: string,
|
||||
) => {
|
||||
if (findRoom === room && userId === client.getUserId() && deviceId === client.getDeviceId()) {
|
||||
return infoEvent;
|
||||
}
|
||||
});
|
||||
client.emit(ClientEvent.Room, room);
|
||||
});
|
||||
|
||||
it("should resume a broadcast", () => {
|
||||
expect(resumeVoiceBroadcastInRoom).toHaveBeenCalledWith(
|
||||
infoEvent,
|
||||
room,
|
||||
client,
|
||||
);
|
||||
});
|
||||
|
||||
describe("and emitting a room event again", () => {
|
||||
beforeEach(() => {
|
||||
client.emit(ClientEvent.Room, room);
|
||||
});
|
||||
|
||||
it("should not resume the broadcast again", () => {
|
||||
expect(resumeVoiceBroadcastInRoom).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("when calling destroy", () => {
|
||||
beforeEach(() => {
|
||||
resumer.destroy();
|
||||
});
|
||||
|
||||
it("should deregister from the client", () => {
|
||||
expect(client.off).toHaveBeenCalledWith(ClientEvent.Room, expect.any(Function));
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import {
|
||||
findRoomLiveVoiceBroadcastFromUserAndDevice,
|
||||
VoiceBroadcastInfoEventType,
|
||||
VoiceBroadcastInfoState,
|
||||
} from "../../../src/voice-broadcast";
|
||||
import { mkEvent, stubClient } from "../../test-utils";
|
||||
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
|
||||
|
||||
describe("findRoomLiveVoiceBroadcastFromUserAndDevice", () => {
|
||||
const roomId = "!room:example.com";
|
||||
let client: MatrixClient;
|
||||
let room: Room;
|
||||
|
||||
const itShouldReturnNull = () => {
|
||||
it("should return null", () => {
|
||||
expect(findRoomLiveVoiceBroadcastFromUserAndDevice(
|
||||
room,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
)).toBeNull();
|
||||
});
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
client = stubClient();
|
||||
room = new Room(roomId, client, client.getUserId());
|
||||
jest.spyOn(room.currentState, "getStateEvents");
|
||||
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
||||
if (getRoomId === roomId) return room;
|
||||
});
|
||||
});
|
||||
|
||||
describe("when there is no info event", () => {
|
||||
itShouldReturnNull();
|
||||
});
|
||||
|
||||
describe("when there is an info event without content", () => {
|
||||
beforeEach(() => {
|
||||
room.currentState.setStateEvents([
|
||||
mkEvent({
|
||||
event: true,
|
||||
type: VoiceBroadcastInfoEventType,
|
||||
room: roomId,
|
||||
user: client.getUserId(),
|
||||
content: {},
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
itShouldReturnNull();
|
||||
});
|
||||
|
||||
describe("when there is a stopped info event", () => {
|
||||
beforeEach(() => {
|
||||
room.currentState.setStateEvents([
|
||||
mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Stopped,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
),
|
||||
]);
|
||||
});
|
||||
|
||||
itShouldReturnNull();
|
||||
});
|
||||
|
||||
describe("when there is a started info event from another device", () => {
|
||||
beforeEach(() => {
|
||||
const event = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Stopped,
|
||||
client.getUserId(),
|
||||
"JKL123",
|
||||
);
|
||||
room.currentState.setStateEvents([event]);
|
||||
});
|
||||
|
||||
itShouldReturnNull();
|
||||
});
|
||||
|
||||
describe("when there is a started info event", () => {
|
||||
let event: MatrixEvent;
|
||||
|
||||
beforeEach(() => {
|
||||
event = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Started,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
);
|
||||
room.currentState.setStateEvents([event]);
|
||||
});
|
||||
|
||||
it("should return this event", () => {
|
||||
expect(room.currentState.getStateEvents).toHaveBeenCalledWith(
|
||||
VoiceBroadcastInfoEventType,
|
||||
client.getUserId(),
|
||||
);
|
||||
|
||||
expect(findRoomLiveVoiceBroadcastFromUserAndDevice(
|
||||
room,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
)).toBe(event);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -35,7 +35,12 @@ describe("hasRoomLiveVoiceBroadcast", () => {
|
|||
sender: string,
|
||||
) => {
|
||||
room.currentState.setStateEvents([
|
||||
mkVoiceBroadcastInfoStateEvent(room.roomId, state, sender),
|
||||
mkVoiceBroadcastInfoStateEvent(
|
||||
room.roomId,
|
||||
state,
|
||||
sender,
|
||||
"ASD123",
|
||||
),
|
||||
]);
|
||||
};
|
||||
|
||||
|
|
110
test/voice-broadcast/utils/resumeVoiceBroadcastInRoom-test.ts
Normal file
110
test/voice-broadcast/utils/resumeVoiceBroadcastInRoom-test.ts
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import {
|
||||
resumeVoiceBroadcastInRoom,
|
||||
VoiceBroadcastInfoState,
|
||||
VoiceBroadcastRecording,
|
||||
VoiceBroadcastRecordingsStore,
|
||||
} from "../../../src/voice-broadcast";
|
||||
import { stubClient } from "../../test-utils";
|
||||
import { mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
||||
|
||||
const mockRecording = jest.fn();
|
||||
|
||||
jest.mock("../../../src/voice-broadcast/models/VoiceBroadcastRecording", () => ({
|
||||
...jest.requireActual("../../../src/voice-broadcast/models/VoiceBroadcastRecording") as object,
|
||||
VoiceBroadcastRecording: jest.fn().mockImplementation(() => mockRecording),
|
||||
}));
|
||||
|
||||
describe("resumeVoiceBroadcastInRoom", () => {
|
||||
let client: MatrixClient;
|
||||
const roomId = "!room:example.com";
|
||||
let room: Room;
|
||||
let startedInfoEvent: MatrixEvent;
|
||||
let stoppedInfoEvent: MatrixEvent;
|
||||
|
||||
const itShouldStartAPausedRecording = () => {
|
||||
it("should start a paused recording", () => {
|
||||
expect(VoiceBroadcastRecording).toHaveBeenCalledWith(
|
||||
startedInfoEvent,
|
||||
client,
|
||||
VoiceBroadcastInfoState.Paused,
|
||||
);
|
||||
expect(VoiceBroadcastRecordingsStore.instance().setCurrent).toHaveBeenCalledWith(mockRecording);
|
||||
});
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
client = stubClient();
|
||||
room = new Room(roomId, client, client.getUserId());
|
||||
jest.spyOn(room, "findEventById");
|
||||
jest.spyOn(VoiceBroadcastRecordingsStore.instance(), "setCurrent").mockImplementation();
|
||||
|
||||
startedInfoEvent = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Started,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
);
|
||||
|
||||
stoppedInfoEvent = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Stopped,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
startedInfoEvent,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("when called with a stopped info event", () => {
|
||||
describe("and there is a related event", () => {
|
||||
beforeEach(() => {
|
||||
mocked(room.findEventById).mockReturnValue(startedInfoEvent);
|
||||
resumeVoiceBroadcastInRoom(stoppedInfoEvent, room, client);
|
||||
});
|
||||
|
||||
itShouldStartAPausedRecording();
|
||||
});
|
||||
|
||||
describe("and there is no related event", () => {
|
||||
beforeEach(() => {
|
||||
mocked(room.findEventById).mockReturnValue(null);
|
||||
resumeVoiceBroadcastInRoom(stoppedInfoEvent, room, client);
|
||||
});
|
||||
|
||||
it("should not start a broadcast", () => {
|
||||
expect(VoiceBroadcastRecording).not.toHaveBeenCalled();
|
||||
expect(VoiceBroadcastRecordingsStore.instance().setCurrent).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("when called with a started info event", () => {
|
||||
beforeEach(() => {
|
||||
resumeVoiceBroadcastInRoom(startedInfoEvent, room, client);
|
||||
});
|
||||
|
||||
itShouldStartAPausedRecording();
|
||||
});
|
||||
});
|
|
@ -70,7 +70,12 @@ describe("startNewVoiceBroadcastRecording", () => {
|
|||
getCurrent: jest.fn(),
|
||||
} as unknown as VoiceBroadcastRecordingsStore;
|
||||
|
||||
infoEvent = mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Started, client.getUserId());
|
||||
infoEvent = mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Started,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
);
|
||||
otherEvent = mkEvent({
|
||||
event: true,
|
||||
type: EventType.RoomMember,
|
||||
|
@ -154,7 +159,12 @@ describe("startNewVoiceBroadcastRecording", () => {
|
|||
describe("when there already is a live broadcast of the current user in the room", () => {
|
||||
beforeEach(async () => {
|
||||
room.currentState.setStateEvents([
|
||||
mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Running, client.getUserId()),
|
||||
mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Running,
|
||||
client.getUserId(),
|
||||
client.getDeviceId(),
|
||||
),
|
||||
]);
|
||||
|
||||
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
||||
|
@ -172,7 +182,12 @@ describe("startNewVoiceBroadcastRecording", () => {
|
|||
describe("when there already is a live broadcast of another user", () => {
|
||||
beforeEach(async () => {
|
||||
room.currentState.setStateEvents([
|
||||
mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Running, otherUserId),
|
||||
mkVoiceBroadcastInfoStateEvent(
|
||||
roomId,
|
||||
VoiceBroadcastInfoState.Running,
|
||||
otherUserId,
|
||||
"ASD123",
|
||||
),
|
||||
]);
|
||||
|
||||
result = await startNewVoiceBroadcastRecording(room, client, recordingsStore);
|
||||
|
|
|
@ -22,7 +22,8 @@ import { mkEvent } from "../../test-utils";
|
|||
export const mkVoiceBroadcastInfoStateEvent = (
|
||||
roomId: string,
|
||||
state: VoiceBroadcastInfoState,
|
||||
sender: string,
|
||||
senderId: string,
|
||||
senderDeviceId: string,
|
||||
startedInfoEvent?: MatrixEvent,
|
||||
): MatrixEvent => {
|
||||
const relationContent = {};
|
||||
|
@ -37,11 +38,12 @@ export const mkVoiceBroadcastInfoStateEvent = (
|
|||
return mkEvent({
|
||||
event: true,
|
||||
room: roomId,
|
||||
user: sender,
|
||||
user: senderId,
|
||||
type: VoiceBroadcastInfoEventType,
|
||||
skey: sender,
|
||||
skey: senderId,
|
||||
content: {
|
||||
state,
|
||||
device_id: senderDeviceId,
|
||||
...relationContent,
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue