All done with only showing posts user is interested in

This commit is contained in:
Aaron Sun 2019-11-19 12:25:55 -05:00
parent 953d58ea12
commit 54ba36abeb

View File

@ -65,36 +65,46 @@ exports.getallPosts = (req, res) => {
}; };
exports.getFollowedPosts = (req, res) => { exports.getFollowedPosts = (req, res) => {
let posts = [];
var followers_list = admin.firestore().collection("users").doc(req.user.handle).collection("followedUsers"); var followers_list = admin.firestore().collection("users").doc(req.user.handle).collection("followedUsers");
var post_query = admin.firestore().collection("posts");
followers_list.get() followers_list.get()
.then(function(allFollowers) { .then(function(allFollowers) {
var followers_likedTopics = new Map();
allFollowers.forEach(function(followers) { allFollowers.forEach(function(followers) {
var post_query = admin.firestore().collection("posts").where("userHandle", "==", followers.data().handle); followers_likedTopics.set(followers.data().handle, followers.data().followedTopics);
post_query.get() });
.then(function(allPosts) {
allPosts.forEach(function(doc) { post_query.get()
var follow = false; .then(function(allPosts) {
doc.data().microBlogTopics.forEach(function(topics) { let posts = [];
follow |= followers.data().followedTopics.includes(topics); allPosts.forEach(function(doc) {
}) if(doc.data().userHandle === req.user.handle) {
if(follow) { posts.push(doc.data());
posts.push(doc.data()); }
}
}); if(followers_likedTopics.has(doc.data().userHandle)) {
return res.status(200).json(posts); //posts.push(doc.data());
})
.catch(function(err) { doc.data().microBlogTopics.forEach(function(topic) {
res.status(500).send("Failed to retrieve posts from database.", err); if(followers_likedTopics.get(doc.data().userHandle).includes(topic)) {
}); posts.push(doc.data());
}
});
}
});
return res.status(200).json(posts);
})
.catch(function(err) {
res.status(500).send("Failed to retrieve any posts.", err);
}); });
//return res.status(200).json(posts);
}) })
.then(function() { .then(function() {
//res.status(200).send("Successfully retrieved all interesting posts from followed users."); //res.status(200).send("Successfully retrieved all interesting posts from followed users.");
return; return;
}) })
.catch(function(err) { .catch(function(err) {
res.status(500).send("Failed to retrieve any post.", err); res.status(500).send("Failed to retrieve any posts.", err);
}); });
}; };