softchatjs-core
Version:
Javascript client for JS based frameworks
2 lines • 6.72 kB
JavaScript
"use strict";var T=Object.create;var M=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty;var v=(e,i)=>{for(var t in i)M(e,t,{get:i[t],enumerable:!0})},C=(e,i,t,n)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of y(i))!_.call(e,r)&&r!==t&&M(e,r,{get:()=>i[r],enumerable:!(n=f(i,r))||n.enumerable});return e};var N=(e,i,t)=>(t=e!=null?T(S(e)):{},C(i||!e||!e.__esModule?M(t,"default",{value:e,enumerable:!0}):t,e)),x=e=>C(M({},"__esModule",{value:!0}),e);var U={};v(U,{default:()=>E});module.exports=x(U);var O=N(require("moment"));function R(e,i=0){let t=BigInt(i),n=BigInt(i),r=BigInt("0x87c37b91114253d5"),s=BigInt("0x4cf5ad432745937f"),p=e.length,d=p&15,g=p-d;for(let c=0;c<g;c+=16){let u=BigInt(e.charCodeAt(c))|BigInt(e.charCodeAt(c+1))<<BigInt(8)|BigInt(e.charCodeAt(c+2))<<BigInt(16)|BigInt(e.charCodeAt(c+3))<<BigInt(24)|BigInt(e.charCodeAt(c+4))<<BigInt(32)|BigInt(e.charCodeAt(c+5))<<BigInt(40)|BigInt(e.charCodeAt(c+6))<<BigInt(48)|BigInt(e.charCodeAt(c+7))<<BigInt(56),m=BigInt(e.charCodeAt(c+8))|BigInt(e.charCodeAt(c+9))<<BigInt(8)|BigInt(e.charCodeAt(c+10))<<BigInt(16)|BigInt(e.charCodeAt(c+11))<<BigInt(24)|BigInt(e.charCodeAt(c+12))<<BigInt(32)|BigInt(e.charCodeAt(c+13))<<BigInt(40)|BigInt(e.charCodeAt(c+14))<<BigInt(48)|BigInt(e.charCodeAt(c+15))<<BigInt(56);u=u*r,u=u<<BigInt(31)|u>>BigInt(33),u=u*s,t^=u,t=t<<BigInt(27)|t>>BigInt(37),t=t+n,t=t*BigInt(5)+BigInt("0x52dce729"),m=m*s,m=m<<BigInt(33)|m>>BigInt(31),m=m*r,n^=m,n=n<<BigInt(31)|n>>BigInt(33),n=t+n,n=n*BigInt(5)+BigInt("0x38495ab5")}let o=BigInt(0),a=BigInt(0);switch(d){case 15:a^=BigInt(e.charCodeAt(g+14))<<BigInt(48);case 14:a^=BigInt(e.charCodeAt(g+13))<<BigInt(40);case 13:a^=BigInt(e.charCodeAt(g+12))<<BigInt(32);case 12:a^=BigInt(e.charCodeAt(g+11))<<BigInt(24);case 11:a^=BigInt(e.charCodeAt(g+10))<<BigInt(16);case 10:a^=BigInt(e.charCodeAt(g+9))<<BigInt(8);case 9:a^=BigInt(e.charCodeAt(g+8)),a=a*s,a=a<<BigInt(33)|a>>BigInt(31),a=a*r,n^=a;case 8:o^=BigInt(e.charCodeAt(g+7))<<BigInt(56);case 7:o^=BigInt(e.charCodeAt(g+6))<<BigInt(48);case 6:o^=BigInt(e.charCodeAt(g+5))<<BigInt(40);case 5:o^=BigInt(e.charCodeAt(g+4))<<BigInt(32);case 4:o^=BigInt(e.charCodeAt(g+3))<<BigInt(24);case 3:o^=BigInt(e.charCodeAt(g+2))<<BigInt(16);case 2:o^=BigInt(e.charCodeAt(g+1))<<BigInt(8);case 1:o^=BigInt(e.charCodeAt(g)),o=o*r,o=o<<BigInt(31)|o>>BigInt(33),o=o*s,t^=o}return t^=BigInt(p),n^=BigInt(p),t+=n,n+=t,t^=t>>BigInt(33),t=t*BigInt("0xff51afd7ed558ccd"),t^=t>>BigInt(33),t=t*BigInt("0xc4ceb9fe1a85ec53"),t^=t>>BigInt(33),n^=n>>BigInt(33),n=n*BigInt("0xff51afd7ed558ccd"),n^=n>>BigInt(33),n=n*BigInt("0xc4ceb9fe1a85ec53"),n^=n>>BigInt(33),t+=n,n+=t,(t&BigInt("0xFFFFFFFFFFFFFFFF")).toString(16)}function B(e,i,t){let r=[e,i].sort().join("_");return R(`${t}:${r}`)}var h=()=>{let e="",i="abcdef0123456789";for(let t=0;t<32;t++){let n=Math.floor(Math.random()*i.length),r=i.charAt(n);(t===8||t===12||t===16||t===20)&&(e+="-"),e+=r}return e};var l=()=>({createdAt:new Date,updatedAt:new Date});var A={id:"",username:"",email:"",firstname:"",lastname:"",profileImgUrl:"",phone:"",profileBannerUrl:"",custom:{}},I=class I{constructor(i,t,n){this.connection=i,this.participants=[],this.otherParticipant=null,this.conversationType="private-chat",Array.isArray(t)?(this.conversationType="group-chat",this.participants=t.map(r=>({...A,...r})),this.groupMeta=n):(this.otherParticipant={...A,...t},this.conversationType="private-chat",this.groupMeta=null)}static getInstance(i,t,n){return I.conversation||(I.conversation=new I(i,t,n)),I.conversation}generateConversation(i,t,n,r){let s=l(),p=t.map(a=>a.uid),d={id:h(),uid:this.connection.userMeta.uid,participantId:this.connection.userMeta.uid,participantDetails:{...this.connection.userMeta,...s},...s},g=t.map(a=>({id:h(),uid:a.uid,participantId:a.uid,participantDetails:{...a,...s},...s})),o=[d,...g];return{participants:[this.connection.userMeta.uid,...p],admins:[this.connection.userMeta.uid],conversationId:i,messages:n.message.length>0?[n]:[],conversationType:r,participantList:o,meta:null,groupMeta:r==="group-chat"?this.groupMeta:null,...s}}create(i){try{let n=h(),r=h(),s={messageId:n,from:this.connection.userMeta.uid,to:[{}],conversationType:this.conversationType,groupMeta:this.groupMeta||{groupName:"My group",groupIcon:"https://picsum.photos/200/300",groupBanner:"https://picsum.photos/200/300"},senderMeta:this.connection.userMeta,participantIds:[],message:{message:i||"",messageId:n,messageState:3,conversationId:"",from:this.connection.userMeta.uid,to:"",attachmentType:"none",messageOwner:{...this.connection.userMeta,meta:this.connection.userMeta,...l()},attachedMedia:[],quotedMessageId:"",quotedMessage:null,reactions:[],lastEdited:null,...l()},token:this.connection.wsAccessConfig.token};if(this.conversationType==="group-chat"){if(this.participants.length===0)throw new Error("Method newConversation() on conversation was not called");let p=this.participants.map(d=>d.uid);s.message.conversationId=r,s.to=this.participants,s.participantIds=[...p,this.connection.userMeta.uid],this.sendMessage(s,r,this.participants,"group-chat")}else{if(!this.otherParticipant)throw new Error("Method newConversation() on conversation was not called");let p=B(this.connection.userMeta.uid,this.otherParticipant.uid,this.connection.projectConfig.projectId);s.message.conversationId=p,s.message.to=this.otherParticipant.uid,s.to=[this.otherParticipant],s.participantIds=[this.otherParticipant.uid,this.connection.userMeta.uid];var t=this.sendMessage(s,p,[this.otherParticipant],"private-chat");return t}}catch(n){n instanceof Error&&console.error(n.message)}}sendMessage(i,t,n,r){try{let o={action:"createConversation",message:i,user:this.connection.userMeta};var s;if(this.connection.socket){if(this.connection.socket.send(JSON.stringify(o)),this.connection.conversationListMeta[t]){let a=this.connection.conversationListMeta[t];var d=[...a.conversation.messages];i.message.message.length>0&&d.unshift(i.message);var g={conversation:{...a.conversation,messages:d},lastMessage:i.message,unread:[]};this.connection.conversationListMeta[t]=g,s=g}else{let a=this.generateConversation(t,n,i.message,r);var p=i.message.message.length>0;this.connection.conversationListMeta[t]={conversation:a,lastMessage:p?i.message:null,unread:[]},s={conversation:a,lastMessage:i.message,unread:[]}}return this.connection.emit("conversation_list_meta_changed",{conversationListMeta:this.connection.conversationListMeta}),this.reset(),s}}catch(o){o instanceof Error&&console.error(o.message)}}reset(){I.conversation=null}};I.conversation=null;var E=I;
//# sourceMappingURL=Conversation.js.map