Skip to content

Commit

Permalink
Chore: Use a single notification listener for all videoconf messages (#…
Browse files Browse the repository at this point in the history
…26682)

Co-authored-by: Diego Sampaio <8591547+sampaiodiego@users.noreply.github.com>
  • Loading branch information
pierre-lehnen-rc and sampaiodiego authored Oct 17, 2022
1 parent 127d118 commit c8f61e5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
58 changes: 42 additions & 16 deletions apps/meteor/client/lib/VideoConfManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
this.emit('incoming/changed');

debug && console.log(`[VideoConf] Notifying user ${callData.uid} that we accept their call.`);
Notifications.notifyUser(callData.uid, 'video-conference.accepted', { callId, uid: this.userId, rid: callData.rid });
this.userId && this.notifyUser(callData.uid, 'accepted', { callId, uid: this.userId, rid: callData.rid });
}

public rejectIncomingCall(callId: string): void {
Expand All @@ -227,7 +227,7 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
return;
}

Notifications.notifyUser(callData.uid, 'video-conference.rejected', { callId, uid: this.userId, rid: callData.rid });
this.userId && this.notifyUser(callData.uid, 'rejected', { callId, uid: this.userId, rid: callData.rid });
this.loseIncomingCall(callId);
}

Expand Down Expand Up @@ -401,12 +401,12 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
}

debug && console.log(`[VideoConf] Ringing user ${uid}, attempt number ${attempt}.`);
Notifications.notifyUser(uid, 'video-conference.call', { uid: this.userId, rid, callId });
this.userId && this.notifyUser(uid, 'call', { uid: this.userId, rid, callId });
}, CALL_INTERVAL);
this.emit('calling/changed');

debug && console.log(`[VideoConf] Ringing user ${uid} for the first time.`);
Notifications.notifyUser(uid, 'video-conference.call', { uid: this.userId, rid, callId });
this.userId && this.notifyUser(uid, 'call', { uid: this.userId, rid, callId });
}

private async giveUp({ uid, rid, callId }: DirectCallParams): Promise<void> {
Expand All @@ -421,7 +421,7 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
}

debug && console.log(`[VideoConf] Notifying user ${uid} that we are no longer calling.`);
Notifications.notifyUser(uid, 'video-conference.canceled', { uid: this.userId, rid, callId });
this.userId && this.notifyUser(uid, 'canceled', { uid: this.userId, rid, callId });

this.emit('direct/cancel', { uid, rid, callId });
this.emit('direct/stopped', { uid, rid, callId });
Expand Down Expand Up @@ -461,21 +461,47 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
this.emit('calling/changed');
}

private async hookNotification(eventName: string, cb: (...params: any[]) => void): Promise<void> {
this.hooks.push(await Notifications.onUser(eventName, cb));
private async onVideoConfNotification({ action, params }: { action: string; params: DirectCallParams }): Promise<void> {
if (!action || typeof action !== 'string') {
debug && console.error('[VideoConf] Invalid action received.');
return;
}
if (!params || typeof params !== 'object' || !params.callId || !params.uid || !params.rid) {
debug && console.error('[VideoConf] Invalid params received.');
return;
}

switch (action) {
case 'call':
return this.onDirectCall(params);
case 'canceled':
return this.onDirectCallCanceled(params);
case 'accepted':
return this.onDirectCallAccepted(params);
case 'rejected':
return this.onDirectCallRejected(params);
case 'confirmed':
return this.onDirectCallConfirmed(params);
case 'join':
return this.onDirectCallJoined(params);
case 'end':
return this.onDirectCallEnded(params);
}
}

private async notifyUser(uid: IUser['_id'], action: string, params: DirectCallParams): Promise<void> {
return Notifications.notifyUser(uid, 'video-conference', { action, params });
}

private connectUser(userId: string): void {
private async connectUser(userId: string): Promise<void> {
debug && console.log(`[VideoConf] connecting user ${userId}`);
this.userId = userId;

this.hookNotification('video-conference.call', (params: DirectCallParams) => this.onDirectCall(params));
this.hookNotification('video-conference.canceled', (params: DirectCallParams) => this.onDirectCallCanceled(params));
this.hookNotification('video-conference.accepted', (params: DirectCallParams) => this.onDirectCallAccepted(params));
this.hookNotification('video-conference.rejected', (params: DirectCallParams) => this.onDirectCallRejected(params));
this.hookNotification('video-conference.confirmed', (params: DirectCallParams) => this.onDirectCallConfirmed(params));
this.hookNotification('video-conference.join', (params: DirectCallParams) => this.onDirectCallJoined(params));
this.hookNotification('video-conference.end', (params: DirectCallParams) => this.onDirectCallEnded(params));
this.hooks.push(
await Notifications.onUser('video-conference', (data: { action: string; params: DirectCallParams }) =>
this.onVideoConfNotification(data),
),
);
}

private abortIncomingCall(callId: string): void {
Expand Down Expand Up @@ -621,7 +647,7 @@ export const VideoConfManager = new (class VideoConfManager extends Emitter<Vide
}

debug && console.log(`[VideoConf] Notifying user ${callData.uid} that they can join the call now.`);
Notifications.notifyUser(callData.uid, 'video-conference.confirmed', { callId: callData.callId, uid: this.userId, rid: callData.rid });
this.userId && this.notifyUser(callData.uid, 'confirmed', { callId: callData.callId, uid: this.userId, rid: callData.rid });
}

private onDirectCallConfirmed(params: DirectCallParams): void {
Expand Down
19 changes: 13 additions & 6 deletions apps/meteor/server/modules/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,25 @@ export class NotificationsModule {
if (e === 'webrtc') {
return true;
}
if (e.startsWith('video-conference.')) {
if (e === 'video-conference') {
if (!this.userId || !data || typeof data !== 'object') {
return false;
}

const callId = 'callId' in data && typeof (data as any).callId === 'string' ? (data as any).callId : '';
const uid = 'uid' in data && typeof (data as any).uid === 'string' ? (data as any).uid : '';
const rid = 'rid' in data && typeof (data as any).rid === 'string' ? (data as any).rid : '';
const { action: videoAction, params } = data as {
action: string | undefined;
params: { callId?: string; uid?: string; rid?: string };
};

if (!videoAction || typeof videoAction !== 'string' || !params || typeof params !== 'object') {
return false;
}

const action = e.replace('video-conference.', '');
const callId = 'callId' in params && typeof params.callId === 'string' ? params.callId : '';
const uid = 'uid' in params && typeof params.uid === 'string' ? params.uid : '';
const rid = 'rid' in params && typeof params.rid === 'string' ? params.rid : '';

return VideoConf.validateAction(action, this.userId, {
return VideoConf.validateAction(videoAction, this.userId, {
callId,
uid,
rid,
Expand Down

0 comments on commit c8f61e5

Please sign in to comment.