mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 08:33:08 +02:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import * as mongo from 'mongodb';
|
|
import * as redis from 'redis';
|
|
import config from '../conf';
|
|
|
|
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 publishPostStream(postId: ID, type: string, value?: any): void {
|
|
this.publish(`post-stream:${postId}`, 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);
|
|
}
|
|
|
|
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 publishPostStream = ev.publishPostStream.bind(ev);
|
|
|
|
export const publishMessagingStream = ev.publishMessagingStream.bind(ev);
|