@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 49.8 kB
JavaScript
const e=new class{init(e){this.core=e}};function t(e,t,n,s){return new(n||(n=Promise))((function(o,i){function r(e){try{c(s.next(e))}catch(e){i(e)}}function a(e){try{c(s.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}c((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const n=t=>{const{core:{OuterConstant:n,utils:{isString:s}}}=e;return s(t)&&t.slice(0,3)===n.CONV_C2C},s=t=>{const{core:{OuterConstant:n,utils:{isString:s}}}=e;return s(t)&&t.slice(0,5)===n.CONV_GROUP},o=t=>{const{core:{OuterConstant:n,utils:{isString:s}}}=e;return s(t)&&t===n.CONV_SYSTEM},i=(t,n)=>{const{core:{utils:{isUndefined:s}}}=e;if(s(n))return"";const{core:{OuterConstant:o}}=e;switch(t){case o.MSG_TEXT:return n.text||"";case o.MSG_IMAGE:return"[Image]";case o.MSG_LOCATION:return"[Location]";case o.MSG_AUDIO:return"[Voice]";case o.MSG_VIDEO:return"[Video]";case o.MSG_FILE:return"[File]";case o.MSG_CUSTOM:return"[Custom Messages]";case o.MSG_GRP_TIP:return"[Group Notification]";case o.MSG_GRP_SYS_NOTICE:return"[Group System Message]";case o.MSG_FACE:return"[Animated Sticker]";case o.MSG_MERGER:return"[Chat Record]";default:return""}},r=e=>({lastTime:(null==e?void 0:e.time)||(null==e?void 0:e.lastTime)||0,lastSequence:(null==e?void 0:e.sequence)||(null==e?void 0:e.lastSequence)||0,fromAccount:(null==e?void 0:e.from)||(null==e?void 0:e.fromAccount)||"",messageForShow:i(null==e?void 0:e.type,null==e?void 0:e.payload),payload:(null==e?void 0:e.payload)||null,type:(null==e?void 0:e.type)||"",isRevoked:(null==e?void 0:e.isRevoked)||!1,cloudCustomData:(null==e?void 0:e.cloudCustomData)||"",onlineOnlyFlag:(null==e?void 0:e._onlineOnlyFlag)||!1,nick:(null==e?void 0:e.nick)||"",nameCard:(null==e?void 0:e.nameCard)||"",version:(null==e?void 0:e.version)||0,isPeerRead:(null==e?void 0:e.isPeerRead)||!1,revoker:(null==e?void 0:e.revoker)||null});function a(t){const{core:{utils:{isString:n}}}=e,s=[];if(!n(t))return s;const o=t.length;if(0===o)return s;for(let e=o-1;e>=0;e--)"1"===t[e]&&s.push(2**(o-e-1));return s}function c(t){const{OuterConstant:n}=e.core;let s="";return 0===t?s=n.MSG_REMIND_ACPT_AND_NOTE:1===t?s=n.MSG_REMIND_DISCARD:2===t?s=n.MSG_REMIND_ACPT_NOT_NOTE:3===t&&(s=n.NOT_RECEIVE_OFFLINE_PUSH_EXCEPT_AT),s}function l(t){const{OuterConstant:n}=e.core;let s;return t.startsWith(n.CONV_C2C)&&(s=t.replace(n.CONV_C2C,"")),"@TLS#ERROR"===s||"@TLS#NOT_FOUND"===s}var u,d,_,p,C;!function(e){e.NOT_START="notStart",e.PENDING="pending",e.RESOLVED="resolved",e.REJECTED="rejected"}(u||(u={})),function(e){e[e.C2C=1]="C2C",e[e.GROUP=2]="GROUP"}(d||(d={})),function(e){e[e.C2C=8]="C2C",e[e.GROUP=2]="GROUP"}(_||(_={})),function(e){e[e.ONLY_CONVERSATIONID=1]="ONLY_CONVERSATIONID"}(p||(p={})),function(e){e[e.CONV_DELETED=1]="CONV_DELETED",e[e.CONV_PINED=2]="CONV_PINED",e[e.CONV_UNPINED=3]="CONV_UNPINED",e[e.CONV_MARK_UPDATED=4]="CONV_MARK_UPDATED",e[e.CONV_GROUP_ADDED=5]="CONV_GROUP_ADDED",e[e.CONV_GROUP_DELETED=6]="CONV_GROUP_DELETED",e[e.CONV_GROUP_UPDATED=7]="CONV_GROUP_UPDATED"}(C||(C={}));var h;!function(e){e[e.CONV_NOT_FOUND=2500]="CONV_NOT_FOUND",e[e.USER_OR_GRP_NOT_FOUND=2501]="USER_OR_GRP_NOT_FOUND",e[e.CONV_UN_RECORDED_TYPE=2502]="CONV_UN_RECORDED_TYPE"}(h||(h={}));class g{constructor(e){this.conversationID=e.conversationID||"",this.unreadCount=e.unreadCount||0,this.type=e.type||"",this.lastMessage=r(e.lastMessage),this.peerReadTime=e.peerReadTime||0,this.groupAtInfoList=[],this.remark=e.remark||"",this.isPinned=e.isPinned||!1,this.messageRemindType=e.messageRemindType,this.markList=e.markList||[],this.customData=e.customData||"",this.conversationGroupList=e.conversationGroupList||[],this.draftText=e.draftText||"",this.userProfile=e.userProfile,this.groupProfile=e.groupProfile,this.subType=e.subType||"",this._isInfoCompleted=!1,this._init()}_init(){const{core:{OuterConstant:t,utils:{isUndefined:n}}}=e;n(this.userProfile)&&this.type===t.CONV_C2C?this.userProfile={}:n(this.groupProfile)&&this.type===t.CONV_GROUP&&(this.groupProfile={}),this.subType&&this.type===t.CONV_GROUP&&(this.subType=this.groupProfile.type||"")}updateUnreadCount(t){const{core:{OuterConstant:n,utils:{isUndefined:s}}}=e,{nextUnreadCount:o,isFromGetConversations:i,isUnreadC2CMessage:r}=t;s(o)||(this.subType!==n.GRP_AVCHATROOM?i&&this.type===n.CONV_GROUP||i&&this.type===n.CONV_TOPIC||r&&this.type===n.CONV_C2C?this.unreadCount=o:this.unreadCount+=o:this.unreadCount=0)}updateLastMessage(e){this.lastMessage=r(e)}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(t){const{core:{OuterConstant:n}}=e,{sequence:s,time:o}=t;return this.type===n.CONV_C2C&&s===this.lastMessage.lastSequence&&o===this.lastMessage.lastTime||this.type===n.CONV_GROUP&&s===this.lastMessage.lastSequence}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}setDraftText(e){this.draftText=e}updateGroupAtInfoList(t){const{core:{OuterConstant:n}}=e;let[...s]=t.groupAtType;s.includes(n.CONV_AT_ME)||s.includes(n.CONV_AT_ALL)||(s=[n.CONV_AT_ALL_AT_ME]);const o={from:t.from,groupID:t.groupID,messageSequence:t.sequence,atTypeArray:s,__random:t.__random,__sequence:t.__sequence};this.groupAtInfoList.push(o)}clearGroupAtInfoList(){this.groupAtInfoList.length=0}getProfileCompleted(){return this._isInfoCompleted}setProfileCompleted(){this._isInfoCompleted=!0}}var v=new class{constructor(){this._name="ConversationDataHandler",this._totalUnreadCount=0,this._groupAtTipsList=[]}init(e){this._core=e;const{helper:t,notificationCenter:n,appStore:{conversationStore:s},constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:r,NEW_MESSAGE:a,MESSAGE_DELETED:c,MESSAGE_REVOKED:l,MESSAGE_MODIFIED:u,CONVERSATION_UPDATED:d,LOGOUT:_,DESTROY:p}}=e;this._conversationStore=s,t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.CONVERSATION_UPDATE_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.CONVERSATION_UPDATE_AFTER_GROUP_LIST_SYNC_FINISHED,this._handleGroupListSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_C2C_NEW_MESSAGE,i.CREATE_OR_UPDATE_CONVERSATION_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),t.registerWorkflowStep(o.RECEIVE_C2C_NEW_MESSAGE,i.CONVERSATION_UPDATE_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.CREATE_OR_UPDATE_CONVERSATION_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_RE_ONLINE,i.CONVERSATION_UPDATE_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this);const{InnerEventSubType:{GROUP_AT_TIPS:C}}=n;n.subscribeInnerEvent(a,this._handleNewMessage,this),n.subscribeInnerEvent(r,C,this._handleNewGroupAtTips,this),n.subscribeInnerEvent(c,this._handleMessageDeleted,this),n.subscribeInnerEvent(l,this._handleMessageRevoked,this),n.subscribeInnerEvent(u,this._handleMessageModified,this),n.subscribeInnerEvent(d,this._handleConversationUpdated,this),n.subscribeInnerEvent(_,this._reset,this),n.subscribeInnerEvent(p,this._dispose,this),console.log(`${this._name}.init`)}_handleUnreadSyncFinished(e){const{constants:{WORKFLOW_STEP:t}}=this._core,{conversationUpdateFieldList:n=[],groupTipList:s=[],isUnreadC2CMessage:o}=e.result[t.UNREAD_MESSAGE_SYNC]||{};let i=!1;n.forEach((e=>{const{conversationID:t,unreadCount:n}=e,s=this.getLocalConversation(t);s&&s.unreadCount!==n&&(s.updateUnreadCount({nextUnreadCount:n,isUnreadC2CMessage:o}),i=!0)})),i&&this.emitConversationListUpdate(),this._handleGroupAtTipsSynced(s)}_handleGroupAtTipsSynced(e){for(let t=0;t<e.length;t++)12===e[t].Event&&this._handleNewGroupAtTips(e[t])}_handleGroupListSyncFinished(e){const{constants:{WORKFLOW_STEP:t}}=this._core,n=e.result[t.GROUP_LIST_SYNC]||[],{OuterConstant:{CONV_GROUP:s}}=this._core;let o=!1;for(let e=0;e<n.length;e++){const t=n[e],i=`${s}${t.groupID}`;if(!this.hasLocalConversation(i))continue;const r=this.getLocalConversation(i);r.groupProfile=JSON.parse(JSON.stringify(t)),r.lastMessage.lastSequence<t.nextMessageSeq&&(r.lastMessage.lastSequence=t.nextMessageSeq-1),r.subType||(r.subType=t.type),o=!0}o&&this.emitConversationListUpdate()}_handleNewMessage(e){const{conversationUpdateFieldList:t=[],isInstantMessage:n=!0,isUnreadC2CMessage:s=!1,updateUnreadCount:o=!0}=e.result||{};if(console.log(`${this._name}._handleNewMessage, conversationUpdateFieldList length:`,t.length,e),0===t.length)return;this.updateLocalConversationList({conversationUpdateFieldList:t,isInstantMessage:n,isUnreadC2CMessage:s,isFromGetConversations:!1,updateUnreadCount:o});t.filter((e=>this._isConversationNeedShow(e.conversationID))).length>0&&this.emitConversationListUpdate()}_handleNewGroupAtTips(e){const{GroupTips:t=[]}=e;t.forEach((e=>{const{GroupAtTips:t,MsgBody:n,GroupAtType:s,MsgRandom:o,ClientSeq:i}=e;let r={};t?r=this._convertGroupAtTipsKey(t):n?r=Object.assign(Object.assign({},this._convertGroupAtTipsKey(n)),{sync:!0}):s&&(r=Object.assign(Object.assign({},this._convertGroupAtTipsKey(e)),{sync:!0})),r.__random=o,r.__sequence=i,this._groupAtTipsList.push(r)})),console.log(`${this._name}._handleNewGroupAtTips groupAtTipsList: ${JSON.stringify(this._groupAtTipsList)}`),this._updateGroupAtInfoList()}_convertGroupAtTipsKey(e){const{From_Account:t,GroupId:n,MsgSeq:s,GroupAtType:o}=e;return{from:t,groupID:n,sequence:s,groupAtType:o}}_updateGroupAtInfoList(){if(0===this._groupAtTipsList.length)return;const{common:e,OuterConstant:t}=this._core,n=e.getCurrentUserID();let s=!1;this._groupAtTipsList.forEach((e=>{const{groupID:o,from:i}=e;if(i!==n){const n=this.getLocalConversation(`${t.CONV_GROUP}${o}`);n&&(n.updateGroupAtInfoList(e),s=!0)}})),s&&this.emitConversationListUpdate(),this._groupAtTipsList.length=0}_handleMessageDeleted(e){var t,n;console.log(`${this._name}._handleMessageDeleted, conversationID:`,e);const{message:{messageDataHandler:s},OuterConstant:o}=this._core,i=(null==s?void 0:s.getLocalMessageList(e))||[];let r={};for(let e=(i.length||0)-1;e>=0;e--)if(!i[e].isDeleted){r=i[e];break}const a=this.getLocalConversation(e);if(!a)return;let c=!1;a.lastMessage.lastSequence===r.sequence&&a.lastMessage.lastTime===r.time||((null===(n=null===(t=this._core)||void 0===t?void 0:t.helper)||void 0===n?void 0:n.isEmpty(r))&&(r=void 0),a.updateLastMessage(r),c=!0),e.startsWith(o.CONV_C2C)&&this.updateUnreadCount(e),c&&(this.emitConversationListUpdate(),console.log(`${this._name}._handleMessageDeleted. update conversationID:${e} with lastMessage:`,a.lastMessage))}_handleMessageRevoked(e){const{messageList:t=[],updateUnreadCount:n=!0}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach((e=>{s=this.getLocalConversation(e.conversationID),s&&(n&&s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked({sequence:e.sequence,time:e.time})&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(e.revoker),o=!0))})),o&&this.emitConversationListUpdate()}_handleMessageModified(e){console.log(`${this._name}._handleMessageModified`,JSON.stringify(e));const{conversationID:t,messageList:n}=e,s=this.getLocalConversation(t),{utils:{isEmpty:o}}=this._core;if(o(s))return;const{lastMessage:r}=s;if(r){const e=(null==n?void 0:n[0])||{};r.lastTime===e.time&&r.lastSequence===e.sequence&&r.version!==e.version&&(r.type=e.type,r.payload=e.payload,r.messageForShow=i(e.type,e.payload),r.cloudCustomData=e.cloudCustomData,r.version=e.version,this.emitConversationListUpdate(),console.log(`${this._name} conversationID:${t} lastMessage updated`))}}_handleConversationUpdated(){this.emitConversationListUpdate()}updateLocalConversationList(e){const{isFromGetConversations:t}=e,{newConversationList:n}=this._getTmpConversationListMapping(e);this._sortConversationList(),t||this._updateNewConversationProfile(n),console.log(`${this._name}.updateLocalConversationList newConversationList: ${n.length}`)}_getTmpConversationListMapping(e){const{OuterConstant:t}=this._core,{conversationUpdateFieldList:n,isFromGetConversations:s,isInstantMessage:o,isUnreadC2CMessage:i=!1,updateUnreadCount:r}=e,a=[],c=null==n?void 0:n.length;for(let e=0;e<c;e++){const c=new g(n[e]),{conversationID:u,type:d}=c;if(!l(u))if(this.hasLocalConversation(u)){const a=this.getLocalConversation(u);if(s){this.setLocalConversation(u,c),d===t.CONV_C2C?c.unreadCount=(null==a?void 0:a.unreadCount)||0:d===t.CONV_GROUP&&(c.groupProfile=JSON.parse(JSON.stringify(null==a?void 0:a.groupProfile)));continue}this._mergeConversationInfo({conversationID:u,tmpConversation:c,lastMessage:n[e].lastMessage,isFromGetConversations:s,isInstantMessage:o}),this._patchLastMessage({conversationID:u,lastMessage:n[e].lastMessage,isInstantMessage:o}),!0===r&&(null==a||a.updateUnreadCount({nextUnreadCount:c.unreadCount,isFromGetConversations:s,isUnreadC2CMessage:i}))}else a.push(c),this.setLocalConversation(u,c)}return{newConversationList:a}}_mergeConversationInfo(e){const{conversationID:t,tmpConversation:n,lastMessage:s,isFromGetConversations:o,isInstantMessage:i}=e,r=this.getLocalConversation(t),a=["unreadCount","allowType","adminForbidType","payload"];!1===i&&a.push("lastMessage"),o&&a.push("isPinned");const{utils:{isUndefined:c,isEmpty:l},common:{deepMerge:u}}=this._core;c(i)&&s&&null===(null==r?void 0:r.lastMessage.payload)&&(r.lastMessage.payload=s.payload),l(null==r?void 0:r.lastMessage.revoker)||(r.lastMessage.revoker=null);u(r,n,a,[null,void 0,"",0,NaN])}_patchLastMessage(e){const{OuterConstant:t}=this._core,{conversationID:n,lastMessage:s,isInstantMessage:o}=e,i=this.getLocalConversation(n);if(i&&s){const{payload:e,nameCard:n,nick:r,cloudCustomData:a=""}=s;o&&(e&&(i.lastMessage.payload=e),i.type===t.CONV_GROUP&&(i.lastMessage.nameCard=n,i.lastMessage.nick=r)),i.lastMessage.cloudCustomData!==a&&(i.lastMessage.cloudCustomData=a)}}_sortConversationList(){const{utils:{isEmpty:e}}=this._core,t=[...this._conversationStore.getConversationMap()],n=[],s=[],o=[],i=[];t.forEach((t=>{!0===t[1].isPinned?e(t[1].lastMessage.lastTime)?s.push(t):n.push(t):e(t[1].lastMessage.lastTime)?i.push(t):o.push(t)}));const r=n.sort(((e,t)=>t[1].lastMessage.lastTime-e[1].lastMessage.lastTime)).concat(s).concat(o.sort(((e,t)=>t[1].lastMessage.lastTime-e[1].lastMessage.lastTime))).concat(i);this._updateConversationMapFromList(r)}_updateNewConversationProfile(e){if(0===e.length)return;const t=[],n=[],{OuterConstant:{CONV_GROUP:s,CONV_C2C:o}}=this._core;e.forEach((e=>{const{conversationID:i,type:r}=e;if(r===o){const e=i.replace(o,"");t.push(e)}else if(r===s){const e=i.replace(s,"");n.push(e)}})),t.length>0&&this._updateC2CConversation(t),n.length>0&&this._updateGroupConversation(n)}_updateC2CConversation(e){var t;const{OuterConstant:{CONV_C2C:n},appStore:{userStore:s},user:o}=this._core;let i=!1;null===(t=o.userProfile)||void 0===t||t.getUserProfile({userIDList:e}).then((e=>{((null==e?void 0:e.data)||[]).forEach((e=>{var t;const{userID:o}=e,r=this.getLocalConversation(`${n}${o}`);if(r){const n=(null===(t=s.getFriend(o))||void 0===t?void 0:t.remark)||"";r.remark=n,r.userProfile=e,i=!0}})),i&&this.emitConversationListUpdate()})).catch((e=>{}))}_updateGroupConversation(e){const{OuterConstant:{CONV_GROUP:t},appStore:{groupStore:n}}=this._core;let s=!1;e.forEach((e=>{const o=n.getGroup(e),i=this.getLocalConversation(`${t}${e}`);o&&i&&(i.groupProfile=o,s=!0)})),s&&this.emitConversationListUpdate()}_isConversationNeedShow(e){const t=this.getLocalConversation(e);if(this._core.utils.isUndefined(t))return!0;const{OuterConstant:n}=this._core,s=t.type===n.CONV_TOPIC,o=t.type===n.CONV_GROUP&&t.subType===n.GRP_ROOM,i=t.type===n.CONV_GROUP&&t.subType===n.GRP_LIVE;return!(s||o||i)}updateUnreadCount(e,t=!0){var n,s;let o=!1;const i=this.getLocalConversation(e),r=null===(s=null===(n=this._core)||void 0===n?void 0:n.message.messageDataHandler)||void 0===s?void 0:s.getLocalMessageList(e);if(!i)return o;const a=i.unreadCount,c=null==r?void 0:r.filter((e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted)).length;return console.log(`${this._name}._updateUnreadCount conversationID:${e} currentUnreadCount:${a} newUnreadCount:${c}`),a!==c&&(i.unreadCount=c,o=!0,!0===t&&this.emitConversationListUpdate()),o}emitConversationListUpdate(e=!1){e&&this._sortConversationList();const{OuterEvent:{CONVERSATION_LIST_UPDATED:t}}=this._core,n=this.getLocalConversationList();this._emitEvent({name:t,data:n}),this._emitTotalUnreadCountUpdate()}_emitTotalUnreadCountUpdate(){var e;const t=this.getTotalUnreadMessageCount();this._totalUnreadCount!==t&&(console.log(`${this._name}._emitTotalUnreadCountUpdate from ${this._totalUnreadCount} to ${t}`),this._totalUnreadCount=t,this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.OuterEvent.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED,data:t}))}_emitEvent(e){var t;null===(t=this._core)||void 0===t||t.notificationCenter.emitOuterEvent(e.name,e)}getTotalUnreadMessageCount(){const{OuterConstant:e}=this._core,t=this.getLocalConversationList();let n=0;return t.forEach((t=>{t.type!==e.CONV_SYSTEM&&(""!==t.messageRemindType&&t.messageRemindType!==e.MSG_REMIND_ACPT_AND_NOTE||(n+=t.unreadCount))})),n}getLocalConversationList(){return[...this._conversationStore.getConversationMap().values()].filter((e=>this._isConversationNeedShow(e.conversationID)))}hasLocalConversation(e){return this._conversationStore.getConversationMap().has(e)}getLocalConversation(e){return this._conversationStore.getConversationMap().get(e)}setLocalConversation(e,t){return this._conversationStore.getConversationMap().set(e,t)}deleteLocalConversation(e){this._conversationStore.getConversationMap().delete(e)}_updateConversationMapFromList(e){this._clearConversationMap();for(const[t,n]of e)this.setLocalConversation(t,n)}_clearConversationMap(){this._conversationStore.getConversationMap().clear()}patchMessageRemindType(e,t){let n=!1;e.forEach((e=>{const s=this.getLocalConversation(e);(null==s?void 0:s.messageRemindType)!==t&&(s.messageRemindType=t,n=!0)})),console.log(`${this._name}.patchMessageRemindType conversationIDList:${e} messageRemindType:${t} hasUpdated:${n}`),n&&this.emitConversationListUpdate()}markMessageAsRead(e){const{message:{messageDataHandler:t}}=this._core,{conversationID:n,lastReadTime:s=0,lastReadSequence:o=0}=e,i=null==t?void 0:t.getLocalMessageList(n);if(0===i.length)return;const{length:r}=i;for(let e=r-1;e>=0;e--){const t=i[e],n=s&&t.time>s,r=o&&t.sequence>o;if(!n&&!r){if("in"===t.flow&&t.isRead)break;t.setIsRead(!0)}}}appendToPinnedConversation(e){const t=[...this._conversationStore.getConversationMap().entries()],n=t.findIndex((e=>!1===e[1].isPinned));t.splice(n,0,[e.conversationID,e]),this._updateConversationMapFromList(t),this.emitConversationListUpdate()}_reset(){this._clearConversationMap(),this._totalUnreadCount=0,this._groupAtTipsList=[]}_dispose(){const{notificationCenter:e,InnerEvent:{NEW_MESSAGE:t,MESSAGE_DELETED:n,MESSAGE_REVOKED:s,MESSAGE_MODIFIED:o,CONVERSATION_UPDATED:i,LOGOUT:r,DESTROY:a}}=this._core,{InnerEventSubType:{GROUP_AT_TIPS:c}}=e;e.unSubscribeInnerEvent(t,this._handleNewMessage,this),e.unSubscribeInnerEvent(t,c,this._handleNewGroupAtTips,this),e.unSubscribeInnerEvent(n,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(s,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(o,this._handleMessageModified,this),e.unSubscribeInnerEvent(i,this._handleConversationUpdated,this),e.unSubscribeInnerEvent(r,this._reset,this),e.unSubscribeInnerEvent(a,this._dispose,this)}};function m(e,n,s){return t(this,void 0,void 0,(function*(){const{helper:t,channel:o}=s,i=t.generateProtocolData({servcmd:e,data:n}),r=`${i.head.seq}${e}`;return yield o.sendPacket(i,{requestId:r})}))}var f=new class{constructor(){this._name="SyncConversationList",this._pagingStatus="",this._pagingTimeStamp=0,this._pagingStartIndex=0,this._pagingPinnedTimeStamp=0,this._pagingPinnedStartIndex=0}init(e){this._core=e;const{notificationCenter:t,InnerEvent:n,helper:s,constants:o}=e;s.registerWorkflowStep(o.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,o.WORKFLOW_STEP.CONVERSATION_RECOVER,this._syncConversationListOnReOnline,this),s.registerWorkflowStep(o.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_LOGIN,o.WORKFLOW_STEP.CONVERSATION_LIST_SYNC,this._syncConversationList,this),t.subscribeInnerEvent(n.DESTROY,this._dispose,this),console.log(`${this._name}.init`)}isSyncCompleted(){return this._pagingStatus===u.RESOLVED}_syncConversationListOnReOnline(){return t(this,void 0,void 0,(function*(){return this._syncConversationList()}))}_syncConversationList(){return t(this,void 0,void 0,(function*(){yield this._pagingGetConversationList(!0),this._pagingStatus=u.RESOLVED,v.emitConversationListUpdate()}))}_pagingGetConversationList(e){return t(this,void 0,void 0,(function*(){try{this._pagingStatus=u.PENDING;const{common:n}=this._core,s=yield function(e,n){return t(this,void 0,void 0,(function*(){const{fromAccount:t,pagingTimeStamp:s,pagingStartIndex:o,pagingPinnedTimeStamp:i,pagingPinnedStartIndex:r}=e;return m("recentcontact.page_get",{AssistFlags:31,MsgAssistFlags:15,OrderType:1,From_Account:t,StartIndex:o,TimeStamp:s,TopStartIndex:r,TopTimeStamp:i},n)}))}({fromAccount:n.getCurrentUserID(),pagingTimeStamp:e?this._pagingTimeStamp:0,pagingStartIndex:e?this._pagingStartIndex:0,pagingPinnedTimeStamp:e?this._pagingPinnedTimeStamp:0,pagingPinnedStartIndex:e?this._pagingPinnedStartIndex:0},this._core),{CompleteFlag:o,SessionItem:i=[],TimeStamp:r,StartIndex:a,TopTimeStamp:c,TopStartIndex:l}=s||{};if(i.length>0){const e=this._getConversationOptions(i);v.updateLocalConversationList({conversationUpdateFieldList:e,isFromGetConversations:!0,updateUnreadCount:!0}),v.emitConversationListUpdate()}this._pagingTimeStamp=r,this._pagingStartIndex=a,this._pagingPinnedTimeStamp=c,this._pagingPinnedStartIndex=l,1!==o&&this._pagingGetConversationList(e)}catch(e){throw console.error(`${this._name}._pagingGetConversationList fail:`,e),e}}))}_getConversationOptions(e){const{utils:{isUndefined:t}}=this._core,n=this._convertConversationKey(e);return this._filterValidConversations(n).map((e=>(t(e.lastMsg)&&(e.lastMsg={elements:[]}),e.type===d.C2C?this._assembleC2COption(e):this._assembleGroupOption(e))))}_filterValidConversations(e){return e.filter((({type:e,userID:t})=>e===d.C2C&&!l(t)||2===e))}_assembleC2COption(e){var t,n,s,o,i,r;const{OuterConstant:c}=this._core,l=this._createUserprofile(e);return{conversationID:`${c.CONV_C2C}${e.userID}`,type:c.CONV_C2C,lastMessage:{lastTime:e.time,lastSequence:e.sequence,fromAccount:e.lastC2CMsgFromAccount,type:(null===(t=e.lastMsg)||void 0===t?void 0:t.elements[0])?null===(n=e.lastMsg)||void 0===n?void 0:n.elements[0].type:null,payload:(null===(s=e.lastMsg)||void 0===s?void 0:s.elements[0])?this._amendLayersOverLimitProp(e.lastMsg.elements[0].content):null,cloudCustomData:(null===(o=e.lastMsg)||void 0===o?void 0:o.cloudCustomData)||"",isRevoked:e.lastMessageFlag===_.C2C,onlineOnlyFlag:!1,nick:"",nameCard:"",version:0,isPeerRead:this._computeIsPeerRead(e),revoker:(null===(r=null===(i=e.lastMsg)||void 0===i?void 0:i.revokerInfo)||void 0===r?void 0:r.revoker)||null},unreadCount:0,userProfile:l,peerReadTime:e.peerReadTime,isPinned:1===e.isPinned,customData:e.customMark||"",markList:a(e.standardMark),conversationGroupList:[],remark:e.friendRemark||"",messageRemindType:this._transMsgRemindType(e.messageRemindType)}}_createUserprofile(e){var t;const{userID:n,nick:s,peerAvatar:o}=e,i=[{tag:"Tag_Profile_IM_Nick",value:s},{tag:"Tag_Profile_IM_Image",value:o}];return null===(t=this._core.user.userProfile)||void 0===t?void 0:t.createProfile(n,i)}_computeIsPeerRead(e){const{common:t}=this._core,n=t.getCurrentUserID(),{lastC2CMsgFromAccount:s,time:o,c2cPeerReadTime:i}=e;return s===n&&o<=i}_assembleGroupOption(e){var t,n,s;const{OuterConstant:o}=this._core;return{conversationID:`${o.CONV_GROUP}${e.groupID}`,type:o.CONV_GROUP,lastMessage:Object.assign(Object.assign({lastTime:e.time,lastSequence:e.sequence,fromAccount:e.msgGroupFromAccount},this._patchTypeAndPayload(e)),{cloudCustomData:(null===(t=e.lastMsg)||void 0===t?void 0:t.cloudCustomData)||"",isRevoked:e.lastMessageFlag===_.GROUP,onlineOnlyFlag:!1,nick:e.msgGroupFromNickName||"",nameCard:e.msgGroupFromCardName||"",revoker:(null===(s=null===(n=e.lastMsg)||void 0===n?void 0:n.revokerInfo)||void 0===s?void 0:s.revoker)||null}),groupProfile:{groupID:e.groupID,name:e.groupNick,avatar:e.groupImage,type:e.groupType,nextMessageSeq:e.nextMessageSeq},unreadCount:this._computeGroupUnreadCount(e),peerReadTime:0,isPinned:1===e.isPinned,version:0,customData:e.customMark||"",markList:a(e.standardMark),conversationGroupList:[],messageRemindType:this._transMsgRemindType(e.messageRemindType),subType:e.groupType}}_convertConversationKey(e){return e.map((e=>({type:e.Type,userID:e.To_Account,nick:e.C2cNick,peerAvatar:e.C2cImage,time:e.MsgTimeStamp,sequence:e.MsgSeq,lastC2CMsgFromAccount:e.LastC2cMsgFrom_Account,lastMsg:this._convertLastMsgKey(e.LastMsg),lastMessageFlag:e.LastMsgFlags,c2cPeerReadTime:e.LastC2cMsgPeerReadTime,peerReadTime:e.C2cPeerReadTime,friendRemark:e.C2cRemark,isPinned:e.TopFlags,standardMark:e.StandardMark,customMark:e.CustomMark,messageRemindType:e.MsgRecvOption,groupID:e.ToAccount,groupNick:e.GroupNick,groupImage:e.GroupImage,groupType:e.GroupType,nextMessageSeq:e.GroupNextMsgSeq,msgGroupFromAccount:e.MsgGroupFrom_Account,msgGroupFromNickName:e.MsgGroupFromNickName,MsgGroupFromCardName:e.MsgGroupFromCardName,unreadCount:e.UnreadMsgCount,noUnreadCount:e.GroupIgnoredUnreadSeqCount})))}_convertLastMsgKey(e){var t,n,s,o;if(null===(n=null===(t=this._core)||void 0===t?void 0:t.helper)||void 0===n?void 0:n.isEmpty(e))return null;let i="",r=null;return e.MsgBody&&(i=null===(s=e.MsgBody[0])||void 0===s?void 0:s.MsgType,r=this._parseContent(i,e.MsgBody[0])),{event:e.Event,elements:[{type:i,content:r,cloudCustomData:e.CloudCustomData}],revokerInfo:{revoker:null===(o=e.RevokerInfo)||void 0===o?void 0:o.revoker}}}_parseContent(e,t){var n;if(!t)return t;const s=this._core.message.messageFactory.getElementClass(e);return s?null===(n=s.parseServerPushElement(t))||void 0===n?void 0:n.content:t}_amendLayersOverLimitProp(e){const{LayersOverLimit:t}=e;return(function(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}(e,["LayersOverLimit"])).layersOverLimit=1===t,e}_transMsgRemindType(e){const{OuterConstant:t}=this._core;let n="";return 0===e?n=t.MSG_REMIND_ACPT_AND_NOTE:1===e?n=t.MSG_REMIND_DISCARD:2===e?n=t.MSG_REMIND_ACPT_NOT_NOTE:3===e&&(n=t.NOT_RECEIVE_OFFLINE_PUSH_EXCEPT_AT),n}_patchTypeAndPayload(e){const{OuterConstant:t,utils:{isUndefined:n}}=this._core,{event:s,elements:o=[]}=e.lastMsg||{};return n(s)?{type:o[0]?o[0].type:null,payload:o[0]?this._amendLayersOverLimitProp(o[0].content):null}:{type:t.MSG_GRP_TIP,payload:{}}}_computeGroupUnreadCount(e){const{unreadCount:t=0,noUnreadCount:n=0}=e,s=t-n;return s>0?s:0}_dispose(){const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};var M=new class{constructor(){this._name="GetConversationList"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getConversationList",context:this})}getConversationList(e){return t(this,void 0,void 0,(function*(){return{code:0,data:{conversationList:this._getConversationList(e),isSyncCompleted:f.isSyncCompleted()}}}))}_getConversationList(e){const{utils:{isUndefined:t,isArray:n,isPlainObject:s}}=this._core;if(t(e))return v.getLocalConversationList();if(n(e)){if(0===e.length)return[];return v.getLocalConversationList().filter((t=>e.includes(t.conversationID)))}if(s(e)){const{type:t,markType:n,groupName:s,hasUnreadCount:o,hasGroupAtInfo:i}=e;return v.getLocalConversationList().filter((e=>this._filterType(e,t)&&this._filterMarkType(e,n)&&this._filterGroupName(e,s)&&this._filterUnreadCount(e,o)&&this._filterGroupAtInfo(e,i)))}return[]}_filterType(e,t){const{OuterConstant:n}=this._core;return t!==n.CONV_C2C&&t!==n.CONV_GROUP||e.type===t}_filterGroupName(e,t){const{utils:{isString:n}}=this._core;return!n(t)||(""===t?0===e.conversationGroupList.length:e.conversationGroupList.includes(t))}_filterMarkType(e,t){const{utils:{isNumber:n}}=this._core;return!n(t)||(0===t?0===e.markList.length:e.markList.includes(t))}_filterUnreadCount(e,t){let n=!0;return!0===t?n=e.unreadCount>=1:!1===t&&(n=0===e.unreadCount),n}_filterGroupAtInfo(e,t){let n=!0;return!0===t?n=e.groupAtInfoList.length>=1:!1===t&&(n=0===e.groupAtInfoList.length),n}};function O(e,n,s){return t(this,void 0,void 0,(function*(){const{helper:t,channel:o}=s,i=t.generateProtocolData({servcmd:e,data:n}),r=`${i.head.seq}${e}`;return yield o.sendPacket(i,{requestId:r})}))}var E=new class{constructor(){this._name="GetC2CMessageRemindType"}init(e){this._core=e}get(e){return t(this,void 0,void 0,(function*(){try{const{common:n}=this._core,s=yield function(e,n){return t(this,void 0,void 0,(function*(){const{toAccount:t,userIDList:s}=e;return O("openim.get_c2c_peer_mute_notifications",{To_Account:t,Peer_Account:s},n)}))}({toAccount:n.getCurrentUserID(),userIDList:e},this._core),{MuteNotificationsList:o=[]}=s||{};o.forEach((e=>{const{Peer_Account:t,MuteNotifications:n}=e,s=`${this._core.OuterConstant.CONV_C2C}${t}`,o=c(n);v.patchMessageRemindType([s],o)}))}catch(e){console.error(`${this._name}.get fail:`,e)}}))}};var I=new class{constructor(){this._name="GetGroupMessageRemindType"}init(e){this._core=e}get(e){return t(this,void 0,void 0,(function*(){if(0!==e.length)try{const n=yield function(e,n){return t(this,void 0,void 0,(function*(){const{groupIDList:t,responseFilter:s}=e;return O("group_open_http_svc.get_group_self_member_info",{GroupIdList:t,ResponseFilter:s},n)}))}({groupIDList:e,responseFilter:{MemberInfoFilter:["MsgFlag"]}},this._core),{GroupInfo:s=[]}=n||{};s.forEach((e=>{var t;const{GroupId:n,MemberList:s}=e,o=(null===(t=s[0])||void 0===t?void 0:t.MsgFlag)||"",i=`${this._core.OuterConstant.CONV_GROUP}${n}`;v.patchMessageRemindType([i],o)}))}catch(e){console.error(`${this._name}.get fail:`,e)}}))}};var S=new class{constructor(){this._name="GetConversationProfile"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getConversationProfile",context:this})}getConversationProfile(e){return t(this,void 0,void 0,(function*(){const{OuterConstant:{CONV_C2C:t,CONV_GROUP:s},appStore:{groupStore:i},utils:{isEmpty:r}}=this._core,a={code:0,data:null};let c=v.getLocalConversation(e);if(o(e))return a.data=c,a;let l=!1;const u=n(e)?t:s;if(r(c)&&(l=!0,c=new g({conversationID:e,type:u})),console.log(`${this._name}.getConversationProfile conversationID:${e} isNewConversation:${l}`),a.data=c,null==c?void 0:c.getProfileCompleted())return a;if(u===t){const n=e.replace(t,"");yield this._handleC2CConversation(c,n),l&&(yield E.get([n]))}if(u===s){const t=e.replace(s,"");if(!i.getGroup(t))return a;yield this._handleGroupConversation(c,t),l&&(yield I.get([t]))}return a}))}_handleC2CConversation(e,n){return t(this,void 0,void 0,(function*(){var t,s;const{user:o,helper:i,utils:{isEmpty:r},appStore:{conversationStore:a,userStore:c}}=this._core,{conversationID:l}=e,u=yield null===(t=o.userProfile)||void 0===t?void 0:t.getUserProfile({userIDList:[n]});if(0===(null==u?void 0:u.data.length))throw new i.ChatError({code:h.USER_OR_GRP_NOT_FOUND});e.userProfile=null==u?void 0:u.data[0];const d=null===(s=c.getFriend(n))||void 0===s?void 0:s.remark;r(d)||e.remark===d||(e.remark=d),e.setProfileCompleted();const _=v.hasLocalConversation(l);console.log(`${this._name}._handleC2CConversation conversationID:${l} hasLocalConversation: ${_}`),_?a.updateConversation(l,e):v.appendToPinnedConversation(e)}))}_handleGroupConversation(e,n){return t(this,void 0,void 0,(function*(){const{apiMap:{getGroupProfile:t},appStore:{conversationStore:s}}=this._core,{conversationID:o}=e,i=yield t({groupID:n});e.groupProfile=null==i?void 0:i.data.group,e.setProfileCompleted();const r=v.hasLocalConversation(o);console.log(`${this._name}._handleGroupConversation conversationID:${o} hasLocalConversation: ${r}`),r?s.updateConversation(o,e):v.appendToPinnedConversation(e)}))}};var T=new class{init(e){const{helper:t}=e;t.registerApi({apiName:"getTotalUnreadMessageCount",context:this})}getTotalUnreadMessageCount(){return v.getTotalUnreadMessageCount()}};var L=new class{constructor(){this._name="SetMessageRead"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"setMessageRead",context:this})}handleC2CMessageReadSync(e){const{helper:{isEmpty:t},OuterConstant:n}=this._core;e.forEach((e=>{const{ReadC2cMsgNotify:s}=e;if(!t(s)){const{UinPairReadArray:e=[]}=s;e.forEach((e=>{const{From_Account:t,LastReadTime:s}=e,o=`${n.CONV_C2C}${t}`;console.log(`${this._name}.handleC2CMessageReadSync conversationID:${o} lastReadTime:${s}`),v.markMessageAsRead({conversationID:o,lastReadTime:s}),v.updateUnreadCount(o)}))}}))}handleGroupMessageReadSync(e){const{OuterConstant:t,utils:{isUndefined:n}}=this._core;e.forEach((e=>{const{GroupReadInfoArray:s}=e.MsgBody;n(s)||s.forEach((e=>{const{GroupId:n,LastReadMsgSeq:s}=e,o=`${t.CONV_GROUP}${n}`;console.log(`${this._name}.handleGroupMessageReadSync conversationID:${o} lastReadSequence:${s}`),v.markMessageAsRead({conversationID:o,lastReadSequence:s}),v.updateUnreadCount(o),this._clearGroupAtInfoList(o)}))}))}setMessageRead(e){return t(this,void 0,void 0,(function*(){var t,n;const{OuterConstant:s}=this._core,{conversationID:o}=e,i={code:0,data:{}},r=v.getLocalConversation(o);if(console.log(`${this._name}.setMessageRead conversationID:${o} unreadCount:${(null==r?void 0:r.unreadCount)||0}`),!r)return i;const a=!(null===(n=null===(t=this._core)||void 0===t?void 0:t.helper)||void 0===n?void 0:n.isEmpty(r.groupAtInfoList));if(r.type===s.CONV_GROUP&&a&&this._deleteGroupAtTips(o),0===r.unreadCount)return i;if(r.type===s.CONV_C2C){const e=this._getLocalMessageMaxTime(r);yield this._setC2CMessageRead(o,e)}if(r.type===s.CONV_GROUP){const e=this._getLocalMessageMaxSequence(r);yield this._setGroupMessageRead(o,e)}return r.type===s.CONV_SYSTEM&&(r.unreadCount=0),v.emitConversationListUpdate(),i}))}_deleteGroupAtTips(e){return t(this,void 0,void 0,(function*(){console.log(`${this._name}._deleteGroupAtTips conversationID:${e}`);const n=v.getLocalConversation(e);if(!n)return;const s=(null==n?void 0:n.groupAtInfoList)||[];if(0!==s.length)try{const{common:n}=this._core,o=s.map((e=>({From_Account:e.from,To_Account:n.getCurrentUserID(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID})));yield function(e,n){return t(this,void 0,void 0,(function*(){const{messageListToDelete:t}=e;return m("openim.deletemsg",{DelMsgList:t},n)}))}({messageListToDelete:o},this._core),console.log(`${this._name}._deleteGroupAtTips ok. count:${s.length}`),this._clearGroupAtInfoList(e)}catch(e){console.error(`${this._name}._deleteGroupAtTips fail:`,e)}}))}_clearGroupAtInfoList(e){const t=v.getLocalConversation(e);if(!t)return;let n=!1;t.groupAtInfoList.length>0&&(t.clearGroupAtInfoList(),n=!0,console.log(`${this._name}._clearGroupAtInfoList conversationID:${e} needEmitConversationUpdate:${n}`)),n&&v.emitConversationListUpdate()}_getLocalMessageMaxTime(e){var t;const{conversationID:n}=e,s=this._core.message.messageDataHandler.getLocalMessageList(n),o=Math.max(...s.map((e=>e.time)));let i=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastTime)||0;return o>i&&(console.log(`${this._name}._getLocalMessageMaxTime update lastMessageTime from ${i} to ${o}`),i=o),i}_getLocalMessageMaxSequence(e){var t;const{conversationID:n}=e,s=this._core.message.messageDataHandler.getLocalMessageList(n),o=Math.max(...s.map((e=>e.sequence)));let i=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return o>i&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${i} to ${o}`),i=o),i}_setC2CMessageRead(e,n){return t(this,void 0,void 0,(function*(){try{yield function(e,n){return t(this,void 0,void 0,(function*(){return m("openim.msgreaded",e,n)}))}({C2CMsgReaded:{Cookie:"",C2CMsgReadedItem:[{To_Account:e.replace("C2C",""),LastedMsgTime:n,Receipt:1}]}},this._core),console.log(`${this._name}._setC2CMessageRead ok, lastReadTime:${n}`),v.markMessageAsRead({conversationID:e,lastReadTime:n}),v.updateUnreadCount(e)}catch(e){throw console.warn(`${this._name}._setC2CMessageRead fail:`,e),e}}))}_setGroupMessageRead(e,n){return t(this,void 0,void 0,(function*(){try{yield function(e,n){return t(this,void 0,void 0,(function*(){const{groupID:t,lastMessageSequence:s}=e;return m("group_open_http_svc.msg_read_report",{GroupId:t,MsgReadedSeq:s},n)}))}({groupID:e.replace("GROUP",""),lastMessageSequence:n},this._core),console.log(`${this._name}._setGroupMessageRead ok, lastReadSequence:${n}`),v.markMessageAsRead({conversationID:e,lastReadSequence:n}),v.updateUnreadCount(e)}catch(e){throw console.warn(`${this._name}._setGroupMessageRead fail:`,e),e}}))}};var R=new class{constructor(){this._name="PinConversation"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"pinConversation",context:this})}handleConversationPinned(e,t){const{utils:{isArray:n}}=this._core;if(!n(e))return;const{OuterConstant:s}=this._core;let o=!1;e.forEach((e=>{const{Type:n,To_Account:i,GroupId:r}=e;let a;1===n?a=v.getLocalConversation(`${s.CONV_C2C}${i}`):2===n&&(a=v.getLocalConversation(`${s.CONV_GROUP}${r}`)),a&&(console.log(`${this._name}.handleConversationPinned conversationID:${a.conversationID} localPinned:${a.isPinned} remotePinned:${t}`),t&&!a.isPinned&&(a.isPinned=!0,o=!0),!t&&a.isPinned&&(a.isPinned=!1,o=!0))})),o&&v.emitConversationListUpdate(!0)}pinConversation(e){return t(this,void 0,void 0,(function*(){const{OuterConstant:i,common:r}=this._core,{conversationID:a,isPinned:c}=e,l={code:0,data:{conversationID:a}},u=v.getLocalConversation(a);if(u&&u.isPinned===c)return l;if(o(a))return u&&(u.isPinned=c),v.emitConversationListUpdate(!0),l;try{let e=null;n(a)?e={Type:1,To_Account:a.replace(i.CONV_C2C,"")}:s(a)&&(e={Type:2,GroupId:a.replace(i.CONV_GROUP,"")});const o=yield function(e,n){return t(this,void 0,void 0,(function*(){const{fromAccount:t,operationType:s,itemList:o}=e;return m("recentcontact.top",{From_Account:t,OperationType:s,RecentContactItem:o},n)}))}({fromAccount:r.getCurrentUserID(),operationType:!0===c?1:2,itemList:[e]},this._core);if(console.log(`${this._name}.pinConversation ok, conversationID:${a} isPinned:${c}.`),o){if(u)u.isPinned!==c&&(u.isPinned=c);else{const e=new g({conversationID:a,type:n(a)?i.CONV_C2C:i.CONV_GROUP,isPinned:c});v.setLocalConversation(a,e)}v.emitConversationListUpdate(!0)}return l}catch(e){throw console.error(`${this._name}.pinConversation fail:`,e),e}}))}};var y=new class{constructor(){this._name="DeleteConversation"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteConversation",context:this})}handleConversationDeleted(e){const{utils:{isArray:t}}=this._core;if(!t(e))return;const{OuterConstant:n}=this._core,s=[];e.forEach((e=>{const{Type:t,Peer_Account:o,GroupId:i}=e;1===t&&s.push(`${n.CONV_C2C}${o}`),2===t&&s.push(`${n.CONV_GROUP}${i}`)})),console.log(`${this._name}.handleConversationDeleted conversationIDList:${s}`),this._deleteLocalConversationList(s)}deleteConversation(e){return t(this,void 0,void 0,(function*(){const{utils:{isString:t}}=this._core;if(t(e))return console.log(`${this._name}.deleteConversation conversationID:${e}`),void(yield this._deleteConversation({conversationIDList:[e],flag:p.ONLY_CONVERSATIONID}));const n=Object.assign({},e);n.conversationIDList.length>100&&(n.conversationIDList=n.conversationIDList.slice(0,100)),yield this._deleteConversation(n)}))}_deleteConversation(e){return t(this,void 0,void 0,(function*(){const{conversationIDList:t,clearHistoryMessage:n=!0,flag:s=0}=e;console.log(`${this._name}._deleteConversation conversationIDList:${t} clearHistoryMessage:${n}`);try{const e=yield Promise.all([this._deleteConversationFromLocal(t),this._deleteConversationFromServer(t,n)]),o=[...e[0],...e[1]];if(0===o.length)throw new this._core.helper.ChatError({code:h.CONV_NOT_FOUND});return console.log(`${this._name}._deleteConversation ok.`),{code:0,data:s===p.ONLY_CONVERSATIONID?{conversationID:o[0]}:{conversationIDList:o}}}catch(e){throw console.error(`${this._name}._deleteConversation fail:`,e),e}}))}_deleteConversationFromLocal(e){const{OuterConstant:t}=this._core;return e.filter((e=>{var n;if(!v.hasLocalConversation(e))return!1;const s=null===(n=v.getLocalConversation(e))||void 0===n?void 0:n.type;return s!==t.CONV_GROUP||this._hasLocalGroup(e)?s===t.CONV_SYSTEM&&(this._deleteLocalConversation(e),!0):(this._deleteLocalConversation(e),!0)}))}_deleteConversationFromServer(e,n){return t(this,void 0,void 0,(function*(){const{OuterConstant:s,common:o}=this._core,i={fromAccount:o.getCurrentUserID(),conversationList:[],clearHistoryMessage:n?1:0};if(e.forEach((e=>{var t;if(v.hasLocalConversation(e)){const n=(null===(t=v.getLocalConversation(e))||void 0===t?void 0:t.type)||"",o=e.replace(n,"");n===s.CONV_C2C?i.conversationList.push({To_Account:o,Type:1}):n===s.CONV_GROUP&&this._hasLocalGroup(e)&&i.conversationList.push({ToGroupid:o,Type:2})}})),0===i.conversationList.length)return[];const r=yield function(e,n){return t(this,void 0,void 0,(function*(){const{fromAccount:t,conversationList:s,clearHistoryMessage:o}=e;return m("recentcontact.batch_delete",{From_Account:t,ContactItem:s,ClearRamble:o},n)}))}(i,this._core);if(r){const{ResultItem:e=[]}=r,t=[];return e.length>0&&e.forEach((e=>{if(0===e.ResultCode){const n=1===e.type?`${s.CONV_C2C}${e.To_Account}`:`${s.CONV_GROUP}${e.ToGroupid}`;t.push(n)}})),this._deleteLocalConversationList(t),t}return[]}))}_deleteLocalConversationList(e){let t=!1;e.forEach((e=>{v.hasLocalConversation(e)&&(this._deleteLocalConversation(e,!1),t=!0)})),console.log(`${this._name}._deleteLocalConversationList isUpdate:${t}`),t&&v.emitConversationListUpdate()}_deleteLocalConversation(e,t=!0){const n=v.hasLocalConversation(e);console.log(`${this._name}._deleteLocalConversation conversationID:${e} has:${n}`),n&&(v.deleteLocalConversation(e),this._deleteConversationLocalMessage(e),t&&v.emitConversationListUpdate())}_hasLocalGroup(e){const{OuterConstant:{CONV_GROUP:t},appStore:{groupStore:n}}=this._core,s=e.replace(t,"");return!!n.getGroup(s)}_deleteConversationLocalMessage(e){console.log(`${this._name}._deleteConversationLocalMessage conversationID:${e}`),this._core.message.messageDataHandler.deleteConversationMessageList(e)}};var N=new class{constructor(){this._name="SetConversationDraft"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"setConversationDraft",context:this})}setConversationDraft(e){return t(this,void 0,void 0,(function*(){const{conversationID:t,draftText:n}=e;if(console.log(`${this._name} conversationID:${t} draftText:${n}`),!v.hasLocalConversation(t))throw new this._core.helper.ChatError({code:h.CONV_NOT_FOUND});const s=v.getLocalConversation(t);return null==s||s.setDraftText(n),v.emitConversationListUpdate(),{code:0,data:{conversation:s}}}))}};var D=new class{constructor(){this._name="SetC2CMessageRemindType"}init(e){this._core=e}set(n,s){return t(this,void 0,void 0,(function*(){n.length>30&&(console.warn(`${this._name}.set userIDList length:${n.length} exceeds limit 30`),n.splice(30));const o=function(){const{MSG_REMIND_ACPT_AND_NOTE:t,MSG_REMIND_DISCARD:n,MSG_REMIND_ACPT_NOT_NOTE:s}=e.core.OuterConstant;return{[t]:0,[n]:1,[s]:2}}()[s],i=yield function(e,n){return t(this,void 0,void 0,(function*(){const{userIDList:t,receiveMessageOption:s}=e;return O("openim.set_c2c_peer_mute_notifications",{Peer_Account:t,Mute_Notifications:s},n)}))}({userIDList:n,receiveMessageOption:o},this._core),{ErrorList:r=[]}=i||{},a=[];r.forEach((e=>{const{Peer_Account:t,ErrorCode:s}=e;a.push({userID:t,code:s});const o=n.indexOf(t);o>-1&&n.splice(o,1)}));const c=[],l=[],{OuterConstant:u}=this._core;return n.forEach((e=>{l.push(`${u.CONV_C2C}${e}`),c.push({userID:e})})),v.patchMessageRemindType(l,s),{code:0,data:{successUserIDList:c,failureUserIDList:a}}}))}};var A=new class{constructor(){this._name="SetGroupMessageRemindType"}init(e){this._core=e}set(e,n){return t(this,void 0,void 0,(function*(){const{common:s,OuterConstant:o}=this._core;yield function(e,n){return t(this,void 0,void 0,(function*(){const{groupID:t,userID:s,receiveMessageOption:o}=e;return O("group_open_http_svc.modify_group_member_info",{GroupId:t,Member_Account:s,MsgFlag:o},n)}))}({groupID:e,userID:s.getCurrentUserID(),receiveMessageOption:n},this._core);const i=`${o.CONV_GROUP}${e}`;return v.patchMessageRemindType([i],n),{code:0,data:{groupID:e,messageRemindType:n}}}))}};var G=new class{constructor(){this._name="SetMessageRemindType"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"setMessageRemindType",context:this})}handleC2CMessageRemindTypeSync(e){const{helper:{isEmpty:t},OuterConstant:n}=this._core;e.forEach((e=>{const{MuteNotificationsSync:s}=e;if(!t(s)){const{To_Account:e,MuteNotifications:t}=s,o=e.map((e=>`${n.CONV_C2C}${e}`)),i=c(t);console.log(`${this._name}.handleC2CMessageRemindTypeSync conversationIDList:${o} messageRemindType:${i}`),v.patchMessageRemindType(o,i)}}))}setMessageRemindType(e){return t(this,void 0,void 0,(function*(){const t="setMessageRemindType",{groupID:n,userIDList:s,messageRemindType:o}=e,{helper:i,utils:{isUndefined:r}}=this._core;try{if(!r(n)){console.log(`${this._name}.${t} groupID:${n} messageRemindType:${o}`);return yield A.set(n,o)}if(!r(s)){console.log(`${this._name}.${t} userIDList:${s} messageRemindType:${o}`);return yield D.set(s,o)}throw new i.ChatError({functionName:t,message:"userIDList or groupID is required"})}catch(e){const n=new i.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{init(e){console.log("ConversationAction.init"),this._core=e,f.init(e),M.init(e),S.init(e),T.init(e),L.init(e),R.init(e),y.init(e),N.init(e),G.init(e);const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:n,DESTROY:s}}=this._core,{InnerEventSubType:{CONV_MODIFIED:o,C2C_MESSAGE_READ_SYNC:i,GROUP_MESSAGE_READ_SYNC:r,C2C_REMIND_TYPE_SYNC:a}}=t;t.subscribeInnerEvent(n,o,this._onConversationModified,this),t.subscribeInnerEvent(n,i,this._onC2CMessageReadSync,this),t.subscribeInnerEvent(n,a,this._onC2CMessageRemindTypeSync,this),t.subscribeInnerEvent(n,r,this._onGroupMessageReadSync,this),t.subscribeInnerEvent(s,this._dispose,this)}handleLoginSuccess(){f.handleLoginSuccess()}_onConversationModified(e){const{RecentContactMod:t=[]}=e;t.forEach((e=>{const{PushType:t}=e;if(t===C.CONV_DELETED){const{RecentContactList:t}=e.RecentContactDeleteItem;y.handleConversationDeleted(t)}if(t===C.CONV_PINED){const{RecentContactList:t}=e.RecentContactTopItem;R.handleConversationPinned(t,!0)}if(t===C.CONV_UNPINED){const{RecentContactList:t}=e.RecentContactTopItem;R.handleConversationPinned(t,!1)}}))}_onC2CMessageReadSync(e){const{C2cNotifyMsgArray:t=[]}=e;L.handleC2CMessageReadSync(t)}_onC2CMessageRemindTypeSync(e){const{C2cNotifyMsgArray:t=[]}=e;G.handleC2CMessageRemindTypeSync(t)}_onGroupMessageReadSync(e){const{GroupTips:t=[]}=e;L.handleGroupMessageReadSync(t)}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:n}}=this._core,{InnerEventSubType:{CONV_MODIFIED:s,C2C_MESSAGE_READ_SYNC:o,GROUP_MESSAGE_READ_SYNC:i,C2C_REMIND_TYPE_SYNC:r}}=e;e.unSubscribeInnerEvent(t,s,this._onConversationModified,this),e.unSubscribeInnerEvent(t,o,this._onC2CMessageReadSync,this),e.unSubscribeInnerEvent(t,r,this._onC2CMessageRemindTypeSync,this),e.unSubscribeInnerEvent(t,i,this._onGroupMessageReadSync,this),e.unSubscribeInnerEvent(n,this._dispose,this)}};var U=new class{init(e){console.log("ReceiveMessageOptions.init"),this._core=e,D.init(e),A.init(e),E.init(e),I.init(e)}};const $=e=>!n(e)&&!s(e)&&!o(e),V={getConversationProfile:[{key:"conversationID",required:!0,rules:["string"],allowEmpty:!1,customValidator:e=>!$(e)||"conversationID is invalid."}],setMessageRead:{conversationID:{required:!0,rules:["string"],allowEmpty:!1,customValidator:e=>!$(e)||"conversationID is invalid."}},pinConversation:{conversationID:{required:!0,rules:["string"],allowEmpty:!1,customValidator:e=>!$(e)||"conversationID is invalid."},isPinned:{required:!0,rules:["boolean"],allowEmpty:!1}},deleteConversation:[{key:"options",required:!0,rules:["string","object"],allowEmpty:!1,customValidator:t=>{const{core:{utils:{isArray:n,isObject:s,isString:o}}}=e;if(!o(t)&&!s(t))return"options is String or Object.";if(o(t)&&$(t))return"conversationID is invalid.";if(s(t)){if(!n(t.conversationIDList))return"conversationIDList is not Array.";if(0===t.conversationIDList.length)return"conversationIDList is empty.";if(t.conversationIDList.some((e=>{if($(e))return!0})))return"conversationIDList includes invalid conversationID.";if(t.clearHistoryMessage&&"boolean"!=typeof t.clearHistoryMessage)return"clearHistoryMessage is not Boolean."}return!0}}],setConversationDraft:{conversationID:{required:!0,rules:["string"],allowEmpty:!1,customValidator:e=>!(!n(e)&&!s(e))||"conversationID is invalid."},draftText:{required:!0,rules:["string"],allowEmpty:!0}}},F={getConversationList:!0,getConversationProfile:!0,getTotalUnreadCount:!0,setMessageRead:!0,pinConversation:!0,deleteConversation:!0,setConversationDraft:!0,setMessageRemindType:!0};var b=new class{install(t){e.init(t),P.init(t),U.init(t),v.init(t),t.helper.registerValidateConfig({auth:F,params:V})}handleLoginSuccess(){P.handleLoginSuccess()}};export{b as default};