From 44442565aa6d9cba6c597fcce70d939318f071d4 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 7 Sep 2019 08:35:05 -0700 Subject: Running cargo fmt on server code. - Adding a .rustfmt.toml for the 2 space indent. --- server/src/api/comment.rs | 140 +++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 88 deletions(-) (limited to 'server/src/api/comment.rs') diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index 3d18c72a..ec010d2f 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -6,7 +6,7 @@ pub struct CreateComment { parent_id: Option, edit_id: Option, pub post_id: i32, - auth: String + auth: String, } #[derive(Serialize, Deserialize)] @@ -20,20 +20,20 @@ pub struct EditComment { deleted: Option, reason: Option, read: Option, - auth: String + auth: String, } #[derive(Serialize, Deserialize)] pub struct SaveComment { comment_id: i32, save: bool, - auth: String + auth: String, } #[derive(Serialize, Deserialize, Clone)] pub struct CommentResponse { op: String, - pub comment: CommentView + pub comment: CommentView, } #[derive(Serialize, Deserialize)] @@ -41,10 +41,9 @@ pub struct CreateCommentLike { comment_id: i32, pub post_id: i32, score: i16, - auth: String + auth: String, } - impl Perform for Oper { fn perform(&self) -> Result { let data: &CreateComment = &self.data; @@ -52,9 +51,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => { - return Err(APIError::err(&self.op, "not_logged_in"))? - } + Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?, }; let user_id = claims.id; @@ -62,12 +59,12 @@ impl Perform for Oper { // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { - return Err(APIError::err(&self.op, "community_ban"))? + return Err(APIError::err(&self.op, "community_ban"))?; } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(&self.op, "site_ban"))? + return Err(APIError::err(&self.op, "site_ban"))?; } let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -80,14 +77,12 @@ impl Perform for Oper { removed: None, deleted: None, read: None, - updated: None + updated: None, }; let inserted_comment = match Comment::create(&conn, &comment_form) { Ok(comment) => comment, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_create_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_comment"))?, }; // You like your own comment by default @@ -95,24 +90,20 @@ impl Perform for Oper { comment_id: inserted_comment.id, post_id: data.post_id, user_id: user_id, - score: 1 + score: 1, }; let _inserted_like = match CommentLike::like(&conn, &like_form) { Ok(like) => like, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_like_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment"))?, }; let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?; - Ok( - CommentResponse { - op: self.op.to_string(), - comment: comment_view - } - ) + Ok(CommentResponse { + op: self.op.to_string(), + comment: comment_view, + }) } } @@ -123,9 +114,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => { - return Err(APIError::err(&self.op, "not_logged_in"))? - } + Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?, }; let user_id = claims.id; @@ -134,38 +123,29 @@ impl Perform for Oper { // You are allowed to mark the comment as read even if you're banned. if data.read.is_none() { - // Verify its the creator or a mod, or an admin let mut editors: Vec = vec![data.creator_id]; editors.append( - &mut CommunityModeratorView::for_community(&conn, orig_comment.community_id) - ? - .into_iter() - .map(|m| m.user_id) - .collect() - ); - editors.append( - &mut UserView::admins(&conn) - ? - .into_iter() - .map(|a| a.id) - .collect() - ); + &mut CommunityModeratorView::for_community(&conn, orig_comment.community_id)? + .into_iter() + .map(|m| m.user_id) + .collect(), + ); + editors.append(&mut UserView::admins(&conn)?.into_iter().map(|a| a.id).collect()); if !editors.contains(&user_id) { - return Err(APIError::err(&self.op, "no_comment_edit_allowed"))? + return Err(APIError::err(&self.op, "no_comment_edit_allowed"))?; } // Check for a community ban if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() { - return Err(APIError::err(&self.op, "community_ban"))? + return Err(APIError::err(&self.op, "community_ban"))?; } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(&self.op, "site_ban"))? + return Err(APIError::err(&self.op, "site_ban"))?; } - } let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -178,14 +158,16 @@ impl Perform for Oper { removed: data.removed.to_owned(), deleted: data.deleted.to_owned(), read: data.read.to_owned(), - updated: if data.read.is_some() { orig_comment.updated } else {Some(naive_now())} + updated: if data.read.is_some() { + orig_comment.updated + } else { + Some(naive_now()) + }, }; let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) { Ok(comment) => comment, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_update_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?, }; // Mod tables @@ -199,16 +181,12 @@ impl Perform for Oper { ModRemoveComment::create(&conn, &form)?; } - let comment_view = CommentView::read(&conn, data.edit_id, Some(user_id))?; - Ok( - CommentResponse { - op: self.op.to_string(), - comment: comment_view - } - ) - + Ok(CommentResponse { + op: self.op.to_string(), + comment: comment_view, + }) } } @@ -219,9 +197,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => { - return Err(APIError::err(&self.op, "not_logged_in"))? - } + Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?, }; let user_id = claims.id; @@ -234,27 +210,21 @@ impl Perform for Oper { if data.save { match CommentSaved::save(&conn, &comment_saved_form) { Ok(comment) => comment, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_save_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment"))?, }; } else { match CommentSaved::unsave(&conn, &comment_saved_form) { Ok(comment) => comment, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_save_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_comment"))?, }; } let comment_view = CommentView::read(&conn, data.comment_id, Some(user_id))?; - Ok( - CommentResponse { - op: self.op.to_string(), - comment: comment_view - } - ) + Ok(CommentResponse { + op: self.op.to_string(), + comment: comment_view, + }) } } @@ -265,9 +235,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => { - return Err(APIError::err(&self.op, "not_logged_in"))? - } + Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?, }; let user_id = claims.id; @@ -275,19 +243,19 @@ impl Perform for Oper { // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { - return Err(APIError::err(&self.op, "community_ban"))? + return Err(APIError::err(&self.op, "community_ban"))?; } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(&self.op, "site_ban"))? + return Err(APIError::err(&self.op, "site_ban"))?; } let like_form = CommentLikeForm { comment_id: data.comment_id, post_id: data.post_id, user_id: user_id, - score: data.score + score: data.score, }; // Remove any likes first @@ -298,20 +266,16 @@ impl Perform for Oper { if do_add { let _inserted_like = match CommentLike::like(&conn, &like_form) { Ok(like) => like, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_like_comment"))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_comment"))?, }; } // Have to refetch the comment to get the current state let liked_comment = CommentView::read(&conn, data.comment_id, Some(user_id))?; - Ok( - CommentResponse { - op: self.op.to_string(), - comment: liked_comment - } - ) + Ok(CommentResponse { + op: self.op.to_string(), + comment: liked_comment, + }) } } -- cgit v1.2.3