From 65a11a72391113a77c61efe33b0188e685e14976 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 16 Apr 2021 13:10:43 +0000 Subject: [PATCH] Use .map_err in api code (fixes #1573) (#1575) * Use .map_err in api code (fixes #1573) * forgot some --- crates/api/src/comment.rs | 7 ++-- crates/api/src/comment_report.rs | 7 ++-- crates/api/src/community.rs | 14 +++----- crates/api/src/local_user.rs | 34 ++++++------------- crates/api/src/post.rs | 7 ++-- crates/api/src/post_report.rs | 7 ++-- crates/api/src/private_message.rs | 7 ++-- crates/api/src/site.rs | 6 ++-- crates/api_common/src/lib.rs | 21 +++++------- crates/api_crud/src/comment/create.rs | 29 +++++----------- crates/api_crud/src/comment/delete.rs | 14 +++----- crates/api_crud/src/comment/read.rs | 7 ++-- crates/api_crud/src/comment/update.rs | 7 ++-- crates/api_crud/src/community/create.rs | 7 ++-- crates/api_crud/src/community/delete.rs | 14 +++----- crates/api_crud/src/community/read.rs | 21 ++++-------- crates/api_crud/src/post/create.rs | 14 +++----- crates/api_crud/src/post/read.rs | 21 ++++-------- crates/api_crud/src/private_message/create.rs | 7 ++-- crates/api_crud/src/private_message/delete.rs | 7 ++-- crates/api_crud/src/private_message/update.rs | 7 ++-- crates/api_crud/src/user/create.rs | 9 ++--- crates/api_crud/src/user/read.rs | 7 ++-- 23 files changed, 85 insertions(+), 196 deletions(-) diff --git a/crates/api/src/comment.rs b/crates/api/src/comment.rs index 2237204f4..dd373cb10 100644 --- a/crates/api/src/comment.rs +++ b/crates/api/src/comment.rs @@ -46,14 +46,11 @@ impl Perform for MarkCommentAsRead { // Do the mark as read let read = data.read; - match blocking(context.pool(), move |conn| { + blocking(context.pool(), move |conn| { Comment::update_read(conn, comment_id, read) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_comment"))?; // Refetch it let comment_id = data.comment_id; diff --git a/crates/api/src/comment_report.rs b/crates/api/src/comment_report.rs index 9cd504b5e..5cdf697c2 100644 --- a/crates/api/src/comment_report.rs +++ b/crates/api/src/comment_report.rs @@ -59,14 +59,11 @@ impl Perform for CreateCommentReport { reason: data.reason.to_owned(), }; - let report = match blocking(context.pool(), move |conn| { + let report = blocking(context.pool(), move |conn| { CommentReport::report(conn, &report_form) }) .await? - { - Ok(report) => report, - Err(_e) => return Err(ApiError::err("couldnt_create_report").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_report"))?; let res = CreateCommentReportResponse { success: true }; diff --git a/crates/api/src/community.rs b/crates/api/src/community.rs index c451cbef7..fec943343 100644 --- a/crates/api/src/community.rs +++ b/crates/api/src/community.rs @@ -414,24 +414,18 @@ impl Perform for TransferCommunity { let community_id = data.community_id; let person_id = local_user_view.person.id; - let community_view = match blocking(context.pool(), move |conn| { + let community_view = blocking(context.pool(), move |conn| { CommunityView::read(conn, community_id, Some(person_id)) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_community"))?; let community_id = data.community_id; - let moderators = match blocking(context.pool(), move |conn| { + let moderators = blocking(context.pool(), move |conn| { CommunityModeratorView::for_community(conn, community_id) }) .await? - { - Ok(moderators) => moderators, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_community"))?; // Return the jwt Ok(GetCommunityResponse { diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index 0acb29f15..241e3405f 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -83,14 +83,11 @@ impl Perform for Login { // Fetch that username / email let username_or_email = data.username_or_email.clone(); - let local_user_view = match blocking(context.pool(), move |conn| { + let local_user_view = blocking(context.pool(), move |conn| { LocalUserView::find_by_email_or_name(conn, &username_or_email) }) .await? - { - Ok(uv) => uv, - Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?; // Verify the password let valid: bool = verify( @@ -629,14 +626,11 @@ impl Perform for PasswordReset { // Fetch that email let email = data.email.clone(); - let local_user_view = match blocking(context.pool(), move |conn| { + let local_user_view = blocking(context.pool(), move |conn| { LocalUserView::find_by_email(conn, &email) }) .await? - { - Ok(lu) => lu, - Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?; // Generate a random token let token = generate_random_string(); @@ -655,10 +649,8 @@ impl Perform for PasswordReset { let subject = &format!("Password reset for {}", local_user_view.person.name); let hostname = &Settings::get().get_protocol_and_hostname(); let html = &format!("

Password Reset Request for {}


Click here to reset your password", local_user_view.person.name, hostname, &token); - match send_email(subject, email, &local_user_view.person.name, html) { - Ok(_o) => _o, - Err(_e) => return Err(ApiError::err(&_e).into()), - }; + send_email(subject, email, &local_user_view.person.name, html) + .map_err(|e| ApiError::err(&e))?; Ok(PasswordResetResponse {}) } @@ -691,14 +683,11 @@ impl Perform for PasswordChange { // Update the user with the new password let password = data.password.clone(); - let updated_local_user = match blocking(context.pool(), move |conn| { + let updated_local_user = blocking(context.pool(), move |conn| { LocalUser::update_password(conn, local_user_id, &password) }) .await? - { - Ok(u) => u, - Err(_e) => return Err(ApiError::err("couldnt_update_user").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_user"))?; // Return the jwt Ok(LoginResponse { @@ -776,14 +765,11 @@ impl Perform for GetFollowedCommunities { let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; let person_id = local_user_view.person.id; - let communities = match blocking(context.pool(), move |conn| { + let communities = blocking(context.pool(), move |conn| { CommunityFollowerView::for_person(conn, person_id) }) .await? - { - Ok(communities) => communities, - _ => return Err(ApiError::err("system_err_login").into()), - }; + .map_err(|_| ApiError::err("system_err_login"))?; // Return the jwt Ok(GetFollowedCommunitiesResponse { communities }) diff --git a/crates/api/src/post.rs b/crates/api/src/post.rs index be39cf53c..34c9c4116 100644 --- a/crates/api/src/post.rs +++ b/crates/api/src/post.rs @@ -71,14 +71,11 @@ impl Perform for CreatePostLike { let post_id = data.post_id; let person_id = local_user_view.person.id; - let post_view = match blocking(context.pool(), move |conn| { + let post_view = blocking(context.pool(), move |conn| { PostView::read(conn, post_id, Some(person_id)) }) .await? - { - Ok(post) => post, - Err(_e) => return Err(ApiError::err("couldnt_find_post").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_post"))?; let res = PostResponse { post_view }; diff --git a/crates/api/src/post_report.rs b/crates/api/src/post_report.rs index c81ee2aa2..b2ebbaf24 100644 --- a/crates/api/src/post_report.rs +++ b/crates/api/src/post_report.rs @@ -68,14 +68,11 @@ impl Perform for CreatePostReport { reason: data.reason.to_owned(), }; - let report = match blocking(context.pool(), move |conn| { + let report = blocking(context.pool(), move |conn| { PostReport::report(conn, &report_form) }) .await? - { - Ok(report) => report, - Err(_e) => return Err(ApiError::err("couldnt_create_report").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_report"))?; let res = CreatePostReportResponse { success: true }; diff --git a/crates/api/src/private_message.rs b/crates/api/src/private_message.rs index 5420084d6..d0163dcf1 100644 --- a/crates/api/src/private_message.rs +++ b/crates/api/src/private_message.rs @@ -36,14 +36,11 @@ impl Perform for MarkPrivateMessageAsRead { // Doing the update let private_message_id = data.private_message_id; let read = data.read; - match blocking(context.pool(), move |conn| { + blocking(context.pool(), move |conn| { PrivateMessage::update_read(conn, private_message_id, read) }) .await? - { - Ok(private_message) => private_message, - Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_private_message"))?; // No need to send an apub update let private_message_id = data.private_message_id; diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 91402da1a..cc61b5206 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -388,10 +388,8 @@ impl Perform for SaveSiteConfig { is_admin(&local_user_view)?; // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem - let config_hjson = match Settings::save_config_file(&data.config_hjson) { - Ok(config_hjson) => config_hjson, - Err(_e) => return Err(ApiError::err("couldnt_update_site").into()), - }; + let config_hjson = Settings::save_config_file(&data.config_hjson) + .map_err(|_| ApiError::err("couldnt_update_site"))?; Ok(GetSiteConfigResponse { config_hjson }) } diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index 2f820f1a2..dca09b97e 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -237,20 +237,18 @@ pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> { } pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result { - match blocking(pool, move |conn| Post::read(conn, post_id)).await? { - Ok(post) => Ok(post), - Err(_e) => Err(ApiError::err("couldnt_find_post").into()), - } + blocking(pool, move |conn| Post::read(conn, post_id)) + .await? + .map_err(|_| ApiError::err("couldnt_find_post").into()) } pub async fn get_local_user_view_from_jwt( jwt: &str, pool: &DbPool, ) -> Result { - let claims = match Claims::decode(&jwt) { - Ok(claims) => claims.claims, - Err(_e) => return Err(ApiError::err("not_logged_in").into()), - }; + let claims = Claims::decode(&jwt) + .map_err(|_| ApiError::err("not_logged_in"))? + .claims; let local_user_id = LocalUserId(claims.sub); let local_user_view = blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??; @@ -291,10 +289,9 @@ pub async fn get_local_user_settings_view_from_jwt( jwt: &str, pool: &DbPool, ) -> Result { - let claims = match Claims::decode(&jwt) { - Ok(claims) => claims.claims, - Err(_e) => return Err(ApiError::err("not_logged_in").into()), - }; + let claims = Claims::decode(&jwt) + .map_err(|_| ApiError::err("not_logged_in"))? + .claims; let local_user_id = LocalUserId(claims.sub); let local_user_view = blocking(pool, move |conn| { LocalUserSettingsView::read(conn, local_user_id) diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 0cdde729f..4f1037c3f 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -48,11 +48,9 @@ impl PerformCrud for CreateComment { // If there's a parent_id, check to make sure that comment is in that post if let Some(parent_id) = data.parent_id { // Make sure the parent comment exists - let parent = - match blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)).await? { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()), - }; + let parent = blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)) + .await? + .map_err(|_| ApiError::err("couldnt_create_comment"))?; if parent.post_id != post_id { return Err(ApiError::err("couldnt_create_comment").into()); } @@ -68,28 +66,22 @@ impl PerformCrud for CreateComment { // Create the comment let comment_form2 = comment_form.clone(); - let inserted_comment = match blocking(context.pool(), move |conn| { + let inserted_comment = blocking(context.pool(), move |conn| { Comment::create(&conn, &comment_form2) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_comment"))?; // Necessary to update the ap_id let inserted_comment_id = inserted_comment.id; let updated_comment: Comment = - match blocking(context.pool(), move |conn| -> Result { + blocking(context.pool(), move |conn| -> Result { let apub_id = generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?; Ok(Comment::update_ap_id(&conn, inserted_comment_id, apub_id)?) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_comment"))?; updated_comment .send_create(&local_user_view.person, context) @@ -134,14 +126,11 @@ impl PerformCrud for CreateComment { // If its a comment to yourself, mark it as read let comment_id = comment_view.comment.id; if local_user_view.person.id == comment_view.get_recipient_id() { - match blocking(context.pool(), move |conn| { + blocking(context.pool(), move |conn| { Comment::update_read(conn, comment_id, true) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_comment"))?; comment_view.comment.read = true; } diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index bbac9c84b..8f9280965 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -47,14 +47,11 @@ impl PerformCrud for DeleteComment { // Do the delete let deleted = data.deleted; - let updated_comment = match blocking(context.pool(), move |conn| { + let updated_comment = blocking(context.pool(), move |conn| { Comment::update_deleted(conn, comment_id, deleted) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_comment"))?; // Send the apub message if deleted { @@ -139,14 +136,11 @@ impl PerformCrud for RemoveComment { // Do the remove let removed = data.removed; - let updated_comment = match blocking(context.pool(), move |conn| { + let updated_comment = blocking(context.pool(), move |conn| { Comment::update_removed(conn, comment_id, removed) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_comment"))?; // Mod tables let form = ModRemoveCommentForm { diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index a7ad3aca2..17f3a57d1 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -40,11 +40,8 @@ impl PerformCrud for GetComments { .limit(limit) .list() }) - .await?; - let comments = match comments { - Ok(comments) => comments, - Err(_) => return Err(ApiError::err("couldnt_get_comments").into()), - }; + .await? + .map_err(|_| ApiError::err("couldnt_get_comments"))?; Ok(GetCommentsResponse { comments }) } diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index fb6bdaf11..e26a9884e 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -52,14 +52,11 @@ impl PerformCrud for EditComment { // Do the update let content_slurs_removed = remove_slurs(&data.content.to_owned()); let comment_id = data.comment_id; - let updated_comment = match blocking(context.pool(), move |conn| { + let updated_comment = blocking(context.pool(), move |conn| { Comment::update_content(conn, comment_id, &content_slurs_removed) }) .await? - { - Ok(comment) => comment, - Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_comment"))?; // Send the apub update updated_comment diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index 393b3746a..d733966ca 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -85,14 +85,11 @@ impl PerformCrud for CreateCommunity { ..CommunityForm::default() }; - let inserted_community = match blocking(context.pool(), move |conn| { + let inserted_community = blocking(context.pool(), move |conn| { Community::create(conn, &community_form) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("community_already_exists").into()), - }; + .map_err(|_| ApiError::err("community_already_exists"))?; // The community creator becomes a moderator let community_moderator_form = CommunityModeratorForm { diff --git a/crates/api_crud/src/community/delete.rs b/crates/api_crud/src/community/delete.rs index fb01b08ec..9511a2caa 100644 --- a/crates/api_crud/src/community/delete.rs +++ b/crates/api_crud/src/community/delete.rs @@ -41,14 +41,11 @@ impl PerformCrud for DeleteCommunity { // Do the delete let community_id = data.community_id; let deleted = data.deleted; - let updated_community = match blocking(context.pool(), move |conn| { + let updated_community = blocking(context.pool(), move |conn| { Community::update_deleted(conn, community_id, deleted) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_update_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_community"))?; // Send apub messages if deleted { @@ -99,14 +96,11 @@ impl PerformCrud for RemoveCommunity { // Do the remove let community_id = data.community_id; let removed = data.removed; - let updated_community = match blocking(context.pool(), move |conn| { + let updated_community = blocking(context.pool(), move |conn| { Community::update_removed(conn, community_id, removed) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_update_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_community"))?; // Mod tables let expires = data.expires.map(naive_from_unix); diff --git a/crates/api_crud/src/community/read.rs b/crates/api_crud/src/community/read.rs index 08982aa15..422161ae1 100644 --- a/crates/api_crud/src/community/read.rs +++ b/crates/api_crud/src/community/read.rs @@ -28,35 +28,26 @@ impl PerformCrud for GetCommunity { Some(id) => id, None => { let name = data.name.to_owned().unwrap_or_else(|| "main".to_string()); - match blocking(context.pool(), move |conn| { + blocking(context.pool(), move |conn| { Community::read_from_name(conn, &name) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - } + .map_err(|_| ApiError::err("couldnt_find_community"))? .id } }; - let community_view = match blocking(context.pool(), move |conn| { + let community_view = blocking(context.pool(), move |conn| { CommunityView::read(conn, community_id, person_id) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_community"))?; - let moderators: Vec = match blocking(context.pool(), move |conn| { + let moderators: Vec = blocking(context.pool(), move |conn| { CommunityModeratorView::for_community(conn, community_id) }) .await? - { - Ok(moderators) => moderators, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_community"))?; let online = context .chat_server() diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 2fe86414f..3a2311a76 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -69,15 +69,12 @@ impl PerformCrud for CreatePost { }; let inserted_post_id = inserted_post.id; - let updated_post = match blocking(context.pool(), move |conn| -> Result { + let updated_post = blocking(context.pool(), move |conn| -> Result { let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?; Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?) }) .await? - { - Ok(post) => post, - Err(_e) => return Err(ApiError::err("couldnt_create_post").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_post"))?; updated_post .send_create(&local_user_view.person, context) @@ -101,14 +98,11 @@ impl PerformCrud for CreatePost { // Refetch the view let inserted_post_id = inserted_post.id; - let post_view = match blocking(context.pool(), move |conn| { + let post_view = blocking(context.pool(), move |conn| { PostView::read(conn, inserted_post_id, Some(local_user_view.person.id)) }) .await? - { - Ok(post) => post, - Err(_e) => return Err(ApiError::err("couldnt_find_post").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_post"))?; let res = PostResponse { post_view }; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index d8897317a..323889101 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -28,14 +28,11 @@ impl PerformCrud for GetPost { let person_id = local_user_view.map(|u| u.person.id); let id = data.id; - let post_view = match blocking(context.pool(), move |conn| { + let post_view = blocking(context.pool(), move |conn| { PostView::read(conn, id, person_id) }) .await? - { - Ok(post) => post, - Err(_e) => return Err(ApiError::err("couldnt_find_post").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_post"))?; let id = data.id; let comments = blocking(context.pool(), move |conn| { @@ -54,14 +51,11 @@ impl PerformCrud for GetPost { .await??; // Necessary for the sidebar - let community_view = match blocking(context.pool(), move |conn| { + let community_view = blocking(context.pool(), move |conn| { CommunityView::read(conn, community_id, person_id) }) .await? - { - Ok(community) => community, - Err(_e) => return Err(ApiError::err("couldnt_find_community").into()), - }; + .map_err(|_| ApiError::err("couldnt_find_community"))?; let online = context .chat_server() @@ -108,7 +102,7 @@ impl PerformCrud for GetPosts { let community_name = data.community_name.to_owned(); let saved_only = data.saved_only; - let posts = match blocking(context.pool(), move |conn| { + let posts = blocking(context.pool(), move |conn| { PostQueryBuilder::create(conn) .listing_type(&type_) .sort(&sort) @@ -122,10 +116,7 @@ impl PerformCrud for GetPosts { .list() }) .await? - { - Ok(posts) => posts, - Err(_e) => return Err(ApiError::err("couldnt_get_posts").into()), - }; + .map_err(|_| ApiError::err("couldnt_get_posts"))?; Ok(GetPostsResponse { posts }) } diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 53c0b950f..2dc223ec0 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -46,7 +46,7 @@ impl PerformCrud for CreatePrivateMessage { }; let inserted_private_message_id = inserted_private_message.id; - let updated_private_message = match blocking( + let updated_private_message = blocking( context.pool(), move |conn| -> Result { let apub_id = generate_apub_endpoint( @@ -61,10 +61,7 @@ impl PerformCrud for CreatePrivateMessage { }, ) .await? - { - Ok(private_message) => private_message, - Err(_e) => return Err(ApiError::err("couldnt_create_private_message").into()), - }; + .map_err(|_| ApiError::err("couldnt_create_private_message"))?; updated_private_message .send_create(&local_user_view.person, context) diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index 506b0490a..65c0022ce 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -37,14 +37,11 @@ impl PerformCrud for DeletePrivateMessage { // Doing the update let private_message_id = data.private_message_id; let deleted = data.deleted; - let updated_private_message = match blocking(context.pool(), move |conn| { + let updated_private_message = blocking(context.pool(), move |conn| { PrivateMessage::update_deleted(conn, private_message_id, deleted) }) .await? - { - Ok(private_message) => private_message, - Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_private_message"))?; // Send the apub update if data.deleted { diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index 4bfb1bb99..b073c928c 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -37,14 +37,11 @@ impl PerformCrud for EditPrivateMessage { // Doing the update let content_slurs_removed = remove_slurs(&data.content); let private_message_id = data.private_message_id; - let updated_private_message = match blocking(context.pool(), move |conn| { + let updated_private_message = blocking(context.pool(), move |conn| { PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed) }) .await? - { - Ok(private_message) => private_message, - Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()), - }; + .map_err(|_| ApiError::err("couldnt_update_private_message"))?; // Send the apub update updated_private_message diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 96bc3be9a..be9fdbed9 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -111,16 +111,11 @@ impl PerformCrud for Register { }; // insert the person - let inserted_person = match blocking(context.pool(), move |conn| { + let inserted_person = blocking(context.pool(), move |conn| { Person::create(conn, &person_form) }) .await? - { - Ok(u) => u, - Err(_) => { - return Err(ApiError::err("user_already_exists").into()); - } - }; + .map_err(|_| ApiError::err("user_already_exists"))?; // Create the local user let local_user_form = LocalUserForm { diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index 3136e100f..710bccb75 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -43,10 +43,9 @@ impl PerformCrud for GetPersonDetails { Person::find_by_name(conn, &username) }) .await?; - match person { - Ok(p) => p.id, - Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()), - } + person + .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))? + .id } };