summaryrefslogtreecommitdiff
path: root/src/stream.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-07-07 19:19:00 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-07-07 19:19:00 +0900
commitaa4ef6745ad798bd7d4f05cb397ef1dd85279814 (patch)
treed34ded516f52b91c4ff1a5443776ce22d8f483f0 /src/stream.ts
parentRefactorijg (diff)
downloadsharkey-aa4ef6745ad798bd7d4f05cb397ef1dd85279814.tar.gz
sharkey-aa4ef6745ad798bd7d4f05cb397ef1dd85279814.tar.bz2
sharkey-aa4ef6745ad798bd7d4f05cb397ef1dd85279814.zip
Refactorng
Diffstat (limited to 'src/stream.ts')
-rw-r--r--src/stream.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/stream.ts b/src/stream.ts
new file mode 100644
index 0000000000..da2f9aecd7
--- /dev/null
+++ b/src/stream.ts
@@ -0,0 +1,77 @@
+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 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 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);