jiuyiUniapp/jiuyi/node_modules/@tencentcloud/chat/modules/signaling-module.js

1 line
28 KiB
JavaScript

"use strict";const t=2,e=6,i=11,n=12,s=20,o=23,a=27;class r{constructor(t=0,e=0){this.high=t,this.low=e}equal(t){return null!==t&&this.low===t.low&&this.high===t.high}toString(){var t=Number(this.high).toString(16);let e=Number(this.low).toString(16);if(e.length<8){let t=8-e.length;for(;t;)e="0"+e,t--}return t+e}}const c={TEST:{CHINA:{DEFAULT:"wss://wss-dev.tim.qq.com"},OVERSEA:{DEFAULT:"wss://wss-dev.tim.qq.com"},SINGAPORE:{DEFAULT:"wss://wsssgp-dev.im.qcloud.com"},KOREA:{DEFAULT:"wss://wsskr-dev.im.qcloud.com"},GERMANY:{DEFAULT:"wss://wssger-dev.im.qcloud.com"},IND:{DEFAULT:"wss://wssind-dev.im.qcloud.com"},JPN:{DEFAULT:"wss://wssjpn-dev.im.qcloud.com"},USA:{DEFAULT:"wss://wssusa-dev.im.qcloud.com"},INDONESIA:{DEFAULT:"wss://wssidn-dev.im.qcloud.com"}},PRODUCTION:{CHINA:{DEFAULT0:"wss://*w4c.my-imcloud.com",DEFAULT:"wss://wss.im.qcloud.com",BACKUP:"wss://wss.tim.qq.com",STAT:"https://events.im.qcloud.com",ANYCAST:"wss://162.14.13.203"},OVERSEA:{DEFAULT0:"wss://*w4c.my-imcloud.com",DEFAULT:"wss://wss.im.qcloud.com",BACKUP:"wss://wss.my-imcloud.com",STAT:"https://api.my-imcloud.com"},SINGAPORE:{DEFAULT0:"wss://*w4s.my-imcloud.com",DEFAULT:"wss://wsssgp.im.qcloud.com",BACKUP:"wss://wsssgp.my-imcloud.com",STAT:"https://apisgp.my-imcloud.com",ANYCAST:"wss://162.14.19.159"},KOREA:{DEFAULT0:"wss://*w4k.my-imcloud.com",DEFAULT:"wss://wsskr.im.qcloud.com",BACKUP:"wss://wsskr.my-imcloud.com",STAT:"https://apikr.my-imcloud.com",ANYCAST:"wss://162.14.13.104"},GERMANY:{DEFAULT0:"wss://*w4g.my-imcloud.com",DEFAULT:"wss://wssger.im.qcloud.com",BACKUP:"wss://wssger.my-imcloud.com",STAT:"https://apiger.my-imcloud.com",ANYCAST:"wss://162.14.3.17"},IND:{DEFAULT0:"wss://*w4i.my-imcloud.com",DEFAULT:"wss://wssind.my-imcloud.com",BACKUP:"wss://wssind.im.qcloud.com",STAT:"https://apiind.my-imcloud.com",ANYCAST:"wss://162.14.18.188"},JPN:{DEFAULT0:"wss://*w4j.my-imcloud.com",DEFAULT:"wss://wssjpn.im.qcloud.com",BACKUP:"wss://wssjpn.my-imcloud.com",STAT:"https://apijpn.my-imcloud.com"},USA:{DEFAULT0:"wss://*w4u.my-imcloud.com",DEFAULT:"wss://wssusa.im.qcloud.com",BACKUP:"wss://wssusa.my-imcloud.com",STAT:"https://apiusa.my-imcloud.com",ANYCAST:"wss://162.14.10.42"},INDONESIA:{DEFAULT0:"wss://*w4y.my-imcloud.com",DEFAULT:"wss://wssidn.im.qcloud.com",BACKUP:"wss://wssidn.my-imcloud.com",STAT:"https://apiidn.my-imcloud.com",ANYCAST:"wss://43.129.34.169"}}},g={ANDROID:2,IOS:3,MAC:4,WEB:7,WX_MP:8,QQ_MP:9,TT_MP:10,BAIDU_MP:11,ALI_MP:12,IPAD:13,UNI_NATIVE_APP:15,DONUT_NATIVE_APP:19,NS_NATIVE_APP:20,RN_NATIVE_APP:21},l="CHINA",h={HOST:{CURRENT:{DEFAULT:"wss://wss.im.qcloud.com",STAT:"https://events.im.qcloud.com"},setCurrent(t=l){this.CURRENT=c.PRODUCTION[t]}},NAME:{OPEN_IM:"openim",OPEN_IM_MSG_EXT:"openim_msg_ext_http_svc",GRP:"group_open_http_svc",GRP_AV:"group_open_avchatroom_http_svc",GRP_COMMUNITY:"million_group_open_http_svc",GRP_ATTR:"group_open_attr_http_svc",FD:"sns",PROFILE:"profile",RECENT_CONTACT:"recentcontact",PIC:"openpic",BIG_GRP_NO_AUTH:"group_open_http_noauth_svc",BIG_GRP_POLLING:"group_open_long_polling_http_svc",BIG_GRP_POLLING_NO_AUTH:"group_open_long_polling_http_noauth_svc",IM_OPEN_STAT:"imopenstat",WEB_IM:"webim",IM_COS_SIGN:"im_cos_sign_svr",CUSTOM_UPLOAD:"im_cos_msg",HEARTBEAT:"heartbeat",IM_OPEN_PUSH:"im_open_push",IM_OPEN_STATUS:"im_open_status",IM_LONG_MSG:"im_long_msg",IM_CONFIG_MANAGER:"im_sdk_config_mgr",STAT_SERVICE:"StatSvc",OVERLOAD_PUSH:"OverLoadPush",IM_MSG_AUDIT_MGR:"im_msg_audit_mgr",TUIROOM_SVR:"tui_room_svr",IM_OPEN_TRANSLATE:"im_open_translate",IM_OPEN_SPEECH:"im_open_speech",MSG_SEARCH:"message_search",FOLLOW:"follow",OFFLINE_PUSH_REPORT:"offline_push_report",IM_MSG_LOGIC:"im_msg_db_logic"}},u=(new r(0,Math.pow(2,1)).toString(),new r(0,Math.pow(2,2)).toString(),new r(0,Math.pow(2,3)).toString(),new r(0,Math.pow(2,4)).toString(),new r(0,Math.pow(2,6)).toString(),new r(0,Math.pow(2,7)).toString(),new r(0,Math.pow(2,9)).toString(),new r(0,Math.pow(2,10)).toString(),new r(0,Math.pow(2,11)).toString(),new r(0,Math.pow(2,13)).toString(),new r(0,Math.pow(2,15)).toString(),new r(Math.pow(2,6)).toString(),new r(Math.pow(2,7)).toString(),new r(Math.pow(2,8)).toString(),new r(Math.pow(2,9)).toString(),new r(Math.pow(2,10)).toString(),new r(Math.pow(2,16)).toString(),new r(Math.pow(2,20)).toString(),h.HOST.setCurrent(l),"undefined"!=typeof wx&&"function"==typeof wx.getSystemInfoSync&&Boolean(wx.getSystemInfoSync().fontSizeSetting)),d=(u&&wx.createGamePortal,"undefined"!=typeof qq&&"function"==typeof qq.getSystemInfoSync&&Boolean(qq.getSystemInfoSync().fontSizeSetting)),_="undefined"!=typeof tt&&"function"==typeof tt.getSystemInfoSync&&Boolean(tt.getSystemInfoSync().fontSizeSetting),m="undefined"!=typeof swan&&"function"==typeof swan.getSystemInfoSync&&Boolean(swan.getSystemInfoSync().fontSizeSetting),I="undefined"!=typeof my&&"function"==typeof my.getSystemInfoSync&&Boolean(my.getSystemInfoSync().fontSizeSetting),f="undefined"!=typeof jd&&"function"==typeof jd.getSystemInfoSync,v="undefined"!=typeof uni&&"undefined"==typeof window&&"function"==typeof uni.requireNativePlugin,p=u&&"object"==typeof wx.miniapp,D=u||d||_||m||I||v||f,S="undefined"==typeof window&&!D&&"undefined"!=typeof global&&void 0!==global.NativeScriptGlobals,y="undefined"!=typeof global&&(void 0!==global.nativeModuleProxy||void 0!==global.ReactNative),M="undefined"!=typeof uni?!D:"undefined"!=typeof window&&!D&&!y,C=(d?qq:_?tt:m?swan:I?my:u?wx:v?uni:f&&jd,M&&window&&window.navigator&&window.navigator.userAgent||""),w=/(micromessenger|webbrowser)/i.test(C),T=function(){let t="WEB";return w?t="WEB":d?t="QQ_MP":_?t="TT_MP":m?t="BAIDU_MP":I?t="ALI_MP":u?t=p?"DONUT_NATIVE_APP":"WX_MP":v?t="UNI_NATIVE_APP":S?t="NS_NATIVE_APP":y&&(t="RN_NATIVE_APP"),g[t]}(),E=(!function(){var t=C.match(/OS (\d+)_/i);t&&t[1]&&t[1]}(),function(){var t,e,i=C.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i);i&&(t=i[1]&&parseFloat(i[1]),e=i[2]&&parseFloat(i[2]),t)&&e&&parseFloat(i[1]+"."+i[2])}(),/MSIE/.test(C)||-1<C.indexOf("Trident")&&-1<C.indexOf("rv:11.0"));let A,L;!function(){var t=/MSIE\s(\d+)\.\d/.exec(C),t=t&&parseFloat(t[1]);!t&&/Trident\/7.0/i.test(C)&&/rv:11.0/.test(C)}(),A="undefined"!=typeof console?console:"undefined"!=typeof global&&global.console?global.console:"undefined"!=typeof window&&window.console?window.console:{};const N=function(){},P=["assert","clear","count","debug","dir","dirxml","error","group","groupCollapsed","groupEnd","info","log","profile","profileEnd","table","time","timeEnd","timeStamp","trace","warn"];let U=P.length;for(;U--;)L=P[U],console[L]||(A[L]=N);var O=A;const $="TIMCustomElem",R="High",H="C2C",F="GROUP",b=function(){return(new Date).getTime()+0},x=function(){var t=new Date;return t.setTime(b()),t},G=function(){return Math.floor(b()/1e3)},q=Object.prototype.hasOwnProperty;function B(t){if(null==t)return!0;if("boolean"==typeof t)return!1;if("number"==typeof t)return 0===t;if("string"==typeof t)return 0===t.length;if("function"==typeof t)return 0===t.length;if(Array.isArray(t))return 0===t.length;if(t instanceof Error)return""===t.message;if(K(t)){for(const e in t)if(q.call(t,e))return!1;return!0}return!!(j(t)||k(t)||V(t))&&0===t.size}const j=function(t){return"map"===W(t)},k=function(t){return"set"===W(t)},V=function(t){return"file"===W(t)},K=function(t){if("object"!=typeof t||null===t)return!1;t=Object.getPrototypeOf(t);if(null===t)return!0;let e=t;for(;null!==Object.getPrototypeOf(e);)e=Object.getPrototypeOf(e);return t===e},J=function(t){return void 0===t},Y=function(t){return e=t,("function"==typeof Array.isArray?Array.isArray(e):"array"===W(e))||null!==t&&"object"==typeof t;var e},W=function(t){return Object.prototype.toString.call(t).match(/^\[object (.*)\]$/)[1].toLowerCase()};function z(){return!E&&!D}Date.now||(Date.now=function(){return(new Date).getTime()});let X=0;function Q(){return z()?"%c Chat %c":"Chat"}function Z(){var t=x();return t.toLocaleTimeString("en-US",{hour12:!1})+"."+function(t){let e;switch(t.toString().length){case 1:e="00"+t;break;case 2:e="0"+t;break;default:e=t}return e}(t.getMilliseconds())}const et={arguments2String(i){let n="";if(1===i.length)n=i[0];else for(let t=0,e=i.length;t<e;t++){if(Y(i[t]))try{n+=i[t]instanceof Error?JSON.stringify(i[t],["message","code"]):JSON.stringify(i[t])}catch(t){n+=t?t.message:"";break}else n+=i[t];n+=" "}return n},_exec(t,e){z()?O[t](Q(),"background:#0abf5b; padding:1px; border-radius:3px; color: #fff","background:transparent",Z(),e):O[t](`${Q()} ${Z()} `+e)},d:function(){var t;X<=-1&&(t=this.arguments2String(arguments),this._exec("debug",t))},l:function(){var t;X<=0&&(t=this.arguments2String(arguments),this._exec("log",t))},log:function(){var t;X<=0&&(t=this.arguments2String(arguments),this._exec("log",t))},i:function(){var t;X<=1&&(t=this.arguments2String(arguments),this._exec("info",t))},w:function(){var t;X<=2&&(t=this.arguments2String(arguments),this._exec("warn",t))},e:function(){var t;X<=3&&(t=this.arguments2String(arguments),this._exec("error",t))},setLevel:function(t){t<4&&this._exec("log","set level from "+X+" to "+t),X=t},getLevel:function(){return X}};class it extends Error{constructor(t){super();var{code:t,message:e,data:i}=t;this.code=t,e?this.message=e:this._getErrMsg&&(this.message=this._getErrMsg(this.code)),this.data=i||{}}}const nt=2903,st=3122,ot=8010,at=8011,rt=8020,ct="error";let gt=null;const lt=function(t,e=!1){if(t instanceof it)return e&&null!==gt&&gt.emit(ct,t),Promise.reject(t);if(t instanceof Error){const t=new it({code:nt});return e&&null!==gt&&gt.emit(ct,t),Promise.reject(t)}return J(t)||J(t.code)?Promise.reject(new it({code:nt})):(t=new it(t),e&&null!==gt&&gt.emit(ct,t),Promise.reject(t))},ht="newInvitationReceived",ut="ts_invitee_accepted",dt="ts_invitee_rejected",_t="ts_invitation_cancelled",mt="ts_invitation_timeout",It="ts_invitation_modified",ft=1,vt=2,pt=3,Dt=4,St=5;class yt{constructor(t){this._n="RemoteSignalingHandler",this._sigM=t}onNewMessageList(t){t.forEach(t=>{var e=this.getPayloadData(t);e&&this._handleActionType(e,t)})}onMessageModified(t){t.forEach(t=>{var e=this.getPayloadData(t);e&&this._onInvitationModified(e,t)})}getPayloadData(e){var i=this._n+".getPayloadData",e=e.payload["data"];try{return JSON.parse(e)}catch(t){return et.e(i+" JSON parse error. signalingData:"+e),null}}_handleActionType(t,e){var i=t["actionType"];switch(i){case ft:this._onNewInvitationReceived(t,e);break;case Dt:this._onInviteeRejected(t);break;case pt:this._onInviteeAccepted(t);break;case vt:this._onInvitationCancelled(t);break;case St:this._onInvitationTimeout(t)}}_genBaseEmitData(t){var{inviteID:t,inviter:e,groupID:i,data:n}=t;return{inviteID:t,inviter:e,groupID:i,data:n||""}}_onNewInvitationReceived(t,e){const i=this._n+"._onNewInvitationReceived",{inviteID:n,inviteeList:s,groupID:o,inviter:a}=t,r=this._sigM.getMyUserID(),g=s.includes(r);let l=t.timeout;var c=(x().getTime()-1e3*e.time)/1e3,h=(0<l&&0<c&&l>c&&(l-=c),i+` myselfIncluded:${g} groupID:${o} signalObj:`+JSON.stringify(t));if(et.l(h+` timeout:${l}s delta:${c}s`),o&&g||!o){const i=this._sigM.getInviteInfo(n);i&&i===t||(i||this._sigM.setInviteInfo(n,{...t,message:e}),this._sigM.emitEvent(ht,{...this._genBaseEmitData(t),inviteeList:s}),a!==r&&this._sigM.startTimer({...t,timeout:l}))}}_onInviteeRejected(t){var e=this._n+"._onInviteeRejected",{inviteID:i,inviter:n,groupID:s}=t,o=this._sigM.hasInviteInfo(i);et.l(e+` inviteID:${i} hasInviteID:${o} inviter:${n} groupID:`+s),o&&(this._sigM.updateInviteInfo(t),this._sigM.emitEvent(dt,{...this._genBaseEmitData(t),invitee:t.inviteeList[0]}))}_onInviteeAccepted(t){var e=this._n+"._onInviteeAccepted",{inviteID:i,inviter:n,groupID:s}=t,o=this._sigM.hasInviteInfo(i);et.l(e+` inviteID:${i} hasInviteID:${o} inviter:${n} groupID:`+s),o&&(this._sigM.updateInviteInfo(t),this._sigM.emitEvent(ut,{...this._genBaseEmitData(t),invitee:t.inviteeList[0]}))}_onInvitationCancelled(t){var e=this._n+"._onInvitationCancelled",{inviteID:i,inviter:n,groupID:s}=t,o=this._sigM.hasInviteInfo(i);et.l(e+` inviteID:${i} hasInviteID:${o} inviter:${n} groupID:`+s),o&&(this._sigM.deleteInviteInfo(i),this._sigM.emitEvent(_t,this._genBaseEmitData(t)))}_onInvitationTimeout(t){var e=this._n+"._onInvitationTimeout",{inviteID:i,inviter:n,groupID:s,inviteeList:o}=t,a=this._sigM.hasInviteInfo(i);et.l(e+` inviteID:${i} hasInviteID:${a} inviter:${n} groupID:${s} data:`+t.data),a&&(this._sigM.updateInviteInfo(t),this._sigM.emitEvent(mt,{...this._genBaseEmitData(t),inviteeList:o,isSelfTimeout:!1}))}_onInvitationModified(t,e){var i=this._n+"._onInvitationModified",{inviteID:n,data:s}=t;et.l(i+` inviteID:${n} data:`+s),this._sigM.setInviteInfo(n,{...t,message:e}),this._sigM.emitEvent(It,{inviteID:n,data:s})}}const Mt=function(t){var e;return t<0||53<t?NaN:(e=0|1073741824*Math.random(),30<t?e+1073741824*(0|Math.random()*(1<<t-30)):e>>>30-t)},Ct=function(t,e){let i=t.toString(16),n=e-i.length,s="0";for(;0<n;n>>>=1,s+=s)1&n&&(i=s+i);return i};class wt{constructor(t){this._n="LocalSignalingHandler",this._sigM=t}generateInviteID(){t=Mt;var t,e=(e=Ct)(t(32),8)+"-"+e(t(16),4)+"-"+e(16384|t(12),4)+"-"+e(32768|t(14),4)+"-"+e(t(48),12);return et.l(this._n+".generateInviteID inviteID:"+e),e}createInviteInfo(t){var e=this.generateInviteID(),t=this.createInviteCustomData({...t,inviteID:e}),{groupID:i,inviteeList:n}=t,i=i||n[0];return{customData:t,message:this._sigM.createSignaling(t,i),inviteID:e}}_genBaseCustomData(t){var{data:t="",inviteID:e="",groupID:i=""}=t;return{businessID:1,timeout:0,data:t,inviteID:e,groupID:i}}createInviteCustomData(t){var{userID:e,timeout:i=0,groupID:n=""}=t,s=this._sigM.getMyUserID(),s={...this._genBaseCustomData(t),actionType:ft,inviter:s,inviteeList:n?t.inviteeList:[e],timeout:i};return et.l(this._n+".createInviteCustomData customData:",s),s}createCancelCustomData(t){var e=this._n+".createCancelCustomData",i=t["inviteID"];let n;var s=this._sigM.getMyUserID(),{inviteeList:i,groupID:o,inviter:a}=this._sigM.getInviteInfo(i);return a===s?n={...this._genBaseCustomData(t),actionType:vt,groupID:o,inviter:s,inviteeList:i}:et.e(e+` unmatched inviter:${a} and my userID:`+s),et.l(e+" customData:",n),n}createAcceptCustomData(t){var e=this._n+".createAcceptCustomData",i=t["inviteID"];let n;var s=this._sigM.getMyUserID(),{inviter:o,groupID:a,inviteeList:r}=this._sigM.getInviteInfo(i);return r.includes(s)?n={...this._genBaseCustomData(t),actionType:pt,groupID:a,inviter:o,inviteeList:[s]}:et.e(e+` userID:${s} not in inviteeList. inviteID:${i} groupID:`+a),et.l(e+" customData:",n),n}createRejectCustomData(t){var e=this._n+".createRejectCustomData",i=t["inviteID"];let n;var s=this._sigM.getMyUserID(),{inviter:o,groupID:a,inviteeList:r}=this._sigM.getInviteInfo(i);return r.includes(s)?n={...this._genBaseCustomData(t),actionType:Dt,groupID:a,inviter:o,inviteeList:[s]}:et.e(e+` userID:${s} not in inviteeList. inviteID:${i} groupID:`+a),et.l(e+" customData:",n),n}createTimeoutCustomData(t){var e=this._n+".createTimeoutCustomData",{inviteeList:i,inviter:n,isInviter:s=!1}=t,o=this._sigM.getMyUserID(),t={...this._genBaseCustomData(t),actionType:St,inviter:n,inviteeList:s?i:[o]};return et.l(e+" customData:",t),t}}class Tt{constructor(t){this._n="HistorySignalingHandler",this._sigM=t,this.COUNT=20,this.EXPIRED_TIME=300,this._map=new Map,this._relatedToMeMap=new Map}setCloudConfig(t=20,e=300){this.COUNT=t,this.EXPIRED_TIME=e,et.l(this._n+`.setCloudConfig count:${t}, time:`+e)}getHistorySignaling(){var t=this._sigM.get(i).getLocalConvList();B(t)||(this._getC2CSignalingList(),t=this._getValidGroupConvList(t),this._getGroupSignalingList(t).then(t=>{this._handleSignalingList(t)}))}_getC2CSignalingList(){var t=this._sigM.get(e).getMessageListFromUnreadDB(),t=this._sigM.filterMessageList(t);this._getRelatedToMeMap(t)}_getGroupSignalingList(t){t=this._createPromiseList(t);return 0===t.length?Promise.resolve(this._sortSignaling(this._relatedToMeMap)):this._concurrentGetMessageList(t).then(t=>{let e=new Map;return t.forEach(t=>{t=t.list,t=this._getRelatedToMeMap(t);e=new Map([...e,...t])}),this._sortSignaling(e)})}_handleSignalingList(t){B(t)||this._sigM.onNewMessageList(t)}_getValidGroupConvList(i){var n=[];for(let t=0,e=i.length;t<e;t++){var{type:s,unreadCount:o,lastMessage:a}=i[t],s=s===F,a=this._isNotExpired(a);s&&o&&a&&n.push(i[t])}return n}_isNotExpired(t){return!(!t||!t.lastTime)&&t.lastTime>G()-this.EXPIRED_TIME}_createPromiseList(e){var n=[];for(let t=0;t<e.length;t++){var{conversationID:s,unreadCount:o}=e[t],o=o<this.COUNT?o:this.COUNT,o=(this._map.set(s,{msgCount:o,list:[]}),this._sigM.get(i).getMessageList({conversationID:s}));n.push(o)}return n}_concurrentGetMessageList(t){const s=[];return Promise.all(t).then(e=>{for(let t=0;t<e.length;t++){var{code:i,data:n}=e[t];0===i&&0!==n.messageList.length&&(this._handleMessageList(n.messageList),i=this._relayGetMessageList(n))&&s.push(i)}return 0<s.length?this._concurrentGetMessageList(s):this._map})}_relayGetMessageList(t){var e,{messageList:t,nextReqMessageID:n,isCompleted:s}=t;return 0===t.length||(t=t[0].conversationID,e=this._map.get(t).msgCount,0===e)||s?null:this._sigM.get(i).getMessageList({conversationID:t,nextReqMessageID:n,count:e})}_handleMessageList(t){var e=t.length,i=t[0]["conversationID"],{msgCount:n,list:s}=this._map.get(i);this._map.set(i,{msgCount:0<n-e?n-e:0,list:s.concat(this._sigM.filterMessageList(t))})}_getRelatedToMeMap(e){for(let t=0;t<e.length;t++){var i=e[t];this._saveRelatedToMe(i)}return this._relatedToMeMap}_saveRelatedToMe(t){var{actionType:e="",inviteID:i=""}=this._sigM.getPayloadData(t)||{};switch(e){case ft:this._setHistoryInvite(t);break;case Dt:case pt:this._updateHistoryInvite(t);break;case vt:this._delHistoryInvite(i);break;case St:this._updateHistoryInvite(t)}}_setHistoryInvite(t){var e=this._sigM.getPayloadData(t)||{},{inviteID:i="",inviteeList:n=[],timeout:s=0}=e,o=this._sigM.getMyUserID();n.includes(o)&&(n=G()-t.time,0<s&&s<n&&0!==s||this._relatedToMeMap.set(i,{...e,messageList:[t]}))}_delHistoryInvite(t){this._relatedToMeMap.has(t)&&this._relatedToMeMap.delete(t)}_updateHistoryInvite(t){const e=this._sigM.getPayloadData(t)||{},{inviteID:i="",inviteeList:n=[]}=e;if(this._relatedToMeMap.has(i)){const{inviteeList:e,messageList:s}=this._relatedToMeMap.get(i);for(let t=0;t<n.length;t++){const i=n[t];e.includes(i)&&e.splice(e.indexOf(i),1)}0===e.length?this._delHistoryInvite(i):s.push(t)}else this._delHistoryInvite(i)}_sortSignaling(t){let e=[];return t.forEach(t=>{e=[...e,...t.messageList]}),e.sort((t,e)=>t.time-e.time)}reset(){this._map.clear(),this._relatedToMeMap.clear()}}class Et{constructor(t,e){this.businessID=t.businessID||1,this.inviteID=t.inviteID,this.groupID=t.groupID||"",this.inviter=t.inviter||"",this.inviteeList=t.inviteeList||[],this.data=t.data||"",this.actionType=t.actionType||ft,this.timeout=t.timeout||0}}const At={A2KEY_AND_TINYID_UPDATED:"_inner1",CLOUD_CONFIG:"_inner2",PROFILE_UPDATED:"_inner3",CONV_SYNC_COMPLETED:"_inner4",C2C_UNREAD_HANDLE_COMPLETED:"_inner5"};class Lt extends class{constructor(t){this._m=t,this._n=""}isLoggedIn(){return this._m.get(n).isLoggedIn()}isOversea(){return this._m.get(n).isOversea()}isPrivateNetWork(){var t=this._m.get(n);return t.isPrivateNetWork()&&!t.getFileDownloadProxy()}getFileDownloadProxy(){return this._m.get(n).getFileDownloadProxy()}getDowloadFileAuthKey(){return this._m.get(n).getDowloadFileAuthKey()}getMyUserID(){return this._m.get(n).getUserID()}getMyTinyID(){return this._m.get(n).getTinyID()}getSDKAppID(){return this._m.get(n).getSDKAppID()}isIntl(){return this._m.get(n).isIntl()}isUsingChatCore(){return this._m.get(n).isUsingChatCore()}isDevMode(){return this._m.get(n).isDevMode()}get(t){return this._m.get(t)}getPlatform(){return T}getCloudConfig(t){return this._m.get(o).getCloudConfig(t)}emitOEvt(t,e){this._m.getOEmitInst().emit(t,e)}emitIEvt(t,e){this._m.getIEmitInst().emit(t,e)}getIEmitInst(){return this._m.getIEmitInst()}req(t){return this._m.get(s).req(t)}canIUse(t){return this._m.get(a).canIUse(t)}getErrMsg(t,e,i){return this._m.getErrMsg(t,e,i)}warn(t,e,i){t=this.getErrMsg(t,e,i);t&&et.w(t)}noUse(t){var e=st;return lt({code:e,message:this.getErrMsg(e,t)})}}{constructor(t){super(t),this._n="SignalingModule",this._inviteInfoMap=new Map,this._canIUseSignaling=!1,this._isHandling=!1,this._remoteSignalingHandler=new yt(this),this._localSignalingHandler=new wt(this),this._historySignalingHandler=new Tt(this),this._isC2CUnreadHandleCompleted=!1,this._isConvSyncCompleted=!1,this._isSyncCompleted=!1,this._isCloudConfigCompleted=!1;t=this.getIEmitInst();t.on(At.C2C_UNREAD_HANDLE_COMPLETED,this.onC2CUnreadHandleCompleted,this),t.on(At.CONV_SYNC_COMPLETED,this.onConvSyncCompleted,this),t.on(At.CLOUD_CONFIG,this.onCloudConfig,this)}onC2CUnreadHandleCompleted(){this._isC2CUnreadHandleCompleted=!0,this._isCloudConfigCompleted&&this._isConvSyncCompleted&&!this._isSyncCompleted&&this.onReady()}onConvSyncCompleted(){this._isConvSyncCompleted=!0,this._isC2CUnreadHandleCompleted&&this._isCloudConfigCompleted&&!this._isSyncCompleted&&this.onReady()}onCloudConfig(){this._isCloudConfigCompleted=!0;let t=this.getCloudConfig("history_s_count"),e=this.getCloudConfig("history_s_time");J(t)||(t=Number(t)),J(e)||(e=Number(e)),this._historySignalingHandler.setCloudConfig(t,e),this._isC2CUnreadHandleCompleted&&this._isConvSyncCompleted&&!this._isSyncCompleted&&this.onReady()}_isListenerExisted(){return-1<this._m.getOEmitInst().eventNames().indexOf(ht)}onReady(){this._isSyncCompleted=!0;var t=this._isListenerExisted();et.l(this._n+".onReady. isListenerExisted: "+t),t&&this._historySignalingHandler.getHistorySignaling()}onNewMessageList(t){t=this.filterMessageList(t);if(0<t.length)return this._remoteSignalingHandler.onNewMessageList(t)}onMessageModified(t){t=this.filterMessageList(t);if(0<t.length)return this._remoteSignalingHandler.onMessageModified(t)}hasInviteInfo(t){return this._inviteInfoMap.has(t)}getInviteInfo(t){return this._inviteInfoMap.get(t)}setInviteInfo(t,e){const{message:i,...n}=e;et.l(this._n+`.setInviteInfo inviteID:${t} data:`,n),this._inviteInfoMap.set(t,{...n,message:i})}deleteInviteInfo(t){this.hasInviteInfo(t)&&(et.l(this._n+`.deleteInviteInfo inviteID:${t}.`),this._inviteInfoMap.delete(t))}updateInviteInfo(t){const e=this._n+".updateInviteInfo",{inviteID:i,inviter:n,inviteeList:s,groupID:o}=t;if(et.l(e+` inviteID:${i} inviter:${n} groupID:`+o),o&&this.hasInviteInfo(i)){const t=s[0],n=this.getInviteInfo(i)["inviteeList"];n.includes(t)&&(n.splice(n.indexOf(t),1),et.l(e+` remove ${t}. localInviteeList.length:`+n.length)),0===n.length&&this.deleteInviteInfo(i)}else this.deleteInviteInfo(i)}canIUseSignaling(){return this._canIUseSignaling}emitEvent(t,e){this.emitOEvt(t,e)}addSignalingListener(t,e,i){this._canIUseSignaling||(this._canIUseSignaling=!0),this._m.getOEmitInst().on(t,e,i)}removeSignalingListener(t,e,i){this._m.getOEmitInst().off(t,e,i),this._isListenerExisted()||(this._canIUseSignaling=!1)}invite(t){const e=this._n+".invite",{message:i,customData:n,inviteID:s}=this._localSignalingHandler.createInviteInfo(t);return et.l(e+` options:${JSON.stringify(t)} inviteID:`+s),this.sendSignaling(i,t).then(t=>t&&0===t.code?(this.setInviteInfo(s,{...n,message:i}),this.startTimer({...n,inviteID:s}),{...t,inviteID:s}):t).catch(t=>lt(t))}inviteSync(t,e,i){const n=this._n+".inviteSync",{message:s,customData:o,inviteID:a}=this._localSignalingHandler.createInviteInfo(t);return et.l(n+` options:${JSON.stringify(t)} inviteID:`+a),this.sendSignaling(s,t).then(t=>{if(t&&0===t.code)return this.setInviteInfo(a,{...o,message:s}),this.startTimer({...o,inviteID:a}),e&&e({inviteID:a}),{inviteID:a};i&&i(0===t.code,t.message||"")}).catch(t=>(i&&i(t.code,t.message),lt(t))),a}_handleImResponse(t,e,i){e&&0===e.code&&(this._isHandling=!1,i?this.deleteInviteInfo(t.inviteID):this.updateInviteInfo(t))}cancel(e){var t,i=this._n+".cancel";if(et.l(i+" options:"+JSON.stringify(e)),!this.hasInviteInfo(e.inviteID)||this._isHandling)return lt({code:rt});this._isHandling=!0;const n=this._localSignalingHandler.createCancelCustomData(e);return n?({groupID:i,inviteeList:t}=n,i=i||t[0],t=this.createSignaling(n,i),this.sendSignaling(t,e).then(t=>(this._handleImResponse(n,t,!0),0===t.code?{...t,inviteID:e.inviteID}:t)).catch(t=>lt(t))):(this._isHandling=!1,lt({code:at}))}accept(e){var t=this._n+".accept";if(et.l(t+" options:"+JSON.stringify(e)),!this.hasInviteInfo(e.inviteID)||this._isHandling)return lt({code:ot});this._isHandling=!0;const i=this._localSignalingHandler.createAcceptCustomData(e);return i?(t=this.createSignaling(i),this.sendSignaling(t,e).then(t=>(this._handleImResponse(i,t),0===t.code?{...t,inviteID:e.inviteID}:t)).catch(t=>lt(t))):(this._isHandling=!1,lt({code:at}))}reject(e){var t=this._n+".reject";if(et.l(t+" options:"+JSON.stringify(e)),!this.hasInviteInfo(e.inviteID)||this._isHandling)return lt({code:ot});this._isHandling=!0;const i=this._localSignalingHandler.createRejectCustomData(e);return i?(t=this.createSignaling(i),this.sendSignaling(t,e).then(t=>(this._handleImResponse(i,t,!0),0===t.code?{...t,inviteID:e.inviteID}:t)).catch(t=>lt(t))):(this._isHandling=!1,lt({code:at}))}getSignalingInfo(t){const e=this._n+".getSignalingInfo",{ID:i,from:n,to:s}=t,o=this._filterSignaling(t);let a=null;if(o){const e=this.getPayloadData(t);a=new Et(e)}t=o?"actionType:"+a.actionType:"";return et.l(e+` messageID:${i} from:${n} to:${s} ${t} isSignaling:`+o),a}modifyInvitation(e){const{inviteID:i,data:n}=e;if(!this.hasInviteInfo(e.inviteID)||this._isHandling)return lt({code:ot});this._isHandling=!0;const{message:s,...o}=this.getInviteInfo(i),a=s.payload.data;return o.data=n,s.payload.data=JSON.stringify(o),this.get(t).modifyRemoteMessage(s).then(t=>(this.setInviteInfo(i,{...o,message:s}),this._isHandling=!1,t)).catch(t=>(this._isHandling=!1,s.payload.data=a,lt(t)))}_genMsgCtrlInfo(t={}){var{data:e="",onlineUserOnly:t,inviteID:i="",offlinePushInfo:n,actionType:s}=t;let o={_onlineOnlyFlag:!1};i={onlineUserOnly:(o=i&&this.getInviteInfo(i)?this.getInviteInfo(i).message:o)._onlineOnlyFlag||t||!1,offlinePushInfo:n,messageControlInfo:{excludedFromContentModeration:!0,excludedFromUnreadCount:!1,excludedFromLastMessage:!1}};if(s===St){const t=!!e.match(/excludeTimeoutSignalingFromHistoryMessage/);i.messageControlInfo.excludedFromUnreadCount=t,i.messageControlInfo.excludedFromLastMessage=t}else{t=!!e.match(/excludeFromHistoryMessage/),n=!!e.match(/excludeOriginalSignalingFromHistoryMessage/);i.messageControlInfo.excludedFromUnreadCount=t||n,i.messageControlInfo.excludedFromLastMessage=t||n}return i}sendSignaling(e,i){return this.get(t).sendMessageInstance(e,this._genMsgCtrlInfo(i)).catch(t=>(this._isHandling=!1,lt(t)))}filterMessageList(t){return t.filter(t=>this._filterSignaling(t))}getPayloadData(t){return this._remoteSignalingHandler.getPayloadData(t)}createSignaling(e,i){var{groupID:n,inviter:s}=e,i={to:i||n||s,conversationType:n?F:H,priority:R,payload:{data:JSON.stringify(e)}},s=this.get(t).createCustomMessage(i);return et.l(this._n+".createSignaling. message:",s),s}_filterSignaling(t){let e=!1;var i,n;return t.type&&t.type===$&&({cloudCustomData:t="",payload:{data:n=""}}=t,t=t.match(/"type":"tsignaling"/),i=n.match(/inviteID/),n=n.match(/actionType/),e=t||i&&n),!!e}startTimer(i){const n=this._n+".startTimer",{timeout:t,inviteID:s,inviter:e,groupID:o}=i,a=e===this.getMyUserID();if(et.l(n+` timeout:${t} isInviter:${a} groupID:`+o),!(t<=0)){const r=a?t+5:t;let e=1;const g=setInterval(()=>{var t=this._hasLocalInviteInfo(i,a);e<r&&t?++e:(t&&this._sendTimeoutNotice(s,a),et.l(n+" end."),clearInterval(g))},1e3)}}_hasLocalInviteInfo(t,e){var i,n,{inviteID:t,groupID:s}=t;return!!this.hasInviteInfo(t)&&(i=this._n+"._hasLocalInviteInfo",n=this.getInviteInfo(t)["inviteeList"],et.l(i+` inviteID:${t} inviteeList:${n} groupID:`+s),!s||(e?0<n.length:0<n.length&&n.includes(this.getMyUserID())))}_getReceiver(t,e){var{groupID:e,inviteeList:i,inviter:n}=e;return t?e||i[0]:e||n}_sendTimeoutNotice(s,o){var t=this.getInviteInfo(s),e=this._getReceiver(o,t);et.l(this._n+`._sendTimeoutNotice inviteID:${s} to:${e} isInviter:`+o);const a=this._localSignalingHandler.createTimeoutCustomData({...t,isInviter:o}),r=this.createSignaling(a,e);return this.sendSignaling(r,a).then(t=>{if(t&&0===t.code){const{data:t,groupID:e,inviteeList:i,inviter:n}=a;this.emitEvent(mt,{data:t,groupID:e,inviteID:s,inviteeList:i,inviter:n,isSelfTimeout:!0,message:r}),o?this.deleteInviteInfo(s):this.updateInviteInfo(a)}})}reset(){et.l(this._n+".reset"),this._inviteInfoMap.clear(),this._canIUseSignaling=!1,this._isHandling=!1,this._historySignalingHandler.reset(),this._isC2CUnreadHandleCompleted=!1,this._isConvSyncCompleted=!1,this._isSyncCompleted=!1,this._isCloudConfigCompleted=!1}}export{Lt as default};