@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 9.72 kB
JavaScript
!function(o,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(o="undefined"!=typeof globalThis?globalThis:o||self).TencentCloudChatFollow=t()}(this,function(){"use strict";function o(o,t,e,r){return new(e||(e=Promise))(function(n,l){function i(o){try{u(r.next(o))}catch(o){l(o)}}function s(o){try{u(r.throw(o))}catch(o){l(o)}}function u(o){var t;o.done?n(o.value):(t=o.value,t instanceof e?t:new e(function(o){o(t)})).then(i,s)}u((r=r.apply(o,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const t=new class{init(o){this.core=o}},e="Follow",r="FOLLOW",n=3122,l={NONE:0,FOLLOWERS:1,FOLLOWING:2,MUTUAL:3};function i(e,r){return o(this,void 0,void 0,function*(){var o;const{common:n,store:l}=t.core,i={FollowType:e,From_Account:null===(o=l.get("login"))||void 0===o?void 0:o.userId,StartCursor:r,WantNum:f()};return function(o){const{FollowItem:t=[],NextCursor:e,ActionStatus:r,ErrorCode:n,ErrorInfo:l,ErrorDisplay:i}=o;return{resultList:t.map(o=>({userID:o.To_Account,followTime:o.FollowTime,profileItem:o.ProfileItem,profileSequence:o.ProfileSequence})),nextPageToken:e,actionStatus:r,errorCode:n,errorInfo:l,errorDisplay:i}}(yield n.buildAndSendPacket({servcmd:"follow.follow_get",data:i}))})}function s(t){return o(this,void 0,void 0,function*(){return i(l.FOLLOWERS,t)})}function u(t){return o(this,void 0,void 0,function*(){return i(l.MUTUAL,t)})}function c(t){return o(this,void 0,void 0,function*(){return i(l.FOLLOWING,t)})}function a(e){return o(this,void 0,void 0,function*(){var o;const{common:r,store:n}=t.core,l={From_Account:null===(o=n.get("login"))||void 0===o?void 0:o.userId,FollowItem:e.map(o=>({To_Account:o}))};return function(o){const{ResultItem:t=[],ActionStatus:e,ErrorCode:r,ErrorInfo:n,ErrorDisplay:l}=o;return{resultList:t.map(o=>({userID:o.To_Account,resultCode:o.ResultCode,resultInfo:o.ResultInfo})),actionStatus:e,errorCode:r,errorInfo:n,errorDisplay:l}}(yield r.buildAndSendPacket({servcmd:"follow.follow_add",data:l}))})}function d(e){return o(this,void 0,void 0,function*(){var o;const{common:r,store:n}=t.core,l={From_Account:null===(o=n.get("login"))||void 0===o?void 0:o.userId,To_Account:e};return function(o){const{ResultItem:t=[],ActionStatus:e,ErrorCode:r,ErrorInfo:n,ErrorDisplay:l}=o;return{resultList:t.map(o=>({userID:o.To_Account,resultCode:o.ResultCode,resultInfo:o.ResultInfo})),actionStatus:e,errorCode:r,errorInfo:n,errorDisplay:l}}(yield r.buildAndSendPacket({servcmd:"follow.follow_delete",data:l}))})}function f(){const o=t.core.store.get("cloudConfig")||{},e=null==o?void 0:o.follow_req_count;if(void 0!==e){const o=Number(e);return o>1e3?1e3:o}return 500}const w={getMyFollowersList:[{required:!1,rules:["string"],allowEmpty:!0}],getMyFollowingsList:[{required:!1,rules:["string"],allowEmpty:!0}],getMutualFollowersList:[{required:!1,rules:["string"],allowEmpty:!0}],followUser:[{required:!0,rules:["array"],allowEmpty:!1}],unfollowUser:[{required:!0,rules:["array"],allowEmpty:!1}],checkFollow:[{required:!0,rules:["array"],allowEmpty:!1}],getUserFollowInfo:[{required:!0,rules:["array"],allowEmpty:!1}]},h={getMyFollowersList:!0,getMyFollowingsList:!0,getMutualFollowersList:!0,followUser:!0,unfollowUser:!0,checkFollow:!0,getUserFollowInfo:!0};var p=new class{constructor(){this.name="Follow"}install(o){this._core=o;const{helper:e,notificationCenter:r,InnerEvent:n}=o;t.init(o),e.registerApi({apiName:"getMyFollowingList",context:this}),e.registerApi({apiName:"getMyFollowersList",context:this}),e.registerApi({apiName:"getMutualFollowersList",context:this}),e.registerApi({apiName:"followUser",context:this}),e.registerApi({apiName:"unfollowUser",context:this}),e.registerApi({apiName:"getUserFollowInfo",context:this}),e.registerApi({apiName:"checkFollowType",context:this}),e.registerValidateConfig({auth:h,params:w}),r.subscribeInnerEvent(n.MESSAGE_PUSH,r.InnerEventSubType.FOLLOW_LIST_UPDATED,this.onFollowListUpdated,this)}getMyFollowingList(){return o(this,arguments,void 0,function*(o=""){return this._getFollowList({functionName:"getMyFollowingList",protocolFunction:c,nextCursor:o})})}getMyFollowersList(){return o(this,arguments,void 0,function*(o=""){return this._getFollowList({functionName:"getMyFollowersList",protocolFunction:s,nextCursor:o})})}getMutualFollowersList(){return o(this,arguments,void 0,function*(o=""){return this._getFollowList({functionName:"getMutualFollowersList",protocolFunction:u,nextCursor:o})})}_getFollowList(t){return o(this,void 0,void 0,function*(){const{functionName:o,protocolFunction:e,nextCursor:r=""}=t;try{this._checkBusinessCapabilityBits(o);const t=yield e(r),{resultList:n=[],nextPageToken:l=""}=t;return{code:0,data:{resultList:this._parseFollowItems(n),nextCursor:l},successLog:{message:`${o} nextCursor: ${r} from remote`}}}catch(t){this._handleError(o,t)}})}followUser(t){return o(this,void 0,void 0,function*(){return this._handleUserFollowRelation({functionName:"followUser",userIDList:t,protocolFunction:a})})}unfollowUser(t){return o(this,void 0,void 0,function*(){return this._handleUserFollowRelation({functionName:"unfollowUser",userIDList:t,protocolFunction:d})})}_handleUserFollowRelation(t){return o(this,void 0,void 0,function*(){const{functionName:o,userIDList:e,protocolFunction:r}=t;try{const t=this._sliceUserIDList(e,20);this._checkBusinessCapabilityBits(o);const{resultList:n=[]}=yield r(t);return{code:0,data:n}}catch(t){this._handleError(o,t)}})}getUserFollowInfo(r){return o(this,void 0,void 0,function*(){const n="getUserFollowInfo";try{this._checkBusinessCapabilityBits(n);const l=r||[this._core.common.getCurrentUserID()];console.log(`${e} ${n} userIDList: ${l}`);const i=yield function(e){return o(this,void 0,void 0,function*(){var o;const{common:r,store:n}=t.core,l={From_Account:null===(o=n.get("login"))||void 0===o?void 0:o.userId,To_Account:e};return function(o){const{FollowInfo:t=[],ActionStatus:e,ErrorCode:r,ErrorInfo:n,ErrorDisplay:l}=o;return{followInfoList:t.map(o=>({userID:o.To_Account,resultCode:o.ResultCode,resultInfo:o.ResultInfo,followerCount:o.FollowerCount,followingCount:o.FollowingCount,mutualFollowingCount:o.MutualFollowingCount})),actionStatus:e,errorCode:r,errorInfo:n,errorDisplay:l}}(yield r.buildAndSendPacket({servcmd:"follow.follow_get_info",data:l}))})}(r),{followInfoList:s=[]}=i,u=[];return s.forEach(o=>{const{followerCount:t,followingCount:e,mutualFollowingCount:r}=o;u.push({followerCount:t,followingCount:e,mutualFollowingCount:r})}),{code:0,data:u}}catch(o){this._handleError(n,o)}})}checkFollowType(r){return o(this,void 0,void 0,function*(){const n="checkFollowType";try{this._checkBusinessCapabilityBits(n),r.length>100&&(r=r.slice(0,100),this._core.ssoLog.warn(n,`${e} ${n} the length of userIDList cannot exceed 100`));const l=yield function(e){return o(this,void 0,void 0,function*(){var o;const{common:r,store:n}=t.core,l={From_Account:null===(o=n.get("login"))||void 0===o?void 0:o.userId,To_Account:e};return function(o){const{ResultItem:t=[],ActionStatus:e,ErrorCode:r,ErrorInfo:n,ErrorDisplay:l}=o;return{resultList:t.map(o=>({userID:o.To_Account,followType:o.FollowType,resultCode:o.ResultCode,resultInfo:o.ResultInfo})),actionStatus:e,errorCode:r,errorInfo:n,errorDisplay:l}}(yield r.buildAndSendPacket({servcmd:"follow.follow_check",data:l}))})}(r),{resultList:i=[]}=l;return{code:0,data:i.map(({userID:o,followType:t})=>({userID:o,followType:t}))}}catch(o){this._handleError(n,o)}})}onFollowListUpdated(o){const{ssoLog:t,utils:{safeStringify:r}}=this._core;t.debug("onFollowListUpdated",`${e} onFollowListUpdated ${r(o)}`);const{FollowChangeList:n=[]}=o;n.forEach(o=>this._processFollowChangeItem(o))}_processFollowChangeItem(o){const{From_Account:t,FollowDiffList:e=[]}=o,r=function(o,t){var e={};for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&t.indexOf(r)<0&&(e[r]=o[r]);if(null!=o&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(o);n<r.length;n++)t.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(o,r[n])&&(e[r[n]]=o[r[n]])}return e}(o,["From_Account","FollowDiffList"]),n={userID:t,avatar:r.FaceUrl,nick:r.NickName};e.forEach(o=>{const t=this._updateFollowRelation(o,n);this._notifyFollowChanges(t)})}_updateFollowRelation(o,t){const{IsAdd:e,FollowType:r=0}=o,n=this._initFollowInfo(),l=n[r];return l.isAdd=1===e,l.userInfoList.push(l.isAdd?t:t.userID),n}_notifyFollowChanges(o){const{OuterEvent:t}=this._core,e={[l.FOLLOWERS]:t.MY_FOLLOWERS_LIST_UPDATED,[l.FOLLOWING]:t.MY_FOLLOWING_LIST_UPDATED,[l.MUTUAL]:t.MUTUAL_FOLLOWERS_LIST_UPDATED};Object.entries(o).forEach(([o,t])=>{const r=Number(o);t.userInfoList.length>0&&e[r]&&this._emitFollowUpdated(e[r],t)})}_initFollowInfo(){const o={};return Object.values(l).forEach(t=>{t!==l.NONE&&(o[t]={userInfoList:[],isAdd:!1})}),o}_emitFollowUpdated(o,t){const{notificationCenter:e}=this._core;e.emitOuterEvent(o,{name:o,data:t})}_sliceUserIDList(o,t){return o.slice(0,t)}_checkBusinessCapabilityBits(o){const{helper:t,store:e}=this._core;if(!(e.get("commercialConfig")||new Map).get(r))throw new t.ChatError({functionName:o,code:n,replacement1:o})}_handleError(o,t){throw new this._core.helper.ChatError({functionName:o,code:null==t?void 0:t.errorCode,message:null==t?void 0:t.errorInfo})}_parseFollowItems(o){return o.map(o=>{var t;const{userID:e,followTime:r,profileItem:n=[]}=o,l=[];n.forEach(o=>{l.push({tag:o.Tag,value:o.Value})});const i=null===(t=this._core.user.userProfile)||void 0===t?void 0:t.createProfile(e,l);return{userID:e,followTime:r,nick:null==i?void 0:i.nick,avatar:null==i?void 0:i.avatar,allowType:null==i?void 0:i.allowType,adminForbidType:null==i?void 0:i.adminForbidType}})}};return p});