2016-12-28 16:49:51 -06:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import * as redis from 'redis';
|
2017-03-19 23:54:59 -05:00
|
|
|
import * as debug from 'debug';
|
|
|
|
|
2017-08-30 03:45:23 -05:00
|
|
|
import User from '../models/user';
|
2017-12-21 16:26:23 -06:00
|
|
|
import Mute from '../models/mute';
|
2017-03-19 23:54:59 -05:00
|
|
|
import serializePost from '../serializers/post';
|
2017-10-30 08:12:10 -05:00
|
|
|
import readNotification from '../common/read-notification';
|
2017-03-19 23:54:59 -05:00
|
|
|
|
|
|
|
const log = debug('misskey');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-12-21 16:26:23 -06:00
|
|
|
export default async function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any) {
|
2016-12-28 16:49:51 -06:00
|
|
|
// Subscribe Home stream channel
|
|
|
|
subscriber.subscribe(`misskey:user-stream:${user._id}`);
|
2017-03-19 23:54:59 -05:00
|
|
|
|
2017-12-21 16:26:23 -06:00
|
|
|
const mute = await Mute.find({
|
|
|
|
muter_id: user._id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
const mutedUserIds = mute.map(m => m.mutee_id.toString());
|
|
|
|
|
2017-03-19 23:54:59 -05:00
|
|
|
subscriber.on('message', async (channel, data) => {
|
|
|
|
switch (channel.split(':')[1]) {
|
|
|
|
case 'user-stream':
|
2017-12-21 16:26:23 -06:00
|
|
|
try {
|
|
|
|
const x = JSON.parse(data);
|
|
|
|
|
|
|
|
if (x.type == 'post') {
|
|
|
|
if (mutedUserIds.indexOf(x.body.user_id) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (x.body.reply != null && mutedUserIds.indexOf(x.body.reply.user_id) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (x.body.repost != null && mutedUserIds.indexOf(x.body.repost.user_id) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (x.type == 'notification') {
|
|
|
|
if (mutedUserIds.indexOf(x.body.user_id) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.send(data);
|
|
|
|
} catch (e) {
|
|
|
|
connection.send(data);
|
|
|
|
}
|
2017-03-19 23:54:59 -05:00
|
|
|
break;
|
|
|
|
case 'post-stream':
|
|
|
|
const postId = channel.split(':')[2];
|
|
|
|
log(`RECEIVED: ${postId} ${data} by @${user.username}`);
|
|
|
|
const post = await serializePost(postId, user, {
|
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'post-updated',
|
|
|
|
body: {
|
|
|
|
post: post
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.on('message', data => {
|
|
|
|
const msg = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (msg.type) {
|
2017-08-30 03:45:23 -05:00
|
|
|
case 'alive':
|
|
|
|
// Update lastUsedAt
|
|
|
|
User.update({ _id: user._id }, {
|
|
|
|
$set: {
|
|
|
|
last_used_at: new Date()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2017-10-30 08:12:10 -05:00
|
|
|
case 'read_notification':
|
|
|
|
if (!msg.id) return;
|
|
|
|
readNotification(user._id, msg.id);
|
|
|
|
break;
|
|
|
|
|
2017-03-19 23:54:59 -05:00
|
|
|
case 'capture':
|
2017-03-20 05:10:13 -05:00
|
|
|
if (!msg.id) return;
|
|
|
|
const postId = msg.id;
|
|
|
|
log(`CAPTURE: ${postId} by @${user.username}`);
|
|
|
|
subscriber.subscribe(`misskey:post-stream:${postId}`);
|
2017-03-19 23:54:59 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
}
|