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