Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
126 lines
4.1 KiB
TypeScript
126 lines
4.1 KiB
TypeScript
import type { IMediaCall, IUser, MediaCallContact, MediaCallActorType } from '@rocket.chat/core-typings';
|
|
import type { VoipPushNotificationEventType } from '@rocket.chat/media-calls';
|
|
import { MediaCalls, Users } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { getPushNotificationType } from './getPushNotificationType';
|
|
import { metrics } from '../../../lib/metrics/lib/metrics';
|
|
import { Push } from '../../../lib/notifications/push/push';
|
|
import PushNotification from '../../../lib/notifications/push-config/lib/PushNotification';
|
|
import { getUserAvatarURL } from '../../../lib/utils/getUserAvatarURL';
|
|
import { getUserPreference } from '../../../lib/utils/lib/getUserPreference';
|
|
import { settings } from '../../../settings';
|
|
import { logger } from '../logger';
|
|
|
|
async function getActorUser(actor: MediaCallContact): Promise<Pick<IUser, '_id' | 'name' | 'username' | 'freeSwitchExtension'> | null> {
|
|
const options = { projection: { name: 1, username: 1, freeSwitchExtension: 1 } };
|
|
|
|
switch (actor.type) {
|
|
case 'user':
|
|
return Users.findOneById(actor.id, options);
|
|
case 'sip':
|
|
return Users.findOneByFreeSwitchExtension(actor.id, options);
|
|
}
|
|
}
|
|
|
|
async function getActorUserData(
|
|
actor: MediaCallContact,
|
|
): Promise<{ type: MediaCallActorType; id: string; name: string; avatarUrl?: string; username?: string }> {
|
|
const actorUsername = actor.type === 'user' ? actor.username : undefined;
|
|
const actorExtension = actor.sipExtension || (actor.type === 'sip' ? actor.id : undefined);
|
|
|
|
const data = {
|
|
type: actor.type,
|
|
id: actor.id,
|
|
name: actor.displayName || actorUsername || actorExtension || '',
|
|
} as const;
|
|
|
|
const user = await getActorUser(actor);
|
|
|
|
if (user) {
|
|
const username = user.username || actorUsername;
|
|
|
|
return {
|
|
...data,
|
|
name: user.name || user.username || user.freeSwitchExtension || data.name,
|
|
...(username && { username, avatarUrl: getUserAvatarURL(username) }),
|
|
};
|
|
}
|
|
|
|
return {
|
|
...data,
|
|
...(actorUsername && { username: actorUsername, avatarUrl: getUserAvatarURL(actorUsername) }),
|
|
};
|
|
}
|
|
|
|
async function sendVoipPushNotificationAsync(callId: IMediaCall['_id'], event: VoipPushNotificationEventType): Promise<void> {
|
|
if (settings.get('Push_enable') !== true || settings.get('VoIP_TeamCollab_Mobile_Ringing_Enabled') !== true) {
|
|
return;
|
|
}
|
|
|
|
const call = await MediaCalls.findOneById(callId);
|
|
if (!call) {
|
|
logger.error({ msg: 'Failed to send push notification: Media Call not found', callId });
|
|
return;
|
|
}
|
|
|
|
if (call.callee.type !== 'user') {
|
|
logger.error({ msg: 'Failed to send push notification: Invalid Callee Type', callId });
|
|
return;
|
|
}
|
|
|
|
const {
|
|
kind,
|
|
callee: { id: userId },
|
|
} = call;
|
|
|
|
// If the call was accepted, we don't need to notify when it ends
|
|
if (call.acceptedAt && event !== 'answer') {
|
|
return;
|
|
}
|
|
|
|
const type = getPushNotificationType(call);
|
|
// If the state changed before we had a chance to send the incoming call, skip it altogether
|
|
if (event === 'new' && type !== 'incoming_call') {
|
|
return;
|
|
}
|
|
if (type === 'incoming_call' && event !== 'new') {
|
|
return;
|
|
}
|
|
|
|
if (!(await getUserPreference(userId, 'enableMobileRinging'))) {
|
|
return;
|
|
}
|
|
|
|
const caller = await getActorUserData(call.caller);
|
|
|
|
metrics.notificationsSent.inc({ notification_type: 'mobile' });
|
|
const useVoipToken = type === 'incoming_call';
|
|
|
|
await Push.send({
|
|
useVoipToken,
|
|
priority: 10,
|
|
payload: {
|
|
host: Meteor.absoluteUrl(),
|
|
hostName: settings.get<string>('Site_Name'),
|
|
notificationType: 'voip',
|
|
type,
|
|
kind,
|
|
callId: call._id,
|
|
caller,
|
|
createdAt: call.createdAt.toISOString(),
|
|
},
|
|
...(useVoipToken && { apn: { expirationSeconds: 60 } }),
|
|
userId,
|
|
notId: PushNotification.getNotificationId(call._id),
|
|
// We should not send state change notifications to the device where the call was accepted/rejected
|
|
...(call.callee.contractId && { skipTokenId: call.callee.contractId }),
|
|
});
|
|
}
|
|
|
|
export function sendVoipPushNotification(callId: IMediaCall['_id'], event: VoipPushNotificationEventType): void {
|
|
void sendVoipPushNotificationAsync(callId, event).catch((err) => {
|
|
logger.error({ msg: 'Failed to send VoIP push notification', err, callId, event });
|
|
});
|
|
}
|