import { AsyncCommandResult , CommandResult , DefaultRes } from '../../request' ; import { Channel , ChannelDataStore , ChannelMeta , NormalChannelInfo , SetChannelMeta , UpdatableChannelDataStore } from '../../channel' ; import { ChannelUser , NormalChannelUserInfo } from '../../user' ; import { Chat , Chatlog , ChatLogged , ChatType , UpdatableChatListStore } from '../../chat' ; import { TalkNormalChannelSession } from './talk-normal-channel-session' ; import { Managed } from '../managed' ; import { EventContext , TypedEmitter } from '../../event' ; import { TalkChannelHandler } from './talk-channel-handler' ; import { Long } from 'bson' ; import { TalkSession } from '../client' ; import { MediaKeyComponent , MediaMultiPost , MediaPost , MediaUploadForm } from '../../media' ; import { ChannelEvents } from '../event' ; import { TalkChannel } from '.' ; import { ChannelMetaType , GroupMetaContent , KnownChannelMetaType , LiveTalkCountMetaContent , LiveTalkInfoMetaContent , ProfileMetaContent , TvLiveMetaContent , TvMetaContent , } from '../../channel/meta' ; import { JsonUtil } from '../../util' ; import { ChatOnRoomRes } from '../../packet/chat' ; import { MediaUploadTemplate } from '../media/upload' ; import { TalkNormalChannelDataSession } from './talk-normal-channel-data-session' ; import { TalkChannelDataSession } from './talk-channel-data-session' ; import { TalkChannelSession } from './talk-channel-session' ; import { TalkNormalChannelHandler } from './talk-normal-channel-handler' ; import { FixedReadStream } from '../../stream' ; import { sendMedia , sendMultiMedia } from './common' ; type TalkChannelEvents = ChannelEvents < TalkNormalChannel , NormalChannelUserInfo > ; export class TalkNormalChannel extends TypedEmitter < TalkChannelEvents > implements TalkChannel , ChannelDataStore < NormalChannelInfo , NormalChannelUserInfo > , Managed < TalkChannelEvents > { private _channelSession : TalkChannelDataSession ; private _normalChannelSession : TalkNormalChannelDataSession ; private _normalHandler : TalkNormalChannelHandler < TalkNormalChannel > ; private _handler : TalkChannelHandler < TalkNormalChannel > ; constructor ( private _channel , session , store , private _chatListStore ) { super ( ) ; this . _channelSession = new TalkChannelDataSession ( session . clientUser , new TalkChannelSession ( this , session ) , store , _chatListStore ) ; const normalSession = new TalkNormalChannelSession ( this , session ) ; this . _normalChannelSession = new TalkNormalChannelDataSession ( session . clientUser , normalSession , store ) ; this . _handler = new TalkChannelHandler ( this , this , store , _chatListStore ) ; this . _normalHandler = new TalkNormalChannelHandler ( this , normalSession , this , store , _chatListStore ) ; } get clientUser ( ) : Readonly < ChannelUser > { return this . _normalChannelSession . clientUser ; } get channelId ( ) : Long { return this . _channel . channelId ; } get chatListStore ( ) : UpdatableChatListStore { return this . _chatListStore ; } get store ( ) : UpdatableChannelDataStore < NormalChannelInfo , NormalChannelUserInfo > { return this . _normalChannelSession . store ; } get info ( ) : Readonly < NormalChannelInfo > { return this . store . info ; } get userCount ( ) : number { return this . store . userCount ; } getName ( ) { const nameMeta = this . info . metaMap [ KnownChannelMetaType . TITLE ] ; return nameMeta && nameMeta . content || '' ; } getDisplayName ( ) { return this . getName ( ) || this . info . displayUserList . map ( ( user ) => user . nickname ) . join ( ', ' ) ; } getUserInfo ( user ) { return this . _normalChannelSession . store . getUserInfo ( user ) ; } getAllUserInfo ( ) { return this . _normalChannelSession . store . getAllUserInfo ( ) ; } getReadCount ( chat ) { return this . store . getReadCount ( chat ) ; } getReaders ( chat ) { return this . store . getReaders ( chat ) ; } sendChat ( chat , noSeen ? ) { return this . _channelSession . sendChat ( chat , noSeen ) ; } forwardChat ( chat , noSeen ? ) { return this . _channelSession . forwardChat ( chat , noSeen ) ; } deleteChat ( chat ) { return this . _channelSession . deleteChat ( chat ) ; } markRead ( chat ) { return this . _channelSession . markRead ( chat ) ; } setMeta ( type , meta ) { return this . _channelSession . setMeta ( type , meta ) ; } setTitleMeta ( title ) { return this . setMeta ( KnownChannelMetaType . TITLE , title ) ; } setNoticeMeta ( notice ) { return this . setMeta ( KnownChannelMetaType . NOTICE , notice ) ; } setProfileMeta ( content ) { return this . setMeta ( KnownChannelMetaType . PROFILE , JsonUtil . stringifyLoseless ( content ) ) ; } setTvMeta ( content ) { return this . setMeta ( KnownChannelMetaType . TV , JsonUtil . stringifyLoseless ( content ) ) ; } setTvLiveMeta ( content ) { return this . setMeta ( KnownChannelMetaType . TV_LIVE , JsonUtil . stringifyLoseless ( content ) ) ; } setLiveTalkInfoMeta ( content ) { return this . setMeta ( KnownChannelMetaType . LIVE_TALK_INFO , JsonUtil . stringifyLoseless ( content ) ) ; } setLiveTalkCountMeta ( content ) { return this . setMeta ( KnownChannelMetaType . LIVE_TALK_COUNT , JsonUtil . stringifyLoseless ( content ) ) ; } setGroupMeta ( content ) { return this . setMeta ( KnownChannelMetaType . GROUP , JsonUtil . stringifyLoseless ( content ) ) ; } setPushAlert ( flag ) { return this . _channelSession . setPushAlert ( flag ) ; } inviteUsers ( users ) { return this . _normalChannelSession . inviteUsers ( users ) ; } syncChatList ( endLogId , startLogId ? ) { return this . _channelSession . syncChatList ( endLogId , startLogId ) ; } getChatListFrom ( startLogId ? ) { return this . _channelSession . getChatListFrom ( startLogId ) ; } async chatON ( ) { return this . _normalChannelSession . chatON ( ) ; } async getLatestChannelInfo ( ) { return this . _normalChannelSession . getLatestChannelInfo ( ) ; } async getLatestUserInfo ( ... users ) { return this . _normalChannelSession . getLatestUserInfo ( ... users ) ; } async getAllLatestUserInfo ( ) { return this . _normalChannelSession . getAllLatestUserInfo ( ) ; } downloadMedia ( media , type , offset ? ) { return this . _channelSession . downloadMedia ( media , type , offset ) ; } downloadMediaThumb ( media , type , offset ? ) { return this . _channelSession . downloadMediaThumb ( media , type , offset ) ; } uploadMedia ( type , form ) { return this . _channelSession . uploadMedia ( type , form ) ; } uploadMultiMedia ( type , forms ) { return this . _channelSession . uploadMultiMedia ( type , forms ) ; } sendMedia ( type , template ) { return sendMedia ( this . _channelSession , type , template ) ; } sendMultiMedia ( type , templates ) { return sendMultiMedia ( this . _channelSession , type , templates ) ; } async updateAll ( ) { const infoRes = await this . getLatestChannelInfo ( ) ; if ( ! infoRes . success ) return infoRes ; return this . chatON ( ) ; } async pushReceived ( method , data , parentCtx ) { await this . _handler . pushReceived ( method , data , parentCtx ) ; await this . _normalHandler . pushReceived ( method , data , parentCtx ) ; } }