likeminds-chat-beta
Version:
LikeMinds Javascript SDK for chat APIs
2 lines (1 loc) • 15 kB
JavaScript
import{getDatabase as e,ref as t,onValue as r}from"firebase/database";import{initializeApp as a}from"firebase/app";import o from"axios";import*as i from"aws-sdk";function s(){return s=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e},s.apply(this,arguments)}const n="https://betaauth.likeminds.community",c="/community/tag",d="/conversation",h="/conversation/reaction",m="/community/member/profile",u="/community/dm/status",k="/community/member",l="/chatroom/participants",$="/community/member/state",p="/conversation";class b{constructor(){this.accessToken=void 0,this.refreshToken=void 0,this.accessToken=null,this.refreshToken=null}setAccessToken(e){this.accessToken=e}getAccessToken(){return this.accessToken}setRefreshToken(e){this.refreshToken=e}getRefreshToken(){return this.refreshToken}async refreshAccessToken(){if(!this.refreshToken)throw new Error("Refresh token is not set.");try{const e=await o.post(`${n}/user/refresh`,{refreshToken:this.refreshToken});console.log("DL access=> ",e);const t=e.data.data||e.data;return this.accessToken=t.access_token,this.setRefreshToken(t.refresh_token),this.setAccessToken(t.access_token),t.access_token}catch(e){throw console.error("Failed to refresh access token:",e),e}}}class _{constructor(e,t,r){this.data=void 0,this.errorMessage=void 0,this.success=void 0,this.data=e,this.errorMessage=t,this.success=r}getData(){return this.data}getErrorMessage(){return this.errorMessage}getStatus(){return this.success}}class y{constructor(){this.tokenManager=void 0,this.xApiKey=void 0,this.xVersionCode=void 0,this.xPlatformCode=void 0,this.tokenManager=new b}setAccessToken(e){this.tokenManager.setAccessToken(e)}setRefreshToken(e){this.tokenManager.setRefreshToken(e)}setApiKey(e){this.xApiKey=e}getApiKey(){return this.xApiKey}setPlatformCode(e){this.xPlatformCode=e}getPlatformCode(){return this.xPlatformCode}setVersionCode(e){this.xVersionCode=e}getVersionCode(){return this.xVersionCode}async makeRequest(e,t){return o.request(s({url:e},t))}async makeAuthenticatedRequest(e,t){const r=s({},t,{headers:s({},null==t?void 0:t.headers,{"x-sdk-source":"chat"})}),a=e.includes("initiate");e.includes("refresh"),r.headers["Content-Type"]="application/json",r.headers["x-platform-code"]=this.xPlatformCode,r.headers["x-version-code"]=this.xVersionCode,e.includes("community/feed")&&(r.headers["x-accept-version"]="v2"),e.includes("mark_read")&&(r.headers["Content-Type"]="application/x-www-form-urlencoded"),this.tokenManager.getAccessToken&&!a&&(r.headers.Authorization=`Bearer ${this.tokenManager.getAccessToken()}`),a&&(r.headers["x-api-key"]=this.xApiKey);try{var o;const t=await this.makeRequest(e,r);return 401===t.status?(await this.tokenManager.refreshAccessToken(),r.headers.Authorization=`Bearer ${this.tokenManager.refreshAccessToken()}`,this.makeRequest(e,r).then(e=>new _(e.data.data,null,!0)).catch(e=>(console.error("Failed to make authenticated request:",e),new _(null,e.message,!1)))):new _(null==t||null==(o=t.data)?void 0:o.data,null,!0)}catch(e){return console.error("Failed to make authenticated request:",e),new _(null,e.message,!1)}}}class g{constructor(e){this.xApiKey=void 0,this.xPlatformCode=void 0,this.xVersionCode=void 0,this.xSdkSource=void 0,this.networkLibrary=new y,this.xApiKey=e.xApiKey,this.xPlatformCode=e.xPlatformCode,this.xVersionCode=e.xVersionCode,this.networkLibrary.setApiKey(this.xApiKey),this.networkLibrary.setPlatformCode(this.xPlatformCode),this.networkLibrary.setVersionCode(this.xVersionCode)}}const w=e(a({apiKey:"AIzaSyBWjDQEiYKdQbQNvoiVvvOn_cbufQzvWuo",authDomain:"collabmates-beta.firebaseapp.com",databaseURL:"https://collabmates-beta.firebaseio.com",projectId:"collabmates-beta",storageBucket:"collabmates-beta.appspot.com",messagingSenderId:"983690302378",appId:"1:983690302378:web:b2fa2c58f2351d5c1b91d3",measurementId:"G-R2PXYC9F4S"}));class f extends g{static setApiKey(e){return this.xApiKey=e,this}static setPlatformCode(e){return this.xPlatformCode=e,this}static setVersionCode(e){return this.xVersionCode=e,this}static build(){return new f({xApiKey:this.xApiKey,xPlatformCode:this.xPlatformCode,xVersionCode:this.xVersionCode,xSdkSource:this.xSdkSource})}}var A;f.xApiKey=void 0,f.xPlatformCode=void 0,f.xVersionCode=void 0,f.xSdkSource=void 0,A=f,[class extends g{constructor(...e){super(...e),this.networkLibrary=new y}getHomeFeed(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/mine?page=${e.page}`)}getInvites(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/channel/invites?channel_type=${e.channelType}&page=${e.page}&page_size=${e.pageSize}`)}sendInvites(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${n}${l}`,{method:"POST",data:{chatroom_id:e.chatroomId,is_secret:e.isSecret,chatroom_participants:e.chatroomParticipants}})}registerDevice(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/user/device/push`,{method:"POST",data:{token:e.token}})}inviteAction(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/channel/invite`,{method:"PUT",data:{channel_id:e.channelId,invite_status:e.inviteStatus}})}fbInstance(){return w}homeFeedListener(e,a){const o=t(w,a);return r(o,t=>{t.exists()&&e(t.val())})}},class extends g{constructor(...e){super(...e),this.networkLibrary=new y}getChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom?chatroom_id=${e.chatroomId}`).then(e=>e.data)}followChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/follow`,{method:"PUT",data:{collabcard_id:e.collabcardId,member_id:e.memberId,value:e.value}})}muteChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/mute`,{method:"PUT",data:{chatroom_id:e.chatroomId,value:e.value}})}markReadChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/mark_read`,{method:"POST",data:{chatroom_id:e.chatroomId}})}shareChatroomUrl(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/share?chatroom_id=${e.chatroomId}&domain=${e.domain}`)}setChatroomTopic(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/conversation/topic`,{method:"PUT",data:{chatroom_id:e.chatroomId,conversation_id:e.conversationId}})}getTaggingList(e){return this.networkLibrary.makeAuthenticatedRequest(e.chatroomId?e.isSecret?`${n}${c}?chatroom_id=${e.chatroomId}&search_name=${e.searchName}&page=${e.page}&page_size=${e.pageSize}&is_secret=${e.isSecret}`:`${n}${c}?chatroom_id=${e.chatroomId}&search_name=${e.searchName}&page=${e.page}&page_size=${e.pageSize}`:e.isSecret?`${n}${c}?feedroom_id=${e.feedroomId}&search_name=${e.searchName}&page=${e.page}&page_size=${e.pageSize}&is_secret=${e.isSecret}`:`${n}${c}?feedroom_id=${e.feedroomId}&search_name=${e.searchName}&page=${e.page}&page_size=${e.pageSize}`)}getConversation(e){return this.networkLibrary.makeAuthenticatedRequest(e.scrollDirection||e.conversationID?`${n}${d}?chatroom_id=${e.chatroomID}&paginate_by=${e.paginateBy}&conversation_id=${e.conversationID}&scroll_direction=${e.scrollDirection}`:`${n}${d}?chatroom_id=${e.chatroomID}&paginate_by=${e.paginateBy}`)}postConversation(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}`,{method:"POST",data:{chatroom_id:e.chatroomId,temporary_id:e.temporaryId,text:e.text,has_files:e.hasFiles,attachment_count:e.attachmentCount,replied_conversation_id:e.repliedConversationId,share_link:e.shareLink,og_tags:e.ogTags}})}editConversation(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}`,{method:"PUT",data:{conversation_id:e.conversationId,text:e.text,share_link:e.shareLink,og_tags:e.ogTags}})}deleteConversation(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}`,{method:"DELETE",data:{conversation_ids:e.conversationIds,reason:e.reason}})}putReaction(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${h}`,{method:"PUT",data:{chatroom_id:e.chatroomId,conversation_id:e.conversationId,reaction:e.reaction}})}deleteReaction(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${h}`,{method:"DELETE",data:{chatroom_id:e.chatroomId,conversation_id:e.conversationId,reaction:e.reaction}})}getAWS(){return i.config.region="ap-south-1",i.config.credentials=new i.CognitoIdentityCredentials({IdentityPoolId:"ap-south-1:181963ba-f2db-450b-8199-964a941b38c2"}),new i.S3({apiVersion:"2006-03-01",params:{Bucket:"beta-likeminds-media"}})}uploadMedia(e){return this.getAWS().upload({Key:`files/collabcard/${e.chatroomId}/conversation/${e.messageId}/${e.file.name}`,Bucket:"beta-likeminds-media",Body:e.file,ACL:"public-read-write",ContentType:e.file.type}).promise()}putMultimedia(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/helper/media/upload`,{method:"POST",data:{conversation_id:e.conversationId,url:e.url,type:e.type,files_count:e.filesCount,index:e.index,height:e.height,width:e.width,meta:e.meta,name:e.name,thumbnail_url:e.thumbnailUrl}})}decodeUrl(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/helper/url?url=${e.url}`)}postPollConversation(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}?chatroom_id=${e.chatroomId}`)}getPollUsers(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}?chatroom_id=${e.chatroomId}`)}addPollOption(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}?chatroom_id=${e.chatroomId}`)}submitPoll(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${d}?chatroom_id=${e.chatroomId}`)}getReportTags(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/report/tag?type=${e.type}`)}pushReport(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/report`,{method:"POST",data:{conversation_id:e.conversationId,tag_id:e.tagId,reason:e.reason,reported_member_id:e.reportedMemberId}})}leaveSecretChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${l}`,{method:"DELETE",data:{chatroom_id:e.chatroomId,member_id:e.memberId}})}profileData(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${$}?community_id=${e.community_id}&member_id=${e.member_id}`)}viewParticipants(e){return this.networkLibrary.makeAuthenticatedRequest(e.page?`${n}${l}?chatroom_id=${e.chatroom_id}&is_secret=${e.is_secret}&page=${e.page}&page_size=${e.page_size}`:`${n}${l}?chatroom_id=${e.chatroom_id}&is_secret=${e.is_secret}`)}conversationsFetch(e){return this.networkLibrary.makeAuthenticatedRequest(e.chatroom_id?`${n}${p}?chatroom_id=${e.chatroom_id}&conversation_id=${e.conversation_id}`:`${n}${p}?conversation_id=${e.conversation_id}`)}fetchChatroomHome(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/home?chatroom_id=${e.chatroom_id}`)}crSeenFn(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/seen`,{method:"PUT",data:{collabcard_id:e.collabcardId,member_id:e.memberId,collabcard_type:e.collabcardType}})}},class extends g{constructor(...e){super(...e),this.networkLibrary=new y}getExploreFeed(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/feed?order_type=${e.orderType}&page=${e.page}`)}},class extends g{constructor(...e){super(...e),this.networkLibrary=new y}initiateUser(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/sdk/initiate`,{method:"POST",data:{is_guest:null==e?void 0:e.isGuest,user_unique_id:null==e?void 0:e.userUniqueId,user_name:null==e?void 0:e.userName}}).then(e=>(this.networkLibrary.setAccessToken(e.data.access_token),this.networkLibrary.setRefreshToken(e.data.access_token),{data:null==e?void 0:e.data,errorMessage:null,success:!0})).catch(e=>{console.log({data:null,errorMessage:e.error_message,success:!1})})}getProfile(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${m}?user_id=${e.userId}`)}getMemberChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/member/chatroom?user_id=${e.userId}&state=${e.state}&page=${e.page}`)}getQuestions(){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/questions`).then(e=>e)}getMemberState(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${$}?member_id=${e.memberId}`).then(e=>e)}editProfile(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${m}`,{method:"PUT",data:{user_name:e.userName,user_unique_id:e.userUniqueId,image_url:e.imageUrl}})}searchMembers(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/member/search?search=${e.search}&search_type=${e.search_type}&page=${e.page}&page_size=${e.page_size}`)}allMembers(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${k}?community_id=${e.community_id}&chatroom_id=${e.chatroom_id}&page=${e.page}`)}getAllMembers(e){return this.networkLibrary.makeAuthenticatedRequest(e.memberState?`${n}${k}?chatroom_id=${e.chatroomId}&member_state=${e.memberState}&page=${e.page}`:`${n}${k}?chatroom_id=${e.chatroomId}&page=${e.page}`)}dmAllMembers(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/member?community_id=${e.community_id}&member_state=${e.member_state}&page=${e.page}`)}},class extends g{constructor(...e){super(...e),this.networkLibrary=new y}fetchDMFeed(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/dm?page=${e.page}`)}checkDMStatus(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}${u}?req_from=${e.requestFrom}`)}checkDMLimit(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/dm/limit?member_id=${e.memberId}`)}createDMChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/dm/create`,{method:"POST",data:{member_id:e.memberId}})}sendDMRequest(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/dm/request`,{method:"POST",data:{chatroom_id:e.chatroomId,chat_request_state:e.chatRequestState,text:e.text}})}blockMember(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/dm/block`,{method:"POST",data:{chatroom_id:e.chatroomId,status:e.status}})}checkDMTab(){return this.networkLibrary.makeAuthenticatedRequest(`${n}/home/dm/meta`)}getDMFeed(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/community/dm/feed?community_id=${e.community_id}`)}canDmFeed(e){return this.networkLibrary.makeAuthenticatedRequest(e.chatroom_id?`${n}${u}?community_id=${e.community_id}&req_from=${e.req_from}&member_id=${e.member_id}&chatroom_id=${e.chatroom_id}`:`${n}${u}?community_id=${e.community_id}&req_from=${e.req_from}&member_id=${e.member_id}`)}},class extends g{constructor(...e){super(...e),this.networkLibrary=new y}searchChatroom(e){return this.networkLibrary.makeAuthenticatedRequest(`${n}/chatroom/search?follow_status=${e.followStatus}&page=${e.page}&page_size=${e.pageSize}&search=${e.search}&search_type=${e.searchType}`)}}].forEach(e=>{Object.getOwnPropertyNames(e.prototype).forEach(t=>{Object.defineProperty(A.prototype,t,Object.getOwnPropertyDescriptor(e.prototype,t)||Object.create(null))})});export{f as default};