export * from './store' ; import { ChannelDataStore } from '../../channel/store' ; import { Chatlog , getOriginalType , isDeletedChat } from '../../chat' ; import { MentionStruct } from '../../chat/attachment' ; import { MediaKeyComponent } from '../../media' ; import { ChannelUser } from '../../user' ; import { TalkChannel } from '../channel' ; /**
 * Store Chatlog and provides convenient methods.
 */ export class TalkChatData { constructor ( private _chat ) { } /**
     * Chat text. Empty string if value is nullish.
     */ get text ( ) : string { return this . _chat . text || '' ; } get sendAt ( ) : Date { return new Date ( this . _chat . sendAt ) ; } /**
     * Chatlog object
     */ get chat ( ) : Readonly < Chatlog > { return this . _chat ; } return { number } get originalType ( ) : number { return getOriginalType ( this . _chat . type ) ; } /**
     * Get url list in chat. Can be used to generate url preview.
     * It is not for detecting urls.
     */ get urls ( ) : string [ ] { if ( ! this . _chat . attachment || ! Array . isArray ( this . _chat . attachment [ 'urls' ] ) ) return [ ] ; return this . _chat . attachment [ 'urls' ] ; } /**
     * Get mention list
     */ get mentions ( ) : MentionStruct [ ] { if ( ! this . _chat . attachment || ! Array . isArray ( this . _chat . attachment [ 'mentions' ] ) ) return [ ] ; return this . _chat . attachment . mentions ; } return { boolean } get isShout ( ) : boolean { if ( ! this . _chat . attachment ) return false ; return ! ! this . _chat . attachment . shout ; } /**
     * Get medias on chat.
     */ get medias ( ) : TalkChatMedia [ ] { const attachment = this . _chat . attachment ; if ( ! attachment ) return [ ] ; if ( Array . isArray ( attachment [ 'kl' ] ) && Array . isArray ( attachment [ 'sl' ] ) && Array . isArray ( attachment [ 'imageUrls' ] ) ) { const keyList = attachment [ 'kl' ] as string [ ] ; const sizeList = attachment [ 'sl' ] as number [ ] ; const urlList = attachment [ 'imageUrls' ] as string [ ] ; return keyList . map ( ( key , index ) => { return { key , size : sizeList [ index ] , url : urlList [ index ] , } ; } ) ; } else if ( attachment [ 'k' ] || attachment [ 'tk' ] ) { const size = ( attachment [ 's' ] || attachment [ 'size' ] ) as number ; const url = attachment [ 'url' ] as string ; return [ { key : ( attachment [ 'k' ] || attachment [ 'tk' ] ) as string , size , url , } , ] ; } return [ ] ; } template T template U param { ChannelDataStore < T , U > } channel return { U | undefined } getSenderInfo < T , U > ( channel ) { return channel . getUserInfo ( this . _chat . sender ) ; } return { Partial < T > } attachment < T > ( ) { if ( ! this . _chat . attachment ) return { } ; return this . _chat . attachment as Partial < T > ; } param { TalkChannel } channel forwardTo ( channel ) { channel . sendChat ( this . _chat ) ; } return { boolean } isDeleted ( ) { return isDeletedChat ( this . _chat . type ) ; } param { ChannelUser [ ] } users return { boolean } isMentioned ( ... users ) { const mentions = this . mentions ; if ( mentions . length < 1 ) return false ; for ( const mention of mentions ) { const userId = mention . user_id ; for ( const user of users ) { if ( user . userId . eq ( userId ) ) return true ; } } return false ; } } export interface TalkChatMedia extends MediaKeyComponent { /**
     * Media size
     */ size : number ; /**
     * Media url
     */ url : string ; }