UNPKG

softchatjs-core

Version:
2 lines 6.12 kB
import _ from"moment";function A(n,a=0){let t=BigInt(a),e=BigInt(a),g=BigInt("0x87c37b91114253d5"),s=BigInt("0x4cf5ad432745937f"),p=n.length,d=p&15,o=p-d;for(let c=0;c<o;c+=16){let u=BigInt(n.charCodeAt(c))|BigInt(n.charCodeAt(c+1))<<BigInt(8)|BigInt(n.charCodeAt(c+2))<<BigInt(16)|BigInt(n.charCodeAt(c+3))<<BigInt(24)|BigInt(n.charCodeAt(c+4))<<BigInt(32)|BigInt(n.charCodeAt(c+5))<<BigInt(40)|BigInt(n.charCodeAt(c+6))<<BigInt(48)|BigInt(n.charCodeAt(c+7))<<BigInt(56),m=BigInt(n.charCodeAt(c+8))|BigInt(n.charCodeAt(c+9))<<BigInt(8)|BigInt(n.charCodeAt(c+10))<<BigInt(16)|BigInt(n.charCodeAt(c+11))<<BigInt(24)|BigInt(n.charCodeAt(c+12))<<BigInt(32)|BigInt(n.charCodeAt(c+13))<<BigInt(40)|BigInt(n.charCodeAt(c+14))<<BigInt(48)|BigInt(n.charCodeAt(c+15))<<BigInt(56);u=u*g,u=u<<BigInt(31)|u>>BigInt(33),u=u*s,t^=u,t=t<<BigInt(27)|t>>BigInt(37),t=t+e,t=t*BigInt(5)+BigInt("0x52dce729"),m=m*s,m=m<<BigInt(33)|m>>BigInt(31),m=m*g,e^=m,e=e<<BigInt(31)|e>>BigInt(33),e=t+e,e=e*BigInt(5)+BigInt("0x38495ab5")}let r=BigInt(0),i=BigInt(0);switch(d){case 15:i^=BigInt(n.charCodeAt(o+14))<<BigInt(48);case 14:i^=BigInt(n.charCodeAt(o+13))<<BigInt(40);case 13:i^=BigInt(n.charCodeAt(o+12))<<BigInt(32);case 12:i^=BigInt(n.charCodeAt(o+11))<<BigInt(24);case 11:i^=BigInt(n.charCodeAt(o+10))<<BigInt(16);case 10:i^=BigInt(n.charCodeAt(o+9))<<BigInt(8);case 9:i^=BigInt(n.charCodeAt(o+8)),i=i*s,i=i<<BigInt(33)|i>>BigInt(31),i=i*g,e^=i;case 8:r^=BigInt(n.charCodeAt(o+7))<<BigInt(56);case 7:r^=BigInt(n.charCodeAt(o+6))<<BigInt(48);case 6:r^=BigInt(n.charCodeAt(o+5))<<BigInt(40);case 5:r^=BigInt(n.charCodeAt(o+4))<<BigInt(32);case 4:r^=BigInt(n.charCodeAt(o+3))<<BigInt(24);case 3:r^=BigInt(n.charCodeAt(o+2))<<BigInt(16);case 2:r^=BigInt(n.charCodeAt(o+1))<<BigInt(8);case 1:r^=BigInt(n.charCodeAt(o)),r=r*g,r=r<<BigInt(31)|r>>BigInt(33),r=r*s,t^=r}return t^=BigInt(p),e^=BigInt(p),t+=e,e+=t,t^=t>>BigInt(33),t=t*BigInt("0xff51afd7ed558ccd"),t^=t>>BigInt(33),t=t*BigInt("0xc4ceb9fe1a85ec53"),t^=t>>BigInt(33),e^=e>>BigInt(33),e=e*BigInt("0xff51afd7ed558ccd"),e^=e>>BigInt(33),e=e*BigInt("0xc4ceb9fe1a85ec53"),e^=e>>BigInt(33),t+=e,e+=t,(t&BigInt("0xFFFFFFFFFFFFFFFF")).toString(16)}function E(n,a,t){let g=[n,a].sort().join("_");return A(`${t}:${g}`)}var h=()=>{let n="",a="abcdef0123456789";for(let t=0;t<32;t++){let e=Math.floor(Math.random()*a.length),g=a.charAt(e);(t===8||t===12||t===16||t===20)&&(n+="-"),n+=g}return n};var M=()=>({createdAt:new Date,updatedAt:new Date});var C={id:"",username:"",email:"",firstname:"",lastname:"",profileImgUrl:"",phone:"",profileBannerUrl:"",custom:{}},I=class I{constructor(a,t,e){this.connection=a,this.participants=[],this.otherParticipant=null,this.conversationType="private-chat",Array.isArray(t)?(this.conversationType="group-chat",this.participants=t.map(g=>({...C,...g})),this.groupMeta=e):(this.otherParticipant={...C,...t},this.conversationType="private-chat",this.groupMeta=null)}static getInstance(a,t,e){return I.conversation||(I.conversation=new I(a,t,e)),I.conversation}generateConversation(a,t,e,g){let s=M(),p=t.map(i=>i.uid),d={id:h(),uid:this.connection.userMeta.uid,participantId:this.connection.userMeta.uid,participantDetails:{...this.connection.userMeta,...s},...s},o=t.map(i=>({id:h(),uid:i.uid,participantId:i.uid,participantDetails:{...i,...s},...s})),r=[d,...o];return{participants:[this.connection.userMeta.uid,...p],admins:[this.connection.userMeta.uid],conversationId:a,messages:e.message.length>0?[e]:[],conversationType:g,participantList:r,meta:null,groupMeta:g==="group-chat"?this.groupMeta:null,...s}}create(a){try{let e=h(),g=h(),s={messageId:e,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:a||"",messageId:e,messageState:3,conversationId:"",from:this.connection.userMeta.uid,to:"",attachmentType:"none",messageOwner:{...this.connection.userMeta,meta:this.connection.userMeta,...M()},attachedMedia:[],quotedMessageId:"",quotedMessage:null,reactions:[],lastEdited:null,...M()},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=g,s.to=this.participants,s.participantIds=[...p,this.connection.userMeta.uid],this.sendMessage(s,g,this.participants,"group-chat")}else{if(!this.otherParticipant)throw new Error("Method newConversation() on conversation was not called");let p=E(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(e){e instanceof Error&&console.error(e.message)}}sendMessage(a,t,e,g){try{let r={action:"createConversation",message:a,user:this.connection.userMeta};var s;if(this.connection.socket){if(this.connection.socket.send(JSON.stringify(r)),this.connection.conversationListMeta[t]){let i=this.connection.conversationListMeta[t];var d=[...i.conversation.messages];a.message.message.length>0&&d.unshift(a.message);var o={conversation:{...i.conversation,messages:d},lastMessage:a.message,unread:[]};this.connection.conversationListMeta[t]=o,s=o}else{let i=this.generateConversation(t,e,a.message,g);var p=a.message.message.length>0;this.connection.conversationListMeta[t]={conversation:i,lastMessage:p?a.message:null,unread:[]},s={conversation:i,lastMessage:a.message,unread:[]}}return this.connection.emit("conversation_list_meta_changed",{conversationListMeta:this.connection.conversationListMeta}),this.reset(),s}}catch(r){r instanceof Error&&console.error(r.message)}}reset(){I.conversation=null}};I.conversation=null;var l=I;export{l as default}; //# sourceMappingURL=Conversation.mjs.map