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();