Skip to content

Commit a9dc1c3

Browse files
committed
MatrixRTCSession emits once the rtc notification is sent.
Signed-off-by: Timo K <[email protected]>
1 parent b80d009 commit a9dc1c3

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ import { KnownMembership } from "../@types/membership.ts";
2727
import { MembershipManager } from "./MembershipManager.ts";
2828
import { EncryptionManager, type IEncryptionManager } from "./EncryptionManager.ts";
2929
import { deepCompare, logDurationSync } from "../utils.ts";
30-
import { type Statistics, type RTCNotificationType, type Status } from "./types.ts";
30+
import {
31+
type Statistics,
32+
type RTCNotificationType,
33+
type Status,
34+
type IRTCNotificationContent,
35+
type ICallNotifyContent,
36+
} from "./types.ts";
3137
import { RoomKeyTransport } from "./RoomKeyTransport.ts";
3238
import {
3339
MembershipManagerEvent,
@@ -42,6 +48,7 @@ import {
4248
} from "./RoomAndToDeviceKeyTransport.ts";
4349
import { TypedReEmitter } from "../ReEmitter.ts";
4450
import { ToDeviceKeyTransport } from "./ToDeviceKeyTransport.ts";
51+
import { type ISendEventResponse } from "src/matrix.ts";
4552

4653
export enum MatrixRTCSessionEvent {
4754
// A member joined, left, or updated a property of their membership.
@@ -54,6 +61,8 @@ export enum MatrixRTCSessionEvent {
5461
EncryptionKeyChanged = "encryption_key_changed",
5562
/** The membership manager had to shut down caused by an unrecoverable error */
5663
MembershipManagerError = "membership_manager_error",
64+
/** The RTCSession did send a call notification caused by joining the call as the first member */
65+
DidSendCallNotification = "did_send_call_notification",
5766
}
5867

5968
export type MatrixRTCSessionEventHandlerMap = {
@@ -68,6 +77,10 @@ export type MatrixRTCSessionEventHandlerMap = {
6877
participantId: string,
6978
) => void;
7079
[MatrixRTCSessionEvent.MembershipManagerError]: (error: unknown) => void;
80+
[MatrixRTCSessionEvent.DidSendCallNotification]: (
81+
notificationContentNew: IRTCNotificationContent,
82+
notificationContentLegacy: ICallNotifyContent,
83+
) => void;
7184
};
7285

7386
export interface SessionConfig {
@@ -652,19 +665,24 @@ export class MatrixRTCSession extends TypedEventEmitter<
652665
* Sends a notification corresponding to the configured notify type.
653666
*/
654667
private sendCallNotify(parentEventId: string, notificationType: RTCNotificationType): void {
655-
// Send legacy event:
656-
this.client
657-
.sendEvent(this.roomSubset.roomId, EventType.CallNotify, {
668+
const sendLegacyNotificationEvent = async (): Promise<{
669+
response: ISendEventResponse;
670+
content: ICallNotifyContent;
671+
}> => {
672+
const content: ICallNotifyContent = {
658673
"application": "m.call",
659674
"m.mentions": { user_ids: [], room: true },
660675
"notify_type": notificationType === "notification" ? "notify" : notificationType,
661676
"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, {
677+
};
678+
const response = await this.client.sendEvent(this.roomSubset.roomId, EventType.CallNotify, content);
679+
return { response, content };
680+
};
681+
const sendNewNotificationEvent = async (): Promise<{
682+
response: ISendEventResponse;
683+
content: IRTCNotificationContent;
684+
}> => {
685+
const content: IRTCNotificationContent = {
668686
"m.mentions": { user_ids: [], room: true },
669687
"notification_type": notificationType,
670688
"m.relates_to": {
@@ -673,8 +691,21 @@ export class MatrixRTCSession extends TypedEventEmitter<
673691
},
674692
"sender_ts": Date.now(),
675693
"lifetime": 30_000, // 30 seconds
694+
};
695+
const response = await this.client.sendEvent(this.roomSubset.roomId, EventType.RTCNotification, content);
696+
return { response, content };
697+
};
698+
699+
void Promise.all([sendLegacyNotificationEvent(), sendNewNotificationEvent()])
700+
.then(([legacy, newNotification]) => {
701+
// Join event_id and origin event content
702+
const legacyResult = { ...legacy.response, ...legacy.content };
703+
const newResult = { ...newNotification.response, ...newNotification.content };
704+
this.emit(MatrixRTCSessionEvent.DidSendCallNotification, newResult, legacyResult);
676705
})
677-
.catch((e) => this.logger.error("Failed to send call notification", e));
706+
.catch(([errorLegacy, errorNew]) =>
707+
this.logger.error("Failed to send call notification", errorLegacy, errorNew),
708+
);
678709
}
679710

680711
/**

0 commit comments

Comments
 (0)