From: Felix Ableitner Date: Thu, 2 Jan 2020 11:30:00 +0000 (+0100) Subject: Apply changes suggested by cargo clippy (fixes #395) X-Git-Url: http://these/git/%22https:/nerdica.net/photo/contact/80/%7Bicon?a=commitdiff_plain;h=d08e09fbdc9d0e50e4b81410ae513b034170c36b;p=lemmy.git Apply changes suggested by cargo clippy (fixes #395) --- 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 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").into()), }; let user_id = claims.id; @@ -59,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").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 for Oper { 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 for Oper { 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 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").into()), }; let user_id = claims.id; @@ -163,17 +163,17 @@ impl Perform for Oper { 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 for Oper { 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 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").into()), }; let user_id = claims.id; @@ -268,12 +268,12 @@ 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").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 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").into()), }; let user_id = claims.id; @@ -301,20 +301,20 @@ impl Perform for Oper { // 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 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").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 for Oper { 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 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").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 for Oper { 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 for Oper { 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 for Oper { 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 for Oper { 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 for Oper { ); 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 for Oper { 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 for Oper { 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 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").into()), }; let user_id = claims.id; @@ -385,12 +385,12 @@ impl Perform for Oper { 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 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").into()), }; let user_id = claims.id; @@ -418,7 +418,7 @@ impl Perform for Oper { let communities: Vec = 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 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").into()), }; let user_id = claims.id; @@ -449,12 +449,12 @@ impl Perform for Oper { 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 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").into()), }; let user_id = claims.id; @@ -505,20 +505,14 @@ impl Perform for Oper { 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 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").into()), }; let user_id = claims.id; @@ -562,14 +556,8 @@ impl Perform for Oper { 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::>() - .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 for Oper { 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 for Oper { 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 for Oper { 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 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").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 for Oper { 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 for Oper { // 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 for Oper { 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 for Oper { .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 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").into()), }; let user_id = claims.id; @@ -268,20 +268,20 @@ impl Perform for Oper { // 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 for Oper { 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 for Oper { fn perform(&self) -> Result { 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 for Oper { ); 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 for Oper { 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 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").into()), }; let user_id = claims.id; @@ -431,12 +431,12 @@ impl Perform for Oper { 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 for Oper { )?; // 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 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").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 for Oper { 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 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").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 for Oper { 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 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").into()), }; let user_id = claims.id; @@ -435,7 +434,7 @@ impl Perform for Oper { // 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 for Oper { 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 for Oper { // Fetch that username / email let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) { Ok(user) => user, - Err(_e) => { - return Err(APIError::err( - &self.op, - "couldnt_find_that_username_or_email", - ))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()), }; // Verify the password let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false); if !valid { - return Err(APIError::err(&self.op, "password_incorrect"))?; + return Err(APIError::err(&self.op, "password_incorrect").into()); } // Return the jwt @@ -202,22 +197,22 @@ impl Perform for Oper { // Make sure site has open registration if let Ok(site) = SiteView::read(&conn) { if !site.open_registration { - return Err(APIError::err(&self.op, "registration_closed"))?; + return Err(APIError::err(&self.op, "registration_closed").into()); } } // Make sure passwords match - if &data.password != &data.password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match"))?; + if data.password != data.password_verify { + return Err(APIError::err(&self.op, "passwords_dont_match").into()); } if has_slurs(&data.username) { - return Err(APIError::err(&self.op, "no_slurs"))?; + return Err(APIError::err(&self.op, "no_slurs").into()); } // Make sure there are no admins - if data.admin && UserView::admins(&conn)?.len() > 0 { - return Err(APIError::err(&self.op, "admin_already_created"))?; + if data.admin && !UserView::admins(&conn)?.is_empty() { + return Err(APIError::err(&self.op, "admin_already_created").into()); } // Register the new user @@ -241,7 +236,7 @@ impl Perform for Oper { // Create the user let inserted_user = match User_::register(&conn, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "user_already_exists"))?, + Err(_e) => return Err(APIError::err(&self.op, "user_already_exists").into()), }; // Create the main community if it doesn't exist @@ -272,7 +267,7 @@ impl Perform for Oper { 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()), }; // If its an admin, add them as a mod and follower to main @@ -286,10 +281,7 @@ impl Perform for Oper { 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()) } }; } @@ -309,7 +301,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").into()), }; let user_id = claims.id; @@ -327,7 +319,7 @@ impl Perform for Oper { Some(new_password_verify) => { // Make sure passwords match if new_password != new_password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match"))?; + return Err(APIError::err(&self.op, "passwords_dont_match").into()); } // Check the old password @@ -336,14 +328,14 @@ impl Perform for Oper { let valid: bool = verify(old_password, &read_user.password_encrypted).unwrap_or(false); if !valid { - return Err(APIError::err(&self.op, "password_incorrect"))?; + return Err(APIError::err(&self.op, "password_incorrect").into()); } User_::update_password(&conn, user_id, &new_password)?.password_encrypted } - None => return Err(APIError::err(&self.op, "password_incorrect"))?, + None => return Err(APIError::err(&self.op, "password_incorrect").into()), } } - None => return Err(APIError::err(&self.op, "passwords_dont_match"))?, + None => return Err(APIError::err(&self.op, "passwords_dont_match").into()), } } None => read_user.password_encrypted, @@ -368,7 +360,7 @@ impl Perform for Oper { let updated_user = match User_::update(&conn, user_id, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), }; // Return the jwt @@ -409,14 +401,14 @@ impl Perform for Oper { None => { match User_::read_from_name( &conn, - data.username.to_owned().unwrap_or("admin".to_string()), + data + .username + .to_owned() + .unwrap_or_else(|| "admin".to_string()), ) { Ok(user) => user.id, Err(_e) => { - return Err(APIError::err( - &self.op, - "couldnt_find_that_username_or_email", - ))? + return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()) } } } @@ -478,14 +470,14 @@ 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").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 read_user = User_::read(&conn, data.user_id)?; @@ -509,7 +501,7 @@ impl Perform for Oper { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), }; // Mod tables @@ -541,14 +533,14 @@ 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").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 read_user = User_::read(&conn, data.user_id)?; @@ -572,7 +564,7 @@ impl Perform for Oper { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), }; // Mod tables @@ -608,7 +600,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").into()), }; let user_id = claims.id; @@ -636,7 +628,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").into()), }; let user_id = claims.id; @@ -664,7 +656,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").into()), }; let user_id = claims.id; @@ -680,7 +672,7 @@ impl Perform for Oper { let _updated_user_mention = match UserMention::update(&conn, user_mention.id, &user_mention_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()), }; let user_mention_view = UserMentionView::read(&conn, user_mention.id, user_id)?; @@ -699,7 +691,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").into()), }; let user_id = claims.id; @@ -724,7 +716,7 @@ impl Perform for Oper { let _updated_comment = match Comment::update(&conn, reply.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()), }; } @@ -745,7 +737,7 @@ impl Perform for Oper { let _updated_mention = match UserMention::update(&conn, mention.user_mention_id, &mention_form) { Ok(mention) => mention, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()), }; } @@ -763,7 +755,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").into()), }; let user_id = claims.id; @@ -773,7 +765,7 @@ impl Perform for Oper { // Verify the password let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false); if !valid { - return Err(APIError::err(&self.op, "password_incorrect"))?; + return Err(APIError::err(&self.op, "password_incorrect").into()); } // Comments @@ -796,7 +788,7 @@ impl Perform for Oper { let _updated_comment = match Comment::update(&conn, comment.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()), }; } @@ -824,7 +816,7 @@ impl Perform for Oper { let _updated_post = match Post::update(&conn, post.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()), }; } @@ -843,12 +835,7 @@ impl Perform for Oper { // Fetch that email let user: User_ = match User_::find_by_email(&conn, &data.email) { Ok(user) => user, - Err(_e) => { - return Err(APIError::err( - &self.op, - "couldnt_find_that_username_or_email", - ))? - } + Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()), }; // Generate a random token @@ -865,7 +852,7 @@ impl Perform for Oper { let html = &format!("

Password Reset Request for {}


Click here to reset your password", user.name, hostname, &token); match send_email(subject, user_email, &user.name, html) { Ok(_o) => _o, - Err(_e) => return Err(APIError::err(&self.op, &_e.to_string()))?, + Err(_e) => return Err(APIError::err(&self.op, &_e).into()), }; Ok(PasswordResetResponse { @@ -883,14 +870,14 @@ impl Perform for Oper { let user_id = PasswordResetRequest::read_from_token(&conn, &data.token)?.user_id; // Make sure passwords match - if &data.password != &data.password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match"))?; + if data.password != data.password_verify { + return Err(APIError::err(&self.op, "passwords_dont_match").into()); } // Update the user with the new password let updated_user = match User_::update_password(&conn, user_id, &data.password) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user"))?, + Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), }; // Return the jwt diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 3de68b96..504e1790 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -60,10 +60,7 @@ impl Community { let mut collection = UnorderedCollection::default(); collection.object_props.set_context_object(context()).ok(); - collection - .object_props - .set_id_string(base_url.to_string()) - .ok(); + collection.object_props.set_id_string(base_url).ok(); let connection = establish_connection(); //As we are an object, we validated that the community id was valid diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 19163657..ebb17129 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -9,7 +9,7 @@ impl Post { let mut page = Page::default(); page.object_props.set_context_object(context()).ok(); - page.object_props.set_id_string(base_url.to_string()).ok(); + page.object_props.set_id_string(base_url).ok(); page.object_props.set_name_string(self.name.to_owned()).ok(); if let Some(body) = &self.body { diff --git a/server/src/db/community_view.rs b/server/src/db/community_view.rs index e57fd759..2d49cd44 100644 --- a/server/src/db/community_view.rs +++ b/server/src/db/community_view.rs @@ -124,7 +124,7 @@ impl<'a> CommunityQueryBuilder<'a> { self } - pub fn from_user_id>(mut self, from_user_id: T) -> Self { + pub fn for_user>(mut self, from_user_id: T) -> Self { self.from_user_id = from_user_id.get_optional(); self } diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 7824580d..fe6cb3ce 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -101,13 +101,13 @@ pub trait MaybeOptional { impl MaybeOptional for T { fn get_optional(self) -> Option { - return Some(self); + Some(self) } } impl MaybeOptional for Option { fn get_optional(self) -> Option { - return self; + self } } @@ -118,12 +118,12 @@ lazy_static! { Pool::builder() .max_size(Settings::get().database.pool_size) .build(manager) - .expect(&format!("Error connecting to {}", db_url)) + .unwrap_or_else(|_| panic!("Error connecting to {}", db_url)) }; } pub fn establish_connection() -> PooledConnection> { - return PG_POOL.get().unwrap(); + PG_POOL.get().unwrap() } #[derive(EnumString, ToString, Debug, Serialize, Deserialize)] diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index d05eecca..22e2eb14 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -189,12 +189,9 @@ impl<'a> PostQueryBuilder<'a> { let mut query = self.query; - match self.listing_type { - ListingType::Subscribed => { - query = query.filter(subscribed.eq(true)); - } - _ => {} - }; + if let ListingType::Subscribed = self.listing_type { + query = query.filter(subscribed.eq(true)); + } query = match self.sort { SortType::Hot => query diff --git a/server/src/main.rs b/server/src/main.rs index 09ba8c5f..10a7a855 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -10,7 +10,7 @@ use lemmy_server::settings::Settings; embed_migrations!(); fn main() { - let _ = env_logger::init(); + env_logger::init(); let sys = actix::System::new("lemmy"); // Run the migrations from code diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index 55b457c7..0b2ccac1 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -85,7 +85,10 @@ fn get_feed(path: web::Path<(String, String)>, info: web::Query) -> Http } fn get_sort_type(info: web::Query) -> Result { - let sort_query = info.sort.to_owned().unwrap_or(SortType::Hot.to_string()); + let sort_query = info + .sort + .to_owned() + .unwrap_or_else(|| SortType::Hot.to_string()); SortType::from_str(&sort_query) } diff --git a/server/src/routes/nodeinfo.rs b/server/src/routes/nodeinfo.rs index cbc0fb0f..24659608 100644 --- a/server/src/routes/nodeinfo.rs +++ b/server/src/routes/nodeinfo.rs @@ -21,9 +21,9 @@ pub fn node_info_well_known() -> HttpResponse { } }); - return HttpResponse::Ok() + HttpResponse::Ok() .content_type("application/json") - .body(json.to_string()); + .body(json.to_string()) } fn node_info() -> HttpResponse { @@ -53,7 +53,7 @@ fn node_info() -> HttpResponse { "openRegistrations": site_view.open_registration, } }); - return HttpResponse::Ok() + HttpResponse::Ok() .content_type("application/json") - .body(json.to_string()); + .body(json.to_string()) } diff --git a/server/src/routes/websocket.rs b/server/src/routes/websocket.rs index 3af229bf..8ae3552c 100644 --- a/server/src/routes/websocket.rs +++ b/server/src/routes/websocket.rs @@ -9,7 +9,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { // Start chat server actor in separate thread let server = ChatServer::default().start(); cfg - .data(server.clone()) + .data(server) .service(web::resource("/api/v1/ws").to(chat_route)); } @@ -33,7 +33,7 @@ fn chat_route( .connection_info() .remote() .unwrap_or("127.0.0.1:12345") - .split(":") + .split(':') .next() .unwrap_or("127.0.0.1") .to_string(), diff --git a/server/src/settings.rs b/server/src/settings.rs index 6cb4de0b..f4bb2a42 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -50,10 +50,10 @@ pub struct Database { lazy_static! { static ref SETTINGS: Settings = { - return match Settings::init() { + match Settings::init() { Ok(c) => c, Err(e) => panic!("{}", e), - }; + } }; } @@ -76,7 +76,7 @@ impl Settings { // https://github.com/mehcode/config-rs/issues/73 s.merge(Environment::with_prefix("LEMMY").separator("__"))?; - return s.try_into(); + s.try_into() } /// Returns the config as a struct. diff --git a/server/src/version.rs b/server/src/version.rs index 6397f1a8..eeb4588a 100644 --- a/server/src/version.rs +++ b/server/src/version.rs @@ -1 +1 @@ -pub const VERSION: &'static str = "v0.5.12"; +pub const VERSION: &str = "v0.5.12"; diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 56fff275..4fcbc241 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -91,7 +91,7 @@ impl Default for ChatServer { ChatServer { sessions: HashMap::new(), rate_limits: HashMap::new(), - rooms: rooms, + rooms, rng: rand::thread_rng(), } } @@ -99,8 +99,8 @@ impl Default for ChatServer { impl ChatServer { /// Send message to all users in the room - fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) { - if let Some(sessions) = self.rooms.get(room) { + fn send_room_message(&self, room: i32, message: &str, skip_id: usize) { + if let Some(sessions) = self.rooms.get(&room) { for id in sessions { if *id != skip_id { if let Some(info) = self.sessions.get(id) { @@ -113,7 +113,7 @@ impl ChatServer { fn join_room(&mut self, room_id: i32, id: usize) { // remove session from all rooms - for (_n, sessions) in &mut self.rooms { + for sessions in self.rooms.values_mut() { sessions.remove(&id); } @@ -122,12 +122,12 @@ impl ChatServer { self.rooms.insert(room_id, HashSet::new()); } - &self.rooms.get_mut(&room_id).unwrap().insert(id); + self.rooms.get_mut(&room_id).unwrap().insert(id); } fn send_community_message( &self, - community_id: &i32, + community_id: i32, message: &str, skip_id: usize, ) -> Result<(), Error> { @@ -138,12 +138,12 @@ impl ChatServer { let posts = PostQueryBuilder::create(&conn) .listing_type(ListingType::Community) .sort(&SortType::New) - .for_community_id(*community_id) + .for_community_id(community_id) .limit(9999) .list()?; for post in posts { - self.send_room_message(&post.id, message, skip_id); + self.send_room_message(post.id, message, skip_id); } Ok(()) @@ -173,6 +173,7 @@ impl ChatServer { ) } + #[allow(clippy::float_cmp)] fn check_rate_limit_full(&mut self, id: usize, rate: i32, per: i32) -> Result<(), Error> { if let Some(info) = self.sessions.get(&id) { if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) { @@ -194,10 +195,13 @@ impl ChatServer { "Rate limited IP: {}, time_passed: {}, allowance: {}", &info.ip, time_passed, rate_limit.allowance ); - Err(APIError { - op: "Rate Limit".to_string(), - message: format!("Too many requests. {} per {} seconds", rate, per), - })? + Err( + APIError { + op: "Rate Limit".to_string(), + message: format!("Too many requests. {} per {} seconds", rate, per), + } + .into(), + ) } else { rate_limit.allowance -= 1.0; Ok(()) @@ -264,7 +268,7 @@ impl Handler for ChatServer { // remove address if self.sessions.remove(&msg.id).is_some() { // remove session from all rooms - for (_id, sessions) in &mut self.rooms { + for sessions in self.rooms.values_mut() { if sessions.remove(&msg.id) { // rooms.push(*id); } @@ -292,7 +296,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result Result { @@ -392,7 +396,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -400,7 +404,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -437,7 +441,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -454,7 +458,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -465,7 +469,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -482,7 +486,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result {