pope-test-lite-chat-test
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 18.9 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).liteChatConversation=t()}(this,(function(){"use strict";function e(e,t,n,s){return new(n||(n=Promise))((function(o,i){function a(e){try{l(s.next(e))}catch(e){i(e)}}function r(e){try{l(s.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;function t(e){if(null==e)return!0;if("boolean"==typeof e)return!1;if("number"==typeof e)return 0===e;if("string"==typeof e)return 0===e.length;if("function"==typeof e)return 0===e.length;if(Array.isArray(e))return 0===e.length;if(e instanceof Error)return""===e.message;if((e=>{if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);if(null===t)return!0;let n=t;for(;null!==Object.getPrototypeOf(n);)n=Object.getPrototypeOf(n);return t===n})(e)){for(const t in e)if(Object.prototype.hasOwnProperty.call(e,t))return!1;return!0}return("[object Map]"===Object.prototype.toString.call(e)||"[object Set]"===Object.prototype.toString.call(e)||"[object File]"===Object.prototype.toString.call(e))&&0===e.size}const n="C2C",s="GROUP",o="TOPIC",i="@TIM#SYSTEM",a="AVChatRoom",r="Room",l="Live",u="TIMTextElem",c="TIMImageElem",d="TIMSoundElem",p="TIMFileElem",g="TIMFaceElem",v="TIMVideoFileElem",m="TIMLocationElem",h="TIMGroupTipElem",C="TIMGroupSystemNoticeElem",y="TIMCustomElem",f="TIMRelayElem",M="AcceptAndNotify",_="AcceptNotNotify",T="Discard",L="AcceptNotNotifyExceptAt";var S,I,O;!function(e){e.NOT_START="notStart",e.PENDING="pending",e.RESOLVED="resolved",e.REJECTED="rejected"}(S||(S={})),function(e){e[e.C2C=1]="C2C",e[e.GROUP=2]="GROUP"}(I||(I={})),function(e){e[e.C2C=8]="C2C",e[e.GROUP=2]="GROUP"}(O||(O={}));const E=e=>void 0===e,P=e=>"string"==typeof e,R=e=>"function"==typeof Array.isArray?Array.isArray(e):"array"===(e=>{var t;return null===(t=Object.prototype.toString.call(e).match(/^\[object (.*)\]$/))||void 0===t?void 0:t[1].toLowerCase()})(e),k=e=>R(e)||(e=>null!==e&&"object"==typeof e)(e),D=(e,t,n,s)=>{if(!k(e)||!k(t))return 0;let o=0;const i=Object.keys(t);let a;for(let r=0,l=i.length;r<l;r++)if(a=i[r],!(E(t[a])||n&&n.includes(a)))if(k(e[a])&&k(t[a]))o+=D(e[a],t[a],n,s);else{if(s&&s.includes(t[a]))continue;e[a]!==t[a]&&(e[a]=t[a],o+=1)}return o},U=e=>P(e)&&e.slice(0,3)===n,b=(e,t)=>{if(E(t))return"";switch(e){case u:return t.text;case c:return"[Image]";case m:return"[Location]";case d:return"[Voice]";case v:return"[Video]";case p:return"[File]";case y:return"[Custom Messages]";case h:return"[Group Notification]";case C:return"[Group System Message]";case g:return"[Animated Sticker]";case f:return"[Chat Record]";default:return""}},A=e=>({lastTime:(null==e?void 0:e.time)||0,lastSequence:(null==e?void 0:e.sequence)||0,fromAccount:(null==e?void 0:e.from)||"",messageForShow:b(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 G(e){const t=[];if(!P(e))return t;const n=e.length;if(0===n)return t;for(let s=n-1;s>=0;s--)"1"===e[s]&&t.push(2**(n-s-1));return t}class N{constructor(e){this.conversationID=e.conversationID||"",this.unreadCount=e.unreadCount||0,this.type=e.type||"",this.lastMessage=A(e.lastMessage),this.peerReadTime=e.peerReadTime||0,this.groupAtInfoList=[],this.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(){E(this.userProfile)&&this.type===n?this.userProfile={}:E(this.groupProfile)&&this.type===s&&(this.groupProfile={}),t(this.subType)&&this.type===s&&(this.subType=this.groupProfile.type||"")}updateUnreadCount(e){const{nextUnreadCount:t,isFromGetConversations:i,isUnreadC2CMessage:r}=e;E(t)||(this.subType!==a?i&&this.type===s||i&&this.type===o||r&&this.type===n?this.unreadCount=t:this.unreadCount+=t:this.unreadCount=0)}updateLastMessage(e){this.lastMessage=A(e)}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){const{sequence:t,time:o}=e;return this.type===n&&t===this.lastMessage.lastSequence&&o===this.lastMessage.lastTime||this.type===s&&t===this.lastMessage.lastSequence}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}setDraftText(e){this.draftText=e}}var F=new class{constructor(){this._name="ConversationFactory",this._pagingStatus="",this._pagingTimeStamp=0,this._pagingStartIndex=0,this._pagingPinnedTimeStamp=0,this._pagingPinnedStartIndex=0,this._pagingConvIDMap=new Map,this._convMap=new Map,this._convTotalUnreadCount=0}install(e){this._core=e;const{notificationCenter:t,InnerEvent:n,helper:s}=e;s.registerApi("getConversationList",this),s.registerApi("getConversationProfile",this),s.registerApi("setMessageRead",this),s.registerApi("getTotalUnreadCount",this),t.subscribeInnerEvent(n.LOGIN,this._handleLoginSuccess,this),t.subscribeInnerEvent(n.NEW_MESSAGE,this._handleNewMessage,this),t.subscribeInnerEvent(n.MESSAGE_DELETED,this._handleMessageDeleted,this),t.subscribeInnerEvent(n.MESSAGE_REVOKED,this._handleMessageRevoked,this),t.subscribeInnerEvent(n.MESSAGE_MODIFIED,this._handleMessageModified,this),console.log(`${this._name}.install`)}_handleLoginSuccess(){this.syncConvList()}_handleNewMessage(e){const{conversationOptionsList:t,isInstantMessage:n=!0,isUnreadC2CMessage:s=!1,updateUnreadCount:o=!0}=e;if(0===t.length)return;this._updateLocalConvList({conversationOptionsList:t,isInstantMessage:n,isUnreadC2CMessage:s,isFromGetConversations:!1,updateUnreadCount:o});t.filter((e=>this._isConvNeedShow(e.conversationID))).length>0&&this._onConversationListUpdate()}_handleMessageDeleted(e){var s,o;const i=(null===(o=null===(s=this._core)||void 0===s?void 0:s.message.messageStore)||void 0===o?void 0:o.getLocalMessageList(e))||[];let a={};for(let e=(i.length||0)-1;e>=0;e--)if(!i[e].isDeleted){a=i[e];break}const r=this._convMap.get(e);if(!r)return;let l=!1;r.lastMessage.lastSequence===a.sequence&&r.lastMessage.lastTime===a.time||(t(a)&&(a=void 0),r.updateLastMessage(a),l=!0),e.startsWith(n)&&this._updateUnreadCount(e),l&&(this._onConversationListUpdate(),console.log(`${this._name}._handleMessageDeleted. \n update conversationID:${e} with lastMessage:`,r.lastMessage))}_handleMessageRevoked(e){const{messageList:t=[],updateUnreadCount:n=!0}=e;if(0===t.length)return;let s=null,o=!1;t.forEach((e=>{s=this._convMap.get(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._onConversationListUpdate()}_handleMessageModified(e){const{conversationID:n,message:s}=e,o=this._convMap.get(n);if(t(o))return;const{lastMessage:i}=o;i&&i.lastTime===s.time&&i.lastSequence===s.sequence&&i.version!==s.version&&(i.type=s.type,i.payload=s.payload,i.messageForShow=b(s.type,s.payload),i.cloudCustomData=s.cloudCustomData,i.version=s.version,this._onConversationListUpdate(),console.log(`${this._name} conversationID:${n} lastMessage updated`))}syncConvList(){return e(this,arguments,void 0,(function*(e=!0){yield this._pagingGetConvList(e),this._pagingStatus=S.RESOLVED,this._onConversationListUpdate()}))}_pagingGetConvList(t){return e(this,void 0,void 0,(function*(){try{this._pagingStatus=S.PENDING;const n=yield function(t,n){return e(this,void 0,void 0,(function*(){const{pagingTimeStamp:e,pagingStartIndex:s,pagingPinnedTimeStamp:o,pagingPinnedStartIndex:i}=t;try{const t="recentcontact.page_get",a=null==n?void 0:n.helper.generateProtocolData({servcmd:t,data:{AssistFlags:31,MsgAssistFlags:15,OrderType:1,From_Account:null==n?void 0:n.store.get("login").userId,StartIndex:s,TimeStamp:e,TopStartIndex:i,TopTimeStamp:o}}),r=`${a.head.seq}${t}`;return yield n.channel.sendPacket(a,{requestId:r})}catch(e){throw console.warn(`ConversationService.pagingGetConvList error: ${e}`),e}}))}({pagingTimeStamp:t?this._pagingTimeStamp:0,pagingStartIndex:t?this._pagingStartIndex:0,pagingPinnedTimeStamp:t?this._pagingPinnedTimeStamp:0,pagingPinnedStartIndex:t?this._pagingPinnedStartIndex:0},this._core);if(n){const{CompleteFlag:e,SessionItem:s=[],TimeStamp:o,StartIndex:i,TopTimeStamp:a,TopStartIndex:r}=n;if(s.length>0){const e=this._getConvOptions(s);this._pagingConvIDMap=new Map([...this._pagingConvIDMap,...e.map((e=>[e.conversationID,1]))]),this._updateLocalConvList({conversationOptionsList:e,isFromGetConversations:!0,updateUnreadCount:!0}),this._onConversationListUpdate()}this._pagingTimeStamp=o,this._pagingStartIndex=i,this._pagingPinnedTimeStamp=a,this._pagingPinnedStartIndex=r,1!==e&&this._pagingGetConvList(t)}}catch(e){throw console.error(`${this._name}._pagingGetConvList fail:`,e),e}}))}_getConvOptions(e){const t=this._convertConvKey(e).filter((({type:e,userID:t})=>1===e&&!this._isNonExistentAccount(t)||2===e)),o=this._core.store.get("login").userId;return t.map((e=>{var t,i,a,r,l,u,c,d,p;return E(e.lastMsg)&&(e.lastMsg={elements:[]}),e.type===I.C2C?(e.userID,e.nick,e.peerAvatar,{conversationID:`${n}${e.userID}`,type:n,lastMessage:{lastTime:e.time,lastSequence:e.sequence,fromAccount:e.lastC2CMsgFromAccount,type:(null===(t=e.lastMsg)||void 0===t?void 0:t.elements[0])?null===(i=e.lastMsg)||void 0===i?void 0:i.elements[0].type:null,payload:(null===(a=e.lastMsg)||void 0===a?void 0:a.elements[0])?this._amendLayersOverLimitProp(e.lastMsg.elements[0].content):null,cloudCustomData:(null===(r=e.lastMsg)||void 0===r?void 0:r.cloudCustomData)||"",isRevoked:e.lastMessageFlag===O.C2C,onlineOnlyFlag:!1,nick:"",nameCard:"",version:0,isPeerRead:e.lastC2CMsgFromAccount===o&&e.time<=e.c2cPeerReadTime,revoker:(null===(u=null===(l=e.lastMsg)||void 0===l?void 0:l.revokerInfo)||void 0===u?void 0:u.revoker)||null},unreadCount:0,userProfile:{},peerReadTime:e.peerReadTime,isPinned:1===e.isPinned,customData:e.customMark||"",markList:G(e.standardMark),conversationGroupList:[],remark:e.friendRemark||"",messageRemindType:this._transMsgRemindType(e.messageRemindType)}):{conversationID:`${s}${e.groupID}`,type:s,lastMessage:Object.assign(Object.assign({lastTime:e.time,lastSequence:e.sequence,fromAccount:e.msgGroupFromAccount},this._patchTypeAndPayload(e)),{cloudCustomData:(null===(c=e.lastMsg)||void 0===c?void 0:c.cloudCustomData)||"",isRevoked:e.lastMessageFlag===O.GROUP,onlineOnlyFlag:!1,nick:e.groupNick||"",nameCard:e.nameCard||"",revoker:(null===(p=null===(d=e.lastMsg)||void 0===d?void 0:d.revokerInfo)||void 0===p?void 0:p.revoker)||null}),groupProfile:{},unreadCount:this._computeGroupUnreadCount(e),peerReadTime:0,isPinned:1===e.isPinned,version:0,customData:e.customMark||"",markList:G(e.standardMark),conversationGroupList:[],messageRemindType:this._transMsgRemindType(e.messageRemindType),subType:e.groupType}}))}_convertConvKey(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.MsgGroupFromNickName,nameCard:e.MsgGroupFromCardName,groupImage:e.GroupImage,groupType:e.GroupType,nextMessageSeq:e.GroupNextMsgSeq,msgGroupFromAccount:e.MsgGroupFrom_Account,unreadCount:e.UnreadMsgCount,noUnreadCount:e.GroupIgnoredUnreadSeqCount})))}_convertLastMsgKey(e){var n,s,o;return t(e)?null:{event:e.Event,elements:[{type:e.MsgBody?null===(n=e.MsgBody[0])||void 0===n?void 0:n.MsgType:"",content:e.MsgBody?null===(s=e.MsgBody[0])||void 0===s?void 0:s.MsgContent:"",cloudCustomData:e.CloudCustomData}],revokerInfo:{revoker:null===(o=e.RevokerInfo)||void 0===o?void 0:o.revoker}}}_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){let t="";return 0===e?t=M:1===e?t=T:2===e?t=_:3===e&&(t=L),t}_patchTypeAndPayload(e){const{event:t,elements:n=[]}=e.lastMsg||{};return E(t)?{type:n[0]?n[0].type:null,payload:n[0]?this._amendLayersOverLimitProp(n[0].content):null}:{type:h,payload:{}}}_computeGroupUnreadCount(e){const{unreadCount:t=0,noUnreadCount:n=0}=e,s=t-n;return s>0?s:0}_updateLocalConvList(e){const{newConvList:t}=this._getTmpConvListMapping(e);this._convMap=new Map(this._sortConvList([...this._convMap])),console.log(`${this._name}._updateLocalConvList newConvList: ${t.length}`)}_getTmpConvListMapping(e){const{conversationOptionsList:i,isFromGetConversations:a,isInstantMessage:r,isUnreadC2CMessage:l=!1,updateUnreadCount:u}=e,c=[],d=null==i?void 0:i.length;for(let e=0;e<d;e++){const d=new N(i[e]),{conversationID:p,type:g}=d;if(!this._isNonExistentAccount(p))if(this._convMap.has(p)){const c=this._convMap.get(p);if(a&&g!==o){this._convMap.set(p,d),g===n?d.unreadCount=c.unreadCount:g===s&&(d.groupProfile=JSON.parse(JSON.stringify(c.groupProfile)));continue}const v=["unreadCount","allowType","adminForbidType","payload"];!1===r&&v.push("lastMessage"),"boolean"==typeof r&&v.push("isPinned");const m=i[e].lastMessage,h=!E(m);h||i[e].type===o||console.log(`${this._name}._getTmpConvListMapping\n isRawLastMessageExisted:${h} ${JSON.stringify(i[e])}`),E(r)&&h&&null===c.lastMessage.payload&&(c.lastMessage.payload=m.payload),t(c.lastMessage.revoker)||(c.lastMessage.revoker=null);D(c,d,v,[null,void 0,"",0,NaN]),!0===u&&c.updateUnreadCount({nextUnreadCount:d.unreadCount,isFromGetConversations:a,isUnreadC2CMessage:l}),r&&h&&(m.payload&&(c.lastMessage.payload=m.payload),c.type===s&&(c.lastMessage.nameCard=m.nameCard,c.lastMessage.nick=m.nick)),h&&c.lastMessage.cloudCustomData!==m.cloudCustomData&&(c.lastMessage.cloudCustomData=m.cloudCustomData||"")}else c.push(d),this._convMap.set(p,d)}return{newConvList:c}}_isNonExistentAccount(e){let t;return e.startsWith(n)&&(t=e.replace(n,"")),"@TLS#ERROR"===t||"@TLS#NOT_FOUND"===t}_sortConvList(e){const n=[],s=[],o=[],i=[];return e.forEach((e=>{!0===e[1].isPinned?t(e[1].lastMessage.lastTime)?s.push(e):n.push(e):t(e[1].lastMessage.lastTime)?i.push(e):o.push(e)})),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)}_getLocalConvList(){return[...this._convMap.values()].filter((e=>this._isConvNeedShow(e.conversationID)))}_isConvNeedShow(e){const t=this._getLocalConversation(e);if(E(t))return!0;const n=t.type===o,i=t.type===s&&t.subType===r,a=t.type===s&&t.subType===l;return!(n||i||a)}_getLocalConversation(e){return this._convMap.get(e)}_updateUnreadCount(e,t=!0){var n,s;let o=!1;const i=this._getLocalConversation(e),a=null===(s=null===(n=this._core)||void 0===n?void 0:n.message.messageStore)||void 0===s?void 0:s.getLocalMessageList(e);if(!i)return o;const r=i.unreadCount,l=null==a?void 0:a.filter((e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted)).length;return r!==l&&(i.unreadCount=l,o=!0,console.log(`${this._name}._updateUnreadCount\n from ${r} to ${l}, conversationID:${e}`),!0===t&&this._onConversationListUpdate()),o}_emitEvent(e){var t;null===(t=this._core)||void 0===t||t.notificationCenter.emitOuterEvent(e.name,e)}_onConversationListUpdate(){var e;const t=this._getLocalConvList();this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.OuterEvent.CONVERSATION_LIST_UPDATED,data:t}),this._onTotalUnreadCountUpdate()}_onTotalUnreadCountUpdate(){var e;const t=this.getTotalUnreadCount();this._convTotalUnreadCount!==t&&(console.log(`${this._name}._onTotalUnreadCountUpdate from ${this._convTotalUnreadCount} to ${t}`),this._convTotalUnreadCount=t,this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.OuterEvent.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED,data:t}))}getConversationList(t){return e(this,void 0,void 0,(function*(){return{code:0,data:{conversationList:this._getConversationList(t),isSyncCompleted:this._isSyncCompleted()}}}))}_getConversationList(e){if(E(e))return this._getLocalConvList();if(R(e)){if(0===e.length)return[];return this._getLocalConvList().filter((t=>e.includes(t.conversationID)))}if((e=>{if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);if(null===t)return!0;let n=t;for(;null!==Object.getPrototypeOf(n);)n=Object.getPrototypeOf(n);return t===n})(e)){const{type:t,markType:n,groupName:s,hasUnreadCount:o,hasGroupAtInfo:i}=e;return this._getLocalConvList().filter((e=>this._filterType(e,t)&&this._filterMarkType(e,n)&&this._filterGroupName(e,s)&&this._filterUnreadCount(e,o)&&this._filterGroupAtInfo(e,i)))}return[]}_isSyncCompleted(){return this._pagingStatus===S.RESOLVED}_filterType(e,t){return t!==n&&t!==s||e.type===t}_filterGroupName(e,t){return!P(t)||(""===t?0===e.conversationGroupList.length:e.conversationGroupList.includes(t))}_filterMarkType(e,t){return null===(n=t)||!("number"==typeof n&&!Number.isNaN(n-0)||"object"==typeof n&&n.constructor===Number)||(0===t?0===e.markList.length:e.markList.includes(t));var n}_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}getConversationProfile(t){return e(this,void 0,void 0,(function*(){let e;return e=this._convMap.has(t)?this._convMap.get(t):new N({conversationID:t,type:U(t)?n:s}),{code:0,data:{conversation:e}}}))}setMessageRead(t){return e(this,void 0,void 0,(function*(){console.log(`${this._name}.setMessageRead ok, ${JSON.stringify(t)}`)}))}getTotalUnreadCount(){const e=this._getLocalConvList();let t=0;return e.forEach((e=>{e.type!==i&&(""!==e.messageRemindType&&e.messageRemindType!==M||(t+=e.unreadCount))})),t}};return function(e){F.install(e)}}));