jormungandr-bite/src/stream.ts

98 lines
3.5 KiB
TypeScript
Raw Normal View History

2016-12-28 15:49:51 -07:00
import * as mongo from 'mongodb';
2018-07-29 16:20:27 -06:00
import Xev from 'xev';
2018-09-11 11:48:19 -06:00
import Meta, { IMeta } from './models/meta';
2017-11-16 07:46:36 -07:00
2018-07-29 16:20:27 -06:00
type ID = string | mongo.ObjectID;
2017-03-19 22:54:59 -06:00
2018-09-11 11:48:19 -06:00
class Publisher {
private ev: Xev;
private meta: IMeta;
2018-04-25 03:04:16 -06:00
2018-09-11 11:48:19 -06:00
constructor() {
this.ev = new Xev();
2017-05-24 05:50:17 -06:00
2018-09-11 11:48:19 -06:00
setInterval(async () => {
this.meta = await Meta.findOne({});
}, 5000);
}
2017-11-13 08:54:16 -07:00
2018-09-11 11:48:19 -06:00
public getMeta = async () => {
if (this.meta != null) return this.meta;
2018-03-06 19:40:40 -07:00
2018-09-11 11:48:19 -06:00
this.meta = await Meta.findOne({});
return this.meta;
}
2018-03-07 01:48:32 -07:00
2018-09-11 11:48:19 -06:00
private publish = (channel: string, type: string, value?: any): void => {
const message = type == null ? value : value == null ?
{ type: type } :
{ type: type, body: value };
2018-09-11 11:48:19 -06:00
this.ev.emit(channel, message);
}
2018-07-10 18:36:30 -06:00
2018-09-11 11:48:19 -06:00
public publishUserStream = (userId: ID, type: string, value?: any): void => {
this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
2018-09-11 11:48:19 -06:00
public publishDriveStream = (userId: ID, type: string, value?: any): void => {
this.publish(`drive-stream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
2017-05-24 05:50:17 -06:00
2018-09-11 11:48:19 -06:00
public publishNoteStream = (noteId: ID, type: string): void => {
this.publish(`note-stream:${noteId}`, null, noteId);
}
2016-12-28 15:49:51 -07:00
2018-09-11 11:48:19 -06:00
public publishUserListStream = (listId: ID, type: string, value?: any): void => {
this.publish(`user-list-stream:${listId}`, type, typeof value === 'undefined' ? null : value);
}
2016-12-28 15:49:51 -07:00
2018-09-11 11:48:19 -06:00
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);
}
2016-12-28 15:49:51 -07:00
2018-09-11 11:48:19 -06:00
public publishLocalTimelineStream = async (note: any): Promise<void> => {
const meta = await this.getMeta();
if (meta.disableLocalTimeline) return;
this.publish('local-timeline', null, note);
}
public publishHybridTimelineStream = async (userId: ID, note: any): Promise<void> => {
const meta = await this.getMeta();
if (meta.disableLocalTimeline) return;
this.publish(userId ? `hybrid-timeline:${userId}` : 'hybrid-timeline', null, note);
}
public publishGlobalTimelineStream = (note: any): void => {
this.publish('global-timeline', null, note);
}
2018-07-29 16:20:27 -06:00
}
2018-09-11 11:48:19 -06:00
const publisher = new Publisher();
export default publisher;
export const publishUserStream = publisher.publishUserStream;
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;
export const publishLocalTimelineStream = publisher.publishLocalTimelineStream;
export const publishHybridTimelineStream = publisher.publishHybridTimelineStream;
export const publishGlobalTimelineStream = publisher.publishGlobalTimelineStream;