2021-03-23 05:56:01 +00:00
|
|
|
import { redisClient } from '../db/redis';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { User } from '../models/entities/user';
|
|
|
|
import { Note } from '../models/entities/note';
|
|
|
|
import { UserList } from '../models/entities/user-list';
|
|
|
|
import { ReversiGame } from '../models/entities/games/reversi/game';
|
2019-05-18 11:36:33 +00:00
|
|
|
import { UserGroup } from '../models/entities/user-group';
|
2019-12-14 18:35:09 +00:00
|
|
|
import config from '../config';
|
2020-01-29 19:37:25 +00:00
|
|
|
import { Antenna } from '../models/entities/antenna';
|
2021-02-20 11:20:05 +00:00
|
|
|
import { Channel } from '../models/entities/channel';
|
2017-03-20 04:54:59 +00:00
|
|
|
|
2018-09-11 17:48:19 +00:00
|
|
|
class Publisher {
|
2019-04-12 16:43:22 +00:00
|
|
|
private publish = (channel: string, type: string | null, value?: any): void => {
|
2018-09-11 17:48:19 +00:00
|
|
|
const message = type == null ? value : value == null ?
|
2018-10-07 16:56:36 +00:00
|
|
|
{ type: type, body: null } :
|
2018-09-11 17:48:19 +00:00
|
|
|
{ type: type, body: value };
|
2018-04-17 05:52:28 +00:00
|
|
|
|
2021-03-23 05:56:01 +00:00
|
|
|
redisClient.publish(config.host, JSON.stringify({
|
2019-04-13 10:19:32 +00:00
|
|
|
channel: channel,
|
|
|
|
message: message
|
|
|
|
}));
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
2018-07-11 00:36:30 +00:00
|
|
|
|
2021-03-23 06:06:56 +00:00
|
|
|
public publishInternalEvent = (type: string, value?: any): void => {
|
|
|
|
this.publish('internal', type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2021-03-21 06:14:03 +00:00
|
|
|
public publishUserEvent = (userId: User['id'], type: string, value?: any): void => {
|
|
|
|
this.publish(`user:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:17:17 +00:00
|
|
|
public publishBroadcastStream = (type: string, value?: any): void => {
|
|
|
|
this.publish('broadcast', type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishMainStream = (userId: User['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
2018-04-17 05:52:28 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishDriveStream = (userId: User['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
2017-05-24 11:50:17 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishNoteStream = (noteId: Note['id'], type: string, value: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`noteStream:${noteId}`, type, {
|
|
|
|
id: noteId,
|
|
|
|
body: value
|
|
|
|
});
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2021-02-20 11:20:05 +00:00
|
|
|
public publishChannelStream = (channelId: Channel['id'], type: string, value?: any): void => {
|
|
|
|
this.publish(`channelStream:${channelId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishUserListStream = (listId: UserList['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
public publishAntennaStream = (antennaId: Antenna['id'], type: string, value?: any): void => {
|
|
|
|
this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishMessagingStream = (userId: User['id'], otherpartyId: User['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 11:36:33 +00:00
|
|
|
public publishGroupMessagingStream = (groupId: UserGroup['id'], type: string, value?: any): void => {
|
|
|
|
this.publish(`messagingStream:${groupId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishMessagingIndexStream = (userId: User['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`messagingIndexStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishReversiStream = (userId: User['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
2018-09-11 17:48:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishReversiGameStream = (gameId: ReversiGame['id'], type: string, value?: any): void => {
|
2018-10-07 02:06:17 +00:00
|
|
|
this.publish(`reversiGameStream:${gameId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishNotesStream = (note: any): void => {
|
|
|
|
this.publish('notesStream', null, note);
|
2018-09-17 00:00:20 +00:00
|
|
|
}
|
2018-11-03 02:38:00 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
public publishAdminStream = (userId: User['id'], type: string, value?: any): void => {
|
2019-01-27 05:55:02 +00:00
|
|
|
this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
2018-07-29 22:20:27 +00:00
|
|
|
}
|
2018-09-11 17:48:19 +00:00
|
|
|
|
|
|
|
const publisher = new Publisher();
|
|
|
|
|
|
|
|
export default publisher;
|
|
|
|
|
2021-03-23 06:06:56 +00:00
|
|
|
export const publishInternalEvent = publisher.publishInternalEvent;
|
2021-03-21 06:14:03 +00:00
|
|
|
export const publishUserEvent = publisher.publishUserEvent;
|
2020-04-02 13:17:17 +00:00
|
|
|
export const publishBroadcastStream = publisher.publishBroadcastStream;
|
2018-10-07 02:06:17 +00:00
|
|
|
export const publishMainStream = publisher.publishMainStream;
|
2018-09-11 17:48:19 +00:00
|
|
|
export const publishDriveStream = publisher.publishDriveStream;
|
|
|
|
export const publishNoteStream = publisher.publishNoteStream;
|
2019-04-07 12:50:36 +00:00
|
|
|
export const publishNotesStream = publisher.publishNotesStream;
|
2021-02-20 11:20:05 +00:00
|
|
|
export const publishChannelStream = publisher.publishChannelStream;
|
2018-09-11 17:48:19 +00:00
|
|
|
export const publishUserListStream = publisher.publishUserListStream;
|
2020-01-29 19:37:25 +00:00
|
|
|
export const publishAntennaStream = publisher.publishAntennaStream;
|
2018-09-11 17:48:19 +00:00
|
|
|
export const publishMessagingStream = publisher.publishMessagingStream;
|
2019-05-18 11:36:33 +00:00
|
|
|
export const publishGroupMessagingStream = publisher.publishGroupMessagingStream;
|
2018-09-11 17:48:19 +00:00
|
|
|
export const publishMessagingIndexStream = publisher.publishMessagingIndexStream;
|
|
|
|
export const publishReversiStream = publisher.publishReversiStream;
|
|
|
|
export const publishReversiGameStream = publisher.publishReversiGameStream;
|
2019-01-27 05:55:02 +00:00
|
|
|
export const publishAdminStream = publisher.publishAdminStream;
|