const DBName = "EMI_SOCIAL"; const Post = require("./../def/post.js") postDB = (DB)=>{ DB.postCols = DB.db.db(DBName).collection("posts"); DB.newPost = (postObj) => { return DB.postCols.insertOne(postObj.toObj()).catch((err)=>{ console.log(err); return false; }); } DB.removePost = (postid) => { const _id = DB.ObjectID(postid); return DB.postCols.deleteOne({_id}).catch((err)=>{ console.log(err); return false; }); } DB.updatePost = (postid, newContent, oldContent) => { const id = DB.ObjectID(postid); let update = { $set:{ content: newContent, // lastUpdated: new Date() // add back when finish updating videos. }, $push: { contentHistory: oldContent } } return DB.postCols.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.newReaction = (postid, profileid, reaction) => { const id = DB.ObjectID(postid); let update = { $set:{ ["reactions." + profileid]: reaction, lastUpdated: new Date() } } return DB.postCols.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.removeReaction = (postid, profileid) => { const id = DB.ObjectID(postid); let update = { $unset:{ ["reactions." + profileid]: "" }, $set: { lastUpdated: new Date() } } return DB.postCols.updateOne({_id: id}, update).catch((err)=>{ console.log(err); return false; }); } DB.bookmarkPost = async (postid, profileId)=>{ const _id = DB.ObjectID(postid); let update = { $addToSet:{ bookmarks: DB.ObjectID(profileId) } } return DB.postCols.updateOne({_id}, update).catch((err)=>{ console.log(err); return false; }); } DB.unbookmarkPost = async (postid, profileId)=>{ const _id = DB.ObjectID(postid); let update = { $pull:{ bookmarks: DB.ObjectID(profileId) } } return DB.postCols.updateOne({_id}, update).catch((err)=>{ console.log(err); return false; }); } DB.newComment = (postid, comment) => { const id = DB.ObjectID(postid); return DB.postCols.updateOne({_id: id}, { $push: { comments: comment }, $set: { lastUpdated: new Date() } }).catch((err)=>{ console.log(err); return false; }); } DB.newCommentReaction = (postid, commentDate, profileid, reaction) => { const id = DB.ObjectID(postid); let update = { $set:{ ["comments.$.reactions." + profileid]: reaction, "comments.$.lastUpdated": new Date(), lastUpdated: new Date() } } return DB.postCols.updateOne({ _id: id, "comments.createdAt": commentDate }, update).catch((err)=>{ console.log(err); return false; }); } DB.removeCommentReaction = (postid, commentDate, profileid) => { const id = DB.ObjectID(postid); let update = { $unset:{ ["comments.$.reactions." + profileid]: '', "comments.$.lastUpdated": new Date(), }, $set: { lastUpdated: new Date() } } return DB.postCols.updateOne({ _id: id, "comments.createdAt": commentDate }, update).catch((err)=>{ console.log(err); return false; }); } DB.getPosts = async (profileId, viewerProfileId) => { const profile = await DB.getProfile(viewerProfileId); let query = {}; if(profileId && DB.ObjectID.isValid(profileId)) { const id = DB.ObjectID(profileId); query = { $or: [ {profileid: id}, {toProfile: id} ] }; } else { if(profileId) return []; } return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().then(async (posts)=>{ return await filterPrivateGroups(posts, profile); }).catch((err)=>{ console.log(err); return false; }); } filterPrivateGroups = async (posts, profile) =>{ let filteredPosts = []; let following = {}; profile.following.forEach(element => { following[element] = 1; }); for(i in posts){ let p = posts[i]; let isPostingAPrivateGroup = await DB.isGroupPrivate(p.profileid); let isPostingToAPrivateGroup = p.toProfile ? await DB.isGroupPrivate(p.toProfile) : false; if(!isPostingAPrivateGroup && !isPostingToAPrivateGroup){ filteredPosts.push(p); continue; } if(isPostingAPrivateGroup && !following[p.profileid]){ continue; } if(isPostingToAPrivateGroup && !following[p.toProfile]){ continue; } filteredPosts.push(p); } return filteredPosts; } DB.getFeed = async (profileId) => { const profile = await DB.getProfile(profileId); if(!profile) return []; let ids = profile.following.map((id)=>DB.ObjectID(id)); ids.push(DB.ObjectID(profileId)) query = { $or: [ {profileid: { $in: ids }}, {toProfile: { $in: ids }} ], nonOrganicType: null // Exlcude news }; return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ return await filterPrivateGroups(posts, profile); }).catch((err)=>{ console.log(err); return false; }); } DB.getNews = async () => { let query = { nonOrganicType: 'News' }; return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().catch((err)=>{ console.log(err); return []; }); } DB.getPostsOfUser = (userId) => { let userid = DB.ObjectID(userId); console.log("getPostsOfUser") return DB.postCols.find({userid}).sort({_id: -1}).limit(20).toArray().catch((err)=>{ console.log(err); return false; }); } DB.getImagesOfUser = (userId) => { let userid = DB.ObjectID(userId); let query = { userid, content: { $regex: "@image" } } return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{ console.log(err); return false; }); } DB.getPost = (postId) => { let _id = DB.ObjectID(postId); return DB.postCols.findOne({_id}).catch((err)=>{ console.log(err); return false; }); } } module.exports = postDB;