UNPKG

pope-test-lite-chat-api

Version:

Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut

3 lines (2 loc) 12.7 kB
"use strict";function e(e,t,i,n){return new(i||(i=Promise))((function(s,r){function o(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?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(o,a)}c((n=n.apply(e,t||[])).next())}))}Object.defineProperty(exports,"__esModule",{value:!0}),"function"==typeof SuppressedError&&SuppressedError;const t=[{cmd:"im_open_status.ws_get_user_status",interval:5,limit:20},{cmd:"im_open_status.ws_status_subscribe",interval:5,limit:20},{cmd:"im_open_status.ws_status_unsubscribe",interval:5,limit:20},{cmd:"group_open_http_svc.get_group_self_member_info",interval:5,limit:20},{cmd:"group_open_http_svc.modify_group_base_info",interval:1,limit:8},{cmd:"group_open_http_svc.get_members",interval:3,limit:1},{cmd:"group_open_http_svc.get_pendency",interval:1,limit:15},{cmd:"group_open_http_svc.set_group_attr",interval:5,limit:10},{cmd:"group_open_http_svc.modify_group_attr",interval:5,limit:10},{cmd:"group_open_http_svc.delete_group_attr",interval:5,limit:10},{cmd:"group_open_http_svc.clear_group_attr",interval:5,limit:10},{cmd:"group_open_http_svc.get_group_attr",interval:5,limit:20},{cmd:"group_open_http_svc.update_group_counter",interval:5,limit:20},{cmd:"group_open_http_svc.get_group_counter",interval:5,limit:20},{cmd:"million_group_open_http_svc.get_topic",interval:1,limit:10},{cmd:"openim.read_all_unread_msg",interval:1,limit:1},{cmd:"message_search.query",interval:5,limit:20},{cmd:"message_search.query_grp",interval:5,limit:20},{cmd:"message_search.query_grp_member",interval:5,limit:20},{cmd:"message_search.query_user",interval:5,limit:20}],i={login:"im_open_status.wslogin",logout:"im_open_status.wslogout",sendMessage:"openim.sendmsg",modifyMessage:"openim.modify_c2c_msg",getMyProfile:"profile.portrait_get_all",getUserProfile:"profile.portrait_get_all",updateMyProfile:"profile.portrait_set",setSelfStatus:"im_open_status.ws_set_custom_status",getUserStatus:"im_open_status.ws_get_user_status",getFriendProfile:"sns.friend_get_specified"},n="im_sdk_config_mgr.fetch_config",s="im_sdk_config_mgr.push_configv2",r="cloud-config";var o=new class{constructor(){this._core=null,this._expirationTime=0,this._version=0,this._isFetching=!1,this._cmdFrequencyLimitMap=new Map,this._methodCallFrequencyMap=new Map}install(e){this._core=e;const{notificationCenter:i,InnerEvent:n,helper:r}=e;i.subscribeInnerEvent(s,this._handlePushedConfig,this),i.subscribeInnerEvent(n.LOGIN,this._handleLoginSuccess,this),i.subscribeInnerEvent(n.LOGOUT,this._reset,this),i.subscribeInnerEvent(n.DESTROY,this._dispose,this),r.registerExperimentalAPI("getServerConfig",this),r.registerInterceptor(this.checkMethodCallOverLimit,this),this._updateCmdFreqLimitMap(t)}getServerConfig(e){var t;const i={code:0,data:""};return e?(i.data=(null===(t=this._core.store.get("cloudConfig"))||void 0===t?void 0:t[e])||"",i):i}checkMethodCallOverLimit(e){const t=i[e];if(!this._cmdFrequencyLimitMap.has(t))return;if(!this._methodCallFrequencyMap.has(t))return void this._methodCallFrequencyMap.set(t,{startTime:Date.now(),methodCallCounter:1});const{limit:n,interval:s}=this._cmdFrequencyLimitMap.get(t);let{startTime:r,methodCallCounter:o}=this._methodCallFrequencyMap.get(t);if(Date.now()-r>1e3*s)this._methodCallFrequencyMap.set(t,{startTime:Date.now(),methodCallCounter:1});else if(o+=1,this._methodCallFrequencyMap.set(t,{startTime:r,methodCallCounter:o}),o>n)throw new this._core.helper.ChatError({functionName:e,message:"frequency control limit exceeded,"})}_fetchCloudConfig(){return e(this,void 0,void 0,(function*(){try{const{sdkAppId:e}=this._core.store.get("instance")||{},t=this._core.helper.generateProtocolData({servcmd:n,data:{uint32_sdkappid:e,uint64_version:this._version}}),i=`${t.head.seq}${n}`;return yield this._core.channel.sendPacket(t,{requestId:i})}catch(e){throw console.warn("fetch cloudControl fail: ",e),e}}))}_handlePushedConfig(e){this._updateCloudConfig(e)}_handleLoginSuccess(){return e(this,void 0,void 0,(function*(){try{if(this._canFetch()){const e=yield this._fetchCloudConfig();this._updateCloudConfig(e)}this._core.helper.taskScheduler.addTask({id:r,intervalMs:1e3,callback:this._fetchCloudConfigIfReady,context:this})}catch(e){console.warn(e)}}))}_fetchCloudConfigIfReady(){return e(this,void 0,void 0,(function*(){if(this._canFetch())try{const e=yield this._fetchCloudConfig();this._updateCloudConfig(e)}catch(e){console.warn(e)}}))}_updateCloudConfig(e){const t=this._parseCloudConfig(e);t&&(this._core.store.set("cloudConfig",t),this._parseCmdFreqLimit(),this._core.notificationCenter.emitInnerEvent(this._core.InnerEvent.CLOUD_CONFIG_UPDATE,t))}_canFetch(){const{isLoggedIn:e}=this._core.store.get("login")||{};return e&&!this._isFetching&&Date.now()>=this._expirationTime}_parseCloudConfig(e){const{int32_error_code:t,str_error_message:i,str_json_config:n,uint32_expired_time:s,uint32_sdkappid:r,uint64_version:o}=e;let a=null;if(0===t){if(this._version!==o)try{a=JSON.parse(n),this._version=o}catch(e){}this._expirationTime=Date.now()+1e3*s}else this._expirationTime=void 0===t?Date.now()+36e5:Date.now()+12e4;return a}_parseCmdFreqLimit(){var e;let t=null===(e=this.getServerConfig("cmd_frequency_limit"))||void 0===e?void 0:e.data;if(void 0!==typeof t&&this._core.helper.isEmpty(t))try{t=JSON.parse(t),this._updateCmdFreqLimitMap(t)}catch(e){console.warn(e)}}_updateCmdFreqLimitMap(e){e.forEach((e=>{this._cmdFrequencyLimitMap.set(e.cmd,{interval:e.interval,limit:e.limit})}))}_reset(){this._core.helper.taskScheduler.removeTask(r),this._core.store.clear("cloudConfig"),this._updateCmdFreqLimitMap(t),this._methodCallFrequencyMap.clear(),this._expirationTime=0,this._version=0,this._isFetching=!1}_dispose(){const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(s,this._handlePushedConfig,this),e.unSubscribeInnerEvent(t.LOGIN,this._handleLoginSuccess,this),e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this),this._reset()}};class a{constructor(e=0,t=0){this.high=e,this.low=t}equal(e){return null!==e&&(this.low===e.low&&this.high===e.high)}toString(){const e=Number(this.high).toString(16);let t=Number(this.low).toString(16);if(t.length<8){let e=8-t.length;for(;e;)t=`0${t}`,e--}return e+t}}const c={SEARCH_GRP_SNS:new a(0,Math.pow(2,1)).toString(),AV_HISTORY_MSG:new a(0,Math.pow(2,2)).toString(),GRP_COMMUNITY:new a(0,Math.pow(2,3)).toString(),MSG_TO_SPECIFIED_GRP_MBR:new a(0,Math.pow(2,4)).toString(),AV_MBR_LIST:new a(0,Math.pow(2,6)).toString(),USER_STATUS:new a(0,Math.pow(2,7)).toString(),CONV_MARK:new a(0,Math.pow(2,9)).toString(),CONV_GROUP:new a(0,Math.pow(2,10)).toString(),AV_BAN_MBR:new a(0,Math.pow(2,11)).toString(),MSG_EXT:new a(0,Math.pow(2,13)).toString(),GRP_COUNTER:new a(0,Math.pow(2,15)).toString(),PLUGIN_TRANSLATE:new a(Math.pow(2,6)).toString(),PLUGIN_VOICE_TO_TEXT:new a(Math.pow(2,7)).toString(),PLUGIN_CS:new a(Math.pow(2,8)).toString(),PLUGIN_PUSH:new a(Math.pow(2,9)).toString(),PLUGIN_BOT:new a(Math.pow(2,10)).toString(),MSG_REACTION:new a(Math.pow(2,16)).toString(),FOLLOW:new a(Math.pow(2,20)).toString()},h="CommercialConfig",_="commercial-config";var l=new class{constructor(){this._core=null,this._expirationTime=0,this._isFetching=!1,this._featureMap=new Map,this._methodKeyMap=new Map}install(e){this._core=e,this._core.notificationCenter.subscribeInnerEvent(e.InnerEvent.COMMERCIAL_CONFIG_PUSH,this._handlePushedConfig,this),this._core.notificationCenter.subscribeInnerEvent(e.InnerEvent.LOGIN,this._handleLoginSuccess,this),this._core.notificationCenter.subscribeInnerEvent(e.InnerEvent.LOGOUT,this._handleLogout,this),this._core.notificationCenter.subscribeInnerEvent(e.InnerEvent.DESTROY,this._dispose,this),e.helper.registerExperimentalAPI("isCommercialAbilityEnabled",this)}isCommercialAbilityEnabled(e){const t=parseInt(e,10).toString(2),{length:i}=t;let n,s=!0;for(let e=i-1,r=0;e>=0;e--,r++)if("1"===t.charAt(e)&&(n=r<32?new a(0,2**r).toString():new a(2**(r-32),0).toString(),!this._featureMap.get(n))){s=!1;break}return console.log(`${h}.isFeatureEnabled decimalNumber:${e} key:${n} ret:${s}`),{code:0,data:{enabled:s}}}_fetchAndParseCommercialConfig(){return e(this,void 0,void 0,(function*(){var e;try{this._isFetching=!0;const t=this._core.helper.generateProtocolData({servcmd:"im_sdk_config_mgr.fetch_imsdk_purchase_bitsv2",data:{uint32_sdkappid:null===(e=this._core.store.get("instance"))||void 0===e?void 0:e.sdkAppId}}),i=yield this._core.channel.sendPacket(t);(null==i?void 0:i.body)&&(this._parseCommercialConfig(i.body),this._core.store.set("commercialConfig",this._methodKeyMap))}catch(e){console.warn("fetch commercial fail: ",e)}finally{this._isFetching=!1}}))}_handleLoginSuccess(t){return e(this,void 0,void 0,(function*(){const{purchaseBits:e}=t||{};e&&(this._parsePurchaseBits(e),this._core.store.set("commercialConfig",this._methodKeyMap)),this._canFetch()&&(yield this._fetchAndParseCommercialConfig()),this._core.helper.taskScheduler.addTask({id:_,intervalMs:1e3,callback:this._fetchCommercialConfigIfReady,context:this})}))}_canFetch(){var e;const t=null===(e=this._core.store.get("login"))||void 0===e?void 0:e.isLoggedIn,i=Date.now()>=this._expirationTime;return t&&!this._isFetching&&i}_handlePushedConfig(e){(null==e?void 0:e.body)&&(this._parseCommercialConfig(e.body),this._core.store.set("commercialConfig",this._methodKeyMap))}_fetchCommercialConfigIfReady(){return e(this,void 0,void 0,(function*(){this._canFetch()&&(yield this._fetchAndParseCommercialConfig())}))}_parseCommercialConfig(e){if("object"!=typeof e)return;const{int32_error_code:t,str_error_message:i,str_purchase_bits:n,uint32_expired_time:s}=e;0===t?(this._parsePurchaseBits(n),this._expirationTime=Date.now()+1e3*s):void 0===t?(console.log(`${h}._parseCommercialConfig failed. Invalid message format:`,e),this._expirationTime=Date.now()+36e5):(console.error(`${h}._parseCommercialConfig errorCode:${t} errorMessage:${i}`),this._expirationTime=Date.now()+12e4)}_isValidPurchaseBits(e){return e&&"string"==typeof e&&e.length>=1&&e.length<=64&&/[01]{1,64}/.test(e)}_parsePurchaseBits(e){if(this._isValidPurchaseBits(e)){this._featureMap.clear(),this._methodKeyMap.clear();let t=null;for(let i=e.length-1,n=0;i>=0;i--,n++)if(t=n<32?new a(0,2**n).toString():new a(2**(n-32),0).toString(),"1"===e[i]){this._featureMap.set(t,!0);const e=this._getKeyByValue(c,t);e&&this._methodKeyMap.set(e,!0)}else{this._featureMap.set(t,!1);const e=this._getKeyByValue(c,t);e&&this._methodKeyMap.set(e,!1)}}else console.warn(`${h}.parsePurchaseBits invalid purchases:${e}`)}_getKeyByValue(e,t){const i=Object.entries(e).find((([e,i])=>i===t));return i?i[0]:void 0}_handleLogout(){this._reset()}_dispose(){this._reset(),this._core.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.COMMERCIAL_CONFIG_PUSH,this._handlePushedConfig,this),this._core.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGIN,this._handleLoginSuccess,this),this._core.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._core.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._core.helper.taskScheduler.removeTask(_),this._core.store.set("commercialConfig",{}),this._expirationTime=0,this._isFetching=!1,this._featureMap.clear()}};var d=new class{constructor(){this._core=null,this._serverOverloadInfoMap=new Map}install(e){this._core=e;const{notificationCenter:t,InnerEvent:i,helper:n}=this._core;t.subscribeInnerEvent(i.OVERLOAD_PUSH,this._handleOverLoadPush,this),t.subscribeInnerEvent(i.LOGOUT,this._reset,this),t.subscribeInnerEvent(i.DESTROY,this._dispose,this),n.registerInterceptor(this.checkServerOverload,this)}checkServerOverload(e){const t=i[e];if(!this._serverOverloadInfoMap.has(t))return;const{overloadStartTimestamp:n,delaySeconds:s}=this._serverOverloadInfoMap.get(t);if(Date.now()-n<=1e3*s)throw new this._core.helper.ChatError({functionName:e,message:"service is busy, please try again later"});this._serverOverloadInfoMap.delete(t)}_handleOverLoadPush(e){const{OverLoadServCmd:t,DelaySecs:i}=e;this._serverOverloadInfoMap.set(t,{overloadStartTimestamp:Date.now(),delaySeconds:i})}_reset(){this._serverOverloadInfoMap.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.OVERLOAD_PUSH,this._handleOverLoadPush,this),e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};console.log("config-center"),exports.default=function(e){o.install(e),l.install(e),d.install(e)}; //# sourceMappingURL=config-center.js.map