UNPKG

@tencentcloud/lite-chat

Version:

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

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