summaryrefslogtreecommitdiff
path: root/src/server/api/stream/othello.ts
blob: 55c993ec85c945e399b2cc2694aef82bad34e0b8 (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
27
28
29
import * as mongo from 'mongodb';
import * as websocket from 'websocket';
import * as redis from 'redis';
import Matching, { pack } from '../models/othello-matching';
import publishUserStream from '../event';

export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
	// Subscribe othello stream
	subscriber.subscribe(`misskey:othello-stream:${user._id}`);
	subscriber.on('message', (_, data) => {
		connection.send(data);
	});

	connection.on('message', async (data) => {
		const msg = JSON.parse(data.utf8Data);

		switch (msg.type) {
			case 'ping':
				if (msg.id == null) return;
				const matching = await Matching.findOne({
					parentId: user._id,
					childId: new mongo.ObjectID(msg.id)
				});
				if (matching == null) return;
				publishUserStream(matching.childId, 'othello_invited', await pack(matching, matching.childId));
				break;
		}
	});
}