@@ -194,6 +194,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
194
194
private joinConfig ?: SessionConfig ;
195
195
private logger : Logger ;
196
196
197
+ private pendingNotificationToSend : undefined | Exclude < RTCNotificationType , "decline" > ;
197
198
/**
198
199
* This timeout is responsible to track any expiration. We need to know when we have to start
199
200
* to ignore other call members. There is no callback for this. This timeout will always be configured to
@@ -459,6 +460,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
459
460
}
460
461
461
462
this . joinConfig = joinConfig ;
463
+ this . pendingNotificationToSend = this . joinConfig ?. notificationType ;
462
464
463
465
// Join!
464
466
this . membershipManager ! . join ( fociPreferred , fociActive , ( e ) => {
@@ -490,9 +492,10 @@ export class MatrixRTCSession extends TypedEventEmitter<
490
492
this . logger . info ( `Leaving call session in room ${ this . roomSubset . roomId } ` ) ;
491
493
492
494
this . encryptionManager ! . leave ( ) ;
493
- const leavePromise = this . membershipManager ! . leave ( timeout ) ;
494
495
496
+ const leavePromise = this . membershipManager ! . leave ( timeout ) ;
495
497
this . emit ( MatrixRTCSessionEvent . JoinStateChanged , false ) ;
498
+
496
499
return await leavePromise ;
497
500
}
498
501
@@ -562,33 +565,30 @@ export class MatrixRTCSession extends TypedEventEmitter<
562
565
/**
563
566
* Sends a notification corresponding to the configured notify type.
564
567
*/
565
- private sendCallNotify ( parentEventId : string ) : void {
566
- const notificationType = this . joinConfig ?. notificationType ;
567
- if ( notificationType !== undefined ) {
568
- // Send legacy event:
569
-
570
- this . client
571
- . sendEvent ( this . roomSubset . roomId , EventType . CallNotify , {
572
- "application" : "m.call" ,
573
- "m.mentions" : { user_ids : [ ] , room : true } ,
574
- "notify_type" : notificationType === "notification" ? "notify" : notificationType ,
575
- "call_id" : this . callId ! ,
576
- } )
577
- . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
578
- // Send new event:
579
- this . client
580
- . sendEvent ( this . roomSubset . roomId , EventType . RTCNotification , {
581
- "m.mentions" : { user_ids : [ ] , room : true } ,
582
- "notification_type" : notificationType ,
583
- "m.relates_to" : {
584
- event_id : parentEventId ,
585
- rel_type : RelationType . unstable_RTCNotificationParent ,
586
- } ,
587
- "sender_ts" : Date . now ( ) ,
588
- "lifetime" : 30_000 , // 30 seconds
589
- } )
590
- . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
591
- }
568
+ private sendCallNotify ( parentEventId : string , notificationType : Exclude < RTCNotificationType , "decline" > ) : void {
569
+ // Send legacy event:
570
+ this . client
571
+ . sendEvent ( this . roomSubset . roomId , EventType . CallNotify , {
572
+ "application" : "m.call" ,
573
+ "m.mentions" : { user_ids : [ ] , room : true } ,
574
+ "notify_type" : notificationType === "notification" ? "notify" : notificationType ,
575
+ "call_id" : this . callId ! ,
576
+ } )
577
+ . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
578
+
579
+ // Send new event:
580
+ this . client
581
+ . sendEvent ( this . roomSubset . roomId , EventType . RTCNotification , {
582
+ "m.mentions" : { user_ids : [ ] , room : true } ,
583
+ "notification_type" : notificationType ,
584
+ "m.relates_to" : {
585
+ event_id : parentEventId ,
586
+ rel_type : RelationType . unstable_RTCNotificationParent ,
587
+ } ,
588
+ "sender_ts" : Date . now ( ) ,
589
+ "lifetime" : 30_000 , // 30 seconds
590
+ } )
591
+ . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
592
592
}
593
593
594
594
/**
@@ -631,16 +631,20 @@ export class MatrixRTCSession extends TypedEventEmitter<
631
631
} ) ;
632
632
633
633
void this . membershipManager ?. onRTCSessionMemberUpdate ( this . memberships ) ;
634
+ // The `ownMembership` will be set when calling `onRTCSessionMemberUpdate`.
634
635
const ownMembership = this . membershipManager ?. ownMembership ;
635
- if ( ownMembership && oldMemberships . length === 0 ) {
636
+ if ( this . pendingNotificationToSend && ownMembership && oldMemberships . length === 0 ) {
636
637
// If we're the first member in the call, we're responsible for
637
638
// sending the notification event
638
- if ( ownMembership . eventId ) {
639
- this . sendCallNotify ( ownMembership . eventId ) ;
639
+ if ( ownMembership . eventId && this . joinConfig ?. notificationType ) {
640
+ this . sendCallNotify ( ownMembership . eventId , this . joinConfig . notificationType ) ;
640
641
} else {
641
642
this . logger . warn ( "Own membership eventId is undefined, cannot send call notification" ) ;
642
643
}
643
644
}
645
+ // If anyone else joins the session it is no longer our responsibility to send the notification.
646
+ // (If we were the joiner we already did sent the notification in the block above.)
647
+ if ( this . memberships . length > 0 ) this . pendingNotificationToSend = undefined ;
644
648
}
645
649
// This also needs to be done if `changed` = false
646
650
// A member might have updated their fingerprint (created_ts)
0 commit comments