/* * Notification manager for SW */ declare var self: ServiceWorkerGlobalScope; import { getNoteSummary } from '@/misc/get-note-summary'; import getUserName from '@/misc/get-user-name'; import { swLang } from '@client/sw/lang'; import { I18n } from '@/misc/i18n'; import { pushNotificationData } from '@/types'; export async function createNotification(data: pushNotificationData) { const n = await composeNotification(data); if (n) await self.registration.showNotification(...n); else await createEmptyNotification(); } async function composeNotification(data: pushNotificationData): Promise<[string, NotificationOptions] | null | undefined> { if (!swLang.i18n) swLang.fetchLocale(); const i18n = await swLang.i18n as I18n<any>; const { t } = i18n; const { body } = data; switch (data.type) { /* case 'driveFileCreated': // TODO (Server Side) return [t('_notification.fileUploaded'), { body: body.name, icon: body.url, data }]; */ case 'notification': switch (body.type) { case 'follow': return [t('_notification.youWereFollowed'), { body: getUserName(body.user), icon: body.user.avatarUrl, data, actions: [ { action: 'follow', title: t('_notification._actions.followBack') } ], }]; case 'mention': return [t('_notification.youGotMention', { name: getUserName(body.user) }), { body: getNoteSummary(body.note, i18n.locale), icon: body.user.avatarUrl, data, actions: [ { action: 'reply', title: t('_notification._actions.reply') } ], }]; case 'reply': return [t('_notification.youGotReply', { name: getUserName(body.user) }), { body: getNoteSummary(body.note, i18n.locale), icon: body.user.avatarUrl, data, actions: [ { action: 'reply', title: t('_notification._actions.reply') } ], }]; case 'renote': return [t('_notification.youRenoted', { name: getUserName(body.user) }), { body: getNoteSummary(body.note.renote, i18n.locale), icon: body.user.avatarUrl, data, actions: [ { action: 'showUser', title: getUserName(body.user) } ], }]; case 'quote': return [t('_notification.youGotQuote', { name: getUserName(body.user) }), { body: getNoteSummary(body.note, i18n.locale), icon: body.user.avatarUrl, data, actions: [ { action: 'reply', title: t('_notification._actions.reply') }, { action: 'renote', title: t('_notification._actions.renote') } ], }]; case 'reaction': return [`${body.reaction} ${getUserName(body.user)}`, { body: getNoteSummary(body.note, i18n.locale), icon: body.user.avatarUrl, data, actions: [ { action: 'showUser', title: getUserName(body.user) } ], }]; case 'pollVote': return [t('_notification.youGotPoll', { name: getUserName(body.user) }), { body: getNoteSummary(body.note, i18n.locale), icon: body.user.avatarUrl, data, }]; case 'receiveFollowRequest': return [t('_notification.youReceivedFollowRequest'), { body: getUserName(body.user), icon: body.user.avatarUrl, data, actions: [ { action: 'accept', title: t('accept') }, { action: 'reject', title: t('reject') } ], }]; case 'followRequestAccepted': return [t('_notification.yourFollowRequestAccepted'), { body: getUserName(body.user), icon: body.user.avatarUrl, data, }]; case 'groupInvited': return [t('_notification.youWereInvitedToGroup', { userName: getUserName(body.user) }), { body: body.invitation.group.name, data, actions: [ { action: 'accept', title: t('accept') }, { action: 'reject', title: t('reject') } ], }]; case 'app': return [body.header, { body: body.body, icon: body.icon, data }]; default: return null; } case 'unreadMessagingMessage': if (body.groupId === null) { return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(body.user) }), { icon: body.user.avatarUrl, tag: `messaging:user:${body.userId}`, data, renotify: true, }]; } return [t('_notification.youGotMessagingMessageFromGroup', { name: body.group.name }), { icon: body.user.avatarUrl, tag: `messaging:group:${body.groupId}`, data, renotify: true, }]; default: return null; } } export async function createEmptyNotification() { if (!swLang.i18n) swLang.fetchLocale(); const i18n = await swLang.i18n as I18n<any>; const { t } = i18n; await self.registration.showNotification( t('_notification.emptyPushNotificationMessage'), { silent: true, tag: 'read_notification', } ); }