2016-12-29 00:49:51 +02:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import * as redis from 'redis';
|
2017-03-20 06:54:59 +02:00
|
|
|
import * as debug from 'debug';
|
|
|
|
|
2017-08-30 11:45:23 +03:00
|
|
|
import User from '../models/user';
|
2017-03-20 06:54:59 +02:00
|
|
|
import serializePost from '../serializers/post';
|
|
|
|
|
|
|
|
const log = debug('misskey');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
export default function homeStream(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
|
|
|
|
// Subscribe Home stream channel
|
|
|
|
subscriber.subscribe(`misskey:user-stream:${user._id}`);
|
2017-03-20 06:54:59 +02:00
|
|
|
|
|
|
|
subscriber.on('message', async (channel, data) => {
|
|
|
|
switch (channel.split(':')[1]) {
|
|
|
|
case 'user-stream':
|
|
|
|
connection.send(data);
|
|
|
|
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 11:45:23 +03:00
|
|
|
case 'alive':
|
|
|
|
// Update lastUsedAt
|
|
|
|
User.update({ _id: user._id }, {
|
|
|
|
$set: {
|
|
|
|
last_used_at: new Date()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2017-03-20 06:54:59 +02:00
|
|
|
case 'capture':
|
2017-03-20 12:10:13 +02:00
|
|
|
if (!msg.id) return;
|
|
|
|
const postId = msg.id;
|
|
|
|
log(`CAPTURE: ${postId} by @${user.username}`);
|
|
|
|
subscriber.subscribe(`misskey:post-stream:${postId}`);
|
2017-03-20 06:54:59 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
}
|