This commit is contained in:
syuilo
2018-07-30 07:20:27 +09:00
parent 4e6dcd16ac
commit 83d9730d93
33 changed files with 189 additions and 242 deletions

View File

@ -1,82 +1,58 @@
import * as mongo from 'mongodb';
import * as redis from 'redis';
import config from './config';
import Xev from 'xev';
const ev = new Xev();
type ID = string | mongo.ObjectID;
class MisskeyEvent {
private redisClient: redis.RedisClient;
function publish(channel: string, type: string, value?: any): void {
const message = type == null ? value : value == null ?
{ type: type } :
{ type: type, body: value };
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));
}
ev.emit(channel, message);
}
const ev = new MisskeyEvent();
export function publishUserStream(userId: ID, type: string, value?: any): void {
publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
export default ev.publishUserStream.bind(ev);
export function publishDriveStream(userId: ID, type: string, value?: any): void {
publish(`drive-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
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);
export function publishNoteStream(noteId: ID, type: string): void {
publish(`note-stream:${noteId}`, type, 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);
}