summaryrefslogtreecommitdiff
path: root/src/stream.ts
blob: be7a8c4ba180771dd24d58056a3496e32ada6bef (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
import * as mongo from 'mongodb';
import Xev from 'xev';

const ev = new Xev();

type ID = string | mongo.ObjectID;

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

		ev.emit(channel, message);
}

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

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

export function publishNoteStream(noteId: ID, type: string): void {
	publish(`note-stream:${noteId}`, null, noteId);
}

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

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

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

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

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

export function publishLocalTimelineStream(note: any): void {
	publish('local-timeline', null, note);
}

export function publishHybridTimelineStream(userId: ID, note: any): void {
	publish(userId ? `hybrid-timeline:${userId}` : 'hybrid-timeline', null, note);
}

export function publishGlobalTimelineStream(note: any): void {
	publish('global-timeline', null, note);
}