diff options
| author | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 12:58:53 +0900 |
|---|---|---|
| committer | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 12:58:53 +0900 |
| commit | 5b9f3701f58ca00c151498d16b6a839a91ba8643 (patch) | |
| tree | d32620bf206288b204060672a0088b4b0c7fa5eb /src/push-sw.ts | |
| parent | Merge branch 'master' of https://github.com/syuilo/misskey (diff) | |
| download | misskey-5b9f3701f58ca00c151498d16b6a839a91ba8643.tar.gz misskey-5b9f3701f58ca00c151498d16b6a839a91ba8643.tar.bz2 misskey-5b9f3701f58ca00c151498d16b6a839a91ba8643.zip | |
Abolish common and misc directories
Diffstat (limited to 'src/push-sw.ts')
| -rw-r--r-- | src/push-sw.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/push-sw.ts b/src/push-sw.ts new file mode 100644 index 0000000000..5b5ec52071 --- /dev/null +++ b/src/push-sw.ts @@ -0,0 +1,52 @@ +const push = require('web-push'); +import * as mongo from 'mongodb'; +import Subscription from './models/sw-subscription'; +import config from './conf'; + +if (config.sw) { + // アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録 + push.setVapidDetails( + config.maintainer.url, + config.sw.public_key, + config.sw.private_key); +} + +export default async function(userId: mongo.ObjectID | string, type, body?) { + if (!config.sw) return; + + if (typeof userId === 'string') { + userId = new mongo.ObjectID(userId); + } + + // Fetch + const subscriptions = await Subscription.find({ + userId: userId + }); + + subscriptions.forEach(subscription => { + const pushSubscription = { + endpoint: subscription.endpoint, + keys: { + auth: subscription.auth, + p256dh: subscription.publickey + } + }; + + push.sendNotification(pushSubscription, JSON.stringify({ + type, body + })).catch(err => { + //console.log(err.statusCode); + //console.log(err.headers); + //console.log(err.body); + + if (err.statusCode == 410) { + Subscription.remove({ + userId: userId, + endpoint: subscription.endpoint, + auth: subscription.auth, + publickey: subscription.publickey + }); + } + }); + }); +} |