From dd175a8e6ee0093d2711bd1bcde9ea7f5fc099fc Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 6 Sep 2021 17:56:17 +0900 Subject: [PATCH] wip --- src/server/api/stream/index.ts | 4 +- src/server/api/stream/types.ts | 95 ++++++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index 0101e6e079..b2cecb4470 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -15,7 +15,7 @@ import { UserProfile } from '@/models/entities/user-profile'; import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '@/services/stream'; import { UserGroup } from '@/models/entities/user-group'; import { PackedNote } from '@/models/repositories/note'; -import { StreamEventEmitter, UserEvents } from './types'; +import { StreamEventEmitter, UserStreams } from './types'; /** * Main stream connection @@ -65,7 +65,7 @@ export default class Connection { } @autobind - private onUserEvent(ev: UserEvents) { // { type, body }と展開すると型も展開されてしまう + private onUserEvent(ev: UserStreams) { // { type, body }と展開すると型も展開されてしまう switch (ev.type) { case 'follow': this.following.add(ev.body.id); diff --git a/src/server/api/stream/types.ts b/src/server/api/stream/types.ts index e0c76306bd..3803e6dcea 100644 --- a/src/server/api/stream/types.ts +++ b/src/server/api/stream/types.ts @@ -7,23 +7,46 @@ import { UserProfile } from '@/models/entities/user-profile'; import { PackedUser } from '@/models/repositories/user'; import { PackedNotification } from '@/models/repositories/notification'; import { PackedNote } from '@/models/repositories/note'; +import { Antenna } from '@/models/entities/antenna'; +import { DriveFile } from '@/models/entities/drive-file'; +import { PackedDriveFile } from '@/models/repositories/drive-file'; +import { PackedDriveFolder } from '@/models/repositories/drive-folder'; +import { DriveFolder } from '@/models/entities/drive-folder'; +import { Note } from '@/models/entities/note'; +import { Emoji } from '@/models/entities/emoji'; -type Payload void> = T extends (payload: infer P) => void ? P : never; - +// 辞書(interface or type)から{ type, body }ユニオンを定義 // https://stackoverflow.com/questions/49311989/can-i-infer-the-type-of-a-value-using-extends-keyof-type type EventUnionFromDictionary< T extends object, U = { [K in keyof T]: { type: K; body: T[K]} } > = U[keyof U]; +// (payload: P) => void からPを取り出す +type Payload void> = T extends (payload: infer P) => void ? P : never; + +// misskey.jsのstreaming.typesの辞書から{ type, body }ユニオンを定義 type EventUnionFromMkJSTypes< T extends { [key: string]: ((payload: any) => void) | (() => void) }, U = { [K in keyof T]: { type: K; body: Payload} } > = U[keyof U]; -export type BroadcastStream = EventUnionFromMkJSTypes; +//#region Stream type-body definitions -export interface UserEventTypes { +// internal +export interface InternalStreamTypes { + antennaCreated: Antenna; + antennaDeleted: Antenna; + antennaUpdated: Antenna; +} +export type InternalStreams = EventUnionFromDictionary; + +// broadcast +export type BroadcastStreams = EventUnionFromMkJSTypes; + +// user +export type UserEventName = `user:${User['id']}`; +export interface UserStreamTypes { terminate: {}; followChannel: Channel; unfollowChannel: Channel; @@ -34,9 +57,10 @@ export interface UserEventTypes { unfollow: PackedUser; userAdded: PackedUser; } -export type UserEventName = `user:${User['id']}`; -export type UserEvents = EventUnionFromDictionary; +export type UserStreams = EventUnionFromDictionary; +// main +export type mainStreamName = `mainStream:${User['id']}`; export interface MainStreamTypes { notification: PackedNotification; mention: PackedNote; @@ -63,17 +87,68 @@ export interface MainStreamTypes { unreadChannel: never; myTokenRegenerated: never; } -export type mainStreamName = `mainStream:${User['id']}`; export type mainStreams = EventUnionFromDictionary; +// drive +export type driveStreamName = `driveStream:${User['id']}`; +export interface DriveStreamTypes { + fileCreated: PackedDriveFile; + fileDeleted: DriveFile['id']; + fileUpdated: PackedDriveFile; + folderCreated: PackedDriveFolder; + folderDeleted: DriveFolder['id']; + folderUpdated: PackedDriveFolder; +} +export type driveStreams= EventUnionFromDictionary; + +// note +export type noteStreamName = `noteStream:${Note['id']}`; +export interface NoteStreamTypes { + pollVoted: { + id: Note['id']; + body: { + choice: number; + userId: User['id']; + }; + }; + deleted: { + id: Note['id']; + body: { + deletedAt: Date; + }; + }; + reacted: { + id: Note['id']; + body: { + reaction: string; + emoji?: Emoji; + userId: User['id']; + }; + }; + unreacted: { + id: Note['id']; + body: { + reaction: string; + userId: User['id']; + } + }; +} +export type noteStreams = EventUnionFromDictionary; + +//#endregion + +//#region API event definitions interface StreamEvents { - 'broadcast': (e: BroadcastStream) => void; + 'broadcast': (e: BroadcastStreams) => void; + 'internal': (e: InternalStreams) => void; } interface AuthenticatedStreamEvents { - [key: UserEventName]: (e: UserEvents) => void; + [key: UserEventName]: (e: UserStreams) => void; [key: mainStreamName]: (e: mainStreams) => void; - [key: `driveStream:${User['id']}`]: (e: { type: string; body: any }) => void; + [key: driveStreamName]: (e: driveStreams) => void; + [key: noteStreamName]: (e: noteStreams) => void; } export type StreamEventEmitter = Emitter; +//#endregion