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

96 lines
2.3 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
import * as websocket from 'websocket';
import * as redis from 'redis';
2017-03-20 13:54:59 +09:00
import * as debug from 'debug';
2018-03-29 20:32:18 +09:00
import User from '../../../models/user';
import Mute from '../../../models/mute';
import { pack as packPost } from '../../../models/post';
import readNotification from '../common/read-notification';
2017-03-20 13:54:59 +09:00
const log = debug('misskey');
2016-12-29 07:49:51 +09:00
2017-12-22 07:26:23 +09:00
export default async function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any) {
2016-12-29 07:49:51 +09:00
// Subscribe Home stream channel
subscriber.subscribe(`misskey:user-stream:${user._id}`);
2017-03-20 13:54:59 +09:00
2017-12-22 07:26:23 +09:00
const mute = await Mute.find({
2018-03-29 14:48:47 +09:00
muterId: user._id,
deletedAt: { $exists: false }
2017-12-22 07:26:23 +09:00
});
2018-03-29 14:48:47 +09:00
const mutedUserIds = mute.map(m => m.muteeId.toString());
2017-12-22 07:26:23 +09:00
2017-03-20 13:54:59 +09:00
subscriber.on('message', async (channel, data) => {
switch (channel.split(':')[1]) {
case 'user-stream':
2017-12-22 07:26:23 +09:00
try {
const x = JSON.parse(data);
if (x.type == 'post') {
2018-03-29 14:48:47 +09:00
if (mutedUserIds.indexOf(x.body.userId) != -1) {
2017-12-22 07:26:23 +09:00
return;
}
2018-03-29 14:48:47 +09:00
if (x.body.reply != null && mutedUserIds.indexOf(x.body.reply.userId) != -1) {
2017-12-22 07:26:23 +09:00
return;
}
2018-03-29 14:48:47 +09:00
if (x.body.repost != null && mutedUserIds.indexOf(x.body.repost.userId) != -1) {
2017-12-22 07:26:23 +09:00
return;
}
} else if (x.type == 'notification') {
2018-03-29 14:48:47 +09:00
if (mutedUserIds.indexOf(x.body.userId) != -1) {
2017-12-22 07:26:23 +09:00
return;
}
}
connection.send(data);
} catch (e) {
connection.send(data);
}
2017-03-20 13:54:59 +09:00
break;
case 'post-stream':
const postId = channel.split(':')[2];
log(`RECEIVED: ${postId} ${data} by @${user.username}`);
2018-02-04 14:52:33 +09:00
const post = await packPost(postId, user, {
2017-03-20 13:54:59 +09:00
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) {
2018-03-03 14:42:25 +09:00
case 'api':
// TODO
break;
2017-08-30 17:45:23 +09:00
case 'alive':
// Update lastUsedAt
User.update({ _id: user._id }, {
$set: {
2018-03-29 14:48:47 +09:00
'account.lastUsedAt': new Date()
2017-08-30 17:45:23 +09:00
}
});
break;
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;
const postId = msg.id;
log(`CAPTURE: ${postId} by @${user.username}`);
subscriber.subscribe(`misskey:post-stream:${postId}`);
2017-03-20 13:54:59 +09:00
break;
}
2016-12-29 07:49:51 +09:00
});
}