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

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