import { Long } from 'bson' ; import { Channel , ChannelDataStore , ChannelMeta , SetChannelMeta , UpdatableChannelDataStore } from '../../channel' ; import { Chat , Chatlog , ChatLogged , ChatLoggedType , ChatType , UpdatableChatListStore } from '../../chat' ; import { TalkSession } from '../client' ; import { EventContext , TypedEmitter } from '../../event' ; import { MediaKeyComponent , MediaMultiPost , MediaPost , MediaUploadForm } from '../../media' ; import { OpenChannel , OpenChannelInfo , OpenChannelSession , OpenChannelUserPerm , OpenLink , OpenLinkChannelUserInfo , OpenLinkKickedUserInfo , OpenLinkProfiles , } from '../../openlink' ; import { AsyncCommandResult , CommandResult , DefaultRes , KnownDataStatusCode } from '../../request' ; import { RelayEventType } from '../../relay' ; import { ChannelUser , OpenChannelUserInfo } from '../../user' ; import { TalkChannel , TalkChannelHandler , TalkChannelSession , TalkChannelDataSession , sendMedia , sendMultiMedia } from '../channel' ; import { TalkOpenChannelSession } from './talk-open-channel-session' ; import { OpenChannelEvents } from '../event' ; import { Managed } from '../managed' ; import { TalkOpenChannelHandler } from './talk-open-channel-handler' ; import { JsonUtil } from '../../util' ; import { BotMetaContent , ChannelMetaType , GroupMetaContent , KnownChannelMetaType , LiveTalkCountMetaContent , LiveTalkInfoMetaContent , PrivilegeMetaContent , ProfileMetaContent , TvLiveMetaContent , TvMetaContent , } from '../../channel/meta' ; import { ChatOnRoomRes } from '../../packet/chat' ; import { MediaUploadTemplate } from '../media/upload' ; import { TalkOpenChannelDataSession } from './talk-open-channel-data-session' ; import { FixedReadStream } from '../../stream' ; type TalkOpenChannelEvents = OpenChannelEvents < TalkOpenChannel , OpenChannelUserInfo > ; export class TalkOpenChannel extends TypedEmitter < TalkOpenChannelEvents > implements OpenChannel , ChannelDataStore < OpenChannelInfo , OpenChannelUserInfo > , TalkChannel , OpenChannelSession , Managed < TalkOpenChannelEvents > { private _channelSession : TalkChannelDataSession ; private _openChannelSession : TalkOpenChannelDataSession ; private _handler : TalkChannelHandler < TalkOpenChannel > ; private _openHandler : TalkOpenChannelHandler < TalkOpenChannel > ; constructor ( private _channel , session , store , private _chatListStore ) { super ( ) ; this . _channelSession = new TalkChannelDataSession ( session . clientUser , new TalkChannelSession ( this , session ) , store , _chatListStore ) ; const openSession = new TalkOpenChannelSession ( this , session ) ; this . _openChannelSession = new TalkOpenChannelDataSession ( session . clientUser , openSession , store ) ; this . _handler = new TalkChannelHandler ( this , this , store , _chatListStore ) ; this . _openHandler = new TalkOpenChannelHandler ( this , openSession , this , store , _chatListStore ) ; } get clientUser ( ) : Readonly < ChannelUser > { return this . _openChannelSession . clientUser ; } get channelId ( ) : Long { return this . _channel . channelId ; } get chatListStore ( ) : UpdatableChatListStore { return this . _chatListStore ; } get store ( ) : UpdatableChannelDataStore < OpenChannelInfo , OpenChannelUserInfo > { return this . _openChannelSession . store ; } get linkId ( ) : Long { return this . store . info . linkId ; } get info ( ) : Readonly < OpenChannelInfo > { 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 . openLink ?. linkName || '' ; } getUserInfo ( user ) { return this . store . getUserInfo ( user ) ; } getAllUserInfo ( ) { return this . store . getAllUserInfo ( ) ; } getReadCount ( chat ) { return this . store . getReadCount ( chat ) ; } getReaders ( chat ) { return this . store . getReaders ( chat ) ; } async sendChat ( chat , noSeen ? ) { return await this . _channelSession . sendChat ( chat , noSeen ) ; } forwardChat ( chat , noSeen ? ) { return this . _channelSession . forwardChat ( chat , noSeen ) ; } deleteChat ( chat ) { return this . _channelSession . deleteChat ( chat ) ; } async inviteUsers ( ) { return { success : false , status : KnownDataStatusCode . OPERATION_DENIED } ; } syncChatList ( endLogId , startLogId ? ) { return this . _channelSession . syncChatList ( endLogId , startLogId ) ; } getChatListFrom ( startLogId ? ) { return this . _channelSession . getChatListFrom ( startLogId ) ; } markRead ( chat ) { return this . _openChannelSession . 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 ) ; } param { PrivilegeMetaContent } content return { AsyncCommandResult < SetChannelMeta > } setPrivilegeMeta ( content ) { return this . setMeta ( KnownChannelMetaType . PRIVILEGE , JsonUtil . stringifyLoseless ( content ) ) ; } 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 ) ) ; } param { BotMetaContent } content return { AsyncCommandResult < SetChannelMeta > } setBotMeta ( content ) { return this . setMeta ( KnownChannelMetaType . BOT , JsonUtil . stringifyLoseless ( content ) ) ; } setPushAlert ( flag ) { return this . _channelSession . setPushAlert ( flag ) ; } chatON ( ) { return this . _openChannelSession . chatON ( ) ; } getLatestChannelInfo ( ) { return this . _openChannelSession . getLatestChannelInfo ( ) ; } getLatestUserInfo ( ... users ) { return this . _openChannelSession . getLatestUserInfo ( ... users ) ; } getAllLatestUserInfo ( ) { return this . _openChannelSession . getAllLatestUserInfo ( ) ; } getLatestOpenLink ( ) { return this . _openChannelSession . getLatestOpenLink ( ) ; } createEvent ( chat , type , count ) { return this . _openChannelSession . createEvent ( chat , type , count ) ; } getKickList ( ) { return this . _openChannelSession . getKickList ( ) ; } removeKicked ( user ) { return this . _openChannelSession . removeKicked ( user ) ; } setUserPerm ( user , perm ) { return this . _openChannelSession . setUserPerm ( user , perm ) ; } handoverHost ( user ) { return this . _openChannelSession . handoverHost ( user ) ; } kickUser ( user ) { return this . _openChannelSession . kickUser ( user ) ; } blockUser ( user ) { return this . _openChannelSession . blockUser ( user ) ; } react ( flag ) { return this . _openChannelSession . react ( flag ) ; } getReaction ( ) { return this . _openChannelSession . getReaction ( ) ; } changeProfile ( profile ) { return this . _openChannelSession . changeProfile ( profile ) ; } hideChat ( chat ) { return this . _openChannelSession . hideChat ( chat ) ; } 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 ; const linkRes = await this . getLatestOpenLink ( ) ; if ( ! linkRes . success ) return linkRes ; return this . chatON ( ) ; } async pushReceived ( method , data , parentCtx ) { await this . _handler . pushReceived ( method , data , parentCtx ) ; await this . _openHandler . pushReceived ( method , data , parentCtx ) ; } }