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

232 lines
6.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';
2021-09-21 01:55:36 +09:00
import { pushNotificationDataMap } from '@client/sw/types';
import { apiFetch } from './operations';
import { getAccountFromId } from '@client/scripts/get-account-from-id';
2021-02-10 22:19:09 +09:00
2021-09-21 01:55:36 +09:00
export async function createNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
2021-02-10 22:19:09 +09:00
const n = await composeNotification(data);
2021-03-29 21:36:53 +09:00
2021-09-21 02:08:02 +09:00
if (n) {
return self.registration.showNotification(...n);
} else {
console.error('Could not compose notification', data);
return createEmptyNotification();
}
2021-02-10 22:19:09 +09:00
}
2021-09-21 02:08:02 +09:00
async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null> {
2021-02-10 22:19:09 +09:00
if (!swLang.i18n) swLang.fetchLocale();
const i18n = await swLang.i18n as I18n<any>;
const { t } = i18n;
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-09-21 01:55:36 +09:00
switch (data.body.type) {
2021-02-15 06:05:18 +09:00
case 'follow':
// users/showの型定義をswos.apiへ当てはめるのが困難なのでapiFetch.requestを直接使用
const account = await getAccountFromId(data.userId);
if (!account) return null;
const userDetail = apiFetch.request('users/show', { userId: data.body.userId }, account.token);
2021-02-15 06:05:18 +09:00
return [t('_notification.youWereFollowed'), {
2021-09-21 01:55:36 +09:00
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
2021-02-15 06:05:18 +09:00
data,
actions: userDetail.isFollowing ? [] : [
{
action: 'follow',
title: t('_notification._actions.followBack')
}
],
2021-02-15 06:05:18 +09:00
}];
2021-02-10 22:19:09 +09:00
case 'mention':
2021-09-21 01:55:36 +09:00
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.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-09-21 01:55:36 +09:00
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.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-09-21 01:55:36 +09:00
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note.renote, i18n.locale),
icon: data.body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'showUser',
2021-09-21 01:55:36 +09:00
title: getUserName(data.body.user)
2021-02-15 06:05:18 +09:00
}
],
2021-02-10 22:19:09 +09:00
}];
case 'quote':
2021-09-21 01:55:36 +09:00
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.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-09-21 03:32:48 +09:00
...((data.body.note.visibility === 'public' || data.body.note.visibility === 'home') ? [
2021-02-15 06:05:18 +09:00
{
action: 'renote',
title: t('_notification._actions.renote')
}
2021-09-21 03:32:48 +09:00
] : [])
2021-02-15 06:05:18 +09:00
],
2021-02-10 22:19:09 +09:00
}];
case 'reaction':
2021-09-21 01:55:36 +09:00
return [`${data.body.reaction} ${getUserName(data.body.user)}`, {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-15 06:05:18 +09:00
actions: [
{
action: 'showUser',
2021-09-21 01:55:36 +09:00
title: getUserName(data.body.user)
2021-02-15 06:05:18 +09:00
}
],
2021-02-10 22:19:09 +09:00
}];
case 'pollVote':
2021-09-21 01:55:36 +09:00
return [t('_notification.youGotPoll', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.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-09-21 01:55:36 +09:00
body: getUserName(data.body.user),
icon: data.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-09-21 01:55:36 +09:00
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
2021-02-13 01:28:20 +09:00
data,
2021-02-10 22:19:09 +09:00
}];
case 'groupInvited':
2021-09-21 02:08:02 +09:00
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(data.body.user) }), {
2021-09-21 01:55:36 +09:00
body: data.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
}];
2021-09-18 22:14:04 +09:00
case 'app':
2021-09-21 01:55:36 +09:00
return [data.body.header || data.body.body, {
body: data.body.header && data.body.body,
icon: data.body.icon,
2021-09-18 22:14:04 +09:00
data
}];
2021-09-21 01:55:36 +09:00
2021-02-10 22:19:09 +09:00
default:
return null;
}
case 'unreadMessagingMessage':
2021-09-21 01:55:36 +09:00
if (data.body.groupId === null) {
return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(data.body.user) }), {
icon: data.body.user.avatarUrl,
tag: `messaging:user:${data.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-09-21 01:55:36 +09:00
return [t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group.name }), {
icon: data.body.user.avatarUrl,
tag: `messaging:group:${data.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
2021-03-29 21:36:53 +09:00
export async function createEmptyNotification() {
2021-02-17 02:20:45 +09:00
if (!swLang.i18n) swLang.fetchLocale();
const i18n = await swLang.i18n as I18n<any>;
const { t } = i18n;
2021-03-29 21:36:53 +09:00
await self.registration.showNotification(
t('_notification.emptyPushNotificationMessage'),
{
2021-02-17 02:20:45 +09:00
silent: true,
2021-03-29 21:36:53 +09:00
tag: 'read_notification',
}
);
2021-09-21 02:08:02 +09:00
return new Promise<void>(res => {
setTimeout(async () => {
for (const n of
[
...(await self.registration.getNotifications({ tag: 'user_visible_auto_notification' })),
...(await self.registration.getNotifications({ tag: 'read_notification' }))
]
) {
n.close();
}
res();
}, 1000);
2021-09-21 02:09:13 +09:00
});
2021-02-17 02:20:45 +09:00
}