summaryrefslogtreecommitdiff
path: root/src/server/api/stream/messaging.ts
blob: 3e6c2cd509d20de909e345cb3e0db9730e1daf02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as websocket from 'websocket';
import * as redis from 'redis';
import read from '../common/read-messaging-message';
import { ParsedUrlQuery } from 'querystring';

export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
	const q = request.resourceURL.query as ParsedUrlQuery;
	const otherparty = q.otherparty as string;

	// 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;
				read(user._id, otherparty, msg.id);
				break;
		}
	});
}