2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-30 14:52:07 +02:00
|
|
|
/*
|
|
|
|
* Notification manager for SW
|
|
|
|
*/
|
2023-09-29 01:04:53 +02:00
|
|
|
import type { BadgeNames, PushNotificationDataMap } from '@/types.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { char2fileName } from '@/scripts/twemoji-base.js';
|
|
|
|
import { cli } from '@/scripts/operations.js';
|
|
|
|
import { getAccountFromId } from '@/scripts/get-account-from-id.js';
|
|
|
|
import { swLang } from '@/scripts/lang.js';
|
|
|
|
import { getUserName } from '@/scripts/get-user-name.js';
|
2022-06-19 17:33:46 +02:00
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
const closeNotificationsByTags = async (tags: string[]): Promise<void> => {
|
2023-02-18 09:48:20 +01:00
|
|
|
for (const n of (await Promise.all(tags.map(tag => globalThis.registration.getNotifications({ tag })))).flat()) {
|
|
|
|
n.close();
|
|
|
|
}
|
2023-02-18 15:11:45 +01:00
|
|
|
};
|
2023-02-18 09:48:20 +01:00
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
const iconUrl = (name: BadgeNames): string => `/static-assets/tabler-badges/${name}.png`;
|
2023-01-08 08:47:57 +01:00
|
|
|
/* How to add a new badge:
|
|
|
|
* 1. Find the icon and download png from https://tabler-icons.io/
|
|
|
|
* 2. vips resize ~/Downloads/icon-name.png vipswork.png 0.4; vips scRGB2BW vipswork.png ~/icon-name.png"[compression=9,strip]"; rm vipswork.png;
|
|
|
|
* 3. mv ~/icon-name.png ~/misskey/packages/backend/assets/tabler-badges/
|
2023-04-11 07:11:39 +02:00
|
|
|
* 4. Add 'icon-name' to BadgeNames
|
2023-01-08 08:47:57 +01:00
|
|
|
* 5. Add `badge: iconUrl('icon-name'),`
|
|
|
|
*/
|
2022-04-30 14:52:07 +02:00
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
export async function createNotification<K extends keyof PushNotificationDataMap>(data: PushNotificationDataMap[K]): Promise<void> {
|
2022-04-30 14:52:07 +02:00
|
|
|
const n = await composeNotification(data);
|
|
|
|
|
|
|
|
if (n) {
|
2023-02-18 06:16:34 +01:00
|
|
|
return globalThis.registration.showNotification(...n);
|
2022-04-30 14:52:07 +02:00
|
|
|
} else {
|
|
|
|
console.error('Could not compose notification', data);
|
|
|
|
return createEmptyNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 14:10:06 +02:00
|
|
|
async function composeNotification(data: PushNotificationDataMap[keyof PushNotificationDataMap]): Promise<[string, NotificationOptions] | null> {
|
2023-04-11 18:07:24 +02:00
|
|
|
const i18n = await (swLang.i18n ?? swLang.fetchLocale());
|
2022-04-30 14:52:07 +02:00
|
|
|
switch (data.type) {
|
|
|
|
/*
|
|
|
|
case 'driveFileCreated': // TODO (Server Side)
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.fileUploaded, {
|
2022-04-30 14:52:07 +02:00
|
|
|
body: body.name,
|
|
|
|
icon: body.url,
|
|
|
|
data
|
|
|
|
}];
|
|
|
|
*/
|
|
|
|
case 'notification':
|
|
|
|
switch (data.body.type) {
|
2022-12-12 11:27:47 +01:00
|
|
|
case 'follow': {
|
2022-04-30 14:52:07 +02:00
|
|
|
// users/showの型定義をswos.apiへ当てはめるのが困難なのでapiFetch.requestを直接使用
|
|
|
|
const account = await getAccountFromId(data.userId);
|
|
|
|
if (!account) return null;
|
|
|
|
const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token);
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.youWereFollowed, {
|
2022-04-30 14:52:07 +02:00
|
|
|
body: getUserName(data.body.user),
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2022-06-19 17:33:46 +02:00
|
|
|
badge: iconUrl('user-plus'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: userDetail.isFollowing ? [] : [
|
|
|
|
{
|
|
|
|
action: 'follow',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts._notification._actions.followBack,
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
2022-12-12 11:27:47 +01:00
|
|
|
}
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
|
|
case 'mention':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.tsx._notification.youGotMention({ name: getUserName(data.body.user) }), {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2022-06-19 17:33:46 +02:00
|
|
|
badge: iconUrl('at'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'reply',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts._notification._actions.reply,
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
|
|
|
case 'reply':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.tsx._notification.youGotReply({ name: getUserName(data.body.user) }), {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('arrow-back-up'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'reply',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts._notification._actions.reply,
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
|
|
|
case 'renote':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.tsx._notification.youRenoted({ name: getUserName(data.body.user) }), {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('repeat'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'showUser',
|
2023-01-08 02:54:45 +01:00
|
|
|
title: getUserName(data.body.user),
|
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
|
|
|
case 'quote':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.tsx._notification.youGotQuote({ name: getUserName(data.body.user) }), {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('quote'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'reply',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts._notification._actions.reply,
|
2022-04-30 14:52:07 +02:00
|
|
|
},
|
|
|
|
...((data.body.note.visibility === 'public' || data.body.note.visibility === 'home') ? [
|
2023-01-08 02:54:45 +01:00
|
|
|
{
|
|
|
|
action: 'renote',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts._notification._actions.renote,
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
|
|
|
] : []),
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
2023-09-21 11:48:15 +02:00
|
|
|
case 'note':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.newNote + ': ' + getUserName(data.body.user), {
|
2023-09-21 11:48:15 +02:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-09-21 11:48:15 +02:00
|
|
|
data,
|
|
|
|
}];
|
|
|
|
|
2022-12-12 11:27:47 +01:00
|
|
|
case 'reaction': {
|
2022-06-19 17:33:46 +02:00
|
|
|
let reaction = data.body.reaction;
|
|
|
|
let badge: string | undefined;
|
|
|
|
|
|
|
|
if (reaction.startsWith(':')) {
|
|
|
|
// カスタム絵文字の場合
|
2023-02-28 11:55:31 +01:00
|
|
|
const name = reaction.substring(1, reaction.length - 1);
|
2023-04-11 18:07:24 +02:00
|
|
|
const badgeUrl = new URL(`/emoji/${name}.webp`, origin);
|
|
|
|
badgeUrl.searchParams.set('badge', '1');
|
|
|
|
badge = badgeUrl.href;
|
2023-02-28 11:55:31 +01:00
|
|
|
reaction = name.split('@')[0];
|
2022-06-19 17:33:46 +02:00
|
|
|
} else {
|
|
|
|
// Unicode絵文字の場合
|
|
|
|
badge = `/twemoji-badge/${char2fileName(reaction)}.png`;
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
if (await fetch(badge).then(res => res.status !== 200).catch(() => true)) {
|
2022-06-19 17:33:46 +02:00
|
|
|
badge = iconUrl('plus');
|
|
|
|
}
|
|
|
|
|
2023-02-18 09:48:20 +01:00
|
|
|
const tag = `reaction:${data.body.note.id}`;
|
2022-06-19 17:33:46 +02:00
|
|
|
return [`${reaction} ${getUserName(data.body.user)}`, {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: data.body.note.text ?? '',
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-02-28 11:55:31 +01:00
|
|
|
tag,
|
2022-06-19 17:33:46 +02:00
|
|
|
badge,
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'showUser',
|
2023-01-08 02:54:45 +01:00
|
|
|
title: getUserName(data.body.user),
|
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
2022-12-12 11:27:47 +01:00
|
|
|
}
|
2022-04-30 14:52:07 +02:00
|
|
|
|
|
|
|
case 'receiveFollowRequest':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.youReceivedFollowRequest, {
|
2022-04-30 14:52:07 +02:00
|
|
|
body: getUserName(data.body.user),
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('user-plus'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'accept',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts.accept,
|
2022-04-30 14:52:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'reject',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts.reject,
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
],
|
|
|
|
}];
|
|
|
|
|
|
|
|
case 'followRequestAccepted':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.yourFollowRequestAccepted, {
|
2022-04-30 14:52:07 +02:00
|
|
|
body: getUserName(data.body.user),
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('circle-check'),
|
2022-04-30 14:52:07 +02:00
|
|
|
data,
|
|
|
|
}];
|
|
|
|
|
2023-04-11 07:11:39 +02:00
|
|
|
case 'achievementEarned':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.achievementEarned, {
|
|
|
|
body: i18n.ts._achievements._types[`_${data.body.achievement}`].title,
|
2023-04-11 07:11:39 +02:00
|
|
|
badge: iconUrl('medal'),
|
|
|
|
data,
|
|
|
|
tag: `achievement:${data.body.achievement}`,
|
|
|
|
}];
|
|
|
|
|
2024-10-03 08:06:04 +02:00
|
|
|
case 'login':
|
|
|
|
return [i18n.ts._notification.login, {
|
|
|
|
badge: iconUrl('login-2'),
|
|
|
|
data,
|
|
|
|
}];
|
|
|
|
|
2024-09-26 07:15:03 +02:00
|
|
|
case 'exportCompleted': {
|
|
|
|
const entityName = {
|
|
|
|
antenna: i18n.ts.antennas,
|
|
|
|
blocking: i18n.ts.blockedUsers,
|
|
|
|
clip: i18n.ts.clips,
|
|
|
|
customEmoji: i18n.ts.customEmojis,
|
|
|
|
favorite: i18n.ts.favorites,
|
|
|
|
following: i18n.ts.following,
|
|
|
|
muting: i18n.ts.mutedUsers,
|
|
|
|
note: i18n.ts.notes,
|
|
|
|
userList: i18n.ts.lists,
|
|
|
|
} as const satisfies Record<typeof data.body.exportedEntity, string>;
|
|
|
|
|
|
|
|
return [i18n.tsx._notification.exportOfXCompleted({ x: entityName[data.body.exportedEntity] }), {
|
|
|
|
badge: iconUrl('circle-check'),
|
|
|
|
data,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2023-07-15 03:00:38 +02:00
|
|
|
case 'pollEnded':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.pollEnded, {
|
2023-07-15 03:00:38 +02:00
|
|
|
body: data.body.note.text ?? '',
|
|
|
|
badge: iconUrl('chart-arrows'),
|
|
|
|
data,
|
|
|
|
}];
|
|
|
|
|
2022-04-30 14:52:07 +02:00
|
|
|
case 'app':
|
2023-02-12 16:31:37 +01:00
|
|
|
return [data.body.header ?? data.body.body, {
|
|
|
|
body: data.body.header ? data.body.body : '',
|
|
|
|
icon: data.body.icon ?? undefined,
|
2023-01-08 02:54:45 +01:00
|
|
|
data,
|
|
|
|
}];
|
2022-04-30 14:52:07 +02:00
|
|
|
|
2023-11-01 05:34:05 +01:00
|
|
|
case 'test':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.ts._notification.testNotification, {
|
|
|
|
body: i18n.ts._notification.notificationWillBeDisplayedLikeThis,
|
2023-11-01 05:34:05 +01:00
|
|
|
badge: iconUrl('bell'),
|
|
|
|
data,
|
|
|
|
}];
|
2024-02-13 16:59:27 +01:00
|
|
|
|
2022-04-30 14:52:07 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2022-12-18 11:50:02 +01:00
|
|
|
case 'unreadAntennaNote':
|
2024-08-30 03:58:11 +02:00
|
|
|
return [i18n.tsx._notification.unreadAntennaNote({ name: data.body.antenna.name }), {
|
2023-02-12 16:31:37 +01:00
|
|
|
body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`,
|
2024-08-30 08:53:04 +02:00
|
|
|
icon: data.body.note.user.avatarUrl ?? undefined,
|
2023-01-08 08:47:57 +01:00
|
|
|
badge: iconUrl('antenna'),
|
2023-02-18 15:11:45 +01:00
|
|
|
tag: `antenna:${data.body.antenna.id}`,
|
2022-12-18 11:50:02 +01:00
|
|
|
data,
|
|
|
|
renotify: true,
|
|
|
|
}];
|
2022-04-30 14:52:07 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:07:24 +02:00
|
|
|
export async function createEmptyNotification(): Promise<void> {
|
2022-04-30 14:52:07 +02:00
|
|
|
return new Promise<void>(async res => {
|
2023-04-11 18:07:24 +02:00
|
|
|
const i18n = await (swLang.i18n ?? swLang.fetchLocale());
|
2023-02-12 16:31:37 +01:00
|
|
|
|
2023-02-18 06:16:34 +01:00
|
|
|
await globalThis.registration.showNotification(
|
2023-04-11 07:11:39 +02:00
|
|
|
(new URL(origin)).host,
|
2022-04-30 14:52:07 +02:00
|
|
|
{
|
2023-04-11 07:11:39 +02:00
|
|
|
body: `Misskey v${_VERSION_}`,
|
2022-04-30 14:52:07 +02:00
|
|
|
silent: true,
|
2022-06-19 17:33:46 +02:00
|
|
|
badge: iconUrl('null'),
|
2022-04-30 14:52:07 +02:00
|
|
|
tag: 'read_notification',
|
2023-04-11 07:11:39 +02:00
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'markAllAsRead',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts.markAllAsRead,
|
2023-04-11 07:11:39 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'settings',
|
2024-08-30 03:58:11 +02:00
|
|
|
title: i18n.ts.notificationSettings,
|
2023-04-11 07:11:39 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
data: {},
|
2023-01-08 02:54:45 +01:00
|
|
|
},
|
2022-04-30 14:52:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
2023-02-18 09:48:20 +01:00
|
|
|
try {
|
2023-04-11 07:11:39 +02:00
|
|
|
await closeNotificationsByTags(['user_visible_auto_notification']);
|
2023-02-18 09:48:20 +01:00
|
|
|
} finally {
|
|
|
|
res();
|
2022-04-30 14:52:07 +02:00
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|