UNPKG

@qonsoll/chat-core

Version:
2 lines (1 loc) 13.8 kB
"use strict";var e=require("./Proxy.js"),t=require("firebase"),a=require("./constants.js"),s=require("moment"),o=require("lodash"),n=require("../../constants/dist/index.cjs.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=i(t),r=i(s),d=i(o),c=function(t){var a=t.collection,s=t.id,o=t.data,n=t.ref;return e.__awaiter(void 0,void 0,void 0,(function(){var t;return e.__generator(this,(function(i){switch(i.label){case 0:return[4,(n||l.default.firestore()).collection(a).doc(s).update(e.__assign(e.__assign({},o),{_updatedBy:null===(t=e.Proxy.user)||void 0===t?void 0:t._id,_updatedAt:l.default.firestore.FieldValue.serverTimestamp()}))];case 1:return i.sent(),[2]}}))}))},u=a.CHAT_COLLECTIONS.MESSAGES,v=a.CHAT_COLLECTIONS.MEMBERS,T=a.CHAT_COLLECTIONS.MESSAGES_VIEWS,S=function(t){var a=t.collection,s=t.id,o=t.data,n=t.ref;return e.__awaiter(void 0,void 0,void 0,(function(){var t,i,r,d;return e.__generator(this,(function(c){switch(c.label){case 0:return t=n||l.default.firestore(),i=s||t.collection(a).doc().id,[4,t.collection(a).doc(i).set(e.__assign({_id:i,_updatedAt:l.default.firestore.FieldValue.serverTimestamp(),_createdAt:l.default.firestore.FieldValue.serverTimestamp(),_updatedBy:null===(r=e.Proxy.user)||void 0===r?void 0:r._id,_createdBy:null===(d=e.Proxy.user)||void 0===d?void 0:d._id,_isUpdated:!1},o))];case 1:return c.sent(),[2,i]}}))}))},_=function(t){var a=t.collection,s=t._id;return e.__awaiter(void 0,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,l.default.database().ref("".concat(a,"/").concat(s)).once("value")];case 1:return[2,e.sent().val()]}}))}))},p=a.CHAT_COLLECTIONS.GROUPS,E=a.CHAT_COLLECTIONS.MEMBERS,m=a.CHAT_COLLECTIONS.MEMBERS_STATUSES,g=a.CHAT_COLLECTIONS.SCHEDULED_MESSAGES,f=a.CHAT_COLLECTIONS.RTDB_MESSENGER_ROOT,I=a.CHAT_COLLECTIONS.GROUPS,h=a.CHAT_COLLECTIONS.CHATS,C=a.CHAT_COLLECTIONS.MESSAGES_VIEWS,A=a.CHAT_COLLECTIONS.CHATS,M=a.CHAT_COLLECTIONS.MEMBERS,O=a.CHAT_COLLECTIONS.MESSAGES,N=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var s,o,n,i,r,d,u,v,T,_,p,E,m,g,f;return e.__generator(this,(function(e){return s=t.oppositeUserId,o=t.messageText,n=t.userData,i=t.userId,r=t.chatId,d=t.attachedFiles,u=t.collectionName,v=void 0===u?A:u,T=t.messageType,_=void 0===T?a.MESSAGE_TYPES.TEXT:T,p=t.groupMemberIds,E=t.lastMessageSenderDisplayName,m=t.qVideoId,g="".concat(v,"/").concat(r,"/").concat(O),function(e){var t=e.messageId,a=e.messageText,s=e.userId,o=e.messageType,n=e.collectionName,i=e.chatId,r=e.lastMessageSenderDisplayName,d={lastMessageId:t,lastMessageText:a,lastMessageDateAndTime:l.default.firestore.Timestamp.now(),lastMessageSenderId:s,lastMessageType:o};n===I&&(d.lastMessageSenderDisplayName=r),c({collection:n,id:i,data:d})}({messageId:f=l.default.firestore().collection(g).doc().id,messageText:o,userId:i,messageType:_,collectionName:v,chatId:r,lastMessageSenderDisplayName:E}),c({collection:"".concat(v,"/").concat(r,"/").concat(M),id:s,data:{lastMessageStatus:a.MESSAGE_STATUS.SEND}}),function(e){var t=e.messageId,s=e.messageType,o=e.messageText,n=e.userData,i=e.attachedFiles,l=e.qVideoId,r=e.messagesCollection,d={_id:t,text:o,status:a.MESSAGE_STATUS.SEND,messageType:s,attachedFiles:i||null,qVideoId:l||null};n&&(d.senderAvatarUrl=null==n?void 0:n.avatarUrl,d.senderName="".concat(null==n?void 0:n.firstName," ").concat(null==n?void 0:n.lastName)),S({collection:r,id:t,data:d})}({messageId:f,messageText:o,messageType:_,attachedFiles:d,qVideoId:m,userData:n,messagesCollection:g}),function(e){var t,a,s=e.collectionName,o=e.userId,n=e.messageId,i=e.chatId,l=e.oppositeUserId,r=e.groupMemberIds;s===h?((t={})[o]=!0,t[l]=!1,a=t):(a=(null==r?void 0:r.length)&&Object.fromEntries(r.map((function(e){return[e,!1]}))))[o]=!0,S({collection:"".concat(s,"/").concat(i,"/").concat(C),id:n,data:{views:a}})}({messageId:f,chatId:r,oppositeUserId:s,groupMemberIds:p,collectionName:v,userId:i}),[2,f]}))}))},U=function(t){var a;return null===(a=e.Proxy.createNotification)||void 0===a?void 0:a.call(e.Proxy,t)},L={videos:a.MESSAGE_TYPES.MEDIA,photos:a.MESSAGE_TYPES.MEDIA,files:a.MESSAGE_TYPES.FILE,media:a.MESSAGE_TYPES.MEDIA},D=function(t){var s=t.user,o=t.oppositeUserData,n=t.data,i=t.create,l=function(e){var t,s,o=d.default.groupBy(e,(function(e){var t=e.type;return"video/"===t?a.ATTACH_TYPES.VIDEO.toLowerCase():t.toLowerCase()})),n=[];return(null===(t=null==o?void 0:o.video)||void 0===t?void 0:t.length)&&(null===(s=null==o?void 0:o.photo)||void 0===s?void 0:s.length)&&n.push.apply(n,null==o?void 0:o.video.concat(null==o?void 0:o.photo)),{videos:(null==o?void 0:o.video)&&!(null==n?void 0:n.length)?null==o?void 0:o.video:null,photos:(null==o?void 0:o.photo)&&!(null==n?void 0:n.length)?null==o?void 0:o.photo:null,media:(null==n?void 0:n.length)?n:null,files:(null==o?void 0:o.file)||null}}(null==n?void 0:n.attachedFiles),r=Object.entries(l).filter((function(e){e[0];var t=e[1];return null==t?void 0:t.length})).map((function(e){var t=e[0],a=e[1];return{type:L[t],attachments:a}}));(null==r?void 0:r.length)||(r=[{attachments:null,type:n.messageType}]),r.forEach((function(t,a){var l=t.attachments,d=t.type,c=(null==r?void 0:r.length)-1===a,u=n.messageText,v=e.__rest(n,["messageText"]),T=c?n:e.__assign(e.__assign({},v),{messageText:null});T.attachedFiles=l,T.messageType=d,i?null==i||i(T):N(T).then((function(e){return U({oppositeUserData:o,user:s,messageText:c&&u,messageId:e})}))}))},y=a.CHAT_COLLECTIONS.GROUPS,H=function(e){var t,s=e.oppositeUserData,o=e.qVideoId,n=e.user,i=e.messageText,l=e.chatId,r=e.attachedFiles,c=e.collectionName,u=e.messageType,v=e.groupMemberIds,T={oppositeUserId:null==s?void 0:s._id,messageText:o?null:(null==i?void 0:i.trim())||null,userId:null==n?void 0:n._id,chatId:l,collectionName:c,messageType:a.MESSAGE_TYPES.TEXT};if((null==r?void 0:r.length)&&!o&&(T.attachedFiles=r,T.messageType=a.MESSAGE_TYPES.MEDIA),o&&(T.qVideoId=o,T.messageType=a.MESSAGE_TYPES.LIVE_VIDEO),u&&(T.messageType=u),c===y&&(T.lastMessageSenderDisplayName=null==n?void 0:n.firstName,T.groupMemberIds=v,T.userData=n),r&&(null==r?void 0:r.length)>10){var S=d.default.chunk(r,10);S.forEach((function(e,t){T.messageText=t===(null==S?void 0:S.length)-1?i:null,T.attachedFiles=e,D({user:n,oppositeUserData:s,data:T})}))}else(null===(t=null==T?void 0:T.attachedFiles)||void 0===t?void 0:t.length)?D({user:n,oppositeUserData:s,data:T}):N(T).then((function(e){return U({oppositeUserData:s,user:n,messageText:null==T?void 0:T.messageText,messageId:e})}))},x=a.CHAT_COLLECTIONS.USERS_STATUS_REFERENCES,b=function(t,a,s,o,n){return e.__awaiter(void 0,void 0,void 0,(function(){var a,s,i,r,d;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(a=l.default.database().ref("".concat(x,"/").concat(t))).once("value")];case 1:return s=e.sent(),i=s.exists(),r={id:o,type:n},i?((d=s.val()).length?d.push(r):d=[r],[4,a.set(d)]):[3,3];case 2:return e.sent(),[3,5];case 3:return[4,a.set([r])];case 4:e.sent(),e.label=5;case 5:return[2]}}))}))},w=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var s,o,i;return e.__generator(this,(function(e){switch(e.label){case 0:return s=t.user,o=t.oppositeUserData,i=t.chatId,[4,b(null==s?void 0:s._id,o._id,n.DEFAULT_FOLDER_ID,i,a.CHAT_TYPES.CHAT)];case 1:return e.sent(),[4,b(null==o?void 0:o._id,s._id,n.DEFAULT_FOLDER_ID,i,a.CHAT_TYPES.CHAT)];case 2:return e.sent(),[2]}}))}))},F=a.CHAT_COLLECTIONS.CHATS,P=a.CHAT_COLLECTIONS.PERMISSIONS,G=a.CHAT_COLLECTIONS.CHATS,R=a.CHAT_COLLECTIONS.CHAT_USERS,V=a.CHAT_COLLECTIONS.FOLDERS,q=a.CHAT_COLLECTIONS.USER_STATUSES,Y=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var a,s,o,n;return e.__generator(this,(function(e){switch(e.label){case 0:return a=t.user,s=t.oppositeUserData,[4,_({collection:q,_id:a._id})];case 1:return o=e.sent(),[4,_({collection:q,_id:s._id})];case 2:return n=e.sent(),[2,{currentUserStatuses:o,oppositeUserStatuses:n}]}}))}))},B=a.CHAT_COLLECTIONS.CHATS,j=a.CHAT_COLLECTIONS.MEMBERS,W=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var s,o,n,i,d,c,u,v;return e.__generator(this,(function(e){switch(e.label){case 0:return s=t.user,o=t.oppositeUserData,n=t.chatId,i="".concat(B,"/").concat(n,"/").concat(j),[4,Y({user:s,oppositeUserData:o})];case 1:return d=e.sent(),c=d.oppositeUserStatuses,u=d.currentUserStatuses,v=function(e){return e&&l.default.firestore.Timestamp.fromDate(r.default(e).toDate())||null},S({collection:i,id:s._id,data:{firstName:null==s?void 0:s.firstName,lastName:null==s?void 0:s.lastName,avatarUrl:null==s?void 0:s.avatarUrl,isOnline:(null==u?void 0:u.isOnline)||null,isTyping:!1,lastActiveTime:v(null==u?void 0:u.lastActiveTime),lastMessageStatus:a.MESSAGE_STATUS.SEND}}),S({collection:i,id:o._id,data:{firstName:null==o?void 0:o.firstName,lastName:null==o?void 0:o.lastName,avatarUrl:null==o?void 0:o.avatarUrl,isOnline:(null==c?void 0:c.isOnline)||null,isTyping:!1,lastActiveTime:v(null==c?void 0:c.lastActiveTime),lastMessageStatus:null}}),[2]}}))}))},k=a.CHAT_COLLECTIONS.CHATS;exports.changeMessageStatus=function(t){var s=t.userId,o=t.message,n=t.chatData;return e.__awaiter(void 0,void 0,void 0,(function(){var t,i;return e.__generator(this,(function(r){return(null==o?void 0:o._createdBy)!==s&&(t=n.chatId,i=n.collectionName,l.default.firestore().collection("".concat(i,"/").concat(t,"/").concat(T)).doc(o._id).get().then((function(l){var r,d,S,_;try{(null==o?void 0:o.status)===a.MESSAGE_STATUS.SEEN&&(null===(S=null===(d=l.data())||void 0===d?void 0:d.views)||void 0===S?void 0:S[s])||(c({collection:"".concat(i,"/").concat(t,"/").concat(T),id:o._id,data:{views:e.__assign(e.__assign({},null===(_=l.data())||void 0===_?void 0:_.views),(r={},r[s]=!0,r))}}),c({collection:"".concat(i,"/").concat(t,"/").concat(u),id:o._id,data:{status:a.MESSAGE_STATUS.SEEN}}),n.lastMessageId===(null==o?void 0:o._id)&&c({collection:"".concat(i,"/").concat(t,"/").concat(v),id:null==n?void 0:n.lastMessageSenderId,data:{lastMessageStatus:a.MESSAGE_STATUS.SEEN}}),function(t){var a;null===(a=e.Proxy.updateMessageSeenStatus)||void 0===a||a.call(e.Proxy,t)}({messageId:o._id}))}catch(e){console.error("message ".concat(o._id," doesn't have views document"))}}))),[2]}))}))},exports.changeOnlineStatus=function(t){var s,o=t.isOnline;return l.default.database().ref("".concat(a.CHAT_COLLECTIONS.USER_STATUSES,"/").concat(null===(s=e.Proxy.user)||void 0===s?void 0:s._id)).update({isOnline:o,lastActiveTime:l.default.database.ServerValue.TIMESTAMP})},exports.createChat=function(t,s){return e.__awaiter(void 0,void 0,void 0,(function(){var o,i,r,d,c,u,v,T,_,p,E;return e.__generator(this,(function(e){switch(e.label){case 0:return o=t.oppositeUserData,i=t.user,r=t.qVideoId,d=t.messageType,c=t.messageText,u=t.attachedFiles,v=t.groupMemberIds,T=t.collectionName,_=void 0===T?k:T,p=l.default.firestore().collection(k).doc().id,E={lastMessageDateAndTime:l.default.firestore.FieldValue.serverTimestamp(),lastMessageText:(null==c?void 0:c.trim())||null},[4,S({collection:k,id:p,data:E})];case 1:return e.sent(),[4,W({user:i,oppositeUserData:o,chatId:p})];case 2:return e.sent(),function(e){var t=e.user,a=e.oppositeUserData,s=e.chatId,o="".concat(F,"/").concat(s,"/").concat(P),n={allowWrite:!0};S({collection:o,id:t._id,data:n}),S({collection:o,id:a._id,data:n})}({user:i,oppositeUserData:o,chatId:p}),function(e){var t=e.user,s=e.oppositeUserData,o=e.chatId,i=function(e){return"".concat(R,"/").concat(e,"/").concat(V,"/").concat(n.DEFAULT_FOLDER_ID,"/").concat(G)},l=function(e){return"".concat(null==e?void 0:e.firstName," ").concat(null==e?void 0:e.lastName)};S({collection:i(null==t?void 0:t._id),id:o,data:{displayName:l(s),chatPhotoUrl:null==s?void 0:s.avatarUrl,oppositeUserId:null==s?void 0:s._id,type:a.CHAT_TYPES.CHAT,order:null}}),S({collection:i(null==s?void 0:s._id),id:o,data:{displayName:l(t),chatPhotoUrl:null==t?void 0:t.avatarUrl,oppositeUserId:null==t?void 0:t._id,type:a.CHAT_TYPES.CHAT,order:null}})}({user:i,oppositeUserData:o,chatId:p}),s?null==s||s({oppositeUserData:o,user:i,qVideoId:r,messageType:d,messageText:c,attachedFiles:u,groupMemberIds:v,collectionName:_,chatId:p}):H({oppositeUserData:o,user:i,qVideoId:r,messageType:d,messageText:c,attachedFiles:u,groupMemberIds:v,collectionName:_,chatId:p}),[4,w({user:i,oppositeUserData:o,chatId:p})];case 3:return e.sent(),[2]}}))}))},exports.createMessage=N,exports.saveScheduledMessage=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var a,s,o,n,i,d,c,u,v,T,S,_,p,E,m,I,h,C,A,M,O,N;return e.__generator(this,(function(e){switch(e.label){case 0:return a=t.chatId,s=t.collectionName,o=t.scheduleDate,n=t.messageType,i=t.messageText,d=t.attachedFiles,c=void 0===d?null:d,u=t.userId,v=t.userData,T=t.oppositeUserId,S=void 0===T?null:T,_=t.groupMemberIds,p=void 0===_?null:_,E=t.lastMessageSenderDisplayName,m=t.qVideoId,I=new Date,h=Math.trunc(I.getTimezoneOffset()/60),C=r.default(o).add(h,"h"),A=r.default(C).format("YYYY-MM-DD"),M=r.default(C).format("HH:mm"),O="".concat(f,"/").concat(g,"/").concat(A,"/").concat(M),O="".concat(O,"/").concat(l.default.database().ref(O).push().key),N={chatId:a,chatType:s,userId:u,text:i||"",messageType:n,attachedFiles:c,oppositeUserId:S},m&&(N.qVideoId=m),v&&(N.senderAvatarUrl=(null==v?void 0:v.avatarUrl)||null,N.senderName=E,N.groupMemberIds=p),[4,l.default.database().ref(O).set(N)];case 1:return e.sent(),[2]}}))}))},exports.sendMessage=H,exports.sendMessageWithAttachments=D,exports.updateFirestoreDocument=c,exports.updateTypingStatus=function(e){var t=e.user,a=e.collectionName,s=e.chatId,o=e.isTyping;a===p&&c({collection:"".concat(a,"/").concat(s,"/").concat(m),id:null==t?void 0:t._id,data:{isTyping:o}}),c({collection:"".concat(a,"/").concat(s,"/").concat(E),id:null==t?void 0:t._id,data:{isTyping:o}})};