Improve live voice broadcast detection by testing if the started event has been redacted (#9780)

This commit is contained in:
Michael Weimann 2022-12-22 11:37:07 +01:00 committed by GitHub
parent fbc3228143
commit b81582d045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 365 additions and 83 deletions

View file

@ -14,6 +14,7 @@ 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 {
@ -26,20 +27,28 @@ import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
describe("hasRoomLiveVoiceBroadcast", () => {
const otherUserId = "@other:example.com";
const otherDeviceId = "ASD123";
const roomId = "!room:example.com";
let client: MatrixClient;
let room: Room;
let expectedEvent: MatrixEvent | null = null;
const addVoiceBroadcastInfoEvent = (state: VoiceBroadcastInfoState, sender: string): MatrixEvent => {
const infoEvent = mkVoiceBroadcastInfoStateEvent(room.roomId, state, sender, "ASD123");
const addVoiceBroadcastInfoEvent = (
state: VoiceBroadcastInfoState,
userId: string,
deviceId: string,
startedEvent?: MatrixEvent,
): MatrixEvent => {
const infoEvent = mkVoiceBroadcastInfoStateEvent(room.roomId, state, userId, deviceId, startedEvent);
room.addLiveEvents([infoEvent]);
room.currentState.setStateEvents([infoEvent]);
room.relations.aggregateChildEvent(infoEvent);
return infoEvent;
};
const itShouldReturnTrueTrue = () => {
it("should return true/true", () => {
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
it("should return true/true", async () => {
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
hasBroadcast: true,
infoEvent: expectedEvent,
startedByUser: true,
@ -48,8 +57,8 @@ describe("hasRoomLiveVoiceBroadcast", () => {
};
const itShouldReturnTrueFalse = () => {
it("should return true/false", () => {
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
it("should return true/false", async () => {
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
hasBroadcast: true,
infoEvent: expectedEvent,
startedByUser: false,
@ -58,8 +67,8 @@ describe("hasRoomLiveVoiceBroadcast", () => {
};
const itShouldReturnFalseFalse = () => {
it("should return false/false", () => {
expect(hasRoomLiveVoiceBroadcast(room, client.getUserId())).toEqual({
it("should return false/false", async () => {
expect(await hasRoomLiveVoiceBroadcast(client, room, client.getSafeUserId())).toEqual({
hasBroadcast: false,
infoEvent: null,
startedByUser: false,
@ -67,13 +76,13 @@ describe("hasRoomLiveVoiceBroadcast", () => {
});
};
beforeAll(() => {
client = stubClient();
});
beforeEach(() => {
client = stubClient();
room = new Room(roomId, client, client.getSafeUserId());
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
return roomId === room.roomId ? room : null;
});
expectedEvent = null;
room = new Room(roomId, client, client.getUserId());
});
describe("when there is no voice broadcast info at all", () => {
@ -86,9 +95,9 @@ describe("hasRoomLiveVoiceBroadcast", () => {
mkEvent({
event: true,
room: room.roomId,
user: client.getUserId(),
user: client.getSafeUserId(),
type: VoiceBroadcastInfoEventType,
skey: client.getUserId(),
skey: client.getSafeUserId(),
content: {},
}),
]);
@ -98,8 +107,12 @@ describe("hasRoomLiveVoiceBroadcast", () => {
describe("when there is a live broadcast from the current and another user", () => {
beforeEach(() => {
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, client.getUserId());
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId);
expectedEvent = addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Started,
client.getSafeUserId(),
client.getDeviceId()!,
);
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
});
itShouldReturnTrueTrue();
@ -107,21 +120,56 @@ describe("hasRoomLiveVoiceBroadcast", () => {
describe("when there are only stopped info events", () => {
beforeEach(() => {
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getUserId());
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, otherUserId);
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getSafeUserId(), client.getDeviceId()!);
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, otherUserId, otherDeviceId);
});
itShouldReturnFalseFalse();
});
describe.each([
// all there are kind of live states
VoiceBroadcastInfoState.Started,
VoiceBroadcastInfoState.Paused,
VoiceBroadcastInfoState.Resumed,
])("when there is a live broadcast (%s) from the current user", (state: VoiceBroadcastInfoState) => {
describe("when there is a live, started broadcast from the current user", () => {
beforeEach(() => {
expectedEvent = addVoiceBroadcastInfoEvent(state, client.getUserId());
expectedEvent = addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Started,
client.getSafeUserId(),
client.getDeviceId()!,
);
});
itShouldReturnTrueTrue();
});
describe("when there is a live, paused broadcast from the current user", () => {
beforeEach(() => {
expectedEvent = addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Started,
client.getSafeUserId(),
client.getDeviceId()!,
);
addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Paused,
client.getSafeUserId(),
client.getDeviceId()!,
expectedEvent,
);
});
itShouldReturnTrueTrue();
});
describe("when there is a live, resumed broadcast from the current user", () => {
beforeEach(() => {
expectedEvent = addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Started,
client.getSafeUserId(),
client.getDeviceId()!,
);
addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Resumed,
client.getSafeUserId(),
client.getDeviceId()!,
expectedEvent,
);
});
itShouldReturnTrueTrue();
@ -129,8 +177,17 @@ describe("hasRoomLiveVoiceBroadcast", () => {
describe("when there was a live broadcast, that has been stopped", () => {
beforeEach(() => {
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Resumed, client.getUserId());
addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped, client.getUserId());
const startedEvent = addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Started,
client.getSafeUserId(),
client.getDeviceId()!,
);
addVoiceBroadcastInfoEvent(
VoiceBroadcastInfoState.Stopped,
client.getSafeUserId(),
client.getDeviceId()!,
startedEvent,
);
});
itShouldReturnFalseFalse();
@ -138,7 +195,7 @@ describe("hasRoomLiveVoiceBroadcast", () => {
describe("when there is a live broadcast from another user", () => {
beforeEach(() => {
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Resumed, otherUserId);
expectedEvent = addVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started, otherUserId, otherDeviceId);
});
itShouldReturnTrueFalse();

View file

@ -0,0 +1,90 @@
/*
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 } from "matrix-js-sdk/src/client";
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import {
retrieveStartedInfoEvent,
VoiceBroadcastInfoEventType,
VoiceBroadcastInfoState,
} from "../../../src/voice-broadcast";
import { mkEvent, stubClient } from "../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "./test-utils";
describe("retrieveStartedInfoEvent", () => {
let client: MatrixClient;
let room: Room;
const mkStartEvent = () => {
return mkVoiceBroadcastInfoStateEvent(
room.roomId,
VoiceBroadcastInfoState.Started,
client.getUserId()!,
client.deviceId!,
);
};
const mkStopEvent = (startEvent: MatrixEvent) => {
return mkVoiceBroadcastInfoStateEvent(
room.roomId,
VoiceBroadcastInfoState.Stopped,
client.getUserId()!,
client.deviceId!,
startEvent,
);
};
beforeEach(() => {
client = stubClient();
room = new Room("!room:example.com", client, client.getUserId()!);
mocked(client.getRoom).mockImplementation((roomId: string): Room | null => {
if (roomId === room.roomId) return room;
return null;
});
});
it("when passing a started event, it should return the event", async () => {
const event = mkStartEvent();
expect(await retrieveStartedInfoEvent(event, client)).toBe(event);
});
it("when passing an event without relation, it should return null", async () => {
const event = mkEvent({
event: true,
type: VoiceBroadcastInfoEventType,
user: client.getUserId()!,
content: {},
});
expect(await retrieveStartedInfoEvent(event, client)).toBeNull();
});
it("when the room contains the event, it should return it", async () => {
const startEvent = mkStartEvent();
const stopEvent = mkStopEvent(startEvent);
room.addLiveEvents([startEvent]);
expect(await retrieveStartedInfoEvent(stopEvent, client)).toBe(startEvent);
});
it("when the room not contains the event, it should fetch it", async () => {
const startEvent = mkStartEvent();
const stopEvent = mkStopEvent(startEvent);
mocked(client.fetchRoomEvent).mockResolvedValue(startEvent.event);
expect((await retrieveStartedInfoEvent(stopEvent, client))?.getId()).toBe(startEvent.getId());
expect(client.fetchRoomEvent).toHaveBeenCalledWith(room.roomId, startEvent.getId());
});
});

View file

@ -75,7 +75,7 @@ describe("setUpVoiceBroadcastPreRecording", () => {
describe("when the preconditions fail", () => {
beforeEach(() => {
mocked(checkVoiceBroadcastPreConditions).mockReturnValue(false);
mocked(checkVoiceBroadcastPreConditions).mockResolvedValue(false);
});
itShouldReturnNull();
@ -83,7 +83,7 @@ describe("setUpVoiceBroadcastPreRecording", () => {
describe("when the preconditions pass", () => {
beforeEach(() => {
mocked(checkVoiceBroadcastPreConditions).mockReturnValue(true);
mocked(checkVoiceBroadcastPreConditions).mockResolvedValue(true);
});
describe("and there is no user id", () => {