mizzkey/src/client/sw/create-notification.ts

209 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-02-10 22:19:09 +09:00
/*
* Notification manager for SW
*/
declare var self: ServiceWorkerGlobalScope;
2021-03-23 21:41:22 +09:00
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';
2021-02-10 22:19:09 +09:00
export async function createNotification(data: pushNotificationData) {
const n = await composeNotification(data);
if (n) return self.registration.showNotification(...n);
}
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;
2021-02-15 06:05:18 +09:00
const { body } = data;
2021-02-10 22:19:09 +09:00
switch (data.type) {
/*
case 'driveFileCreated': // TODO (Server Side)
return [t('_notification.fileUploaded'), {
2021-02-15 06:05:18 +09:00
body: body.name,
icon: body.url,
2021-02-10 22:19:09 +09:00
data
}];
*/
case 'notification':
2021-02-15 06:05:18 +09:00
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')
}
],
}];
2021-02-10 22:19:09 +09:00
case 'mention':
2021-02-15 06:05:18 +09:00
return [t('_notification.youGotMention', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
2021-02-10 22:19:09 +09:00
data,
actions: [
{
2021-02-15 06:05:18 +09:00
action: 'reply',
title: t('_notification._actions.reply')
2021-02-10 22:19:09 +09:00
}
2021-02-15 06:05:18 +09:00
],
2021-02-10 22:19:09 +09:00
}];
case 'reply':
2021-02-15 06:05:18 +09:00
return [t('_notification.youGotReply', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'reply',
title: t('_notification._actions.reply')
}
],
2021-02-10 22:19:09 +09:00
}];
case 'renote':
2021-02-15 06:05:18 +09:00
return [t('_notification.youRenoted', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note.renote, i18n.locale),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'showUser',
title: getUserName(body.user)
}
],
2021-02-10 22:19:09 +09:00
}];
case 'quote':
2021-02-15 06:05:18 +09:00
return [t('_notification.youGotQuote', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'reply',
title: t('_notification._actions.reply')
},
{
action: 'renote',
title: t('_notification._actions.renote')
}
],
2021-02-10 22:19:09 +09:00
}];
case 'reaction':
2021-02-15 06:05:18 +09:00
return [`${body.reaction} ${getUserName(body.user)}`, {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'showUser',
title: getUserName(body.user)
}
],
2021-02-10 22:19:09 +09:00
}];
case 'pollVote':
2021-02-15 06:05:18 +09:00
return [t('_notification.youGotPoll', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-10 22:19:09 +09:00
}];
case 'receiveFollowRequest':
return [t('_notification.youReceivedFollowRequest'), {
2021-02-15 06:05:18 +09:00
body: getUserName(body.user),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'accept',
title: t('accept')
},
{
action: 'reject',
title: t('reject')
}
],
2021-02-10 22:19:09 +09:00
}];
case 'followRequestAccepted':
return [t('_notification.yourFollowRequestAccepted'), {
2021-02-15 06:05:18 +09:00
body: getUserName(body.user),
icon: body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-10 22:19:09 +09:00
}];
case 'groupInvited':
2021-02-15 06:05:18 +09:00
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(body.user) }), {
body: body.invitation.group.name,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'accept',
title: t('accept')
},
{
action: 'reject',
title: t('reject')
}
],
2021-02-10 22:19:09 +09:00
}];
default:
return null;
}
case 'unreadMessagingMessage':
2021-02-15 06:05:18 +09:00
if (body.groupId === null) {
return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(body.user) }), {
icon: body.user.avatarUrl,
tag: `messaging:user:${body.userId}`,
2021-02-13 01:28:20 +09:00
data,
2021-02-17 02:20:45 +09:00
renotify: true,
2021-02-10 22:19:09 +09:00
}];
}
2021-02-15 06:05:18 +09:00
return [t('_notification.youGotMessagingMessageFromGroup', { name: body.group.name }), {
icon: body.user.avatarUrl,
tag: `messaging:group:${body.groupId}`,
2021-02-13 01:28:20 +09:00
data,
2021-02-17 02:20:45 +09:00
renotify: true,
2021-02-10 22:19:09 +09:00
}];
default:
return null;
}
}
2021-02-17 02:20:45 +09:00
export async function createAllReadNotification(type: 'notifications' | 'messagingMessages') {
const n = await composeAllReadNotification(type);
if (n) return self.registration.showNotification(...n);
}
async function composeAllReadNotification(type: string): Promise<[string, NotificationOptions] | null | undefined> {
if (!swLang.i18n) swLang.fetchLocale();
const i18n = await swLang.i18n as I18n<any>;
const { t } = i18n;
if (type === 'notifications') {
return [t('readAllNotifications'), {
silent: true,
tag: 'user_visible_auto_notification',
}];
}
if (type === 'messagingMessages') {
return [t('readAllMessagingMessages'), {
silent: true,
tag: 'user_visible_auto_notification',
}];
}
}