2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 13:50:05 +09:00
|
|
|
import * as Redis from 'ioredis';
|
2023-09-20 11:33:36 +09:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiNote } from '@/models/Note.js';
|
|
|
|
import type { MiUserList } from '@/models/UserList.js';
|
|
|
|
import type { MiAntenna } from '@/models/Antenna.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type {
|
|
|
|
StreamChannels,
|
|
|
|
AdminStreamTypes,
|
|
|
|
AntennaStreamTypes,
|
|
|
|
BroadcastTypes,
|
|
|
|
DriveStreamTypes,
|
|
|
|
InternalStreamTypes,
|
|
|
|
MainStreamTypes,
|
|
|
|
NoteStreamTypes,
|
|
|
|
UserListStreamTypes,
|
2023-04-12 11:40:08 +09:00
|
|
|
RoleTimelineStreamTypes,
|
2022-09-18 03:27:08 +09:00
|
|
|
} from '@/server/api/stream/types.js';
|
2023-03-10 14:22:37 +09:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import { MiRole } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GlobalEventService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2023-04-09 17:09:27 +09:00
|
|
|
@Inject(DI.redisForPub)
|
|
|
|
private redisForPub: Redis.Redis,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09: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 17:09:27 +09:00
|
|
|
this.redisForPub.publish(this.config.host, JSON.stringify({
|
2022-09-18 03:27:08 +09:00
|
|
|
channel: channel,
|
|
|
|
message: message,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09: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 17:05:32 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09: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 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishMainStream<K extends keyof MainStreamTypes>(userId: MiUser['id'], type: K, value?: MainStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishDriveStream<K extends keyof DriveStreamTypes>(userId: MiUser['id'], type: K, value?: DriveStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishNoteStream<K extends keyof NoteStreamTypes>(noteId: MiNote['id'], type: K, value?: NoteStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`noteStream:${noteId}`, type, {
|
|
|
|
id: noteId,
|
|
|
|
body: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishUserListStream<K extends keyof UserListStreamTypes>(listId: MiUserList['id'], type: K, value?: UserListStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishAntennaStream<K extends keyof AntennaStreamTypes>(antennaId: MiAntenna['id'], type: K, value?: AntennaStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:40:08 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishRoleTimelineStream<K extends keyof RoleTimelineStreamTypes>(roleId: MiRole['id'], type: K, value?: RoleTimelineStreamTypes[K]): void {
|
2023-04-12 11:40:08 +09:00
|
|
|
this.publish(`roleTimelineStream:${roleId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public publishNotesStream(note: Packed<'Note'>): void {
|
|
|
|
this.publish('notesStream', null, note);
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:05:32 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public publishAdminStream<K extends keyof AdminStreamTypes>(userId: MiUser['id'], type: K, value?: AdminStreamTypes[K]): void {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value);
|
|
|
|
}
|
|
|
|
}
|