mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-14 10:33:09 +02:00
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
|
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':
|
||
|
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
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (message == null) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (message.is_read) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Update documents
|
||
|
await Message.update({
|
||
|
_id: id
|
||
|
}, {
|
||
|
$set: { is_read: true }
|
||
|
});
|
||
|
|
||
|
// Publish event
|
||
|
publishMessagingStream(message.user_id, user._id, 'read', id.toString());
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
}
|