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() post_query.get()
.then(function(allPosts) { .then(function(allPosts) {
let posts = [];
allPosts.forEach(function(doc) { allPosts.forEach(function(doc) {
var follow = false; if(doc.data().userHandle === req.user.handle) {
doc.data().microBlogTopics.forEach(function(topics) {
follow |= followers.data().followedTopics.includes(topics);
})
if(follow) {
posts.push(doc.data()); posts.push(doc.data());
} }
if(followers_likedTopics.has(doc.data().userHandle)) {
//posts.push(doc.data());
doc.data().microBlogTopics.forEach(function(topic) {
if(followers_likedTopics.get(doc.data().userHandle).includes(topic)) {
posts.push(doc.data());
}
});
}
}); });
return res.status(200).json(posts); return res.status(200).json(posts);
}) })
.catch(function(err) { .catch(function(err) {
res.status(500).send("Failed to retrieve posts from database.", 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);
}); });
}; };