From 291a79dcaaa1aad8911770a5396ce87fa595452a Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 1 Dec 2022 15:08:29 -0500 Subject: [PATCH 1/2] Fix call splitbrains when switching between rooms Mounting CallView causes the user's call membership room state to be cleaned up. However, because the GroupCall object always thought the local device was disconnected from the call, it would remove the local device from room state when the clean method is called, causing a splitbrain. This uses GroupCall's new enteredViaAnotherSession field to fix that, and also simplify participant tracking. --- src/models/Call.ts | 27 ++------------------------- test/models/Call-test.ts | 5 +++-- test/test-utils/test-utils.ts | 1 + 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/models/Call.ts b/src/models/Call.ts index 9d8d727f27e..5b4a0a8febf 100644 --- a/src/models/Call.ts +++ b/src/models/Call.ts @@ -647,7 +647,6 @@ export class ElementCall extends Call { client, ); - this.on(CallEvent.ConnectionState, this.onConnectionState); this.on(CallEvent.Participants, this.onParticipants); groupCall.on(GroupCallEvent.ParticipantsChanged, this.onGroupCallParticipants); groupCall.on(GroupCallEvent.GroupCallStateChanged, this.onGroupCallState); @@ -704,6 +703,7 @@ export class ElementCall extends Call { throw new Error(`Failed to join call in room ${this.roomId}: ${e}`); } + this.groupCall.enteredViaAnotherSession = true; this.messaging!.on(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); this.messaging!.on(`action:${ElementWidgetActions.TileLayout}`, this.onTileLayout); this.messaging!.on(`action:${ElementWidgetActions.SpotlightLayout}`, this.onSpotlightLayout); @@ -724,11 +724,11 @@ export class ElementCall extends Call { this.messaging!.off(`action:${ElementWidgetActions.SpotlightLayout}`, this.onSpotlightLayout); this.messaging!.off(`action:${ElementWidgetActions.ScreenshareRequest}`, this.onScreenshareRequest); super.setDisconnected(); + this.groupCall.enteredViaAnotherSession = false; } public destroy() { WidgetStore.instance.removeVirtualWidget(this.widget.id, this.groupCall.room.roomId); - this.off(CallEvent.ConnectionState, this.onConnectionState); this.off(CallEvent.Participants, this.onParticipants); this.groupCall.off(GroupCallEvent.ParticipantsChanged, this.onGroupCallParticipants); this.groupCall.off(GroupCallEvent.GroupCallStateChanged, this.onGroupCallState); @@ -760,20 +760,6 @@ export class ElementCall extends Call { participants.set(member, new Set(deviceMap.keys())); } - // We never enter group calls natively, so the GroupCall will think it's - // disconnected regardless of what our call member state says. Thus we - // have to insert our own device manually when connected via the widget. - if (this.connected) { - const localMember = this.room.getMember(this.client.getUserId()!)!; - let devices = participants.get(localMember); - if (devices === undefined) { - devices = new Set(); - participants.set(localMember, devices); - } - - devices.add(this.client.getDeviceId()!); - } - this.participants = participants; } @@ -782,15 +768,6 @@ export class ElementCall extends Call { && this.room.currentState.mayClientSendStateEvent(ElementCall.CALL_EVENT_TYPE.name, this.client); } - private onConnectionState = (state: ConnectionState, prevState: ConnectionState) => { - if ( - (state === ConnectionState.Connected && !isConnected(prevState)) - || (state === ConnectionState.Disconnected && isConnected(prevState)) - ) { - this.updateParticipants(); // Local echo - } - }; - private onParticipants = async ( participants: Map>, prevParticipants: Map>, diff --git a/test/models/Call-test.ts b/test/models/Call-test.ts index aa22db2718e..150afab9288 100644 --- a/test/models/Call-test.ts +++ b/test/models/Call-test.ts @@ -989,15 +989,16 @@ describe("ElementCall", () => { }); it("doesn't clean up valid devices", async () => { + await call.connect(); await client.sendStateEvent( room.roomId, ElementCall.MEMBER_EVENT_TYPE.name, - mkContent([aliceDesktop]), + mkContent([aliceWeb, aliceDesktop]), alice.userId, ); await call.clean(); - expectDevices([aliceDesktop]); + expectDevices([aliceWeb, aliceDesktop]); }); it("cleans up our own device if we're disconnected", async () => { diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index e2186295472..69b626dfd5a 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -91,6 +91,7 @@ export function createTestClient(): MatrixClient { getDeviceId: jest.fn().mockReturnValue("ABCDEFGHI"), deviceId: "ABCDEFGHI", getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), + getSessionId: jest.fn().mockReturnValue("iaszphgvfku"), credentials: { userId: "@userId:matrix.org" }, store: { From 822d0a96b9546d546820af5e1c9fb5454cf501f2 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 1 Dec 2022 23:30:01 -0500 Subject: [PATCH 2/2] Remove clean tests that have been moved to matrix-js-sdk --- test/models/Call-test.ts | 91 ---------------------------------------- 1 file changed, 91 deletions(-) diff --git a/test/models/Call-test.ts b/test/models/Call-test.ts index 150afab9288..785b9eea58b 100644 --- a/test/models/Call-test.ts +++ b/test/models/Call-test.ts @@ -939,97 +939,6 @@ describe("ElementCall", () => { call.off(CallEvent.Destroy, onDestroy); }); - - describe("clean", () => { - const aliceWeb: IMyDevice = { - device_id: "aliceweb", - last_seen_ts: 0, - }; - const aliceDesktop: IMyDevice = { - device_id: "alicedesktop", - last_seen_ts: 0, - }; - const aliceDesktopOffline: IMyDevice = { - device_id: "alicedesktopoffline", - last_seen_ts: 1000 * 60 * 60 * -2, // 2 hours ago - }; - const aliceDesktopNeverOnline: IMyDevice = { - device_id: "alicedesktopneveronline", - }; - - const mkContent = (devices: IMyDevice[]) => ({ - "m.calls": [{ - "m.call_id": call.groupCall.groupCallId, - "m.devices": devices.map(d => ({ - device_id: d.device_id, session_id: "1", feeds: [], expires_ts: 1000 * 60 * 10, - })), - }], - }); - const expectDevices = (devices: IMyDevice[]) => expect( - room.currentState.getStateEvents(ElementCall.MEMBER_EVENT_TYPE.name, alice.userId)?.getContent(), - ).toEqual({ - "m.calls": [{ - "m.call_id": call.groupCall.groupCallId, - "m.devices": devices.map(d => ({ - device_id: d.device_id, session_id: "1", feeds: [], expires_ts: expect.any(Number), - })), - }], - }); - - beforeEach(() => { - client.getDeviceId.mockReturnValue(aliceWeb.device_id); - client.getDevices.mockResolvedValue({ - devices: [ - aliceWeb, - aliceDesktop, - aliceDesktopOffline, - aliceDesktopNeverOnline, - ], - }); - }); - - it("doesn't clean up valid devices", async () => { - await call.connect(); - await client.sendStateEvent( - room.roomId, - ElementCall.MEMBER_EVENT_TYPE.name, - mkContent([aliceWeb, aliceDesktop]), - alice.userId, - ); - - await call.clean(); - expectDevices([aliceWeb, aliceDesktop]); - }); - - it("cleans up our own device if we're disconnected", async () => { - await client.sendStateEvent( - room.roomId, - ElementCall.MEMBER_EVENT_TYPE.name, - mkContent([aliceWeb, aliceDesktop]), - alice.userId, - ); - - await call.clean(); - expectDevices([aliceDesktop]); - }); - - it("cleans up devices that have never been online", async () => { - await client.sendStateEvent( - room.roomId, - ElementCall.MEMBER_EVENT_TYPE.name, - mkContent([aliceDesktop, aliceDesktopNeverOnline]), - alice.userId, - ); - - await call.clean(); - expectDevices([aliceDesktop]); - }); - - it("no-ops if there are no state events", async () => { - await call.clean(); - expect(room.currentState.getStateEvents(JitsiCall.MEMBER_EVENT_TYPE, alice.userId)).toBe(null); - }); - }); }); describe("instance in a video room", () => {