@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 27.3 kB
JavaScript
const e=new class{init(e){this.core=e}};function t(e,t){var n={};for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(n[s]=e[s]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(s=Object.getOwnPropertySymbols(e);o<s.length;o++)t.indexOf(s[o])<0&&Object.prototype.propertyIsEnumerable.call(e,s[o])&&(n[s[o]]=e[s[o]])}return n}function n(e,t,n,s){return new(n||(n=Promise))((function(o,i){function r(e){try{l(s.next(e))}catch(e){i(e)}}function a(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const s="AV_MBR_LIST",o="AV_BAN_MBR",i={NORMAL_MESSAGE:3,GROUP_TIPS_HAS_ROAMING:4,GROUP_SYSTEM_MESSAGE:5,GROUP_TIPS_HAS_NO_ROAMING:6,BROADCAST_MESSAGE:17,MESSAGE_REVOKED:20,MESSAGE_REACTION:21,LIVE_CUSTOM_DATA:100},r={GROUP_DISMISSED:5,QUIT_GROUP:8,AVCHATROOM_MEMBER_BANNED:21},a=60,l=2603,u=2686,c=2688,g=3122;class p{constructor(e){const{core:t,manager:n,groupID:s,getRequestParams:o,onSuccess:i,onFail:r}=e;this._name="Polling",this._core=t,this._manager=n,this._timeoutID=-1,this._isRunning=!1,this._groupID=s,this._getRequestParams=o,this._onSuccess=i,this._onFail=r}start(){this._isRunning=!0,this._request(),console.log(`${this._name}.start pollingInterval:${this._manager.getPollingInterval()}`)}isRunning(){return this._isRunning}_request(){return n(this,void 0,void 0,(function*(){try{const e=this._getRequestParams(this._groupID),t=yield function(e,t){return n(this,void 0,void 0,(function*(){const{longPollingKey:n,startSequence:s,startBroadcastSeq:o,simplifiedMessage:i}=e,r={Key:n,StartSeq:s,StartBroadcastSeq:o,DownsizeFlag:i,USP:1,HoldTime:90};return t.common.buildAndSendPacket({servcmd:"group_open_long_polling_http_svc.get_msg",data:r})}))}(e,this._core);this._onSuccess(this._groupID,t);const s=this._manager.getPollingInterval();this._runNextPolling(s)}catch(e){this._onFail(this._groupID,e),this._runNextPolling(2e3)}}))}_runNextPolling(e){this.isRunning()&&(this._timeoutID>-1&&clearTimeout(this._timeoutID),this._timeoutID=setTimeout(this._request.bind(this),e))}stop(){console.log(`${this._name}.stop`),this._timeoutID>-1&&(clearTimeout(this._timeoutID),this._timeoutID=-1),this._isRunning=!1}}class h{constructor(e){this._maxLength=e,this._map=new Map}set(e){var t;if(this._map.size>=this._maxLength){const e=(null===(t=this._map.entries().next().value)||void 0===t?void 0:t[0])||"";this._map.delete(e)}this._map.set(e,1)}has(e){return this._map.has(e)}delete(e){this.has(e)&&this._map.delete(e)}clear(){this._map.clear()}}const _=e=>e===i.GROUP_TIPS_HAS_NO_ROAMING||e===i.GROUP_TIPS_HAS_ROAMING,m=e=>e===i.GROUP_SYSTEM_MESSAGE;function d(n){const s=function(e){const{E:n,MCT:s,MR:o,MP:i,MTS:r,GId:a,MS:l,CCD:u,F_Account:c,IsSys:g,GInf:p,MsgBody:h}=e,_=t(e,["E","MCT","MR","MP","MTS","GId","MS","CCD","F_Account","IsSys","GInf","MsgBody"]);return Object.assign({Event:n,MsgClientTime:s,MsgRandom:o,MsgPriority:i,MsgTimeStamp:r,ToGroupId:a,MsgSeq:l,CloudCustomData:u,From_Account:c,IsSystemMsg:g,GroupInfo:M(p),MsgBody:I(h)},_)}(n);return function(t){const{Event:n}=t;(_(n)||m(n))&&(t.From_Account=t.From_Account||"@TIM#SYSTEM");s=n,(s===i.BROADCAST_MESSAGE||(e=>e===i.NORMAL_MESSAGE)(n))&&function(t){const{core:{OuterConstant:n}}=e;t.CloudCustomData=t.CloudCustomData||"",t.MsgBody=t.MsgBody.map((e=>{if(e.MsgType===n.MSG_CUSTOM){const{content:t={}}=e;e.content=Object.assign({Data:"",Desc:"",Ext:""},t)}return e}))}(t);var s;_(n)&&function(e){const{GroupJoinType:t,MsgOperatorMemberExtraInfo:n={},MsgMemberExtraInfo:s,Operator_Account:o,List_Account:i,OpType:r}=e.MsgBody||{};"number"==typeof t||1!==r&&2!==r||(e.MsgBody.GroupJoinType=2===r?0:1);s||(e.MsgBody.MsgMemberExtraInfo=i.map((e=>({UserId:e}))));1!==r||s||(e.MsgBody.MsgMemberExtraInfo=[{UserId:n.UserId}]);e.MsgBody.MsgOperatorMemberExtraInfo=Object.assign({Operator_Account:o,ImageUrl:"",NickName:""},n)}(t);m(n)&&function(e){const{MsgOperatorMemberExtraInfo:t={},Operator_Account:n}=e.MsgBody||{};e.MsgBody.MsgMemberExtraInfo=Object.assign({UserId:n,ImageUrl:"",NickName:""},t),e.MsgBody=Object.assign({Authentication:"",RemarkInfo:"",MsgKey:1e3*e.MsgTimeStamp},e.MsgBody),e.MsgBody=Object.keys(e.MsgBody).filter((e=>"MsgOperatorMemberExtraInfo"!==e)).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:e.MsgBody[n]})),{})}(t)}(s),s}function M(e){const n=e||{},{GN:s,GT:o,F_Hd:i,F_NN:r}=n,a=t(n,["GN","GT","F_Hd","F_NN"]),l=Object.assign({GroupName:s,GroupType:o},a);return i&&(l.From_AccountHeadurl=i),r&&(l.From_AccountNick=r),l}function I(e){let n=e;Array.isArray(e)||(n=[e]);const s=n.map((e=>{const{O_Account:n,Opt:s,L_Account:o,RT:i,UDF:r,OpInf:a,OnlineInf:l,MsgMemberExtraInfo:u}=e,c=t(e,["O_Account","Opt","L_Account","RT","UDF","OpInf","OnlineInf","MsgMemberExtraInfo"]),g=Object.assign({Operator_Account:n,OpType:s,List_Account:o,ReportType:i,UserDefinedField:r},c);return a&&(g.MsgOperatorMemberExtraInfo=function(e){const{Img:n,NN:s}=e,o=t(e,["Img","NN"]);return Object.assign({ImageUrl:n,NickName:s},o)}(a)),u&&(g.MsgMemberExtraInfo=function(e){return e.map((e=>{const{Img:n,NN:s}=e,o=t(e,["Img","NN"]);return Object.assign({ImageUrl:n,NickName:s},o)}))}(u)),l&&(g.OnlineMemberInfo=function(e){const{ET:t,Num:n}=e;return{ExpireTime:t,OnlineMemberNum:n}}(l)),g}));return Array.isArray(e)?s:s[0]}var v=new class{constructor(){this._name="MessageParser",this._sequenceList=new h(200),this._messageIDList=new h(100),this._broadcastMessageIDMap=new Map,this._reportMessageStackedCount=0}init(e,t){this._core=e,this._avChatRoomHandler=t}onMessageReceived(t,n,s=!1){this._sortServerMessageList({groupID:t,serverMessageList:n,isHistoryMessage:s});const o=this._handleMessageList(t,n);if(0===o.length)return;if(!s){const{appStore:{conversationStore:n},OuterConstant:{CONV_GROUP:s}}=this._core,i=(t=>{const{core:{common:{getMessagePreviewText:n}}}=e;return{lastTime:(null==t?void 0:t.time)||(null==t?void 0:t.lastTime)||0,lastSequence:(null==t?void 0:t.sequence)||(null==t?void 0:t.lastSequence)||0,fromAccount:(null==t?void 0:t.from)||(null==t?void 0:t.fromAccount)||"",messageForShow:n(null==t?void 0:t.type,null==t?void 0:t.payload),payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",onlineOnlyFlag:(null==t?void 0:t._onlineOnlyFlag)||!1,nick:(null==t?void 0:t.nick)||"",nameCard:(null==t?void 0:t.nameCard)||"",version:(null==t?void 0:t.version)||0,isPeerRead:(null==t?void 0:t.isPeerRead)||!1,revoker:(null==t?void 0:t.revoker)||null}})(o[o.length-1]);n.updateConversation(`${s}${t}`,{lastMessage:i})}this._checkMessageStacked(o);const i=o.filter((e=>!0===e.isModified)),r=o.filter((e=>!1===e.isModified)),{OuterEvent:{MESSAGE_RECEIVED:a,MESSAGE_MODIFIED:l}}=this._core;i.length>0&&this._emitEvent({name:l,data:i}),r.length>0&&this._emitEvent({name:a,data:r})}_sortServerMessageList(e){const{groupID:t,serverMessageList:n,isHistoryMessage:s}=e;let o=[];this._avChatRoomHandler.isPollingSimplifiedMessage()&&!s?(n.sort(((e,t)=>e.MS-t.MS)),o=n.map((e=>e.MS))):(n.sort(((e,t)=>e.MsgSeq-t.MsgSeq)),o=n.map((e=>e.MsgSeq))),console.log(`${this._name}._sortServerMessageList groupID:${t} count:${o.length} sequenceList:${o}`),o.length=0}_handleMessageList(e,t){const{message:{messageDataHandler:n,messageHelper:s}}=this._core,o=this._avChatRoomHandler.isPollingSimplifiedMessage(),i=[],a=t.length;for(let l=0;l<a;l++){let a=t[l];if(o&&(a=d(a)),m(a.Event)&&Object.values(r).includes(a.MsgBody.ReportType)){this._onGroupDismissed(e);break}if(null===this._filterMessageItem(a))continue;this._patchMessageType(a);const u=s.parseServerGroupMessage(a),c=this._createMessage(u);_(a.Event)&&this._avChatRoomHandler.isGroupAttributesUpdated(u)?this._avChatRoomHandler.handleGroupAttributesUpdated(u):null!==this._filterDuplicateMessage(c)&&(n.isMessageSentByCurrentInstance(c)||(1===u.isModified&&(c.isModified=!0),_(a.Event)&&this._updateLocalOnlineMemberCountFromTips(a),i.push(c)))}return i}_patchMessageType(e){const{OuterConstant:t}=this._core;_(e.Event)&&(e.MsgBody.MsgType=t.MSG_GRP_TIP),m(e.Event)&&(e.MsgBody.MsgType=t.MSG_GRP_SYS_NOTICE)}_filterMessageItem(e){const{Event:t}=e;return Object.values(i).includes(t)?(e=>e===i.MESSAGE_REVOKED)(t)?(this._handleMessageRevoked(e),null):(e=>e===i.LIVE_CUSTOM_DATA)(t)?(this._onLiveCustomData(e),null):(e=>e===i.MESSAGE_REACTION)(t)?null:e:(console.warn(`${this._name}.onMessageReceived unknown event:${t}`),null)}_createMessage(e){const{message:{messageFactory:t},OuterConstant:n}=this._core;let s=n.CONV_GROUP;e.event===i.GROUP_SYSTEM_MESSAGE&&(s=n.CONV_SYSTEM);const o=!!e.isSystemMessage,r=t.createMessage(Object.assign(Object.assign({},e),{conversationType:s,isSystemMessage:o,flow:"in"}));return r.setElement(e.elements),r}_filterDuplicateMessage(e){const{common:t}=this._core;if(!t.isUnlimitedAVChatRoom()){if(this._sequenceList.has(e.sequence))return null;this._sequenceList.set(e.sequence)}const n=this._messageIDList.has(e.ID);return n?(console.warn(`${this._name}_filterDuplicateMessageItem ID:${e.ID} has:${n}`),null):(this._messageIDList.set(e.ID),e)}_handleMessageRevoked(e){const{OuterConstant:t,OuterEvent:{MESSAGE_REVOKED:n}}=this._core,{ToGroupId:s,MsgBody:{RevokeMsgList:o},RevokerInfo:{Revoker_Account:i,Reason:r=""}}=e,a=[];o.forEach((e=>{const{TinyId:n,MsgClientTime:o,Random:l,MsgSeq:u}=e,c={conversationID:`${t.CONV_GROUP}${s}`,ID:`${n}-${o}-${l}`,revoker:i,revokeReason:r,revokerInfo:{userID:i,nick:"",avatar:""},sequence:u};a.push(c)})),0!==a.length&&this._emitEvent({name:n,data:a})}_onLiveCustomData(e){const{OuterEvent:{ROOM_CUSTOM_DATA_RECEIVED:t}}=this._core,{ToGroupId:n,MsgSeq:s,MsgTimeStamp:o,MsgBody:i}=e,r=(null==i?void 0:i.MsgContent)||"";this._emitEvent({name:t,data:r}),console.log(`${this._name}._onLiveCustomData groupID:${n} sequence:${s} time:${o} data:${r}`)}_onGroupDismissed(e){this._avChatRoomHandler.reset(e)}_checkMessageStacked(e){const{length:t}=e;if(t>=100&&this._reportMessageStackedCount<5){const e=this._avChatRoomHandler.getJoinedGroups();this._core.ssoLog.createSSOLogData({method:"MessageStacked"}).setLogInfo({message:`count:${t} groupID:${e.join(",")}`}).end(),this._reportMessageStackedCount+=1}}_emitEvent(e){this._core.notificationCenter.emitOuterEvent(e.name,e)}onBroadcastMessageReceived(e){const{message:{messageHelper:t},OuterEvent:{MESSAGE_RECEIVED:n}}=this._core,s=this._avChatRoomHandler.isPollingSimplifiedMessage(),o=[],r=e.length;for(let n=0;n<r;n++){let r=e[n];s&&(r=d(r));const a=t.parseServerGroupMessage(r);if(!Object.values(i).includes(r.Event)){console.warn(`${this._name}.onBroadcastMessageReceived unknown event:${r.Event}`);continue}const l=this._createMessage(a);l.isBroadcastMessage=!0,this._broadcastMessageIDMap.has(l.ID)||(o.push(l),this._broadcastMessageIDMap.set(l.ID,1))}o.length>0&&this._emitEvent({name:n,data:o})}_updateLocalOnlineMemberCountFromTips(e){const{utils:{isEmpty:t}}=this._core,{ToGroupId:n,MsgBody:{OnlineMemberInfo:s}}=e;if(t(s))return;const{OnlineMemberNum:o=0,ExpireTime:i=a}=s,r=Date.now();let l=this._avChatRoomHandler.getLocalOnlineMemberCount(n);t(l)?l={lastReqTime:0,lastSyncTime:0,latestUpdateTime:r,memberCount:o,expireTime:i}:(l.latestUpdateTime=r,l.memberCount=o),this._avChatRoomHandler.updateLocalOnlineMemberCount(n,l)}reset(){this._reportMessageStackedCount=0,this._sequenceList.clear(),this._messageIDList.clear(),this._broadcastMessageIDMap.clear()}};const S=t=>{const{core:{store:n}}=e;return(n.get("cloudConfig")||{})[t]},f=t=>{const{core:{utils:{isUndefined:n}}}=e;return!n(t)},C=()=>{const e=S("polling_interval");return f(e)?parseInt(e,10):300},O=()=>{const e=S("polling_simplified_msg");return f(e)?parseInt(e,10):0};var b=new class{constructor(){this._name="AVChatRoomHandler",this._joinedGroupMap=new Map,this._pollingRequestInfoMap=new Map,this._pollingInstanceMap=new Map,this._onlineMemberCountMap=new Map,this._pollingInterval=300,this._pollingNoMessageCount=0,this._startBroadcastSequence=1}init(e,t){this._core=e,this._parentPlugin=t,v.init(e,this),console.log("AVChatRoomHandler.init")}onAVChatRoomSystemNotification(e){const{OuterConstant:{GRP_AVCHATROOM:t}}=this._core,{GroupTips:n=[]}=e;for(let e=0;e<n.length;e++){const{MsgBody:s,GroupInfo:{GroupId:o,GroupType:i}}=n[e],a=Object.values(r).includes(s.ReportType);i===t&&a&&this.reset(o)}}getPollingInterval(){return this._pollingInterval}isPollingSimplifiedMessage(){return 0!==O()}isGroupAttributesUpdated(e){return!!this._parentPlugin.groupAttribute.isGroupAttributesUpdated(e)&&(this._parentPlugin.groupAttribute.handleGroupAttributesUpdated(e),!0)}updateLocalOnlineMemberCount(e,t){this._onlineMemberCountMap.set(e,t)}getLocalOnlineMemberCount(e){return this._onlineMemberCountMap.get(e)||{}}hasPollingInstance(e){return this._pollingInstanceMap.has(e)}getJoinedLiveList(){let e=[];return this._joinedGroupMap.size>0&&(e=[...this._joinedGroupMap.values()].filter((e=>e.type===TYPES.GRP_LIVE))),e}handleJoinGroupResult(e){return n(this,void 0,void 0,(function*(){const{utils:{isUndefined:t},OuterConstant:{CONV_GROUP:n},apiMap:{getConversationProfile:s},OuterConstant:o}=this._core,{longPollingKey:i,group:r,historyMessageList:a}=e,{groupID:l}=r;return yield this._preCheck(r),this._joinedGroupMap.set(l,r),this._parentPlugin.groupDataHandler.updateLocalGroup([r]),this._parentPlugin.groupDataHandler.emitGroupListUpdate(),s(`${n}${l}`),a.length>0&&v.onMessageReceived(l,a,!0),t(i)?{status:o.JOIN_STATUS_SUCCESS,group:r}:this.startMessageLongPolling(e)}))}_preCheck(e){return n(this,void 0,void 0,(function*(){const{common:t,OuterConstant:n,helper:s,apiMap:{quitGroup:o}}=this._core;if(t.isUnlimitedAVChatRoom()){if(this._pollingInstanceMap.size>(()=>{const e=S("polling_count_limit");return f(e)&&e>0?parseInt(e,10):20})())throw new s.ChatError({code:c,message:"the count of longPolling exceeds the max limit"});return}if(0===this._joinedAVChatRoomCount())return;if(e.type===n.GRP_LIVE)return;const[i,r]=this._joinedGroupMap.entries().next().value;r.selfInfo.role===n.GRP_MBR_ROLE_OWNER?this._parentPlugin.groupDataHandler.deleteLocalGroup(i):yield o(i),this.reset(i)}))}startMessageLongPolling(e){const{OuterConstant:t}=this._core,{longPollingKey:n,startSequence:s=1,group:o}=e,{groupID:i}=o;return this._pollingRequestInfoMap.set(i,{longPollingKey:n,startSequence:s}),this._pollingInterval=C(),this._startPolling(i),{status:t.JOIN_STATUS_SUCCESS,group:o}}_startPolling(e){if(console.log(`${this._name}._startPolling groupID:${e}`),this._pollingInstanceMap.has(e)){const t=this._pollingInstanceMap.get(e);return void((null==t?void 0:t.isRunning())||null==t||t.start())}const t=new p({core:this._core,manager:this,groupID:e,getRequestParams:this._handleRequestParams.bind(this),onSuccess:this._handleSuccess.bind(this),onFail:this._handleFailure.bind(this)});t.start(),this._pollingInstanceMap.set(e,t)}_handleRequestParams(e){const{longPollingKey:t,startSequence:n}=this._pollingRequestInfoMap.get(e)||{};return e===[...this._pollingInstanceMap.keys()][0]?{longPollingKey:t,startSequence:n,startBroadcastSeq:this._startBroadcastSequence,simplifiedMessage:O()}:{longPollingKey:t,startSequence:n,simplifiedMessage:O()}}_handleSuccess(e,t){const{ErrorCode:n}=t;if(0===n)this._hasJoinedAVChatRoom(e)&&this._handleResponseData(e,t);else{const{longPollingKey:t,startSequence:s}=this._pollingRequestInfoMap.get(e)||{};console.warn(`${this._name}._handleSuccess groupID:${e} key:${t} startSeq:${s} errorCode:${n}`)}}_handleResponseData(e,t){const{Key:n,NextSeq:s,NextBroadcastSeq:o,RspMsgList:i=[],RspBroadcastMsgList:r=[]}=t;n&&s&&this._pollingRequestInfoMap.set(e,{longPollingKey:n,startSequence:s}),o&&o>this._startBroadcastSequence&&(this._startBroadcastSequence=o),i.length>0?(0!==this._pollingNoMessageCount&&(this._pollingNoMessageCount=0,this._pollingInterval=C()),v.onMessageReceived(e,i)):(this._pollingNoMessageCount+=1,this._pollingNoMessageCount===(()=>{const e=S("polling_no_msg_count");return f(e)?parseInt(e,10):20})()&&(this._pollingInterval=C()+(()=>{const e=S("polling_interval_plus");return f(e)?parseInt(e,10):2e3})())),r.length>0&&v.onBroadcastMessageReceived(r)}_handleFailure(e,t){console.warn(`${this._name}._handleFailure groupID:${e} error:`,t)}_joinedAVChatRoomCount(){const{OuterConstant:e}=this._core;let t=[];return this._joinedGroupMap.size>0&&(t=this.getJoinedGroups().filter((t=>t.type===e.GRP_AVCHATROOM))),t.length}_hasJoinedAVChatRoom(e){return this._joinedGroupMap.has(e)}getJoinedGroups(){return[...this._joinedGroupMap.values()]}_stopPolling(e){if(e){console.log(`${this._name}._stopPolling groupID:${e}`);const{appStore:{conversationStore:t},OuterConstant:{CONV_GROUP:n}}=this._core;t.deleteConversation(`${n}${e}`);const s=this._pollingInstanceMap.get(e);return null==s||s.stop(),this._parentPlugin.groupDataHandler.deleteLocalGroup(e),this._pollingInstanceMap.delete(e),this._pollingRequestInfoMap.delete(e),this._joinedGroupMap.delete(e),void this._onlineMemberCountMap.delete(e)}console.log(`${this._name}._stopPolling all`);for(const e of this._pollingInstanceMap.values())e.stop();this._pollingInstanceMap.clear(),this._pollingRequestInfoMap.clear(),this._joinedGroupMap.clear(),this._onlineMemberCountMap.clear()}reset(e){this._stopPolling(e),this._pollingInterval=300,this._pollingNoMessageCount=0,this._startBroadcastSequence=1,v.reset()}};var R=new class{init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"joinGroup",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}joinGroup(e){return n(this,void 0,void 0,(function*(){const{OuterConstant:t}=this._core,n=yield this._parentPlugin.groupAction.joinGroup(e),{data:{group:{type:s}}}=n;return s===t.GRP_AVCHATROOM?b.handleJoinGroupResult(n.data):n}))}};var E=new class{init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"quitGroup",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}quitGroup(e){return n(this,void 0,void 0,(function*(){const{OuterConstant:t}=this._core,n=yield this._parentPlugin.groupAction.quitGroup(e),{data:{type:s}}=n;return s===t.GRP_AVCHATROOM&&b.reset(e),n}))}};var y=new class{init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"dismissGroup",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}dismissGroup(e){return n(this,void 0,void 0,(function*(){const{OuterConstant:t}=this._core,n=yield this._parentPlugin.groupAction.dismissGroup(e),{data:{type:s}}=n;return s===t.GRP_AVCHATROOM&&b.reset(e),n}))}};var G=new class{constructor(){this._name="GetAVChatRoomMemberList"}init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"getGroupMemberList",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}getGroupMemberList(e){return n(this,void 0,void 0,(function*(){const{appStore:{groupStore:t},helper:n,OuterConstant:o}=this._core,{groupID:i}=e,r=t.getGroup(i);if((null==r?void 0:r.type)===o.GRP_AVCHATROOM){if(n.checkBusinessCapabilityBits(s))return this._getGroupMemberList(e)}return this._parentPlugin.groupMember.getGroupMemberList(e)}))}_getGroupMemberList(e){return n(this,void 0,void 0,(function*(){const t="_getGroupMemberList",{helper:s}=this._core;try{const s=yield function(e,t){return n(this,void 0,void 0,(function*(){const{groupID:n,offset:s=0}=e,o={GroupId:n,Timestamp:s};return t.common.buildAndSendPacket({servcmd:"group_open_avchatroom_http_svc.get_members",data:o})}))}(e,this._core),{MemberList:o=[],NextTimestamp:i=0}=s||{},r=this._handleMemberList(o);return console.log(`${this._name}.${t} ok, groupID:${e.groupID} count:${r.length} nextOffset:${i}`),{code:0,data:{memberList:r,offset:i}}}catch(e){const n=new s.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${this._name}.${t} fail:`,n),n}}))}_handleMemberList(e){return e.map((e=>{const{Member_Account:t,NickName:n="",Avatar:s="",Remark:o="",JoinTime:i=0,Marks:r=[]}=e;return{userID:t,nick:n,avatar:s,remark:o,joinTime:i,marks:r,isOnline:!0}}))}};var A=new class{constructor(){this._name="GetAVChatRoomOnlineMemberCount"}init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"getGroupOnlineMemberCount",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}getGroupOnlineMemberCount(e){return n(this,void 0,void 0,(function*(){const{appStore:{groupStore:t},OuterConstant:n}=this._core,s=t.getGroup(e);return s?s.type===n.GRP_AVCHATROOM?this._getOnlineMemberCount(e):this._parentPlugin.groupAction.getGroupOnlineMemberCount(e):{code:0,data:{memberCount:0}}}))}_getOnlineMemberCount(e){return n(this,void 0,void 0,(function*(){const t="_getOnlineMemberCount",{utils:{isEmpty:n}}=this._core,s=b.getLocalOnlineMemberCount(e);if(n(s)||this._isExpired(e)){const{memberCount:n=0}=yield this._getOnlineMemberCountFromServer(e);return console.log(`${this._name}.${t} ok, groupID:${e} memberCount:${n} from server.`),{code:0,data:{memberCount:n}}}return console.log(`${this._name}.${t} ok, groupID:${e} memberCount:${s.memberCount} from local.`),{code:0,data:{memberCount:s.memberCount}}}))}_isExpired(e){const t=b.getLocalOnlineMemberCount(e),n=Date.now(),s=n-t.lastSyncTime>1e3*t.expireTime,o=n-t.latestUpdateTime>1e4,i=n-t.lastReqTime>3e3;return s&&o&&i}_getOnlineMemberCountFromServer(e){return n(this,void 0,void 0,(function*(){const t="_getOnlineMemberCountFromServer";try{const t=yield function(e,t){return n(this,void 0,void 0,(function*(){const n={GroupId:e};return t.common.buildAndSendPacket({servcmd:"group_open_http_svc.get_online_member_num",data:n})}))}(e,this._core),{OnlineMemberNum:s=0,ExpireTime:o=a}=t||{},i=Date.now(),r={lastSyncTime:i,latestUpdateTime:i,lastReqTime:i,memberCount:s,expireTime:o};return b.updateLocalOnlineMemberCount(e,r),{memberCount:s}}catch(e){const n=new this._core.helper.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${this._name}.${t} fail:`,n),n}}))}};var P=new class{constructor(){this._name="DeleteAVChatRoomMember"}init(e,t){this._core=e,this._parentPlugin=t;const{helper:n}=e;n.registerApi({apiName:"deleteGroupMember",context:this,matcher:()=>t.getInstalledSubPlugins().length>0})}deleteGroupMember(e){return n(this,void 0,void 0,(function*(){const t="deleteGroupMember",{appStore:{groupStore:n},utils:{isUndefined:s},helper:i,OuterConstant:r}=this._core,{groupID:a}=e,u=n.getGroup(a);if(s(u))throw new i.ChatError({functionName:t,code:l});if(u.type===r.GRP_AVCHATROOM){if(i.checkBusinessCapabilityBits(o))return this._deleteGroupMember(e);throw new i.ChatError({functionName:t,code:g})}return this._parentPlugin.groupMember.deleteGroupMember(e)}))}_deleteGroupMember(e){return n(this,void 0,void 0,(function*(){const t="_deleteGroupMember",{appStore:{groupStore:s},helper:o}=this._core,{groupID:i,duration:r=0,userIDList:a}=e;if(0===r)throw new o.ChatError({functionName:t,code:u});try{yield function(e,t){return n(this,void 0,void 0,(function*(){const{groupID:n,userIDList:s,duration:o,reason:i}=e,r={GroupId:n,Members_Account:s,Duration:o,Description:i};return t.common.buildAndSendPacket({servcmd:"group_open_http_svc.ban_group_member",data:r})}))}(e,this._core),console.log(`${this._name}.${t} ok, groupID:${i}`);return{code:0,data:{group:s.getGroup(i),userIDList:a}}}catch(e){const n=new o.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${this._name}.${t} fail:`,n),n}}))}};var D=new class{constructor(){this._name="MarkAVChatRoomMember"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"markGroupMemberList",context:this})}markGroupMemberList(e){return n(this,void 0,void 0,(function*(){const t="markGroupMemberList",{groupID:s,markType:o,enableMark:i,userIDList:r=[]}=e,a=this._generateRequestData(e);try{const e=yield function(e,t){return n(this,void 0,void 0,(function*(){const{groupID:n,operationType:s,memberList:o}=e,i={GroupId:n,CommandType:s,MemberList:o};return t.common.buildAndSendPacket({servcmd:"group_open_avchatroom_http_svc.modify_user_info",data:i})}))}(a,this._core),{MemberList:l=[]}=e||{},{successUserIDList:u,failureUserIDList:c}=this._handleResult(l,r);return console.log(`${this._name}.${t} ok, groupID:${s} markType:${o} enableMark:${i} success:${u.length} fail:${c.length}`),{code:0,data:{successUserIDList:u,failureUserIDList:c}}}catch(e){const n=new this._core.helper.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${this._name}.${t} fail:`,n),n}}))}_generateRequestData(e){const{groupID:t,markType:n,enableMark:s,userIDList:o=[]}=e,i=!0===s?1:2,r=[...o];r.length>500&&console.warn(`${this._name}._generateRequestData, the length of userIDList cannot exceed 500`);return{groupID:t,operationType:i,memberList:r.map((e=>({Member_Account:e,Marks:[n]})))}}_handleResult(e,t){const n=[],s=[];return e.length===t.length?(n.push(...t),{successUserIDList:n,failureUserIDList:s}):(t.forEach((t=>{e.find((e=>e.Member_Account===t))?n.push(t):s.push(t)})),{successUserIDList:n,failureUserIDList:s})}};var T=new class{init(e,t){console.log("AVChatRoomAction.init"),R.init(e,t),E.init(e,t),y.init(e,t),G.init(e,t),A.init(e,t),P.init(e,t),D.init(e)}};var L=new class{constructor(){this._name="LiveHandler"}init(e){this._core=e;const{helper:t}=e;t.registerExperimentalAPI("startMessageLongPolling",this),t.registerExperimentalAPI("stopMessageLongPolling",this),console.log("LiveHandler.init")}startMessageLongPolling(e){const{common:t,utils:{isEmpty:n},OuterConstant:s}=this._core,{groupID:o,longPollingKey:i,longPollingSequence:r=1}=e;if(n(i))return console.warn(`${this._name}.startMessageLongPolling longPollingKey is empty.`),Promise.resolve({});b.hasPollingInstance(o)&&this.stopMessageLongPolling({groupID:o});const a=b.getJoinedLiveList();!t.isUnlimitedAVChatRoom()&&a.length>0&&this.stopMessageLongPolling({groupID:a[0]});const l={groupID:o,type:s.GRP_LIVE};return console.log(`${this._name}.startMessageLongPolling groupID:${o} longPollingKey:${i} longPollingSequence:${r}`),b.startMessageLongPolling({group:l,longPollingKey:i,startSequence:r})}stopMessageLongPolling(e){const{groupID:t}=e;return b.reset(t),console.log(`${this._name}.stopMessageLongPolling ok, groupID:${t}`),Promise.resolve({groupID:t})}};var N=new class{constructor(){this.name="AVChatRoom"}install(t,n){this._core=t,e.init(t),b.init(t,n),T.init(t,n),L.init(t);const{notificationCenter:s,InnerEvent:o}=t,{InnerEventSubType:i}=s;s.subscribeInnerEvent(o.MESSAGE_PUSH,i.GROUP_SYSTEM_NOTIFICATION,this._onAVChatRoomSystemNotification,this),s.subscribeInnerEvent(o.LOGOUT,this._reset,this),s.subscribeInnerEvent(o.DESTROY,this._dispose,this)}_onAVChatRoomSystemNotification(e){b.onAVChatRoomSystemNotification(e)}_reset(){b.reset()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core,{InnerEventSubType:n}=e;e.unSubscribeInnerEvent(t.MESSAGE_PUSH,n.GROUP_SYSTEM_NOTIFICATION,this._onAVChatRoomSystemNotification,this),e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};export{N as default};