2016-12-28 14:49:51 -08:00
|
|
|
import * as mongo from 'mongodb';
|
2018-10-11 02:09:41 -07:00
|
|
|
import redis from './db/redis';
|
2018-07-29 15:20:27 -07:00
|
|
|
import Xev from 'xev';
|
2018-11-05 14:14:43 -08:00
|
|
|
import { IMeta } from './models/meta';
|
|
|
|
import fetchMeta from './misc/fetch-meta';
|
2017-11-16 06:46:36 -08:00
|
|
|
|
2018-07-29 15:20:27 -07:00
|
|
|
type ID = string | mongo.ObjectID;
|
2017-03-19 21:54:59 -07:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
class Publisher {
|
|
|
|
private ev: Xev;
|
|
|
|
private meta: IMeta;
|
2018-04-25 02:04:16 -07:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
constructor() {
|
2018-10-11 02:09:41 -07:00
|
|
|
// Redisがインストールされてないときはプロセス間通信を使う
|
|
|
|
if (redis == null) {
|
|
|
|
this.ev = new Xev();
|
|
|
|
}
|
2017-05-24 04:50:17 -07:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
setInterval(async () => {
|
2018-11-05 14:14:43 -08:00
|
|
|
this.meta = await fetchMeta();
|
2018-09-11 10:48:19 -07:00
|
|
|
}, 5000);
|
|
|
|
}
|
2017-11-13 07:54:16 -08:00
|
|
|
|
2018-11-05 14:14:43 -08:00
|
|
|
public fetchMeta = async () => {
|
2018-09-11 10:48:19 -07:00
|
|
|
if (this.meta != null) return this.meta;
|
2018-03-06 18:40:40 -08:00
|
|
|
|
2018-11-05 14:14:43 -08:00
|
|
|
this.meta = await fetchMeta();
|
2018-09-11 10:48:19 -07:00
|
|
|
return this.meta;
|
|
|
|
}
|
2018-03-07 00:48:32 -08:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
private publish = (channel: string, type: string, value?: any): void => {
|
|
|
|
const message = type == null ? value : value == null ?
|
2018-10-07 09:56:36 -07:00
|
|
|
{ type: type, body: null } :
|
2018-09-11 10:48:19 -07:00
|
|
|
{ type: type, body: value };
|
2018-04-16 22:52:28 -07:00
|
|
|
|
2018-10-11 02:09:41 -07:00
|
|
|
if (this.ev) {
|
|
|
|
this.ev.emit(channel, message);
|
|
|
|
} else {
|
|
|
|
redis.publish('misskey', JSON.stringify({
|
|
|
|
channel: channel,
|
|
|
|
message: message
|
|
|
|
}));
|
|
|
|
}
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2018-07-10 17:36:30 -07:00
|
|
|
|
2018-10-06 19:06:17 -07:00
|
|
|
public publishMainStream = (userId: ID, type: string, value?: any): void => {
|
|
|
|
this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2018-04-16 22:52:28 -07:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
public publishDriveStream = (userId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2017-05-24 04:50:17 -07:00
|
|
|
|
2018-10-06 19:06:17 -07:00
|
|
|
public publishNoteStream = (noteId: ID, type: string, value: any): void => {
|
|
|
|
this.publish(`noteStream:${noteId}`, type, {
|
|
|
|
id: noteId,
|
|
|
|
body: value
|
|
|
|
});
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2016-12-28 14:49:51 -08:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
public publishUserListStream = (listId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2016-12-28 14:49:51 -08:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
public publishMessagingStream = (userId: ID, otherpartyId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public publishMessagingIndexStream = (userId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`messagingIndexStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public publishReversiStream = (userId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public publishReversiGameStream = (gameId: ID, type: string, value?: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(`reversiGameStream:${gameId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public publishHomeTimelineStream = (userId: ID, note: any): void => {
|
|
|
|
this.publish(`homeTimeline:${userId}`, null, note);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2016-12-28 14:49:51 -08:00
|
|
|
|
2018-09-11 10:48:19 -07:00
|
|
|
public publishLocalTimelineStream = async (note: any): Promise<void> => {
|
2018-11-05 14:14:43 -08:00
|
|
|
const meta = await this.fetchMeta();
|
2018-09-11 10:48:19 -07:00
|
|
|
if (meta.disableLocalTimeline) return;
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish('localTimeline', null, note);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public publishHybridTimelineStream = async (userId: ID, note: any): Promise<void> => {
|
2018-11-05 14:14:43 -08:00
|
|
|
const meta = await this.fetchMeta();
|
2018-09-11 10:48:19 -07:00
|
|
|
if (meta.disableLocalTimeline) return;
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish(userId ? `hybridTimeline:${userId}` : 'hybridTimeline', null, note);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public publishGlobalTimelineStream = (note: any): void => {
|
2018-10-06 19:06:17 -07:00
|
|
|
this.publish('globalTimeline', null, note);
|
2018-09-11 10:48:19 -07:00
|
|
|
}
|
2018-09-16 17:00:20 -07:00
|
|
|
|
|
|
|
public publishHashtagStream = (note: any): void => {
|
|
|
|
this.publish('hashtag', null, note);
|
|
|
|
}
|
2018-11-02 19:38:00 -07:00
|
|
|
|
|
|
|
public publishApLogStream = (log: any): void => {
|
|
|
|
this.publish('apLog', null, log);
|
|
|
|
}
|
2018-07-29 15:20:27 -07:00
|
|
|
}
|
2018-09-11 10:48:19 -07:00
|
|
|
|
|
|
|
const publisher = new Publisher();
|
|
|
|
|
|
|
|
export default publisher;
|
|
|
|
|
2018-10-06 19:06:17 -07:00
|
|
|
export const publishMainStream = publisher.publishMainStream;
|
2018-09-11 10:48:19 -07:00
|
|
|
export const publishDriveStream = publisher.publishDriveStream;
|
|
|
|
export const publishNoteStream = publisher.publishNoteStream;
|
|
|
|
export const publishUserListStream = publisher.publishUserListStream;
|
|
|
|
export const publishMessagingStream = publisher.publishMessagingStream;
|
|
|
|
export const publishMessagingIndexStream = publisher.publishMessagingIndexStream;
|
|
|
|
export const publishReversiStream = publisher.publishReversiStream;
|
|
|
|
export const publishReversiGameStream = publisher.publishReversiGameStream;
|
2018-10-06 19:06:17 -07:00
|
|
|
export const publishHomeTimelineStream = publisher.publishHomeTimelineStream;
|
2018-09-11 10:48:19 -07:00
|
|
|
export const publishLocalTimelineStream = publisher.publishLocalTimelineStream;
|
|
|
|
export const publishHybridTimelineStream = publisher.publishHybridTimelineStream;
|
|
|
|
export const publishGlobalTimelineStream = publisher.publishGlobalTimelineStream;
|
2018-09-16 17:00:20 -07:00
|
|
|
export const publishHashtagStream = publisher.publishHashtagStream;
|
2018-11-02 19:38:00 -07:00
|
|
|
export const publishApLogStream = publisher.publishApLogStream;
|