misskey/src/server/api/stream/home.ts

115 lines
2.9 KiB
TypeScript
Raw Normal View History

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