@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 30.5 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).TencentCloudChatAvchatroom=t()}(this,function(){"use strict";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{u(s.next(e))}catch(e){i(e)}}function a(e){try{u(s.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(r,a)}u((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,u=2603,l=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.getCurrentPollingInterval(this._groupID)}`)}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.getCurrentPollingInterval(this._groupID);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 timerID:${this._timeoutID}`),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:u,CCD:l,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:u,CloudCustomData:l,From_Account:c,IsSystemMsg:g,GroupInfo:M(p),MsgBody:v(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=null==i?void 0: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,F_Ll:a}=n,u=t(n,["GN","GT","F_Hd","F_NN","F_Ll"]),l=Object.assign({GroupName:s,GroupType:o},u);return i&&(l.From_AccountHeadurl=i),r&&(l.From_AccountNick=r),a&&(l.From_AccountLevel=a),l}function v(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:u,MsgMemberExtraInfo:l}=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)),l&&(g.MsgMemberExtraInfo=function(e){return null==e?void 0:e.map(e=>{const{Img:n,NN:s}=e,o=t(e,["Img","NN"]);return Object.assign({ImageUrl:n,NickName:s},o)})}(l)),u&&(g.OnlineMemberInfo=function(e){const{ET:t,Num:n}=e;return{ExpireTime:t,OnlineMemberNum:n}}(u)),g});return Array.isArray(e)?s:s[0]}var I=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:u}}=this._core;i.length>0&&this._emitEvent({name:u,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 u=0;u<a;u++){let a=t[u];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 l=s.parseServerGroupMessage(a),c=this._createMessage(l);c.level=a.GroupInfo.From_AccountLevel,_(a.Event)&&this._avChatRoomHandler.isGroupAttributesUpdated(l)?this._avChatRoomHandler.handleGroupAttributesUpdated(l):null!==this._filterDuplicateMessage(c)&&(n.isMessageSentByCurrentInstance(c)||(1===l.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:u,MsgSeq:l}=e,c={conversationID:`${t.CONV_GROUP}${s}`,ID:`${n}-${o}-${u}`,revoker:i,revokeReason:r,revokerInfo:{userID:i,nick:"",avatar:""},sequence:l};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.Content)||(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.info("MessageStacked",`count:${t} groupID:${e.join(",")}`),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 u=this._createMessage(a);u.isBroadcastMessage=!0,this._broadcastMessageIDMap.has(u.ID)||(o.push(u),this._broadcastMessageIDMap.set(u.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 u=this._avChatRoomHandler.getLocalOnlineMemberCount(n);t(u)?u={lastReqTime:0,lastSyncTime:0,latestUpdateTime:r,memberCount:o,expireTime:i}:(u.latestUpdateTime=r,u.memberCount=o),this._avChatRoomHandler.updateLocalOnlineMemberCount(n,u)}reset(){this._reportMessageStackedCount=0,this._sequenceList.clear(),this._messageIDList.clear(),this._broadcastMessageIDMap.clear()}};const f=t=>{const{core:{store:n}}=e;return(n.get("cloudConfig")||{})[t]},S=t=>{const{core:{utils:{isUndefined:n}}}=e;return!n(t)},C=()=>{const e=f("polling_interval");return S(e)?parseInt(e,10):300},b=()=>{const e=f("polling_simplified_msg");return S(e)?parseInt(e,10):0};var O=new class{constructor(){this._name="AVChatRoomHandler",this._joinedGroupMap=new Map,this._pollingRequestInfoMap=new Map,this._pollingInstanceMap=new Map,this._onlineMemberCountMap=new Map,this._pollingIntervalMap=new Map,this._pollingNoMessageCountMap=new Map,this._membersReqInfoMap=new Map,this._startBroadcastSequence=1}init(e,t){this._core=e,this._parentPlugin=t,I.init(e,this),e.ssoLog.debug("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)}}getCurrentPollingInterval(e){return this._pollingIntervalMap.get(e)||C()}isPollingSimplifiedMessage(){return 0!==b()}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(){const{OuterConstant:{GRP_LIVE:e}}=this._core;let t=[];return this._joinedGroupMap.size>0&&(t=[...this._joinedGroupMap.values()].filter(t=>t.type===e)),t}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:u}=r;if(yield this._preCheck(r),this._joinedGroupMap.set(u,r),this._parentPlugin.groupDataHandler.updateLocalGroup([r]),this._parentPlugin.groupDataHandler.emitGroupListUpdate(),s(`${n}${u}`),a.length>0&&I.onMessageReceived(u,a,!0),!t(i)){return{code:0,data:this.startMessageLongPolling(e)}}return{code:0,data:{status:o.JOIN_STATUS_SUCCESS,group:r}}})}_preCheck(e){return n(this,void 0,void 0,function*(){const{common:t,OuterConstant:n,helper:s,apiMap:{quitGroup:o},ssoLog:i}=this._core;if(t.isUnlimitedAVChatRoom()){if(this._pollingInstanceMap.size>(()=>{const e=f("polling_count_limit");return S(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[r,a]=this._joinedGroupMap.entries().next().value;if(a.selfInfo.role===n.GRP_MBR_ROLE_OWNER)this._parentPlugin.groupDataHandler.deleteLocalGroup(r);else try{yield o(r)}catch(e){i.debug("quitGroup",`${this._name}._preCheck quitGroup failed, groupID:${r} info:`,e)}this.reset(r)})}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._pollingIntervalMap.set(i,C()),this._startPolling(i),this._reportLongPollingCount(),{status:t.JOIN_STATUS_SUCCESS,group:o}}_startPolling(e){if(this._core.ssoLog.debug("_startPolling",`${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:b()}:{longPollingKey:t,startSequence:n,simplifiedMessage:b()}}_handleSuccess(e,t){const{ErrorCode:n}=t;if(0!==n){const{longPollingKey:t,startSequence:s}=this._pollingRequestInfoMap.get(e)||{};return void console.warn(`${this._name}._handleSuccess groupID:${e} key:${t} startSeq:${s} errorCode:${n}`)}this._hasJoinedAVChatRoom(e)&&this._handleResponseData(e,t)}_handleResponseData(e,t){const{Key:n,NextSeq:s,NextBroadcastSeq:o,RspMsgList:i=[],RspBroadcastMsgList:r=[]}=t;if(n&&s&&this._pollingRequestInfoMap.set(e,{longPollingKey:n,startSequence:s}),o&&o>this._startBroadcastSequence&&(this._startBroadcastSequence=o),i.length>0)0!==this._getPollingNoMessageCount(e)&&(this._updatePollingNoMessageCount(e,0),this._pollingIntervalMap.set(e,C())),I.onMessageReceived(e,i);else{let t=this._getPollingNoMessageCount(e);if(t+=1,this._updatePollingNoMessageCount(e,t),t===(()=>{const e=f("polling_no_msg_count");return S(e)?parseInt(e,10):20})()){const t=C()+(()=>{const e=f("polling_interval_plus");return S(e)?parseInt(e,10):2e3})();this._pollingIntervalMap.set(e,t)}}r.length>0&&I.onBroadcastMessageReceived(r)}_handleFailure(e,t){const{ssoLog:n,utils:{safeStringify:s}}=this._core;n.warn("polling",`${this._name}._handleFailure groupID:${e} error: ${s(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()]}updateLocalLiveGroup(e,t){this._joinedGroupMap.set(e,t),this._parentPlugin.groupDataHandler.updateLocalGroup([t])}handleLiveHistoryMessages(e,t){I.onMessageReceived(e,t,!0)}isOverFrequencyLimit(e){if(!this._membersReqInfoMap.has(e))return this._membersReqInfoMap.set(e,{startTime:Date.now(),requestCount:1}),!1;let{startTime:t,requestCount:n}=this._membersReqInfoMap.get(e);const{interval:s,count:o}=(()=>{const e=f("av_members_freq_limit");if(S(e)){const{interval:t,count:n}=JSON.parse(e);if(n>0&&t>0)return{interval:t,count:n}}return{interval:30,count:4}})();return Date.now()-t>1e3*s?(this._membersReqInfoMap.set(e,{startTime:Date.now(),requestCount:1}),!1):(n+=1,this._membersReqInfoMap.set(e,{startTime:t,requestCount:n}),n>o)}_stopPolling(e){if(this._core.ssoLog.debug("_stopPolling",`${this._name}._stopPolling groupID:${e}`),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),this._onlineMemberCountMap.delete(e),this._pollingIntervalMap.delete(e),this._pollingNoMessageCountMap.delete(e),void this._membersReqInfoMap.delete(e)}for(const e of this._pollingInstanceMap.values())null==e||e.stop();this._pollingInstanceMap.clear(),this._pollingRequestInfoMap.clear(),this._joinedGroupMap.clear(),this._onlineMemberCountMap.clear(),this._pollingIntervalMap.clear(),this._pollingNoMessageCountMap.clear(),this._membersReqInfoMap.clear()}_updatePollingNoMessageCount(e,t){this._pollingNoMessageCountMap.set(e,t)}_getPollingNoMessageCount(e){return this._pollingNoMessageCountMap.get(e)||0}_reportLongPollingCount(){const e=this._joinedGroupMap.size;if(e>1){const{common:t,OuterConstant:n,ssoLog:s}=this._core,o=t.isUnlimitedAVChatRoom()?1:0,i=[],r=[];this._joinedGroupMap.values().forEach(({groupID:e,type:t})=>{t===n.GRP_LIVE?r.push(e):i.push(e)}),s.info("longPollingCount",String(e),{moreMessage:`av:${i.join(",")} live:${r.join(",")} code: ${o}`,eventType:29})}}reset(e){this._stopPolling(e),this._startBroadcastSequence=1,I.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:{status:s,group:{type:o}}}=n;return o===t.GRP_AVCHATROOM?s===t.JOIN_STATUS_ALREADY_IN_GROUP?n:O.handleJoinGroupResult(n.data):n})}};var y=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&&O.reset(e),n})}};var P=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&&O.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)){if(O.isOverFrequencyLimit(i)){throw{code:2996,message:`Over frequency limit: get_members-${i}`}}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 D=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=O.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=O.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 O.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 E=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,l=n.getGroup(a);if(s(l))throw new i.ChatError({functionName:t,code:u});if(l.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,ssoLog:i}=this._core,{groupID:r,duration:a=0,userIDList:u}=e;if(0===a)throw new o.ChatError({functionName:t,code:l});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),i.debug(t,`${this._name}.${t} ok, groupID:${r}`);return{code:0,data:{group:s.getGroup(r),userIDList:u}}}catch(e){throw new o.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo})}})}};var A=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:u=[]}=e||{},{successUserIDList:l,failureUserIDList:c}=this._handleResult(u,r);return{code:0,data:{successUserIDList:l,failureUserIDList:c},successLog:{message:`${this._name}.${t} ok, groupID:${s} markType:${o} enableMark:${i} success:${l.length} fail:${c.length}`}}}catch(e){throw new this._core.helper.ChatError({functionName:t,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo})}})}_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 L=new class{init(e,t){e.ssoLog.debug("AVChatRoomAction.init"),R.init(e,t),y.init(e,t),P.init(e,t),G.init(e,t),D.init(e,t),E.init(e,t),A.init(e)}};var T=new class{constructor(){this._name="LiveHandler"}init(e){this._core=e;const{helper:t,ssoLog:n}=e;t.registerExperimentalAPI("startMessageLongPolling",this),t.registerExperimentalAPI("stopMessageLongPolling",this),n.debug("LiveHandler.init")}startMessageLongPolling(e){const{common:t,utils:{isEmpty:n},OuterConstant:s,ssoLog:o}=this._core,{groupID:i,longPollingKey:r,longPollingSequence:a=1}=e;if(n(r))return o.warn("startMessageLongPolling",`${this._name}.startMessageLongPolling longPollingKey is empty.`),Promise.resolve({});O.hasPollingInstance(i)&&this.stopMessageLongPolling({groupID:i});const u=O.getJoinedLiveList(),l=t.isUnlimitedAVChatRoom();!l&&u.length>0&&this.stopMessageLongPolling({groupID:u[0].groupID}),o.debug("startMessageLongPolling",`${this._name}.startMessageLongPolling isUnlimited:${l} groupID:${i} longPollingKey:${r} longPollingSequence:${a}`);const c={groupID:i,type:s.GRP_LIVE};return O.updateLocalLiveGroup(i,c),this._getLiveHistoryMessages({groupID:i,longPollingKey:r,startSequence:a}),O.startMessageLongPolling({group:c,longPollingKey:r,startSequence:a})}stopMessageLongPolling(e){const{groupID:t}=e;return O.reset(t),this._core.ssoLog.debug("stopMessageLongPolling",`${this._name}.stopMessageLongPolling ok, groupID:${t}`),Promise.resolve({groupID:t})}_getLiveHistoryMessages(e){return n(this,void 0,void 0,function*(){const{ssoLog:t}=this._core,{groupID:s}=e;try{const o=yield function(e,t){return n(this,void 0,void 0,function*(){const{groupID:n,longPollingKey:s,startSequence:o}=e,i={GroupId:n,LongPollingKey:s,PullPreSeq:o};return t.common.buildAndSendPacket({servcmd:"group_open_http_svc.get_huge_group_msg",data:i})})}(e,this._core),{RspMsgList:i=[]}=o||{};t.debug("_getLiveHistoryMessages",`${this._name}._getLiveHistoryMessages ok, groupID:${s} count:${i.length}`),i.length>0&&O.handleLiveHistoryMessages(s,i)}catch(e){t.debug("_getLiveHistoryMessages",`${this._name}._getLiveHistoryMessages failed, groupID:${s} info:${e.message}`)}})}};return new class{constructor(){this.name="AVChatRoom"}install(t,n){this._core=t,e.init(t),O.init(t,n),L.init(t,n),T.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){O.onAVChatRoomSystemNotification(e)}_reset(){O.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)}}});