@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 28.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).TencentCloudChatTopic=t()}(this,(function(){"use strict";const e=new class{init(e){this.core=e}},t="@TOPIC#_",s=7,o=8,i=17,n=18,r=20,c=300,a=0,u=3600,p=3021,l=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],d=t=>{const{core:{common:{getMessagePreviewText:s}}}=e;return{lastTime:(null==t?void 0:t.time)||0,lastSequence:(null==t?void 0:t.sequence)||0,fromAccount:(null==t?void 0:t.from)||"",payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",messageForShow:s(null==t?void 0:t.type,null==t?void 0:t.payload),nick:(null==t?void 0:t.nick)||"",avatar:(null==t?void 0:t.avatar)||"",version:(null==t?void 0:t.version)||0,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,revoker:(null==t?void 0:t.revoker)||null}};class h{constructor(e){this.topicID="",this.topicName="",this.avatar="",this.introduction="",this.notification="",this.unreadCount=0,this.muteAllMembers=!1,this.customData="",this.groupAtInfoList=[],this.nextMessageSeq=0,this.lastMessage=d(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(const t in e)l.includes(t)&&("lastMessage"===t?this[t]=d(e[t]):"selfInfo"===t?this.updateSelfInfo(e[t]):this[t]="muteAllMembers"===t?1===e[t]:e[t])}updateUnreadCount(e=0){this.unreadCount=e}updateReadSequence(e=0,t=!1){t&&(this.selfInfo.readedSequence=e)}updateNextMessageSeq(e){this.nextMessageSeq=e}updateLastMessage(e){this.lastMessage=d(e)}updateGroupAtInfoList(t){const{core:{common:{updateGroupAtInfo:s}}}=e;s(t,this.groupAtInfoList)}updateTopic(t){const{core:{utils:{isUndefined:s},common:{deepMerge:o}}}=e;s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),o(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){const{core:{common:{deepMerge:s}}}=e;return s(this.selfInfo,t,[],[""])>0}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){var t;return e===(null===(t=this.lastMessage)||void 0===t?void 0:t.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var g=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){this._core=e;const{helper:t,constants:s,notificationCenter:o,InnerEvent:{LOGOUT:i,DESTROY:n}}=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),o.subscribeInnerEvent(i,this._reset,this),o.subscribeInnerEvent(n,this._dispose,this),console.log(`${this._name}.init`)}_hasTopic(e,t){var s;return!!(null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t))}_setTopic(e,t,s){var o;null===(o=this._topicMap.get(e))||void 0===o||o.set(t,s)}_deleteTopic(e,t){var s;null===(s=this._topicMap.get(e))||void 0===s||s.delete(t)}getTopic(e,t){var s;return null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t)}updateTopic(e){e.forEach((e=>{const{groupID:t,topicID:s}=e;let o=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?(o=this.getTopic(t,s),null==o||o.updateTopic(e)):(this._getLastMessage(e),o=new h(e),this._setTopic(t,s,o));const i=this.computeUnreadCount(o);null==o||o.updateUnreadCount(i)}))}_getLastMessage(e){const{utils:{isEmpty:t,isUndefined:s,isArray:o}}=this._core;if(!s(e.lastMsg)){let s=null,i="";o(e.lastMsg.elements)?(s=e.lastMsg.elements[0].content,i=e.lastMsg.elements[0].type):(s=e.lastMsg.elements.content,i=e.lastMsg.elements.type);const n={payload:s,type:i,time:e.lastMsg.time,sequence:e.lastMsg.sequence,from:e.lastMsg.from,nick:e.lastMsg.nick,avatar:e.lastMsg.avatar,version:e.lastMsg.messageVersion,cloudCustomData:e.lastMsg.cloudCustomData,isRevoked:2===e.lastMsg.isPlaceMessage,revoker:t(e.lastMsg.revokerInfo)?null:e.lastMsg.revokerInfo.revoker};e.lastMessage=n}}computeUnreadCount(e){const{excludedUnreadSequenceList:t=[],readedSequence:s}=e.selfInfo;let o=e.nextMessageSeq-e.selfInfo.readedSequence-1;if(t.length>0){let i=0;t.forEach((t=>{t>s&&t<=e.nextMessageSeq-1&&(i+=1)})),i>=1&&(o-=i)}return o>0?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach((e=>{const{utils:{isEmpty:t}}=this._core,{groupAtInfoList:s}=e;t(s)||this.updateGroupAtInfoList(s)}))}updateGroupAtInfoList(e,t=!1){if(0===e.length)return;const{common:{getCurrentUserID:s},utils:{isUndefined:o,isEmpty:i}}=this._core,n=s();let r=null;e.forEach((e=>{var s;const{groupID:i,topicID:c,from:a}=e;a!==n&&(r=this.getTopic(i,c),r&&r.updateGroupAtInfoList(e),o(r)&&t&&(this.updateTopic([{groupID:i,topicID:c}]),null===(s=this.getTopic(i,c))||void 0===s||s.updateGroupAtInfoList(e)))})),i(r)||this.emitTopicUpdate(r)}getTopicList(e,t=[]){const s=this._topicMap.get(e),o=[];return s&&o.push(...s.values()),t.length>0?o.filter((e=>t.includes(e.topicID))):o}deleteTopicList(e,t=[]){this._topicMap.has(e)&&(t.forEach((t=>{this._hasTopic(e,t)&&this._deleteTopic(e,t)})),console.log(`${this._name}.deleteTopicList groupID:${e} topicID:${t.join(",")}`))}clearGroupAtInfoList(e,t){const{utils:{isEmpty:s}}=this._core,o=this.getTopic(e,t);s(null==o?void 0:o.groupAtInfoList)||(null==o||o.clearGroupAtInfoList(),console.log(`${this._name}.clearGroupAtInfoList topicID:${t}`))}markMessageAsRead(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,i=(null==s?void 0:s.getLocalMessageList(`${o}${e}`))||[];if(0===i.length)return;const{length:n}=i;for(let e=n-1;e>=0;e--){const s=i[e];if(!(t&&s.sequence>t)){if("in"===s.flow&&s.isRead)break;s.setIsRead(!0)}}}updateUnreadCount(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core;let i=!1;const n=this.getTopic(e,t);if(!n)return;const r=(null==s?void 0:s.getLocalMessageList(`${o}${t}`))||[],c=n.unreadCount,a=null==r?void 0:r.filter((e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted)).length;console.log(`${this._name}._updateUnreadCount topicID:${t} currentUnreadCount:${c} newUnreadCount:${a}`),c!==a&&(n.updateUnreadCount(a),i=!0),i&&this.emitTopicUpdate(n)}updateSelfInfo(e){const{groupID:t,topicID:s,selfInfo:o}=e,i=this.getTopic(t,s);if(i){const e=i.updateSelfInfo(o);console.log(`${this._name}.updateSelfInfo topicID:${s} info:${JSON.stringify(o)} isUpdated:${e}`),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){const{OuterEvent:t,notificationCenter:s}=this._core;s.emitOuterEvent(t.TOPIC_UPDATED,{name:t.TOPIC_UPDATED,data:e})}_resetTopicRequestInfo(){this._topicRequestInfo.clear()}_reset(){this._topicMap.clear(),this._topicRequestInfo.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};const _=(t="")=>{const{core:{OuterConstant:s}}=e;return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){this._core=e;const{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:r,TOPIC_MESSAGE_DELETED:c,TOPIC_MESSAGE_REVOKED:a,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=e,{InnerEventSubType:{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}}=s;t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleNewMessage,this),s.subscribeInnerEvent(c,this._handleMessageDeleted,this),s.subscribeInnerEvent(a,this._handleMessageRevoked,this),s.subscribeInnerEvent(u,this._handleMessageModified,this),s.subscribeInnerEvent(n,l,this._handleTopicAtTips,this),s.subscribeInnerEvent(n,d,this._handleLatestMessage,this),s.subscribeInnerEvent(p,this._dispose,this),console.log(`${this._name}.init`)}_handleUnreadSyncFinished(e){var t;const{constants:{WORKFLOW_STEP:s}}=this._core,{groupTipList:o=[]}=e.result[s.UNREAD_MESSAGE_SYNC]||{};for(let e=0;e<o.length;e++){const s=o[e];12===s.Event&&(null===(t=s.GroupTips[0].MsgBody)||void 0===t?void 0:t.TopicId)&&this._handleTopicAtTips(s)}}_handleNewMessage(e){const{conversationUpdateFieldList:t=[]}=e.result||{};if(0===t.length)return;const{common:{isTopic:s}}=this._core;if(!s(t[0].conversationID))return;const{common:{getCurrentUserID:o}}=this._core;t.forEach((e=>{const{conversationID:t="",lastMessage:s}=e,i=_(t),n=m(i),r=g.getTopic(n,i);if(r){const{from:e,sequence:t}=s,i=t+1,n=e===o();if(i>r.nextMessageSeq){r.updateNextMessageSeq(i),r.updateLastMessage(s),r.updateReadSequence(t,n);const e=n?0:g.computeUnreadCount(r);r.updateUnreadCount(e),this._emitTopicUpdate(r)}}}))}_handleMessageDeleted(e){const t=_(e),s=m(t),o=g.getTopic(s,t);if(console.log(`${this._name}._handleMessageDeleted, topicID:${t} hasTopic:${!!o}`),!o)return;const{message:{messageDataHandler:i},utils:{isEmpty:n}}=this._core,r=(null==i?void 0:i.getLocalMessageList(e))||[];let c={};for(let e=(r.length||0)-1;e>=0;e--)if(!r[e].isDeleted){c=r[e];break}let a=!1;o.lastMessage.lastSequence!==c.sequence&&(n(c)&&(c={}),o.updateLastMessage(c),a=!0),a&&(this._emitTopicUpdate(o),console.log(`${this._name}._handleMessageDeleted, topicID:${t} with lastMessage:${o.lastMessage}`))}_handleMessageRevoked(e){const{messageList:t=[]}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach((e=>{const{to:t,sequence:i,revoker:n}=e,r=m(t);if(s=g.getTopic(r,t),s){s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked(i)&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(n),o=!0);const{excludedUnreadSequenceList:e=[]}=s.selfInfo;e.push(i),s.updateSelfInfo({excludedUnreadSequenceList:e})}})),o&&this._emitTopicUpdate(s)}_handleMessageModified(e){const{conversationID:t,messageList:s}=e,o=_(t),i=m(o),n=g.getTopic(i,o);if(console.log(`${this._name}._handleMessageModified topicID:${o} hasTopic:${!!n}`),!n)return;const{lastMessage:r}=n;if(r){const e=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(r,e)){const{common:{getMessagePreviewText:t}}=this._core,{time:s,sequence:o,elements:i,cloudCustomData:c,messageVersion:a}=e;r.type=i[0].type,r.payload=i[0].content,r.messageForShow=t(r.type,r.payload),r.cloudCustomData=c,r.version=a,r.lastSequence=o,r.lastTime=s,this._emitTopicUpdate(n)}}}_shouldUpdateLastMessage(e,t){if(null===e.payload)return!0;const{sequence:s,messageVersion:o}=t;return e.lastSequence===s&&e.version!==o}_emitTopicUpdate(e){const{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){const{GroupTips:t=[]}=e,s=[];let o=!1;t.forEach((e=>{const{GroupAtTips:t,MsgBody:i,MsgRandom:n,ClientSeq:r}=e;let c={};t?c=this._convertAtInfo(t):i&&(c=this._convertAtInfo(i),o=!0),c.__random=n,c.__sequence=r,s.push(c)})),g.updateGroupAtInfoList(s,o)}_convertAtInfo(e){const{From_Account:t,GroupId:s,TopicId:o,MsgSeq:i,GroupAtType:n}=e;return{groupID:s,topicID:o,from:t,sequence:i,groupAtType:n}}_handleLatestMessage(e){const{utils:{isEmpty:t},message:{messageHelper:s,messageFactory:o}}=this._core,{NewMsg:i,NoUnreadSeqList:n}=e||{};if(!t(i)){const{GroupInfo:{TopicId:e}}=i,t=s.parseServerGroupMessage(i),r=o.createMessage(t);r.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:r,excludedUnreadSequenceList:n})}}_updateUnreadCountAndLastMessage(e){const{utils:{isUndefined:t}}=this._core,{topicID:s,message:o,excludedUnreadSequenceList:i}=e,n=m(s),r=g.getTopic(n,s);if(r){let e=r.selfInfo.excludedUnreadSequenceList||[];if(t(i)||(e=i),o._isExcludedFromUnreadCount&&e.push(o.sequence),r.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${o.sequence} lastMessage sequence:${r.lastMessage.lastSequence}`),o.sequence>r.lastMessage.lastSequence){const e=o.sequence+1;r.updateLastMessage(o),r.updateNextMessageSeq(e);const t=g.computeUnreadCount(r);r.updateUnreadCount(t),this._emitTopicUpdate(r)}}}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:o,TOPIC_MESSAGE_REVOKED:i,TOPIC_MESSAGE_MODIFIED:n,DESTROY:r}}=this._core,{InnerEventSubType:{TOPIC_AT_TIPS:c,TOPIC_LATEST_MESSAGE:a}}=e;e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(o,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(i,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,c,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,a,this._handleLatestMessage,this),e.unSubscribeInnerEvent(r,this._dispose,this)}};var f=new class{constructor(){this._name="TopicOperationHandler"}init(e){this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:o}}=e,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:r}}=t;t.subscribeInnerEvent(s,i,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,r,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(o,this._dispose,this),console.log(`${this._name}.init`)}_handleTopicTips(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){const{common:{getCurrentUserID:t}}=this._core,{GroupInfo:{GroupId:s,TopicId:o},MsgBody:{MsgMemberInfo:i=[]}}=e;for(let e=0;e<i.length;e++){const{User_Account:n,ShutupTime:r}=i[e];if(n===t()&&r>=0){g.updateSelfInfo({groupID:s,topicID:o,selfInfo:{muteTime:r}});break}}}_handleTopicProfileUpdated(e){const{GroupInfo:{GroupId:t,TopicId:s},MsgBody:{MsgTopicNewInfo:{CustomString:o,ShutupAll:i,TopicFaceUrl:n,TopicIntroduction:r,TopicName:c,TopicNotification:a}}}=e,u=g.getTopic(t,s);u&&(u.updateTopic({groupID:t,topicID:s,customData:o,muteAllMembers:i,avatar:n,introduction:r,topicName:c,notification:a}),g.emitTopicUpdate(u))}_handleTopicNotices(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const s=t[e],{MsgBody:{ReportType:o,TopicIdArray:c,MsgFlag:a},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:c}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:a}):this._emitTopicNotices(s)}}_handleTopicCreated(e){const{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){const{OuterEvent:t,notificationCenter:s}=this._core,{groupID:o,topicIDList:i=[]}=e;g.deleteTopicList(o,i),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){const{groupID:t,topicID:s,messageRemindType:o}=e;g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{messageRemindType:o}})}_handleTopicMessageReadSynced(e){const{GroupTips:t}=e,{utils:{isUndefined:s}}=this._core;t.forEach((e=>{const{GroupReadInfoArray:t}=e.MsgBody;s(t)||t.forEach((e=>{const{GroupId:t,TopicId:s,LastReadMsgSeq:o}=e;console.log(`${this._name}._handleTopicMessageReadSynced topicID:${s} lastReadSequence:${o}`),g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{readedSequence:o}}),g.clearGroupAtInfoList(t,s),g.markMessageAsRead(s,o),g.updateUnreadCount(t,s)}))}))}_emitTopicTips(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_TIP;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_GROUP;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_emitTopicNotices(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_SYS_NOTICE;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_SYSTEM,r.conversationID=o.CONV_SYSTEM;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:i,TOPIC_MESSAGE_READ_SYNC:n}}=e;e.unSubscribeInnerEvent(t,o,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,i,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function T(e,t,s,o){return new(s||(s=Promise))((function(i,n){function r(e){try{a(o.next(e))}catch(e){n(e)}}function c(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,c)}a((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return T(this,void 0,void 0,(function*(){const{apiMap:{getGroupList:e}}=this._core,t=(yield e(!0)).filter((e=>!0===e.isSupportTopic));return console.log(`${this._name}.getJoinedCommunityList ok, count:${t.length}`),{code:0,data:{groupList:t}}}))}};var v=new class{constructor(){this._name="CreateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(e){return T(this,void 0,void 0,(function*(){const t="createTopicInCommunity",{utils:{isUndefined:s},common:{isTopic:o},helper:{ChatError:i}}=this._core,{topicID:n}=e;if(!s(n)&&!o(n)){const e=new i({functionName:t,code:p});throw console.error(`${this._name}.${t} fail:`,e),e}try{const s=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a}=e,u={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:u})}))}(e,this._core),{TopicId:o=""}=s||{};return console.log(`${this._name}.${t} ok, topicID:${o}`),g.updateTopic([Object.assign(Object.assign({},e),{topicID:o})]),{code:0,data:{topicID:o}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(e){return T(this,void 0,void 0,(function*(){const t="deleteTopicFromCommunity",{groupID:s,topicIDList:o=[]}=e;try{const i=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:i})}))}({groupID:s,topicIDList:o},this._core),{DestroyResultItem:n=[]}=i||{},r=[],c=[],a=[];return n.forEach((e=>{const{TopicId:t,ErrorCode:s,ErrorInfo:o}=e;0===s?(r.push({topicID:t}),a.push(t)):c.push({topicID:t,code:s,message:o})})),a.length>0&&g.deleteTopicList(e.groupID,a),console.log(`${this._name}.${t} ok, groupID:${s} success:${r.length} fail:${c.length}`),{code:0,data:{successTopicList:r,failureTopicList:c}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{const{store:t}=this._core;return(t.get("cloudConfig")||{})[e]}}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return T(this,void 0,void 0,(function*(){const{groupID:t,topicIDList:s=[]}=e,o=0===s.length;if(this._isFetchingFromCache(t,o)){const e=g.getTopicList(t,s);if(o||e.length===s.length)return console.log(`${this._name}.getTopicList groupID:${t} from cache, count:${e.length}`),{code:0,data:{successTopicList:e,failureTopicList:[]}}}return this._getTopicListFromServer({groupID:t,topicIDList:s},o)}))}_isFetchingFromCache(e,t){const{hasFetchedAllTopics:s=!1,time:o=0}=g.getTopicRequestInfo(e);return!(!s&&(t||s))&&Date.now()-o<this._getCacheDuration()}_getCacheDuration(){const{utils:{isUndefined:e}}=this._core;let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10),t>=a&&t<=u)?1e3*t:1e3*c}_getTopicListFromServer(e,t){return T(this,void 0,void 0,(function*(){const s="_getTopicListFromServer",{utils:{isEmpty:o}}=this._core,{groupID:i}=e;try{const n=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o,MemberInfoFilter:["NoUnreadSeqList"]};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:i})}))}(e,this._core),{TopicAndSelfInfo:r=[]}=n||{},c=[],a=[],u=[];this._convertTopicInfoList(r).forEach((e=>{const{topic:t,selfInfo:s,code:o,message:i}=e,{topicID:n}=t;0===o?(c.push(Object.assign(Object.assign({},t),{selfInfo:s})),a.push(n)):u.push({topicID:n,code:o,message:i})})),g.updateTopic(c),g.updateTopicAtInfo(c),console.log(`${this._name}.${s} groupID:${i} from remote, success count:${a.length}, fail count:${u.length}`);let p=[];return o(a)||(g.setTopicRequestInfo(i,{hasFetchedAllTopics:t,time:Date.now()}),p=g.getTopicList(i,a)),{code:0,data:{successTopicList:p,failureTopicList:u}}}catch(e){throw console.error(`${this._name}.${s} failed, error:`,e),e}}))}_convertTopicInfoList(e){const t=[];return e.forEach((e=>{const{TopicInfo:s={},SelfInfo:o={},ErrorCode:i,ErrorInfo:n}=e;t.push({code:i,message:n,topic:this._convertTopicInfo(s),selfInfo:this._convertSelfInfo(o)})})),t}_convertTopicInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{CreateTime:s,CustomString:o,FaceUrl:i,GroupId:n,LastMsgTime:r,NextMsgSeq:c,Introduction:a,Notification:u,ShutUpAllFlag:p,TopicId:l,TopicName:d,LastMsg:h,AtInfoList:g}=e;return{createTime:s,customData:o,avatar:i,groupID:n,lastMessageTime:r,nextMessageSeq:c,introduction:a,notification:u,muteAllMembers:p,topicID:l,topicName:d,lastMsg:this._convertLastMsg(h),groupAtInfoList:this._convertAtInfoList({groupID:n,topicID:l,atInfoList:g})}}_convertLastMsg(e){const{utils:{isUndefined:t},OuterConstant:s}=this._core;if(t(e))return;4===e.Event&&(e.MsgBody.MsgType=s.MSG_GRP_TIP);const{message:{messageHelper:o}}=this._core,i=o.parseServerGroupMessage(e),{From_AccountHeadurl:n,From_AccountNick:r,LastMsgSeq:c}=e;return Object.assign(Object.assign({},i),{avatar:n,nick:r,sequence:c})}_convertAtInfoList(e){const{groupID:t,topicID:s,atInfoList:o=[]}=e;if(0===o.length)return[];const i=[];return o.forEach((e=>{const{AtFlagList:o,AtMsgSeq:n,From_Account:r}=e;i.push({groupID:t,topicID:s,groupAtType:o,sequence:n,from:r})})),i}_convertSelfInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{InheritGroupReceiveOption:s,MsgFlag:o,MsgSeq:i,NoUnreadSeqList:n,ShutUpTime:r}=e;return{inheritGroupReceiveOption:s,messageRemindType:o,readedSequence:i,excludedUnreadSequenceList:n,muteTime:r}}};var D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(e){return T(this,void 0,void 0,(function*(){const t="updateTopicProfile",{utils:{isUndefined:s}}=this._core,{groupID:o,topicID:i,muteAllMembers:n}=e;s(e.muteAllMembers)||(e.muteAllMembers=!0===n?"On":"Off");try{return yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a,muteAllMembers:u}=e,p={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a,ShutUpAllMember:u};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:p})}))}(e,this._core),console.log(`${this._name}.${t} ok, topicID:${i}`),g.updateTopic([e]),{code:0,data:{topic:g.getTopic(o,i)}}}catch(e){throw console.error(`${this._name}.${t} failed, topicID:${i} error:`,e),e}}))}};var C=new class{constructor(){this._name="MessageReadHandler"}init(e){this._core=e;const{helper:t,common:{isTopic:s}}=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(e){return T(this,void 0,void 0,(function*(){const{utils:{isEmpty:t},ssoLog:s,helper:{ChatError:o}}=this._core,{conversationID:i}=e,n={code:0,data:{}},r=_(i),c=m(r),a=g.getTopic(c,r);if(console.log(`${this._name}.setMessageRead topicID:${r} unreadCount:${(null==a?void 0:a.unreadCount)||0}`),!a)return n;if(t(a.groupAtInfoList)||this._deleteTopicAtTips(c,r),0===a.unreadCount)return n;const u=this._getLocalMessageMaxSequence(a),p=s.createSSOLogData({method:"setMessageRead"});p.setLogInfo({message:`conversationID:${i}`,moreMessage:`lastMessageSequence:${u}`});try{yield this._setTopicMessageRead(c,r,u),p.end()}catch(e){const{errorCode:t,errorInfo:s}=e,i=new o({functionName:"setMessageRead",code:t,message:s});throw p.setError(i).end(),i}return n}))}_deleteTopicAtTips(e,t){return T(this,void 0,void 0,(function*(){console.log(`${this._name}._deleteTopicAtTips groupID:${e} topicID:${t}`);const s=g.getTopic(e,t);if(!s)return;const o=(null==s?void 0:s.groupAtInfoList)||[];if(0!==o.length)try{const{common:{getCurrentUserID:s},OuterConstant:{CONV_AT_ALL:i}}=this._core;let n=[...o];if(n=o.filter((e=>!e.atTypeArray.includes(i))),0===n.length)return void g.clearGroupAtInfoList(e,t);const r=n.map((e=>({From_Account:e.from,To_Account:s(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID})));yield function(e,t){return T(this,void 0,void 0,(function*(){const{messageListToDelete:s}=e,o={DelMsgList:s};return t.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:o})}))}({messageListToDelete:r},this._core),console.log(`${this._name}._deleteTopicAtTips ok. count:${o.length}`),g.clearGroupAtInfoList(e,t)}catch(e){console.error(`${this._name}._deleteTopicAtTips fail:`,e)}}))}_getLocalMessageMaxSequence(e){var t;const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,{topicID:i}=e,n=s.getLocalMessageList(`${o}${i}`),r=Math.max(...n.map((e=>e.sequence)));let c=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return r>c&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${c} to ${r}`),c=r),c}_setTopicMessageRead(e,t,s){return T(this,void 0,void 0,(function*(){try{yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,lastMessageSequence:i}=e,n={GroupId:s,TopicId:o,MsgReadedSeq:i};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:n})}))}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(`${this._name}._setTopicMessageRead ok, lastReadSequence:${s}`),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)}catch(e){throw console.error(`${this._name}._setTopicMessageRead fail:`,e),e}}))}};var A=new class{init(e){console.log("TopicAction.init"),M.init(e),v.init(e),E.init(e),S.init(e),D.init(e),C.init(e)}};const L={required:!0,rules:["string"],allowEmpty:!1},y={createTopicInCommunity:{groupID:L,topicName:L},deleteTopicFromCommunity:{groupID:L,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:L,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:L,topicID:L}},O={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};return new class{constructor(){this.name="Topic"}install(t){e.init(t),g.init(t),I.init(t),f.init(t),A.init(t),t.helper.registerValidateConfig({auth:O,params:y})}}}));