@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app
2 lines (1 loc) • 11 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self)["TencentCloudChatCloud-search"]=r()}(this,function(){"use strict";function e(e,r){var t={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&r.indexOf(o)<0&&(t[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(o=Object.getOwnPropertySymbols(e);s<o.length;s++)r.indexOf(o[s])<0&&Object.prototype.propertyIsEnumerable.call(e,o[s])&&(t[o[s]]=e[o[s]])}return t}function r(e,r,t,o){return new(t||(t=Promise))(function(s,n){function a(e){try{u(o.next(e))}catch(e){n(e)}}function i(e){try{u(o.throw(e))}catch(e){n(e)}}function u(e){var r;e.done?s(e.value):(r=e.value,r instanceof t?r:new t(function(e){e(r)})).then(a,i)}u((o=o.apply(e,r||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const t=new class{init(e){this.core=e}};const o="message",s="user",n={OR:"or",AND:"and"},a=20,i=20,u=20,c={required:!1,rules:["array"],allowEmpty:!0,customValidator:e=>!e||(!!(Array.isArray(e)&&e.length<=5)||"keywordList should be an array and length <= 5")},d={required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>!e||(!![n.OR,n.AND].includes(e)||"keywordListMatchType should be OR or AND")},p={required:!1,rules:["number"],allowEmpty:!0,customValidator:e=>"number"==typeof e&&e>=1&&e<=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:e=>{const{OuterConstant:r}=t.core;if(!Array.isArray(e))return"groupTypeList should be an array";const o=[r.GRP_PUBLIC,r.GRP_COMMUNITY,r.GRP_WORK,r.GRP_MEETING];let s=!1;for(let r=0;r<e.length;r++)if(-1===o.indexOf(e[r])){s=!0;break}return!s||"groupTypeList should be supported group type"}},h=e=>({required:!1,rules:["number"],allowEmpty:!0,customValidator:r=>"number"==typeof r&&r>=0||`${e} should be a number >= 0';`}),m={required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>{const{OuterConstant:r}=t.core;return!![r.GENDER_FEMALE,r.GENDER_MALE].includes(e)||"gender is invalid"}},C={searchCloudMessages:{keywordList:c,keywordListMatchType:d,cursor:l,senderUserIDList:{required:!1,rules:["array"],allowEmpty:!0,customValidator:e=>!!(Array.isArray(e)&&e.length<=5)||"senderUserIDList should be an array and length <= 5"},messageTypeList:{required:!1,rules:["array"],allowEmpty:!0,customValidator:e=>{const{OuterConstant:r}=t.core,o=[r.MSG_TEXT,r.MSG_IMAGE,r.MSG_AUDIO,r.MSG_FILE,r.MSG_VIDEO,r.MSG_LOCATION,r.MSG_CUSTOM,r.MSG_MERGER];let s=!1;for(let r=0;r<e.length;r++)if(-1===o.indexOf(e[r])){s=!0;break}return!s||"messageTypeList should be supported message type"}},conversationID:{required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>{const{OuterConstant:r}=t.core;return!(!(null==e?void 0:e.startsWith(r.CONV_C2C))&&!(null==e?void 0:e.startsWith(r.CONV_GROUP))&&e!==r.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};return new class{constructor(){this.name="CloudSearch"}install(e){this._core=e,t.init(e),e.helper.registerApi({apiName:"searchCloudMessages",context:this}),e.helper.registerApi({apiName:"searchCloudUsers",context:this}),e.helper.registerApi({apiName:"searchCloudGroupMembers",context:this}),e.helper.registerApi({apiName:"searchCloudGroups",context:this}),e.helper.registerValidateConfig({auth:g,params:C})}searchCloudMessages(s){return r(this,void 0,void 0,function*(){try{const{OuterConstant:o,helper:n}=this._core,{conversationID:a,timePeriod:i,timePosition:u}=s,c=e(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(e){return r(this,void 0,void 0,function*(){const{count:r,keywordList:o,keywordListMatchType:s,senderUserIDList:n,messageTypeList:a,endTime:i,startTime:u,cursor:c,account:d,groupID:p}=e,l={Count:r,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}`,f=C.map(e=>{const{MsgList:r,Count:t,GroupID:a,UserID:i}=e,u=a?`${o.CONV_GROUP}${a}`:`${o.CONV_C2C}${i}`;if(this._isSearchingAllConversations(s)&&t>1)return{conversationID:u,messageCount:t,messageList:[]};const c=r.map(e=>n.isEmpty(a)?function(e,r){const t=r.OuterConstant.CONV_C2C,o=r.message.messageHelper.parseServerPushMessage(e),s=r.message.messageFactory.createMessage(Object.assign(Object.assign({},o),{conversationType:t,flow:"in"}));return s.setElement(o.elements),s}(e,this._core):function(e,r){const t=r.OuterConstant.CONV_GROUP,o=r.message.messageHelper.parseServerGroupMessage(e),s=r.message.messageFactory.createMessage(Object.assign(Object.assign({},o),{conversationType:t,flow:"in"}));return s.setElement(o.elements),s}(e,this._core));return{conversationID:u,messageCount:t,messageList:c}});return{code:0,data:{searchResultList:f,cursor:m,totalCount:h},successLog:{message:g}}}catch(e){const{errorCode:r,errorInfo:t}=e||{};this._handleError({errorCode:r,errorInfo:t,searchType:o,functionName:"searchCloudMessages"})}})}searchCloudUsers(o){return r(this,void 0,void 0,function*(){var a;try{const{keywordListMatchType:s,count:u=i}=o,c=e(o,["keywordListMatchType","count"]),d=Object.assign({count:u,keywordListMatchType:s===n.AND?1:0},c);this._setBirthdayRangeParams(d,o);const p=yield function(e){return r(this,void 0,void 0,function*(){const{count:r,keywordList:o,keywordListMatchType:s,miniBirthday:n,maxBirthday:a,cursor:i,gender:u}=e,c={Count:r,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}`,f=[];for(let e=0,r=C.length;e<r;e++){const{UserID:r,ProfileItems:t}=C[e];if(r===this._core.OuterConstant.USER_NOT_FOUND||""===r)continue;const o=t.map(e=>({tag:e.Tag,value:e.StrValue})),s=null===(a=this._core.user.userProfile)||void 0===a?void 0:a.createProfile(r,o);f.push(s)}return{code:0,data:{searchResultList:f,cursor:m,totalCount:h},successLog:{message:g}}}catch(e){const{errorCode:r,errorInfo:t}=e||{};this._handleError({errorCode:r,errorInfo:t,searchType:s,functionName:"searchCloudUsers"})}})}searchCloudGroupMembers(o){return r(this,void 0,void 0,function*(){try{const{count:s=u,keywordListMatchType:a}=o,i=e(o,["count","keywordListMatchType"]),c=Object.assign({count:s,keywordListMatchType:a===n.AND?1:0},i),d=yield function(e){return r(this,void 0,void 0,function*(){const{count:r,keywordList:o,keywordListMatchType:s,groupTypeList:n,cursor:a,groupIDList:i}=e,u={Count:r,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(e=>{const{GroupID:r,GroupName:t,GroupType:o,GroupFaceUrl:s,GroupMemberUserName:n,GroupMemberUserID:a,GroupMemberNameCard:i,GroupMemberAvatar:u=""}=e,c={groupID:r,name:t,type:o,avatar:s},d={userID:a,nick:n,nameCard:i,avatar:u};if(g.has(r)){const e=g.get(r);e.memberList.push(d),g.set(r,e)}else g.set(r,{groupInfo:c,memberList:[d]})}),{code:0,data:{searchResultList:[...g.values()],cursor:h,totalCount:m},successLog:{message:C}}}catch(e){const{errorCode:r,errorInfo:t}=e||{};this._handleError({errorCode:r,errorInfo:t,searchType:s,functionName:"searchCloudGroupMembers"})}})}searchCloudGroups(o){return r(this,void 0,void 0,function*(){try{const{count:s=a,keywordListMatchType:i}=o,u=e(o,["count","keywordListMatchType"]),c=Object.assign({count:s,keywordListMatchType:i===n.AND?1:0},u),d=yield function(e){return r(this,void 0,void 0,function*(){const{count:r,keywordList:o,keywordListMatchType:s,groupTypeList:n,cursor:a}=e,i={Count:r,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(e=>function(e){const{GroupFaceUrl:r,GroupID:t,GroupIntroduction:o,GroupMemberNum:s,GroupName:n,GroupOwnerTinyID:a,GroupOwnerUserID:i,GroupOwnerUserName:u,GroupType:c,GroupAddOption:d,GroupInviteOption:p}=e;return{avatar:r,groupID:t,introduction:o,memberCount:s,name:n,ownerTinyID:a,ownerID:i,ownerNick:u,type:c,joinOption:d,inviteOption:p}}(e)))||[],cursor:h,totalCount:m},successLog:{message:C}}}catch(e){const{errorCode:r,errorInfo:t}=e||{};this._handleError({errorCode:r,errorInfo:t,searchType:s,functionName:"searchCloudGroups"})}})}_setTimeRangeParams(e,{timePeriod:r,timePosition:t}){r&&r>0&&(e.startTime=t&&t>0?t-r:this._core.helper.timeManager.getServerTimeSeconds()-r),e.startTime&&e.startTime<0&&(e.startTime=void 0),t&&t>0&&(e.endTime=t)}_handleError({errorCode:e,errorMessage:r,searchType:t}){const{helper:s}=this._core;let n=e;60020===e?n="SearchUnable":t!==o&&27003===e?n="SearchParamsError":t!==o&&60018===e&&(n="SearchOverLimit");throw new s.ChatError({code:n,message:r})}_isSearchingAllConversations(e){return this._core.helper.isEmpty(e.conversationID)}_setBirthdayRangeParams(e,r){const{miniBirthday:t,maxBirthday:o}=r;void 0!==t&&(e.miniBirthday=t,void 0===o&&(e.maxBirthday=4294967295)),void 0!==o&&(e.maxBirthday=o)}}});