2019-01-29 07:36:24 +02:00
|
|
|
import * as push from 'web-push';
|
2021-08-19 15:55:45 +03:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { SwSubscriptions } from '@/models/index';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2021-09-22 16:35:55 +03:00
|
|
|
import { Packed } from '@/misc/schema';
|
2021-12-24 19:01:35 +02:00
|
|
|
import { getNoteSummary } from '@/misc/get-note-summary';
|
2017-11-20 20:40:09 +02:00
|
|
|
|
2020-05-23 07:19:31 +03:00
|
|
|
type notificationType = 'notification' | 'unreadMessagingMessage';
|
2021-09-22 16:35:55 +03:00
|
|
|
type notificationBody = Packed<'Notification'> | Packed<'MessagingMessage'>;
|
2020-05-23 07:19:31 +03:00
|
|
|
|
2021-12-24 19:01:35 +02: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),
|
|
|
|
...{
|
|
|
|
cw: undefined,
|
|
|
|
reply: undefined,
|
|
|
|
renote: undefined,
|
|
|
|
user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:19:31 +03:00
|
|
|
export default async function(userId: string, type: notificationType, body: notificationBody) {
|
2019-04-12 19:43:22 +03:00
|
|
|
const meta = await fetchMeta();
|
2018-12-19 21:08:13 +02:00
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;
|
2017-11-20 20:40:09 +02:00
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
|
|
|
push.setVapidDetails(config.url,
|
|
|
|
meta.swPublicKey,
|
|
|
|
meta.swPrivateKey);
|
2017-11-21 00:19:02 +02:00
|
|
|
|
2017-11-20 20:40:09 +02:00
|
|
|
// Fetch
|
2019-04-07 15:50:36 +03:00
|
|
|
const subscriptions = await SwSubscriptions.find({
|
2021-12-09 16:58:30 +02:00
|
|
|
userId: userId,
|
2017-11-20 20:40:09 +02:00
|
|
|
});
|
|
|
|
|
2018-12-11 13:36:55 +02:00
|
|
|
for (const subscription of subscriptions) {
|
2017-11-20 20:40:09 +02:00
|
|
|
const pushSubscription = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
auth: subscription.auth,
|
2021-12-09 16:58:30 +02:00
|
|
|
p256dh: subscription.publickey,
|
|
|
|
},
|
2017-11-20 20:40:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
push.sendNotification(pushSubscription, JSON.stringify({
|
2021-12-24 19:01:35 +02:00
|
|
|
type,
|
|
|
|
body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body,
|
|
|
|
userId,
|
2019-10-13 19:53:28 +03:00
|
|
|
}), {
|
2021-12-09 16:58:30 +02:00
|
|
|
proxy: config.proxy,
|
2019-10-13 19:53:28 +03:00
|
|
|
}).catch((err: any) => {
|
2019-02-03 11:16:57 +02:00
|
|
|
//swLogger.info(err.statusCode);
|
|
|
|
//swLogger.info(err.headers);
|
|
|
|
//swLogger.info(err.body);
|
2017-11-20 20:40:09 +02:00
|
|
|
|
2020-04-04 02:46:54 +03:00
|
|
|
if (err.statusCode === 410) {
|
2019-04-07 15:50:36 +03:00
|
|
|
SwSubscriptions.delete({
|
2018-03-29 08:48:47 +03:00
|
|
|
userId: userId,
|
2017-11-20 20:40:09 +02:00
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: subscription.auth,
|
2021-12-09 16:58:30 +02:00
|
|
|
publickey: subscription.publickey,
|
2017-11-20 20:40:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 13:36:55 +02:00
|
|
|
}
|
2017-11-20 20:40:09 +02:00
|
|
|
}
|