@@ -83,6 +83,7 @@ import {UIFeature} from "./settings/UIFeature";
8383import { CallError } from "matrix-js-sdk/src/webrtc/call" ;
8484import { logger } from 'matrix-js-sdk/src/logger' ;
8585import { Action } from './dispatcher/actions' ;
86+ import { roomForVirtualRoom , getOrCreateVirtualRoomForRoom } from './VoipUserMapper' ;
8687
8788const CHECK_PSTN_SUPPORT_ATTEMPTS = 3 ;
8889
@@ -133,6 +134,15 @@ export default class CallHandler {
133134 return window . mxCallHandler ;
134135 }
135136
137+ /*
138+ * Gets the user-facing room associated with a call (call.roomId may be the call "virtual room"
139+ * if a voip_mxid_translate_pattern is set in the config)
140+ */
141+ public static roomIdForCall ( call : MatrixCall ) {
142+ if ( ! call ) return null ;
143+ return roomForVirtualRoom ( call . roomId ) || call . roomId ;
144+ }
145+
136146 start ( ) {
137147 this . dispatcherRef = dis . register ( this . onAction ) ;
138148 // add empty handlers for media actions, otherwise the media keys
@@ -284,11 +294,15 @@ export default class CallHandler {
284294 // We don't allow placing more than one call per room, but that doesn't mean there
285295 // can't be more than one, eg. in a glare situation. This checks that the given call
286296 // is the call we consider 'the' call for its room.
287- const callForThisRoom = this . getCallForRoom ( call . roomId ) ;
297+ const mappedRoomId = CallHandler . roomIdForCall ( call ) ;
298+
299+ const callForThisRoom = this . getCallForRoom ( mappedRoomId ) ;
288300 return callForThisRoom && call . callId === callForThisRoom . callId ;
289301 }
290302
291303 private setCallListeners ( call : MatrixCall ) {
304+ const mappedRoomId = CallHandler . roomIdForCall ( call ) ;
305+
292306 call . on ( CallEvent . Error , ( err : CallError ) => {
293307 if ( ! this . matchesCallForThisRoom ( call ) ) return ;
294308
@@ -318,7 +332,7 @@ export default class CallHandler {
318332
319333 Analytics . trackEvent ( 'voip' , 'callHangup' ) ;
320334
321- this . removeCallForRoom ( call . roomId ) ;
335+ this . removeCallForRoom ( mappedRoomId ) ;
322336 } ) ;
323337 call . on ( CallEvent . State , ( newState : CallState , oldState : CallState ) => {
324338 if ( ! this . matchesCallForThisRoom ( call ) ) return ;
@@ -343,7 +357,7 @@ export default class CallHandler {
343357 break ;
344358 case CallState . Ended :
345359 Analytics . trackEvent ( 'voip' , 'callEnded' , 'hangupReason' , call . hangupReason ) ;
346- this . removeCallForRoom ( call . roomId ) ;
360+ this . removeCallForRoom ( mappedRoomId ) ;
347361 if ( oldState === CallState . InviteSent && (
348362 call . hangupParty === CallParty . Remote ||
349363 ( call . hangupParty === CallParty . Local && call . hangupReason === CallErrorCode . InviteTimeout )
@@ -392,7 +406,7 @@ export default class CallHandler {
392406 this . pause ( AudioID . Ringback ) ;
393407 }
394408
395- this . calls . set ( newCall . roomId , newCall ) ;
409+ this . calls . set ( mappedRoomId , newCall ) ;
396410 this . setCallListeners ( newCall ) ;
397411 this . setCallState ( newCall , newCall . state ) ;
398412 } ) ;
@@ -404,13 +418,15 @@ export default class CallHandler {
404418 }
405419
406420 private setCallState ( call : MatrixCall , status : CallState ) {
421+ const mappedRoomId = CallHandler . roomIdForCall ( call ) ;
422+
407423 console . log (
408- `Call state in ${ call . roomId } changed to ${ status } ` ,
424+ `Call state in ${ mappedRoomId } changed to ${ status } ` ,
409425 ) ;
410426
411427 dis . dispatch ( {
412428 action : 'call_state' ,
413- room_id : call . roomId ,
429+ room_id : mappedRoomId ,
414430 state : status ,
415431 } ) ;
416432 }
@@ -477,14 +493,20 @@ export default class CallHandler {
477493 } , null , true ) ;
478494 }
479495
480- private placeCall (
496+ private async placeCall (
481497 roomId : string , type : PlaceCallType ,
482498 localElement : HTMLVideoElement , remoteElement : HTMLVideoElement ,
483499 ) {
484500 Analytics . trackEvent ( 'voip' , 'placeCall' , 'type' , type ) ;
485501 CountlyAnalytics . instance . trackStartCall ( roomId , type === PlaceCallType . Video , false ) ;
486- const call = createNewMatrixCall ( MatrixClientPeg . get ( ) , roomId ) ;
502+
503+ const mappedRoomId = ( await getOrCreateVirtualRoomForRoom ( roomId ) ) || roomId ;
504+ logger . debug ( "Mapped real room " + roomId + " to room ID " + mappedRoomId ) ;
505+
506+ const call = createNewMatrixCall ( MatrixClientPeg . get ( ) , mappedRoomId ) ;
507+
487508 this . calls . set ( roomId , call ) ;
509+
488510 this . setCallListeners ( call ) ;
489511 this . setCallAudioElement ( call ) ;
490512
@@ -586,13 +608,14 @@ export default class CallHandler {
586608
587609 const call = payload . call as MatrixCall ;
588610
589- if ( this . getCallForRoom ( call . roomId ) ) {
611+ const mappedRoomId = CallHandler . roomIdForCall ( call ) ;
612+ if ( this . getCallForRoom ( mappedRoomId ) ) {
590613 // ignore multiple incoming calls to the same room
591614 return ;
592615 }
593616
594617 Analytics . trackEvent ( 'voip' , 'receiveCall' , 'type' , call . type ) ;
595- this . calls . set ( call . roomId , call )
618+ this . calls . set ( mappedRoomId , call )
596619 this . setCallListeners ( call ) ;
597620 }
598621 break ;
0 commit comments