2019-01-29 14:36:24 +09:00
|
|
|
import * as push from 'web-push';
|
2021-08-19 21:55:45 +09:00
|
|
|
import config from '@/config/index';
|
|
|
|
import { SwSubscriptions } from '@/models/index';
|
2021-03-23 17:43:07 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2020-05-23 13:19:31 +09:00
|
|
|
import { PackedNotification } from '../models/repositories/notification';
|
|
|
|
import { PackedMessagingMessage } from '../models/repositories/messaging-message';
|
2021-02-10 22:19:09 +09:00
|
|
|
import { pushNotificationData } from '../types';
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2021-01-30 19:56:36 +09:00
|
|
|
type pushNotificationsTypes = {
|
|
|
|
'notification': PackedNotification;
|
|
|
|
'unreadMessagingMessage': PackedMessagingMessage;
|
|
|
|
'readNotifications': { notificationIds: string[] };
|
|
|
|
'readAllNotifications': undefined;
|
2021-02-15 06:05:18 +09:00
|
|
|
'readAllMessagingMessages': undefined;
|
|
|
|
'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string };
|
2021-01-30 19:56:36 +09:00
|
|
|
};
|
2020-05-23 13:19:31 +09:00
|
|
|
|
2021-02-10 01:54: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
|
2019-04-07 21:50:36 +09:00
|
|
|
const subscriptions = await SwSubscriptions.find({
|
2018-03-29 14:48:47 +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,
|
|
|
|
p256dh: subscription.publickey
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
push.sendNotification(pushSubscription, JSON.stringify({
|
2021-01-28 03:24:32 +09:00
|
|
|
type, body, userId
|
2021-02-10 22:19:09 +09:00
|
|
|
} as pushNotificationData), {
|
2019-10-14 01:53:28 +09:00
|
|
|
proxy: config.proxy
|
|
|
|
}).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,
|
|
|
|
publickey: subscription.publickey
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2017-11-21 03:40:09 +09:00
|
|
|
}
|