diff --git a/functions/handlers/post.js b/functions/handlers/post.js index 2f5ce4f..340220c 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -66,46 +66,77 @@ exports.getallPosts = (req, res) => { // Get all the posts var postsPromise = new Promise((resolve, reject) => { - db.collection("posts").get() - .then((allPosts) => { - allPosts.forEach((post) => { + db.collection("posts") + .get() + .then(allPosts => { + allPosts.forEach(post => { posts.push(post.data()); }); resolve(); }) - .catch((error) => { + .catch(error => { reject(error); - }) + }); }); // Get all users var usersPromise = new Promise((resolve, reject) => { - db.collection("users").get() - .then((allUsers) => { - allUsers.forEach((user) => { + db.collection("users") + .get() + .then(allUsers => { + allUsers.forEach(user => { users[user.data().handle] = user.data(); - }) + }); resolve(); }) - .catch((error) => { + .catch(error => { reject(error); - }) + }); }); // Wait for the two promises Promise.all([postsPromise, usersPromise]) .then(() => { - let newPosts = [] + let newPosts = []; // Add the image url of the person who made the post to all of the post objects - posts.forEach((post) => { - post.profileImage = users[post.userHandle].imageUrl ? users[post.userHandle].imageUrl : null; + posts.forEach(post => { + post.profileImage = users[post.userHandle].imageUrl + ? users[post.userHandle].imageUrl + : null; newPosts.push(post); }); return res.status(200).json(newPosts); }) - .catch((error) => { - return res.status(500).json({error}); + .catch(error => { + return res.status(500).json({ error }); + }); +}; + +exports.getAlert = (req, res) => { + var post_query = admin + .firestore() + .collection("posts") + .where("microBlogTitle", "==", "Alert"); + + post_query + .get() + .then(function(myPosts) { + let posts = []; + myPosts.forEach(function(doc) { + posts.push(doc.data()); + }); + return res.status(200).json(posts); }) + .then(function() { + return res + .status(200) + .json("Successfully retrieved all user's posts from database."); + }) + .catch(function(err) { + return res + .status(500) + .json("Failed to retrieve user's posts from database.", err); + }); }; exports.getOtherUsersPosts = (req, res) => { @@ -136,23 +167,25 @@ exports.getOtherUsersPosts = (req, res) => { }; exports.quoteWithPost = (req, res) => { - let quoteData; - const quoteDoc = admin.firestore().collection('quote'). - where('userHandle', '==', req.user.handle). - where('quoteId', '==', req.params.postId).limit(1); + let quoteData; + const quoteDoc = admin + .firestore() + .collection("quote") + .where("userHandle", "==", req.user.handle) + .where("quoteId", "==", req.params.postId) + .limit(1); - const postDoc = db.doc(`/posts/${req.params.postId}`); + const postDoc = db.doc(`/posts/${req.params.postId}`); - postDoc.get() - .then((doc) => { - if(doc.exists) { - quoteData = doc.data(); - return quoteDoc.get(); - } - else - { - return res.status(404).json({error: 'Post not found'}); - } + postDoc + .get() + .then(doc => { + if (doc.exists) { + quoteData = doc.data(); + return quoteDoc.get(); + } else { + return res.status(404).json({ error: "Post not found" }); + } }) .then(data => { if (data.empty) { @@ -200,23 +233,25 @@ exports.quoteWithPost = (req, res) => { }; exports.quoteWithoutPost = (req, res) => { - let quoteData; - const quoteDoc = admin.firestore().collection('quote'). - where('userHandle', '==', req.user.handle). - where('quoteId', '==', req.params.postId).limit(1); + let quoteData; + const quoteDoc = admin + .firestore() + .collection("quote") + .where("userHandle", "==", req.user.handle) + .where("quoteId", "==", req.params.postId) + .limit(1); - const postDoc = db.doc(`/posts/${req.params.postId}`); + const postDoc = db.doc(`/posts/${req.params.postId}`); - postDoc.get() - .then((doc) => { - if(doc.exists) { - quoteData = doc.data(); - return quoteDoc.get(); - } - else - { - return res.status(404).json({error: 'Post not found'}); - } + postDoc + .get() + .then(doc => { + if (doc.exists) { + quoteData = doc.data(); + return quoteDoc.get(); + } else { + return res.status(404).json({ error: "Post not found" }); + } }) .then(data => { if (data.empty) { @@ -258,7 +293,7 @@ exports.quoteWithoutPost = (req, res) => { } }) .catch(err => { - // return res.status(500).json({ error: "Something is wrong" }); + // return res.status(500).json({ error: "Something is wrong" }); return res.status(500).json({ error: err }); }); }; @@ -272,202 +307,198 @@ exports.checkforLikePost = (req, res) => { .limit(1); let result; - likedPostDoc.get().then(data => { - if (data.empty) { - result = false; - return res.status(200).json(result); - } else { - result = true; - return res.status(200).json(result); - } - }) - .catch((err) => { + likedPostDoc + .get() + .then(data => { + if (data.empty) { + result = false; + return res.status(200).json(result); + } else { + result = true; + return res.status(200).json(result); + } + }) + .catch(err => { console.log(err); - return res.status(500).json({error: err}); - }) + return res.status(500).json({ error: err }); + }); }; exports.likePost = (req, res) => { + const postId = req.params.postId; + let likedPostDoc; + db.doc(`/users/${req.userData.handle}`) + .get() + .then(userDoc => { + let likes = userDoc.data().likes; + if (likes === undefined || likes === null) { + likes = []; + } - const postId = req.params.postId; - let likedPostDoc; - db.doc(`/users/${req.userData.handle}`) - .get() - .then((userDoc) => { - let likes = userDoc.data().likes; - if (likes === undefined || likes === null) { - likes = []; - } + if (likes.includes(postId)) { + return res + .status(400) + .json({ error: "This user has already liked this post" }); + } - if (likes.includes(postId)) { - return res.status(400).json({error: "This user has already liked this post"}); - } + likes.push(postId); - likes.push(postId); + return userDoc.ref.update({ likes }); + }) + .then(() => { + return db.doc(`/posts/${postId}`).get(); + }) + .then(postDoc => { + let postData = postDoc.data(); + postData.likeCount++; + likedPostDoc = postData; + return postDoc.ref.update({ likeCount: postData.likeCount }); + }) + .then(() => { + return res.status(201).json(likedPostDoc); + }) + .catch(err => { + console.log(err); + return res.status(500).json({ error: err }); + }); - return userDoc.ref.update({likes}) - }) - .then(() => { - return db.doc(`/posts/${postId}`).get() - - }) - .then((postDoc) => { - let postData = postDoc.data(); - postData.likeCount++; - likedPostDoc = postData; - return postDoc.ref.update({likeCount : postData.likeCount}) - }) - .then(() => { - return res.status(201).json(likedPostDoc); - }) - .catch((err) => { - console.log(err); - return res.status(500).json({error: err}); - }) + // let postData; + // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) + // .where('postId', '==', req.params.postId).limit(1); - // let postData; - // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) - // .where('postId', '==', req.params.postId).limit(1); + // const postDoc = db.doc(`/posts/${req.params.postId}`); - // const postDoc = db.doc(`/posts/${req.params.postId}`); - - // postDoc.get() - // .then((doc) => { - // if(doc.exists) { - // postData = doc.data(); - // return likeDoc.get(); - // } - // else - // { - // return res.status(404).json({error: 'Post not found'}); - // } - // }) - // .then((data) => { - // if (data.empty) { - // return admin.firestore().collection('likes').add({ - // postId : req.params.postId, - // userHandle: req.user.handle - - // }) - // .then(() => { - // postData.likeCount++; - // return postDoc.update({likeCount : postData.likeCount}) - // }) - // .then(() => { - // return res.status(200).json(postData); - // }) - // } - // }) - // .catch((err) => { - // return res.status(500).json({error: 'Something is wrong'}); - // }) - -} + // postDoc.get() + // .then((doc) => { + // if(doc.exists) { + // postData = doc.data(); + // return likeDoc.get(); + // } + // else + // { + // return res.status(404).json({error: 'Post not found'}); + // } + // }) + // .then((data) => { + // if (data.empty) { + // return admin.firestore().collection('likes').add({ + // postId : req.params.postId, + // userHandle: req.user.handle + // }) + // .then(() => { + // postData.likeCount++; + // return postDoc.update({likeCount : postData.likeCount}) + // }) + // .then(() => { + // return res.status(200).json(postData); + // }) + // } + // }) + // .catch((err) => { + // return res.status(500).json({error: 'Something is wrong'}); + // }) +}; exports.unlikePost = (req, res) => { + const postId = req.params.postId; + let likedPostDoc; + db.doc(`/users/${req.userData.handle}`) + .get() + .then(userDoc => { + let likes = userDoc.data().likes; + if (likes === undefined || likes === null) { + likes = []; + } - const postId = req.params.postId; - let likedPostDoc; - db.doc(`/users/${req.userData.handle}`) - .get() - .then((userDoc) => { - let likes = userDoc.data().likes; - if (likes === undefined || likes === null) { - likes = []; - } + if (!likes.includes(postId)) { + return res + .status(400) + .json({ error: "This user hasn't liked this post yet" }); + } - if (!likes.includes(postId)) { - return res.status(400).json({error: "This user hasn't liked this post yet"}); - } + let i; + for (i = 0; i < likes.length; i++) { + if (likes[i] === postId) { + likes.splice(i, 1); + } + } - let i; - for (i = 0; i < likes.length; i++) { - if (likes[i] === postId) { - likes.splice(i, 1); - } - } + return userDoc.ref.update({ likes }); + }) + .then(() => { + return db.doc(`/posts/${postId}`).get(); + }) + .then(postDoc => { + let postData = postDoc.data(); + postData.likeCount--; + likedPostDoc = postData; + return postDoc.ref.update({ likeCount: postData.likeCount }); + }) + .then(() => { + return res.status(201).json(likedPostDoc); + }) + .catch(err => { + console.log(err); + return res.status(500).json({ error: err }); + }); - return userDoc.ref.update({likes}) - }) - .then(() => { - return db.doc(`/posts/${postId}`).get() - - }) - .then((postDoc) => { - let postData = postDoc.data(); - postData.likeCount--; - likedPostDoc = postData; - return postDoc.ref.update({likeCount : postData.likeCount}) - }) - .then(() => { - return res.status(201).json(likedPostDoc); - }) - .catch((err) => { - console.log(err); - return res.status(500).json({error: err}); - }) + // let postData; + // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) + // .where('postId', '==', req.params.postId).limit(1); - // let postData; - // const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle) - // .where('postId', '==', req.params.postId).limit(1); + // const postDoc = db.doc(`/posts/${req.params.postId}`); - // const postDoc = db.doc(`/posts/${req.params.postId}`); - - // postDoc.get() - // .then((doc) => { - // if(doc.exists) { - // postData = doc.data(); - // return likeDoc.get(); - // } - // else - // { - // return res.status(404).json({error: 'Post not found'}); - // } - // }) - // .then((data) => { - // return db - // .doc(`/likes/${data.docs[0].id}`) - // .delete() - // .then(() => { - // postData.likeCount--; - // return postDoc.update({ likeCount: postData.likeCount }); - // }) - // .then(() => { - // res.status(200).json(postData); - // }); - - // }) - // .catch((err) => { - // console.error(err); - // return res.status(500).json({error: 'Something is wrong'}); - // }) - -} + // postDoc.get() + // .then((doc) => { + // if(doc.exists) { + // postData = doc.data(); + // return likeDoc.get(); + // } + // else + // { + // return res.status(404).json({error: 'Post not found'}); + // } + // }) + // .then((data) => { + // return db + // .doc(`/likes/${data.docs[0].id}`) + // .delete() + // .then(() => { + // postData.likeCount--; + // return postDoc.update({ likeCount: postData.likeCount }); + // }) + // .then(() => { + // res.status(200).json(postData); + // }); + // }) + // .catch((err) => { + // console.error(err); + // return res.status(500).json({error: 'Something is wrong'}); + // }) +}; exports.getLikes = (req, res) => { - db.doc(`/users/${req.userData.handle}`) - .get() - .then((doc) => { - let likes = doc.data().likes; - if (likes === undefined || likes === null) { - likes = []; - } - return res.status(200).json({likes}); - }) - .catch((err) => { - console.log(err); - return res.status(500).json({error: err}); - }) -} + db.doc(`/users/${req.userData.handle}`) + .get() + .then(doc => { + let likes = doc.data().likes; + if (likes === undefined || likes === null) { + likes = []; + } + return res.status(200).json({ likes }); + }) + .catch(err => { + console.log(err); + return res.status(500).json({ error: err }); + }); +}; exports.getFilteredPosts = (req, res) => { - admin .firestore() .collection("posts") .where("userHandle", "==", "new user") .where("microBlogTopics", "=="); }; - diff --git a/functions/index.js b/functions/index.js index 4392439..f8b9192 100644 --- a/functions/index.js +++ b/functions/index.js @@ -100,13 +100,23 @@ app.post("/addSubscription", fbAuth, addSubscription); // remove one subscription app.post("/removeSub", fbAuth, removeSub); - /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ -const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, getLikes, quoteWithPost, quoteWithoutPost, checkforLikePost, getOtherUsersPosts} = require("./handlers/post"); - +const { + getallPostsforUser, + getallPosts, + putPost, + likePost, + unlikePost, + getLikes, + quoteWithPost, + quoteWithoutPost, + checkforLikePost, + getOtherUsersPosts, + getAlert +} = require("./handlers/post"); app.get("/getallPostsforUser", fbAuth, getallPostsforUser); @@ -125,6 +135,8 @@ app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost); app.post("/getOtherUsersPosts", fbAuth, getOtherUsersPosts); +app.get("/getAlert", fbAuth, getAlert); + /*------------------------------------------------------------------* * handlers/topic.js * *------------------------------------------------------------------*/ diff --git a/twistter-frontend/src/pages/otherUser.js b/twistter-frontend/src/pages/otherUser.js index a7b4935..f1ce1a4 100644 --- a/twistter-frontend/src/pages/otherUser.js +++ b/twistter-frontend/src/pages/otherUser.js @@ -149,6 +149,23 @@ class user extends Component { }); }) .catch(err => console.log(err)); + + axios + .get("/getAlert") + .then(res => { + let temp = this.state.posts; + console.log(res.data); + res.data.forEach(element => { + element ? temp.push(element) : console.err; + }); + // temp.push(res.data[0]); + this.setState({ + posts: temp + }); + }) + .catch(function(err) { + console.log(err); + }); } render() { @@ -211,7 +228,7 @@ class user extends Component { ) : ( ); - + console.log(this.state.posts); let postMarkup = this.state.posts ? ( this.state.posts.map(post => (