diff --git a/component/index.tsx b/component/index.tsx index 77f0debb8..867c1dedb 100644 --- a/component/index.tsx +++ b/component/index.tsx @@ -50,6 +50,7 @@ import { } from "react"; import { logger } from "matrix-js-sdk/lib/logger"; import { MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc"; +import { type Room } from "matrix-js-sdk"; import { I18nextProvider } from "react-i18next"; import { TooltipProvider } from "@vector-im/compound-web"; import { ErrorBoundary } from "@sentry/react"; @@ -205,7 +206,41 @@ export const ElementCall: FC = ({ }; }, [controlledAudioDevices, callIntent]); - const room = client.getRoom(roomId); + // The host's `Room`, from the host's copy of matrix-js-sdk: only the parts + // listed here are what we rely on. + // So only breaking changes in those methods/props would break Compoenent EC. + const room = client.getRoom(roomId) as Pick< + Room, + // Everywhere: the RTC session, analytics, e2ee keys, invite link, RTC slot, + // call view model + | "roomId" + // RTC session: memberships from the timeline and room state + | "getLiveTimeline" + | "hasMembershipState" + | "getVersion" + // RTC session and call view model: sticky memberships and call + // notifications + | "_unstable_getStickyEvents" + // Room name for the header and invite link + | "name" + // Room avatar for the header + | "getMxcAvatarUrl" + // `matrixInfo.roomAlias` + | "getCanonicalAlias" + // Room state for the avatar and join rule hooks + | "currentState" + // Joined and invited members, for the RTC slot and the call view model + | "getMembersWithMembership" + // `on`/`off`, to listen for: + // - the RTC session's room events + // - `RoomEvent.Name` (room name) + // - `RoomEvent.CurrentStateUpdated` (avatar and join rule hooks) + // - `RoomStateEvent.Events` (RTC slot status) + // - `RoomStateEvent.Members` (RTC slot and call view model members) + // - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications) + | "on" + | "off" + > | null; // A session of our own rather than `client.matrixRTC.getRoomSession`: the // client is the host's, built from the host's copy of matrix-js-sdk, so the @@ -220,7 +255,7 @@ export const ElementCall: FC = ({ }; // It is important we create a session from the js-sdk and dont inherit from // the host passed client. - const session = MatrixRTCSession.sessionForSlot(client, room, slot); + const session = new MatrixRTCSession(client, room, slot); setRtcSession(session); return (): void => { setRtcSession(null); diff --git a/sdk/src/RTCSlot.ts b/sdk/src/RTCSlot.ts index 43f9a78c1..f126a9ff3 100644 --- a/sdk/src/RTCSlot.ts +++ b/sdk/src/RTCSlot.ts @@ -48,6 +48,8 @@ import { } from "./matrixrtc/Members"; import { createTransportRegistry } from "./matrixrtc/Transports"; + + /** * Takes the whole `MatrixClient` rather than a slice of it. The js-sdk is the * MatrixRTC implementation today; when the rust-rtc crate replaces it, the SDK @@ -61,7 +63,11 @@ import { createTransportRegistry } from "./matrixrtc/Transports"; export function createRTCSlot( scope: ObservableScope, client: MatrixClient, - room: Room, + // The parts of the Matrix room a slot reads. `on` and `off` are only there for + // `fromEvent`, which listens on the room for: + // - `RoomStateEvent.Events`, to update the slot status; + // - `RoomStateEvent.Members`, to update the members' display names and avatars. + room: Pick, options: RTCSlotOptions, jsSdkSession: JsSdkRTCSession, ): RTCSlot { diff --git a/sdk/src/matrixrtc/MatrixMemberMetadata.ts b/sdk/src/matrixrtc/MatrixMemberMetadata.ts index f0e155e66..a411a4620 100644 --- a/sdk/src/matrixrtc/MatrixMemberMetadata.ts +++ b/sdk/src/matrixrtc/MatrixMemberMetadata.ts @@ -23,7 +23,9 @@ export type RoomMemberMap = Map< string, Pick >; -export function roomToMembersMap(matrixRoom: MatrixRoom): RoomMemberMap { +export function roomToMembersMap( + matrixRoom: Pick, +): RoomMemberMap { const members = matrixRoom .getMembersWithMembership(KnownMembership.Join) .concat(matrixRoom.getMembersWithMembership(KnownMembership.Invite)); @@ -37,9 +39,10 @@ export function roomToMembersMap(matrixRoom: MatrixRoom): RoomMemberMap { }, new Map()); } +/** Listens for `RoomStateEvent.Members` on the room. */ export function createRoomMembers$( scope: ObservableScope, - matrixRoom: MatrixRoom, + matrixRoom: Pick, ): Behavior { return scope.behavior( fromEvent(matrixRoom, RoomStateEvent.Members).pipe( diff --git a/src/room/CallView.tsx b/src/room/CallView.tsx index a79824007..661fe56a8 100644 --- a/src/room/CallView.tsx +++ b/src/room/CallView.tsx @@ -92,11 +92,32 @@ interface Props { * takes the whole client. */ client: MatrixClient; - /** - * The room to call in. Whole, because the call's RTC slot takes the whole - * room. - */ - room: Room; + /** The room to call in. */ + room: Pick< + Room, + // Everywhere: analytics, e2ee keys, invite link, RTC slot, call view model + | "roomId" + // Room name for the header and invite link + | "name" + // Room avatar for the header + | "getMxcAvatarUrl" + // `matrixInfo.roomAlias` + | "getCanonicalAlias" + // Room state for the avatar and join rule hooks + | "currentState" + // Joined and invited members, for the RTC slot and the call view model + | "getMembersWithMembership" + // Call notifications, for the call view model + | "_unstable_getStickyEvents" + // `on`/`off`, to listen for: + // - `RoomEvent.Name` (room name) + // - `RoomEvent.CurrentStateUpdated` (avatar and join rule hooks) + // - `RoomStateEvent.Events` (RTC slot status) + // - `RoomStateEvent.Members` (RTC slot and call view model members) + // - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications) + | "on" + | "off" + >; /** The call to join. */ rtcSession: MatrixRTCSession; /** @@ -240,14 +261,6 @@ const LoadedCallView: FC = ({ }, ); - useEffect(() => { - // Sanity check the room object - if (client.getRoom(room.roomId) !== room) - logger.warn( - `We've ended up with multiple rooms for the same ID (${room.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`, - ); - }, [client, room]); - const { displayName, avatarUrl } = useProfile(client); const roomName = useRoomName(room); const roomAvatar = useRoomAvatar(room); diff --git a/src/room/InCallView.tsx b/src/room/InCallView.tsx index 3e2a718ae..b7e7aac0e 100644 --- a/src/room/InCallView.tsx +++ b/src/room/InCallView.tsx @@ -138,8 +138,21 @@ export interface ActiveCallProps extends Omit< > { /** Whole, because {@link createRTCSlot} takes the whole client. */ client: MatrixClient; - /** Whole, because {@link createRTCSlot} takes the whole room. */ - matrixRoom: MatrixRoom; + matrixRoom: Pick< + MatrixRoom, + // RTC slot and call view model + | "roomId" + // Joined and invited members, for the RTC slot and the call view model + | "getMembersWithMembership" + // Call notifications, for the call view model + | "_unstable_getStickyEvents" + // `on`/`off`, to listen for: + // - `RoomStateEvent.Events` (RTC slot status) + // - `RoomStateEvent.Members` (RTC slot and call view model members) + // - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications) + | "on" + | "off" + >; e2eeSystem: EncryptionSystem; /** The tracks the lobby opened, for the join to publish. */ media: LocalMedia; diff --git a/src/room/RoomPage.tsx b/src/room/RoomPage.tsx index a1ca6c5ed..dc9c1b15e 100644 --- a/src/room/RoomPage.tsx +++ b/src/room/RoomPage.tsx @@ -51,9 +51,9 @@ export const RoomPage: FC = (): ReactNode => { // The page title is the page's to set, not the call's: a host embedding the // call has a title of its own. So it is set here, for whichever room we have // got as far as knowing about. - const roomName = useRoomName( - groupCallState.kind === "loaded" ? groupCallState.rtcSession.room : null, - ); + const loadedRoom = + groupCallState.kind === "loaded" ? groupCallState.rtcSession.room : null; + const roomName = useRoomName(loadedRoom); usePageTitle( roomName ?? (groupCallState.kind === "canKnock" || @@ -90,6 +90,18 @@ export const RoomPage: FC = (): ReactNode => { if (optInAnalytics === null && setOptInAnalytics) setOptInAnalytics(true); }, [optInAnalytics, setOptInAnalytics]); + useEffect(() => { + // Sanity check the room object + if ( + client && + loadedRoom !== null && + client.getRoom(loadedRoom.roomId) !== loadedRoom + ) + logger.warn( + `We've ended up with multiple rooms for the same ID (${loadedRoom.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`, + ); + }, [client, loadedRoom]); + const wasInWaitForInviteState = useRef(false); useEffect(() => { diff --git a/src/room/useRoomAvatar.ts b/src/room/useRoomAvatar.ts index 32d7b1055..05d912c26 100644 --- a/src/room/useRoomAvatar.ts +++ b/src/room/useRoomAvatar.ts @@ -8,10 +8,19 @@ Please see LICENSE in the repository root for full details. import { useCallback } from "react"; import { type Room } from "matrix-js-sdk"; -import { type RoomStateParts, useRoomState } from "./useRoomState"; +import { useRoomState } from "./useRoomState"; export function useRoomAvatar( - room: RoomStateParts & Pick, + room: Pick< + Room, + // The avatar itself + | "getMxcAvatarUrl" + // Room state, and `on`/`off` for `RoomEvent.CurrentStateUpdated`, for + // `useRoomState` + | "currentState" + | "on" + | "off" + >, ): string | null { return useRoomState( room, diff --git a/src/state/CallViewModel/CallNotificationLifecycle.ts b/src/state/CallViewModel/CallNotificationLifecycle.ts index 7620cd93f..cbf5cec70 100644 --- a/src/state/CallViewModel/CallNotificationLifecycle.ts +++ b/src/state/CallViewModel/CallNotificationLifecycle.ts @@ -75,35 +75,32 @@ export function createSentCallNotification$( } export function createReceivedDecline$( - matrixRoom: Pick< - MatrixRoom, - "_unstable_getStickyEvents" | "addListener" | "removeListener" - >, + matrixRoom: Pick, ): Observable { // A decline is sticky, so after a gappy sync it arrives only in the sticky // section, which never reaches the timeline. Racing for the first outcome // makes a decline seen in both places harmless. - const timeline$ = ( - fromEvent(matrixRoom, RoomEvent.Timeline) as Observable< - Parameters - > - ).pipe(map(([event]) => event)); + const timeline$ = fromEvent( + matrixRoom, + RoomEvent.Timeline, + (...[event]: Parameters) => + event, + ); // The decline can arrive via sync before the send of our notification // resolves, which is when we start listening. To guard against this, we // include what is already in the sticky section. const currentSticky$ = defer(() => from(matrixRoom._unstable_getStickyEvents()), ); - const sticky$ = ( - fromEvent(matrixRoom, RoomStickyEventsEvent.Update) as Observable< - Parameters - > - ).pipe( - mergeMap(([added, updated]) => [ - ...added, - ...updated.map(({ current }) => current), - ]), - ); + const sticky$ = fromEvent( + matrixRoom, + RoomStickyEventsEvent.Update, + ( + ...[added, updated]: Parameters< + RoomStickyEventsMap[RoomStickyEventsEvent.Update] + > + ) => [...added, ...updated.map(({ current }) => current)], + ).pipe(mergeMap((events) => events)); return merge(currentSticky$, sticky$, timeline$).pipe( filter((event) => event.getType() === EventType.RTCDecline), ); diff --git a/src/state/CallViewModel/CallViewModel.ts b/src/state/CallViewModel/CallViewModel.ts index 063556d2b..48ca36892 100644 --- a/src/state/CallViewModel/CallViewModel.ts +++ b/src/state/CallViewModel/CallViewModel.ts @@ -199,8 +199,8 @@ export interface CallViewModelProps { | "roomId" | "getMembersWithMembership" | "_unstable_getStickyEvents" - | "addListener" - | "removeListener" + | "on" + | "off" >; client: Pick; mediaDevices: MediaDevices;