UNPKG

@tencentcloud/lite-chat

Version:

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

2 lines (1 loc) 38.3 kB
!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).TencentCloudChatFriend=t()}(this,function(){"use strict";function e(e,t,r,i){return new(r||(r=Promise))(function(o,n){function s(e){try{a(i.next(e))}catch(e){n(e)}}function d(e){try{a(i.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,d)}a((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const t="friend",r=100,i=2700,o=2710,n=2711,s=2716,d=2742,a=30539,c="Tag_SNS_IM_",u={GROUP:`${c}Group`,REMARK:`${c}Remark`,ADDSOURCE:`${c}AddSource`,ADDWORDING:`${c}Wording`,ADDTIME:`${c}AddTime`},l="Update_Type_Add",p="Update_Type_Delete",h="add",m="remove",_="rename",f=e=>void 0===e;function F(t,r){return e(this,void 0,void 0,function*(){return function(e){return L(e,"ResultItem",e=>({to:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo}))}(yield r.common.buildAndSendPacket({servcmd:"sns.friend_update",data:t}))})}function I(t,r){return e(this,void 0,void 0,function*(){return function(e){return L(e,"InfoItem",e=>({userID:e.To_Account,relation:e.Relation,resultCode:e.ResultCode,resultInfo:e.ResultInfo}))}(yield r.common.buildAndSendPacket({servcmd:"sns.friend_check",data:t}))})}function L(e,t="ResultItem",r=e=>({userID:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo})){const{[t]:i=[],ActionStatus:o,ErrorCode:n,ErrorInfo:s,ErrorDisplay:d}=e;return{resultList:i.map(r),actionStatus:o,errorCode:n,errorInfo:s,errorDisplay:d}}const v={[u.REMARK]:(e,t)=>{e.remark=t},[u.GROUP]:(e,t)=>{e.groupList=t?JSON.parse(JSON.stringify(t)):[]},[u.ADDSOURCE]:(e,t)=>{e.source=t},[u.ADDWORDING]:(e,t)=>{e.wording=t},[u.ADDTIME]:(e,t)=>{e.addTime=t}};let A=class{constructor(e){var t;this.userID="",this.source="",this.wording="",this.remark="",this.addTime=0,this.groupList=[],this.friendCustomField=[];const{userID:r,tagValueList:i=[],core:o}=e,n=null===(t=o.user.userProfile)||void 0===t?void 0:t.createProfile(r,i);this.userID=r,this.profile=n,i.forEach(e=>{const{tag:t,value:r}=e;t.indexOf("Tag_Profile_Custom")>-1&&this.friendCustomField.push({key:t,value:r});const i=v[t];null==i||i(this,r)})}update(e){const t=[];this.friendCustomField.forEach(e=>{t.push(e.key)}),e.forEach(e=>{const{tag:r,value:i}=e;r.indexOf("Tag_Profile_Custom")>-1&&(t.indexOf(r)>-1?this.friendCustomField.forEach(e=>{e.key===r&&(e.value=i)}):this.friendCustomField.push({key:r,value:i}));const o=v[r];null==o||o(this,i)})}};var g=new class{constructor(){this._startIndex=0,this._standardSequence=0,this._customSequence=0}init(e){this._core=e;const{helper:t,constants:r}=e;t.registerApi({apiName:"getFriendList",context:this,methodName:"getFriendListFromCache"}),t.registerApi({apiName:"getFriendProfile",context:this}),t.registerApi({apiName:"addFriend",context:this}),t.registerApi({apiName:"deleteFriend",context:this}),t.registerApi({apiName:"updateFriend",context:this}),t.registerApi({apiName:"checkFriend",context:this}),t.registerWorkflowStep(r.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,r.WORKFLOW_STEP.FRIEND_RECOVER,this.syncFriendListFromServer,this)}getFriendListFromCache(){return e(this,void 0,void 0,function*(){const{appStore:{userStore:e},ssoLog:r,utils:{safeStringify:i}}=this._core,o=[...e.getFriendMap().values()];return r.debug("getFriendListFromCache",`${t} getFriendListFromCache, data:${i(o)}`),{code:0,data:o}})}syncFriendListFromServer(){return e(this,void 0,void 0,function*(){var r;const{ssoLog:i,helper:o}=this._core;try{i.debug("syncFriendListFromServer",`${t} syncFriendListFromServer`);const o={From_Account:null===(r=this._core.store.get("login"))||void 0===r?void 0:r.userId,StartIndex:this._startIndex,StandardSequence:this._standardSequence,CustomSequence:this._customSequence},n=yield function(t,r){return e(this,void 0,void 0,function*(){return function(e){const{UserDataItem:t=[],StandardSequence:r,CustomSequence:i,FriendNum:o,CompleteFlag:n,NextStartIndex:s,ActionStatus:d,ErrorCode:a,ErrorInfo:c,ErrorDisplay:u}=e;return{friendList:t.map(e=>{var t;return{userID:e.To_Account,tagValueList:null===(t=e.ValueItem)||void 0===t?void 0:t.map(e=>({tag:e.Tag,value:e.Value}))}}),standardSequence:r,customSequence:i,friendNumber:o,completeFlag:n,nextStartIndex:s,actionStatus:d,errorCode:a,errorInfo:c,errorDisplay:u}}(yield r.common.buildAndSendPacket({servcmd:"sns.friend_get",data:t}))})}(o,this._core),{friendList:s,nextStartIndex:d,standardSequence:a,customSequence:c,completeFlag:u}=n;this._startIndex=d,this._standardSequence=a,this._customSequence=c,this._processFriendList(s),0===u?yield this.syncFriendListFromServer():(this._emitFriendListUpdated(),this._fetchFriendProfile()),i.debug("syncFriendListFromServer",`${t} syncFriendListFromServer ok`)}catch(e){const{errorCode:t}=e||{};throw new o.ChatError({code:t})}})}getFriendProfile(t){return e(this,void 0,void 0,function*(){var i;try{let{userIDList:o}=t;o.length>r&&(o=o.slice(0,r));const{friendUserIdList:n,unFriendUserIdList:s}=this.partitionByIsFriend(o),d=this._createNonFriendErrorList(s);if(0===n.length)return{code:0,data:{friendList:[],failureUserIDList:d}};const a={From_Account:null===(i=this._core.store.get("login"))||void 0===i?void 0:i.userId,To_Account:n},c=(yield function(t,r){return e(this,void 0,void 0,function*(){return function(e){const{InfoItem:t=[],ActionStatus:r,ErrorCode:i,ErrorInfo:o,ErrorDisplay:n}=e;return{friendProfileList:t.map(e=>{var t;return{userID:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo,relation:e.Relation,standardSequence:e.StandardSequence,customSequence:e.CustomSequence,profileItem:(null===(t=e.SnsProfileItem)||void 0===t?void 0:t.map(e=>({tag:e.Tag,value:e.Value})))||[]}}),actionStatus:r,errorCode:i,errorInfo:o,errorDisplay:n}}(yield r.common.buildAndSendPacket({servcmd:"sns.friend_get_specified",data:t}))})}(a,this._core))||{},{successProfiles:u,failedProfiles:l}=this._processFriendProfileResponse(c);return{code:0,data:{friendList:u,failureUserIDList:[...l,...d]}}}catch(e){const{errorCode:t}=e||{};throw new this._core.helper.ChatError({code:t})}})}addFriend(r){return e(this,void 0,void 0,function*(){var i;const{helper:o,store:n,ssoLog:s}=this._core;try{const{to:d,source:c,type:u,wording:l,remark:p,groupName:h}=r,m={From_Account:null===(i=n.get("login"))||void 0===i?void 0:i.userId,AddFriendItem:[{To_Account:d,AddSource:c,AddWording:l,GroupName:h,Remark:p}],AddType:u},_=yield function(t,r){return e(this,void 0,void 0,function*(){return L(yield r.common.buildAndSendPacket({servcmd:"sns.friend_add",data:t}))})}(m,this._core),{resultList:f=[]}=_,{userID:F,resultCode:I}=f[0];if(0===I)return s.debug("addFriend",`${t} addFriend ok`),{code:0,data:{userID:F}};if(I===a)return s.debug("addFriend",`${t} addFriend wait for the other party to agree`),{code:0,data:{userID:F,code:I,message:"wait for the other party to agree"}};throw new o.ChatError({functionName:"addFriend",code:I})}catch(e){const{errorCode:t}=e||{};throw new o.ChatError({code:t})}})}deleteFriend(i){return e(this,void 0,void 0,function*(){var o;const{store:n,ssoLog:s,helper:d}=this._core;try{let{userIDList:d,type:a}=i;const c=null===(o=n.get("login"))||void 0===o?void 0:o.userId;d.length>1e3&&(s.info("deleteFriend",`${t} deleteFriend the length of userIDList cannot exceed:1000`),d=d.slice(0,r));const{friendUserIdList:u,unFriendUserIdList:l}=this.partitionByIsFriend(d);if(0===u.length)return{code:0,data:{friendList:[],failureUserIDList:this._createNonFriendErrorList(l)}};const p={From_Account:c,To_Account:u,DeleteType:a},h=yield function(t,r){return e(this,void 0,void 0,function*(){return L(yield r.common.buildAndSendPacket({servcmd:"sns.friend_delete",data:t}))})}(p,this._core),{successUserIDList:m,failureUserIDList:_}=this._processOperationResult({resultList:h.resultList,includeRelation:!1});return{code:0,data:{successUserIDList:m,failureUserIDList:_}}}catch(e){const{errorCode:t}=e||{};throw new d.ChatError({code:t})}})}updateFriend(t){return e(this,void 0,void 0,function*(){var e;const{helper:r,store:o,appStore:{userStore:n}}=this._core;try{const{userID:s,remark:d,friendCustomField:a}=t,c=null===(e=o.get("login"))||void 0===e?void 0:e.userId;if(!n.getFriendMap().has(s))throw new r.ChatError({functionName:"updateFriend",code:i});const u={From_Account:c,UpdateItem:[{To_Account:s,SnsItem:this._buildSnsItemList(d,a)}]},l=yield F(u,this._core),{resultList:p}=l,{to:h,resultCode:m,resultInfo:_}=p[0];if(f(m)||0===m){return{code:0,data:this._updateLocalFriend({userID:h,remark:d,friendCustomField:a})}}throw new r.ChatError({functionName:"updateFriend",code:m,message:_})}catch(e){const{errorCode:t}=e||{};throw new r.ChatError({code:t})}})}checkFriend(t){return e(this,void 0,void 0,function*(){var e;const{OuterConstant:r,store:i,helper:o}=this._core;try{const{userIDList:o,type:n}=t,s=null===(e=i.get("login"))||void 0===e?void 0:e.userId,d={From_Account:s,To_Account:o,CheckType:n===r.SNS_CHECK_TYPE_SINGLE?n:r.SNS_CHECK_TYPE_BOTH},a=yield I(d,this._core),{successUserIDList:c=[],failureUserIDList:u=[]}=this._processOperationResult({resultList:a.resultList,includeRelation:!0});return{code:0,data:{successUserIDList:c,failureUserIDList:u}}}catch(e){const{errorCode:t}=e||{};throw new o.ChatError({code:t})}})}dispose(){this.reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this.reset,this)}reset(){const{userStore:e}=this._core.appStore;e.getFriendMap().clear(),this._startIndex=0,this._standardSequence=0,this._customSequence=0}updateFriendList(r){return e(this,void 0,void 0,function*(){const{ssoLog:e,utils:{safeStringify:i}}=this._core;e.debug("updateFriendList",`${t} updateFriendList data:${i(r)}`);const{userStore:o}=this._core.appStore,{addedFriendList:n=[],removedFriendList:s=[]}=r;n.length>0&&(n.forEach(e=>{const t=new A({userID:e,core:this._core});o.getFriendMap().set(e,t)}),yield this.getFriendProfile({userIDList:n}),this._emitFriendListUpdated()),s.length>0&&(s.forEach(e=>{o.getFriendMap().delete(e)}),this._emitFriendListUpdated())})}updateFriendInfo(e){const{ssoLog:r,utils:{safeStringify:i}}=this._core;r.debug("updateFriendList",`${t} updateFriendInfo data:${i(e)}`);const{appStore:{userStore:o,conversationStore:n},utils:{isEmpty:s}}=this._core,{userID:d,tagValueList:a}=e[0]||{},c=o.getFriend(d);if(s(a)||!c)return;const l=a.map(e=>({tag:e.BytesTag,value:e.BytesValue}));l.forEach(e=>{var t;const{tag:r,value:i}=e,o=null===(t=n.getConversation(`C2C${d}`))||void 0===t?void 0:t.remark;r===u.REMARK&&i!==o&&n.updateConversation(`C2C${d}`,{remark:i}),c.update(l)}),this._emitFriendListUpdated()}_processFriendList(e){const{utils:{isEmpty:t},appStore:{userStore:r}}=this._core;t(e)||e.forEach(e=>{const{userID:t,tagValueList:i}=e;if(this._isInvalidFriend(t))return;const o=new A({userID:t,tagValueList:i,core:this._core});r.getFriendMap().set(t,o)})}_isInvalidFriend(e){return"@TLS#NOT_FOUND"===e||""===e}_fetchFriendProfile(){return e(this,void 0,void 0,function*(){const{appStore:{userStore:e},user:{userProfile:r},ssoLog:i}=this._core;i.debug("_fetchFriendProfile",`${t} _fetchFriendProfile`);const o=[...e.getFriendMap().keys()],n=Math.ceil(o.length/100);for(let t=0;t<n;t++){const i=100*t,n=Math.min(i+100,o.length),s=o.slice(i,n),d=yield null==r?void 0:r.getUserProfile({userIDList:s});if(d&&d.data){d.data.forEach(t=>{const r=e.getFriend(t.userID);r&&(r.profile=t)})}this._emitFriendListUpdated()}})}_emitFriendListUpdated(){const{notificationCenter:e,OuterEvent:t,appStore:{userStore:r}}=this._core,i=Array.from(r.getFriendMap().values());e.emitOuterEvent(t.FRIEND_LIST_UPDATED,{name:t.FRIEND_LIST_UPDATED,data:i})}_updateCustomField(e,t){if(!Array.isArray(e)||!Array.isArray(t))return!1;let r=!1;return t.forEach(({key:t,value:i})=>{const o=e.find(e=>e.key===t);o?o.value!==i&&(o.value=i,r=!0):(e.push({key:t,value:i}),r=!0)}),r}_updateLocalFriend(e){const{userStore:t}=this._core.appStore,{userID:r,remark:i,friendCustomField:o}=e,n=t.getFriend(r);if(n)return f(i)||(n.remark=i),Array.isArray(o)&&o.length>0&&this._updateCustomField(n.friendCustomField,o),this._emitFriendListUpdated(),n}_processOperationResult(e){const{resultList:t,includeRelation:r=!1}=e,{isEmpty:i}=this._core.utils,o=[],n=[];return Array.isArray(t)&&!i(t)&&t.forEach(e=>{const{userID:t,relation:i,resultCode:s,resultInfo:d}=e;if(f(s)||0===s){const e={userID:t};r&&(e.code=0,e.relation=i),o.push(e)}else n.push({userID:t,code:s,message:d})}),{successUserIDList:o,failureUserIDList:n}}_createNonFriendErrorList(e){return e.map(e=>({userID:e,code:i,message:"The specified userID is not my friend"}))}_buildSnsItemList(e,t){const r=[];return f(e)||r.push({Tag:u.REMARK,Value:e}),Array.isArray(t)&&t.length>0&&t.forEach(e=>{r.push({tag:e.key,value:e.value})}),r}_processFriendProfileResponse(e){const{friendProfileList:t=[]}=e,{userStore:r}=this._core.appStore,i=[],o=[];return t.forEach(e=>{const{userID:t,resultCode:n,resultInfo:s,profileItem:d}=e;if(0===n){const e=new A({userID:t,tagValueList:d,core:this._core});i.push(e),r.getFriendMap().set(t,e)}else o.push({userID:t,code:n,message:s})}),{successProfiles:i,failedProfiles:o}}partitionByIsFriend(e){const t=[],r=[];return e.forEach(e=>{const{userStore:i}=this._core.appStore;i.getFriendMap().has(e)?t.push(e):r.push(e)}),{friendUserIdList:t,unFriendUserIdList:r}}isMyFriend(e){const{userStore:t}=this._core.appStore;return t.getFriendMap().has(e)}};var y=new class{init(e){this.core=e}};function S(t){return e(this,void 0,void 0,function*(){var e;const{store:r,common:i}=y.core,o={From_Account:null===(e=r.get("login"))||void 0===e?void 0:e.userId,ResponseFriendItem:[{To_Account:t.userID,ResponseAction:t.type,Remark:t.remark}]};return C(yield i.buildAndSendPacket({servcmd:"sns.friend_response",data:o}))})}function E(t){return e(this,void 0,void 0,function*(){var e;const{store:r,common:i}=y.core,o={From_Account:null===(e=r.get("login"))||void 0===e?void 0:e.userId,ResponseFriendItem:[{To_Account:t.userID,ResponseAction:"Response_Action_Reject"}]};return C(yield i.buildAndSendPacket({servcmd:"sns.friend_response",data:o}))})}function D(t){return e(this,void 0,void 0,function*(){var e;const{store:r,common:i}=y.core,o={From_Account:null===(e=r.get("login"))||void 0===e?void 0:e.userId,To_Account:[t.userID],PendencyType:t.type};return C(yield i.buildAndSendPacket({servcmd:"sns.pendency_delete",data:o}))})}function C(e){const{ResultItem:t=[],ActionStatus:r,ErrorCode:i,ErrorInfo:o,ErrorDisplay:n}=e;return{resultList:t.map(e=>({userID:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo})),actionStatus:r,errorCode:i,errorInfo:o,errorDisplay:n}}var T=new class{constructor(){this._friendApplicationMap=new Map,this._unreadCount=0,this._startTime=0,this._currentSequence=0}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getFriendApplicationList",context:this,methodName:"getFriendApplicationListFromCache"}),t.registerApi({apiName:"acceptFriendApplication",context:this}),t.registerApi({apiName:"refuseFriendApplication",context:this}),t.registerApi({apiName:"deleteFriendApplication",context:this}),t.registerApi({apiName:"setFriendApplicationRead",context:this})}getFriendApplicationListFromCache(){return e(this,void 0,void 0,function*(){const{ssoLog:e,utils:{safeStringify:r}}=this._core,i=[...this._friendApplicationMap.values()];return e.debug("getFriendApplicationListFromCache",`${t} getFriendApplicationListFromCache friendApplicationList:${r(i)}`),{code:0,data:{friendApplicationList:i,unreadCount:this._unreadCount}}})}syncFriendApplicationListFromServer(){return e(this,void 0,void 0,function*(){const{ssoLog:i,utils:{safeStringify:o}}=this._core,n="syncFriendApplicationListFromServer";try{const t=yield function(t){return e(this,void 0,void 0,function*(){var e;const{store:i,OuterConstant:o,common:n}=y.core,s={From_Account:null===(e=i.get("login"))||void 0===e?void 0:e.userId,PendencyType:o.SNS_APPLICATION_TYPE_BOTH,MaxLimited:r,StartTime:t.startTime,LastSequence:t.currentSequence};return function(e){const{PendencyItem:t=[],StartTime:r,UnreadPendencyCount:i,CurrentSequence:o,ActionStatus:n,ErrorCode:s,ErrorInfo:d,ErrorDisplay:a}=e;return{resultList:t.map(e=>({userID:e.To_Account,time:e.AddTime,source:e.AddSource,wording:e.AddWording,nick:e.Nick,type:e.PendencyType,avatar:e.Image})),startTime:r,unreadCount:i,currentSequence:o,actionStatus:n,errorCode:s,errorInfo:d,errorDisplay:a}}(yield n.buildAndSendPacket({servcmd:"sns.pendency_get",data:s}))})}({startTime:this._startTime,currentSequence:this._currentSequence}),{resultList:i,unreadCount:o,startTime:n,currentSequence:s}=t;this._startTime=n,this._currentSequence=s,this._unreadCount=o,this._friendApplicationMap.clear(),Array.isArray(i)&&i.forEach(e=>{const{userID:t,type:r}=e,i=this._createFriendApplication(e);this._friendApplicationMap.set(`${t}_${r}`,i)}),this._emitApplicationListUpdated()}catch(e){const r=new this._core.helper.ChatError({functionName:n,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw i.error("syncFriendApplicationListFromServer",`${t} ${n} error:`,{error:r}),r}})}acceptFriendApplication(t){return e(this,void 0,void 0,function*(){const{OuterConstant:{SNS_APPLICATION_AGREE:e,SNS_APPLICATION_AGREE_AND_ADD:r}}=this._core;return[e,r].includes(t.type)||(t.type=r),this._operationApplication({functionName:"acceptFriendApplication",options:t,protocolFunction:S})})}refuseFriendApplication(t){return e(this,void 0,void 0,function*(){return this._operationApplication({functionName:"refuseFriendApplication",options:t,protocolFunction:E})})}deleteFriendApplication(t){return e(this,void 0,void 0,function*(){const{userID:e,type:r}=t;return this._checkApplicationExists(e,r,"deleteFriendApplication"),this._operationApplication({functionName:"deleteFriendApplication",options:t,protocolFunction:D})})}_checkApplicationExists(e,t,r){if(!this._friendApplicationMap.has(`${e}_${t}`))throw new this._core.helper.ChatError({functionName:r,code:s})}_operationApplication(r){return e(this,void 0,void 0,function*(){const{functionName:e,options:i,protocolFunction:o}=r;console.log(`${t} ${e} options:`,i);try{const r=yield o(i),{resultList:n=[]}=r,{resultCode:s,resultInfo:d}=n[0];if(0===s)return console.log(`${t} ${e} ok`),{code:0,data:{}};throw{errorCode:s,errorInfo:d}}catch(r){const i=new this._core.helper.ChatError({functionName:e,code:null==r?void 0:r.errorCode,message:null==r?void 0:r.errorInfo});throw console.error(`${t} ${e} error:`,i),i}})}setFriendApplicationRead(){return e(this,void 0,void 0,function*(){try{return yield function(){return e(this,void 0,void 0,function*(){var e;const{store:t,common:r}=y.core,i={From_Account:null===(e=t.get("login"))||void 0===e?void 0:e.userId,LatestPendencyTimeStamp:(o=Math.floor(Date.now()/1e3),n=0,Math.round(Number(o)*Math.pow(10,n))/Math.pow(10,n))};var o,n;return function(e){const{ActionStatus:t,ErrorCode:r,ErrorInfo:i,ErrorDisplay:o}=e;return{actionStatus:t,errorCode:r,errorInfo:i,errorDisplay:o}}(yield r.buildAndSendPacket({servcmd:"sns.pendency_report",data:i}))})}(),this._unreadCount=0,this._emitApplicationListUpdated(),{code:0,data:{}}}catch(e){throw new this._core.helper.ChatError({functionName:"setFriendApplicationRead",code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo})}})}updateFriendApplicationList(e){const{from:t,addedApplicationList:r=[],removedApplicationList:i=[]}=e,{isEmpty:o}=this._core.utils;o(r)||this.onApplicationAdded(r,t),o(i)||this.onApplicationDeleted()}onApplicationAdded(e,t){var r;const{store:i,OuterConstant:o}=this._core,n=t===(null===(r=i.get("login"))||void 0===r?void 0:r.userId)?o.SNS_APPLICATION_SENT_BY_ME:o.SNS_APPLICATION_SENT_TO_ME;e.forEach(e=>{const t=`${e.userID}_${n}`;n!==o.SNS_APPLICATION_SENT_TO_ME||this._friendApplicationMap.has(t)||(this._unreadCount+=1),this._friendApplicationMap.set(t,this._createFriendApplication(Object.assign(Object.assign({},e),{type:n})))}),this._emitApplicationListUpdated()}onApplicationDeleted(){this._startTime=0,this._currentSequence=0,this.syncFriendApplicationListFromServer()}onApplicationRead(){this._unreadCount=0,this._emitApplicationListUpdated()}reset(){this._startTime=0,this._currentSequence=0,this._unreadCount=0,this._friendApplicationMap.clear()}dispose(){this.reset()}_createFriendApplication(e){return{userID:e.userID||"",nick:e.nick||"",avatar:e.avatar||"",time:e.time||0,source:e.source||"",wording:e.wording||"",type:e.type||""}}_emitApplicationListUpdated(){const{notificationCenter:e,OuterEvent:t}=this._core;e.emitOuterEvent(t.FRIEND_APPLICATION_LIST_UPDATED,{name:t.FRIEND_APPLICATION_LIST_UPDATED,data:{friendApplicationList:[...this._friendApplicationMap.values()],unreadCount:this._unreadCount}})}};function k(){return e(this,void 0,void 0,function*(){const{common:e}=y.core,t={From_Account:e.getCurrentUserID(),LastSequence:0,NeedFriend:"Need_Friend_Type_Yes"};return function(e){return U(e,e=>({name:e.GroupName,userIDList:e.To_Account||[],count:e.FriendNumber}))}(yield e.buildAndSendPacket({servcmd:"sns.group_get",data:t}))})}function N(t){return e(this,void 0,void 0,function*(){const{name:e,userIDList:r}=t,{common:i}=y.core,o={From_Account:i.getCurrentUserID(),GroupName:[e],To_Account:r};return function(e){return U(e,e=>({userID:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo}))}(yield i.buildAndSendPacket({servcmd:"sns.group_add",data:o}))})}function G(t){return e(this,void 0,void 0,function*(){const{common:e}=y.core,r={From_Account:e.getCurrentUserID(),GroupName:[t.name]};return function(e){return U(e)}(yield e.buildAndSendPacket({servcmd:"sns.group_delete",data:r}))})}function w(t){return e(this,void 0,void 0,function*(){const{common:e}=y.core,{name:r,userIDList:i}=t,o=R(i,l),n={From_Account:e.getCurrentUserID(),GroupOldName:r,UpdateGroupItem:o};return function(e){return U(e,e=>({userID:e.To_Account,updateType:e.UpdateType,resultCode:e.ResultCode,resultInfo:e.ResultInfo}))}(yield e.buildAndSendPacket({servcmd:"sns.group_update",data:n}))})}function P(t){return e(this,void 0,void 0,function*(){const{common:e}=y.core,{name:r,userIDList:i}=t,o=R(i,p),n={From_Account:e.getCurrentUserID(),GroupOldName:r,UpdateGroupItem:o};return function(e){return U(e,e=>({userID:e.To_Account,updateType:e.UpdateType,resultCode:e.ResultCode,resultInfo:e.ResultInfo}))}(yield e.buildAndSendPacket({servcmd:"sns.group_update",data:n}))})}function M(t){return e(this,void 0,void 0,function*(){const{common:e}=y.core,{oldName:r,newName:i}=t,o={From_Account:e.getCurrentUserID(),GroupOldName:r,GroupNewName:i};return function(e){return U(e)}(yield e.buildAndSendPacket({servcmd:"sns.group_update",data:o}))})}function R(e,t){return e.filter(e=>g.isMyFriend(e)).map(e=>({To_Account:e,UpdateType:t}))}function U(e,t){const{CurrentSequence:r,ActionStatus:i,ErrorCode:o,ErrorInfo:n,ErrorDisplay:s,ResultItem:d=[]}=e,a={currentSequence:r,actionStatus:i,errorCode:o,errorInfo:n,errorDisplay:s};return t&&d.length>0?Object.assign(Object.assign({},a),{resultList:d.map(t)}):a}class O{constructor(e){const{name:t="",userIDList:r=[]}=e;this.name=t,this.userIDList=r,this.count=r.length}addToUserIDList(e){-1===this.userIDList.indexOf(e)&&(this.userIDList.push(e),this.count=this.userIDList.length)}removeFromUserIDList(e){const t=this.userIDList.indexOf(e);t>-1&&(this.userIDList.splice(t,1),this.count=this.userIDList.length)}}var b=new class{constructor(){this._friendGroupMap=new Map}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getFriendGroupList",context:this,methodName:"getFriendGroupListFromCache"}),t.registerApi({apiName:"createFriendGroup",context:this}),t.registerApi({apiName:"deleteFriendGroup",context:this}),t.registerApi({apiName:"addToFriendGroup",context:this}),t.registerApi({apiName:"removeFromFriendGroup",context:this}),t.registerApi({apiName:"renameFriendGroup",context:this})}getFriendGroupListFromCache(){return e(this,void 0,void 0,function*(){const{ssoLog:e,utils:{safeStringify:r}}=this._core,i=[...this._friendGroupMap.values()];return e.debug(`${t} getFriendGroupListFromCache, data:${r(i)}`),{code:0,data:i}})}syncFriendGroupListFromServer(){return e(this,void 0,void 0,function*(){const{ssoLog:e}=this._core,r="syncFriendGroupListFromServer";try{const i=yield k(),{resultList:o=[]}=i;this._core.helper.isEmpty(o)&&e.debug("syncFriendGroupListFromServer",`${t} ${r} ok. count: 0`),o.forEach(e=>{const t=new O(e);this._friendGroupMap.set(e.name,t)}),this._emitGroupListUpdated()}catch(e){this._handleError(r,e)}})}createFriendGroup(t){return e(this,void 0,void 0,function*(){const e="createFriendGroup";try{const{name:r}=t;this._checkGroupExist(e,r);const i=yield N(t),{successUserIDList:o,failureUserIDList:n}=this._parseCreateGroupResponse(i),s=new O({name:r,userIDList:o});return this._friendGroupMap.set(r,s),this._emitGroupListUpdated(),{code:0,data:{friendGroup:s,failureUserIDList:n}}}catch(t){this._handleError(e,t)}})}deleteFriendGroup(t){return e(this,void 0,void 0,function*(){const e="deleteFriendGroup";try{const{name:r}=t;this._checkGroupNotExist(e,r),yield G(t);const i=this._friendGroupMap.get(r);return i&&(this._updateFriendGroup({name:r,userIDList:i.userIDList,action:m}),this._friendGroupMap.delete(r)),this._emitGroupListUpdated(),{code:0,data:i}}catch(t){this._handleError(e,t)}})}addToFriendGroup(t){return e(this,void 0,void 0,function*(){return this._updateFriendGroupMember({functionName:"addToFriendGroup",params:t,protocolFunction:w})})}removeFromFriendGroup(t){return e(this,void 0,void 0,function*(){return this._updateFriendGroupMember({functionName:"removeFromFriendGroup",params:t,protocolFunction:P})})}renameFriendGroup(t){return e(this,void 0,void 0,function*(){const e="renameFriendGroup";try{const{oldName:r,newName:i}=t;this._checkGroupNotExist(e,r),this._checkGroupExist(e,i),yield M(t);const o=this._updateGroupNameInCache(r,i);return o&&this._updateFriendGroup({name:r,userIDList:o.userIDList,action:_,newName:i}),this._emitGroupListUpdated(),{code:0,data:o}}catch(t){this._handleError(e,t)}})}_updateFriendGroupMember(t){return e(this,void 0,void 0,function*(){const{functionName:e,params:r,protocolFunction:i}=t;try{const{name:t}=r;this._checkGroupNotExist(e,t);const o=yield i(r),{friendGroup:n,failureUserIDList:s}=this._parseUpdateFriendGroupResponse(t,o);return this._emitGroupListUpdated(),{code:0,data:{friendGroup:n,failureUserIDList:s}}}catch(t){this._handleError(e,t)}})}_updateGroupNameInCache(e,t){const r=this._friendGroupMap.get(e);return r&&(r.name=t,this._friendGroupMap.delete(e),this._friendGroupMap.set(t,r)),r}_parseCreateGroupResponse(e){const{resultList:t=[]}=e,r=[],i=[];return t.forEach(({userID:e,resultCode:t,resultInfo:o})=>{f(t)||0===t?r.push(e):i.push({userID:e,code:t,message:o})}),{successUserIDList:r,failureUserIDList:i}}_parseUpdateFriendGroupResponse(e,t){const{resultList:r=[]}=t,i=this._friendGroupMap.get(e),o=[],n=[],s=[];return r.forEach(t=>{0===t.resultCode?i&&(t.updateType===l?this._addFriendToGroup(e,t.userID,i,n):t.updateType===p&&this._removeFriendFromGroup(e,t.userID,i,s)):o.push({userID:t.userID,code:t.resultCode,message:t.resultInfo})}),{friendGroup:i,failureUserIDList:o}}_addFriendToGroup(e,t,r,i){r.addToUserIDList(t),i.push(t),this._updateFriendGroup({name:e,userIDList:i,action:h})}_removeFriendFromGroup(e,t,r,i){r.removeFromUserIDList(t),i.push(t),this._updateFriendGroup({name:e,userIDList:i,action:m})}_emitGroupListUpdated(){const{notificationCenter:e,OuterEvent:t}=this._core,r=[...this._friendGroupMap.values()];e.emitOuterEvent(t.FRIEND_GROUP_LIST_UPDATED,{name:t.FRIEND_GROUP_LIST_UPDATED,data:r})}_checkGroupNotExist(e,t){if(!this._friendGroupMap.has(t))throw new this._core.helper.ChatError({functionName:e,code:n})}_checkGroupExist(e,t){if(this._friendGroupMap.has(t))throw new this._core.helper.ChatError({functionName:e,code:o})}_updateFriendGroup(e){const{name:t,userIDList:r,action:i,newName:o}=e,{userStore:n}=this._core.appStore;t&&!this._core.utils.isEmpty(r)&&r.forEach(e=>{const r=n.getFriend(e);if(!r)return;const{groupList:s}=r;switch(i){case h:-1===s.indexOf(t)&&(s.push(t),r.count=s.length);break;case m:{const e=s.indexOf(t);e>-1&&(s.splice(e,1),r.count=s.length);break}case _:{if(!o)return;const e=s.indexOf(t);-1!==e&&(s[e]=o);break}}})}_handleError(e,t){throw new this._core.helper.ChatError({functionName:e,code:null==t?void 0:t.errorCode,message:null==t?void 0:t.errorInfo})}reset(){this._friendGroupMap.clear()}dispose(){this.reset()}};function B(e){return e&&0!==e.length?e.map(e=>({userID:e.To_Account,addBlackTimeStamp:e.AddBlackTimeStamp})):[]}var q=new class{constructor(){this._blacklistMap=new Map,this._retryCount=0,this._startIndex=0}init(e){this._core=e;const{helper:t,constants:r}=e;t.registerApi({apiName:"getBlacklist",context:this,methodName:"getBlacklistFromCache"}),t.registerApi({apiName:"addToBlacklist",context:this}),t.registerApi({apiName:"removeFromBlacklist",context:this}),t.registerWorkflowStep(r.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,r.WORKFLOW_STEP.BLACKLIST_RECOVER,this.syncBlacklistFromServer,this)}getBlacklistFromCache(){return e(this,void 0,void 0,function*(){return{code:0,data:Array.from(this._blacklistMap.keys())}})}syncBlacklistFromServer(){return e(this,void 0,void 0,function*(){try{this._core.ssoLog.debug(`${t} syncBlacklistFromServer`);const i={From_Account:this._core.common.getCurrentUserID(),MaxLimited:r,StartIndex:this._startIndex},o=yield function(t,r){return e(this,void 0,void 0,function*(){const{helper:e,channel:i}=r,o="sns.black_list_get",n=e.generateProtocolData({servcmd:o,data:t}),s=`${n.head.seq}${o}`,d=yield i.sendPacket(n,{requestId:s}),{BlackListItem:a=[],StartIndex:c=0}=d;return{blackList:B(a),startIndex:c}})}(i,this._core),{blackList:n=[],startIndex:s=0}=o;return this._startIndex=s,this._addBlacklistMap(n),this._emitBlackListUpdated(),0!==this._startIndex&&this._retryCount<10?(this._retryCount++,yield this.syncBlacklistFromServer()):this._retryCount=0,{code:0,data:Array.from(this._blacklistMap.keys())}}catch(e){throw this._retryCount=0,e}})}addToBlacklist(t){return e(this,void 0,void 0,function*(){const{helper:r}=this._core;try{const r=this._preprocessBlacklist(t),i={From_Account:this._core.common.getCurrentUserID(),To_Account:r},o=yield function(t,r){return e(this,void 0,void 0,function*(){const{helper:e,channel:i}=r,o="sns.black_list_add",n=e.generateProtocolData({servcmd:o,data:t}),s=`${n.head.seq}${o}`,d=yield i.sendPacket(n,{requestId:s}),{ResultItem:a=[]}=d;return{blackList:B(a)}})}(i,this._core),{blackList:n=[]}=o;return this._addBlacklistMap(n),this._emitBlackListUpdated(),{code:0,data:Array.from(this._blacklistMap.keys())}}catch(e){const{errorCode:t}=e||{};throw new r.ChatError({code:t})}})}removeFromBlacklist(t){return e(this,void 0,void 0,function*(){const{helper:r}=this._core;try{const r={From_Account:this._core.common.getCurrentUserID(),To_Account:t.userIDList},i=yield function(t,r){return e(this,void 0,void 0,function*(){const{helper:e,channel:i}=r,o="sns.black_list_delete",n=e.generateProtocolData({servcmd:o,data:t}),s=`${n.head.seq}${o}`,d=yield i.sendPacket(n,{requestId:s}),{ResultItem:a=[]}=d;return{blackList:B(a)}})}(r,this._core),{blackList:o=[]}=i;return this._deleteBlacklistMap(o),this._emitBlackListUpdated(),{code:0,data:Array.from(this._blacklistMap.keys())}}catch(e){const{errorCode:t}=e||{};throw new r.ChatError({code:t})}})}dispose(){this.reset()}reset(){this._retryCount=0,this._startIndex=0,this._blacklistMap.clear()}updateBlacklist(e){const{ssoLog:r,utils:{safeStringify:i}}=this._core;r.debug("updateBlacklist",`${t} updateBlacklist data: ${i(e)}`);const{addedBlacklist:o=[],removedBlacklist:n=[]}=e;let s=!1;if(o.length>0){o.some(e=>!this._blacklistMap.has(e))&&(s=!0,this._addBlacklistMap(o.map(e=>({userID:e}))))}if(n.length>0){n.some(e=>this._blacklistMap.has(e))&&(s=!0,this._deleteBlacklistMap(n.map(e=>({userID:e}))))}s&&this._emitBlackListUpdated()}_addBlacklistMap(e){const{ssoLog:r,utils:{safeStringify:i}}=this._core;r.debug("_addBlacklistMap",`${t} _addBlacklistMap blackList: ${i(e)}`),e.forEach(e=>{const{userID:t,addBlackTimeStamp:r=Date.now()}=e;t&&!this._blacklistMap.has(t)&&this._blacklistMap.set(t,{userID:t,addBlackTimeStamp:r})})}_deleteBlacklistMap(e){const{ssoLog:r,utils:{safeStringify:i}}=this._core;r.debug("_deleteBlacklistMap",`${t} _deleteBlacklistMap blackList: ${i(e)}`),e.forEach(e=>{const{userID:t}=e;t&&this._blacklistMap.has(t)&&this._blacklistMap.delete(t)})}_preprocessBlacklist(e){let{userIDList:t}=e;const r=this._core.common.getCurrentUserID();if(1===t.length&&t[0]===r)throw new this._core.helper.ChatError({functionName:"addToBlacklist",code:d});return t.includes(r)&&(t=e.userIDList.filter(e=>e!==r)),t}_emitBlackListUpdated(){const{notificationCenter:e,OuterEvent:r,ssoLog:i,utils:{safeStringify:o}}=this._core,n=Array.from(this._blacklistMap.keys());i.debug(`${t} _emitBlackListUpdated blackList:${o(n)}`),e.emitOuterEvent(r.BLACKLIST_UPDATED,{name:r.BLACKLIST_UPDATED,data:n})}};const x={addToBlacklist:{userIDList:{required:!0,rules:["array"],allowEmpty:!1}},removeFromBlacklist:{userIDList:{required:!0,rules:["array"],allowEmpty:!1}},getFriendProfile:{userIDList:{required:!0,rules:["array"],allowEmpty:!1}},addFriend:{to:{required:!0,rules:["string"],allowEmpty:!1},source:{required:!0,rules:["string"],allowEmpty:!1},wording:{required:!1,rules:["string"],allowEmpty:!0},type:{required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>{const{OuterConstant:t}=y.core;return e===t.SNS_ADD_TYPE_SINGLE||e===t.SNS_ADD_TYPE_BOTH||"type should be SNS_ADD_TYPE_SINGLE or SNS_ADD_TYPE_BOTH"}},remark:{required:!1,rules:["string"],allowEmpty:!0},groupName:{required:!1,rules:["string"],allowEmpty:!0}},deleteFriend:{userIDList:{required:!0,rules:["array"],allowEmpty:!1},type:{required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>{const{OuterConstant:t}=y.core;return e===t.SNS_DELETE_TYPE_SINGLE||e===t.SNS_DELETE_TYPE_BOTH||"type should be SNS_DELETE_TYPE_SINGLE or SNS_DELETE_TYPE_BOTH"}}},checkFriend:{userIDList:{required:!0,rules:["array"],allowEmpty:!1},type:{required:!1,rules:["string"],allowEmpty:!0}},updateFriend:{userID:{required:!0,rules:["string"],allowEmpty:!1},remark:{required:!1,rules:["string"],allowEmpty:!0},friendCustomField:{required:!1,rules:["array"],allowEmpty:!0}},acceptFriendApplication:{userID:{required:!0,rules:["string"],allowEmpty:!1},remark:{required:!1,rules:["string"],allowEmpty:!0},type:{required:!1,rules:["string"],allowEmpty:!0}},refuseFriendApplication:{userID:{required:!0,rules:["string"],allowEmpty:!1}},deleteFriendApplication:{userID:{required:!0,rules:["string"],allowEmpty:!1},type:{required:!0,rules:["string"],allowEmpty:!1,customValidator:e=>{const{OuterConstant:t}=y.core;return e===t.SNS_APPLICATION_SENT_TO_ME||e===t.SNS_APPLICATION_SENT_BY_ME||"type should be SNS_APPLICATION_SENT_TO_ME or SNS_APPLICATION_SENT_BY_ME"}}},createFriendGroup:{name:{required:!0,rules:["string"],allowEmpty:!1},userIDList:{required:!0,rules:["array"],allowEmpty:!1}},deleteFriendGroup:{name:{required:!0,rules:["string"],allowEmpty:!1}},addToFriendGroup:{name:{required:!0,rules:["string"],allowEmpty:!1},userIDList:{required:!0,rules:["array"],allowEmpty:!1}},removeFromFriendGroup:{name:{required:!0,rules:["string"],allowEmpty:!1},userIDList:{required:!0,rules:["array"],allowEmpty:!1}},renameFriendGroup:{oldName:{required:!0,rules:["string"],allowEmpty:!1},newName:{required:!0,rules:["string"],allowEmpty:!1}}},$={getBlacklist:!0,addToBlacklist:!0,removeFromBlacklist:!0,getFriendList:!0,addFriend:!0,deleteFriend:!0,updateFriend:!0,checkFriend:!0,getFriendProfile:!0,getFriendApplicationList:!0,acceptFriendApplication:!0,refuseFriendApplication:!0,deleteFriendApplication:!0,setFriendApplicationRead:!0,getFriendGroupList:!0,createFriendGroup:!0,deleteFriendGroup:!0,addToFriendGroup:!0,removeFromFriendGroup:!0,renameFriendGroup:!0};return new class{constructor(){this.name="Friend"}install(e){e.ssoLog.debug(`${t} install`),this._core=e;const{helper:r,notificationCenter:i,InnerEvent:o,constants:{WORKFLOW_NAME:n,WORKFLOW_STEP:s}}=e;y.init(e),g.init(e),T.init(e),b.init(e),q.init(e),r.registerValidateConfig({auth:$,params:x}),r.registerWorkflowStep(n.SYNC_SERVER_INFO_AFTER_LOGIN,s.FRIEND_AND_BLACKLIST_SYNC,this.handleLoginSuccess,this),i.subscribeInnerEvent(o.LOGOUT,this.reset,this),i.subscribeInnerEvent(o.MESSAGE_PUSH,i.InnerEventSubType.FRIEND_LIST_MODIFIED,this.onFriendModified,this)}handleLoginSuccess(){q.syncBlacklistFromServer(),g.syncFriendListFromServer(),T.syncFriendApplicationListFromServer(),b.syncFriendGroupListFromServer()}onFriendModified(e){const{from:t,addedBlacklist:r,removedBlacklist:i,addedFriendList:o,removedFriendList:n,addedApplicationList:s,removedApplicationList:d,friendUpdateInfo:a,reportTime:c}=this._parseFriendListModification(null==e?void 0:e.FriendListMod[0]),{isEmpty:u}=this._core.utils;u(r)&&u(i)||q.updateBlacklist({addedBlacklist:r,removedBlacklist:i}),u(o)&&u(n)||(g.updateFriendList({addedFriendList:o,removedFriendList:n}),T.updateFriendApplicationList({from:t,removedApplicationList:o})),u(a)||g.updateFriendInfo(a),u(s)&&u(d)||T.updateFriendApplicationList({from:t,addedApplicationList:s,removedApplicationList:d}),c&&T.onApplicationRead()}_parseFriendListModification(e){const{PushType:t,From_Account:r,To_Account:i,ReportTime:o,BlackListAdd_Account:n=[],BlackListDel_Account:s=[],FriendAdd_Account:d=[],FriendDel_Account:a=[],PendencyAdd:c=[],FrienPencydDel_Account:u=[],FriendUpInfo:l=[]}=e,p=l.map(e=>({userID:e.Friend_Account,tagValueList:[...e.SNS]}));return{pushType:t,from:r,to:i,reportTime:o,addedBlacklist:n,removedBlacklist:s,addedFriendList:d,removedFriendList:a,addedApplicationList:c.map(e=>({userID:e.PendencyAdd_Account,source:e.AddSource,time:e.AddTime,wording:e.AddWording,nick:e.ProfileImNick})),friendUpdateInfo:p,removedApplicationList:u}}reset(){q.reset(),g.reset(),T.reset(),b.reset()}dispose(){const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this.reset,this),e.unSubscribeInnerEvent(t.MESSAGE_PUSH,e.InnerEventSubType.FRIEND_LIST_MODIFIED,this.onFriendModified,this),q.dispose(),g.dispose(),T.dispose(),b.dispose()}}});