From d9d98f84bf3fe71c8182d86522cc0d8abf72fd96 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 1 Feb 2023 20:12:42 +0900 Subject: [PATCH] refactor: fix type --- .../backend/src/server/api/stream/types.ts | 78 ++++++++++--------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index fc145cf0e..36bfa7836 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -18,31 +18,26 @@ import { Following, Role, RoleAssignment } from '@/models'; import type Emitter from 'strict-event-emitter-types'; import type { EventEmitter } from 'events'; -// redis通すとDateのインスタンスはstringに変換されるので -type Serialized = { - [K in keyof T]: T[K] extends Date ? string : T[K]; -}; - //#region Stream type-body definitions export interface InternalStreamTypes { - userChangeSuspendedState: Serialized<{ id: User['id']; isSuspended: User['isSuspended']; }>; - userTokenRegenerated: Serialized<{ id: User['id']; oldToken: User['token']; newToken: User['token']; }>; - remoteUserUpdated: Serialized<{ id: User['id']; }>; - follow: Serialized<{ followerId: User['id']; followeeId: User['id']; }>; - unfollow: Serialized<{ followerId: User['id']; followeeId: User['id']; }>; - policiesUpdated: Serialized; - roleCreated: Serialized; - roleDeleted: Serialized; - roleUpdated: Serialized; - userRoleAssigned: Serialized; - userRoleUnassigned: Serialized; - webhookCreated: Serialized; - webhookDeleted: Serialized; - webhookUpdated: Serialized; - antennaCreated: Serialized; - antennaDeleted: Serialized; - antennaUpdated: Serialized; - metaUpdated: Serialized; + userChangeSuspendedState: { id: User['id']; isSuspended: User['isSuspended']; }; + userTokenRegenerated: { id: User['id']; oldToken: User['token']; newToken: User['token']; }; + remoteUserUpdated: { id: User['id']; }; + follow: { followerId: User['id']; followeeId: User['id']; }; + unfollow: { followerId: User['id']; followeeId: User['id']; }; + policiesUpdated: Role['policies']; + roleCreated: Role; + roleDeleted: Role; + roleUpdated: Role; + userRoleAssigned: RoleAssignment; + userRoleUnassigned: RoleAssignment; + webhookCreated: Webhook; + webhookDeleted: Webhook; + webhookUpdated: Webhook; + antennaCreated: Antenna; + antennaDeleted: Antenna; + antennaUpdated: Antenna; + metaUpdated: Meta; } export interface BroadcastTypes { @@ -210,63 +205,72 @@ type EventUnionFromDictionary< U = Events > = U[keyof U]; +// redis通すとDateのインスタンスはstringに変換されるので +type Serialized = { + [K in keyof T]: T[K] extends Date ? string : T[K] extends Record ? Serialized : T[K]; +}; + +type SerializedAll = { + [K in keyof T]: Serialized; +}; + // name/messages(spec) pairs dictionary export type StreamMessages = { internal: { name: 'internal'; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; broadcast: { name: 'broadcast'; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; user: { name: `user:${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; main: { name: `mainStream:${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; drive: { name: `driveStream:${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; note: { name: `noteStream:${Note['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; channel: { name: `channelStream:${Channel['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; userList: { name: `userListStream:${UserList['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; antenna: { name: `antennaStream:${Antenna['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; messaging: { name: `messagingStream:${User['id']}-${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; groupMessaging: { name: `messagingStream:${UserGroup['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; messagingIndex: { name: `messagingIndexStream:${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; admin: { name: `adminStream:${User['id']}`; - payload: EventUnionFromDictionary; + payload: EventUnionFromDictionary>; }; notes: { name: 'notesStream'; - payload: Packed<'Note'>; + payload: Serialized>; }; };