summaryrefslogtreecommitdiff
path: root/src/api/event.ts
blob: 4a2e4e453dd02aaf814ea5c454b0bb88ddba546e (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
import * as mongo from 'mongodb';
import * as redis from 'redis';
import swPush from './common/push-sw';
import config from '../conf';

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 publishSw(userId: ID, type: string, value?: any): void {
		swPush(userId, type, value);
	}

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

	public publishPostStream(postId: ID, type: string, value?: any): void {
		this.publish(`post-stream:${postId}`, 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 publishChannelStream(channelId: ID, type: string, value?: any): void {
		this.publish(`channel-stream:${channelId}`, type, typeof value === 'undefined' ? null : value);
	}

	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 pushSw = ev.publishSw.bind(ev);

export const publishDriveStream = ev.publishDriveStream.bind(ev);

export const publishPostStream = ev.publishPostStream.bind(ev);

export const publishMessagingStream = ev.publishMessagingStream.bind(ev);

export const publishMessagingIndexStream = ev.publishMessagingIndexStream.bind(ev);

export const publishChannelStream = ev.publishChannelStream.bind(ev);