From 309ae1b69b3284e7ecb4a3616648fc0b948cd497 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Wed, 5 Feb 2025 20:46:24 -0500 Subject: [PATCH] Add promotional endpoint (extended) for new users --- dbTools/post.js | 39 +++++++++++++++++++++++++++++---------- routes/post.js | 11 ++++++++++- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index a17a389..9c47a2d 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -176,29 +176,33 @@ postDB = (DB)=>{ }); } + // Filter out posts that are posted by private groups that the viewer is not part of. filterPrivateGroups = async (posts, profile) =>{ - let filteredPosts = []; + let validatedPosts = []; 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); + let isPostedByAPrivateGroup = await DB.isGroupPrivate(p.profileid); + let isPostedToAPrivateGroup = p.toProfile ? await DB.isGroupPrivate(p.toProfile) : false; + // Not private posts + if(!isPostedByAPrivateGroup && !isPostedToAPrivateGroup){ + validatedPosts.push(p); continue; } - if(isPostingAPrivateGroup && !following[p.profileid]){ + // skipps private post if viewer is not part of the group + if(isPostedByAPrivateGroup && !following[p.profileid]){ continue; } - if(isPostingToAPrivateGroup && !following[p.toProfile]){ + if(isPostedToAPrivateGroup && !following[p.toProfile]){ continue; } - filteredPosts.push(p); + // Private but the user is part of the group + validatedPosts.push(p); } - return filteredPosts; + return validatedPosts; } DB.getFeed = async (profileId) => { @@ -217,7 +221,22 @@ postDB = (DB)=>{ ], nonOrganicType: null // Exlcude news }; - return DB.postCols.find(query).sort({lastUpdated: -1}).limit(20).toArray().then(async (posts)=>{ + return DB.postCols.find(query).sort({lastUpdated: -1}).limit(30).toArray().then(async (posts)=>{ + return await filterPrivateGroups(posts, profile); + }).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.getPromotionalPosts = async (profileId) => { + if(!DB.ObjectID.isValid(profileId)) return []; + const profile = await DB.getProfile(profileId); + if(!profile) return []; + query = { + nonOrganicType: null // Exlcude news + }; + return DB.postCols.find(query).sort({lastUpdated: -1}).limit(50).toArray().then(async (posts)=>{ return await filterPrivateGroups(posts, profile); }).catch((err)=>{ console.log(err); diff --git a/routes/post.js b/routes/post.js index bbc8e17..800a987 100644 --- a/routes/post.js +++ b/routes/post.js @@ -82,7 +82,16 @@ DB.getDB.then((DB) => { let organicPosts = await DB.getFeed(profileid); //Add non-organic posts const nonOrganicPosts = await generateNonOrganicPosts(req, profileid); - const posts = mergePosts(organicPosts, nonOrganicPosts); + const posts = mergePosts(promotionalPosts, nonOrganicPosts); + return res.json(posts); + }); + + router.get("/extended", async (req, res) => { + const profileid = getProfileId(req); + //Add non-organic posts + const nonOrganicPosts = await generateNonOrganicPosts(req, profileid); + let promotionalPosts = await DB.getPromotionalPosts(profileid); + const posts = mergePosts(promotionalPosts, nonOrganicPosts); return res.json(posts); });