import { TalkSession } from '../client' ; import { InformedOpenLink , OpenChannel , OpenLink , OpenLinkChannelTemplate , OpenLinkComponent , OpenLinkCreateTemplate , OpenLinkKickedUser , OpenLinkKickedUserInfo , OpenLinkProfile , OpenLinkProfiles , OpenLinkProfileTemplate , OpenLinkSession , OpenLinkType , OpenLinkUpdateTemplate } from '../../openlink' ; import { CreateOpenLinkRes , InfoLinkRes , JoinInfoRes , KLSyncRes , SyncLinkRes , UpdateOpenLinkRes } from '../../packet/chat' ; import { AsyncCommandResult , KnownDataStatusCode } from '../../request' ; import { structToOpenLink , structToOpenLinkInfo , structToOpenLinkKickedUserInfo } from '../../packet/struct' ; import { Long } from 'bson' ; /**
 * Provides openlink operations
 */ export class TalkOpenLinkSession implements OpenLinkSession { private _lastLinkToken : number ; constructor ( private _session ) { this . _lastLinkToken = 0 ; } async getLatestLinkList ( ) { const res = await this . _session . request < SyncLinkRes > ( 'SYNCLINK' , { 'ltk' : this . _lastLinkToken , } ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; const list = ! res . ols ? [ ] : res . ols . map ( ( struct ) => { return { openLink : structToOpenLink ( struct ) , info : structToOpenLinkInfo ( struct ) } ; } ) ; this . _lastLinkToken = res . ltk ; return { status : res . status , success : true , result : list } ; } async getOpenLink ( ... components ) { const res = await this . _session . request < InfoLinkRes > ( 'INFOLINK' , { 'lis' : components . map ( ( component ) => component . linkId ) , } ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; const list = res . ols ? res . ols . map ( structToOpenLink ) : [ ] ; return { status : res . status , success : true , result : list } ; } async getJoinInfo ( linkURL , referer = 'EW' ) { const res = await this . _session . request < JoinInfoRes > ( 'JOININFO' , { 'lu' : linkURL , 'ref' : referer , } ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { status : res . status , success : true , result : { openLink : structToOpenLink ( res . ol ) , info : structToOpenLinkInfo ( res . ol ) } , } ; } async getKickList ( link ) { const res = await this . _session . request < KLSyncRes > ( 'KLSYNC' , { 'li' : link . linkId , } ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { status : res . status , success : true , result : res . kickMembers . map ( structToOpenLinkKickedUserInfo ) } ; } async removeKicked ( link , user ) { const res = await this . _session . request ( 'KLDELITEM' , { 'li' : link . linkId , 'c' : user . kickedChannelId , 'kid' : user . userId , } ) ; return { status : res . status , success : res . status === KnownDataStatusCode . SUCCESS } ; } async deleteLink ( link ) { const res = await this . _session . request < JoinInfoRes > ( 'DELETELINK' , { 'li' : link . linkId , } ) ; return { status : res . status , success : res . status === KnownDataStatusCode . SUCCESS } ; } async react ( link , flag ) { const res = await this . _session . request < JoinInfoRes > ( 'REACT' , { 'li' : link . linkId , 'rt' : flag ? 1 : 0 , } ) ; return { status : res . status , success : res . status === KnownDataStatusCode . SUCCESS } ; } async getReaction ( link ) { const res = await this . _session . request ( 'REACTCNT' , { 'li' : link . linkId , } ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { status : res . status , success : true , result : [ res [ 'rc' ] as number , res [ 'rt' ] as boolean ] , } ; } async createOpenChannel ( template , profile ) { const reqData = { 'lt' : OpenLinkType . CHANNEL , 'lip' : template . linkCoverURL || '' , 'aptp' : ! template . mainProfileOnly , 'ln' : template . linkName , 'pa' : template . activated , 'ri' : Long . fromInt ( Date . now ( ) / 1000 ) , 'ml' : template . userLimit , 'desc' : template . description , 'sc' : template . searchable , ... OpenLinkProfile . serializeLinkProfile ( profile ) } ; const res = await this . _session . request < CreateOpenLinkRes > ( 'CREATELINK' , reqData ) ; if ( res . status !== KnownDataStatusCode . SUCCESS || ! res . chatRoom ) return { status : res . status , success : false } ; return { success : true , status : res . status , result : { channelId : res . chatRoom . chatId , linkId : res . ol . li } } ; } async createOpenDirectProfile ( template , profile ) { const reqData = { 'lt' : OpenLinkType . PROFILE , 'lip' : template . linkCoverURL || '' , 'aptp' : ! template . mainProfileOnly , 'ln' : template . linkName , 'pa' : template . activated , 'ri' : Long . fromInt ( Date . now ( ) / 1000 ) , 'dcl' : template . userLimit , 'desc' : template . description , 'sc' : template . searchable , ... OpenLinkProfile . serializeLinkProfile ( profile ) } ; const res = await this . _session . request < CreateOpenLinkRes > ( 'CREATELINK' , reqData ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { success : true , status : res . status , result : { openLink : structToOpenLink ( res . ol ) , info : structToOpenLinkInfo ( res . ol ) } } ; } async createOpenProfile ( template ) { const reqData = { 'lt' : OpenLinkType . PROFILE , 'lip' : '' , 'aptp' : ! template . mainProfileOnly , 'ln' : template . linkName , 'pa' : template . activated , 'ri' : Long . fromInt ( Date . now ( ) / 1000 ) , 'did' : Long . fromNumber ( 40 ) , ... OpenLinkProfile . serializeLinkProfile ( { nickname : template . linkName , profilePath : template . linkCoverURL } ) , 'dcl' : template . directLimit , 'desc' : null , 'sc' : template . searchable , 'pfc' : JSON . stringify ( { description : template . description , tags : template . tags } ) } ; const res = await this . _session . request < CreateOpenLinkRes > ( 'CREATELINK' , reqData ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { success : true , status : res . status , result : { openLink : structToOpenLink ( res . ol ) , info : structToOpenLinkInfo ( res . ol ) } } ; } async updateOpenLink ( link , settings ) { const reqData = { 'li' : link . linkId , 'ln' : settings . linkName , 'ac' : settings . activated , 'pa' : true , 'pc' : settings . passcode || '' , 'desc' : settings . description , 'sc' : settings . searchable } ; if ( 'directLimit' in settings ) { reqData [ 'dcl' ] = settings . directLimit ; } if ( 'userLimit' in settings ) { reqData [ 'ml' ] = settings . userLimit ; } if ( settings . linkCoverURL ) { reqData [ 'lip' ] = settings . linkCoverURL ; } const res = await this . _session . request < UpdateOpenLinkRes > ( 'UPDATELINK' , reqData ) ; if ( res . status !== KnownDataStatusCode . SUCCESS ) return { status : res . status , success : false } ; return { success : true , status : res . status , result : { openLink : structToOpenLink ( res . ol ) , info : structToOpenLinkInfo ( res . ol ) } } ; } }