From dc07b69afd7aa18efa9a7a0b9eb969889cf19814 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Wed, 4 May 2022 16:08:52 -0400 Subject: [PATCH 1/3] Disconnect from video rooms when leaving --- src/stores/VideoChannelStore.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/stores/VideoChannelStore.ts b/src/stores/VideoChannelStore.ts index d32f748fb7f..bb361462ddd 100644 --- a/src/stores/VideoChannelStore.ts +++ b/src/stores/VideoChannelStore.ts @@ -15,6 +15,7 @@ limitations under the License. */ import EventEmitter from "events"; +import { Room, RoomEvent } from "matrix-js-sdk/src/models/room"; import { ClientWidgetApi, IWidgetApiRequest } from "matrix-widget-api"; import defaultDispatcher from "../dispatcher/dispatcher"; @@ -193,6 +194,7 @@ export default class VideoChannelStore extends AsyncStoreWithClient { this.connected = true; messaging.once(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); + this.matrixClient.on(RoomEvent.MyMembership, this.onMyMembership); window.addEventListener("beforeunload", this.setDisconnected); this.emit(VideoChannelEvent.Connect, roomId); @@ -216,6 +218,7 @@ export default class VideoChannelStore extends AsyncStoreWithClient { public setDisconnected = async () => { this.activeChannel.off(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); this.activeChannel.off(`action:${ElementWidgetActions.CallParticipants}`, this.onParticipants); + this.matrixClient.off(RoomEvent.MyMembership, this.onMyMembership); window.removeEventListener("beforeunload", this.setDisconnected); const roomId = this.roomId; @@ -242,6 +245,8 @@ export default class VideoChannelStore extends AsyncStoreWithClient { private updateDevices = async (roomId: string, fn: (devices: string[]) => string[]) => { const room = this.matrixClient.getRoom(roomId); + if (room.getMyMembership() !== "join") return; + const devicesState = room.currentState.getStateEvents(VIDEO_CHANNEL_MEMBER, this.matrixClient.getUserId()); const devices = devicesState?.getContent()?.devices ?? []; @@ -280,4 +285,8 @@ export default class VideoChannelStore extends AsyncStoreWithClient { this.videoMuted = false; this.ack(ev); }; + + private onMyMembership = (room: Room, membership: string) => { + if (room.roomId === this.roomId && membership !== "join") this.setDisconnected(); + }; } From 3201bbe5d2d1c96c29fe6f0f024424a8efd76fa5 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Wed, 4 May 2022 16:42:50 -0400 Subject: [PATCH 2/3] Listen on the specific room --- src/stores/VideoChannelStore.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stores/VideoChannelStore.ts b/src/stores/VideoChannelStore.ts index bb361462ddd..075e5bc8f8c 100644 --- a/src/stores/VideoChannelStore.ts +++ b/src/stores/VideoChannelStore.ts @@ -194,7 +194,7 @@ export default class VideoChannelStore extends AsyncStoreWithClient { this.connected = true; messaging.once(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); - this.matrixClient.on(RoomEvent.MyMembership, this.onMyMembership); + this.matrixClient.getRoom(roomId).on(RoomEvent.MyMembership, this.onMyMembership); window.addEventListener("beforeunload", this.setDisconnected); this.emit(VideoChannelEvent.Connect, roomId); @@ -218,7 +218,7 @@ export default class VideoChannelStore extends AsyncStoreWithClient { public setDisconnected = async () => { this.activeChannel.off(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); this.activeChannel.off(`action:${ElementWidgetActions.CallParticipants}`, this.onParticipants); - this.matrixClient.off(RoomEvent.MyMembership, this.onMyMembership); + this.matrixClient.getRoom(roomId).off(RoomEvent.MyMembership, this.onMyMembership); window.removeEventListener("beforeunload", this.setDisconnected); const roomId = this.roomId; @@ -287,6 +287,6 @@ export default class VideoChannelStore extends AsyncStoreWithClient { }; private onMyMembership = (room: Room, membership: string) => { - if (room.roomId === this.roomId && membership !== "join") this.setDisconnected(); + if (membership !== "join") this.setDisconnected(); }; } From 54b00bfd1b69743018ac0491463603d8b3bb5545 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Wed, 4 May 2022 16:49:08 -0400 Subject: [PATCH 3/3] Fix lints --- src/stores/VideoChannelStore.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stores/VideoChannelStore.ts b/src/stores/VideoChannelStore.ts index 075e5bc8f8c..62e3571aa3e 100644 --- a/src/stores/VideoChannelStore.ts +++ b/src/stores/VideoChannelStore.ts @@ -216,12 +216,13 @@ export default class VideoChannelStore extends AsyncStoreWithClient { }; public setDisconnected = async () => { + const roomId = this.roomId; + this.activeChannel.off(`action:${ElementWidgetActions.HangupCall}`, this.onHangup); this.activeChannel.off(`action:${ElementWidgetActions.CallParticipants}`, this.onParticipants); this.matrixClient.getRoom(roomId).off(RoomEvent.MyMembership, this.onMyMembership); window.removeEventListener("beforeunload", this.setDisconnected); - const roomId = this.roomId; this.activeChannel = null; this.roomId = null; this.connected = false;