diff --git a/dbTools/post.js b/dbTools/post.js index 931f718..f795abf 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -100,7 +100,8 @@ postDB = (DB)=>{ }); } - DB.getPosts = (profileId) => { + DB.getPosts = async (profileId) => { + const profile = await DB.getProfile(profileId); let query = {}; if(profileId) { const id = DB.ObjectID(profileId); @@ -111,12 +112,35 @@ postDB = (DB)=>{ ] }; } - return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{ + 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 = []; + 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 && !profile[p.profileid]){ + continue; + } + if(isPostingToAPrivateGroup && !profile[p.toProfile]){ + continue; + } + filteredPosts.push(p); + } + return filteredPosts; + } + DB.getFeed = async (profileId) => { const profile = await DB.getProfile(profileId); let ids = profile.following.map((id)=>DB.ObjectID(id)); @@ -133,21 +157,7 @@ postDB = (DB)=>{ ] }; return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ - //we need to filter when toProfile is private and not part of the following array - let filteredPosts = []; - for(p in posts){ - if(!DB.isGroupPrivate(p.profileid) && !DB.isGroupPrivate(p.toProfile)){ - filteredPosts.push(p); - } - if(DB.isGroupPrivate(p.profileid) && !profile[p.profileid]){ - continue; - } - if(DB.isGroupPrivate(p.toProfile) && !profile[p.toProfile]){ - continue; - } - filteredPosts.push(p); - } - return posts; + return await filterPrivateGroups(posts, profile); }).catch((err)=>{ console.log(err); return false; @@ -156,12 +166,27 @@ postDB = (DB)=>{ 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)=>{