From 06c0373be924d5b970353746ddba7ce4f4428d5c Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 6 Sep 2021 00:55:40 +0900 Subject: [PATCH] add main stream --- src/server/api/stream/channels/main.ts | 25 ++++++------ src/server/api/stream/index.ts | 4 +- src/server/api/stream/types.ts | 53 ++++++++++++++++++++------ 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/server/api/stream/channels/main.ts b/src/server/api/stream/channels/main.ts index b99cb931da..d1dce76dd9 100644 --- a/src/server/api/stream/channels/main.ts +++ b/src/server/api/stream/channels/main.ts @@ -11,35 +11,34 @@ export default class extends Channel { public async init(params: any) { // Subscribe main stream channel this.subscriber.on(`mainStream:${this.user!.id}`, async data => { - const { type } = data; - let { body } = data; - - switch (type) { + switch (data.type) { case 'notification': { - if (this.muting.has(body.userId)) return; - if (body.note && body.note.isHidden) { - const note = await Notes.pack(body.note.id, this.user, { + if (data.body.userId && this.muting.has(data.body.userId)) return; + + // ???? + if (data.body.note && data.body.note.isHidden) { + const note = await Notes.pack(data.body.note.id, this.user, { detail: true }); this.connection.cacheNote(note); - body.note = note; + data.body.note = note; } break; } case 'mention': { - if (this.muting.has(body.userId)) return; - if (body.isHidden) { - const note = await Notes.pack(body.id, this.user, { + if (this.muting.has(data.body.userId)) return; + if (data.body.isHidden) { + const note = await Notes.pack(data.body.id, this.user, { detail: true }); this.connection.cacheNote(note); - body = note; + data.body = note; } break; } } - this.send(type, body); + this.send(data.type, data.body); }); } } diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index c2f0870f11..5bd021c814 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, UserEvent } from './types'; +import { StreamEventEmitter, UserEvents } from './types'; /** * Main stream connection @@ -65,7 +65,7 @@ export default class Connection { } @autobind - private onUserEvent(ev: UserEvent) { // { type, body }と展開すると型も展開されてしまう + private onUserEvent(ev: UserEvents) { // { 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 74a0db8e53..549af96fa8 100644 --- a/src/server/api/stream/types.ts +++ b/src/server/api/stream/types.ts @@ -5,6 +5,8 @@ import StreamTypes from 'misskey-js/built/streaming.types'; import { Channel } from '@/models/entities/channel'; 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'; type Payload void> = T extends (payload: infer P) => void ? P : never; @@ -14,11 +16,12 @@ type EventUnionFromDictionary< U = { [K in keyof T]: { type: K; body: T[K]} } > = U[keyof U]; -export type BroadcastStream = { - name: 'broadcast'; - type: T; - body: Payload; -}; +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; export interface UserEventTypes { terminate: {}; @@ -31,19 +34,45 @@ export interface UserEventTypes { unfollow: PackedUser; userAdded: PackedUser; }; - -// UserList userRemoved: PackedUser; - export type UserEventName = `user:${User['id']}`; -export type UserEvent = EventUnionFromDictionary; +export type UserEvents = EventUnionFromDictionary; + +export interface mainStreamTypes { + notification: PackedNotification; + mention: PackedNote; + reply: PackedNote; + renote: PackedNote; + follow: PackedUser; + followed: PackedUser; + unfollow: PackedUser; + meUpdated: PackedUser; + pageEvent: Payload; + urlUploadFinished: Payload; + readAllNotifications: never; + unreadNotification: never; + unreadMention: never; + readAllUnreadMentions: never; + unreadSpecifiedNote: never; + readAllUnreadSpecifiedNotes: never; + readAllMessagingMessages: never; + unreadMessagingMessage: never; + readAllAntennas: never; + unreadAntenna: never; + readAllAnnouncements: never; + readAllChannels: never; + unreadChannel: never; + myTokenRegenerated: never; +}; +export type mainStreamName = `mainStream:${User['id']}`; +export type mainStreams = EventUnionFromDictionary; interface StreamEvents { - 'broadcast': (e: BroadcastStream) => void; + 'broadcast': (e: BroadcastStream) => void; } interface AuthenticatedStreamEvents { - [key: UserEventName]: (e: UserEvent) => void; - [key: `mainStream:${User['id']}`]: (e: { type: string; body: any }) => void; + [key: UserEventName]: (e: UserEvents) => void; + [key: mainStreamName]: (e: mainStreams) => void; [key: `driveStream:${User['id']}`]: (e: { type: string; body: any }) => void; }