2016-12-29 00:49:51 +02:00
|
|
|
import * as mongodb from 'mongodb';
|
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import * as redis from 'redis';
|
|
|
|
import Message from '../models/messaging-message';
|
|
|
|
import { publishMessagingStream } from '../event';
|
|
|
|
|
|
|
|
export default function messagingStream(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
|
|
|
|
const otherparty = request.resourceURL.query.otherparty;
|
|
|
|
|
|
|
|
// Subscribe messaging stream
|
|
|
|
subscriber.subscribe(`misskey:messaging-stream:${user._id}-${otherparty}`);
|
|
|
|
subscriber.on('message', (_, data) => {
|
|
|
|
connection.send(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.on('message', async (data) => {
|
|
|
|
const msg = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (msg.type) {
|
|
|
|
case 'read':
|
2017-03-20 12:10:13 +02:00
|
|
|
if (!msg.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = new mongodb.ObjectID(msg.id);
|
|
|
|
|
|
|
|
// Fetch message
|
|
|
|
// SELECT _id, user_id, is_read
|
|
|
|
const message = await Message.findOne({
|
|
|
|
_id: id,
|
|
|
|
recipient_id: user._id
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true,
|
|
|
|
user_id: true,
|
|
|
|
is_read: true
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
2017-03-20 12:10:13 +02:00
|
|
|
});
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-03-20 12:10:13 +02:00
|
|
|
if (message == null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-03-20 12:10:13 +02:00
|
|
|
if (message.is_read) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-03-20 12:10:13 +02:00
|
|
|
// Update documents
|
|
|
|
await Message.update({
|
|
|
|
_id: id
|
|
|
|
}, {
|
|
|
|
$set: { is_read: true }
|
|
|
|
});
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-03-20 12:10:13 +02:00
|
|
|
// Publish event
|
|
|
|
publishMessagingStream(message.user_id, user._id, 'read', id.toString());
|
2016-12-29 00:49:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|