Skip to content

Commit 3abd8bb

Browse files
committed
review
1 parent bf95e55 commit 3abd8bb

File tree

3 files changed

+36
-86
lines changed

3 files changed

+36
-86
lines changed

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -201,59 +201,6 @@ describe("MatrixRTCSession", () => {
201201
});
202202
});
203203

204-
describe("updateCallMembershipEvent", () => {
205-
const mockFocus = { type: "livekit", livekit_service_url: "https://test.org" };
206-
const joinSessionConfig = {};
207-
208-
const sessionMembershipData: MembershipData = {
209-
call_id: "",
210-
scope: "m.room",
211-
application: "m.call",
212-
user_id: "@mock:user.example",
213-
device_id: "AAAAAAA_session",
214-
focus_active: mockFocus,
215-
foci_preferred: [mockFocus],
216-
};
217-
218-
let sendStateEventMock: jest.Mock;
219-
let sendDelayedStateMock: jest.Mock;
220-
221-
let sentStateEvent: Promise<void>;
222-
let sentDelayedState: Promise<void>;
223-
224-
beforeEach(() => {
225-
sentStateEvent = new Promise((resolve) => {
226-
sendStateEventMock = jest.fn(resolve);
227-
});
228-
sentDelayedState = new Promise((resolve) => {
229-
sendDelayedStateMock = jest.fn(() => {
230-
resolve();
231-
return {
232-
delay_id: "id",
233-
};
234-
});
235-
});
236-
client.sendStateEvent = sendStateEventMock;
237-
client._unstable_sendDelayedStateEvent = sendDelayedStateMock;
238-
});
239-
240-
async function testSession(membershipData: MembershipData): Promise<void> {
241-
sess = MatrixRTCSession.roomSessionForRoom(client, makeMockRoom([membershipData]));
242-
243-
sess.joinRoomSession([mockFocus], mockFocus, joinSessionConfig);
244-
await Promise.race([sentStateEvent, new Promise((resolve) => setTimeout(resolve, 500))]);
245-
246-
expect(sendStateEventMock).toHaveBeenCalledTimes(1);
247-
248-
await Promise.race([sentDelayedState, new Promise((resolve) => setTimeout(resolve, 500))]);
249-
expect(sendDelayedStateMock).toHaveBeenCalledTimes(1);
250-
}
251-
252-
it("sends events", async () => {
253-
await testSession(sessionMembershipData);
254-
});
255-
});
256-
257204
describe("getOldestMembership", () => {
258205
it("returns the oldest membership event", () => {
259206
jest.useFakeTimers();
@@ -376,7 +323,7 @@ describe("MatrixRTCSession", () => {
376323
"sender_ts": expect.any(Number),
377324
});
378325

379-
// check if deprecated notify event is also sent
326+
// Check if deprecated notify event is also sent.
380327
expect(client.sendEvent).toHaveBeenCalledWith(mockRoom!.roomId, EventType.CallNotify, {
381328
"application": "m.call",
382329
"m.mentions": { user_ids: [], room: true },

src/@types/event.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ export enum RelationType {
164164
// Once we support *only* the stable prefix, THREAD_RELATION_TYPE can die and we can switch to this.
165165
Thread = "m.thread",
166166
unstable_RTCNotificationParent = "org.matrix.msc4075.rtc.notification.parent",
167-
unstable_RTCNotificationDecline = "org.matrix.msc4075.rtc.notification.decline",
168167
}
169168

170169
export enum MsgType {

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class MatrixRTCSession extends TypedEventEmitter<
194194
private joinConfig?: SessionConfig;
195195
private logger: Logger;
196196

197+
private pendingNotificationToSend: undefined | Exclude<RTCNotificationType, "decline">;
197198
/**
198199
* This timeout is responsible to track any expiration. We need to know when we have to start
199200
* 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<
459460
}
460461

461462
this.joinConfig = joinConfig;
463+
this.pendingNotificationToSend = this.joinConfig?.notificationType;
462464

463465
// Join!
464466
this.membershipManager!.join(fociPreferred, fociActive, (e) => {
@@ -490,9 +492,10 @@ export class MatrixRTCSession extends TypedEventEmitter<
490492
this.logger.info(`Leaving call session in room ${this.roomSubset.roomId}`);
491493

492494
this.encryptionManager!.leave();
493-
const leavePromise = this.membershipManager!.leave(timeout);
494495

496+
const leavePromise = this.membershipManager!.leave(timeout);
495497
this.emit(MatrixRTCSessionEvent.JoinStateChanged, false);
498+
496499
return await leavePromise;
497500
}
498501

@@ -562,33 +565,30 @@ export class MatrixRTCSession extends TypedEventEmitter<
562565
/**
563566
* Sends a notification corresponding to the configured notify type.
564567
*/
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));
592592
}
593593

594594
/**
@@ -631,16 +631,20 @@ export class MatrixRTCSession extends TypedEventEmitter<
631631
});
632632

633633
void this.membershipManager?.onRTCSessionMemberUpdate(this.memberships);
634+
// The `ownMembership` will be set when calling `onRTCSessionMemberUpdate`.
634635
const ownMembership = this.membershipManager?.ownMembership;
635-
if (ownMembership && oldMemberships.length === 0) {
636+
if (this.pendingNotificationToSend && ownMembership && oldMemberships.length === 0) {
636637
// If we're the first member in the call, we're responsible for
637638
// 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);
640641
} else {
641642
this.logger.warn("Own membership eventId is undefined, cannot send call notification");
642643
}
643644
}
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;
644648
}
645649
// This also needs to be done if `changed` = false
646650
// A member might have updated their fingerprint (created_ts)

0 commit comments

Comments
 (0)