This commit is contained in:
syuilo 2017-11-21 05:09:45 +09:00
parent 3fa7215f39
commit c598a9cba2
7 changed files with 91 additions and 31 deletions

View file

@ -9,8 +9,7 @@ import User from '../../../models/user';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
import serialize from '../../../serializers/messaging-message'; import serialize from '../../../serializers/messaging-message';
import publishUserStream from '../../../event'; import publishUserStream from '../../../event';
import { publishMessagingStream } from '../../../event'; import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event';
import { publishMessagingIndexStream } from '../../../event';
import config from '../../../../conf'; 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 }); const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true });
if (!freshMessage.is_read) { if (!freshMessage.is_read) {
publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj); publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj);
pushSw(message.recipient_id, 'unread_messaging_message', messageObj);
} }
}, 3000); }, 3000);

View file

@ -6,6 +6,9 @@ import HomeStreamManager from './scripts/streaming/home-stream-manager';
import CONFIG from './scripts/config'; import CONFIG from './scripts/config';
import api from './scripts/api'; import api from './scripts/api';
declare var VERSION: string;
declare var LANG: string;
/** /**
* Misskey Operating System * Misskey Operating System
*/ */
@ -142,7 +145,7 @@ export default class MiOS extends EventEmitter {
navigator.serviceWorker.ready.then(this.swSubscribe); navigator.serviceWorker.ready.then(this.swSubscribe);
// Register service worker // 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); console.info('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(err => { }).catch(err => {

View file

@ -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;
}
}

View file

@ -11,9 +11,9 @@ import * as riot from 'riot';
import init from '../init'; import init from '../init';
import route from './router'; import route from './router';
import fuckAdBlock from './scripts/fuck-ad-block'; import fuckAdBlock from './scripts/fuck-ad-block';
import getPostSummary from '../../../common/get-post-summary';
import MiOS from '../common/mios'; import MiOS from '../common/mios';
import HomeStreamManager from '../common/scripts/streaming/home-stream-manager'; import HomeStreamManager from '../common/scripts/streaming/home-stream-manager';
import composeNotification from '../common/scripts/compose-notification';
/** /**
* init * init
@ -55,41 +55,46 @@ function registerNotifications(stream: HomeStreamManager) {
function attach(connection) { function attach(connection) {
connection.on('drive_file_created', file => { connection.on('drive_file_created', file => {
const n = new Notification('ファイルがアップロードされました', { const _n = composeNotification('drive_file_created', file);
body: file.name, const n = new Notification(_n.title, {
icon: file.url + '?thumbnail&size=64' body: _n.body,
icon: _n.icon
}); });
setTimeout(n.close.bind(n), 5000); setTimeout(n.close.bind(n), 5000);
}); });
connection.on('mention', post => { connection.on('mention', post => {
const n = new Notification(`${post.user.name}さんから:`, { const _n = composeNotification('mention', post);
body: getPostSummary(post), const n = new Notification(_n.title, {
icon: post.user.avatar_url + '?thumbnail&size=64' body: _n.body,
icon: _n.icon
}); });
setTimeout(n.close.bind(n), 6000); setTimeout(n.close.bind(n), 6000);
}); });
connection.on('reply', post => { connection.on('reply', post => {
const n = new Notification(`${post.user.name}さんから返信:`, { const _n = composeNotification('reply', post);
body: getPostSummary(post), const n = new Notification(_n.title, {
icon: post.user.avatar_url + '?thumbnail&size=64' body: _n.body,
icon: _n.icon
}); });
setTimeout(n.close.bind(n), 6000); setTimeout(n.close.bind(n), 6000);
}); });
connection.on('quote', post => { connection.on('quote', post => {
const n = new Notification(`${post.user.name}さんが引用:`, { const _n = composeNotification('quote', post);
body: getPostSummary(post), const n = new Notification(_n.title, {
icon: post.user.avatar_url + '?thumbnail&size=64' body: _n.body,
icon: _n.icon
}); });
setTimeout(n.close.bind(n), 6000); setTimeout(n.close.bind(n), 6000);
}); });
connection.on('unread_messaging_message', message => { connection.on('unread_messaging_message', message => {
const n = new Notification(`${message.user.name}さんからメッセージ:`, { const _n = composeNotification('unread_messaging_message', message);
body: message.text, // TODO: getMessagingMessageSummary(message), const n = new Notification(_n.title, {
icon: message.user.avatar_url + '?thumbnail&size=64' body: _n.body,
icon: _n.icon
}); });
n.onclick = () => { n.onclick = () => {
n.close(); n.close();

View file

@ -2,6 +2,8 @@
* Service Worker * Service Worker
*/ */
import composeNotification from './common/scripts/compose-notification';
// インストールされたとき // インストールされたとき
self.addEventListener('install', () => { self.addEventListener('install', () => {
console.log('[sw]', 'Your ServiceWorker is installed'); console.log('[sw]', 'Your ServiceWorker is installed');
@ -17,15 +19,12 @@ self.addEventListener('push', ev => {
if (clients.length != 0) return; if (clients.length != 0) return;
const { type, body } = ev.data.json(); const { type, body } = ev.data.json();
handlers[type](body); const n = composeNotification(type, body);
if (n) {
self.registration.showNotification(n.title, {
body: n.body,
icon: n.icon,
});
}
}); });
}); });
const handlers = {
mention: body => {
self.registration.showNotification('mentioned', {
body: body.text,
icon: body.user.avatar_url + '?thumbnail&size=64',
});
}
};

View file

@ -42,7 +42,7 @@ app.use('/assets', express.static(`${__dirname}/assets`, {
maxAge: ms('7 days') 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 * Manifest

View file

@ -20,7 +20,8 @@ module.exports = langs.map(([lang, locale]) => {
stats: './src/web/app/stats/script.ts', stats: './src/web/app/stats/script.ts',
status: './src/web/app/status/script.ts', status: './src/web/app/status/script.ts',
dev: './src/web/app/dev/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 = { const output = {