2022-03-01 03:34:40 +09:00
|
|
|
import push from 'web-push';
|
2022-02-27 11:07:39 +09:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { SwSubscriptions } from '@/models/index.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2022-04-30 21:52:07 +09:00
|
|
|
// Defined also packages/sw/types.ts#L14-L21
|
|
|
|
type pushNotificationsTypes = {
|
|
|
|
'notification': Packed<'Notification'>;
|
|
|
|
'unreadMessagingMessage': Packed<'MessagingMessage'>;
|
|
|
|
'readNotifications': { notificationIds: string[] };
|
|
|
|
'readAllNotifications': undefined;
|
|
|
|
'readAllMessagingMessages': undefined;
|
|
|
|
'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string };
|
|
|
|
};
|
2020-05-23 13:19:31 +09:00
|
|
|
|
2021-12-25 02:01:35 +09:00
|
|
|
// プッシュメッセージサーバーには文字数制限があるため、内容を削減します
|
|
|
|
function truncateNotification(notification: Packed<'Notification'>): any {
|
|
|
|
if (notification.note) {
|
|
|
|
return {
|
|
|
|
...notification,
|
|
|
|
note: {
|
|
|
|
...notification.note,
|
|
|
|
// textをgetNoteSummaryしたものに置き換える
|
|
|
|
text: getNoteSummary(notification.type === 'renote' ? notification.note.renote as Packed<'Note'> : notification.note),
|
2022-04-30 21:52:07 +09:00
|
|
|
|
|
|
|
cw: undefined,
|
|
|
|
reply: undefined,
|
|
|
|
renote: undefined,
|
|
|
|
user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる
|
2021-12-25 02:01:35 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
2022-04-30 21:52:07 +09:00
|
|
|
export async function pushNotification<T extends keyof pushNotificationsTypes>(userId: string, type: T, body: pushNotificationsTypes[T]) {
|
2019-04-13 01:43:22 +09:00
|
|
|
const meta = await fetchMeta();
|
2018-12-20 04:08:13 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
|
|
|
push.setVapidDetails(config.url,
|
|
|
|
meta.swPublicKey,
|
|
|
|
meta.swPrivateKey);
|
2017-11-21 07:19:02 +09:00
|
|
|
|
2017-11-21 03:40:09 +09:00
|
|
|
// Fetch
|
2022-03-26 15:34:00 +09:00
|
|
|
const subscriptions = await SwSubscriptions.findBy({
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: userId,
|
2017-11-21 03:40:09 +09:00
|
|
|
});
|
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const subscription of subscriptions) {
|
2017-11-21 03:40:09 +09:00
|
|
|
const pushSubscription = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
auth: subscription.auth,
|
2021-12-09 23:58:30 +09:00
|
|
|
p256dh: subscription.publickey,
|
|
|
|
},
|
2017-11-21 03:40:09 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
push.sendNotification(pushSubscription, JSON.stringify({
|
2021-12-25 02:01:35 +09:00
|
|
|
type,
|
|
|
|
body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body,
|
|
|
|
userId,
|
2022-07-10 15:15:21 +09:00
|
|
|
dateTime: (new Date()).getTime(),
|
2019-10-14 01:53:28 +09:00
|
|
|
}), {
|
2021-12-09 23:58:30 +09:00
|
|
|
proxy: config.proxy,
|
2019-10-14 01:53:28 +09:00
|
|
|
}).catch((err: any) => {
|
2019-02-03 18:16:57 +09:00
|
|
|
//swLogger.info(err.statusCode);
|
|
|
|
//swLogger.info(err.headers);
|
|
|
|
//swLogger.info(err.body);
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2020-04-04 08:46:54 +09:00
|
|
|
if (err.statusCode === 410) {
|
2019-04-07 21:50:36 +09:00
|
|
|
SwSubscriptions.delete({
|
2018-03-29 14:48:47 +09:00
|
|
|
userId: userId,
|
2017-11-21 03:40:09 +09:00
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: subscription.auth,
|
2021-12-09 23:58:30 +09:00
|
|
|
publickey: subscription.publickey,
|
2017-11-21 03:40:09 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2017-11-21 03:40:09 +09:00
|
|
|
}
|