initerest and random merge posts

This commit is contained in:
Adolfo Reyna
2022-02-19 18:36:01 -08:00
parent 1e50bcd5e1
commit ca44e5b424
2 changed files with 78 additions and 11 deletions

View File

@@ -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);
});