mirror of
https://codeberg.org/yeentown/barkey
synced 2024-11-25 08:15:12 +00:00
wip
This commit is contained in:
parent
ff1a20d74a
commit
1436617aab
13 changed files with 234 additions and 14 deletions
|
@ -159,6 +159,7 @@
|
|||
"typescript": "2.6.1",
|
||||
"uuid": "3.1.0",
|
||||
"vhost": "3.0.2",
|
||||
"web-push": "^3.2.4",
|
||||
"websocket": "1.0.25",
|
||||
"xev": "2.0.0"
|
||||
}
|
||||
|
|
44
src/api/common/push-sw.ts
Normal file
44
src/api/common/push-sw.ts
Normal file
|
@ -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
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
|
@ -146,6 +146,11 @@ const endpoints: Endpoint[] = [
|
|||
name: 'aggregation/posts/reactions'
|
||||
},
|
||||
|
||||
{
|
||||
name: 'sw/register',
|
||||
withCredential: true
|
||||
},
|
||||
|
||||
{
|
||||
name: 'i',
|
||||
withCredential: true
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
50
src/api/endpoints/sw/register.ts
Normal file
50
src/api/endpoints/sw/register.ts
Normal file
|
@ -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<any>}
|
||||
*/
|
||||
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();
|
||||
});
|
|
@ -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);
|
||||
|
|
3
src/api/models/sw-subscription.ts
Normal file
3
src/api/models/sw-subscription.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import db from '../../db/mongodb';
|
||||
|
||||
export default db.get('sw_subscriptions') as any; // fuck type definition
|
|
@ -75,6 +75,14 @@ type Source = {
|
|||
analysis?: {
|
||||
mecab_command?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Service Worker
|
||||
*/
|
||||
sw?: {
|
||||
gcm_sender_id: string;
|
||||
gcm_api_key: string;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -109,7 +117,7 @@ export default function load() {
|
|||
const url = URL.parse(config.url);
|
||||
const head = url.host.split('.')[0];
|
||||
|
||||
if (head != 'misskey') {
|
||||
if (head != 'misskey' && head != 'localhost') {
|
||||
console.error(`プライマリドメインは、必ず「misskey」ドメインで始まっていなければなりません(現在の設定では「${head}」で始まっています)。例えば「https://misskey.xyz」「http://misskey.my.app.example.com」などが正しいプライマリURLです。`);
|
||||
process.exit();
|
||||
}
|
||||
|
|
|
@ -27,7 +27,9 @@
|
|||
// misskey.alice => misskey
|
||||
// misskey.strawberry.pasta => misskey
|
||||
// dev.misskey.arisu.tachibana => dev
|
||||
let app = url.host.split('.')[0];
|
||||
let app = url.host == 'localhost'
|
||||
? 'misskey'
|
||||
: url.host.split('.')[0];
|
||||
|
||||
// Detect the user language
|
||||
// Note: The default language is English
|
||||
|
|
|
@ -37,6 +37,11 @@ export default class MiOS extends EventEmitter {
|
|||
*/
|
||||
public stream: HomeStreamManager;
|
||||
|
||||
/**
|
||||
* A registration of service worker
|
||||
*/
|
||||
private swRegistration: ServiceWorkerRegistration = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
|
@ -44,6 +49,7 @@ export default class MiOS extends EventEmitter {
|
|||
this.init = this.init.bind(this);
|
||||
this.api = this.api.bind(this);
|
||||
this.getMeta = this.getMeta.bind(this);
|
||||
this.swSubscribe = this.swSubscribe.bind(this);
|
||||
//#endregion
|
||||
}
|
||||
|
||||
|
@ -126,6 +132,25 @@ export default class MiOS extends EventEmitter {
|
|||
|
||||
// Finish init
|
||||
callback();
|
||||
|
||||
//#region Service worker
|
||||
const isSwSupported =
|
||||
('serviceWorker' in navigator) && ('PushManager' in window);
|
||||
|
||||
if (isSwSupported && this.isSignedin) {
|
||||
// When service worker activated
|
||||
navigator.serviceWorker.ready.then(this.swSubscribe);
|
||||
|
||||
// Register service worker
|
||||
navigator.serviceWorker.register('/sw.js').then(registration => {
|
||||
// 登録成功
|
||||
console.info('ServiceWorker registration successful with scope: ', registration.scope);
|
||||
}).catch(err => {
|
||||
// 登録失敗 :(
|
||||
console.error('ServiceWorker registration failed: ', err);
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
};
|
||||
|
||||
// Get cached account data
|
||||
|
@ -147,6 +172,30 @@ export default class MiOS extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
private async swSubscribe(swRegistration: ServiceWorkerRegistration) {
|
||||
this.swRegistration = swRegistration;
|
||||
|
||||
// Subscribe
|
||||
this.swRegistration.pushManager.subscribe({
|
||||
// A boolean indicating that the returned push subscription
|
||||
// will only be used for messages whose effect is made visible to the user.
|
||||
userVisibleOnly: true
|
||||
}).then(subscription => {
|
||||
console.log('Subscribe OK:', subscription);
|
||||
|
||||
// Register
|
||||
this.api('sw/register', {
|
||||
endpoint: subscription.endpoint,
|
||||
auth: subscription.getKey('auth') ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))) : '',
|
||||
publickey: subscription.getKey('p256dh') ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))) : ''
|
||||
});
|
||||
}).then(() => {
|
||||
console.log('Server Stored Subscription.');
|
||||
}).catch(err => {
|
||||
console.error('Subscribe Error:', err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Misskey APIにリクエストします
|
||||
* @param endpoint エンドポイント名
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
const Url = new URL(location.href);
|
||||
const _url = new URL(location.href);
|
||||
|
||||
const isRoot = Url.host.split('.')[0] == 'misskey';
|
||||
const isRoot = _url.host == 'localhost'
|
||||
? true
|
||||
: _url.host.split('.')[0] == 'misskey';
|
||||
|
||||
const host = isRoot ? Url.host : Url.host.substring(Url.host.indexOf('.') + 1, Url.host.length);
|
||||
const scheme = Url.protocol;
|
||||
const host = isRoot ? _url.host : _url.host.substring(_url.host.indexOf('.') + 1, _url.host.length);
|
||||
const scheme = _url.protocol;
|
||||
const url = `${scheme}//${host}`;
|
||||
const apiUrl = `${scheme}//api.${host}`;
|
||||
const chUrl = `${scheme}//ch.${host}`;
|
||||
|
|
31
src/web/assets/sw.js
Normal file
31
src/web/assets/sw.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* 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',
|
||||
});
|
||||
}
|
||||
};
|
|
@ -37,28 +37,45 @@ app.use((req, res, next) => {
|
|||
* Static assets
|
||||
*/
|
||||
app.use(favicon(`${__dirname}/assets/favicon.ico`));
|
||||
app.get('/manifest.json', (req, res) => res.sendFile(`${__dirname}/assets/manifest.json`));
|
||||
app.get('/apple-touch-icon.png', (req, res) => res.sendFile(`${__dirname}/assets/apple-touch-icon.png`));
|
||||
app.use('/assets', express.static(`${__dirname}/assets`, {
|
||||
maxAge: ms('7 days')
|
||||
}));
|
||||
|
||||
app.get('/sw.js', (req, res) => res.sendFile(`${__dirname}/assets/sw.js`));
|
||||
|
||||
/**
|
||||
* Common API
|
||||
* Manifest
|
||||
*/
|
||||
app.get(/\/api:url/, require('./service/url-preview'));
|
||||
app.get('/manifest.json', (req, res) => {
|
||||
const manifest = require((`${__dirname}/assets/manifest.json`));
|
||||
|
||||
// Service Worker
|
||||
if (config.sw) {
|
||||
manifest['gcm_sender_id'] = config.sw.gcm_sender_id;
|
||||
}
|
||||
|
||||
res.send(manifest);
|
||||
});
|
||||
|
||||
/**
|
||||
* Serve config
|
||||
*/
|
||||
app.get('/config.json', (req, res) => {
|
||||
res.send({
|
||||
const conf = {
|
||||
recaptcha: {
|
||||
siteKey: config.recaptcha.siteKey
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
res.send(conf);
|
||||
});
|
||||
|
||||
/**
|
||||
* Common API
|
||||
*/
|
||||
app.get(/\/api:url/, require('./service/url-preview'));
|
||||
|
||||
/**
|
||||
* Routing
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue