From 1436617aab030fa5b3760a3eeae37a1cdeeba2df Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 21 Nov 2017 03:40:09 +0900 Subject: wip --- src/api/common/push-sw.ts | 44 ++++++++++++++++++++++++++++++++++ src/api/endpoints.ts | 5 ++++ src/api/endpoints/posts/create.ts | 7 +++--- src/api/endpoints/sw/register.ts | 50 +++++++++++++++++++++++++++++++++++++++ src/api/event.ts | 7 ++++++ src/api/models/sw-subscription.ts | 3 +++ 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/api/common/push-sw.ts create mode 100644 src/api/endpoints/sw/register.ts create mode 100644 src/api/models/sw-subscription.ts (limited to 'src/api') diff --git a/src/api/common/push-sw.ts b/src/api/common/push-sw.ts new file mode 100644 index 0000000000..927dc50635 --- /dev/null +++ b/src/api/common/push-sw.ts @@ -0,0 +1,44 @@ +const push = require('web-push'); +import * as mongo from 'mongodb'; +import Subscription from '../models/sw-subscription'; +import config from '../../conf'; + +push.setGCMAPIKey(config.sw.gcm_api_key); + +export default async function(userId: mongo.ObjectID | string, type, body?) { + if (typeof userId === 'string') { + userId = new mongo.ObjectID(userId); + } + + // Fetch + const subscriptions = await Subscription.find({ + user_id: 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({ + user_id: userId, + endpoint: subscription.endpoint, + auth: subscription.auth, + publickey: subscription.publickey + }); + } + }); + }); +} diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 2783c92027..06fb9a64ae 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -146,6 +146,11 @@ const endpoints: Endpoint[] = [ name: 'aggregation/posts/reactions' }, + { + name: 'sw/register', + withCredential: true + }, + { name: 'i', withCredential: true diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 4f4b7e2e83..ae4959dae4 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -14,7 +14,7 @@ import ChannelWatching from '../../models/channel-watching'; import serialize from '../../serializers/post'; import notify from '../../common/notify'; import watch from '../../common/watch-post'; -import { default as event, publishChannelStream } from '../../event'; +import event, { pushSw, publishChannelStream } from '../../event'; import config from '../../../conf'; /** @@ -234,7 +234,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => { const mentions = []; - function addMention(mentionee, type) { + function addMention(mentionee, reason) { // Reject if already added if (mentions.some(x => x.equals(mentionee))) return; @@ -243,7 +243,8 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => { // Publish event if (!user._id.equals(mentionee)) { - event(mentionee, type, postObj); + event(mentionee, reason, postObj); + pushSw(mentionee, reason, postObj); } } diff --git a/src/api/endpoints/sw/register.ts b/src/api/endpoints/sw/register.ts new file mode 100644 index 0000000000..99406138db --- /dev/null +++ b/src/api/endpoints/sw/register.ts @@ -0,0 +1,50 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import Subscription from '../../models/sw-subscription'; + +/** + * subscribe service worker + * + * @param {any} params + * @param {any} user + * @param {any} _ + * @param {boolean} isSecure + * @return {Promise} + */ +module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { + // Get 'endpoint' parameter + const [endpoint, endpointErr] = $(params.endpoint).string().$; + if (endpointErr) return rej('invalid endpoint param'); + + // Get 'auth' parameter + const [auth, authErr] = $(params.auth).string().$; + if (authErr) return rej('invalid auth param'); + + // Get 'publickey' parameter + const [publickey, publickeyErr] = $(params.publickey).string().$; + if (publickeyErr) return rej('invalid publickey param'); + + // if already subscribed + const exist = await Subscription.findOne({ + user_id: user._id, + endpoint: endpoint, + auth: auth, + publickey: publickey, + deleted_at: { $exists: false } + }); + + if (exist !== null) { + return res(); + } + + await Subscription.insert({ + user_id: user._id, + endpoint: endpoint, + auth: auth, + publickey: publickey + }); + + res(); +}); diff --git a/src/api/event.ts b/src/api/event.ts index 8605a0f1e4..4a2e4e453d 100644 --- a/src/api/event.ts +++ b/src/api/event.ts @@ -1,5 +1,6 @@ import * as mongo from 'mongodb'; import * as redis from 'redis'; +import swPush from './common/push-sw'; import config from '../conf'; type ID = string | mongo.ObjectID; @@ -17,6 +18,10 @@ class MisskeyEvent { this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value); } + public publishSw(userId: ID, type: string, value?: any): void { + swPush(userId, type, value); + } + public publishDriveStream(userId: ID, type: string, value?: any): void { this.publish(`drive-stream:${userId}`, type, typeof value === 'undefined' ? null : value); } @@ -50,6 +55,8 @@ const ev = new MisskeyEvent(); export default ev.publishUserStream.bind(ev); +export const pushSw = ev.publishSw.bind(ev); + export const publishDriveStream = ev.publishDriveStream.bind(ev); export const publishPostStream = ev.publishPostStream.bind(ev); diff --git a/src/api/models/sw-subscription.ts b/src/api/models/sw-subscription.ts new file mode 100644 index 0000000000..ecca04cb91 --- /dev/null +++ b/src/api/models/sw-subscription.ts @@ -0,0 +1,3 @@ +import db from '../../db/mongodb'; + +export default db.get('sw_subscriptions') as any; // fuck type definition -- cgit v1.2.3-freya From c598a9cba2d4cdecf2aec3dcde1a893ee039bce3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 21 Nov 2017 05:09:45 +0900 Subject: wip --- src/api/endpoints/messaging/messages/create.ts | 4 +- src/web/app/common/mios.ts | 5 ++- src/web/app/common/scripts/compose-notification.ts | 52 ++++++++++++++++++++++ src/web/app/desktop/script.ts | 37 ++++++++------- src/web/app/sw.js | 30 +++++++++++++ src/web/assets/sw.js | 31 ------------- src/web/server.ts | 2 +- webpack/webpack.config.ts | 3 +- 8 files changed, 112 insertions(+), 52 deletions(-) create mode 100644 src/web/app/common/scripts/compose-notification.ts create mode 100644 src/web/app/sw.js delete mode 100644 src/web/assets/sw.js (limited to 'src/api') diff --git a/src/api/endpoints/messaging/messages/create.ts b/src/api/endpoints/messaging/messages/create.ts index 29a4671f84..3c7689f967 100644 --- a/src/api/endpoints/messaging/messages/create.ts +++ b/src/api/endpoints/messaging/messages/create.ts @@ -9,8 +9,7 @@ import User from '../../../models/user'; import DriveFile from '../../../models/drive-file'; import serialize from '../../../serializers/messaging-message'; import publishUserStream from '../../../event'; -import { publishMessagingStream } from '../../../event'; -import { publishMessagingIndexStream } from '../../../event'; +import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event'; import config from '../../../../conf'; /** @@ -99,6 +98,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => { const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true }); if (!freshMessage.is_read) { publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj); + pushSw(message.recipient_id, 'unread_messaging_message', messageObj); } }, 3000); diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts index 4a36d6375f..a98ef5f477 100644 --- a/src/web/app/common/mios.ts +++ b/src/web/app/common/mios.ts @@ -6,6 +6,9 @@ import HomeStreamManager from './scripts/streaming/home-stream-manager'; import CONFIG from './scripts/config'; import api from './scripts/api'; +declare var VERSION: string; +declare var LANG: string; + /** * Misskey Operating System */ @@ -142,7 +145,7 @@ export default class MiOS extends EventEmitter { navigator.serviceWorker.ready.then(this.swSubscribe); // Register service worker - navigator.serviceWorker.register('/sw.js').then(registration => { + navigator.serviceWorker.register(`/sw.${VERSION}.${LANG}.js`).then(registration => { // 登録成功 console.info('ServiceWorker registration successful with scope: ', registration.scope); }).catch(err => { diff --git a/src/web/app/common/scripts/compose-notification.ts b/src/web/app/common/scripts/compose-notification.ts new file mode 100644 index 0000000000..181dca734f --- /dev/null +++ b/src/web/app/common/scripts/compose-notification.ts @@ -0,0 +1,52 @@ +import getPostSummary from '../../../../common/get-post-summary'; + +type Notification = { + title: string; + body: string; + icon: string; + onclick?: any; +}; + +// TODO: i18n + +export default function(type, data): Notification { + switch (type) { + case 'drive_file_created': + return { + title: 'ファイルがアップロードされました', + body: data.name, + icon: data.url + '?thumbnail&size=64' + }; + + case 'mention': + return { + title: `${data.user.name}さんから:`, + body: getPostSummary(data), + icon: data.user.avatar_url + '?thumbnail&size=64' + }; + + case 'reply': + return { + title: `${data.user.name}さんから返信:`, + body: getPostSummary(data), + icon: data.user.avatar_url + '?thumbnail&size=64' + }; + + case 'quote': + return { + title: `${data.user.name}さんが引用:`, + body: getPostSummary(data), + icon: data.user.avatar_url + '?thumbnail&size=64' + }; + + case 'unread_messaging_message': + return { + title: `${data.user.name}さんからメッセージ:`, + body: data.text, // TODO: getMessagingMessageSummary(data), + icon: data.user.avatar_url + '?thumbnail&size=64' + }; + + default: + return null; + } +} diff --git a/src/web/app/desktop/script.ts b/src/web/app/desktop/script.ts index bc0fc8dfe3..694cb7879c 100644 --- a/src/web/app/desktop/script.ts +++ b/src/web/app/desktop/script.ts @@ -11,9 +11,9 @@ import * as riot from 'riot'; import init from '../init'; import route from './router'; import fuckAdBlock from './scripts/fuck-ad-block'; -import getPostSummary from '../../../common/get-post-summary'; import MiOS from '../common/mios'; import HomeStreamManager from '../common/scripts/streaming/home-stream-manager'; +import composeNotification from '../common/scripts/compose-notification'; /** * init @@ -55,41 +55,46 @@ function registerNotifications(stream: HomeStreamManager) { function attach(connection) { connection.on('drive_file_created', file => { - const n = new Notification('ファイルがアップロードされました', { - body: file.name, - icon: file.url + '?thumbnail&size=64' + const _n = composeNotification('drive_file_created', file); + const n = new Notification(_n.title, { + body: _n.body, + icon: _n.icon }); setTimeout(n.close.bind(n), 5000); }); connection.on('mention', post => { - const n = new Notification(`${post.user.name}さんから:`, { - body: getPostSummary(post), - icon: post.user.avatar_url + '?thumbnail&size=64' + const _n = composeNotification('mention', post); + const n = new Notification(_n.title, { + body: _n.body, + icon: _n.icon }); setTimeout(n.close.bind(n), 6000); }); connection.on('reply', post => { - const n = new Notification(`${post.user.name}さんから返信:`, { - body: getPostSummary(post), - icon: post.user.avatar_url + '?thumbnail&size=64' + const _n = composeNotification('reply', post); + const n = new Notification(_n.title, { + body: _n.body, + icon: _n.icon }); setTimeout(n.close.bind(n), 6000); }); connection.on('quote', post => { - const n = new Notification(`${post.user.name}さんが引用:`, { - body: getPostSummary(post), - icon: post.user.avatar_url + '?thumbnail&size=64' + const _n = composeNotification('quote', post); + const n = new Notification(_n.title, { + body: _n.body, + icon: _n.icon }); setTimeout(n.close.bind(n), 6000); }); connection.on('unread_messaging_message', message => { - const n = new Notification(`${message.user.name}さんからメッセージ:`, { - body: message.text, // TODO: getMessagingMessageSummary(message), - icon: message.user.avatar_url + '?thumbnail&size=64' + const _n = composeNotification('unread_messaging_message', message); + const n = new Notification(_n.title, { + body: _n.body, + icon: _n.icon }); n.onclick = () => { n.close(); diff --git a/src/web/app/sw.js b/src/web/app/sw.js new file mode 100644 index 0000000000..463ae0c75e --- /dev/null +++ b/src/web/app/sw.js @@ -0,0 +1,30 @@ +/** + * Service Worker + */ + +import composeNotification from './common/scripts/compose-notification'; + +// インストールされたとき +self.addEventListener('install', () => { + console.log('[sw]', 'Your ServiceWorker is installed'); +}); + +// プッシュ通知を受け取ったとき +self.addEventListener('push', ev => { + // クライアント取得 + self.clients.matchAll({ + includeUncontrolled: true + }).then(clients => { + // クライアントがあったらストリームに接続しているということなので通知しない + if (clients.length != 0) return; + + const { type, body } = ev.data.json(); + const n = composeNotification(type, body); + if (n) { + self.registration.showNotification(n.title, { + body: n.body, + icon: n.icon, + }); + } + }); +}); diff --git a/src/web/assets/sw.js b/src/web/assets/sw.js deleted file mode 100644 index 6a1251614a..0000000000 --- a/src/web/assets/sw.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Service Worker - */ - -// インストールされたとき -self.addEventListener('install', () => { - console.log('[sw]', 'Your ServiceWorker is installed'); -}); - -// プッシュ通知を受け取ったとき -self.addEventListener('push', ev => { - // クライアント取得 - self.clients.matchAll({ - includeUncontrolled: true - }).then(clients => { - // クライアントがあったらストリームに接続しているということなので通知しない - if (clients.length != 0) return; - - const { type, body } = ev.data.json(); - handlers[type](body); - }); -}); - -const handlers = { - mention: body => { - self.registration.showNotification('mentioned', { - body: body.text, - icon: body.user.avatar_url + '?thumbnail&size=64', - }); - } -}; diff --git a/src/web/server.ts b/src/web/server.ts index 300f3ed477..0be07b2d8b 100644 --- a/src/web/server.ts +++ b/src/web/server.ts @@ -42,7 +42,7 @@ app.use('/assets', express.static(`${__dirname}/assets`, { maxAge: ms('7 days') })); -app.get('/sw.js', (req, res) => res.sendFile(`${__dirname}/assets/sw.js`)); +app.get(/^\/sw\.(.+?)\.js$/, (req, res) => res.sendFile(`${__dirname}/assets/sw.${req.params[0]}.js`)); /** * Manifest diff --git a/webpack/webpack.config.ts b/webpack/webpack.config.ts index f2bcf48f31..753d89fede 100644 --- a/webpack/webpack.config.ts +++ b/webpack/webpack.config.ts @@ -20,7 +20,8 @@ module.exports = langs.map(([lang, locale]) => { stats: './src/web/app/stats/script.ts', status: './src/web/app/status/script.ts', dev: './src/web/app/dev/script.ts', - auth: './src/web/app/auth/script.ts' + auth: './src/web/app/auth/script.ts', + sw: './src/web/app/sw.js' }; const output = { -- cgit v1.2.3-freya From 6b7ed744677d48be40c76ffe0cde0524c1698dd0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 21 Nov 2017 07:19:02 +0900 Subject: Fix bug --- src/api/common/push-sw.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/common/push-sw.ts b/src/api/common/push-sw.ts index 927dc50635..782a4a6a6c 100644 --- a/src/api/common/push-sw.ts +++ b/src/api/common/push-sw.ts @@ -3,9 +3,13 @@ import * as mongo from 'mongodb'; import Subscription from '../models/sw-subscription'; import config from '../../conf'; -push.setGCMAPIKey(config.sw.gcm_api_key); +if (config.sw) { + push.setGCMAPIKey(config.sw.gcm_api_key); +} export default async function(userId: mongo.ObjectID | string, type, body?) { + if (!config.sw) return; + if (typeof userId === 'string') { userId = new mongo.ObjectID(userId); } -- cgit v1.2.3-freya