@tencentcloud/lite-chat
Version:
Tencent Cloud Chat SDK for Web/Mini Program/uni-app/React Native/Donut
2 lines (1 loc) • 9.66 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{c(o.next(e))}catch(e){n(e)}}function i(e){try{c(o.throw(e))}catch(e){n(e)}}function c(e){var r;e.done?s(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(a,i)}c((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={required:!1,rules:["array"],allowEmpty:!0,customValidator:e=>!e||(!!(Array.isArray(e)&&e.length<=5)||"keywordList should be an array and length <= 5")},i={required:!1,rules:["string"],allowEmpty:!0,customValidator:e=>!e||(!![n.OR,n.AND].includes(e)||"keywordListMatchType should be OR or AND")},c={required:!1,rules:["number"],allowEmpty:!0,customValidator:e=>"number"==typeof e&&e>=1&&e<=100||"count must be a number between 1 and 100"},u={required:!1,rules:["string"],allowEmpty:!0},d={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"}},l=e=>({required:!1,rules:["number"],allowEmpty:!0,customValidator:r=>"number"==typeof r&&r>=0||`${e} should be a number >= 0';`}),h={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"}},m={searchCloudMessages:{keywordList:a,keywordListMatchType:i,cursor:u,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:l("timePosition"),timePeriod:l("timePeriod")},searchCloudUsers:{keywordList:a,keywordListMatchType:i,cursor:u,count:c,miniBirthday:l("miniBirthday"),maxBirthday:l("maxBirthday"),gender:h},searchCloudGroupMembers:{keywordList:a,keywordListMatchType:i,cursor:u,count:c,groupTypeList:d}},y={searchCloudMessages:!0,searchCloudUsers:!0,searchCloudGroupMembers:!0};return new class{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.registerValidateConfig({auth:y,params:m})}searchCloudMessages(s){return r(this,void 0,void 0,(function*(){try{const{OuterConstant:o,helper:n,ssoLog:a}=this._core,i=a.createSSOLogData({method:"searchCloudMessages"}),{conversationID:c,timePeriod:u,timePosition:d}=s,l=e(s,["conversationID","timePeriod","timePosition"]),h=Object.assign({count:10},l);c&&(c.startsWith(o.CONV_C2C)?h.account=c.replace(o.CONV_C2C,""):c.startsWith(o.CONV_GROUP)&&(h.groupID=c.replace(o.CONV_GROUP,""))),this._setTimeRangeParams(h,{timePeriod:u,timePosition:d});const m=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:c,cursor:u,account:d,groupID:l}=e,h={Count:r,KeywordList:o,MatchType:s,SendUserIDList:n,MsgTypeList:a,EndTime:i,StartTime:c,Cursor:u,PeerAccount:d,GroupID:l};return t.core.common.buildAndSendPacket({servcmd:"message_search.query",data:h})}))}(h);if(!m)return{code:0,data:{}};const{ErrorCode:y,ErrorInfo:p,TotalCount:g,Cursor:C="",ConversationMsgs:f=[]}=m;if(0!==y)throw{errorCode:y,errorInfo:p};const L=`keywordList:${s.keywordList} keywordListMatchType:${s.keywordListMatchType} cursor:${s.cursor} res: totalCount:${g}`;i.setLogInfo({message:L}).end();const M=f.map((e=>{const{MsgList:r,Count:t,GroupID:a,UserID:i}=e,c=a?`${o.CONV_GROUP}${a}`:`${o.CONV_C2C}${i}`;if(this._isSearchingAllConversations(s)&&t>1)return{conversationID:c,messageCount:t,messageList:[]};const u=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:c,messageCount:t,messageList:u}}));return{code:0,data:{searchResultList:M,cursor:C,totalCount: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 s=this._core.ssoLog.createSSOLogData({method:"searchCloudUsers"}),{keywordListMatchType:i,count:c=20}=o,u=e(o,["keywordListMatchType","count"]),d=Object.assign({count:c,keywordListMatchType:i===n.AND?1:0},u);this._setBirthdayRangeParams(d,o);const l=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:c}=e,u={Count:r,Keywords:o,KeywordMatchType:s,Cursor:i,UserBirthStart:n,UserBirthEnd:a,Gender:c};return t.core.common.buildAndSendPacket({servcmd:"user_search.query",data:u})}))}(d);if(!l)return{error:0,data:{}};const{ErrorCode:h,ErrorInfo:m,TotalCount:y,Cursor:p="",Users:g=[]}=l;if(0!==h)throw{errorCode:h,errorInfo:m};const C=`keywordList:${o.keywordList} keywordListMatchType:${o.keywordListMatchType} cursor:${o.cursor} count:${o.count} res: totalCount:${y}`;s.setLogInfo({message:C}).end();const f=[];for(let e=0,r=g.length;e<r;e++){const{UserID:r,ProfileItems:t}=g[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:p,totalCount:y}}}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 s=this._core.ssoLog.createSSOLogData({method:"searchCloudGroupMembers"}),{count:a=20,keywordListMatchType:i}=o,c=e(o,["count","keywordListMatchType"]),u=Object.assign({count:a,keywordListMatchType:i===n.AND?1:0},c),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,c={Count:r,Keywords:o,KeywordMatchType:s,Cursor:a,GroupType:n,GroupIdList:i};return t.core.common.buildAndSendPacket({servcmd:"group_member_search.query",data:c})}))}(u);if(!d)return{code:0,data:{}};const{ErrorCode:l,ErrorInfo:h,GroupMembers:m,Cursor:y,TotalCount:p}=d;if(0!==l)throw{errorCode:l,errorInfo:h};const g=`keywordList:${o.keywordList} keywordListMatchType:${o.keywordListMatchType} cursor:${o.cursor} count:${o.count} res: totalCount:${p}`;s.setLogInfo({message:g}).end();const C=new Map;return m.forEach((e=>{const{GroupID:r,GroupName:t,GroupType:o,GroupFaceUrl:s,GroupMemberUserName:n,GroupMemberUserID:a,GroupMemberNameCard:i}=e,c={groupID:r,name:t,type:o,avatar:s},u={userID:a,nick:n,nameCard:i};if(C.has(r)){const e=C.get(r);e.memberList.push(u),C.set(r,e)}else C.set(r,{groupInfo:c,memberList:[u]})})),{code:0,data:{searchResultList:[...C.values()],cursor:y,totalCount:p}}}catch(e){const{errorCode:r,errorInfo:t}=e||{};this._handleError({errorCode:r,errorInfo:t,searchType:s,functionName:"searchCloudGroupMembers"})}}))}_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,functionName:s}){const{ssoLog:n,helper:a}=this._core;let i=e;60020===e?i="SearchUnable":t!==o&&27003===e?i="SearchParamsError":t!==o&&60018===e&&(i="SearchOverLimit");const c=new a.ChatError({code:i,message:r});throw n.getSSOLogData(s).setError(c).end(),c}_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)}}}));