2016-12-29 07:49:51 +09:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import * as redis from 'redis';
|
2018-03-31 19:55:00 +09:00
|
|
|
import swPush from './push-sw';
|
|
|
|
import config from '../conf';
|
2016-12-29 07:49:51 +09: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 17:37:01 +09:00
|
|
|
public publishUserStream(userId: ID, type: string, value?: any): void {
|
2016-12-29 07:49:51 +09:00
|
|
|
this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:40:09 +09:00
|
|
|
public publishSw(userId: ID, type: string, value?: any): void {
|
|
|
|
swPush(userId, type, value);
|
|
|
|
}
|
|
|
|
|
2017-11-16 23:46:36 +09:00
|
|
|
public publishDriveStream(userId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`drive-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-03-20 13:54:59 +09:00
|
|
|
public publishPostStream(postId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`post-stream:${postId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:37:01 +09:00
|
|
|
public publishMessagingStream(userId: ID, otherpartyId: ID, type: string, value?: any): void {
|
2016-12-29 07:49:51 +09:00
|
|
|
this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
2017-05-24 20:50:17 +09:00
|
|
|
|
2017-11-14 00:54:16 +09:00
|
|
|
public publishMessagingIndexStream(userId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`messaging-index-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2018-03-07 11:40:40 +09:00
|
|
|
public publishOthelloStream(userId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`othello-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2018-03-07 17:48:32 +09:00
|
|
|
public publishOthelloGameStream(gameId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`othello-game-stream:${gameId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:17:14 +09:00
|
|
|
public publishChannelStream(channelId: ID, type: string, value?: any): void {
|
|
|
|
this.publish(`channel-stream:${channelId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:50:17 +09:00
|
|
|
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));
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const ev = new MisskeyEvent();
|
|
|
|
|
|
|
|
export default ev.publishUserStream.bind(ev);
|
|
|
|
|
2017-11-21 03:40:09 +09:00
|
|
|
export const pushSw = ev.publishSw.bind(ev);
|
|
|
|
|
2017-11-16 23:46:36 +09:00
|
|
|
export const publishDriveStream = ev.publishDriveStream.bind(ev);
|
|
|
|
|
2017-03-20 13:54:59 +09:00
|
|
|
export const publishPostStream = ev.publishPostStream.bind(ev);
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
export const publishMessagingStream = ev.publishMessagingStream.bind(ev);
|
2017-11-01 03:17:14 +09:00
|
|
|
|
2017-11-14 00:54:16 +09:00
|
|
|
export const publishMessagingIndexStream = ev.publishMessagingIndexStream.bind(ev);
|
|
|
|
|
2018-03-07 11:40:40 +09:00
|
|
|
export const publishOthelloStream = ev.publishOthelloStream.bind(ev);
|
|
|
|
|
2018-03-07 17:48:32 +09:00
|
|
|
export const publishOthelloGameStream = ev.publishOthelloGameStream.bind(ev);
|
|
|
|
|
2017-11-01 03:17:14 +09:00
|
|
|
export const publishChannelStream = ev.publishChannelStream.bind(ev);
|