From ca44e5b4247106478831f18a91a0cdc8d39c80cf Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sat, 19 Feb 2022 18:36:01 -0800 Subject: [PATCH] initerest and random merge posts --- dbTools/profile.js | 34 ++++++++++++++++++++++++---- routes/post.js | 55 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/dbTools/profile.js b/dbTools/profile.js index 0985e76..74c334c 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -51,10 +51,10 @@ userDB = (DB) => { DB.getPopularProfiles = async (limit = 10) => { return DB.profileCols.aggregate([ { - $match: {isGroup: false} + $match: {isGroup: {$ne: true}} }, { - $addFields: { subscribed_count: {$size: { "$ifNull": [ {"$objectToArray" : "$subscribed"}, [] ] } } } + $addFields: { subscribed_count: {$size: { "$ifNull": [ "$following", [] ] } } } }, { $sort: {"subscribed_count":-1} @@ -64,7 +64,7 @@ userDB = (DB) => { } ]).limit(limit).toArray().catch((err) => { console.log(err); - return false; + return []; }); } @@ -84,7 +84,33 @@ userDB = (DB) => { } ]).limit(limit).toArray().catch((err) => { console.log(err); - return false; + return []; + }); + } + + DB.getFriendsFriends = async (profileId) => { + const profile = await DB.getProfile(profileId); + if(!profile) return []; + let ids = profile.following.map((id)=>DB.ObjectID(id)); + let alreadyFollowingMap = {}; + alreadyFollowingMap[profileId] = 1; //skip that profile + profile.following.forEach(id => { + if(!alreadyFollowingMap[id]) alreadyFollowingMap[id] = 1; + }) + return DB.profileCols.find({_id:{$in: ids}}).project({following: 1}).toArray().then(profiles => { + let friendsOfFriendsMap = {}; + profiles.forEach(p => { + p.following.forEach(followingId => { + if(alreadyFollowingMap[followingId]) return 0; + if(!friendsOfFriendsMap[followingId]) friendsOfFriendsMap[followingId] = 0; + friendsOfFriendsMap[followingId] = friendsOfFriendsMap[followingId] + 1; + }); + }); + // sort by most related? + return Object.keys(friendsOfFriendsMap); + }).catch((err) => { + console.log(err); + return []; }); } diff --git a/routes/post.js b/routes/post.js index 4d1b123..42e5b90 100644 --- a/routes/post.js +++ b/routes/post.js @@ -16,24 +16,65 @@ DB.getDB.then((DB) => { return post.profileid === profileid; }; - router.get("/", async (req, res) => { - const profileid = getProfileId(req); - let posts = await DB.getFeed(profileid); - //Add non-organic posts + const generateNonOrganicPosts = async (req, profileid) => { + let posts = []; + // Popular Profiles const popularProfiles = await DB.getPopularProfiles(5); - let content = "Popular users to follow:\n"; + let content = "Active users to follow:\n"; popularProfiles.forEach((p)=>{ content += "@p:" + p._id + "\n"; }); let popularProfilesPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"}); + posts.push(popularProfilesPost); + + // Popular Groups const popularGroups = await DB.getPopularGroups(5); - content = "Popular users to follow:\n"; + content = "Popular groups to follow:\n"; popularGroups.forEach((p)=>{ content += "@p:" + p._id + "\n"; }); let popularGroupsPost = new Post({profileid: 1, content, nonOrganicType: "PopularGroups"}); - posts.push(popularProfilesPost); posts.push(popularGroupsPost); + + const yourFollowsInterests = await DB.getFriendsFriends(profileid); + content = "People your follow has these interests:\n"; + yourFollowsInterests.forEach((p)=>{ + content += "@p:" + p + "\n"; + }); + let yourFollowsInterestsPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"}); + posts.push(yourFollowsInterestsPost); + + return posts; + }; + + const mergePosts = (organic, nonOrganic) => { + if(!organic || organic.length < 5) return nonOrganic; + //organic.push(...nonOrganic); + let mergedPosts = []; + while(organic.length + nonOrganic.length){ + if(organic.length == 0){ + mergedPosts.push(nonOrganic.shift()); + continue; + } + if(nonOrganic.length == 0){ + mergedPosts.push(organic.shift()); + continue; + } + if(Math.random() < 0.2){ + mergedPosts.push(nonOrganic.shift()); + } else { + mergedPosts.push(organic.shift()); + } + } + return mergedPosts; + }; + + router.get("/", async (req, res) => { + const profileid = getProfileId(req); + let organicPosts = await DB.getFeed(profileid); + //Add non-organic posts + const nonOrganicPosts = await generateNonOrganicPosts(req, profileid); + const posts = mergePosts(organicPosts, nonOrganicPosts); return res.json(posts); });