Merge branch 'master' into fix_follow

This commit is contained in:
Leon Liang 2019-12-02 18:38:54 -05:00 committed by GitHub
commit 684ccd1a85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 491 additions and 34 deletions

View File

@ -1,6 +1,8 @@
/* eslint-disable prefer-arrow-callback */ /* eslint-disable prefer-arrow-callback */
/* eslint-disable promise/always-return */ /* eslint-disable promise/always-return */
const admin = require('firebase-admin'); const admin = require('firebase-admin');
const { db } = require('../util/admin');
exports.putPost = (req, res) => { exports.putPost = (req, res) => {
const newPost = { const newPost = {
@ -12,7 +14,8 @@ exports.putPost = (req, res) => {
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
likeCount: 0, likeCount: 0,
commentCount: 0, commentCount: 0,
microBlogTopics: req.body.microBlogTopics microBlogTopics: req.body.microBlogTopics,
quoteBody: null
}; };
admin.firestore().collection('posts').add(newPost) admin.firestore().collection('posts').add(newPost)
@ -30,6 +33,7 @@ exports.putPost = (req, res) => {
exports.getallPostsforUser = (req, res) => { exports.getallPostsforUser = (req, res) => {
var post_query = admin.firestore().collection("posts").where("userHandle", "==", req.user.handle); var post_query = admin.firestore().collection("posts").where("userHandle", "==", req.user.handle);
post_query.get() post_query.get()
.then(function(myPosts) { .then(function(myPosts) {
let posts = []; let posts = [];
@ -39,11 +43,11 @@ exports.getallPostsforUser = (req, res) => {
return res.status(200).json(posts); return res.status(200).json(posts);
}) })
.then(function() { .then(function() {
res.status(200).send("Successfully retrieved all user's posts from database."); return res.status(200).json("Successfully retrieved all user's posts from database.");
return;
}) })
.catch(function(err) { .catch(function(err) {
res.status(500).send("Failed to retrieve user's posts from database.", err); return res.status(500).json("Failed to retrieve user's posts from database.", err);
}); });
}; };
@ -58,14 +62,236 @@ exports.getallPosts = (req, res) => {
return res.status(200).json(posts); return res.status(200).json(posts);
}) })
.then(function() { .then(function() {
res.status(200).send("Successfully retrieved every post from database."); return res.status(200).json("Successfully retrieved every post from database.");
return;
}) })
.catch(function(err) { .catch(function(err) {
res.status(500).send("Failed to retrieve posts from database.", err); return res.status(500).json("Failed to retrieve posts from database.", err);
}); });
}; };
exports.quoteWithPost = (req, res) => {
let quoteData;
const quoteDoc = admin.firestore().collection('quote').
where('userHandle', '==', req.user.handle).
where('postId', '==', req.params.postId).limit(1);
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'});
}
})
.then((data) => {
if(data.empty) {
return admin.firestore().collection('quote').add({
quoteId : req.params.postId,
userHandle : req.user.handle,
quoteBody : req.body.quoteBody
})
.then(() => {
const post = {
body: quoteData.body,
userHandle : req.user.handle,
quoteBody: req.body.quoteBody,
createdAt : new Date().toISOString(),
userImage: req.body.userImage,
likeCount: 0,
commentCount: 0,
userID: req.user.uid,
microBlogTitle: quoteData.microBlogTitle,
microBlogTopics: quoteData.microBlogTopics,
quoteId: req.params.postId
}
return admin.firestore().collection('posts').add(post)
.then((doc) => {
doc.update({postId: doc.id})
const resPost = post;
resPost.postId = doc.id;
return res.status(200).json(resPost);
})
})
}
else {
return res.status(400).json({ error: 'Post has already been quoted.' });
}
})
.catch((err) => {
return res.status(500).json({error: err});
})
}
exports.quoteWithoutPost = (req, res) => {
let quoteData;
const quoteDoc = admin.firestore().collection('quote').
where('userHandle', '==', req.user.handle).
where('postId', '==', req.params.postId).limit(1);
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'});
}
})
.then((data) => {
if(data.empty) {
return admin.firestore().collection('quote').add({
quoteId : req.params.postId,
userHandle : req.user.handle,
quoteBody: null
})
.then(() => {
const post = {
userHandle : req.user.handle,
body: quoteData.body,
quoteBody: null,
createdAt : new Date().toISOString(),
likeCount: 0,
commentCount: 0,
userID: req.user.uid,
userImage: req.body.userImage,
microBlogTitle: quoteData.microBlogTitle,
microBlogTopics: quoteData.microBlogTopics,
quoteId: req.params.postId
}
return admin.firestore().collection('posts').add(post)
.then((doc) => {
doc.update({postId: doc.id})
const resPost = post;
resPost.postId = doc.id;
return res.status(200).json(resPost);
})
})
}
else {
return res.status(400).json({ error: 'Post has already been quoted.' });
}
})
.catch((err) => {
return res.status(500).json({error: 'Something is wrong'});
})
}
exports.checkforLikePost = (req, res) => {
const likedPostDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
.where('postId', '==', req.params.postId).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);
}
})
}
exports.likePost = (req, res) => {
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}`);
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) => {
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}`);
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.getFilteredPosts = (req, res) => { exports.getFilteredPosts = (req, res) => {
admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==') admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==')
}; };

View File

@ -70,7 +70,7 @@ app.post("/removeSub", fbAuth, removeSub);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/post.js * * handlers/post.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
const { getallPostsforUser, getallPosts, putPost } = require("./handlers/post"); const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, quoteWithPost, quoteWithoutPost, checkforLikePost} = require("./handlers/post");
app.get("/getallPostsforUser", fbAuth, getallPostsforUser); app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@ -79,6 +79,15 @@ app.get("/getallPosts", getallPosts);
// Adds one post to the database // Adds one post to the database
app.post("/putPost", fbAuth, putPost); app.post("/putPost", fbAuth, putPost);
app.get("/like/:postId", fbAuth, likePost);
app.get("/unlike/:postId", fbAuth, unlikePost);
app.get("/checkforLikePost/:postId", fbAuth, checkforLikePost);
app.post("/quoteWithPost/:postId", fbAuth, quoteWithPost);
app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/topic.js * * handlers/topic.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/

View File

@ -41,5 +41,5 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api" "proxy": "http://localhost:5001/twistter-e4649/us-central1/api"
} }

View File

@ -11,13 +11,18 @@ import CardContent from "@material-ui/core/CardContent";
import Typography from "@material-ui/core/Typography"; import Typography from "@material-ui/core/Typography";
// component // component
import "../App.css"; import '../App.css';
import logo from "../images/twistter-logo.png"; import logo from '../images/twistter-logo.png';
import noImage from "../images/no-img.png"; import noImage from '../images/no-img.png';
import Writing_Microblogs from "../Writing_Microblogs"; import Writing_Microblogs from '../Writing_Microblogs';
import ReactModal from 'react-modal';
class Home extends Component { class Home extends Component {
state = {}; state = {
};
componentDidMount() { componentDidMount() {
axios axios
@ -31,9 +36,10 @@ class Home extends Component {
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
render() { render() {
let authenticated = this.props.user.authenticated; let authenticated = this.props.user.authenticated;
let username = this.props.user.credentials.handle;
let postMarkup = this.state.posts ? ( let postMarkup = this.state.posts ? (
this.state.posts.map(post => ( this.state.posts.map(post => (
<Card> <Card>
@ -52,18 +58,16 @@ class Home extends Component {
{post.createdAt} {post.createdAt}
</Typography> </Typography>
<br /> <br />
<Typography variant="body1"> <Typography variant="body1"><b>{post.microBlogTitle}</b></Typography>
<b>{post.microBlogTitle}</b> <Typography variant="body2">{post.quoteBody}</Typography>
</Typography> <br />
<Typography variant="body2">{post.body}</Typography> <Typography variant="body2">{post.body}</Typography>
<br /> <br />
<Typography variant="body2"> <Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<b>Topics:</b> {post.microBlogTopics}
</Typography>
<br /> <br />
<Typography variant="body2" color={"textSecondary"}> {/* <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> */}
Likes {post.likeCount} Comments {post.commentCount} <Like microBlog = {post.postId} count = {post.likeCount} name = {username}></Like>
</Typography> <Quote microblog = {post.postId}></Quote>
</CardContent> </CardContent>
</Card> </Card>
)) ))
@ -114,7 +118,217 @@ class Home extends Component {
} }
} }
const mapStateToProps = state => ({ class Quote extends Component {
constructor(props) {
super(props);
this.state = {
characterCount: 250,
showModal: false,
value: ""
}
this.handleSubmitWithoutPost = this.handleSubmitWithoutPost.bind(this);
this.handleOpenModal = this.handleOpenModal.bind(this);
this.handleCloseModal = this.handleCloseModal.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmitWithoutPost(event) {
const post = {
userImage: "bing-url",
}
const headers = {
headers: { "Content-Type": "application/json" }
};
axios.post(`/quoteWithoutPost/${this.props.microblog}`, post, headers)
.then((res) => {
console.log(res.data);
})
.catch(err => {
console.error(err);
});
event.preventDefault();
}
handleOpenModal() {
this.setState({ showModal: true });
}
handleCloseModal() {
this.setState({ showModal: false });
}
handleChangeforPost(event) {
this.setState({ value: event.target.value });
}
handleChangeforCharacterCount(event) {
const charCount = event.target.value.length;
const charRemaining = 250 - charCount;
this.setState({ characterCount: charRemaining });
}
handleSubmit(event) {
const quotedPost = {
quoteBody: this.state.value,
userImage: "bing-url",
};
const headers = {
headers: { "Content-Type": "application/json" }
};
axios.post(`/quoteWithPost/${this.props.microblog}`, quotedPost, headers)
.then((res) => {
console.log(res.data);
})
.catch(err => {
console.error(err);
});
event.preventDefault();
this.setState({ showModal: false, characterCount: 250, value: "" });
}
render() {
return (
<div>
<button onClick={this.handleOpenModal}>Quote with Post</button>
<ReactModal
isOpen={this.state.showModal}
style={{content: {height: "50%", width: "25%", marginTop: "auto", marginLeft: "auto", marginRight: "auto", marginBottom : "auto"}}}
>
<div style={{ width: "200px", marginLeft: "50px" }}>
<form>
<textarea
value={this.state.value}
required
maxLength="250"
placeholder="Write Quoted Post here..."
onChange={e => {
this.handleChangeforPost(e);
this.handleChangeforCharacterCount(e);
}}
cols={40}
rows={20}
/>
<div style={{ fontSize: "14px", marginRight: "-100px" }}>
<p2>Characters Left: {this.state.characterCount}</p2>
</div>
<button onClick={this.handleSubmit}>Share Quoted Post</button>
<button onClick={this.handleCloseModal}>Cancel</button>
</form>
</div>
</ReactModal>
<button onClick={this.handleSubmitWithoutPost}>Quote without Post</button>
</div>
)
}
}
class Like extends Component {
constructor(props) {
super(props)
this.state = {
num : this.props.count,
}
this.handleClick = this.handleClick.bind(this);
}
componentDidMount() {
this.setState({
like: localStorage.getItem(this.props.microBlog + this.props.name) === "false"
})
}
handleClick(){
this.setState({
like: !this.state.like
});
localStorage.setItem(this.props.microBlog + this.props.name, this.state.like.toString())
if(this.state.like == false)
{
this.setState(() => {
return {num: this.state.num + 1}
});
axios.get(`/like/${this.props.microBlog}`)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
})
}
else
{
this.setState(() => {
return {num: this.state.num - 1}
});
axios.get(`/unlike/${this.props.microBlog}`)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
})
}
}
/* componentDidMount() {
axios.get(`/checkforLikePost/${this.props.microBlog}`)
.then((res) => {
this.setState({
like2: res.data
})
console.log(res.data);
})
.catch((err) => {
console.log(err)
})
if (this.state.like2 === this.state.like)
{
this.setState({
like: false
})
}
} */
render() {
const label = this.state.like ? 'Unlike' : 'Like'
return(
<div>
<Typography variant="body2" color={"textSecondary"}>Likes {this.state.num}</Typography>
<button onClick={this.handleClick}>{label}</button>
</div>
)
}
}
const mapStateToProps = (state) => ({
user: state.user user: state.user
}); });
@ -122,4 +336,12 @@ Home.propTypes = {
user: PropTypes.object.isRequired user: PropTypes.object.isRequired
}; };
export default connect(mapStateToProps)(Home); Like.propTypes = {
user: PropTypes.object.isRequired
}
Quote.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(Home, Like, Quote);

View File

@ -147,7 +147,7 @@ class user extends Component {
// console.log(res.data); // console.log(res.data);
this.setState({ this.setState({
posts: res.data posts: res.data
}); })
}) })
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
@ -216,19 +216,19 @@ class user extends Component {
<Typography variant="body2" color={"textSecondary"}> <Typography variant="body2" color={"textSecondary"}>
{post.createdAt} {post.createdAt}
</Typography> </Typography>
<br /> <br />
<Typography variant="body1"> <Typography variant="body1">
<b>{post.microBlogTitle}</b> <b>{post.microBlogTitle}</b>
</Typography> </Typography>
<Typography variant="body2">{post.quoteBody}</Typography>
<br />
<Typography variant="body2">{post.body}</Typography> <Typography variant="body2">{post.body}</Typography>
<br /> <br />
<Typography variant="body2"> <Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<b>Topics:</b> {post.microBlogTopics}
</Typography>
<br /> <br />
<Typography variant="body2" color={"textSecondary"}> <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography>
Likes {post.likeCount} Comments {post.commentCount}
</Typography>
</CardContent> </CardContent>
</Card> </Card>
)) ))