@@ -27,7 +27,12 @@ import { KnownMembership } from "../@types/membership.ts";
27
27
import { MembershipManager } from "./MembershipManager.ts" ;
28
28
import { EncryptionManager , type IEncryptionManager } from "./EncryptionManager.ts" ;
29
29
import { deepCompare , logDurationSync } from "../utils.ts" ;
30
- import { type Statistics , type RTCNotificationType } from "./types.ts" ;
30
+ import {
31
+ type Statistics ,
32
+ type RTCNotificationType ,
33
+ type IRTCNotificationContent ,
34
+ type ICallNotifyContent ,
35
+ } from "./types.ts" ;
31
36
import { RoomKeyTransport } from "./RoomKeyTransport.ts" ;
32
37
import {
33
38
MembershipManagerEvent ,
@@ -42,6 +47,7 @@ import {
42
47
} from "./RoomAndToDeviceKeyTransport.ts" ;
43
48
import { TypedReEmitter } from "../ReEmitter.ts" ;
44
49
import { ToDeviceKeyTransport } from "./ToDeviceKeyTransport.ts" ;
50
+ import { type ISendEventResponse } from "src/matrix.ts" ;
45
51
46
52
export enum MatrixRTCSessionEvent {
47
53
// A member joined, left, or updated a property of their membership.
@@ -54,6 +60,8 @@ export enum MatrixRTCSessionEvent {
54
60
EncryptionKeyChanged = "encryption_key_changed" ,
55
61
/** The membership manager had to shut down caused by an unrecoverable error */
56
62
MembershipManagerError = "membership_manager_error" ,
63
+ /** The RTCSession did send a call notification caused by joining the call as the first member */
64
+ DidSendCallNotification = "did_send_call_notification" ,
57
65
}
58
66
59
67
export type MatrixRTCSessionEventHandlerMap = {
@@ -68,6 +76,10 @@ export type MatrixRTCSessionEventHandlerMap = {
68
76
participantId : string ,
69
77
) => void ;
70
78
[ MatrixRTCSessionEvent . MembershipManagerError ] : ( error : unknown ) => void ;
79
+ [ MatrixRTCSessionEvent . DidSendCallNotification ] : (
80
+ notificationContentNew : IRTCNotificationContent ,
81
+ notificationContentLegacy : ICallNotifyContent ,
82
+ ) => void ;
71
83
} ;
72
84
73
85
export interface SessionConfig {
@@ -654,19 +666,24 @@ export class MatrixRTCSession extends TypedEventEmitter<
654
666
* Sends a notification corresponding to the configured notify type.
655
667
*/
656
668
private sendCallNotify ( parentEventId : string , notificationType : RTCNotificationType ) : void {
657
- // Send legacy event:
658
- this . client
659
- . sendEvent ( this . roomSubset . roomId , EventType . CallNotify , {
669
+ const sendLegacyNotificationEvent = async ( ) : Promise < {
670
+ response : ISendEventResponse ;
671
+ content : ICallNotifyContent ;
672
+ } > => {
673
+ const content : ICallNotifyContent = {
660
674
"application" : "m.call" ,
661
675
"m.mentions" : { user_ids : [ ] , room : true } ,
662
676
"notify_type" : notificationType === "notification" ? "notify" : notificationType ,
663
677
"call_id" : this . callId ! ,
664
- } )
665
- . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
666
-
667
- // Send new event:
668
- this . client
669
- . sendEvent ( this . roomSubset . roomId , EventType . RTCNotification , {
678
+ } ;
679
+ const response = await this . client . sendEvent ( this . roomSubset . roomId , EventType . CallNotify , content ) ;
680
+ return { response, content } ;
681
+ } ;
682
+ const sendNewNotificationEvent = async ( ) : Promise < {
683
+ response : ISendEventResponse ;
684
+ content : IRTCNotificationContent ;
685
+ } > => {
686
+ const content : IRTCNotificationContent = {
670
687
"m.mentions" : { user_ids : [ ] , room : true } ,
671
688
"notification_type" : notificationType ,
672
689
"m.relates_to" : {
@@ -675,8 +692,21 @@ export class MatrixRTCSession extends TypedEventEmitter<
675
692
} ,
676
693
"sender_ts" : Date . now ( ) ,
677
694
"lifetime" : 30_000 , // 30 seconds
695
+ } ;
696
+ const response = await this . client . sendEvent ( this . roomSubset . roomId , EventType . RTCNotification , content ) ;
697
+ return { response, content } ;
698
+ } ;
699
+
700
+ void Promise . all ( [ sendLegacyNotificationEvent ( ) , sendNewNotificationEvent ( ) ] )
701
+ . then ( ( [ legacy , newNotification ] ) => {
702
+ // Join event_id and origin event content
703
+ const legacyResult = { ...legacy . response , ...legacy . content } ;
704
+ const newResult = { ...newNotification . response , ...newNotification . content } ;
705
+ this . emit ( MatrixRTCSessionEvent . DidSendCallNotification , newResult , legacyResult ) ;
678
706
} )
679
- . catch ( ( e ) => this . logger . error ( "Failed to send call notification" , e ) ) ;
707
+ . catch ( ( [ errorLegacy , errorNew ] ) =>
708
+ this . logger . error ( "Failed to send call notification" , errorLegacy , errorNew ) ,
709
+ ) ;
680
710
}
681
711
682
712
/**
0 commit comments