From e3ef57c24230abe4bc48fd920f2d2abcc9bad9d5 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 28 Mar 2019 12:53:29 -0700 Subject: [PATCH] Now by default like your own comment. --- server/src/actions/comment.rs | 6 +--- server/src/websocket_server/server.rs | 43 +++++++++++++++++++++------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/server/src/actions/comment.rs b/server/src/actions/comment.rs index 7f2dace6c..6a1a4671e 100644 --- a/server/src/actions/comment.rs +++ b/server/src/actions/comment.rs @@ -145,7 +145,7 @@ pub struct CommentView { } impl CommentView { - fn from_comment(comment: &Comment, likes: &Vec, fedi_user_id: &Option) -> Self { + pub fn from_comment(comment: &Comment, likes: &Vec, fedi_user_id: &Option) -> Self { let mut upvotes: i32 = 0; let mut downvotes: i32 = 0; let mut my_vote: Option = Some(0); @@ -182,10 +182,6 @@ impl CommentView { } } - pub fn from_new_comment(comment: &Comment) -> Self { - Self::from_comment(comment, &Vec::new(), &None) - } - pub fn read(conn: &PgConnection, comment_id: i32, fedi_user_id: &Option) -> Self { let comment = Comment::read(&conn, comment_id).unwrap(); let likes = CommentLike::read(&conn, comment_id).unwrap(); diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index 78a71ec8e..cb2b619bf 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -693,26 +693,45 @@ impl Perform for CreateComment { let user_id = claims.id; let iss = claims.iss; + let fedi_user_id = format!("{}/{}", iss, user_id); let comment_form = CommentForm { content: self.content.to_owned(), parent_id: self.parent_id.to_owned(), post_id: self.post_id, - attributed_to: format!("{}/{}", iss, user_id), + attributed_to: fedi_user_id.to_owned(), updated: None }; let inserted_comment = match Comment::create(&conn, &comment_form) { Ok(comment) => comment, Err(e) => { - return self.error("Couldn't create Post"); + return self.error("Couldn't create Comment"); } }; - // TODO You like your own comment by default + // You like your own comment by default + let like_form = CommentLikeForm { + comment_id: inserted_comment.id, + post_id: self.post_id, + fedi_user_id: fedi_user_id.to_owned(), + score: 1 + }; - // Simulate a comment view to get back blank score, no need to fetch anything - let comment_view = CommentView::from_new_comment(&inserted_comment); + let inserted_like = match CommentLike::like(&conn, &like_form) { + Ok(like) => like, + Err(e) => { + return self.error("Couldn't like comment."); + } + }; + + let likes: Vec = vec![inserted_like]; + + let comment_view = CommentView::from_comment(&inserted_comment, &likes, &Some(fedi_user_id)); + + + let mut comment_sent = comment_view.clone(); + comment_sent.my_vote = None; let comment_out = serde_json::to_string( &CreateCommentResponse { @@ -721,11 +740,17 @@ impl Perform for CreateComment { } ) .unwrap(); - - chat.send_room_message(self.post_id, &comment_out, addr); - // println!("{:?}", chat.rooms.keys()); - // println!("{:?}", chat.rooms.get(&5i32).unwrap()); + + let comment_sent_out = serde_json::to_string( + &CreateCommentLikeResponse { + op: self.op_type().to_string(), + comment: comment_sent + } + ) + .unwrap(); + + chat.send_room_message(self.post_id, &comment_sent_out, addr); comment_out }