FIrst nonorganic posts

This commit is contained in:
aeroreyna
2022-02-16 22:28:11 -08:00
parent 117bc5833a
commit e0c6470bce
3 changed files with 57 additions and 2 deletions

View File

@@ -48,6 +48,46 @@ userDB = (DB) => {
return r; return r;
} }
DB.getPopularProfiles = async (limit = 10) => {
return DB.profileCols.aggregate([
{
$match: {isGroup: false}
},
{
$addFields: { subscribed_count: {$size: { "$ifNull": [ {"$objectToArray" : "$subscribed"}, [] ] } } }
},
{
$sort: {"subscribed_count":-1}
},
{
$project: {_id: 1, "subscribed_count": 1}
}
]).limit(limit).toArray().catch((err) => {
console.log(err);
return false;
});
}
DB.getPopularGroups = async (limit = 10) => {
return DB.profileCols.aggregate([
{
$match: {isGroup: true, isPrivate: false, isCourse: false}
},
{
$addFields: { subscribed_count: {$size: { "$ifNull": [ {"$objectToArray" : "$subscribed"}, [] ] } } }
},
{
$sort: {"subscribed_count":-1}
},
{
$project: {_id: 1, "subscribed_count": 1}
}
]).limit(limit).toArray().catch((err) => {
console.log(err);
return false;
});
}
DB.getProfileCache = async (profileId) => { DB.getProfileCache = async (profileId) => {
if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
return DB.getProfile(profileId); return DB.getProfile(profileId);

View File

@@ -7,6 +7,7 @@ class Post {
this.reactions = info.reactions || {}; this.reactions = info.reactions || {};
this.comments = info.comments || []; this.comments = info.comments || [];
this.bookmarks = info.bookmarks || []; //set profiles subscribed to this post this.bookmarks = info.bookmarks || []; //set profiles subscribed to this post
this.nonOrganicType = info.nonOrganicType;
//This should record edits //This should record edits
this.contentHistory = info.contentHistory || []; this.contentHistory = info.contentHistory || [];
this.toProfile = info.toProfile || ''; this.toProfile = info.toProfile || '';
@@ -38,6 +39,7 @@ class Post {
r.createdAt = this.createdAt; r.createdAt = this.createdAt;
r.reactions = this.reactions; r.reactions = this.reactions;
r.comments = this.comments; r.comments = this.comments;
r.nonOrganicType = this.nonOrganicType;
r.contentHistory = this.contentHistory; r.contentHistory = this.contentHistory;
r.lastUpdated = this.lastUpdated; r.lastUpdated = this.lastUpdated;
r.toProfile = this.toProfile; r.toProfile = this.toProfile;

View File

@@ -20,8 +20,21 @@ DB.getDB.then((DB) => {
const profileid = getProfileId(req); const profileid = getProfileId(req);
let posts = await DB.getFeed(profileid); let posts = await DB.getFeed(profileid);
//Add non-organic posts //Add non-organic posts
const popularProfiles = await DB.getPopularProfiles(5);
return res.json(posts) let content = "Popular users to follow:\n";
popularProfiles.forEach((p)=>{
content += "@p:" + p._id + "\n";
});
let popularProfilesPost = new Post({profileid: 1, content, nonOrganicType: "PopularUsers"});
const popularGroups = await DB.getPopularGroups(5);
content = "Popular users 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);
return res.json(posts);
}); });
router.get("/usr/:id", async (req, res) => { router.get("/usr/:id", async (req, res) => {