diff options
-rw-r--r-- | server/src/api/comment.rs | 42 | ||||
-rw-r--r-- | server/src/api/community.rs | 89 | ||||
-rw-r--r-- | server/src/api/post.rs | 52 | ||||
-rw-r--r-- | server/src/api/site.rs | 43 | ||||
-rw-r--r-- | server/src/api/user.rs | 105 | ||||
-rw-r--r-- | server/src/apub/community.rs | 5 | ||||
-rw-r--r-- | server/src/apub/post.rs | 2 | ||||
-rw-r--r-- | server/src/db/community_view.rs | 2 | ||||
-rw-r--r-- | server/src/db/mod.rs | 8 | ||||
-rw-r--r-- | server/src/db/post_view.rs | 9 | ||||
-rw-r--r-- | server/src/main.rs | 2 | ||||
-rw-r--r-- | server/src/routes/feeds.rs | 5 | ||||
-rw-r--r-- | server/src/routes/nodeinfo.rs | 8 | ||||
-rw-r--r-- | server/src/routes/websocket.rs | 4 | ||||
-rw-r--r-- | server/src/settings.rs | 6 | ||||
-rw-r--r-- | server/src/version.rs | 2 | ||||
-rw-r--r-- | server/src/websocket/server.rs | 46 |
17 files changed, 201 insertions, 229 deletions
diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index 9a057f80..ed658985 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -51,7 +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").into()), }; let user_id = claims.id; @@ -59,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").into()); } // 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").into()); } let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -82,14 +82,14 @@ impl Perform<CommentResponse> for Oper<CreateComment> { 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").into()), }; // Scan the comment for user mentions, add those rows let extracted_usernames = extract_usernames(&comment_form.content); for username_mention in &extracted_usernames { - let mention_user = User_::read_from_name(&conn, username_mention.to_string()); + let mention_user = User_::read_from_name(&conn, (*username_mention).to_string()); if mention_user.is_ok() { let mention_user_id = mention_user?.id; @@ -124,7 +124,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> { 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").into()), }; let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?; @@ -143,7 +143,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").into()), }; let user_id = claims.id; @@ -163,17 +163,17 @@ impl Perform<CommentResponse> for Oper<EditComment> { 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").into()); } // 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").into()); } // 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").into()); } } @@ -196,14 +196,14 @@ impl Perform<CommentResponse> for Oper<EditComment> { 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").into()), }; // Scan the comment for user mentions, add those rows let extracted_usernames = extract_usernames(&comment_form.content); for username_mention in &extracted_usernames { - let mention_user = User_::read_from_name(&conn, username_mention.to_string()); + let mention_user = User_::read_from_name(&conn, (*username_mention).to_string()); if mention_user.is_ok() { let mention_user_id = mention_user?.id; @@ -255,7 +255,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").into()), }; let user_id = claims.id; @@ -268,12 +268,12 @@ 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").into()), }; } 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").into()), }; } @@ -293,7 +293,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").into()), }; let user_id = claims.id; @@ -301,20 +301,20 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> { // Don't do a downvote if site has downvotes disabled if data.score == -1 { let site = SiteView::read(&conn)?; - if site.enable_downvotes == false { - return Err(APIError::err(&self.op, "downvotes_disabled"))?; + if !site.enable_downvotes { + return Err(APIError::err(&self.op, "downvotes_disabled").into()); } } // 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").into()); } // 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").into()); } let like_form = CommentLikeForm { @@ -332,7 +332,7 @@ 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").into()), }; } diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 5c97f088..a1109c03 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -136,21 +136,24 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> { let community_id = match data.id { Some(id) => id, None => { - match Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string())) { + match Community::read_from_name( + &conn, + data.name.to_owned().unwrap_or_else(|| "main".to_string()), + ) { Ok(community) => community.id, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()), } } }; let community_view = match CommunityView::read(&conn, community_id, user_id) { Ok(community) => community, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()), }; let moderators = match CommunityModeratorView::for_community(&conn, community_id) { Ok(moderators) => moderators, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()), }; let site_creator_id = Site::read(&conn, 1)?.creator_id; @@ -176,21 +179,21 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> { 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").into()), }; if has_slurs(&data.name) || has_slurs(&data.title) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let user_id = claims.id; // 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").into()); } // When you create a community, make sure the user becomes a moderator and a follower @@ -208,7 +211,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> { let inserted_community = match Community::create(&conn, &community_form) { Ok(community) => community, - Err(_e) => return Err(APIError::err(&self.op, "community_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_already_exists").into()), }; let community_moderator_form = CommunityModeratorForm { @@ -220,10 +223,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> { match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err( - &self.op, - "community_moderator_already_exists", - ))? + return Err(APIError::err(&self.op, "community_moderator_already_exists").into()) } }; @@ -235,7 +235,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> { let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()), }; let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?; @@ -252,21 +252,21 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> { let data: &EditCommunity = &self.data; if has_slurs(&data.name) || has_slurs(&data.title) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let conn = establish_connection(); 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").into()), }; let user_id = claims.id; // 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").into()); } // Verify its a mod @@ -279,7 +279,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> { ); 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_community_edit_allowed"))?; + return Err(APIError::err(&self.op, "no_community_edit_allowed").into()); } let community_form = CommunityForm { @@ -296,7 +296,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> { let _updated_community = match Community::update(&conn, data.edit_id, &community_form) { Ok(community) => community, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community").into()), }; // Mod tables @@ -351,7 +351,7 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> { let communities = CommunityQueryBuilder::create(&conn) .sort(&sort) - .from_user_id(user_id) + .for_user(user_id) .show_nsfw(show_nsfw) .page(data.page) .limit(data.limit) @@ -372,7 +372,7 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> { 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").into()), }; let user_id = claims.id; @@ -385,12 +385,12 @@ impl Perform<CommunityResponse> for Oper<FollowCommunity> { if data.follow { match CommunityFollower::follow(&conn, &community_follower_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()), }; } else { match CommunityFollower::ignore(&conn, &community_follower_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_follower_already_exists").into()), }; } @@ -410,7 +410,7 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> { 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").into()), }; let user_id = claims.id; @@ -418,7 +418,7 @@ impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> { let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) { Ok(communities) => communities, - Err(_e) => return Err(APIError::err(&self.op, "system_err_login"))?, + Err(_e) => return Err(APIError::err(&self.op, "system_err_login").into()), }; // Return the jwt @@ -436,7 +436,7 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> { 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").into()), }; let user_id = claims.id; @@ -449,12 +449,12 @@ impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> { if data.ban { match CommunityUserBan::ban(&conn, &community_user_ban_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned").into()), }; } else { match CommunityUserBan::unban(&conn, &community_user_ban_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned"))?, + Err(_e) => return Err(APIError::err(&self.op, "community_user_already_banned").into()), }; } @@ -491,7 +491,7 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> { 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").into()), }; let user_id = claims.id; @@ -505,20 +505,14 @@ impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> { match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err( - &self.op, - "community_moderator_already_exists", - ))? + return Err(APIError::err(&self.op, "community_moderator_already_exists").into()) } }; } else { match CommunityModerator::leave(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err( - &self.op, - "community_moderator_already_exists", - ))? + return Err(APIError::err(&self.op, "community_moderator_already_exists").into()) } }; } @@ -548,7 +542,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> { 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").into()), }; let user_id = claims.id; @@ -562,14 +556,8 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> { admins.insert(0, creator_user); // Make sure user is the creator, or an admin - if user_id != read_community.creator_id - && !admins - .iter() - .map(|a| a.id) - .collect::<Vec<i32>>() - .contains(&user_id) - { - return Err(APIError::err(&self.op, "not_an_admin"))?; + if user_id != read_community.creator_id && !admins.iter().map(|a| a.id).any(|x| x == user_id) { + return Err(APIError::err(&self.op, "not_an_admin").into()); } let community_form = CommunityForm { @@ -586,7 +574,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> { let _updated_community = match Community::update(&conn, data.community_id, &community_form) { Ok(community) => community, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_community").into()), }; // You also have to re-do the community_moderator table, reordering it. @@ -610,10 +598,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> { match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err( - &self.op, - "community_moderator_already_exists", - ))? + return Err(APIError::err(&self.op, "community_moderator_already_exists").into()) } }; } @@ -629,12 +614,12 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> { let community_view = match CommunityView::read(&conn, data.community_id, Some(user_id)) { Ok(community) => community, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()), }; let moderators = match CommunityModeratorView::for_community(&conn, data.community_id) { Ok(moderators) => moderators, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_community").into()), }; // Return the jwt diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 4b2395a8..5bc31def 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -93,23 +93,23 @@ impl Perform<PostResponse> for Oper<CreatePost> { 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").into()), }; if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let user_id = claims.id; // Check for a community ban if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { - return Err(APIError::err(&self.op, "community_ban"))?; + return Err(APIError::err(&self.op, "community_ban").into()); } // 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").into()); } let post_form = PostForm { @@ -128,7 +128,7 @@ impl Perform<PostResponse> for Oper<CreatePost> { let inserted_post = match Post::create(&conn, &post_form) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_create_post").into()), }; // They like their own post by default @@ -141,13 +141,13 @@ impl Perform<PostResponse> for Oper<CreatePost> { // Only add the like if the score isnt 0 let _inserted_like = match PostLike::like(&conn, &like_form) { Ok(like) => like, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post").into()), }; // Refetch the view let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()), }; Ok(PostResponse { @@ -175,7 +175,7 @@ impl Perform<GetPostResponse> for Oper<GetPost> { let post_view = match PostView::read(&conn, data.id, user_id) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()), }; let comments = CommentQueryBuilder::create(&conn) @@ -243,7 +243,7 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> { .list() { Ok(posts) => posts, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts").into()), }; Ok(GetPostsResponse { @@ -260,7 +260,7 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> { 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").into()), }; let user_id = claims.id; @@ -268,20 +268,20 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> { // Don't do a downvote if site has downvotes disabled if data.score == -1 { let site = SiteView::read(&conn)?; - if site.enable_downvotes == false { - return Err(APIError::err(&self.op, "downvotes_disabled"))?; + if !site.enable_downvotes { + return Err(APIError::err(&self.op, "downvotes_disabled").into()); } } // 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").into()); } // 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").into()); } let like_form = PostLikeForm { @@ -294,17 +294,17 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> { PostLike::remove(&conn, &like_form)?; // Only add the like if the score isnt 0 - let do_add = &like_form.score != &0 && (&like_form.score == &1 || &like_form.score == &-1); + let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1); if do_add { let _inserted_like = match PostLike::like(&conn, &like_form) { Ok(like) => like, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_like_post").into()), }; } let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post").into()), }; // just output the score @@ -319,14 +319,14 @@ impl Perform<PostResponse> for Oper<EditPost> { fn perform(&self) -> Result<PostResponse, Error> { let data: &EditPost = &self.data; if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let conn = establish_connection(); 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").into()), }; let user_id = claims.id; @@ -341,17 +341,17 @@ impl Perform<PostResponse> for Oper<EditPost> { ); 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_post_edit_allowed"))?; + return Err(APIError::err(&self.op, "no_post_edit_allowed").into()); } // Check for a community ban if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { - return Err(APIError::err(&self.op, "community_ban"))?; + return Err(APIError::err(&self.op, "community_ban").into()); } // 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").into()); } let post_form = PostForm { @@ -370,7 +370,7 @@ impl Perform<PostResponse> for Oper<EditPost> { let _updated_post = match Post::update(&conn, data.edit_id, &post_form) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post").into()), }; // Mod tables @@ -418,7 +418,7 @@ impl Perform<PostResponse> for Oper<SavePost> { 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").into()), }; let user_id = claims.id; @@ -431,12 +431,12 @@ impl Perform<PostResponse> for Oper<SavePost> { if data.save { match PostSaved::save(&conn, &post_saved_form) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post").into()), }; } else { match PostSaved::unsave(&conn, &post_saved_form) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_save_post").into()), }; } diff --git a/server/src/api/site.rs b/server/src/api/site.rs index ec89e46c..58c34e8f 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -160,16 +160,15 @@ impl Perform<GetModlogResponse> for Oper<GetModlog> { )?; // These arrays are only for the full modlog, when a community isn't given - let mut removed_communities = Vec::new(); - let mut banned = Vec::new(); - let mut added = Vec::new(); - - if data.community_id.is_none() { - removed_communities = - ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?; - banned = ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?; - added = ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?; - } + let (removed_communities, banned, added) = if data.community_id.is_none() { + ( + ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?, + ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?, + ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?, + ) + } else { + (Vec::new(), Vec::new(), Vec::new()) + }; // Return the jwt Ok(GetModlogResponse { @@ -194,20 +193,20 @@ impl Perform<SiteResponse> for Oper<CreateSite> { 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").into()), }; if has_slurs(&data.name) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let user_id = claims.id; // Make sure user is an admin if !UserView::read(&conn, user_id)?.admin { - return Err(APIError::err(&self.op, "not_an_admin"))?; + return Err(APIError::err(&self.op, "not_an_admin").into()); } let site_form = SiteForm { @@ -222,7 +221,7 @@ impl Perform<SiteResponse> for Oper<CreateSite> { match Site::create(&conn, &site_form) { Ok(site) => site, - Err(_e) => return Err(APIError::err(&self.op, "site_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "site_already_exists").into()), }; let site_view = SiteView::read(&conn)?; @@ -241,20 +240,20 @@ impl Perform<SiteResponse> for Oper<EditSite> { 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").into()), }; if has_slurs(&data.name) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } let user_id = claims.id; // Make sure user is an admin - if UserView::read(&conn, user_id)?.admin == false { - return Err(APIError::err(&self.op, "not_an_admin"))?; + if !UserView::read(&conn, user_id)?.admin { + return Err(APIError::err(&self.op, "not_an_admin").into()); } let found_site = Site::read(&conn, 1)?; @@ -271,7 +270,7 @@ impl Perform<SiteResponse> for Oper<EditSite> { match Site::update(&conn, 1, &site_form) { Ok(site) => site, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site").into()), }; let site_view = SiteView::read(&conn)?; @@ -426,7 +425,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> { 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").into()), }; let user_id = claims.id; @@ -435,7 +434,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> { // Make sure user is the creator if read_site.creator_id != user_id { - return Err(APIError::err(&self.op, "not_an_admin"))?; + return Err(APIError::err(&self.op, "not_an_admin").into()); } let site_form = SiteForm { @@ -450,7 +449,7 @@ impl Perform<GetSiteResponse> for Oper<TransferSite> { match Site::update(&conn, 1, &site_form) { Ok(site) => site, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site").into()), }; // Mod tables diff --git a/server/src/api/user.rs b/server/src/api/user.rs index c074228f..912587da 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -172,18 +172,13 @@ impl Perform<LoginResponse> for Oper<Login> { // Fetch that username / email let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) { |