2022-09-17 18:27:08 +00:00
|
|
|
import type { Channel } from '@/models/entities/Channel.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { UserProfile } from '@/models/entities/UserProfile.js';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { Antenna } from '@/models/entities/Antenna.js';
|
|
|
|
import type { DriveFile } from '@/models/entities/DriveFile.js';
|
|
|
|
import type { DriveFolder } from '@/models/entities/DriveFolder.js';
|
|
|
|
import type { UserList } from '@/models/entities/UserList.js';
|
|
|
|
import type { MessagingMessage } from '@/models/entities/MessagingMessage.js';
|
|
|
|
import type { UserGroup } from '@/models/entities/UserGroup.js';
|
|
|
|
import type { AbuseUserReport } from '@/models/entities/AbuseUserReport.js';
|
|
|
|
import type { Signin } from '@/models/entities/Signin.js';
|
|
|
|
import type { Page } from '@/models/entities/Page.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import type { Webhook } from '@/models/entities/Webhook.js';
|
2022-09-20 17:35:49 +00:00
|
|
|
import type { Meta } from '@/models/entities/Meta.js';
|
2023-01-13 23:27:23 +00:00
|
|
|
import { Following, Role, RoleAssignment } from '@/models';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type Emitter from 'strict-event-emitter-types';
|
|
|
|
import type { EventEmitter } from 'events';
|
2021-10-20 16:04:10 +00:00
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
// redis通すとDateのインスタンスはstringに変換されるので
|
|
|
|
type Serialized<T> = {
|
|
|
|
[K in keyof T]: T[K] extends Date ? string : T[K];
|
|
|
|
};
|
|
|
|
|
2021-10-20 16:04:10 +00:00
|
|
|
//#region Stream type-body definitions
|
|
|
|
export interface InternalStreamTypes {
|
2023-01-12 12:02:26 +00:00
|
|
|
userChangeSuspendedState: Serialized<{ id: User['id']; isSuspended: User['isSuspended']; }>;
|
|
|
|
userTokenRegenerated: Serialized<{ id: User['id']; oldToken: User['token']; newToken: User['token']; }>;
|
|
|
|
remoteUserUpdated: Serialized<{ id: User['id']; }>;
|
2023-01-13 23:27:23 +00:00
|
|
|
follow: Serialized<{ followerId: User['id']; followeeId: User['id']; }>;
|
|
|
|
unfollow: Serialized<{ followerId: User['id']; followeeId: User['id']; }>;
|
2023-01-15 11:52:53 +00:00
|
|
|
policiesUpdated: Serialized<Role['options']>;
|
2023-01-12 12:02:26 +00:00
|
|
|
roleCreated: Serialized<Role>;
|
|
|
|
roleDeleted: Serialized<Role>;
|
|
|
|
roleUpdated: Serialized<Role>;
|
|
|
|
userRoleAssigned: Serialized<RoleAssignment>;
|
|
|
|
userRoleUnassigned: Serialized<RoleAssignment>;
|
|
|
|
webhookCreated: Serialized<Webhook>;
|
|
|
|
webhookDeleted: Serialized<Webhook>;
|
|
|
|
webhookUpdated: Serialized<Webhook>;
|
|
|
|
antennaCreated: Serialized<Antenna>;
|
|
|
|
antennaDeleted: Serialized<Antenna>;
|
|
|
|
antennaUpdated: Serialized<Antenna>;
|
|
|
|
metaUpdated: Serialized<Meta>;
|
2021-10-20 16:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface BroadcastTypes {
|
|
|
|
emojiAdded: {
|
|
|
|
emoji: Packed<'Emoji'>;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UserStreamTypes {
|
2021-11-12 01:52:10 +00:00
|
|
|
terminate: Record<string, unknown>;
|
2021-10-20 16:04:10 +00:00
|
|
|
followChannel: Channel;
|
|
|
|
unfollowChannel: Channel;
|
|
|
|
updateUserProfile: UserProfile;
|
|
|
|
mute: User;
|
|
|
|
unmute: User;
|
2022-01-18 13:27:10 +00:00
|
|
|
follow: Packed<'UserDetailedNotMe'>;
|
2021-10-20 16:04:10 +00:00
|
|
|
unfollow: Packed<'User'>;
|
|
|
|
userAdded: Packed<'User'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MainStreamTypes {
|
|
|
|
notification: Packed<'Notification'>;
|
|
|
|
mention: Packed<'Note'>;
|
|
|
|
reply: Packed<'Note'>;
|
|
|
|
renote: Packed<'Note'>;
|
2022-01-18 13:27:10 +00:00
|
|
|
follow: Packed<'UserDetailedNotMe'>;
|
2021-10-20 16:04:10 +00:00
|
|
|
followed: Packed<'User'>;
|
|
|
|
unfollow: Packed<'User'>;
|
|
|
|
meUpdated: Packed<'User'>;
|
|
|
|
pageEvent: {
|
|
|
|
pageId: Page['id'];
|
|
|
|
event: string;
|
|
|
|
var: any;
|
|
|
|
userId: User['id'];
|
|
|
|
user: Packed<'User'>;
|
|
|
|
};
|
|
|
|
urlUploadFinished: {
|
|
|
|
marker?: string | null;
|
|
|
|
file: Packed<'DriveFile'>;
|
|
|
|
};
|
|
|
|
readAllNotifications: undefined;
|
|
|
|
unreadNotification: Packed<'Notification'>;
|
|
|
|
unreadMention: Note['id'];
|
|
|
|
readAllUnreadMentions: undefined;
|
|
|
|
unreadSpecifiedNote: Note['id'];
|
|
|
|
readAllUnreadSpecifiedNotes: undefined;
|
|
|
|
readAllMessagingMessages: undefined;
|
|
|
|
messagingMessage: Packed<'MessagingMessage'>;
|
|
|
|
unreadMessagingMessage: Packed<'MessagingMessage'>;
|
|
|
|
readAllAntennas: undefined;
|
|
|
|
unreadAntenna: Antenna;
|
|
|
|
readAllAnnouncements: undefined;
|
|
|
|
readAllChannels: undefined;
|
|
|
|
unreadChannel: Note['id'];
|
|
|
|
myTokenRegenerated: undefined;
|
|
|
|
signin: Signin;
|
|
|
|
registryUpdated: {
|
|
|
|
scope?: string[];
|
|
|
|
key: string;
|
|
|
|
value: any | null;
|
|
|
|
};
|
|
|
|
driveFileCreated: Packed<'DriveFile'>;
|
|
|
|
readAntenna: Antenna;
|
2022-02-19 14:21:28 +00:00
|
|
|
receiveFollowRequest: Packed<'User'>;
|
2021-10-20 16:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface DriveStreamTypes {
|
|
|
|
fileCreated: Packed<'DriveFile'>;
|
|
|
|
fileDeleted: DriveFile['id'];
|
|
|
|
fileUpdated: Packed<'DriveFile'>;
|
|
|
|
folderCreated: Packed<'DriveFolder'>;
|
|
|
|
folderDeleted: DriveFolder['id'];
|
|
|
|
folderUpdated: Packed<'DriveFolder'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NoteStreamTypes {
|
|
|
|
pollVoted: {
|
|
|
|
choice: number;
|
|
|
|
userId: User['id'];
|
|
|
|
};
|
|
|
|
deleted: {
|
|
|
|
deletedAt: Date;
|
|
|
|
};
|
|
|
|
reacted: {
|
|
|
|
reaction: string;
|
2022-02-02 17:41:22 +00:00
|
|
|
emoji?: {
|
|
|
|
name: string;
|
|
|
|
url: string;
|
|
|
|
} | null;
|
2021-10-20 16:04:10 +00:00
|
|
|
userId: User['id'];
|
|
|
|
};
|
|
|
|
unreacted: {
|
|
|
|
reaction: string;
|
|
|
|
userId: User['id'];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
type NoteStreamEventTypes = {
|
|
|
|
[key in keyof NoteStreamTypes]: {
|
|
|
|
id: Note['id'];
|
|
|
|
body: NoteStreamTypes[key];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface ChannelStreamTypes {
|
|
|
|
typing: User['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UserListStreamTypes {
|
|
|
|
userAdded: Packed<'User'>;
|
|
|
|
userRemoved: Packed<'User'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AntennaStreamTypes {
|
|
|
|
note: Note;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessagingStreamTypes {
|
|
|
|
read: MessagingMessage['id'][];
|
|
|
|
typing: User['id'];
|
|
|
|
message: Packed<'MessagingMessage'>;
|
|
|
|
deleted: MessagingMessage['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GroupMessagingStreamTypes {
|
|
|
|
read: {
|
|
|
|
ids: MessagingMessage['id'][];
|
|
|
|
userId: User['id'];
|
|
|
|
};
|
|
|
|
typing: User['id'];
|
|
|
|
message: Packed<'MessagingMessage'>;
|
|
|
|
deleted: MessagingMessage['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessagingIndexStreamTypes {
|
|
|
|
read: MessagingMessage['id'][];
|
|
|
|
message: Packed<'MessagingMessage'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AdminStreamTypes {
|
|
|
|
newAbuseUserReport: {
|
|
|
|
id: AbuseUserReport['id'];
|
|
|
|
targetUserId: User['id'],
|
|
|
|
reporterId: User['id'],
|
|
|
|
comment: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
// 辞書(interface or type)から{ type, body }ユニオンを定義
|
|
|
|
// https://stackoverflow.com/questions/49311989/can-i-infer-the-type-of-a-value-using-extends-keyof-type
|
|
|
|
// VS Codeの展開を防止するためにEvents型を定義
|
|
|
|
type Events<T extends object> = { [K in keyof T]: { type: K; body: T[K]; } };
|
|
|
|
type EventUnionFromDictionary<
|
|
|
|
T extends object,
|
|
|
|
U = Events<T>
|
|
|
|
> = U[keyof U];
|
|
|
|
|
|
|
|
// name/messages(spec) pairs dictionary
|
|
|
|
export type StreamMessages = {
|
|
|
|
internal: {
|
|
|
|
name: 'internal';
|
|
|
|
payload: EventUnionFromDictionary<InternalStreamTypes>;
|
|
|
|
};
|
|
|
|
broadcast: {
|
|
|
|
name: 'broadcast';
|
|
|
|
payload: EventUnionFromDictionary<BroadcastTypes>;
|
|
|
|
};
|
|
|
|
user: {
|
|
|
|
name: `user:${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<UserStreamTypes>;
|
|
|
|
};
|
|
|
|
main: {
|
|
|
|
name: `mainStream:${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<MainStreamTypes>;
|
|
|
|
};
|
|
|
|
drive: {
|
|
|
|
name: `driveStream:${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<DriveStreamTypes>;
|
|
|
|
};
|
|
|
|
note: {
|
|
|
|
name: `noteStream:${Note['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<NoteStreamEventTypes>;
|
|
|
|
};
|
|
|
|
channel: {
|
|
|
|
name: `channelStream:${Channel['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<ChannelStreamTypes>;
|
|
|
|
};
|
|
|
|
userList: {
|
|
|
|
name: `userListStream:${UserList['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<UserListStreamTypes>;
|
|
|
|
};
|
|
|
|
antenna: {
|
|
|
|
name: `antennaStream:${Antenna['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<AntennaStreamTypes>;
|
|
|
|
};
|
|
|
|
messaging: {
|
|
|
|
name: `messagingStream:${User['id']}-${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<MessagingStreamTypes>;
|
|
|
|
};
|
|
|
|
groupMessaging: {
|
|
|
|
name: `messagingStream:${UserGroup['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<GroupMessagingStreamTypes>;
|
|
|
|
};
|
|
|
|
messagingIndex: {
|
|
|
|
name: `messagingIndexStream:${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<MessagingIndexStreamTypes>;
|
|
|
|
};
|
|
|
|
admin: {
|
|
|
|
name: `adminStream:${User['id']}`;
|
|
|
|
payload: EventUnionFromDictionary<AdminStreamTypes>;
|
|
|
|
};
|
|
|
|
notes: {
|
|
|
|
name: 'notesStream';
|
|
|
|
payload: Packed<'Note'>;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// API event definitions
|
|
|
|
// ストリームごとのEmitterの辞書を用意
|
|
|
|
type EventEmitterDictionary = { [x in keyof StreamMessages]: Emitter<EventEmitter, { [y in StreamMessages[x]['name']]: (e: StreamMessages[x]['payload']) => void }> };
|
|
|
|
// 共用体型を交差型にする型 https://stackoverflow.com/questions/54938141/typescript-convert-union-to-intersection
|
|
|
|
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
|
|
// Emitter辞書から共用体型を作り、UnionToIntersectionで交差型にする
|
|
|
|
export type StreamEventEmitter = UnionToIntersection<EventEmitterDictionary[keyof StreamMessages]>;
|
|
|
|
// { [y in name]: (e: spec) => void }をまとめてその交差型をEmitterにかけるとts(2590)にひっかかる
|
|
|
|
|
|
|
|
// provide stream channels union
|
|
|
|
export type StreamChannels = StreamMessages[keyof StreamMessages]['name'];
|