initerest and random merge posts
This commit is contained in:
@@ -51,10 +51,10 @@ userDB = (DB) => {
|
|||||||
DB.getPopularProfiles = async (limit = 10) => {
|
DB.getPopularProfiles = async (limit = 10) => {
|
||||||
return DB.profileCols.aggregate([
|
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}
|
$sort: {"subscribed_count":-1}
|
||||||
@@ -64,7 +64,7 @@ userDB = (DB) => {
|
|||||||
}
|
}
|
||||||
]).limit(limit).toArray().catch((err) => {
|
]).limit(limit).toArray().catch((err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return false;
|
return [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,33 @@ userDB = (DB) => {
|
|||||||
}
|
}
|
||||||
]).limit(limit).toArray().catch((err) => {
|
]).limit(limit).toArray().catch((err) => {
|
||||||
console.log(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 [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,24 +16,65 @@ DB.getDB.then((DB) => {
|
|||||||
return post.profileid === profileid;
|
return post.profileid === profileid;
|
||||||
};
|
};
|
||||||
|
|
||||||
router.get("/", async (req, res) => {
|
const generateNonOrganicPosts = async (req, profileid) => {
|
||||||
const profileid = getProfileId(req);
|
let posts = [];
|
||||||
let posts = await DB.getFeed(profileid);
|
// Popular Profiles
|
||||||
//Add non-organic posts
|
|
||||||
const popularProfiles = await DB.getPopularProfiles(5);
|
const popularProfiles = await DB.getPopularProfiles(5);
|
||||||
let content = "Popular users to follow:\n";
|
let content = "Active users to follow:\n";
|
||||||
popularProfiles.forEach((p)=>{
|
popularProfiles.forEach((p)=>{
|
||||||
content += "@p:" + p._id + "\n";
|
content += "@p:" + p._id + "\n";
|
||||||
});
|
});
|
||||||
let popularProfilesPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"});
|
let popularProfilesPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"});
|
||||||
|
posts.push(popularProfilesPost);
|
||||||
|
|
||||||
|
// Popular Groups
|
||||||
const popularGroups = await DB.getPopularGroups(5);
|
const popularGroups = await DB.getPopularGroups(5);
|
||||||
content = "Popular users to follow:\n";
|
content = "Popular groups to follow:\n";
|
||||||
popularGroups.forEach((p)=>{
|
popularGroups.forEach((p)=>{
|
||||||
content += "@p:" + p._id + "\n";
|
content += "@p:" + p._id + "\n";
|
||||||
});
|
});
|
||||||
let popularGroupsPost = new Post({profileid: 1, content, nonOrganicType: "PopularGroups"});
|
let popularGroupsPost = new Post({profileid: 1, content, nonOrganicType: "PopularGroups"});
|
||||||
posts.push(popularProfilesPost);
|
|
||||||
posts.push(popularGroupsPost);
|
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);
|
return res.json(posts);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user