2016-12-28 16:49:51 -06:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import * as redis from 'redis';
|
2017-01-16 18:17:52 -06:00
|
|
|
import config from '../conf';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:37:01 -06:00
|
|
|
private publish(channel: string, type: string, value?: any): void {
|
2016-12-28 16:49:51 -06:00
|
|
|
const message = value == null ?
|
|
|
|
{ type: type } :
|
|
|
|
{ type: type, body: value };
|
|
|
|
|
|
|
|
this.redisClient.publish(`misskey:${channel}`, JSON.stringify(message));
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:37:01 -06:00
|
|
|
public publishUserStream(userId: ID, type: string, value?: any): void {
|
2016-12-28 16:49:51 -06:00
|
|
|
this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:54:59 -05:00
|
|
|
public publishPostStream(postId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`post-stream:${postId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:37:01 -06:00
|
|
|
public publishMessagingStream(userId: ID, otherpartyId: ID, type: string, value?: any): void {
|
2016-12-28 16:49:51 -06:00
|
|
|
this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ev = new MisskeyEvent();
|
|
|
|
|
|
|
|
export default ev.publishUserStream.bind(ev);
|
|
|
|
|
2017-03-19 23:54:59 -05:00
|
|
|
export const publishPostStream = ev.publishPostStream.bind(ev);
|
|
|
|
|
2016-12-28 16:49:51 -06:00
|
|
|
export const publishMessagingStream = ev.publishMessagingStream.bind(ev);
|