UNPKG

@tencentcloud/lite-chat

Version:

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

2 lines (1 loc) 39 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 I(t,r){return e(this,void 0,void 0,(function*(){return function(e){return v(e,"ResultItem",(e=>({to:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo})))}(yield r.common.buildAndSendPacket({servcmd:"sns.friend_update",data:t}))}))}function F(t,r){return e(this,void 0,void 0,(function*(){return function(e){return v(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 v(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 A={[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 g=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=A[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=A[r];null==o||o(this,i)}))}};var L=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{userStore:e}=this._core.appStore,r=[...e.getFriendMap().values()];return console.log(`${t} getFriendListFromCache, data `,r),{code:0,data:r}}))}syncFriendListFromServer(){return e(this,void 0,void 0,(function*(){var r;try{console.log(`${t} syncFriendListFromServer`);const i={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},o=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}))}))}(i,this._core),{friendList:n,nextStartIndex:s,standardSequence:d,customSequence:a,completeFlag:c}=o;this._startIndex=s,this._standardSequence=d,this._customSequence=a,this._processFriendList(n),0===c?yield this.syncFriendListFromServer():(this._emitFriendListUpdated(),this._fetchFriendProfile()),console.log(`${t} syncFriendListFromServer ok`)}catch(e){throw console.error(`${t} syncFriendListFromServer error:`,e),e}}))}getFriendProfile(i){return e(this,void 0,void 0,(function*(){var o;try{console.log(`${t} getFriendProfile params:`,i);let{userIDList:n}=i;n.length>r&&(n=n.slice(0,r));const{friendUserIdList:s,unFriendUserIdList:d}=this.partitionByIsFriend(n),a=this._createNonFriendErrorList(d);if(0===s.length)return{code:0,data:{friendList:[],failureUserIDList:a}};const c={From_Account:null===(o=this._core.store.get("login"))||void 0===o?void 0:o.userId,To_Account:s},u=(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}))}))}(c,this._core))||{},{successProfiles:l,failedProfiles:p}=this._processFriendProfileResponse(u);return{code:0,data:{friendList:l,failureUserIDList:[...p,...a]}}}catch(e){throw console.error(`${t} getFriendProfile error:`,e),e}}))}addFriend(r){return e(this,void 0,void 0,(function*(){var i;try{console.log(`${t} addFriend options:`,r);const{helper:o,store:n}=this._core,{to:s,source:d,type:c,wording:u,remark:l,groupName:p}=r,h={From_Account:null===(i=n.get("login"))||void 0===i?void 0:i.userId,AddFriendItem:[{To_Account:s,AddSource:d,AddWording:u,GroupName:p,Remark:l}],AddType:c},m=yield function(t,r){return e(this,void 0,void 0,(function*(){return v(yield r.common.buildAndSendPacket({servcmd:"sns.friend_add",data:t}))}))}(h,this._core),{resultList:_=[]}=m,{userID:f,resultCode:I}=_[0];if(0===I)return console.log(`${t} addFriend ok`),{code:0,data:{userID:f}};if(I===a)return console.log(`${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){throw console.error(`${t} addFriend error:`,e),e}}))}deleteFriend(i){return e(this,void 0,void 0,(function*(){var o;try{console.log(`${t} deleteFriend options:`,i);const{store:n}=this._core;let{userIDList:s,type:d}=i;const a=null===(o=n.get("login"))||void 0===o?void 0:o.userId;s.length>1e3&&(console.warn(`${t} deleteFriend the length of userIDList cannot exceed:`,1e3),s=s.slice(0,r));const{friendUserIdList:c,unFriendUserIdList:u}=this.partitionByIsFriend(s);if(0===c.length)return{code:0,data:{friendList:[],failureUserIDList:this._createNonFriendErrorList(u)}};const l={From_Account:a,To_Account:c,DeleteType:d},p=yield function(t,r){return e(this,void 0,void 0,(function*(){return v(yield r.common.buildAndSendPacket({servcmd:"sns.friend_delete",data:t}))}))}(l,this._core),{successUserIDList:h,failureUserIDList:m}=this._processOperationResult({resultList:p.resultList,includeRelation:!1});return console.log(`${t} deleteFriend ok`),{code:0,data:{successUserIDList:h,failureUserIDList:m}}}catch(e){throw console.error(`${t} deleteFriend error:`,e),e}}))}updateFriend(r){return e(this,void 0,void 0,(function*(){var e;try{console.log(`${t} updateFriend options:`,r);const{userID:o,remark:n,friendCustomField:s}=r,{helper:d,store:a,appStore:{userStore:c}}=this._core,u=null===(e=a.get("login"))||void 0===e?void 0:e.userId;if(!c.getFriendMap().has(o))throw new d.ChatError({functionName:"updateFriend",code:i});const l={From_Account:u,UpdateItem:[{To_Account:o,SnsItem:this._buildSnsItemList(n,s)}]},p=yield I(l,this._core),{resultList:h}=p,{to:m,resultCode:_,resultInfo:F}=h[0];if(f(_)||0===_){return{code:0,data:this._updateLocalFriend({userID:m,remark:n,friendCustomField:s})}}throw new d.ChatError({functionName:"updateFriend",code:_,message:F})}catch(e){throw console.error(`${t} updateFriend error:`,e),e}}))}checkFriend(r){return e(this,void 0,void 0,(function*(){var e;try{console.log(`${t} checkFriend options:`,r);const{userIDList:i,type:o}=r,{OuterConstant:n,store:s}=this._core,d=null===(e=s.get("login"))||void 0===e?void 0:e.userId,a={From_Account:d,To_Account:i,CheckType:o===n.SNS_CHECK_TYPE_SINGLE?o:n.SNS_CHECK_TYPE_BOTH},c=yield F(a,this._core),{successUserIDList:u=[],failureUserIDList:l=[]}=this._processOperationResult({resultList:c.resultList,includeRelation:!0});return{code:0,data:{successUserIDList:u,failureUserIDList:l}}}catch(e){throw console.error(`${t} checkFriend error:`,e),e}}))}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*(){console.log(`${t} updateFriendList data:`,r);const{userStore:e}=this._core.appStore,{addedFriendList:i=[],removedFriendList:o=[]}=r;i.length>0&&(i.forEach((t=>{const r=new g({userID:t,core:this._core});e.getFriendMap().set(t,r)})),yield this.getFriendProfile({userIDList:i}),this._emitFriendListUpdated()),o.length>0&&(o.forEach((t=>{e.getFriendMap().delete(t)})),this._emitFriendListUpdated())}))}updateFriendInfo(e){console.log(`${t} updateFriendInfo data: `,e);const{appStore:{userStore:r,conversationStore:i},utils:{isEmpty:o}}=this._core,{userID:n,tagValueList:s}=e[0]||{},d=r.getFriend(n);if(o(s)||!d)return;const a=s.map((e=>({tag:e.BytesTag,value:e.BytesValue})));a.forEach((e=>{var t;const{tag:r,value:o}=e,s=null===(t=i.getConversation(`C2C${n}`))||void 0===t?void 0:t.remark;r===u.REMARK&&o!==s&&i.updateConversation(`C2C${n}`,{remark:o}),d.update(a)})),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 g({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*(){try{console.log(`${t} _fetchFriendProfile`);const{appStore:{userStore:e},user:{userProfile:r}}=this._core,i=[...e.getFriendMap().keys()],o=100,n=Math.ceil(i.length/o);for(let t=0;t<n;t++){const n=t*o,s=Math.min(n+o,i.length),d=i.slice(n,s),a=yield null==r?void 0:r.getUserProfile({userIDList:d});if(a&&a.data){a.data.forEach((t=>{const r=e.getFriend(t.userID);r&&(r.profile=t)}))}this._emitFriendListUpdated()}}catch(e){throw console.error(`${t} _fetchFriendProfile error:`,e),e}}))}_emitFriendListUpdated(){const{notificationCenter:e,OuterEvent:r,appStore:{userStore:i}}=this._core,o=Array.from(i.getFriendMap().values());console.log(`${t} _emitFriendListUpdated friendList:`,o),e.emitOuterEvent(r.FRIEND_LIST_UPDATED,{name:r.FRIEND_LIST_UPDATED,data:o})}_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:r=[]}=e,{userStore:i}=this._core.appStore,o=[],n=[];return r.forEach((e=>{const{userID:t,resultCode:r,resultInfo:s,profileItem:d}=e;if(0===r){const e=new g({userID:t,tagValueList:d,core:this._core});o.push(e),i.getFriendMap().set(t,e)}else n.push({userID:t,code:r,message:s})})),console.log(`${t} _processFriendProfileResponse successProfiles:`,o),{successProfiles:o,failedProfiles:n}}partitionByIsFriend(e){const r=[],i=[];return e.forEach((e=>{const{userStore:t}=this._core.appStore;t.getFriendMap().has(e)?r.push(e):i.push(e)})),console.log(`${t} partitionByIsFriend, friendUserIdList: ${r}, unFriendUserIdList: ${i}`),{friendUserIdList:r,unFriendUserIdList:i}}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 T(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 T(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 T(yield i.buildAndSendPacket({servcmd:"sns.pendency_delete",data:o}))}))}function T(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 C=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 e=[...this._friendApplicationMap.values()];return console.log(`${t} getFriendApplicationListFromCache friendApplicationList:`,e),{code:0,data:{friendApplicationList:e,unreadCount:this._unreadCount}}}))}syncFriendApplicationListFromServer(){return e(this,void 0,void 0,(function*(){const i="syncFriendApplicationListFromServer";console.log(`${t} ${i}`);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,image: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:i,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${t} ${i} error:`,r),r}}))}acceptFriendApplication(t){return e(this,void 0,void 0,(function*(){return 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*(){const r="setFriendApplicationRead";console.log(`${t} ${r}`);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(),console.log(`${t} ${r} ok`),{code:0,data:{}}}catch(e){const i=new this._core.helper.ChatError({functionName:r,code:null==e?void 0:e.errorCode,message:null==e?void 0:e.errorInfo});throw console.error(`${t} ${r} error:`,i),i}}))}updateFriendApplicationList(e){const{from:r,addedApplicationList:i=[],removedApplicationList:o=[]}=e;console.log(`${t} updateFriendApplicationList data:`,e);const{isEmpty:n}=this._core.utils;n(i)||this.onApplicationAdded(i,r),n(o)||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(){console.log(`${t} _emitApplicationListUpdated`);const{notificationCenter:e,OuterEvent:r}=this._core;e.emitOuterEvent(r.FRIEND_APPLICATION_LIST_UPDATED,{name:r.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 M(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 M(e,(e=>({userID:e.To_Account,resultCode:e.ResultCode,resultInfo:e.ResultInfo})))}(yield i.buildAndSendPacket({servcmd:"sns.group_add",data:o}))}))}function P(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 M(e)}(yield e.buildAndSendPacket({servcmd:"sns.group_delete",data:r}))}))}function G(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 M(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 w(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 M(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 U(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 M(e)}(yield e.buildAndSendPacket({servcmd:"sns.group_update",data:o}))}))}function R(e,t){return e.filter((e=>L.isMyFriend(e))).map((e=>({To_Account:e,UpdateType:t})))}function M(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 $=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 e=[...this._friendGroupMap.values()];return console.log(`${t} getFriendGroupListFromCache, data: `,e),{code:0,data:e}}))}syncFriendGroupListFromServer(){return e(this,void 0,void 0,(function*(){const e="syncFriendGroupListFromServer";console.log(`${t} ${e}`);try{const r=yield k(),{resultList:i=[]}=r;this._core.helper.isEmpty(i)&&console.log(`${t} ${e} ok. count: 0`),i.forEach((e=>{const t=new O(e);this._friendGroupMap.set(e.name,t)})),this._emitGroupListUpdated()}catch(t){this._handleError(e,t)}}))}createFriendGroup(r){return e(this,void 0,void 0,(function*(){const e="createFriendGroup";console.log(`${t} ${e} params:`,r);try{const{name:t}=r;this._checkGroupExist(e,t);const i=yield N(r),{successUserIDList:o,failureUserIDList:n}=this._parseCreateGroupResponse(i),s=new O({name:t,userIDList:o});return this._friendGroupMap.set(t,s),this._emitGroupListUpdated(),{code:0,data:{friendGroup:s,failureUserIDList:n}}}catch(t){this._handleError(e,t)}}))}deleteFriendGroup(r){return e(this,void 0,void 0,(function*(){const e="deleteFriendGroup";console.log(`${t} ${e} params:`,r);try{const{name:t}=r;this._checkGroupNotExist(e,t),yield P(r);const i=this._friendGroupMap.get(t);return i&&(this._updateFriendGroup({name:t,userIDList:i.userIDList,action:m}),this._friendGroupMap.delete(t)),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:G})}))}removeFromFriendGroup(t){return e(this,void 0,void 0,(function*(){return this._updateFriendGroupMember({functionName:"removeFromFriendGroup",params:t,protocolFunction:w})}))}renameFriendGroup(r){return e(this,void 0,void 0,(function*(){const e="renameFriendGroup";console.log(`${t} ${e} params:`,r);try{const{oldName:t,newName:i}=r;this._checkGroupNotExist(e,t),this._checkGroupExist(e,i),yield U(r);const o=this._updateGroupNameInCache(t,i);return o&&this._updateFriendGroup({name:t,userIDList:o.userIDList,action:_,newName:i}),this._emitGroupListUpdated(),{code:0,data:o}}catch(t){this._handleError(e,t)}}))}_updateFriendGroupMember(r){return e(this,void 0,void 0,(function*(){const{functionName:e,params:i,protocolFunction:o}=r;console.log(`${t} ${e} params:`,i);try{const{name:t}=i;this._checkGroupNotExist(e,t);const r=yield o(i),{friendGroup:n,failureUserIDList:s}=this._parseUpdateFriendGroupResponse(t,r);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:r}=this._core,i=[...this._friendGroupMap.values()];console.log(`${t} _emitGroupListUpdated friendGroupList:`,i),e.emitOuterEvent(r.FRIEND_GROUP_LIST_UPDATED,{name:r.FRIEND_GROUP_LIST_UPDATED,data:i})}_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:r,userIDList:i,action:o,newName:n}=e,{userStore:s}=this._core.appStore;console.log(`${t} updateFriendGroup`,{name:r,userIDList:i,action:o,newName:n}),r&&!this._core.utils.isEmpty(i)&&i.forEach((e=>{const t=s.getFriend(e);if(!t)return;const{groupList:i}=t;switch(o){case h:-1===i.indexOf(r)&&(i.push(r),t.count=i.length);break;case m:{const e=i.indexOf(r);e>-1&&(i.splice(e,1),t.count=i.length);break}case _:{if(!n)return;const e=i.indexOf(r);-1!==e&&(i[e]=n);break}}}))}_handleError(e,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}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 b=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 console.log(`${t} getBlacklistFromCache`),{code:0,data:Array.from(this._blacklistMap.keys())}}))}syncBlacklistFromServer(){return e(this,void 0,void 0,(function*(){try{console.log(`${t} syncBlacklistFromServer`);const i={From_Account:this._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,console.error(`${t} syncBlacklistFromServer error:`,e),e}}))}addToBlacklist(r){return e(this,void 0,void 0,(function*(){try{console.log(`${t} addToBlacklist options:`,r);const i=this._preprocessBlacklist(r),o={From_Account:this._getCurrentUserId(),To_Account:i},n=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)}}))}(o,this._core),{blackList:s=[]}=n;return this._addBlacklistMap(s),this._emitBlackListUpdated(),{code:0,data:Array.from(this._blacklistMap.keys())}}catch(e){throw console.error(`${t} addToBlacklist error:`,e),e}}))}removeFromBlacklist(r){return e(this,void 0,void 0,(function*(){try{console.log(`${t} removeFromBlacklist options:`,r);const i={From_Account:this._getCurrentUserId(),To_Account:r.userIDList},o=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)}}))}(i,this._core),{blackList:n=[]}=o;return this._deleteBlacklistMap(n),this._emitBlackListUpdated(),{code:0,data:Array.from(this._blacklistMap.keys())}}catch(e){throw console.error(`${t} removeFromBlacklist error:`,e),e}}))}dispose(){this.reset()}reset(){this._retryCount=0,this._startIndex=0,this._blacklistMap.clear()}updateBlacklist(e){console.log(`${t} updateBlacklist data:`,e);const{addedBlacklist:r=[],removedBlacklist:i=[]}=e;let o=!1;if(r.length>0){r.some((e=>!this._blacklistMap.has(e)))&&(o=!0,this._addBlacklistMap(r.map((e=>({userID:e})))))}if(i.length>0){i.some((e=>this._blacklistMap.has(e)))&&(o=!0,this._deleteBlacklistMap(i.map((e=>({userID:e})))))}o&&this._emitBlackListUpdated()}_addBlacklistMap(e){console.log(`${t} _addBlacklistMap blackList:`,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){console.log(`${t} _deleteBlacklistMap blackList:`,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._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}_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}_emitBlackListUpdated(){const{notificationCenter:e,OuterEvent:r}=this._core,i=Array.from(this._blacklistMap.keys());console.log(`${t} _emitBlackListUpdated blackList:`,i),e.emitOuterEvent(r.BLACKLIST_UPDATED,{name:r.BLACKLIST_UPDATED,data:i})}};const q={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:!0,rules:["string"],allowEmpty:!1,customValidator:e=>{const{OuterConstant:t}=y.core;return e===t.SNS_APPLICATION_AGREE||e===t.SNS_APPLICATION_AGREE_AND_ADD||"type should be SNS_APPLICATION_AGREE or SNS_APPLICATION_AGREE_AND_ADD"}}},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}}},x={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{install(e){console.log(`${t} install`),this._core=e;const{helper:r,notificationCenter:i,InnerEvent:o,constants:{WORKFLOW_NAME:n,WORKFLOW_STEP:s}}=e;y.init(e),L.init(e),C.init(e),$.init(e),b.init(e),r.registerValidateConfig({auth:x,params:q}),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(){console.log(`${t} handleLoginSuccess`),b.syncBlacklistFromServer(),L.syncFriendListFromServer(),C.syncFriendApplicationListFromServer(),$.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)||b.updateBlacklist({addedBlacklist:r,removedBlacklist:i}),u(o)&&u(n)||(L.updateFriendList({addedFriendList:o,removedFriendList:n}),C.updateFriendApplicationList({from:t,removedApplicationList:o})),u(a)||L.updateFriendInfo(a),u(s)&&u(d)||C.updateFriendApplicationList({from:t,addedApplicationList:s,removedApplicationList:d}),c&&C.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(){console.log(`${t} reset`),b.reset(),L.reset(),C.reset(),$.reset()}dispose(){console.log(`${t} dispose`);const{notificationCenter:e,InnerEvent:r}=this._core;e.unSubscribeInnerEvent(r.LOGOUT,this.reset,this),e.unSubscribeInnerEvent(r.MESSAGE_PUSH,e.InnerEventSubType.FRIEND_LIST_MODIFIED,this.onFriendModified,this),b.dispose(),L.dispose(),C.dispose(),$.dispose()}}}));