Filtered timeline posts to only followers

This commit is contained in:
Aaron Sun 2019-11-17 22:36:52 -05:00
parent 6184a22607
commit 1a2f269466
3 changed files with 51 additions and 9 deletions

View File

@ -63,3 +63,43 @@ exports.getallPosts = (req, res) => {
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);
});
};

View File

@ -44,12 +44,14 @@ 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);

View File

@ -21,7 +21,7 @@ class Home extends Component {
componentDidMount() {
axios
.get("/getallPosts")
.get("/getFollowedPosts")
.then(res => {
console.log(res.data);
this.setState({