summaryrefslogtreecommitdiff
path: root/src/stream.ts
blob: cec5fcf0f3fcde2ae2fc4238f4217df7eeb5b0ec (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as mongo from 'mongodb';
import * as redis from 'redis';
import config from './config';

type ID = string | mongo.ObjectID;

class MisskeyEvent {
	private redisClient: redis.RedisClient;

	constructor() {
		// Connect to Redis
		this.redisClient = redis.createClient(
			config.redis.port, config.redis.host);
	}

	public publishUserStream(userId: ID, type: string, value?: any): void {
		this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishDriveStream(userId: ID, type: string, value?: any): void {
		this.publish(`drive-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishNoteStream(noteId: ID, type: string, value?: any): void {
		this.publish(`note-stream:${noteId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishUserListStream(listId: ID, type: string, value?: any): void {
		this.publish(`user-list-stream:${listId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishMessagingStream(userId: ID, otherpartyId: ID, type: string, value?: any): void {
		this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishMessagingIndexStream(userId: ID, type: string, value?: any): void {
		this.publish(`messaging-index-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishReversiStream(userId: ID, type: string, value?: any): void {
		this.publish(`reversi-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishReversiGameStream(gameId: ID, type: string, value?: any): void {
		this.publish(`reversi-game-stream:${gameId}`, type, typeof value === 'undefined' ? null : value);
	}

	public publishLocalTimelineStream(note: any): void {
		this.redisClient.publish('misskey:local-timeline', JSON.stringify(note));
	}

	public publishHybridTimelineStream(userId: ID, note: any): void {
		this.redisClient.publish(userId ? `misskey:hybrid-timeline:${userId}` : 'misskey:hybrid-timeline', JSON.stringify(note));
	}

	public publishGlobalTimelineStream(note: any): void {
		this.redisClient.publish('misskey:global-timeline', JSON.stringify(note));
	}

	private publish(channel: string, type: string, value?: any): void {
		const message = value == null ?
			{ type: type } :
			{ type: type, body: value };

		this.redisClient.publish(`misskey:${channel}`, JSON.stringify(message));
	}
}

const ev = new MisskeyEvent();

export default ev.publishUserStream.bind(ev);

export const publishLocalTimelineStream = ev.publishLocalTimelineStream.bind(ev);
export const publishHybridTimelineStream = ev.publishHybridTimelineStream.bind(ev);
export const publishGlobalTimelineStream = ev.publishGlobalTimelineStream.bind(ev);
export const publishDriveStream = ev.publishDriveStream.bind(ev);
export const publishUserListStream = ev.publishUserListStream.bind(ev);
export const publishNoteStream = ev.publishNoteStream.bind(ev);
export const publishMessagingStream = ev.publishMessagingStream.bind(ev);
export const publishMessagingIndexStream = ev.publishMessagingIndexStream.bind(ev);
export const publishReversiStream = ev.publishReversiStream.bind(ev);
export const publishReversiGameStream = ev.publishReversiGameStream.bind(ev);