summaryrefslogtreecommitdiffstats
path: root/server/src/api/comment.rs
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2019-09-07 08:35:05 -0700
committerDessalines <tyhou13@gmx.com>2019-09-07 08:35:05 -0700
commit44442565aa6d9cba6c597fcce70d939318f071d4 (patch)
treef5344c3a3ae4fa720e6f0e184ada866337ac3ed7 /server/src/api/comment.rs
parent1954d2b67fa0d6dc1e629eabb5c27e3d1eef37cb (diff)
Running cargo fmt on server code.
- Adding a .rustfmt.toml for the 2 space indent.
Diffstat (limited to 'server/src/api/comment.rs')
-rw-r--r--server/src/api/comment.rs140
1 files changed, 52 insertions, 88 deletions
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<i32>,
edit_id: Option<i32>,
pub post_id: i32,
- auth: String
+ auth: String,
}
#[derive(Serialize, Deserialize)]
@@ -20,20 +20,20 @@ pub struct EditComment {
deleted: Option<bool>,
reason: Option<String>,
read: Option<bool>,
- 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<CommentResponse> for Oper<CreateComment> {
fn perform(&self) -> Result<CommentResponse, Error> {
let data: &CreateComment = &self.data;
@@ -52,9 +51,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
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<CommentResponse> for Oper<CreateComment> {
// 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<CommentResponse> for Oper<CreateComment> {
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<CommentResponse> for Oper<CreateComment> {
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<CommentResponse> for Oper<EditComment> {
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<CommentResponse> for Oper<EditComment> {
// 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<i32> = 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<CommentResponse> for Oper<EditComment> {
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<CommentResponse> for Oper<EditComment> {
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<CommentResponse> for Oper<SaveComment> {
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<CommentResponse> for Oper<SaveComment> {
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<CommentResponse> for Oper<CreateCommentLike> {
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<CommentResponse> for Oper<CreateCommentLike> {
// 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<CommentResponse> for Oper<CreateCommentLike> {
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,
+ })
}
}