pope-test-lite-chat-test
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 11.3 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)["liteChatMessage-enhancer"]=t()}(this,(function(){"use strict";const e={deleteMessage:[{required:!0,rules:["array"],allowEmpty:!1}],revokeMessage:[{required:!0,rules:["object"],allowEmpty:!1}],resendMessage:[{key:"message",required:!0,rules:["object"],allowEmpty:!1},{key:"options",required:!1,rules:["object"],allowEmpty:!1}]},t={deleteMessage:!0,revokeMessage:!0,resendMessage:!0};function s(e,t,s,n){return new(s||(s=Promise))((function(o,r){function i(e){try{c(n.next(e))}catch(e){r(e)}}function a(e){try{c(n.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(i,a)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class n{constructor(e){this._core=e}deleteMessage(e){return s(this,void 0,void 0,(function*(){const{to:t,messageIdentifiers:s}=e,n="openim.delete_c2c_msg_ramble",o={From_Account:this._getCurrentUserId(),To_Account:t,MsgKeyList:s},r=this._core.helper.generateProtocolData({servcmd:n,data:o}),i=`${r.head.seq}${n}`;return this._core.channel.sendPacket(r,{requestId:i})}))}revokeMessage(e){return s(this,void 0,void 0,(function*(){const{to:t,from:s,sequence:n,time:o,random:r}=e,i="openim.msgwithdraw",a={MsgInfo:{From_Account:s,To_Account:t,MsgSeq:n,MsgRandom:r,MsgTimeStamp:o}},c=this._core.helper.generateProtocolData({servcmd:i,data:a}),l=`${c.head.seq}${i}`;return this._core.channel.sendPacket(c,{requestId:l})}))}_getCurrentUserId(){var e,t;return null===(t=null===(e=this._core)||void 0===e?void 0:e.store.get("login"))||void 0===t?void 0:t.userId}}class o{constructor(e){this._core=e}deleteMessage(e){return s(this,void 0,void 0,(function*(){const{to:t,messageIdentifiers:s}=e,n="group_open_http_svc.delete_group_ramble_msg_by_seq",o={GroupId:t,Deleter_Account:this._getCurrentUserId(),Seqs:s},r=this._core.helper.generateProtocolData({servcmd:n,data:o}),i=`${r.head.seq}${n}`;return this._core.channel.sendPacket(r,{requestId:i})}))}revokeMessage(e){return s(this,void 0,void 0,(function*(){const{to:t,sequence:s}=e,n="group_open_http_svc.group_msg_recall",o={GroupId:t,MsgSeqList:[{MsgSeq:s}]},r=this._core.helper.generateProtocolData({servcmd:n,data:o}),i=`${r.head.seq}${n}`;return this._core.channel.sendPacket(r,{requestId:i})}))}_getCurrentUserId(){var e,t;return null===(t=null===(e=this._core)||void 0===e?void 0:e.store.get("login"))||void 0===t?void 0:t.userId}}const r=2116;var i,a=new class{constructor(){this._core=null,this._c2cMessageAction=null,this._groupMessageAction=null}install(e){this._core=e,this._groupMessageAction=new o(e),this._c2cMessageAction=new n(e);const{notificationCenter:t,helper:s,InnerEvent:r}=e;t.subscribeInnerEvent(r.DESTROY,this._dispose,this),s.registerApi("deleteMessage",this),s.registerApi("revokeMessage",this),s.registerApi("resendMessage",this)}deleteMessage(e){return s(this,void 0,void 0,(function*(){let t=[],s=[];const{conversationID:n,conversationType:o}=e[0],i=n.replace(o,"");if("@TIM#SYSTEM"===o)throw new this._core.helper.ChatError({code:r});if(e.forEach((e=>{const{conversationID:r,conversationType:i,status:a,_onlineOnlyFlag:c,sequence:l,random:d,time:u}=e||{};if("success"===a&&r===n&&i===o){if(!c){const e="C2C"===i?`${l}_${d}_${u}`:String(l);t.push(e)}s.push(e)}})),0===t.length)return this._handleDeleteMessageSuccess(s),{code:0,data:{localMessagesToDelete:s}};t.length>30&&(t=t.slice(0,30),s=s.slice(0,30));try{return"C2C"===o?yield this._c2cMessageAction.deleteMessage({to:i,messageIdentifiers:t}):yield this._groupMessageAction.deleteMessage({to:i,messageIdentifiers:t}),this._handleDeleteMessageSuccess(s),{code:0,data:{localMessagesToDelete:s}}}catch(e){const{errorCode:t}=e;throw new this._core.helper.ChatError({functionName:"deleteMessage",code:t})}}))}revokeMessage(e){return s(this,void 0,void 0,(function*(){var t;const s=this._core.ssoLog.createSSOLogData({method:"revokeMessage"}),{conversationType:n,isRevoked:o}=e;let r=null;if("@TIM#SYSTEM"===n)throw new this._core.helper.ChatError({message:"system message cannot be revoked"});if(o)throw new this._core.helper.ChatError({message:"message has been revoked"});s.setLogInfo({message:`type:${e.type} from:${e.from} to:${e.to}`});try{if(r="C2C"===n?yield this._c2cMessageAction.revokeMessage(e):yield this._groupMessageAction.revokeMessage(e),r){const{RecallRetList:n}=r,o=(null===(t=null==n?void 0:n[0])||void 0===t?void 0:t.RetCode)||0;if(0!==o)throw new this._core.helper.ChatError({code:o});return s.end(),this._handleRevokeMessageSuccess(e),{code:0,data:{message:e}}}}catch(e){const{errorCode:t}=e,s=new this._core.helper.ChatError({functionName:"revokeMessage",code:t});throw this._core.ssoLog.getSSOLogData("revokeMessage").setError(s).end(),s}}))}resendMessage(e,t){return s(this,void 0,void 0,(function*(){var s;return e.isResend=!0,e.status="unSend",null===(s=this._core.message.messageSender)||void 0===s?void 0:s.sendMessage(e,t)}))}_handleDeleteMessageSuccess(e){0!==e.length&&(e.forEach((e=>{e.isDeleted=!0;const t=this._core.message.messageStore.getLocalMessageList(e.conversationID);null==t||t.forEach((t=>{t.ID===e.ID&&(t.isDeleted=!0)}))})),this._core.notificationCenter.emitInnerEvent(this._core.InnerEvent.MESSAGE_DELETED,e[0].conversationID))}_handleRevokeMessageSuccess(e){e.isRevoked=!0,this._core.notificationCenter.emitInnerEvent(this._core.InnerEvent.MESSAGE_REVOKED,[e])}_dispose(){const{notificationCenter:e,InnerEvent:t}=this._core;e.subscribeInnerEvent(t.DESTROY,this._dispose,this)}};!function(e){e.MSG_TEXT="TIMTextElem",e.MSG_CUSTOM="TIMCustomElem",e.MSG_LOCATION="TIMLocationElem",e.MSG_FACE="TIMFaceElem"}(i||(i={}));class c{static parseServerPushElement(e){const{MsgContent:t={Text:""}}=e,{Text:s}=t;return new c({text:s})}constructor(e){this.type="TIMTextElem",this.content={text:e.text||""}}validateBeforeSend(){var e,t;return(null===(t=null===(e=this.content)||void 0===e?void 0:e.text)||void 0===t?void 0:t.length)>0?{isValid:!0}:{isValid:!1,error:{message:"content can not be empty"}}}convertToBackendFormat(){const{text:e}=this.content;return{MsgType:this.type,MsgContent:{Text:e}}}}var l=new class{install(e){this._core=e,e.message.messageFactory.registerElementClass(i.MSG_TEXT,c),e.helper.registerApi("createTextMessage",this)}createTextMessage(e){var t,s;if(!e)return null;const n="string"==typeof e.payload?e.payload:(null===(t=null==e?void 0:e.payload)||void 0===t?void 0:t.text)||"",o=new c({text:n}),r=null===(s=this._core.store.get("login"))||void 0===s?void 0:s.userId,i=this._core.message.messageFactory.createMessage(Object.assign(Object.assign({},e),{from:r}));return i.setElement(o),i}};class d{constructor(e){this._core=e}generateRevokeMessage(e){const{conversationID:t,sequence:s,random:n,tinyID:o,clientTime:r,revokeReason:i,revoker:a=""}=e;let c=null;const{messageStore:l}=this._core.message;return c=l.revokeMessage({conversationID:t,sequence:s,random:n}),c||(c={conversationID:t,sequence:s},o&&r&&n&&(c.ID=`${o}-${r}-${n}`)),c=Object.assign(Object.assign({},c),{revoker:a,revokeReason:i,revokerInfo:{userID:a,nick:"",avatar:""}}),c}updateRevokerInfo(e){return s(this,void 0,void 0,(function*(){const t=e.map((e=>e.revoker)).filter((e=>!!e));try{const s=yield this._fetchUserInfos(t);s&&e.forEach((e=>{const{revoker:t}=e;s[t]&&(e.revokerInfo.nick=s[t].nick||"",e.revokerInfo.avatar=s[t].avatar||"")}))}catch(e){console.debug(e)}}))}_fetchUserInfos(e){return s(this,void 0,void 0,(function*(){var t,s;const n=yield null===(t=this._core.user.userProfile)||void 0===t?void 0:t.getUserProfile({userIDList:e});return(null==n?void 0:n.data)?null===(s=n.data)||void 0===s?void 0:s.reduce(((e,{userID:t,nick:s,avatar:n})=>(e[t]={nick:s||"",avatar:n||""},e)),{}):null}))}}class u{install(e){this._messageReceiverHelper=new d(e),this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:n},InnerEventSubType:{C2C_NOTIFY_MESSAGE:o}}=e;t.subscribeInnerEvent(s,o,this._handleC2CNotifyMessage,this),t.subscribeInnerEvent(n,this._dispose,this)}_handleC2CNotifyMessage(e){const{C2cNotifyMsgArray:t}=e;null==t||t.forEach((e=>{Object.keys(e).includes("WithdrawC2cMsgNotify")&&this._handleC2CRevokeMessage(e)}))}_handleC2CRevokeMessage(e){return s(this,void 0,void 0,(function*(){try{const{WithdrawC2cMsgNotify:{C2cWithdrawInfoArray:t}}=e,s=[],{notificationCenter:n,OuterEvent:o,InnerEvent:r}=this._core;t.forEach((e=>{const{MsgRand:t,MsgSeq:n,To_Account:o,From_Account:r,RevokerInfo:{Revoker_Account:i,Reason:a}}=e,c=this._getCurrentUserId()===r?`C2C${o}`:`C2C${r}`,l=this._messageReceiverHelper.generateRevokeMessage({conversationID:c,sequence:n,random:t,revoker:i,revokeReason:a});s.push(l)})),0!==s.length&&n.emitInnerEvent(r.MESSAGE_REVOKED,{revokedMessages:s,updateUnreadCount:!0}),yield this._messageReceiverHelper.updateRevokerInfo(s),n.emitOuterEvent(o.MESSAGE_REVOKED,{name:o.MESSAGE_REVOKED,data:s})}catch(e){console.debug(e)}}))}_getCurrentUserId(){var e,t;return null===(t=null===(e=this._core)||void 0===e?void 0:e.store.get("login"))||void 0===t?void 0:t.userId}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s},InnerEventSubType:{C2C_NOTIFY_MESSAGE:n}}=this._core;e.unSubscribeInnerEvent(t,n,this._handleC2CNotifyMessage,this),e.unSubscribeInnerEvent(s,this._dispose,this)}}class h{install(e){this._messageReceiverHelper=new d(e),this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:n},InnerEventSubType:{GROUP_NOTIFY_MESSAGE:o}}=e;t.subscribeInnerEvent(s,o,this._handleGroupNotifyMessage,this),t.subscribeInnerEvent(n,this._dispose,this)}_handleGroupNotifyMessage(e){const{GroupTips:t}=e;null==t||t.forEach((e=>{var t;Array.isArray(null===(t=null==e?void 0:e.MsgBody)||void 0===t?void 0:t.GroupWithdrawInfoArray)&&this._handleGroupRevokeMessage(e)}))}_handleGroupRevokeMessage(e){return s(this,void 0,void 0,(function*(){try{const{RevokerInfo:t,MsgBody:{GroupWithdrawInfoArray:s}}=e,n=[],{notificationCenter:o,OuterEvent:r,InnerEvent:i}=this._core;s.forEach((e=>{const{Random:s,MsgSeq:o,GroupId:r,MsgClientTime:i,TinyId:a,TopicId:c,RevokerInfo:{Revoker_Account:l=(null==t?void 0:t.Revoker_Account)||"",Reason:d=(null==t?void 0:t.Reason)||""}}=e,u=c?`GROUP${c}`:`GROUP${r}`,h=this._messageReceiverHelper.generateRevokeMessage({conversationID:u,sequence:o,random:s,tinyID:a,clientTime:i,revoker:l,revokeReason:d});n.push(h)})),0!==n.length&&o.emitInnerEvent(i.MESSAGE_REVOKED,{revokedMessages:n,updateUnreadCount:!0}),yield this._messageReceiverHelper.updateRevokerInfo(n),o.emitOuterEvent(r.MESSAGE_REVOKED,{name:r.MESSAGE_REVOKED,data:n})}catch(e){console.debug(e)}}))}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s},InnerEventSubType:{GROUP_NOTIFY_MESSAGE:n}}=this._core;e.unSubscribeInnerEvent(t,n,this._handleGroupNotifyMessage,this),e.unSubscribeInnerEvent(s,this._dispose,this)}}var v=new class{constructor(){this._c2cMessageReceiver=new u,this._groupMessageReceiver=new h}install(e){this._c2cMessageReceiver.install(e),this._groupMessageReceiver.install(e)}};return function(s){a.install(s),l.install(s),v.install(s),s.helper.registerValidateConfig({auth:t,params:e})}}));