2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import Redis from 'ioredis';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { UserList } from '@/models/entities/UserList.js';
|
|
|
|
import type { Antenna } from '@/models/entities/Antenna.js';
|
|
|
|
import type {
|
|
|
|
StreamChannels,
|
|
|
|
AdminStreamTypes,
|
|
|
|
AntennaStreamTypes,
|
|
|
|
BroadcastTypes,
|
|
|
|
DriveStreamTypes,
|
|
|
|
InternalStreamTypes,
|
|
|
|
MainStreamTypes,
|
|
|
|
NoteStreamTypes,
|
|
|
|
UserListStreamTypes,
|
|
|
|
} from '@/server/api/stream/types.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GlobalEventService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2023-04-09 08:09:27 +00:00
|
|
|
@Inject(DI.redisForPub)
|
|
|
|
private redisForPub: Redis.Redis,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
private publish(channel: StreamChannels, type: string | null, value?: any): void {
|
|
|
|
const message = type == null ? value : value == null ?
|
|
|
|
{ type: type, body: null } :
|
|
|
|
{ type: type, body: value };
|
|
|
|
|
2023-04-09 08:09:27 +00:00
|
|
|
this.redisForPub.publish(this.config.host, JSON.stringify({
|
2022-09-17 18:27:08 +00:00
|
|
|
channel: channel,
|
|
|
|
message: message,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishInternalEvent<K extends keyof InternalStreamTypes>(type: K, value?: InternalStreamTypes[K]): void {
|
|
|
|
this.publish('internal', type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishBroadcastStream<K extends keyof BroadcastTypes>(type: K, value?: BroadcastTypes[K]): void {
|
|
|
|
this.publish('broadcast', type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishMainStream<K extends keyof MainStreamTypes>(userId: User['id'], type: K, value?: MainStreamTypes[K]): void {
|
|
|
|
this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishDriveStream<K extends keyof DriveStreamTypes>(userId: User['id'], type: K, value?: DriveStreamTypes[K]): void {
|
|
|
|
this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishNoteStream<K extends keyof NoteStreamTypes>(noteId: Note['id'], type: K, value?: NoteStreamTypes[K]): void {
|
|
|
|
this.publish(`noteStream:${noteId}`, type, {
|
|
|
|
id: noteId,
|
|
|
|
body: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishUserListStream<K extends keyof UserListStreamTypes>(listId: UserList['id'], type: K, value?: UserListStreamTypes[K]): void {
|
|
|
|
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishAntennaStream<K extends keyof AntennaStreamTypes>(antennaId: Antenna['id'], type: K, value?: AntennaStreamTypes[K]): void {
|
|
|
|
this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishNotesStream(note: Packed<'Note'>): void {
|
|
|
|
this.publish('notesStream', null, note);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:05:32 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public publishAdminStream<K extends keyof AdminStreamTypes>(userId: User['id'], type: K, value?: AdminStreamTypes[K]): void {
|
|
|
|
this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
}
|