Initial commit 🍀
This commit is contained in:
10
src/api/stream/home.ts
Normal file
10
src/api/stream/home.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import * as websocket from 'websocket';
|
||||
import * as redis from 'redis';
|
||||
|
||||
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}`);
|
||||
subscriber.on('message', (_, data) => {
|
||||
connection.send(data);
|
||||
});
|
||||
}
|
60
src/api/stream/messaging.ts
Normal file
60
src/api/stream/messaging.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user