From e0c6470bce6f03b696969693bb9e117c7c999f54 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Wed, 16 Feb 2022 22:28:11 -0800 Subject: [PATCH] FIrst nonorganic posts --- dbTools/profile.js | 40 ++++++++++++++++++++++++++++++++++++++++ def/post.js | 2 ++ routes/post.js | 17 +++++++++++++++-- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/dbTools/profile.js b/dbTools/profile.js index 58e7e3c..8513e12 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -48,6 +48,46 @@ userDB = (DB) => { 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) => { if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; return DB.getProfile(profileId); diff --git a/def/post.js b/def/post.js index 372e311..6ec6c41 100644 --- a/def/post.js +++ b/def/post.js @@ -7,6 +7,7 @@ class Post { this.reactions = info.reactions || {}; this.comments = info.comments || []; this.bookmarks = info.bookmarks || []; //set profiles subscribed to this post + this.nonOrganicType = info.nonOrganicType; //This should record edits this.contentHistory = info.contentHistory || []; this.toProfile = info.toProfile || ''; @@ -38,6 +39,7 @@ class Post { r.createdAt = this.createdAt; r.reactions = this.reactions; r.comments = this.comments; + r.nonOrganicType = this.nonOrganicType; r.contentHistory = this.contentHistory; r.lastUpdated = this.lastUpdated; r.toProfile = this.toProfile; diff --git a/routes/post.js b/routes/post.js index 05d73b6..4d1b123 100644 --- a/routes/post.js +++ b/routes/post.js @@ -20,8 +20,21 @@ DB.getDB.then((DB) => { const profileid = getProfileId(req); let posts = await DB.getFeed(profileid); //Add non-organic posts - - return res.json(posts) + const popularProfiles = await DB.getPopularProfiles(5); + 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) => {