Skip to content

Commit 1d62bcd

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

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ 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 } from "./types.ts";
30+
import {
31+
type Statistics,
32+
type RTCNotificationType,
33+
type IRTCNotificationContent,
34+
type ICallNotifyContent,
35+
} from "./types.ts";
3136
import { RoomKeyTransport } from "./RoomKeyTransport.ts";
3237
import {
3338
MembershipManagerEvent,
@@ -42,6 +47,7 @@ import {
4247
} from "./RoomAndToDeviceKeyTransport.ts";
4348
import { TypedReEmitter } from "../ReEmitter.ts";
4449
import { ToDeviceKeyTransport } from "./ToDeviceKeyTransport.ts";
50+
import { type ISendEventResponse } from "src/matrix.ts";
4551

4652
export enum MatrixRTCSessionEvent {
4753
// A member joined, left, or updated a property of their membership.
@@ -54,6 +60,8 @@ export enum MatrixRTCSessionEvent {
5460
EncryptionKeyChanged = "encryption_key_changed",
5561
/** The membership manager had to shut down caused by an unrecoverable error */
5662
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",
5765
}
5866

5967
export type MatrixRTCSessionEventHandlerMap = {
@@ -68,6 +76,10 @@ export type MatrixRTCSessionEventHandlerMap = {
6876
participantId: string,
6977
) => void;
7078
[MatrixRTCSessionEvent.MembershipManagerError]: (error: unknown) => void;
79+
[MatrixRTCSessionEvent.DidSendCallNotification]: (
80+
notificationContentNew: IRTCNotificationContent,
81+
notificationContentLegacy: ICallNotifyContent,
82+
) => void;
7183
};
7284

7385
export interface SessionConfig {
@@ -654,19 +666,24 @@ export class MatrixRTCSession extends TypedEventEmitter<
654666
* Sends a notification corresponding to the configured notify type.
655667
*/
656668
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 = {
660674
"application": "m.call",
661675
"m.mentions": { user_ids: [], room: true },
662676
"notify_type": notificationType === "notification" ? "notify" : notificationType,
663677
"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 = {
670687
"m.mentions": { user_ids: [], room: true },
671688
"notification_type": notificationType,
672689
"m.relates_to": {
@@ -675,8 +692,21 @@ export class MatrixRTCSession extends TypedEventEmitter<
675692
},
676693
"sender_ts": Date.now(),
677694
"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);
678706
})
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+
);
680710
}
681711

682712
/**

0 commit comments

Comments
 (0)