UNPKG

@tencentcloud/lite-chat

Version:

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

2 lines (1 loc) 10.8 kB
function r(r,e){var t={};for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&e.indexOf(o)<0&&(t[o]=r[o]);if(null!=r&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(o=Object.getOwnPropertySymbols(r);s<o.length;s++)e.indexOf(o[s])<0&&Object.prototype.propertyIsEnumerable.call(r,o[s])&&(t[o[s]]=r[o[s]])}return t}function e(r,e,t,o){return new(t||(t=Promise))(function(s,n){function a(r){try{u(o.next(r))}catch(r){n(r)}}function i(r){try{u(o.throw(r))}catch(r){n(r)}}function u(r){var e;r.done?s(r.value):(e=r.value,e instanceof t?e:new t(function(r){r(e)})).then(a,i)}u((o=o.apply(r,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const t=new class{init(r){this.core=r}};const o="message",s="user",n={OR:"or",AND:"and"},a=20,i=20,u=20,c={required:!1,rules:["array"],allowEmpty:!0,customValidator:r=>!r||(!!(Array.isArray(r)&&r.length<=5)||"keywordList should be an array and length <= 5")},d={required:!1,rules:["string"],allowEmpty:!0,customValidator:r=>!r||(!![n.OR,n.AND].includes(r)||"keywordListMatchType should be OR or AND")},p={required:!1,rules:["number"],allowEmpty:!0,customValidator:r=>"number"==typeof r&&r>=1&&r<=100||"count must be a number between 1 and 100"},l={required:!1,rules:["string"],allowEmpty:!0},y={required:!1,rules:["array"],allowEmpty:!0,customValidator:r=>{const{OuterConstant:e}=t.core;if(!Array.isArray(r))return"groupTypeList should be an array";const o=[e.GRP_PUBLIC,e.GRP_COMMUNITY,e.GRP_WORK,e.GRP_MEETING];let s=!1;for(let e=0;e<r.length;e++)if(-1===o.indexOf(r[e])){s=!0;break}return!s||"groupTypeList should be supported group type"}},h=r=>({required:!1,rules:["number"],allowEmpty:!0,customValidator:e=>"number"==typeof e&&e>=0||`${r} should be a number >= 0';`}),m={required:!1,rules:["string"],allowEmpty:!0,customValidator:r=>{const{OuterConstant:e}=t.core;return!![e.GENDER_FEMALE,e.GENDER_MALE].includes(r)||"gender is invalid"}},C={searchCloudMessages:{keywordList:c,keywordListMatchType:d,cursor:l,senderUserIDList:{required:!1,rules:["array"],allowEmpty:!0,customValidator:r=>!!(Array.isArray(r)&&r.length<=5)||"senderUserIDList should be an array and length <= 5"},messageTypeList:{required:!1,rules:["array"],allowEmpty:!0,customValidator:r=>{const{OuterConstant:e}=t.core,o=[e.MSG_TEXT,e.MSG_IMAGE,e.MSG_AUDIO,e.MSG_FILE,e.MSG_VIDEO,e.MSG_LOCATION,e.MSG_CUSTOM,e.MSG_MERGER];let s=!1;for(let e=0;e<r.length;e++)if(-1===o.indexOf(r[e])){s=!0;break}return!s||"messageTypeList should be supported message type"}},conversationID:{required:!1,rules:["string"],allowEmpty:!0,customValidator:r=>{const{OuterConstant:e}=t.core;return!(!(null==r?void 0:r.startsWith(e.CONV_C2C))&&!(null==r?void 0:r.startsWith(e.CONV_GROUP))&&r!==e.CONV_SYSTEM)||"conversationID is invalid"}},timePosition:h("timePosition"),timePeriod:h("timePeriod")},searchCloudUsers:{keywordList:c,keywordListMatchType:d,cursor:l,count:p,miniBirthday:h("miniBirthday"),maxBirthday:h("maxBirthday"),gender:m},searchCloudGroupMembers:{keywordList:c,keywordListMatchType:d,cursor:l,count:p,groupTypeList:y,groupIDList:{required:!1,rules:["array"],allowEmpty:!0}},searchCloudGroups:{keywordList:c,keywordListMatchType:d,cursor:l,count:p,groupTypeList:y}},g={searchCloudMessages:!0,searchCloudUsers:!0,searchCloudGroupMembers:!0,searchCloudGroups:!0};var w=new class{constructor(){this.name="CloudSearch"}install(r){this._core=r,t.init(r),r.helper.registerApi({apiName:"searchCloudMessages",context:this}),r.helper.registerApi({apiName:"searchCloudUsers",context:this}),r.helper.registerApi({apiName:"searchCloudGroupMembers",context:this}),r.helper.registerApi({apiName:"searchCloudGroups",context:this}),r.helper.registerValidateConfig({auth:g,params:C})}searchCloudMessages(s){return e(this,void 0,void 0,function*(){try{const{OuterConstant:o,helper:n}=this._core,{conversationID:a,timePeriod:i,timePosition:u}=s,c=r(s,["conversationID","timePeriod","timePosition"]),d=Object.assign({count:100},c);a&&(a.startsWith(o.CONV_C2C)?d.account=a.replace(o.CONV_C2C,""):a.startsWith(o.CONV_GROUP)&&(d.groupID=a.replace(o.CONV_GROUP,""))),this._setTimeRangeParams(d,{timePeriod:i,timePosition:u});const p=yield function(r){return e(this,void 0,void 0,function*(){const{count:e,keywordList:o,keywordListMatchType:s,senderUserIDList:n,messageTypeList:a,endTime:i,startTime:u,cursor:c,account:d,groupID:p}=r,l={Count:e,KeywordList:o,MatchType:s,SendUserIDList:n,MsgTypeList:a,EndTime:i,StartTime:u,Cursor:c,PeerAccount:d,GroupID:p};return t.core.common.buildAndSendPacket({servcmd:"message_search.query",data:l})})}(d);if(!p)return{code:0,data:{}};const{ErrorCode:l,ErrorInfo:y,TotalCount:h,Cursor:m="",ConversationMsgs:C=[]}=p;if(0!==l)throw{errorCode:l,errorInfo:y};const g=`keywordList:${s.keywordList} keywordListMatchType:${s.keywordListMatchType} cursor:${s.cursor} res: totalCount:${h}`,w=C.map(r=>{const{MsgList:e,Count:t,GroupID:a,UserID:i}=r,u=a?`${o.CONV_GROUP}${a}`:`${o.CONV_C2C}${i}`;if(this._isSearchingAllConversations(s)&&t>1)return{conversationID:u,messageCount:t,messageList:[]};const c=e.map(r=>n.isEmpty(a)?function(r,e){const t=e.OuterConstant.CONV_C2C,o=e.message.messageHelper.parseServerPushMessage(r),s=e.message.messageFactory.createMessage(Object.assign(Object.assign({},o),{conversationType:t,flow:"in"}));return s.setElement(o.elements),s}(r,this._core):function(r,e){const t=e.OuterConstant.CONV_GROUP,o=e.message.messageHelper.parseServerGroupMessage(r),s=e.message.messageFactory.createMessage(Object.assign(Object.assign({},o),{conversationType:t,flow:"in"}));return s.setElement(o.elements),s}(r,this._core));return{conversationID:u,messageCount:t,messageList:c}});return{code:0,data:{searchResultList:w,cursor:m,totalCount:h},successLog:{message:g}}}catch(r){const{errorCode:e,errorInfo:t}=r||{};this._handleError({errorCode:e,errorInfo:t,searchType:o,functionName:"searchCloudMessages"})}})}searchCloudUsers(o){return e(this,void 0,void 0,function*(){var a;try{const{keywordListMatchType:s,count:u=i}=o,c=r(o,["keywordListMatchType","count"]),d=Object.assign({count:u,keywordListMatchType:s===n.AND?1:0},c);this._setBirthdayRangeParams(d,o);const p=yield function(r){return e(this,void 0,void 0,function*(){const{count:e,keywordList:o,keywordListMatchType:s,miniBirthday:n,maxBirthday:a,cursor:i,gender:u}=r,c={Count:e,Keywords:o,KeywordMatchType:s,Cursor:i,UserBirthStart:n,UserBirthEnd:a,Gender:u};return t.core.common.buildAndSendPacket({servcmd:"user_search.query",data:c})})}(d);if(!p)return{error:0,data:{}};const{ErrorCode:l,ErrorInfo:y,TotalCount:h,Cursor:m="",Users:C=[]}=p;if(0!==l)throw{errorCode:l,errorInfo:y};const g=`keywordList:${o.keywordList} keywordListMatchType:${o.keywordListMatchType} cursor:${o.cursor} count:${o.count} res: totalCount:${h}`,w=[];for(let r=0,e=C.length;r<e;r++){const{UserID:e,ProfileItems:t}=C[r];if(e===this._core.OuterConstant.USER_NOT_FOUND||""===e)continue;const o=t.map(r=>({tag:r.Tag,value:r.StrValue})),s=null===(a=this._core.user.userProfile)||void 0===a?void 0:a.createProfile(e,o);w.push(s)}return{code:0,data:{searchResultList:w,cursor:m,totalCount:h},successLog:{message:g}}}catch(r){const{errorCode:e,errorInfo:t}=r||{};this._handleError({errorCode:e,errorInfo:t,searchType:s,functionName:"searchCloudUsers"})}})}searchCloudGroupMembers(o){return e(this,void 0,void 0,function*(){try{const{count:s=u,keywordListMatchType:a}=o,i=r(o,["count","keywordListMatchType"]),c=Object.assign({count:s,keywordListMatchType:a===n.AND?1:0},i),d=yield function(r){return e(this,void 0,void 0,function*(){const{count:e,keywordList:o,keywordListMatchType:s,groupTypeList:n,cursor:a,groupIDList:i}=r,u={Count:e,Keywords:o,KeywordMatchType:s,Cursor:a,GroupType:n,GroupIdList:i};return t.core.common.buildAndSendPacket({servcmd:"group_member_search.query",data:u})})}(c);if(!d)return{code:0,data:{}};const{ErrorCode:p,ErrorInfo:l,GroupMembers:y=[],Cursor:h,TotalCount:m}=d;if(0!==p)throw{errorCode:p,errorInfo:l};const C=`keywordList:${o.keywordList} keywordListMatchType:${o.keywordListMatchType} cursor:${o.cursor} count:${o.count} res: totalCount:${m}`,g=new Map;return y.forEach(r=>{const{GroupID:e,GroupName:t,GroupType:o,GroupFaceUrl:s,GroupMemberUserName:n,GroupMemberUserID:a,GroupMemberNameCard:i,GroupMemberAvatar:u=""}=r,c={groupID:e,name:t,type:o,avatar:s},d={userID:a,nick:n,nameCard:i,avatar:u};if(g.has(e)){const r=g.get(e);r.memberList.push(d),g.set(e,r)}else g.set(e,{groupInfo:c,memberList:[d]})}),{code:0,data:{searchResultList:[...g.values()],cursor:h,totalCount:m},successLog:{message:C}}}catch(r){const{errorCode:e,errorInfo:t}=r||{};this._handleError({errorCode:e,errorInfo:t,searchType:s,functionName:"searchCloudGroupMembers"})}})}searchCloudGroups(o){return e(this,void 0,void 0,function*(){try{const{count:s=a,keywordListMatchType:i}=o,u=r(o,["count","keywordListMatchType"]),c=Object.assign({count:s,keywordListMatchType:i===n.AND?1:0},u),d=yield function(r){return e(this,void 0,void 0,function*(){const{count:e,keywordList:o,keywordListMatchType:s,groupTypeList:n,cursor:a}=r,i={Count:e,Keywords:o,KeywordMatchType:s,Cursor:a,GroupType:n};return t.core.common.buildAndSendPacket({servcmd:"group_search.query",data:i})})}(c);if(!d)return{code:0,data:{}};const{ErrorCode:p,ErrorInfo:l,Groups:y,Cursor:h,TotalCount:m}=d;if(0!==p)throw{errorCode:p,errorInfo:l};const C=`keywordList:${o.keywordList} keywordListMatchType:${o.keywordListMatchType} cursor:${o.cursor} count:${o.count} res: totalCount:${m}`;return{code:0,data:{searchResultList:(null==y?void 0:y.map(r=>function(r){const{GroupFaceUrl:e,GroupID:t,GroupIntroduction:o,GroupMemberNum:s,GroupName:n,GroupOwnerTinyID:a,GroupOwnerUserID:i,GroupOwnerUserName:u,GroupType:c,GroupAddOption:d,GroupInviteOption:p}=r;return{avatar:e,groupID:t,introduction:o,memberCount:s,name:n,ownerTinyID:a,ownerID:i,ownerNick:u,type:c,joinOption:d,inviteOption:p}}(r)))||[],cursor:h,totalCount:m},successLog:{message:C}}}catch(r){const{errorCode:e,errorInfo:t}=r||{};this._handleError({errorCode:e,errorInfo:t,searchType:s,functionName:"searchCloudGroups"})}})}_setTimeRangeParams(r,{timePeriod:e,timePosition:t}){e&&e>0&&(r.startTime=t&&t>0?t-e:this._core.helper.timeManager.getServerTimeSeconds()-e),r.startTime&&r.startTime<0&&(r.startTime=void 0),t&&t>0&&(r.endTime=t)}_handleError({errorCode:r,errorMessage:e,searchType:t}){const{helper:s}=this._core;let n=r;60020===r?n="SearchUnable":t!==o&&27003===r?n="SearchParamsError":t!==o&&60018===r&&(n="SearchOverLimit");throw new s.ChatError({code:n,message:e})}_isSearchingAllConversations(r){return this._core.helper.isEmpty(r.conversationID)}_setBirthdayRangeParams(r,e){const{miniBirthday:t,maxBirthday:o}=e;void 0!==t&&(r.miniBirthday=t,void 0===o&&(r.maxBirthday=4294967295)),void 0!==o&&(r.maxBirthday=o)}};export{w as default};