From 4793d1cc56e5d0778f54a1a4b45fc3cdc09ea172 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 6 Sep 2021 23:18:16 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=81=E4=BF=A1=E5=81=B4=E3=81=AB=E5=9E=8B?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=8C=E3=81=AA=E3=81=84=E3=81=93?= =?UTF-8?q?=E3=81=A8=E3=82=923=E5=9B=9E=E7=A2=BA=E8=AA=8D=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/stream/types.ts | 59 +++++++++++++++------------------- src/services/stream.ts | 46 ++++++++++++++++++-------- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/src/server/api/stream/types.ts b/src/server/api/stream/types.ts index 17acc95379..b78b894977 100644 --- a/src/server/api/stream/types.ts +++ b/src/server/api/stream/types.ts @@ -1,27 +1,27 @@ -import { User } from '@/models/entities/user'; import { EventEmitter } from 'events'; import Emitter from 'strict-event-emitter-types'; import { Channel } from '@/models/entities/channel'; +import { User } from '@/models/entities/user'; import { UserProfile } from '@/models/entities/user-profile'; import { PackedUser } from '@/models/repositories/user'; import { PackedNotification } from '@/models/repositories/notification'; +import { Note } from '@/models/entities/note'; 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 { PackedDriveFolder } from '@/models/repositories/drive-folder'; import { Emoji } from '@/models/entities/emoji'; +import { PackedEmoji } from '@/models/repositories/emoji'; import { UserList } from '@/models/entities/user-list'; import { MessagingMessage } from '@/models/entities/messaging-message'; import { PackedMessagingMessage } from '@/models/repositories/messaging-message'; import { UserGroup } from '@/models/entities/user-group'; +import { ReversiGame } from '@/models/entities/games/reversi/game'; import { PackedReversiGame } from '@/models/repositories/games/reversi/game'; import { PackedReversiMatching } from '@/models/repositories/games/reversi/matching'; -import { ReversiGame } from '@/models/entities/games/reversi/game'; import { AbuseUserReport } from '@/models/entities/abuse-user-report'; -import { PackedEmoji } from '@/models/repositories/emoji'; import { PackedSignin } from '@/models/repositories/signin'; import { Page } from '@/models/entities/page'; @@ -32,9 +32,6 @@ type EventUnionFromDictionary< 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; - //#region Stream type-body definitions export interface InternalStreamTypes { antennaCreated: Antenna; @@ -43,7 +40,9 @@ export interface InternalStreamTypes { } export interface BroadcastTypes { - emojiAdded: PackedEmoji; + emojiAdded: { + emoji: PackedEmoji; + }; } export interface UserStreamTypes { @@ -116,34 +115,28 @@ export interface DriveStreamTypes { export interface NoteStreamTypes { pollVoted: { - id: Note['id']; - body: { - choice: number; - userId: User['id']; - }; + choice: number; + userId: User['id']; }; deleted: { - id: Note['id']; - body: { - deletedAt: Date; - }; + deletedAt: Date; }; reacted: { - id: Note['id']; - body: { - reaction: string; - emoji?: Emoji; - userId: User['id']; - }; + reaction: string; + emoji?: Emoji; + userId: User['id']; }; unreacted: { - id: Note['id']; - body: { - reaction: string; - userId: User['id']; - } + reaction: string; + userId: User['id']; }; } +type NoteStreamEventTypes = { + [key in keyof NoteStreamTypes]: { + id: Note['id']; + body: NoteStreamTypes[key]; + }; +}; export interface ChannelStreamTypes { typing: User['id']; @@ -188,7 +181,7 @@ export interface ReversiStreamTypes { export interface ReversiGameStreamTypes { started: PackedReversiGame; ended: { - winnerId: User['id'], + winnerId?: User['id'] | null, game: PackedReversiGame; }; updateSettings: { @@ -255,7 +248,7 @@ interface StreamMessages { }; note: { name: `noteStream:${Note['id']}`; - spec: EventUnionFromDictionary; + spec: EventUnionFromDictionary; }; channel: { name: `channelStream:${Channel['id']}`; @@ -302,6 +295,6 @@ type EventsGenerater = { [key in StreamMessages[ type NotesStreamEvent = { notesStream: (e: PackedNote) => void }; export type StreamEventEmitter = Emitter & NotesStreamEvent>; -// Channel Union +// provide stream channels union type ChannelsUnionGenerater = StreamMessages[K]['name']; -export type Channels = ChannelsUnionGenerater | 'notesStream'; +export type StreamChannels = ChannelsUnionGenerater | 'notesStream'; diff --git a/src/services/stream.ts b/src/services/stream.ts index 3b0c90d75c..3c5ef36399 100644 --- a/src/services/stream.ts +++ b/src/services/stream.ts @@ -7,10 +7,28 @@ import { UserGroup } from '@/models/entities/user-group'; import config from '@/config/index'; import { Antenna } from '@/models/entities/antenna'; import { Channel } from '@/models/entities/channel'; -import { BroadcastTypes, Channels, InternalStreamTypes, MainStreamTypes, UserStreamTypes } from '@/server/api/stream/types'; +import { + StreamChannels, + AdminStreamTypes, + AntennaStreamTypes, + BroadcastTypes, + ChannelStreamTypes, + DriveStreamTypes, + GroupMessagingStreamTypes, + InternalStreamTypes, + MainStreamTypes, + MessagingIndexStreamTypes, + MessagingStreamTypes, + NoteStreamTypes, + ReversiGameStreamTypes, + ReversiStreamTypes, + UserListStreamTypes, + UserStreamTypes +} from '@/server/api/stream/types'; +import { PackedNote } from '@/models/repositories/note'; class Publisher { - private publish = (channel: Channels, type: string | null, value?: any): void => { + 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 }; @@ -37,54 +55,54 @@ class Publisher { this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value); } - public publishDriveStream = (userId: User['id'], type: string, value?: any): void => { + public publishDriveStream = (userId: User['id'], type: K, value?: DriveStreamTypes[K]): void => { this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value); } - public publishNoteStream = (noteId: Note['id'], type: string, value: any): void => { + public publishNoteStream = (noteId: Note['id'], type: K, value: NoteStreamTypes[K]): void => { this.publish(`noteStream:${noteId}`, type, { id: noteId, body: value }); } - public publishChannelStream = (channelId: Channel['id'], type: string, value?: any): void => { + public publishChannelStream = (channelId: Channel['id'], type: K, value?: ChannelStreamTypes[K]): void => { this.publish(`channelStream:${channelId}`, type, typeof value === 'undefined' ? null : value); } - public publishUserListStream = (listId: UserList['id'], type: string, value?: any): void => { + public publishUserListStream = (listId: UserList['id'], type: K, value?: UserListStreamTypes[K]): void => { this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value); } - public publishAntennaStream = (antennaId: Antenna['id'], type: string, value?: any): void => { + public publishAntennaStream = (antennaId: Antenna['id'], type: K, value?: AntennaStreamTypes[K]): void => { this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value); } - public publishMessagingStream = (userId: User['id'], otherpartyId: User['id'], type: string, value?: any): void => { + public publishMessagingStream = (userId: User['id'], otherpartyId: User['id'], type: K, value?: MessagingStreamTypes[K]): void => { this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value); } - public publishGroupMessagingStream = (groupId: UserGroup['id'], type: string, value?: any): void => { + public publishGroupMessagingStream = (groupId: UserGroup['id'], type: K, value?: GroupMessagingStreamTypes[K]): void => { this.publish(`messagingStream:${groupId}`, type, typeof value === 'undefined' ? null : value); } - public publishMessagingIndexStream = (userId: User['id'], type: string, value?: any): void => { + public publishMessagingIndexStream = (userId: User['id'], type: K, value?: MessagingIndexStreamTypes[K]): void => { this.publish(`messagingIndexStream:${userId}`, type, typeof value === 'undefined' ? null : value); } - public publishReversiStream = (userId: User['id'], type: string, value?: any): void => { + public publishReversiStream = (userId: User['id'], type: K, value?: ReversiStreamTypes[K]): void => { this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value); } - public publishReversiGameStream = (gameId: ReversiGame['id'], type: string, value?: any): void => { + public publishReversiGameStream = (gameId: ReversiGame['id'], type: K, value?: ReversiGameStreamTypes[K]): void => { this.publish(`reversiGameStream:${gameId}`, type, typeof value === 'undefined' ? null : value); } - public publishNotesStream = (note: any): void => { + public publishNotesStream = (note: PackedNote): void => { this.publish('notesStream', null, note); } - public publishAdminStream = (userId: User['id'], type: string, value?: any): void => { + public publishAdminStream = (userId: User['id'], type: K, value?: AdminStreamTypes[K]): void => { this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value); } }