Now by default like your own comment.

This commit is contained in:
Dessalines 2019-03-28 12:53:29 -07:00
parent 0ee8f50abe
commit 03a2f67d57
2 changed files with 35 additions and 14 deletions

View file

@ -145,7 +145,7 @@ pub struct CommentView {
} }
impl CommentView { impl CommentView {
fn from_comment(comment: &Comment, likes: &Vec<CommentLike>, fedi_user_id: &Option<String>) -> Self { pub fn from_comment(comment: &Comment, likes: &Vec<CommentLike>, fedi_user_id: &Option<String>) -> Self {
let mut upvotes: i32 = 0; let mut upvotes: i32 = 0;
let mut downvotes: i32 = 0; let mut downvotes: i32 = 0;
let mut my_vote: Option<i16> = Some(0); let mut my_vote: Option<i16> = 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<String>) -> Self { pub fn read(conn: &PgConnection, comment_id: i32, fedi_user_id: &Option<String>) -> Self {
let comment = Comment::read(&conn, comment_id).unwrap(); let comment = Comment::read(&conn, comment_id).unwrap();
let likes = CommentLike::read(&conn, comment_id).unwrap(); let likes = CommentLike::read(&conn, comment_id).unwrap();

View file

@ -693,26 +693,45 @@ impl Perform for CreateComment {
let user_id = claims.id; let user_id = claims.id;
let iss = claims.iss; let iss = claims.iss;
let fedi_user_id = format!("{}/{}", iss, user_id);
let comment_form = CommentForm { let comment_form = CommentForm {
content: self.content.to_owned(), content: self.content.to_owned(),
parent_id: self.parent_id.to_owned(), parent_id: self.parent_id.to_owned(),
post_id: self.post_id, post_id: self.post_id,
attributed_to: format!("{}/{}", iss, user_id), attributed_to: fedi_user_id.to_owned(),
updated: None updated: None
}; };
let inserted_comment = match Comment::create(&conn, &comment_form) { let inserted_comment = match Comment::create(&conn, &comment_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(e) => { 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 inserted_like = match CommentLike::like(&conn, &like_form) {
let comment_view = CommentView::from_new_comment(&inserted_comment); Ok(like) => like,
Err(e) => {
return self.error("Couldn't like comment.");
}
};
let likes: Vec<CommentLike> = 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( let comment_out = serde_json::to_string(
&CreateCommentResponse { &CreateCommentResponse {
@ -721,11 +740,17 @@ impl Perform for CreateComment {
} }
) )
.unwrap(); .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 comment_out
} }