From 1a2f26946623e91962c049228fd0de75038ab23e Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Sun, 17 Nov 2019 22:36:52 -0500 Subject: [PATCH] Filtered timeline posts to only followers --- functions/handlers/post.js | 52 +++++++++++++++++++++++++---- functions/index.js | 6 ++-- twistter-frontend/src/pages/Home.js | 2 +- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 48fbbcc..45b9ed2 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -37,11 +37,11 @@ exports.getallPostsforUser = (req, res) => { return res.status(200).json(posts); }) .then(function() { - res.status(200).send("Successfully retrieved all user's posts from database."); - return; + res.status(200).send("Successfully retrieved all user's posts from database."); + return; }) .catch(function(err) { - res.status(500).send("Failed to retrieve user's posts from database.", err); + res.status(500).send("Failed to retrieve user's posts from database.", err); }); }; @@ -56,10 +56,50 @@ exports.getallPosts = (req, res) => { return res.status(200).json(posts); }) .then(function() { - res.status(200).send("Successfully retrieved every post from database."); - return; + res.status(200).send("Successfully retrieved every post from database."); + return; }) .catch(function(err) { - res.status(500).send("Failed to retrieve posts from database.", err); + res.status(500).send("Failed to retrieve posts from database.", err); + }); +}; + +exports.getFollowedPosts = (req, res) => { + var post_query = admin.firestore().collection("posts"); + var followers_list = admin.firestore().collection("users").doc(req.user.handle).collection("followedUsers"); + let followers_str = []; + followers_str.push(req.user.handle); + + followers_list.get() + .then(function(allFollowers) { + allFollowers.forEach(function(followers) { + followers_str.push(followers.data().handle); + }); + + post_query.get() + .then(function(allPosts) { + let posts = []; + allPosts.forEach(function(doc) { + if(followers_str.includes(doc.data().userHandle)) { + posts.push(doc.data()); + } + }); + return res.status(200).json(posts); + }) + /*.then(function() { + res.status(200).send("Successfully retrieved all posts from followed users."); + return; + })*/ + .catch(function(err) { + res.status(500).send("Failed to retrieve posts.", err); + }); + }) + .then(function() { + //res.status(200).send("Successfully added all follower handles to array."); + //res.status(200).send("Successfully retrieved all posts from followed users."); + return; + }) + .catch(function(err) { + res.status(500).send("Failed to retrieve follower handles.", err); }); }; diff --git a/functions/index.js b/functions/index.js index 12bb46d..65fbc67 100644 --- a/functions/index.js +++ b/functions/index.js @@ -44,17 +44,19 @@ app.get("/user", fbAuth, getAuthenticatedUser); /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ -const { getallPostsforUser, getallPosts, putPost } = require("./handlers/post"); +const { getallPostsforUser, getallPosts, getFollowedPosts, putPost } = require("./handlers/post"); app.get("/getallPostsforUser", fbAuth, getallPostsforUser); app.get("/getallPosts", getallPosts); +app.get("/getFollowedPosts", fbAuth, getFollowedPosts); + // Adds one post to the database app.post("/putPost", fbAuth, putPost); /*------------------------------------------------------------------* - * handlers/topic.js * + * handlers/topic.js * *------------------------------------------------------------------*/ const { putTopic, diff --git a/twistter-frontend/src/pages/Home.js b/twistter-frontend/src/pages/Home.js index cee0a93..6f1439a 100644 --- a/twistter-frontend/src/pages/Home.js +++ b/twistter-frontend/src/pages/Home.js @@ -21,7 +21,7 @@ class Home extends Component { componentDidMount() { axios - .get("/getallPosts") + .get("/getFollowedPosts") .then(res => { console.log(res.data); this.setState({