2016-12-29 07:49:51 +09:00
|
|
|
import * as websocket from 'websocket';
|
2018-07-30 07:20:27 +09:00
|
|
|
import Xev from 'xev';
|
2017-03-20 13:54:59 +09:00
|
|
|
import * as debug from 'debug';
|
|
|
|
|
2018-04-11 17:40:01 +09:00
|
|
|
import User, { IUser } from '../../../models/user';
|
2018-03-29 20:32:18 +09:00
|
|
|
import Mute from '../../../models/mute';
|
2018-06-12 18:54:36 +09:00
|
|
|
import { pack as packNote, pack } from '../../../models/note';
|
2017-10-30 22:12:10 +09:00
|
|
|
import readNotification from '../common/read-notification';
|
2018-04-11 17:40:01 +09:00
|
|
|
import call from '../call';
|
|
|
|
import { IApp } from '../../../models/app';
|
2017-03-20 13:54:59 +09:00
|
|
|
|
|
|
|
const log = debug('misskey');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-04-11 17:40:01 +09:00
|
|
|
export default async function(
|
|
|
|
request: websocket.request,
|
|
|
|
connection: websocket.connection,
|
2018-07-30 07:20:27 +09:00
|
|
|
subscriber: Xev,
|
2018-04-11 17:40:01 +09:00
|
|
|
user: IUser,
|
|
|
|
app: IApp
|
|
|
|
) {
|
2018-04-17 14:52:28 +09:00
|
|
|
const mute = await Mute.find({ muterId: user._id });
|
2018-03-29 14:48:47 +09:00
|
|
|
const mutedUserIds = mute.map(m => m.muteeId.toString());
|
2017-12-22 07:26:23 +09:00
|
|
|
|
2018-07-30 07:20:27 +09:00
|
|
|
async function onNoteStream(noteId: any) {
|
|
|
|
const note = await packNote(noteId, user, {
|
|
|
|
detail: true
|
|
|
|
});
|
2017-12-22 07:26:23 +09:00
|
|
|
|
2018-07-30 07:20:27 +09:00
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'note-updated',
|
|
|
|
body: {
|
|
|
|
note: note
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2018-06-12 18:54:36 +09:00
|
|
|
|
2018-07-30 07:20:27 +09:00
|
|
|
// Subscribe Home stream channel
|
|
|
|
subscriber.on(`user-stream:${user._id}`, async x => {
|
|
|
|
//#region 流れてきたメッセージがミュートしているユーザーが関わるものだったら無視する
|
|
|
|
if (x.type == 'note') {
|
|
|
|
if (mutedUserIds.includes(x.body.userId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (x.body.reply != null && mutedUserIds.includes(x.body.reply.userId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (x.body.renote != null && mutedUserIds.includes(x.body.renote.userId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (x.type == 'notification') {
|
|
|
|
if (mutedUserIds.includes(x.body.userId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
2018-04-23 18:00:58 +09:00
|
|
|
|
2018-07-30 07:20:27 +09:00
|
|
|
// Renoteなら再pack
|
|
|
|
if (x.type == 'note' && x.body.renoteId != null) {
|
|
|
|
x.body.renote = await pack(x.body.renoteId, user, {
|
|
|
|
detail: true
|
|
|
|
});
|
2017-03-20 13:54:59 +09:00
|
|
|
}
|
2018-07-30 07:20:27 +09:00
|
|
|
|
|
|
|
connection.send(JSON.stringify(x));
|
2017-03-20 13:54:59 +09:00
|
|
|
});
|
|
|
|
|
2018-04-29 17:54:50 +09:00
|
|
|
connection.on('message', async data => {
|
2017-03-20 13:54:59 +09:00
|
|
|
const msg = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (msg.type) {
|
2018-03-03 14:42:25 +09:00
|
|
|
case 'api':
|
2018-04-29 17:54:50 +09:00
|
|
|
// 新鮮なデータを利用するためにユーザーをフェッチ
|
|
|
|
call(msg.endpoint, await User.findOne({ _id: user._id }), app, msg.data).then(res => {
|
2018-04-11 17:40:01 +09:00
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: `api-res:${msg.id}`,
|
|
|
|
body: { res }
|
|
|
|
}));
|
|
|
|
}).catch(e => {
|
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: `api-res:${msg.id}`,
|
|
|
|
body: { e }
|
|
|
|
}));
|
|
|
|
});
|
2018-03-03 14:42:25 +09:00
|
|
|
break;
|
|
|
|
|
2017-08-30 17:45:23 +09:00
|
|
|
case 'alive':
|
|
|
|
// Update lastUsedAt
|
|
|
|
User.update({ _id: user._id }, {
|
|
|
|
$set: {
|
2018-04-08 03:58:11 +09:00
|
|
|
'lastUsedAt': new Date()
|
2017-08-30 17:45:23 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2017-10-30 22:12:10 +09:00
|
|
|
case 'read_notification':
|
|
|
|
if (!msg.id) return;
|
|
|
|
readNotification(user._id, msg.id);
|
|
|
|
break;
|
|
|
|
|
2017-03-20 13:54:59 +09:00
|
|
|
case 'capture':
|
2017-03-20 19:10:13 +09:00
|
|
|
if (!msg.id) return;
|
2018-07-30 07:20:27 +09:00
|
|
|
log(`CAPTURE: ${msg.id} by @${user.username}`);
|
|
|
|
subscriber.on(`note-stream:${msg.id}`, onNoteStream);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'decapture':
|
|
|
|
if (!msg.id) return;
|
|
|
|
log(`DECAPTURE: ${msg.id} by @${user.username}`);
|
|
|
|
subscriber.off(`note-stream:${msg.id}`, onNoteStream);
|
2017-03-20 13:54:59 +09:00
|
|
|
break;
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
}
|