From 9a302a87de1197dea7ab55b55f1647b6f39e4ed7 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 2 Sep 2021 12:02:09 -0700 Subject: [PATCH] feed with followers only --- dbTools/post.js | 21 +++++++++++++++++++++ dbTools/profile.js | 28 ++++++++++++++++++++++++++++ routes/post.js | 3 ++- routes/profile.js | 20 +++++++++++++++++++- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index 55f2c64..3005edc 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -91,6 +91,27 @@ postDB = (DB)=>{ }); } + DB.getFeed = async (profileId) => { + const profile = await DB.getProfile(profileId); + let ids = profile.following.map((id)=>DB.ObjectID(id)); + ids.push(profileId) + const _id = DB.ObjectID(profileId); + query = { + $or: [ + {profileid: { + $in: ids + }}, + {toProfile: { + $in: ids + }} + ] + }; + return DB.postCols.find(query).sort({_id: -1}).limit(20).toArray().catch((err)=>{ + console.log(err); + return false; + }); + } + DB.getPostsOfUser = (userId) => { let userid = DB.ObjectID(userId); return DB.postCols.find({userid}).sort({_id: -1}).toArray().catch((err)=>{ diff --git a/dbTools/profile.js b/dbTools/profile.js index 0cbb12b..695398f 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -45,6 +45,32 @@ userDB = (DB) => { return r[index]; } + DB.followProfile = async (profileId, followProfileId)=>{ + const _id = DB.ObjectID(profileId); + let update = { + $addToSet:{ + following: followProfileId + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + + DB.unfollowProfile = async (profileId, followProfileId)=>{ + const _id = DB.ObjectID(profileId); + let update = { + $pull:{ + following: followProfileId + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + //Groups DB.getGroups = async () => { let r = await DB.profileCols.find({isGroup: true}) @@ -75,6 +101,7 @@ userDB = (DB) => { ["subscribed." + profileid]: new Date() } } + DB.followProfile(profileid, groupid) return DB.profileCols.updateOne({_id}, update).catch((err)=>{ console.log(err); return false; @@ -88,6 +115,7 @@ userDB = (DB) => { ["subscribed." + profileid]: "", } } + DB.unfollowProfile(profileid, groupid) return DB.profileCols.updateOne({_id}, update).catch((err)=>{ console.log(err); return false; diff --git a/routes/post.js b/routes/post.js index afa383a..848c8dc 100644 --- a/routes/post.js +++ b/routes/post.js @@ -11,7 +11,8 @@ DB.getDB.then((DB)=>{ } router.get("/", async (req, res) => { - let posts = await DB.getPosts(); + const profileid = getProfileId(req); + let posts = await DB.getFeed(profileid); return res.json(posts) }); diff --git a/routes/profile.js b/routes/profile.js index 74c9a99..c4b7ac0 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -81,7 +81,7 @@ DB.getDB.then((DB)=>{ router.get("/groups/:id/unsubscribe", async (req, res) => { const groupid = req.params.id; const profileid = getProfileId(req); - DB.unsubscribeToGroup(profileid, groupid).then(console.log); + DB.unsubscribeToGroup(profileid, groupid); return res.json({ status: "ok" }); @@ -96,6 +96,24 @@ DB.getDB.then((DB)=>{ }); }); + router.get("/:id/follow", async (req, res) => { + let followProfileId = req.params.id; + const profileid = getProfileId(req); + DB.followProfile(profileid, followProfileId); + return res.json({ + status: "ok" + }); + }); + + router.get("/:id/unfollow", async (req, res) => { + let followProfileId = req.params.id; + const profileid = getProfileId(req); + DB.unfollowProfile(profileid, followProfileId); + return res.json({ + status: "ok" + }); + }); + }); module.exports = router \ No newline at end of file