From d2c6263ea13710177d49b2791278db5ad115fca5 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 30 Jun 2023 06:50:11 +0000 Subject: [PATCH] Get rid of repetitive `&mut *context.conn().await?` --- crates/api/src/comment/distinguish.rs | 13 ++-- crates/api/src/comment/like.rs | 19 +++--- crates/api/src/comment/save.rs | 8 +-- crates/api/src/comment_report/create.rs | 19 ++---- crates/api/src/comment_report/list.rs | 1 + crates/api/src/comment_report/resolve.rs | 12 ++-- crates/api/src/community/add_mod.rs | 19 ++---- crates/api/src/community/ban.rs | 21 +++--- crates/api/src/community/block.rs | 16 ++--- crates/api/src/community/follow.rs | 28 +++----- crates/api/src/community/hide.rs | 5 +- crates/api/src/community/transfer.rs | 28 ++++---- crates/api/src/local_user/add_admin.rs | 7 +- crates/api/src/local_user/ban_person.rs | 15 ++--- crates/api/src/local_user/block.rs | 7 +- crates/api/src/local_user/change_password.rs | 3 +- .../local_user/change_password_after_reset.rs | 12 ++-- crates/api/src/local_user/get_captcha.rs | 3 +- crates/api/src/local_user/list_banned.rs | 3 +- crates/api/src/local_user/login.rs | 17 ++--- .../local_user/notifications/mark_all_read.rs | 7 +- .../notifications/mark_mention_read.rs | 14 ++-- .../notifications/mark_reply_read.rs | 14 ++-- .../local_user/notifications/unread_count.rs | 10 ++- crates/api/src/local_user/report_count.rs | 15 ++--- crates/api/src/local_user/reset_password.rs | 12 ++-- crates/api/src/local_user/save_settings.rs | 23 ++----- crates/api/src/local_user/verify_email.rs | 8 +-- crates/api/src/post/feature.rs | 23 ++----- crates/api/src/post/like.rs | 20 +++--- crates/api/src/post/lock.rs | 23 ++----- crates/api/src/post/mark_read.rs | 8 +-- crates/api/src/post/save.rs | 10 +-- crates/api/src/post_report/create.rs | 19 ++---- crates/api/src/post_report/resolve.rs | 12 ++-- crates/api/src/private_message/mark_read.rs | 8 +-- .../api/src/private_message_report/create.rs | 13 ++-- .../api/src/private_message_report/resolve.rs | 8 +-- crates/api/src/site/federated_instances.rs | 3 +- crates/api/src/site/leave_admin.rs | 20 +++--- crates/api/src/site/mod_log.rs | 51 ++++++--------- crates/api/src/site/purge/comment.rs | 7 +- crates/api/src/site/purge/community.rs | 9 +-- crates/api/src/site/purge/person.rs | 16 ++--- crates/api/src/site/purge/post.rs | 7 +- .../site/registration_applications/approve.rs | 16 ++--- .../site/registration_applications/list.rs | 3 +- .../registration_applications/unread_count.rs | 10 ++- crates/api_common/src/build_response.rs | 65 +++++++++---------- crates/api_common/src/utils.rs | 3 +- crates/api_crud/src/comment/create.rs | 55 +++++----------- crates/api_crud/src/comment/delete.rs | 9 +-- crates/api_crud/src/comment/read.rs | 3 +- crates/api_crud/src/comment/remove.rs | 13 ++-- crates/api_crud/src/comment/update.rs | 11 ++-- crates/api_crud/src/community/create.rs | 16 ++--- crates/api_crud/src/community/delete.rs | 6 +- crates/api_crud/src/community/list.rs | 3 +- crates/api_crud/src/community/remove.rs | 5 +- crates/api_crud/src/community/update.rs | 16 ++--- crates/api_crud/src/custom_emoji/create.rs | 9 +-- crates/api_crud/src/custom_emoji/delete.rs | 3 +- crates/api_crud/src/custom_emoji/update.rs | 11 ++-- crates/api_crud/src/post/create.rs | 40 ++++-------- crates/api_crud/src/post/delete.rs | 14 ++-- crates/api_crud/src/post/read.rs | 52 ++++++--------- crates/api_crud/src/post/remove.rs | 21 ++---- crates/api_crud/src/post/update.rs | 16 ++--- crates/api_crud/src/private_message/create.rs | 20 ++---- crates/api_crud/src/private_message/delete.rs | 8 +-- crates/api_crud/src/private_message/read.rs | 3 +- crates/api_crud/src/private_message/update.rs | 10 +-- crates/api_crud/src/site/create.rs | 14 ++-- crates/api_crud/src/site/read.rs | 29 ++++----- crates/api_crud/src/site/update.rs | 31 ++++----- crates/api_crud/src/user/create.rs | 27 +++----- .../apub/src/activities/block/block_user.rs | 28 ++++---- crates/apub/src/activities/block/mod.rs | 18 ++--- .../src/activities/block/undo_block_user.rs | 14 ++-- .../activities/community/collection_add.rs | 27 ++++---- .../activities/community/collection_remove.rs | 9 +-- .../src/activities/community/lock_page.rs | 10 +-- crates/apub/src/activities/community/mod.rs | 4 +- .../apub/src/activities/community/report.rs | 5 +- .../apub/src/activities/community/update.rs | 14 ++-- .../activities/create_or_update/comment.rs | 21 +++--- .../src/activities/create_or_update/post.rs | 14 ++-- .../create_or_update/private_message.rs | 9 +-- crates/apub/src/activities/deletion/delete.rs | 13 ++-- .../src/activities/deletion/delete_user.rs | 20 ++---- crates/apub/src/activities/deletion/mod.rs | 46 ++++++------- .../src/activities/deletion/undo_delete.rs | 13 ++-- .../apub/src/activities/following/accept.rs | 4 +- .../apub/src/activities/following/follow.rs | 11 ++-- crates/apub/src/activities/following/mod.rs | 8 +-- .../src/activities/following/undo_follow.rs | 5 +- crates/apub/src/activities/mod.rs | 7 +- crates/apub/src/activities/voting/mod.rs | 23 ++++--- crates/apub/src/activities/voting/vote.rs | 4 +- crates/apub/src/api/list_comments.rs | 9 +-- crates/apub/src/api/list_posts.rs | 13 ++-- crates/apub/src/api/read_community.rs | 36 ++++------ crates/apub/src/api/read_person.rs | 8 +-- crates/apub/src/api/resolve_object.rs | 5 +- crates/apub/src/api/search.rs | 3 +- crates/apub/src/fetcher/mod.rs | 6 +- crates/apub/src/fetcher/post_or_comment.rs | 13 +--- crates/apub/src/http/comment.rs | 3 +- crates/apub/src/http/community.rs | 36 +++++----- crates/apub/src/http/mod.rs | 3 +- crates/apub/src/http/person.rs | 6 +- crates/apub/src/http/post.rs | 3 +- crates/apub/src/http/site.rs | 6 +- crates/apub/src/lib.rs | 1 + crates/apub/src/mentions.rs | 3 +- crates/apub/src/objects/comment.rs | 33 +++++----- crates/apub/src/objects/community.rs | 19 +++--- crates/apub/src/objects/instance.rs | 3 +- crates/apub/src/objects/person.rs | 16 +++-- crates/apub/src/objects/post.rs | 31 +++++---- crates/apub/src/objects/private_message.rs | 20 ++++-- .../activities/community/collection_add.rs | 4 +- .../activities/community/collection_remove.rs | 4 +- .../activities/community/lock_page.rs | 3 +- .../activities/create_or_update/note.rs | 3 +- .../protocol/activities/deletion/delete.rs | 5 +- .../protocol/collections/group_followers.rs | 4 +- crates/apub/src/protocol/objects/group.rs | 3 +- crates/apub/src/protocol/objects/note.rs | 7 +- crates/db_views_actor/src/person_view.rs | 1 + crates/routes/src/feeds.rs | 21 +++--- crates/routes/src/images.rs | 4 +- crates/routes/src/nodeinfo.rs | 4 +- crates/routes/src/webfinger.rs | 13 ++-- 134 files changed, 820 insertions(+), 1017 deletions(-) diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index ef513243b..8df503cf2 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -18,22 +18,24 @@ impl Perform for DistinguishComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; + let data: &DistinguishComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let orig_comment = CommentView::read(&mut conn, comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - &mut *context.conn().await?, + &mut conn, ) .await?; // Verify that only a mod or admin can distinguish a comment is_mod_or_admin( - &mut *context.conn().await?, + &mut conn, local_user_view.person.id, orig_comment.community.id, ) @@ -44,14 +46,13 @@ impl Perform for DistinguishComment { let form = CommentUpdateForm::builder() .distinguished(Some(data.distinguished)) .build(); - Comment::update(&mut *context.conn().await?, comment_id, &form) + Comment::update(&mut conn, comment_id, &form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; let comment_id = data.comment_id; let person_id = local_user_view.person.id; - let comment_view = - CommentView::read(&mut *context.conn().await?, comment_id, Some(person_id)).await?; + let comment_view = CommentView::read(&mut conn, comment_id, Some(person_id)).await?; Ok(CommentResponse { comment_view, diff --git a/crates/api/src/comment/like.rs b/crates/api/src/comment/like.rs index ca090c788..7a4309795 100644 --- a/crates/api/src/comment/like.rs +++ b/crates/api/src/comment/like.rs @@ -24,8 +24,10 @@ impl Perform for CreateCommentLike { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; + let data: &CreateCommentLike = self; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let mut recipient_ids = Vec::::new(); @@ -34,23 +36,20 @@ impl Perform for CreateCommentLike { check_downvotes_enabled(data.score, &local_site)?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let orig_comment = CommentView::read(&mut conn, comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - &mut *context.conn().await?, + &mut conn, ) .await?; // Add parent poster or commenter to recipients - let comment_reply = - CommentReply::read_by_comment(&mut *context.conn().await?, comment_id).await; + let comment_reply = CommentReply::read_by_comment(&mut conn, comment_id).await; if let Ok(reply) = comment_reply { let recipient_id = reply.recipient_id; - if let Ok(local_recipient) = - LocalUserView::read_person(&mut *context.conn().await?, recipient_id).await - { + if let Ok(local_recipient) = LocalUserView::read_person(&mut conn, recipient_id).await { recipient_ids.push(local_recipient.local_user.id); } } @@ -65,12 +64,12 @@ impl Perform for CreateCommentLike { // Remove any likes first let person_id = local_user_view.person.id; - CommentLike::remove(&mut *context.conn().await?, person_id, comment_id).await?; + CommentLike::remove(&mut conn, person_id, comment_id).await?; // Only add the like if the score isnt 0 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1); if do_add { - CommentLike::like(&mut *context.conn().await?, &like_form) + CommentLike::like(&mut conn, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?; } diff --git a/crates/api/src/comment/save.rs b/crates/api/src/comment/save.rs index 35269d7ce..6b7d67352 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -18,6 +18,7 @@ impl Perform for SaveComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &SaveComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -27,19 +28,18 @@ impl Perform for SaveComment { }; if data.save { - CommentSaved::save(&mut *context.conn().await?, &comment_saved_form) + CommentSaved::save(&mut conn, &comment_saved_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?; } else { - CommentSaved::unsave(&mut *context.conn().await?, &comment_saved_form) + CommentSaved::unsave(&mut conn, &comment_saved_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?; } let comment_id = data.comment_id; let person_id = local_user_view.person.id; - let comment_view = - CommentView::read(&mut *context.conn().await?, comment_id, Some(person_id)).await?; + let comment_view = CommentView::read(&mut conn, comment_id, Some(person_id)).await?; Ok(CommentResponse { comment_view, diff --git a/crates/api/src/comment_report/create.rs b/crates/api/src/comment_report/create.rs index 34243f8b9..e04de88f6 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -25,23 +25,19 @@ impl Perform for CreateCommentReport { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &CreateCommentReport = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let reason = self.reason.trim(); check_report_reason(reason, &local_site)?; let person_id = local_user_view.person.id; let comment_id = data.comment_id; - let comment_view = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let comment_view = CommentView::read(&mut conn, comment_id, None).await?; - check_community_ban( - person_id, - comment_view.community.id, - &mut *context.conn().await?, - ) - .await?; + check_community_ban(person_id, comment_view.community.id, &mut conn).await?; let report_form = CommentReportForm { creator_id: person_id, @@ -50,19 +46,18 @@ impl Perform for CreateCommentReport { reason: reason.to_owned(), }; - let report = CommentReport::report(&mut *context.conn().await?, &report_form) + let report = CommentReport::report(&mut conn, &report_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?; - let comment_report_view = - CommentReportView::read(&mut *context.conn().await?, report.id, person_id).await?; + let comment_report_view = CommentReportView::read(&mut conn, report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &comment_report_view.creator.name, &comment_report_view.comment_creator.name, - &mut *context.conn().await?, + &mut conn, context.settings(), ) .await?; diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index 61568794c..d642c85af 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -30,6 +30,7 @@ impl Perform for ListCommentReports { let page = data.page; let limit = data.limit; let mut conn = context.conn().await?; + let comment_reports = CommentReportQuery::builder() .conn(&mut conn) .my_person_id(person_id) diff --git a/crates/api/src/comment_report/resolve.rs b/crates/api/src/comment_report/resolve.rs index 496184d58..41e62f141 100644 --- a/crates/api/src/comment_report/resolve.rs +++ b/crates/api/src/comment_report/resolve.rs @@ -19,29 +19,29 @@ impl Perform for ResolveCommentReport { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &ResolveCommentReport = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let report_id = data.report_id; let person_id = local_user_view.person.id; - let report = CommentReportView::read(&mut *context.conn().await?, report_id, person_id).await?; + let report = CommentReportView::read(&mut conn, report_id, person_id).await?; let person_id = local_user_view.person.id; - is_mod_or_admin(&mut *context.conn().await?, person_id, report.community.id).await?; + is_mod_or_admin(&mut conn, person_id, report.community.id).await?; if data.resolved { - CommentReport::resolve(&mut *context.conn().await?, report_id, person_id) + CommentReport::resolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } else { - CommentReport::unresolve(&mut *context.conn().await?, report_id, person_id) + CommentReport::unresolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } let report_id = data.report_id; - let comment_report_view = - CommentReportView::read(&mut *context.conn().await?, report_id, person_id).await?; + let comment_report_view = CommentReportView::read(&mut conn, report_id, person_id).await?; Ok(CommentReportResponse { comment_report_view, diff --git a/crates/api/src/community/add_mod.rs b/crates/api/src/community/add_mod.rs index 6f17db270..4702f9261 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -24,19 +24,15 @@ impl Perform for AddModToCommunity { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &AddModToCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; // Verify that only mods or admins can add mod - is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - community_id, - ) - .await?; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + is_mod_or_admin(&mut conn, local_user_view.person.id, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; if local_user_view.person.admin && !community.local { return Err(LemmyError::from_message("not_a_moderator")); } @@ -47,11 +43,11 @@ impl Perform for AddModToCommunity { person_id: data.person_id, }; if data.added { - CommunityModerator::join(&mut *context.conn().await?, &community_moderator_form) + CommunityModerator::join(&mut conn, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; } else { - CommunityModerator::leave(&mut *context.conn().await?, &community_moderator_form) + CommunityModerator::leave(&mut conn, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; } @@ -64,13 +60,12 @@ impl Perform for AddModToCommunity { removed: Some(!data.added), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(&mut conn, &form).await?; // Note: in case a remote mod is added, this returns the old moderators list, it will only get // updated once we receive an activity from the community (like `Announce/Add/Moderator`) let community_id = data.community_id; - let moderators = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + let moderators = CommunityModeratorView::for_community(&mut conn, community_id).await?; Ok(AddModToCommunityResponse { moderators }) } diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 93a588641..fbb80ed50 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -32,6 +32,7 @@ impl Perform for BanFromCommunity { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &BanFromCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -41,12 +42,7 @@ impl Perform for BanFromCommunity { let expires = data.expires.map(naive_from_unix); // Verify that only mods or admins can ban - is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - community_id, - ) - .await?; + is_mod_or_admin(&mut conn, local_user_view.person.id, community_id).await?; is_valid_body_field(&data.reason, false)?; let community_user_ban_form = CommunityPersonBanForm { @@ -56,7 +52,7 @@ impl Perform for BanFromCommunity { }; if data.ban { - CommunityPersonBan::ban(&mut *context.conn().await?, &community_user_ban_form) + CommunityPersonBan::ban(&mut conn, &community_user_ban_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?; @@ -67,19 +63,18 @@ impl Perform for BanFromCommunity { pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(&mut conn, &community_follower_form) .await .ok(); } else { - CommunityPersonBan::unban(&mut *context.conn().await?, &community_user_ban_form) + CommunityPersonBan::unban(&mut conn, &community_user_ban_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?; } // Remove/Restore their data if that's desired if remove_data { - remove_user_data_in_community(community_id, banned_person_id, &mut *context.conn().await?) - .await?; + remove_user_data_in_community(community_id, banned_person_id, &mut conn).await?; } // Mod tables @@ -92,10 +87,10 @@ impl Perform for BanFromCommunity { expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(&mut conn, &form).await?; let person_id = data.person_id; - let person_view = PersonView::read(&mut *context.conn().await?, person_id).await?; + let person_view = PersonView::read(&mut conn, person_id).await?; Ok(BanFromCommunityResponse { person_view, diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index 9af2070f9..ec8dfd739 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -24,6 +24,7 @@ impl Perform for BlockCommunity { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &BlockCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -35,7 +36,7 @@ impl Perform for BlockCommunity { }; if data.block { - CommunityBlock::block(&mut *context.conn().await?, &community_block_form) + CommunityBlock::block(&mut conn, &community_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?; @@ -46,22 +47,17 @@ impl Perform for BlockCommunity { pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(&mut conn, &community_follower_form) .await .ok(); } else { - CommunityBlock::unblock(&mut *context.conn().await?, &community_block_form) + CommunityBlock::unblock(&mut conn, &community_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?; } - let community_view = CommunityView::read( - &mut *context.conn().await?, - community_id, - Some(person_id), - None, - ) - .await?; + let community_view = + CommunityView::read(&mut conn, community_id, Some(person_id), None).await?; Ok(BlockCommunityResponse { blocked: data.block, diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index cacedeeba..bb998dcc3 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -21,11 +21,12 @@ impl Perform for FollowCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &FollowCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: local_user_view.person.id, @@ -33,35 +34,24 @@ impl Perform for FollowCommunity { }; if community.local && data.follow { - check_community_ban( - local_user_view.person.id, - community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, community_id, &mut conn).await?; + check_community_deleted_or_removed(community_id, &mut conn).await?; - CommunityFollower::follow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::follow(&mut conn, &community_follower_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?; } if !data.follow { - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(&mut conn, &community_follower_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?; } let community_id = data.community_id; let person_id = local_user_view.person.id; - let community_view = CommunityView::read( - &mut *context.conn().await?, - community_id, - Some(person_id), - None, - ) - .await?; - let discussion_languages = - CommunityLanguage::read(&mut *context.conn().await?, community_id).await?; + let community_view = + CommunityView::read(&mut conn, community_id, Some(person_id), None).await?; + let discussion_languages = CommunityLanguage::read(&mut conn, community_id).await?; Ok(Self::Response { community_view, diff --git a/crates/api/src/community/hide.rs b/crates/api/src/community/hide.rs index 6251f95fa..54cb60326 100644 --- a/crates/api/src/community/hide.rs +++ b/crates/api/src/community/hide.rs @@ -21,6 +21,7 @@ impl Perform for HideCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &HideCommunity = self; // Verify its a admin (only admin can hide or unhide it) @@ -39,11 +40,11 @@ impl Perform for HideCommunity { }; let community_id = data.community_id; - Community::update(&mut *context.conn().await?, community_id, &community_form) + Community::update(&mut conn, community_id, &community_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?; - ModHideCommunity::create(&mut *context.conn().await?, &mod_hide_community_form).await?; + ModHideCommunity::create(&mut conn, &mod_hide_community_form).await?; build_community_response(context, local_user_view, community_id).await } diff --git a/crates/api/src/community/transfer.rs b/crates/api/src/community/transfer.rs index 1d4286e26..17e3feddf 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -27,13 +27,13 @@ impl Perform for TransferCommunity { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &TransferCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Fetch the community mods let community_id = data.community_id; - let mut community_mods = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + let mut community_mods = CommunityModeratorView::for_community(&mut conn, community_id).await?; // Make sure transferrer is either the top community mod, or an admin if !(is_top_mod(&local_user_view, &community_mods).is_ok() @@ -54,7 +54,7 @@ impl Perform for TransferCommunity { // Delete all the mods let community_id = data.community_id; - CommunityModerator::delete_for_community(&mut *context.conn().await?, community_id).await?; + CommunityModerator::delete_for_community(&mut conn, community_id).await?; // TODO: this should probably be a bulk operation // Re-add the mods, in the new order @@ -64,7 +64,7 @@ impl Perform for TransferCommunity { person_id: cmod.moderator.id, }; - CommunityModerator::join(&mut *context.conn().await?, &community_moderator_form) + CommunityModerator::join(&mut conn, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; } @@ -76,24 +76,18 @@ impl Perform for TransferCommunity { community_id: data.community_id, }; - ModTransferCommunity::create(&mut *context.conn().await?, &form).await?; + ModTransferCommunity::create(&mut conn, &form).await?; let community_id = data.community_id; let person_id = local_user_view.person.id; - let community_view = CommunityView::read( - &mut *context.conn().await?, - community_id, - Some(person_id), - None, - ) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; + let community_view = CommunityView::read(&mut conn, community_id, Some(person_id), None) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; let community_id = data.community_id; - let moderators = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; + let moderators = CommunityModeratorView::for_community(&mut conn, community_id) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; // Return the jwt Ok(GetCommunityResponse { diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index e9cd562f2..23a1c53db 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -21,6 +21,7 @@ impl Perform for AddAdmin { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &AddAdmin = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -30,7 +31,7 @@ impl Perform for AddAdmin { let added = data.added; let added_person_id = data.person_id; let added_admin = Person::update( - &mut *context.conn().await?, + &mut conn, added_person_id, &PersonUpdateForm::builder().admin(Some(added)).build(), ) @@ -44,9 +45,9 @@ impl Perform for AddAdmin { removed: Some(!data.added), }; - ModAdd::create(&mut *context.conn().await?, &form).await?; + ModAdd::create(&mut conn, &form).await?; - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let admins = PersonView::admins(&mut conn).await?; Ok(AddAdminResponse { admins }) } diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index 50e3ca550..425cc588f 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -24,6 +24,7 @@ impl Perform for BanPerson { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &BanPerson = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -37,7 +38,7 @@ impl Perform for BanPerson { let expires = data.expires.map(naive_from_unix); let person = Person::update( - &mut *context.conn().await?, + &mut conn, banned_person_id, &PersonUpdateForm::builder() .banned(Some(ban)) @@ -50,13 +51,7 @@ impl Perform for BanPerson { // Remove their data if that's desired let remove_data = data.remove_data.unwrap_or(false); if remove_data { - remove_user_data( - person.id, - &mut *context.conn().await?, - context.settings(), - context.client(), - ) - .await?; + remove_user_data(person.id, &mut conn, context.settings(), context.client()).await?; } // Mod tables @@ -68,10 +63,10 @@ impl Perform for BanPerson { expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(&mut conn, &form).await?; let person_id = data.person_id; - let person_view = PersonView::read(&mut *context.conn().await?, person_id).await?; + let person_view = PersonView::read(&mut conn, person_id).await?; Ok(BanPersonResponse { person_view, diff --git a/crates/api/src/local_user/block.rs b/crates/api/src/local_user/block.rs index 4ffb07b5d..ea30202d8 100644 --- a/crates/api/src/local_user/block.rs +++ b/crates/api/src/local_user/block.rs @@ -18,6 +18,7 @@ impl Perform for BlockPerson { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &BlockPerson = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -34,18 +35,18 @@ impl Perform for BlockPerson { target_id, }; - let target_person_view = PersonView::read(&mut *context.conn().await?, target_id).await?; + let target_person_view = PersonView::read(&mut conn, target_id).await?; if target_person_view.person.admin { return Err(LemmyError::from_message("cant_block_admin")); } if data.block { - PersonBlock::block(&mut *context.conn().await?, &person_block_form) + PersonBlock::block(&mut conn, &person_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?; } else { - PersonBlock::unblock(&mut *context.conn().await?, &person_block_form) + PersonBlock::unblock(&mut conn, &person_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?; } diff --git a/crates/api/src/local_user/change_password.rs b/crates/api/src/local_user/change_password.rs index 87f7bb84c..64255b224 100644 --- a/crates/api/src/local_user/change_password.rs +++ b/crates/api/src/local_user/change_password.rs @@ -15,6 +15,7 @@ impl Perform for ChangePassword { #[tracing::instrument(skip(self, context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &ChangePassword = self; let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?; @@ -38,7 +39,7 @@ impl Perform for ChangePassword { let local_user_id = local_user_view.local_user.id; let new_password = data.new_password.clone(); let updated_local_user = - LocalUser::update_password(&mut *context.conn().await?, local_user_id, &new_password).await?; + LocalUser::update_password(&mut conn, local_user_id, &new_password).await?; // Return the jwt Ok(LoginResponse { diff --git a/crates/api/src/local_user/change_password_after_reset.rs b/crates/api/src/local_user/change_password_after_reset.rs index d3453aa98..2203c33a7 100644 --- a/crates/api/src/local_user/change_password_after_reset.rs +++ b/crates/api/src/local_user/change_password_after_reset.rs @@ -18,11 +18,12 @@ impl Perform for PasswordChangeAfterReset { #[tracing::instrument(skip(self, context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &PasswordChangeAfterReset = self; // Fetch the user_id from the token let token = data.token.clone(); - let local_user_id = PasswordResetRequest::read_from_token(&mut *context.conn().await?, &token) + let local_user_id = PasswordResetRequest::read_from_token(&mut conn, &token) .await .map(|p| p.local_user_id)?; @@ -35,13 +36,12 @@ impl Perform for PasswordChangeAfterReset { // Update the user with the new password let password = data.password.clone(); - let updated_local_user = - LocalUser::update_password(&mut *context.conn().await?, local_user_id, &password) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?; + let updated_local_user = LocalUser::update_password(&mut conn, local_user_id, &password) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?; // Return the jwt if login is allowed - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let jwt = if site_view.local_site.registration_mode == RegistrationMode::RequireApplication && !updated_local_user.accepted_application { diff --git a/crates/api/src/local_user/get_captcha.rs b/crates/api/src/local_user/get_captcha.rs index 8ee785fcd..45e16bcee 100644 --- a/crates/api/src/local_user/get_captcha.rs +++ b/crates/api/src/local_user/get_captcha.rs @@ -17,7 +17,8 @@ impl Perform for GetCaptcha { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let mut conn = context.conn().await?; + let local_site = LocalSite::read(&mut conn).await?; if !local_site.captcha_enabled { return Ok(GetCaptchaResponse { ok: None }); diff --git a/crates/api/src/local_user/list_banned.rs b/crates/api/src/local_user/list_banned.rs index 773b1f493..47ce5bc84 100644 --- a/crates/api/src/local_user/list_banned.rs +++ b/crates/api/src/local_user/list_banned.rs @@ -13,13 +13,14 @@ impl Perform for GetBannedPersons { type Response = BannedPersonsResponse; async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetBannedPersons = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; - let banned = PersonView::banned(&mut *context.conn().await?).await?; + let banned = PersonView::banned(&mut conn).await?; Ok(Self::Response { banned }) } diff --git a/crates/api/src/local_user/login.rs b/crates/api/src/local_user/login.rs index d7f91a8ab..a188ddb19 100644 --- a/crates/api/src/local_user/login.rs +++ b/crates/api/src/local_user/login.rs @@ -15,16 +15,16 @@ impl Perform for Login { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Login = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; // Fetch that username / email let username_or_email = data.username_or_email.clone(); - let local_user_view = - LocalUserView::find_by_email_or_name(&mut *context.conn().await?, &username_or_email) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; + let local_user_view = LocalUserView::find_by_email_or_name(&mut conn, &username_or_email) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; // Verify the password let valid: bool = verify( @@ -50,12 +50,7 @@ impl Perform for Login { return Err(LemmyError::from_message("email_not_verified")); } - check_registration_application( - &local_user_view, - &site_view.local_site, - &mut *context.conn().await?, - ) - .await?; + check_registration_application(&local_user_view, &site_view.local_site, &mut conn).await?; // Check the totp check_totp_2fa_valid( diff --git a/crates/api/src/local_user/notifications/mark_all_read.rs b/crates/api/src/local_user/notifications/mark_all_read.rs index beceeb758..766515330 100644 --- a/crates/api/src/local_user/notifications/mark_all_read.rs +++ b/crates/api/src/local_user/notifications/mark_all_read.rs @@ -18,22 +18,23 @@ impl Perform for MarkAllAsRead { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &MarkAllAsRead = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; // Mark all comment_replies as read - CommentReply::mark_all_as_read(&mut *context.conn().await?, person_id) + CommentReply::mark_all_as_read(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; // Mark all user mentions as read - PersonMention::mark_all_as_read(&mut *context.conn().await?, person_id) + PersonMention::mark_all_as_read(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; // Mark all private_messages as read - PrivateMessage::mark_all_as_read(&mut *context.conn().await?, person_id) + PrivateMessage::mark_all_as_read(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?; diff --git a/crates/api/src/local_user/notifications/mark_mention_read.rs b/crates/api/src/local_user/notifications/mark_mention_read.rs index 5119ea751..4dc31cc03 100644 --- a/crates/api/src/local_user/notifications/mark_mention_read.rs +++ b/crates/api/src/local_user/notifications/mark_mention_read.rs @@ -21,12 +21,12 @@ impl Perform for MarkPersonMentionAsRead { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &MarkPersonMentionAsRead = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_mention_id = data.person_mention_id; - let read_person_mention = - PersonMention::read(&mut *context.conn().await?, person_mention_id).await?; + let read_person_mention = PersonMention::read(&mut conn, person_mention_id).await?; if local_user_view.person.id != read_person_mention.recipient_id { return Err(LemmyError::from_message("couldnt_update_comment")); @@ -35,7 +35,7 @@ impl Perform for MarkPersonMentionAsRead { let person_mention_id = read_person_mention.id; let read = Some(data.read); PersonMention::update( - &mut *context.conn().await?, + &mut conn, person_mention_id, &PersonMentionUpdateForm { read }, ) @@ -44,12 +44,8 @@ impl Perform for MarkPersonMentionAsRead { let person_mention_id = read_person_mention.id; let person_id = local_user_view.person.id; - let person_mention_view = PersonMentionView::read( - &mut *context.conn().await?, - person_mention_id, - Some(person_id), - ) - .await?; + let person_mention_view = + PersonMentionView::read(&mut conn, person_mention_id, Some(person_id)).await?; Ok(PersonMentionResponse { person_mention_view, diff --git a/crates/api/src/local_user/notifications/mark_reply_read.rs b/crates/api/src/local_user/notifications/mark_reply_read.rs index 10e574c6a..4de8524d8 100644 --- a/crates/api/src/local_user/notifications/mark_reply_read.rs +++ b/crates/api/src/local_user/notifications/mark_reply_read.rs @@ -21,12 +21,12 @@ impl Perform for MarkCommentReplyAsRead { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_reply_id = data.comment_reply_id; - let read_comment_reply = - CommentReply::read(&mut *context.conn().await?, comment_reply_id).await?; + let read_comment_reply = CommentReply::read(&mut conn, comment_reply_id).await?; if local_user_view.person.id != read_comment_reply.recipient_id { return Err(LemmyError::from_message("couldnt_update_comment")); @@ -36,7 +36,7 @@ impl Perform for MarkCommentReplyAsRead { let read = Some(data.read); CommentReply::update( - &mut *context.conn().await?, + &mut conn, comment_reply_id, &CommentReplyUpdateForm { read }, ) @@ -45,12 +45,8 @@ impl Perform for MarkCommentReplyAsRead { let comment_reply_id = read_comment_reply.id; let person_id = local_user_view.person.id; - let comment_reply_view = CommentReplyView::read( - &mut *context.conn().await?, - comment_reply_id, - Some(person_id), - ) - .await?; + let comment_reply_view = + CommentReplyView::read(&mut conn, comment_reply_id, Some(person_id)).await?; Ok(CommentReplyResponse { comment_reply_view }) } diff --git a/crates/api/src/local_user/notifications/unread_count.rs b/crates/api/src/local_user/notifications/unread_count.rs index 49c78dc9c..ab7c59d7c 100644 --- a/crates/api/src/local_user/notifications/unread_count.rs +++ b/crates/api/src/local_user/notifications/unread_count.rs @@ -15,19 +15,17 @@ impl Perform for GetUnreadCount { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; - let replies = - CommentReplyView::get_unread_replies(&mut *context.conn().await?, person_id).await?; + let replies = CommentReplyView::get_unread_replies(&mut conn, person_id).await?; - let mentions = - PersonMentionView::get_unread_mentions(&mut *context.conn().await?, person_id).await?; + let mentions = PersonMentionView::get_unread_mentions(&mut conn, person_id).await?; - let private_messages = - PrivateMessageView::get_unread_messages(&mut *context.conn().await?, person_id).await?; + let private_messages = PrivateMessageView::get_unread_messages(&mut conn, person_id).await?; Ok(Self::Response { replies, diff --git a/crates/api/src/local_user/report_count.rs b/crates/api/src/local_user/report_count.rs index bfcbe71a7..e130a302b 100644 --- a/crates/api/src/local_user/report_count.rs +++ b/crates/api/src/local_user/report_count.rs @@ -17,6 +17,7 @@ impl Perform for GetReportCount { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &GetReportCount = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -24,20 +25,14 @@ impl Perform for GetReportCount { let admin = local_user_view.person.admin; let community_id = data.community_id; - let comment_reports = CommentReportView::get_report_count( - &mut *context.conn().await?, - person_id, - admin, - community_id, - ) - .await?; + let comment_reports = + CommentReportView::get_report_count(&mut conn, person_id, admin, community_id).await?; let post_reports = - PostReportView::get_report_count(&mut *context.conn().await?, person_id, admin, community_id) - .await?; + PostReportView::get_report_count(&mut conn, person_id, admin, community_id).await?; let private_message_reports = if admin && community_id.is_none() { - Some(PrivateMessageReportView::get_report_count(&mut *context.conn().await?).await?) + Some(PrivateMessageReportView::get_report_count(&mut conn).await?) } else { None }; diff --git a/crates/api/src/local_user/reset_password.rs b/crates/api/src/local_user/reset_password.rs index d0072f6cd..29411cae0 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -18,17 +18,18 @@ impl Perform for PasswordReset { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &PasswordReset = self; // Fetch that email let email = data.email.to_lowercase(); - let local_user_view = LocalUserView::find_by_email(&mut *context.conn().await?, &email) + let local_user_view = LocalUserView::find_by_email(&mut conn, &email) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; // Check for too many attempts (to limit potential abuse) let recent_resets_count = PasswordResetRequest::get_recent_password_resets_count( - &mut *context.conn().await?, + &mut conn, local_user_view.local_user.id, ) .await?; @@ -37,12 +38,7 @@ impl Perform for PasswordReset { } // Email the pure token to the user. - send_password_reset_email( - &local_user_view, - &mut *context.conn().await?, - context.settings(), - ) - .await?; + send_password_reset_email(&local_user_view, &mut conn, context.settings()).await?; Ok(PasswordResetResponse {}) } } diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 0c8974029..1356e895f 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -33,9 +33,10 @@ impl Perform for SaveUserSettings { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &SaveUserSettings = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let avatar = diesel_option_overwrite_to_url(&data.avatar)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -49,13 +50,7 @@ impl Perform for SaveUserSettings { let previous_email = local_user_view.local_user.email.clone().unwrap_or_default(); // Only send the verification email if there was an email change if previous_email.ne(email) { - send_verification_email( - &local_user_view, - email, - &mut *context.conn().await?, - context.settings(), - ) - .await?; + send_verification_email(&local_user_view, email, &mut conn, context.settings()).await?; } } @@ -95,17 +90,12 @@ impl Perform for SaveUserSettings { .banner(banner) .build(); - Person::update(&mut *context.conn().await?, person_id, &person_form) + Person::update(&mut conn, person_id, &person_form) .await .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?; if let Some(discussion_languages) = data.discussion_languages.clone() { - LocalUserLanguage::update( - &mut *context.conn().await?, - discussion_languages, - local_user_id, - ) - .await?; + LocalUserLanguage::update(&mut conn, discussion_languages, local_user_id).await?; } // If generate_totp is Some(false), this will clear it out from the database. @@ -139,8 +129,7 @@ impl Perform for SaveUserSettings { .totp_2fa_url(totp_2fa_url) .build(); - let local_user_res = - LocalUser::update(&mut *context.conn().await?, local_user_id, &local_user_form).await; + let local_user_res = LocalUser::update(&mut conn, local_user_id, &local_user_form).await; let updated_local_user = match local_user_res { Ok(u) => u, Err(e) => { diff --git a/crates/api/src/local_user/verify_email.rs b/crates/api/src/local_user/verify_email.rs index 99d6c1d81..2d9006f22 100644 --- a/crates/api/src/local_user/verify_email.rs +++ b/crates/api/src/local_user/verify_email.rs @@ -18,8 +18,9 @@ impl Perform for VerifyEmail { type Response = VerifyEmailResponse; async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let token = self.token.clone(); - let verification = EmailVerification::read_for_token(&mut *context.conn().await?, &token) + let verification = EmailVerification::read_for_token(&mut conn, &token) .await .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?; @@ -31,10 +32,9 @@ impl Perform for VerifyEmail { .build(); let local_user_id = verification.local_user_id; - LocalUser::update(&mut *context.conn().await?, local_user_id, &form).await?; + LocalUser::update(&mut conn, local_user_id, &form).await?; - EmailVerification::delete_old_tokens_for_local_user(&mut *context.conn().await?, local_user_id) - .await?; + EmailVerification::delete_old_tokens_for_local_user(&mut conn, local_user_id).await?; Ok(VerifyEmailResponse {}) } diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index b4bded456..50b583010 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -28,28 +28,19 @@ impl Perform for FeaturePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &FeaturePost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; - let orig_post = Post::read(&mut *context.conn().await?, post_id).await?; + let orig_post = Post::read(&mut conn, post_id).await?; - check_community_ban( - local_user_view.person.id, - orig_post.community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(orig_post.community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, orig_post.community_id, &mut conn).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut conn).await?; if data.feature_type == PostFeatureType::Community { // Verify that only the mods can feature in community - is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - orig_post.community_id, - ) - .await?; + is_mod_or_admin(&mut conn, local_user_view.person.id, orig_post.community_id).await?; } else { is_admin(&local_user_view)?; } @@ -65,7 +56,7 @@ impl Perform for FeaturePost { .featured_local(Some(data.featured)) .build() }; - Post::update(&mut *context.conn().await?, post_id, &new_post).await?; + Post::update(&mut conn, post_id, &new_post).await?; // Mod tables let form = ModFeaturePostForm { @@ -75,7 +66,7 @@ impl Perform for FeaturePost { is_featured_community: data.feature_type == PostFeatureType::Community, }; - ModFeaturePost::create(&mut *context.conn().await?, &form).await?; + ModFeaturePost::create(&mut conn, &form).await?; build_post_response( context, diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index f0c88a875..3ba8747bc 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -27,24 +27,20 @@ impl Perform for CreatePostLike { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreatePostLike = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Don't do a downvote if site has downvotes disabled check_downvotes_enabled(data.score, &local_site)?; // Check for a community ban let post_id = data.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; - check_community_ban( - local_user_view.person.id, - post.community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(post.community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, post.community_id, &mut conn).await?; + check_community_deleted_or_removed(post.community_id, &mut conn).await?; let like_form = PostLikeForm { post_id: data.post_id, @@ -55,18 +51,18 @@ impl Perform for CreatePostLike { // Remove any likes first let person_id = local_user_view.person.id; - PostLike::remove(&mut *context.conn().await?, person_id, post_id).await?; + PostLike::remove(&mut conn, person_id, post_id).await?; // Only add the like if the score isnt 0 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1); if do_add { - PostLike::like(&mut *context.conn().await?, &like_form) + PostLike::like(&mut conn, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?; } // Mark the post as read - mark_post_as_read(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_read(person_id, post_id, &mut conn).await?; build_post_response( context, diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index 638630cad..b163be7d3 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -26,33 +26,24 @@ impl Perform for LockPost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &LockPost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; - let orig_post = Post::read(&mut *context.conn().await?, post_id).await?; + let orig_post = Post::read(&mut conn, post_id).await?; - check_community_ban( - local_user_view.person.id, - orig_post.community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(orig_post.community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, orig_post.community_id, &mut conn).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut conn).await?; // Verify that only the mods can lock - is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - orig_post.community_id, - ) - .await?; + is_mod_or_admin(&mut conn, local_user_view.person.id, orig_post.community_id).await?; // Update the post let post_id = data.post_id; let locked = data.locked; Post::update( - &mut *context.conn().await?, + &mut conn, post_id, &PostUpdateForm::builder().locked(Some(locked)).build(), ) @@ -64,7 +55,7 @@ impl Perform for LockPost { post_id: data.post_id, locked: Some(locked), }; - ModLockPost::create(&mut *context.conn().await?, &form).await?; + ModLockPost::create(&mut conn, &form).await?; build_post_response( context, diff --git a/crates/api/src/post/mark_read.rs b/crates/api/src/post/mark_read.rs index 3b54479ac..076c76745 100644 --- a/crates/api/src/post/mark_read.rs +++ b/crates/api/src/post/mark_read.rs @@ -14,6 +14,7 @@ impl Perform for MarkPostAsRead { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -22,14 +23,13 @@ impl Perform for MarkPostAsRead { // Mark the post as read / unread if data.read { - mark_post_as_read(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_read(person_id, post_id, &mut conn).await?; } else { - mark_post_as_unread(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_unread(person_id, post_id, &mut conn).await?; } // Fetch it - let post_view = - PostView::read(&mut *context.conn().await?, post_id, Some(person_id), None).await?; + let post_view = PostView::read(&mut conn, post_id, Some(person_id), None).await?; Ok(Self::Response { post_view }) } diff --git a/crates/api/src/post/save.rs b/crates/api/src/post/save.rs index 231ff1944..53d9de66a 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -18,6 +18,7 @@ impl Perform for SavePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &SavePost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -27,22 +28,21 @@ impl Perform for SavePost { }; if data.save { - PostSaved::save(&mut *context.conn().await?, &post_saved_form) + PostSaved::save(&mut conn, &post_saved_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?; } else { - PostSaved::unsave(&mut *context.conn().await?, &post_saved_form) + PostSaved::unsave(&mut conn, &post_saved_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?; } let post_id = data.post_id; let person_id = local_user_view.person.id; - let post_view = - PostView::read(&mut *context.conn().await?, post_id, Some(person_id), None).await?; + let post_view = PostView::read(&mut conn, post_id, Some(person_id), None).await?; // Mark the post as read - mark_post_as_read(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_read(person_id, post_id, &mut conn).await?; Ok(PostResponse { post_view }) } diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index ca89d32dc..400a4f325 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -22,23 +22,19 @@ impl Perform for CreatePostReport { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreatePostReport = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let reason = self.reason.trim(); check_report_reason(reason, &local_site)?; let person_id = local_user_view.person.id; let post_id = data.post_id; - let post_view = PostView::read(&mut *context.conn().await?, post_id, None, None).await?; + let post_view = PostView::read(&mut conn, post_id, None, None).await?; - check_community_ban( - person_id, - post_view.community.id, - &mut *context.conn().await?, - ) - .await?; + check_community_ban(person_id, post_view.community.id, &mut conn).await?; let report_form = PostReportForm { creator_id: person_id, @@ -49,19 +45,18 @@ impl Perform for CreatePostReport { reason: reason.to_owned(), }; - let report = PostReport::report(&mut *context.conn().await?, &report_form) + let report = PostReport::report(&mut conn, &report_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?; - let post_report_view = - PostReportView::read(&mut *context.conn().await?, report.id, person_id).await?; + let post_report_view = PostReportView::read(&mut conn, report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &post_report_view.creator.name, &post_report_view.post_creator.name, - &mut *context.conn().await?, + &mut conn, context.settings(), ) .await?; diff --git a/crates/api/src/post_report/resolve.rs b/crates/api/src/post_report/resolve.rs index 93c537c91..62c649a1a 100644 --- a/crates/api/src/post_report/resolve.rs +++ b/crates/api/src/post_report/resolve.rs @@ -16,28 +16,28 @@ impl Perform for ResolvePostReport { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &ResolvePostReport = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let report_id = data.report_id; let person_id = local_user_view.person.id; - let report = PostReportView::read(&mut *context.conn().await?, report_id, person_id).await?; + let report = PostReportView::read(&mut conn, report_id, person_id).await?; let person_id = local_user_view.person.id; - is_mod_or_admin(&mut *context.conn().await?, person_id, report.community.id).await?; + is_mod_or_admin(&mut conn, person_id, report.community.id).await?; if data.resolved { - PostReport::resolve(&mut *context.conn().await?, report_id, person_id) + PostReport::resolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } else { - PostReport::unresolve(&mut *context.conn().await?, report_id, person_id) + PostReport::unresolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } - let post_report_view = - PostReportView::read(&mut *context.conn().await?, report_id, person_id).await?; + let post_report_view = PostReportView::read(&mut conn, report_id, person_id).await?; Ok(PostReportResponse { post_report_view }) } diff --git a/crates/api/src/private_message/mark_read.rs b/crates/api/src/private_message/mark_read.rs index 67bf2b3ee..893fca0d2 100644 --- a/crates/api/src/private_message/mark_read.rs +++ b/crates/api/src/private_message/mark_read.rs @@ -21,13 +21,13 @@ impl Perform for MarkPrivateMessageAsRead { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &MarkPrivateMessageAsRead = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + let orig_private_message = PrivateMessage::read(&mut conn, private_message_id).await?; if local_user_view.person.id != orig_private_message.recipient_id { return Err(LemmyError::from_message("couldnt_update_private_message")); } @@ -36,14 +36,14 @@ impl Perform for MarkPrivateMessageAsRead { let private_message_id = data.private_message_id; let read = data.read; PrivateMessage::update( - &mut *context.conn().await?, + &mut conn, private_message_id, &PrivateMessageUpdateForm::builder().read(Some(read)).build(), ) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?; - let view = PrivateMessageView::read(&mut *context.conn().await?, private_message_id).await?; + let view = PrivateMessageView::read(&mut conn, private_message_id).await?; Ok(PrivateMessageResponse { private_message_view: view, }) diff --git a/crates/api/src/private_message_report/create.rs b/crates/api/src/private_message_report/create.rs index 77574cd5e..6305aa649 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -22,16 +22,16 @@ impl Perform for CreatePrivateMessageReport { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let reason = self.reason.trim(); check_report_reason(reason, &local_site)?; let person_id = local_user_view.person.id; let private_message_id = self.private_message_id; - let private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + let private_message = PrivateMessage::read(&mut conn, private_message_id).await?; let report_form = PrivateMessageReportForm { creator_id: person_id, @@ -40,19 +40,18 @@ impl Perform for CreatePrivateMessageReport { reason: reason.to_owned(), }; - let report = PrivateMessageReport::report(&mut *context.conn().await?, &report_form) + let report = PrivateMessageReport::report(&mut conn, &report_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?; - let private_message_report_view = - PrivateMessageReportView::read(&mut *context.conn().await?, report.id).await?; + let private_message_report_view = PrivateMessageReportView::read(&mut conn, report.id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &private_message_report_view.creator.name, &private_message_report_view.private_message_creator.name, - &mut *context.conn().await?, + &mut conn, context.settings(), ) .await?; diff --git a/crates/api/src/private_message_report/resolve.rs b/crates/api/src/private_message_report/resolve.rs index f6ebf3dd4..0c8f5893d 100644 --- a/crates/api/src/private_message_report/resolve.rs +++ b/crates/api/src/private_message_report/resolve.rs @@ -15,6 +15,7 @@ impl Perform for ResolvePrivateMessageReport { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; is_admin(&local_user_view)?; @@ -22,17 +23,16 @@ impl Perform for ResolvePrivateMessageReport { let report_id = self.report_id; let person_id = local_user_view.person.id; if self.resolved { - PrivateMessageReport::resolve(&mut *context.conn().await?, report_id, person_id) + PrivateMessageReport::resolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } else { - PrivateMessageReport::unresolve(&mut *context.conn().await?, report_id, person_id) + PrivateMessageReport::unresolve(&mut conn, report_id, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?; } - let private_message_report_view = - PrivateMessageReportView::read(&mut *context.conn().await?, report_id).await?; + let private_message_report_view = PrivateMessageReportView::read(&mut conn, report_id).await?; Ok(PrivateMessageReportResponse { private_message_report_view, diff --git a/crates/api/src/site/federated_instances.rs b/crates/api/src/site/federated_instances.rs index 0d29214d8..08ba092e3 100644 --- a/crates/api/src/site/federated_instances.rs +++ b/crates/api/src/site/federated_instances.rs @@ -14,7 +14,8 @@ impl Perform for GetFederatedInstances { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let mut conn = context.conn().await?; + let site_view = SiteView::read_local(&mut conn).await?; let federated_instances = build_federated_instances(&site_view.local_site, context.pool()).await?; diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 14cc8986a..eb611ac21 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -25,20 +25,21 @@ impl Perform for LeaveAdmin { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &LeaveAdmin = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; is_admin(&local_user_view)?; // Make sure there isn't just one admin (so if one leaves, there will still be one left) - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let admins = PersonView::admins(&mut conn).await?; if admins.len() == 1 { return Err(LemmyError::from_message("cannot_leave_admin")); } let person_id = local_user_view.person.id; Person::update( - &mut *context.conn().await?, + &mut conn, person_id, &PersonUpdateForm::builder().admin(Some(false)).build(), ) @@ -51,17 +52,16 @@ impl Perform for LeaveAdmin { removed: Some(true), }; - ModAdd::create(&mut *context.conn().await?, &form).await?; + ModAdd::create(&mut conn, &form).await?; // Reread site and admins - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; + let admins = PersonView::admins(&mut conn).await?; - let all_languages = Language::read_all(&mut *context.conn().await?).await?; - let discussion_languages = SiteLanguage::read_local_raw(&mut *context.conn().await?).await?; - let taglines = Tagline::get_all(&mut *context.conn().await?, site_view.local_site.id).await?; - let custom_emojis = - CustomEmojiView::get_all(&mut *context.conn().await?, site_view.local_site.id).await?; + let all_languages = Language::read_all(&mut conn).await?; + let discussion_languages = SiteLanguage::read_local_raw(&mut conn).await?; + let taglines = Tagline::get_all(&mut conn, site_view.local_site.id).await?; + let custom_emojis = CustomEmojiView::get_all(&mut conn, site_view.local_site.id).await?; Ok(GetSiteResponse { site_view, diff --git a/crates/api/src/site/mod_log.rs b/crates/api/src/site/mod_log.rs index 6e3926b24..bfa904f91 100644 --- a/crates/api/src/site/mod_log.rs +++ b/crates/api/src/site/mod_log.rs @@ -37,10 +37,11 @@ impl Perform for GetModlog { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetModlog = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; @@ -56,13 +57,9 @@ impl Perform for GetModlog { None => CommunityId(-1), }; let is_mod_of_community = data.community_id.is_some() - && is_mod_or_admin( - &mut *context.conn().await?, - local_person_id, - community_id_value, - ) - .await - .is_ok(); + && is_mod_or_admin(&mut conn, local_person_id, community_id_value) + .await + .is_ok(); let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin; let mod_person_id = if hide_modlog_names { @@ -80,51 +77,43 @@ impl Perform for GetModlog { hide_modlog_names, }; let removed_posts = match type_ { - All | ModRemovePost => ModRemovePostView::list(&mut *context.conn().await?, params).await?, + All | ModRemovePost => ModRemovePostView::list(&mut conn, params).await?, _ => Default::default(), }; let locked_posts = match type_ { - All | ModLockPost => ModLockPostView::list(&mut *context.conn().await?, params).await?, + All | ModLockPost => ModLockPostView::list(&mut conn, params).await?, _ => Default::default(), }; let featured_posts = match type_ { - All | ModFeaturePost => ModFeaturePostView::list(&mut *context.conn().await?, params).await?, + All | ModFeaturePost => ModFeaturePostView::list(&mut conn, params).await?, _ => Default::default(), }; let removed_comments = match type_ { - All | ModRemoveComment => { - ModRemoveCommentView::list(&mut *context.conn().await?, params).await? - } + All | ModRemoveComment => ModRemoveCommentView::list(&mut conn, params).await?, _ => Default::default(), }; let banned_from_community = match type_ { - All | ModBanFromCommunity => { - ModBanFromCommunityView::list(&mut *context.conn().await?, params).await? - } + All | ModBanFromCommunity => ModBanFromCommunityView::list(&mut conn, params).await?, _ => Default::default(), }; let added_to_community = match type_ { - All | ModAddCommunity => { - ModAddCommunityView::list(&mut *context.conn().await?, params).await? - } + All | ModAddCommunity => ModAddCommunityView::list(&mut conn, params).await?, _ => Default::default(), }; let transferred_to_community = match type_ { - All | ModTransferCommunity => { - ModTransferCommunityView::list(&mut *context.conn().await?, params).await? - } + All | ModTransferCommunity => ModTransferCommunityView::list(&mut conn, params).await?, _ => Default::default(), }; let hidden_communities = match type_ { All | ModHideCommunity if other_person_id.is_none() => { - ModHideCommunityView::list(&mut *context.conn().await?, params).await? + ModHideCommunityView::list(&mut conn, params).await? } _ => Default::default(), }; @@ -141,40 +130,40 @@ impl Perform for GetModlog { ) = if data.community_id.is_none() { ( match type_ { - All | ModBan => ModBanView::list(&mut *context.conn().await?, params).await?, + All | ModBan => ModBanView::list(&mut conn, params).await?, _ => Default::default(), }, match type_ { - All | ModAdd => ModAddView::list(&mut *context.conn().await?, params).await?, + All | ModAdd => ModAddView::list(&mut conn, params).await?, _ => Default::default(), }, match type_ { All | ModRemoveCommunity if other_person_id.is_none() => { - ModRemoveCommunityView::list(&mut *context.conn().await?, params).await? + ModRemoveCommunityView::list(&mut conn, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePerson if other_person_id.is_none() => { - AdminPurgePersonView::list(&mut *context.conn().await?, params).await? + AdminPurgePersonView::list(&mut conn, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeCommunity if other_person_id.is_none() => { - AdminPurgeCommunityView::list(&mut *context.conn().await?, params).await? + AdminPurgeCommunityView::list(&mut conn, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePost if other_person_id.is_none() => { - AdminPurgePostView::list(&mut *context.conn().await?, params).await? + AdminPurgePostView::list(&mut conn, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeComment if other_person_id.is_none() => { - AdminPurgeCommentView::list(&mut *context.conn().await?, params).await? + AdminPurgeCommentView::list(&mut conn, params).await? } _ => Default::default(), }, diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 444f80f20..50ee4f230 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -20,6 +20,7 @@ impl Perform for PurgeComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Self = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -29,13 +30,13 @@ impl Perform for PurgeComment { let comment_id = data.comment_id; // Read the comment to get the post_id - let comment = Comment::read(&mut *context.conn().await?, comment_id).await?; + let comment = Comment::read(&mut conn, comment_id).await?; let post_id = comment.post_id; // TODO read comments for pictrs images and purge them - Comment::delete(&mut *context.conn().await?, comment_id).await?; + Comment::delete(&mut conn, comment_id).await?; // Mod tables let reason = data.reason.clone(); @@ -45,7 +46,7 @@ impl Perform for PurgeComment { post_id, }; - AdminPurgeComment::create(&mut *context.conn().await?, &form).await?; + AdminPurgeComment::create(&mut conn, &form).await?; Ok(PurgeItemResponse { success: true }) } diff --git a/crates/api/src/site/purge/community.rs b/crates/api/src/site/purge/community.rs index 6e81b59e5..949482358 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -21,6 +21,7 @@ impl Perform for PurgeCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Self = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -30,7 +31,7 @@ impl Perform for PurgeCommunity { let community_id = data.community_id; // Read the community to get its images - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; if let Some(banner) = community.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -46,13 +47,13 @@ impl Perform for PurgeCommunity { purge_image_posts_for_community( community_id, - &mut *context.conn().await?, + &mut conn, context.settings(), context.client(), ) .await?; - Community::delete(&mut *context.conn().await?, community_id).await?; + Community::delete(&mut conn, community_id).await?; // Mod tables let reason = data.reason.clone(); @@ -61,7 +62,7 @@ impl Perform for PurgeCommunity { reason, }; - AdminPurgeCommunity::create(&mut *context.conn().await?, &form).await?; + AdminPurgeCommunity::create(&mut conn, &form).await?; Ok(PurgeItemResponse { success: true }) } diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index 923052a90..885f4b1a3 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -21,6 +21,7 @@ impl Perform for PurgePerson { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Self = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -29,7 +30,7 @@ impl Perform for PurgePerson { // Read the person to get their images let person_id = data.person_id; - let person = Person::read(&mut *context.conn().await?, person_id).await?; + let person = Person::read(&mut conn, person_id).await?; if let Some(banner) = person.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -43,15 +44,10 @@ impl Perform for PurgePerson { .ok(); } - purge_image_posts_for_person( - person_id, - &mut *context.conn().await?, - context.settings(), - context.client(), - ) - .await?; + purge_image_posts_for_person(person_id, &mut conn, context.settings(), context.client()) + .await?; - Person::delete(&mut *context.conn().await?, person_id).await?; + Person::delete(&mut conn, person_id).await?; // Mod tables let reason = data.reason.clone(); @@ -60,7 +56,7 @@ impl Perform for PurgePerson { reason, }; - AdminPurgePerson::create(&mut *context.conn().await?, &form).await?; + AdminPurgePerson::create(&mut conn, &form).await?; Ok(PurgeItemResponse { success: true }) } diff --git a/crates/api/src/site/purge/post.rs b/crates/api/src/site/purge/post.rs index 7661a9c24..740c7abc3 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -21,6 +21,7 @@ impl Perform for PurgePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Self = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -30,7 +31,7 @@ impl Perform for PurgePost { let post_id = data.post_id; // Read the post to get the community_id - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; // Purge image if let Some(url) = post.url { @@ -47,7 +48,7 @@ impl Perform for PurgePost { let community_id = post.community_id; - Post::delete(&mut *context.conn().await?, post_id).await?; + Post::delete(&mut conn, post_id).await?; // Mod tables let reason = data.reason.clone(); @@ -57,7 +58,7 @@ impl Perform for PurgePost { community_id, }; - AdminPurgePost::create(&mut *context.conn().await?, &form).await?; + AdminPurgePost::create(&mut conn, &form).await?; Ok(PurgeItemResponse { success: true }) } diff --git a/crates/api/src/site/registration_applications/approve.rs b/crates/api/src/site/registration_applications/approve.rs index 2074c064c..3aeaea9ba 100644 --- a/crates/api/src/site/registration_applications/approve.rs +++ b/crates/api/src/site/registration_applications/approve.rs @@ -21,6 +21,7 @@ impl Perform for ApproveRegistrationApplication { type Response = RegistrationApplicationResponse; async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -37,7 +38,7 @@ impl Perform for ApproveRegistrationApplication { }; let registration_application = - RegistrationApplication::update(&mut *context.conn().await?, app_id, &app_form).await?; + RegistrationApplication::update(&mut conn, app_id, &app_form).await?; // Update the local_user row let local_user_form = LocalUserUpdateForm::builder() @@ -45,16 +46,10 @@ impl Perform for ApproveRegistrationApplication { .build(); let approved_user_id = registration_application.local_user_id; - LocalUser::update( - &mut *context.conn().await?, - approved_user_id, - &local_user_form, - ) - .await?; + LocalUser::update(&mut conn, approved_user_id, &local_user_form).await?; if data.approve { - let approved_local_user_view = - LocalUserView::read(&mut *context.conn().await?, approved_user_id).await?; + let approved_local_user_view = LocalUserView::read(&mut conn, approved_user_id).await?; if approved_local_user_view.local_user.email.is_some() { send_application_approved_email(&approved_local_user_view, context.settings())?; @@ -62,8 +57,7 @@ impl Perform for ApproveRegistrationApplication { } // Read the view - let registration_application = - RegistrationApplicationView::read(&mut *context.conn().await?, app_id).await?; + let registration_application = RegistrationApplicationView::read(&mut conn, app_id).await?; Ok(Self::Response { registration_application, diff --git a/crates/api/src/site/registration_applications/list.rs b/crates/api/src/site/registration_applications/list.rs index aa8771b55..e5a25343e 100644 --- a/crates/api/src/site/registration_applications/list.rs +++ b/crates/api/src/site/registration_applications/list.rs @@ -15,9 +15,10 @@ impl Perform for ListRegistrationApplications { type Response = ListRegistrationApplicationsResponse; async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Make sure user is an admin is_admin(&local_user_view)?; diff --git a/crates/api/src/site/registration_applications/unread_count.rs b/crates/api/src/site/registration_applications/unread_count.rs index 20b3b5567..981dc2604 100644 --- a/crates/api/src/site/registration_applications/unread_count.rs +++ b/crates/api/src/site/registration_applications/unread_count.rs @@ -14,20 +14,18 @@ impl Perform for GetUnreadRegistrationApplicationCount { type Response = GetUnreadRegistrationApplicationCountResponse; async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Only let admins do this is_admin(&local_user_view)?; let verified_email_only = local_site.require_email_verification; - let registration_applications = RegistrationApplicationView::get_unread_count( - &mut *context.conn().await?, - verified_email_only, - ) - .await?; + let registration_applications = + RegistrationApplicationView::get_unread_count(&mut conn, verified_email_only).await?; Ok(Self::Response { registration_applications, diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 61294c53b..71be681a6 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -29,8 +29,10 @@ pub async fn build_comment_response( form_id: Option, recipient_ids: Vec, ) -> Result { + let mut conn = context.conn().await?; + let person_id = local_user_view.map(|l| l.person.id); - let comment_view = CommentView::read(&mut *context.conn().await?, comment_id, person_id).await?; + let comment_view = CommentView::read(&mut conn, comment_id, person_id).await?; Ok(CommentResponse { comment_view, recipient_ids, @@ -43,23 +45,20 @@ pub async fn build_community_response( local_user_view: LocalUserView, community_id: CommunityId, ) -> Result { - let is_mod_or_admin = is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - community_id, - ) - .await - .is_ok(); + let mut conn = context.conn().await?; + + let is_mod_or_admin = is_mod_or_admin(&mut conn, local_user_view.person.id, community_id) + .await + .is_ok(); let person_id = local_user_view.person.id; let community_view = CommunityView::read( - &mut *context.conn().await?, + &mut conn, community_id, Some(person_id), Some(is_mod_or_admin), ) .await?; - let discussion_languages = - CommunityLanguage::read(&mut *context.conn().await?, community_id).await?; + let discussion_languages = CommunityLanguage::read(&mut conn, community_id).await?; Ok(CommunityResponse { community_view, @@ -73,16 +72,13 @@ pub async fn build_post_response( person_id: PersonId, post_id: PostId, ) -> Result { - let is_mod_or_admin = is_mod_or_admin(&mut *context.conn().await?, person_id, community_id) + let mut conn = context.conn().await?; + + let is_mod_or_admin = is_mod_or_admin(&mut conn, person_id, community_id) .await .is_ok(); - let post_view = PostView::read( - &mut *context.conn().await?, - post_id, - Some(person_id), - Some(is_mod_or_admin), - ) - .await?; + let post_view = + PostView::read(&mut conn, post_id, Some(person_id), Some(is_mod_or_admin)).await?; Ok(PostResponse { post_view }) } @@ -96,6 +92,8 @@ pub async fn send_local_notifs( do_send_email: bool, context: &LemmyContext, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; + let mut recipient_ids = Vec::new(); let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname()); @@ -105,7 +103,7 @@ pub async fn send_local_notifs( .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name)) { let mention_name = mention.name.clone(); - let user_view = LocalUserView::read_from_name(&mut *context.conn().await?, &mention_name).await; + let user_view = LocalUserView::read_from_name(&mut conn, &mention_name).await; if let Ok(mention_user_view) = user_view { // TODO // At some point, make it so you can't tag the parent creator either @@ -120,7 +118,7 @@ pub async fn send_local_notifs( // Allow this to fail softly, since comment edits might re-update or replace it // Let the uniqueness handle this fail - PersonMention::create(&mut *context.conn().await?, &user_mention_form) + PersonMention::create(&mut conn, &user_mention_form) .await .ok(); @@ -139,21 +137,19 @@ pub async fn send_local_notifs( // Send comment_reply to the parent commenter / poster if let Some(parent_comment_id) = comment.parent_comment_id() { - let parent_comment = Comment::read(&mut *context.conn().await?, parent_comment_id).await?; + let parent_comment = Comment::read(&mut conn, parent_comment_id).await?; // Get the parent commenter local_user let parent_creator_id = parent_comment.creator_id; // Only add to recipients if that person isn't blocked - let creator_blocked = - check_person_block(person.id, parent_creator_id, &mut *context.conn().await?) - .await - .is_err(); + let creator_blocked = check_person_block(person.id, parent_creator_id, &mut conn) + .await + .is_err(); // Don't send a notif to yourself if parent_comment.creator_id != person.id && !creator_blocked { - let user_view = - LocalUserView::read_person(&mut *context.conn().await?, parent_creator_id).await; + let user_view = LocalUserView::read_person(&mut conn, parent_creator_id).await; if let Ok(parent_user_view) = user_view { recipient_ids.push(parent_user_view.local_user.id); @@ -165,7 +161,7 @@ pub async fn send_local_notifs( // Allow this to fail softly, since comment edits might re-update or replace it // Let the uniqueness handle this fail - CommentReply::create(&mut *context.conn().await?, &comment_reply_form) + CommentReply::create(&mut conn, &comment_reply_form) .await .ok(); @@ -183,14 +179,13 @@ pub async fn send_local_notifs( } else { // If there's no parent, its the post creator // Only add to recipients if that person isn't blocked - let creator_blocked = - check_person_block(person.id, post.creator_id, &mut *context.conn().await?) - .await - .is_err(); + let creator_blocked = check_person_block(person.id, post.creator_id, &mut conn) + .await + .is_err(); if post.creator_id != person.id && !creator_blocked { let creator_id = post.creator_id; - let parent_user = LocalUserView::read_person(&mut *context.conn().await?, creator_id).await; + let parent_user = LocalUserView::read_person(&mut conn, creator_id).await; if let Ok(parent_user_view) = parent_user { recipient_ids.push(parent_user_view.local_user.id); @@ -202,7 +197,7 @@ pub async fn send_local_notifs( // Allow this to fail softly, since comment edits might re-update or replace it // Let the uniqueness handle this fail - CommentReply::create(&mut *context.conn().await?, &comment_reply_form) + CommentReply::create(&mut conn, &comment_reply_form) .await .ok(); diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index f5ffe7d0c..29f307922 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -138,11 +138,12 @@ pub async fn local_user_view_from_jwt( jwt: &str, context: &LemmyContext, ) -> Result { + let mut conn = context.conn().await?; let claims = Claims::decode(jwt, &context.secret().jwt_secret) .map_err(|e| e.with_message("not_logged_in"))? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(&mut *context.conn().await?, local_user_id).await?; + let local_user_view = LocalUserView::read(&mut conn, local_user_id).await?; check_user_valid( local_user_view.person.banned, local_user_view.person.ban_expires, diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 79ed5be2b..5bb1e244c 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -41,9 +41,10 @@ impl PerformCrud for CreateComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreateComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -53,16 +54,11 @@ impl PerformCrud for CreateComment { // Check for a community ban let post_id = data.post_id; - let post = get_post(post_id, &mut *context.conn().await?).await?; + let post = get_post(post_id, &mut conn).await?; let community_id = post.community_id; - check_community_ban( - local_user_view.person.id, - community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, community_id, &mut conn).await?; + check_community_deleted_or_removed(community_id, &mut conn).await?; check_post_deleted_or_removed(&post)?; // Check if post is locked, no new comments @@ -72,9 +68,7 @@ impl PerformCrud for CreateComment { // Fetch the parent, if it exists let parent_opt = if let Some(parent_id) = data.parent_id { - Comment::read(&mut *context.conn().await?, parent_id) - .await - .ok() + Comment::read(&mut conn, parent_id).await.ok() } else { None }; @@ -95,12 +89,8 @@ impl PerformCrud for CreateComment { .unwrap_or(post.language_id); let language_id = data.language_id.unwrap_or(parent_language); - CommunityLanguage::is_allowed_community_language( - &mut *context.conn().await?, - Some(language_id), - community_id, - ) - .await?; + CommunityLanguage::is_allowed_community_language(&mut conn, Some(language_id), community_id) + .await?; let comment_form = CommentInsertForm::builder() .content(content_slurs_removed.clone()) @@ -111,13 +101,9 @@ impl PerformCrud for CreateComment { // Create the comment let parent_path = parent_opt.clone().map(|t| t.path); - let inserted_comment = Comment::create( - &mut *context.conn().await?, - &comment_form, - parent_path.as_ref(), - ) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?; + let inserted_comment = Comment::create(&mut conn, &comment_form, parent_path.as_ref()) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?; // Necessary to update the ap_id let inserted_comment_id = inserted_comment.id; @@ -129,7 +115,7 @@ impl PerformCrud for CreateComment { &protocol_and_hostname, )?; let updated_comment = Comment::update( - &mut *context.conn().await?, + &mut conn, inserted_comment_id, &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -156,18 +142,17 @@ impl PerformCrud for CreateComment { score: 1, }; - CommentLike::like(&mut *context.conn().await?, &like_form) + CommentLike::like(&mut conn, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?; // If its a reply, mark the parent as read if let Some(parent) = parent_opt { let parent_id = parent.id; - let comment_reply = - CommentReply::read_by_comment(&mut *context.conn().await?, parent_id).await; + let comment_reply = CommentReply::read_by_comment(&mut conn, parent_id).await; if let Ok(reply) = comment_reply { CommentReply::update( - &mut *context.conn().await?, + &mut conn, reply.id, &CommentReplyUpdateForm { read: Some(true) }, ) @@ -177,15 +162,11 @@ impl PerformCrud for CreateComment { // If the parent has PersonMentions mark them as read too let person_id = local_user_view.person.id; - let person_mention = PersonMention::read_by_comment_and_person( - &mut *context.conn().await?, - parent_id, - person_id, - ) - .await; + let person_mention = + PersonMention::read_by_comment_and_person(&mut conn, parent_id, person_id).await; if let Ok(mention) = person_mention { PersonMention::update( - &mut *context.conn().await?, + &mut conn, mention.id, &PersonMentionUpdateForm { read: Some(true) }, ) diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index 6f33530ee..5115cb593 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -22,11 +22,12 @@ impl PerformCrud for DeleteComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &DeleteComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let orig_comment = CommentView::read(&mut conn, comment_id, None).await?; // Dont delete it if its already been deleted. if orig_comment.comment.deleted == data.deleted { @@ -36,7 +37,7 @@ impl PerformCrud for DeleteComment { check_community_ban( local_user_view.person.id, orig_comment.community.id, - &mut *context.conn().await?, + &mut conn, ) .await?; @@ -48,7 +49,7 @@ impl PerformCrud for DeleteComment { // Do the delete let deleted = data.deleted; let updated_comment = Comment::update( - &mut *context.conn().await?, + &mut conn, comment_id, &CommentUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -56,7 +57,7 @@ impl PerformCrud for DeleteComment { .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; let post_id = updated_comment.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; let recipient_ids = send_local_notifs( vec![], &updated_comment, diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 8bfeb0d41..210b74b0a 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -15,9 +15,10 @@ impl PerformCrud for GetComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index 090e55e50..56719922a 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -23,22 +23,23 @@ impl PerformCrud for RemoveComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &RemoveComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let orig_comment = CommentView::read(&mut conn, comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - &mut *context.conn().await?, + &mut conn, ) .await?; // Verify that only a mod or admin can remove is_mod_or_admin( - &mut *context.conn().await?, + &mut conn, local_user_view.person.id, orig_comment.community.id, ) @@ -47,7 +48,7 @@ impl PerformCrud for RemoveComment { // Do the remove let removed = data.removed; let updated_comment = Comment::update( - &mut *context.conn().await?, + &mut conn, comment_id, &CommentUpdateForm::builder().removed(Some(removed)).build(), ) @@ -61,10 +62,10 @@ impl PerformCrud for RemoveComment { removed: Some(removed), reason: data.reason.clone(), }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(&mut conn, &form).await?; let post_id = updated_comment.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; let recipient_ids = send_local_notifs( vec![], &updated_comment, diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index d0baa8b36..6db62295b 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -31,17 +31,18 @@ impl PerformCrud for EditComment { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &EditComment = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut *context.conn().await?, comment_id, None).await?; + let orig_comment = CommentView::read(&mut conn, comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - &mut *context.conn().await?, + &mut conn, ) .await?; @@ -52,7 +53,7 @@ impl PerformCrud for EditComment { let language_id = self.language_id; CommunityLanguage::is_allowed_community_language( - &mut *context.conn().await?, + &mut conn, language_id, orig_comment.community.id, ) @@ -72,7 +73,7 @@ impl PerformCrud for EditComment { .language_id(data.language_id) .updated(Some(Some(naive_now()))) .build(); - let updated_comment = Comment::update(&mut *context.conn().await?, comment_id, &form) + let updated_comment = Comment::update(&mut conn, comment_id, &form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index ed441c8fe..f9e2b9c3c 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -46,9 +46,10 @@ impl PerformCrud for CreateCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreateCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let local_site = site_view.local_site; if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() { @@ -75,8 +76,7 @@ impl PerformCrud for CreateCommunity { &data.name, &context.settings().get_protocol_and_hostname(), )?; - let community_dupe = - Community::read_from_apub_id(&mut *context.conn().await?, &community_actor_id).await?; + let community_dupe = Community::read_from_apub_id(&mut conn, &community_actor_id).await?; if community_dupe.is_some() { return Err(LemmyError::from_message("community_already_exists")); } @@ -101,7 +101,7 @@ impl PerformCrud for CreateCommunity { .instance_id(site_view.site.instance_id) .build(); - let inserted_community = Community::create(&mut *context.conn().await?, &community_form) + let inserted_community = Community::create(&mut conn, &community_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?; @@ -111,7 +111,7 @@ impl PerformCrud for CreateCommunity { person_id: local_user_view.person.id, }; - CommunityModerator::join(&mut *context.conn().await?, &community_moderator_form) + CommunityModerator::join(&mut conn, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; @@ -122,21 +122,21 @@ impl PerformCrud for CreateCommunity { pending: false, }; - CommunityFollower::follow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::follow(&mut conn, &community_follower_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?; // Update the discussion_languages if that's provided let community_id = inserted_community.id; if let Some(languages) = data.discussion_languages.clone() { - let site_languages = SiteLanguage::read_local_raw(&mut *context.conn().await?).await?; + let site_languages = SiteLanguage::read_local_raw(&mut conn).await?; // check that community languages are a subset of site languages // https://stackoverflow.com/a/64227550 let is_subset = languages.iter().all(|item| site_languages.contains(item)); if !is_subset { return Err(LemmyError::from_message("language_not_allowed")); } - CommunityLanguage::update(&mut *context.conn().await?, languages, community_id).await?; + CommunityLanguage::update(&mut conn, languages, community_id).await?; } build_community_response(context, local_user_view, community_id).await diff --git a/crates/api_crud/src/community/delete.rs b/crates/api_crud/src/community/delete.rs index cf13f2756..84e555de6 100644 --- a/crates/api_crud/src/community/delete.rs +++ b/crates/api_crud/src/community/delete.rs @@ -19,13 +19,13 @@ impl PerformCrud for DeleteCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &DeleteCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Fetch the community mods let community_id = data.community_id; - let community_mods = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + let community_mods = CommunityModeratorView::for_community(&mut conn, community_id).await?; // Make sure deleter is the top mod is_top_mod(&local_user_view, &community_mods)?; @@ -34,7 +34,7 @@ impl PerformCrud for DeleteCommunity { let community_id = data.community_id; let deleted = data.deleted; Community::update( - &mut *context.conn().await?, + &mut conn, community_id, &CommunityUpdateForm::builder() .deleted(Some(deleted)) diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index 6ab31fd0c..d00db436b 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -18,9 +18,10 @@ impl PerformCrud for ListCommunities { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &ListCommunities = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/api_crud/src/community/remove.rs b/crates/api_crud/src/community/remove.rs index d93800b33..d3c7cae46 100644 --- a/crates/api_crud/src/community/remove.rs +++ b/crates/api_crud/src/community/remove.rs @@ -21,6 +21,7 @@ impl PerformCrud for RemoveCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &RemoveCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -31,7 +32,7 @@ impl PerformCrud for RemoveCommunity { let community_id = data.community_id; let removed = data.removed; Community::update( - &mut *context.conn().await?, + &mut conn, community_id, &CommunityUpdateForm::builder() .removed(Some(removed)) @@ -49,7 +50,7 @@ impl PerformCrud for RemoveCommunity { reason: data.reason.clone(), expires, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(&mut conn, &form).await?; build_community_response(context, local_user_view, community_id).await } diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index 9ffe082f1..9cd60c05a 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -28,9 +28,10 @@ impl PerformCrud for EditCommunity { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &EditCommunity = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let icon = diesel_option_overwrite_to_url(&data.icon)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -43,24 +44,23 @@ impl PerformCrud for EditCommunity { // Verify its a mod (only mods can edit it) let community_id = data.community_id; - let mods: Vec = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id) - .await - .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; + let mods: Vec = CommunityModeratorView::for_community(&mut conn, community_id) + .await + .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; if !mods.contains(&local_user_view.person.id) { return Err(LemmyError::from_message("not_a_moderator")); } let community_id = data.community_id; if let Some(languages) = data.discussion_languages.clone() { - let site_languages = SiteLanguage::read_local_raw(&mut *context.conn().await?).await?; + let site_languages = SiteLanguage::read_local_raw(&mut conn).await?; // check that community languages are a subset of site languages // https://stackoverflow.com/a/64227550 let is_subset = languages.iter().all(|item| site_languages.contains(item)); if !is_subset { return Err(LemmyError::from_message("language_not_allowed")); } - CommunityLanguage::update(&mut *context.conn().await?, languages, community_id).await?; + CommunityLanguage::update(&mut conn, languages, community_id).await?; } let community_form = CommunityUpdateForm::builder() @@ -74,7 +74,7 @@ impl PerformCrud for EditCommunity { .build(); let community_id = data.community_id; - Community::update(&mut *context.conn().await?, community_id, &community_form) + Community::update(&mut conn, community_id, &community_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?; diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index f59e97dfd..8df6be0b6 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -19,10 +19,11 @@ impl PerformCrud for CreateCustomEmoji { #[tracing::instrument(skip(self, context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreateCustomEmoji = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -33,7 +34,7 @@ impl PerformCrud for CreateCustomEmoji { .category(data.category.to_string()) .image_url(data.clone().image_url.into()) .build(); - let emoji = CustomEmoji::create(&mut *context.conn().await?, &emoji_form).await?; + let emoji = CustomEmoji::create(&mut conn, &emoji_form).await?; let mut keywords = vec![]; for keyword in &data.keywords { let keyword_form = CustomEmojiKeywordInsertForm::builder() @@ -42,8 +43,8 @@ impl PerformCrud for CreateCustomEmoji { .build(); keywords.push(keyword_form); } - CustomEmojiKeyword::create(&mut *context.conn().await?, keywords).await?; - let view = CustomEmojiView::get(&mut *context.conn().await?, emoji.id).await?; + CustomEmojiKeyword::create(&mut conn, keywords).await?; + let view = CustomEmojiView::get(&mut conn, emoji.id).await?; Ok(CustomEmojiResponse { custom_emoji: view }) } } diff --git a/crates/api_crud/src/custom_emoji/delete.rs b/crates/api_crud/src/custom_emoji/delete.rs index 57ec855c2..ac9752f68 100644 --- a/crates/api_crud/src/custom_emoji/delete.rs +++ b/crates/api_crud/src/custom_emoji/delete.rs @@ -17,12 +17,13 @@ impl PerformCrud for DeleteCustomEmoji { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &DeleteCustomEmoji = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; - CustomEmoji::delete(&mut *context.conn().await?, data.id).await?; + CustomEmoji::delete(&mut conn, data.id).await?; Ok(DeleteCustomEmojiResponse { id: data.id, success: true, diff --git a/crates/api_crud/src/custom_emoji/update.rs b/crates/api_crud/src/custom_emoji/update.rs index 93ee98668..1d0e6416d 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -19,10 +19,11 @@ impl PerformCrud for EditCustomEmoji { #[tracing::instrument(skip(self, context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &EditCustomEmoji = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -32,8 +33,8 @@ impl PerformCrud for EditCustomEmoji { .category(data.category.to_string()) .image_url(data.clone().image_url.into()) .build(); - let emoji = CustomEmoji::update(&mut *context.conn().await?, data.id, &emoji_form).await?; - CustomEmojiKeyword::delete(&mut *context.conn().await?, data.id).await?; + let emoji = CustomEmoji::update(&mut conn, data.id, &emoji_form).await?; + CustomEmojiKeyword::delete(&mut conn, data.id).await?; let mut keywords = vec![]; for keyword in &data.keywords { let keyword_form = CustomEmojiKeywordInsertForm::builder() @@ -42,8 +43,8 @@ impl PerformCrud for EditCustomEmoji { .build(); keywords.push(keyword_form); } - CustomEmojiKeyword::create(&mut *context.conn().await?, keywords).await?; - let view = CustomEmojiView::get(&mut *context.conn().await?, emoji.id).await?; + CustomEmojiKeyword::create(&mut conn, keywords).await?; + let view = CustomEmojiView::get(&mut conn, emoji.id).await?; Ok(CustomEmojiResponse { custom_emoji: view }) } } diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index b1eaf0925..a7b5bdd82 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -44,9 +44,10 @@ impl PerformCrud for CreatePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreatePost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let slur_regex = local_site_to_slur_regex(&local_site); check_slurs(&data.name, &slur_regex)?; @@ -59,20 +60,15 @@ impl PerformCrud for CreatePost { is_valid_post_title(&data.name)?; is_valid_body_field(&data.body, true)?; - check_community_ban( - local_user_view.person.id, - data.community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(data.community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, data.community_id, &mut conn).await?; + check_community_deleted_or_removed(data.community_id, &mut conn).await?; let community_id = data.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; if community.posting_restricted_to_mods { let community_id = data.community_id; let is_mod = CommunityView::is_mod_or_admin( - &mut *context.conn().await?, + &mut conn, local_user_view.local_user.person_id, community_id, ) @@ -91,21 +87,9 @@ impl PerformCrud for CreatePost { let language_id = match data.language_id { Some(lid) => Some(lid), - None => { - default_post_language( - &mut *context.conn().await?, - community_id, - local_user_view.local_user.id, - ) - .await? - } + None => default_post_language(&mut conn, community_id, local_user_view.local_user.id).await?, }; - CommunityLanguage::is_allowed_community_language( - &mut *context.conn().await?, - language_id, - community_id, - ) - .await?; + CommunityLanguage::is_allowed_community_language(&mut conn, language_id, community_id).await?; let post_form = PostInsertForm::builder() .name(data.name.trim().to_owned()) @@ -121,7 +105,7 @@ impl PerformCrud for CreatePost { .thumbnail_url(thumbnail_url) .build(); - let inserted_post = match Post::create(&mut *context.conn().await?, &post_form).await { + let inserted_post = match Post::create(&mut conn, &post_form).await { Ok(post) => post, Err(e) => { let err_type = if e.to_string() == "value too long for type character varying(200)" { @@ -142,7 +126,7 @@ impl PerformCrud for CreatePost { &protocol_and_hostname, )?; let updated_post = Post::update( - &mut *context.conn().await?, + &mut conn, inserted_post_id, &PostUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -158,12 +142,12 @@ impl PerformCrud for CreatePost { score: 1, }; - PostLike::like(&mut *context.conn().await?, &like_form) + PostLike::like(&mut conn, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?; // Mark the post as read - mark_post_as_read(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_read(person_id, post_id, &mut conn).await?; if let Some(url) = &updated_post.url { let mut webmention = diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index 4f9341647..1206023de 100644 --- a/crates/api_crud/src/post/delete.rs +++ b/crates/api_crud/src/post/delete.rs @@ -18,24 +18,20 @@ impl PerformCrud for DeletePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &DeletePost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; - let orig_post = Post::read(&mut *context.conn().await?, post_id).await?; + let orig_post = Post::read(&mut conn, post_id).await?; // Dont delete it if its already been deleted. if orig_post.deleted == data.deleted { return Err(LemmyError::from_message("couldnt_update_post")); } - check_community_ban( - local_user_view.person.id, - orig_post.community_id, - &mut *context.conn().await?, - ) - .await?; - check_community_deleted_or_removed(orig_post.community_id, &mut *context.conn().await?).await?; + check_community_ban(local_user_view.person.id, orig_post.community_id, &mut conn).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut conn).await?; // Verify that only the creator can delete if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { @@ -46,7 +42,7 @@ impl PerformCrud for DeletePost { let post_id = data.post_id; let deleted = data.deleted; Post::update( - &mut *context.conn().await?, + &mut conn, post_id, &PostUpdateForm::builder().deleted(Some(deleted)).build(), ) diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 232adaf7f..d746dbd61 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -25,9 +25,10 @@ impl PerformCrud for GetPost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetPost = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; @@ -37,7 +38,7 @@ impl PerformCrud for GetPost { let post_id = if let Some(id) = data.id { id } else if let Some(comment_id) = data.comment_id { - Comment::read(&mut *context.conn().await?, comment_id) + Comment::read(&mut conn, comment_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))? .post_id @@ -46,41 +47,27 @@ impl PerformCrud for GetPost { }; // Check to see if the person is a mod or admin, to show deleted / removed - let community_id = Post::read(&mut *context.conn().await?, post_id) - .await? - .community_id; - let is_mod_or_admin = is_mod_or_admin_opt( - &mut *context.conn().await?, - local_user_view.as_ref(), - Some(community_id), - ) - .await - .is_ok(); + let community_id = Post::read(&mut conn, post_id).await?.community_id; + let is_mod_or_admin = + is_mod_or_admin_opt(&mut conn, local_user_view.as_ref(), Some(community_id)) + .await + .is_ok(); - let post_view = PostView::read( - &mut *context.conn().await?, - post_id, - person_id, - Some(is_mod_or_admin), - ) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?; + let post_view = PostView::read(&mut conn, post_id, person_id, Some(is_mod_or_admin)) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?; // Mark the post as read let post_id = post_view.post.id; if let Some(person_id) = person_id { - mark_post_as_read(person_id, post_id, &mut *context.conn().await?).await?; + mark_post_as_read(person_id, post_id, &mut conn).await?; } // Necessary for the sidebar subscribed - let community_view = CommunityView::read( - &mut *context.conn().await?, - community_id, - person_id, - Some(is_mod_or_admin), - ) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; + let community_view = + CommunityView::read(&mut conn, community_id, person_id, Some(is_mod_or_admin)) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; // Insert into PersonPostAggregates // to update the read_comments count @@ -92,18 +79,17 @@ impl PerformCrud for GetPost { read_comments, ..PersonPostAggregatesForm::default() }; - PersonPostAggregates::upsert(&mut *context.conn().await?, &person_post_agg_form) + PersonPostAggregates::upsert(&mut conn, &person_post_agg_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?; } - let moderators = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + let moderators = CommunityModeratorView::for_community(&mut conn, community_id).await?; // Fetch the cross_posts let cross_posts = if let Some(url) = &post_view.post.url { let mut x_posts = PostQuery::builder() - .conn(&mut *context.conn().await?) + .conn(&mut conn) .url_search(Some(url.inner().as_str().into())) .build() .list() diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs index 8ca5d58e9..d4cb193c4 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -21,32 +21,23 @@ impl PerformCrud for RemovePost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &RemovePost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; - let orig_post = Post::read(&mut *context.conn().await?, post_id).await?; + let orig_post = Post::read(&mut conn, post_id).await?; - check_community_ban( - local_user_view.person.id, - orig_post.community_id, - &mut *context.conn().await?, - ) - .await?; + check_community_ban(local_user_view.person.id, orig_post.community_id, &mut conn).await?; // Verify that only the mods can remove - is_mod_or_admin( - &mut *context.conn().await?, - local_user_view.person.id, - orig_post.community_id, - ) - .await?; + is_mod_or_admin(&mut conn, local_user_view.person.id, orig_post.community_id).await?; // Update the post let post_id = data.post_id; let removed = data.removed; Post::update( - &mut *context.conn().await?, + &mut conn, post_id, &PostUpdateForm::builder().removed(Some(removed)).build(), ) @@ -59,7 +50,7 @@ impl PerformCrud for RemovePost { removed: Some(removed), reason: data.reason.clone(), }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(&mut conn, &form).await?; build_post_response( context, diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 839235271..ad25e881e 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -30,9 +30,10 @@ impl PerformCrud for EditPost { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &EditPost = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let data_url = data.url.as_ref(); @@ -52,14 +53,9 @@ impl PerformCrud for EditPost { is_valid_body_field(&data.body, true)?; let post_id = data.post_id; - let orig_post = Post::read(&mut *context.conn().await?, post_id).await?; + let orig_post = Post::read(&mut conn, post_id).await?; - check_community_ban( - local_user_view.person.id, - orig_post.community_id, - &mut *context.conn().await?, - ) - .await?; + check_community_ban(local_user_view.person.id, orig_post.community_id, &mut conn).await?; // Verify that only the creator can edit if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { @@ -76,7 +72,7 @@ impl PerformCrud for EditPost { let language_id = self.language_id; CommunityLanguage::is_allowed_community_language( - &mut *context.conn().await?, + &mut conn, language_id, orig_post.community_id, ) @@ -96,7 +92,7 @@ impl PerformCrud for EditPost { .build(); let post_id = data.post_id; - let res = Post::update(&mut *context.conn().await?, post_id, &post_form).await; + let res = Post::update(&mut conn, post_id, &post_form).await; if let Err(e) = res { let err_type = if e.to_string() == "value too long for type character varying(200)" { "post_title_too_long" diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index a8580906a..ca41b65b6 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -35,9 +35,10 @@ impl PerformCrud for CreatePrivateMessage { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &CreatePrivateMessage = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -45,12 +46,7 @@ impl PerformCrud for CreatePrivateMessage { ); is_valid_body_field(&Some(content_slurs_removed.clone()), false)?; - check_person_block( - local_user_view.person.id, - data.recipient_id, - &mut *context.conn().await?, - ) - .await?; + check_person_block(local_user_view.person.id, data.recipient_id, &mut conn).await?; let private_message_form = PrivateMessageInsertForm::builder() .content(content_slurs_removed.clone()) @@ -59,7 +55,7 @@ impl PerformCrud for CreatePrivateMessage { .build(); let inserted_private_message = - match PrivateMessage::create(&mut *context.conn().await?, &private_message_form).await { + match PrivateMessage::create(&mut conn, &private_message_form).await { Ok(private_message) => private_message, Err(e) => { return Err(LemmyError::from_error_message( @@ -77,7 +73,7 @@ impl PerformCrud for CreatePrivateMessage { &protocol_and_hostname, )?; PrivateMessage::update( - &mut *context.conn().await?, + &mut conn, inserted_private_message.id, &PrivateMessageUpdateForm::builder() .ap_id(Some(apub_id)) @@ -86,14 +82,12 @@ impl PerformCrud for CreatePrivateMessage { .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_private_message"))?; - let view = - PrivateMessageView::read(&mut *context.conn().await?, inserted_private_message.id).await?; + let view = PrivateMessageView::read(&mut conn, inserted_private_message.id).await?; // Send email to the local recipient, if one exists if view.recipient.local { let recipient_id = data.recipient_id; - let local_recipient = - LocalUserView::read_person(&mut *context.conn().await?, recipient_id).await?; + let local_recipient = LocalUserView::read_person(&mut conn, recipient_id).await?; let lang = get_interface_language(&local_recipient); let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname()); let sender_name = &local_user_view.person.name; diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index f58e0d9d7..35e4b2158 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -21,13 +21,13 @@ impl PerformCrud for DeletePrivateMessage { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &DeletePrivateMessage = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + let orig_private_message = PrivateMessage::read(&mut conn, private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { return Err(LemmyError::from_message("no_private_message_edit_allowed")); } @@ -36,7 +36,7 @@ impl PerformCrud for DeletePrivateMessage { let private_message_id = data.private_message_id; let deleted = data.deleted; PrivateMessage::update( - &mut *context.conn().await?, + &mut conn, private_message_id, &PrivateMessageUpdateForm::builder() .deleted(Some(deleted)) @@ -45,7 +45,7 @@ impl PerformCrud for DeletePrivateMessage { .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?; - let view = PrivateMessageView::read(&mut *context.conn().await?, private_message_id).await?; + let view = PrivateMessageView::read(&mut conn, private_message_id).await?; Ok(PrivateMessageResponse { private_message_view: view, }) diff --git a/crates/api_crud/src/private_message/read.rs b/crates/api_crud/src/private_message/read.rs index 92b694be6..ddd3e83a1 100644 --- a/crates/api_crud/src/private_message/read.rs +++ b/crates/api_crud/src/private_message/read.rs @@ -17,6 +17,7 @@ impl PerformCrud for GetPrivateMessages { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &GetPrivateMessages = self; let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?; let person_id = local_user_view.person.id; @@ -25,7 +26,7 @@ impl PerformCrud for GetPrivateMessages { let limit = data.limit; let unread_only = data.unread_only; let mut messages = PrivateMessageQuery::builder() - .conn(&mut *context.conn().await?) + .conn(&mut conn) .recipient_id(person_id) .page(page) .limit(limit) diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index bc2e834c4..064c33bc9 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -28,14 +28,14 @@ impl PerformCrud for EditPrivateMessage { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &EditPrivateMessage = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + let orig_private_message = PrivateMessage::read(&mut conn, private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { return Err(LemmyError::from_message("no_private_message_edit_allowed")); } @@ -46,7 +46,7 @@ impl PerformCrud for EditPrivateMessage { let private_message_id = data.private_message_id; PrivateMessage::update( - &mut *context.conn().await?, + &mut conn, private_message_id, &PrivateMessageUpdateForm::builder() .content(Some(content_slurs_removed)) @@ -56,7 +56,7 @@ impl PerformCrud for EditPrivateMessage { .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?; - let view = PrivateMessageView::read(&mut *context.conn().await?, private_message_id).await?; + let view = PrivateMessageView::read(&mut conn, private_message_id).await?; Ok(PrivateMessageResponse { private_message_view: view, diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index abd3af808..ed5c86067 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -47,10 +47,11 @@ impl PerformCrud for CreateSite { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &CreateSite = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; // Make sure user is an admin; other types of users should not create site data... is_admin(&local_user_view)?; @@ -75,7 +76,7 @@ impl PerformCrud for CreateSite { let site_id = local_site.site_id; - Site::update(&mut *context.conn().await?, site_id, &site_form).await?; + Site::update(&mut conn, site_id, &site_form).await?; let local_site_form = LocalSiteUpdateForm::builder() // Set the site setup to true @@ -100,7 +101,7 @@ impl PerformCrud for CreateSite { .captcha_difficulty(data.captcha_difficulty.clone()) .build(); - LocalSite::update(&mut *context.conn().await?, &local_site_form).await?; + LocalSite::update(&mut conn, &local_site_form).await?; let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder() .message(data.rate_limit_message) @@ -117,13 +118,12 @@ impl PerformCrud for CreateSite { .search_per_second(data.rate_limit_search_per_second) .build(); - LocalSiteRateLimit::update(&mut *context.conn().await?, &local_site_rate_limit_form).await?; + LocalSiteRateLimit::update(&mut conn, &local_site_rate_limit_form).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let new_taglines = data.taglines.clone(); - let taglines = - Tagline::replace(&mut *context.conn().await?, local_site.id, new_taglines).await?; + let taglines = Tagline::replace(&mut conn, local_site.id, new_taglines).await?; let rate_limit_config = local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit); diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 491311048..835b569e6 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -30,11 +30,12 @@ impl PerformCrud for GetSite { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetSite = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let admins = PersonView::admins(&mut conn).await?; // Build the local user let my_user = if let Some(local_user_view) = @@ -43,28 +44,27 @@ impl PerformCrud for GetSite { let person_id = local_user_view.person.id; let local_user_id = local_user_view.local_user.id; - let follows = CommunityFollowerView::for_person(&mut *context.conn().await?, person_id) + let follows = CommunityFollowerView::for_person(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; let person_id = local_user_view.person.id; - let community_blocks = CommunityBlockView::for_person(&mut *context.conn().await?, person_id) + let community_blocks = CommunityBlockView::for_person(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; let person_id = local_user_view.person.id; - let person_blocks = PersonBlockView::for_person(&mut *context.conn().await?, person_id) + let person_blocks = PersonBlockView::for_person(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; - let moderates = CommunityModeratorView::for_person(&mut *context.conn().await?, person_id) + let moderates = CommunityModeratorView::for_person(&mut conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; - let discussion_languages = - LocalUserLanguage::read(&mut *context.conn().await?, local_user_id) - .await - .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; + let discussion_languages = LocalUserLanguage::read(&mut conn, local_user_id) + .await + .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; Some(MyUserInfo { local_user_view, @@ -78,11 +78,10 @@ impl PerformCrud for GetSite { None }; - let all_languages = Language::read_all(&mut *context.conn().await?).await?; - let discussion_languages = SiteLanguage::read_local_raw(&mut *context.conn().await?).await?; - let taglines = Tagline::get_all(&mut *context.conn().await?, site_view.local_site.id).await?; - let custom_emojis = - CustomEmojiView::get_all(&mut *context.conn().await?, site_view.local_site.id).await?; + let all_languages = Language::read_all(&mut conn).await?; + let discussion_languages = SiteLanguage::read_local_raw(&mut conn).await?; + let taglines = Tagline::get_all(&mut conn, site_view.local_site.id).await?; + let custom_emojis = CustomEmojiView::get_all(&mut conn, site_view.local_site.id).await?; Ok(GetSiteResponse { site_view, diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 32115aea1..377e38cd3 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -44,9 +44,10 @@ impl PerformCrud for EditSite { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &EditSite = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let local_site = site_view.local_site; let site = site_view.site; @@ -56,12 +57,7 @@ impl PerformCrud for EditSite { validate_update_payload(&local_site, data)?; if let Some(discussion_languages) = data.discussion_languages.clone() { - SiteLanguage::update( - &mut *context.conn().await?, - discussion_languages.clone(), - &site, - ) - .await?; + SiteLanguage::update(&mut conn, discussion_languages.clone(), &site).await?; } let site_form = SiteUpdateForm::builder() @@ -73,7 +69,7 @@ impl PerformCrud for EditSite { .updated(Some(Some(naive_now()))) .build(); - Site::update(&mut *context.conn().await?, site.id, &site_form) + Site::update(&mut conn, site.id, &site_form) .await // Ignore errors for all these, so as to not throw errors if no update occurs // Diesel will throw an error for empty update forms @@ -101,9 +97,7 @@ impl PerformCrud for EditSite { .reports_email_admins(data.reports_email_admins) .build(); - let update_local_site = LocalSite::update(&mut *context.conn().await?, &local_site_form) - .await - .ok(); + let update_local_site = LocalSite::update(&mut conn, &local_site_form).await.ok(); let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder() .message(data.rate_limit_message) @@ -120,15 +114,15 @@ impl PerformCrud for EditSite { .search_per_second(data.rate_limit_search_per_second) .build(); - LocalSiteRateLimit::update(&mut *context.conn().await?, &local_site_rate_limit_form) + LocalSiteRateLimit::update(&mut conn, &local_site_rate_limit_form) .await .ok(); // Replace the blocked and allowed instances let allowed = data.allowed_instances.clone(); - FederationAllowList::replace(&mut *context.conn().await?, allowed).await?; + FederationAllowList::replace(&mut conn, allowed).await?; let blocked = data.blocked_instances.clone(); - FederationBlockList::replace(&mut *context.conn().await?, blocked).await?; + FederationBlockList::replace(&mut conn, blocked).await?; // TODO can't think of a better way to do this. // If the server suddenly requires email verification, or required applications, no old users @@ -142,7 +136,7 @@ impl PerformCrud for EditSite { .map(|ols| ols.registration_mode == RegistrationMode::RequireApplication) .unwrap_or(false); if !old_require_application && new_require_application { - LocalUser::set_all_users_registration_applications_accepted(&mut *context.conn().await?) + LocalUser::set_all_users_registration_applications_accepted(&mut conn) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?; } @@ -152,16 +146,15 @@ impl PerformCrud for EditSite { .map(|ols| ols.require_email_verification) .unwrap_or(false); if !local_site.require_email_verification && new_require_email_verification { - LocalUser::set_all_users_email_verified(&mut *context.conn().await?) + LocalUser::set_all_users_email_verified(&mut conn) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?; } let new_taglines = data.taglines.clone(); - let taglines = - Tagline::replace(&mut *context.conn().await?, local_site.id, new_taglines).await?; + let taglines = Tagline::replace(&mut conn, local_site.id, new_taglines).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let rate_limit_config = local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit); diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 9092376b6..ab00149d4 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -43,9 +43,10 @@ impl PerformCrud for Register { #[tracing::instrument(skip(self, context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Register = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(&mut conn).await?; let local_site = site_view.local_site; let require_registration_application = local_site.registration_mode == RegistrationMode::RequireApplication; @@ -104,7 +105,7 @@ impl PerformCrud for Register { )?; if let Some(email) = &data.email { - if LocalUser::is_email_taken(&mut *context.conn().await?, email).await? { + if LocalUser::is_email_taken(&mut conn, email).await? { return Err(LemmyError::from_message("email_already_exists")); } } @@ -125,7 +126,7 @@ impl PerformCrud for Register { .build(); // insert the person - let inserted_person = Person::create(&mut *context.conn().await?, &person_form) + let inserted_person = Person::create(&mut conn, &person_form) .await .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?; @@ -142,8 +143,7 @@ impl PerformCrud for Register { .accepted_application(accepted_application) .build(); - let inserted_local_user = - LocalUser::create(&mut *context.conn().await?, &local_user_form).await?; + let inserted_local_user = LocalUser::create(&mut conn, &local_user_form).await?; if local_site.site_setup && require_registration_application { // Create the registration application @@ -153,17 +153,12 @@ impl PerformCrud for Register { answer: data.answer.clone().expect("must have an answer"), }; - RegistrationApplication::create(&mut *context.conn().await?, &form).await?; + RegistrationApplication::create(&mut conn, &form).await?; } // Email the admins if local_site.application_email_admins { - send_new_applicant_email_to_admins( - &data.username, - &mut *context.conn().await?, - context.settings(), - ) - .await?; + send_new_applicant_email_to_admins(&data.username, &mut conn, context.settings()).await?; } let mut login_response = LoginResponse { @@ -198,13 +193,7 @@ impl PerformCrud for Register { .clone() .expect("email was provided"); - send_verification_email( - &local_user_view, - &email, - &mut *context.conn().await?, - context.settings(), - ) - .await?; + send_verification_email(&local_user_view, &email, &mut conn, context.settings()).await?; login_response.verify_email_sent = true; } diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index a143344b3..3064ab85c 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -51,6 +51,8 @@ impl BlockUser { expires: Option, context: &Data, ) -> Result { + let mut conn = context.conn().await?; + let audience = if let SiteOrCommunity::Community(c) = target { Some(c.id().into()) } else { @@ -60,7 +62,7 @@ impl BlockUser { actor: mod_.id().into(), to: vec![public()], object: user.id().into(), - cc: generate_cc(target, &mut *context.conn().await?).await?, + cc: generate_cc(target, &mut conn).await?, target: target.id(), kind: BlockType::Block, remove_data, @@ -84,6 +86,8 @@ impl BlockUser { expires: Option, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; + let block = BlockUser::new( target, user, @@ -97,7 +101,7 @@ impl BlockUser { match target { SiteOrCommunity::Site(_) => { - let inboxes = remote_instance_inboxes(&mut *context.conn().await?).await?; + let inboxes = remote_instance_inboxes(&mut conn).await?; send_lemmy_activity(context, block, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -147,6 +151,7 @@ impl ActivityHandler for BlockUser { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let expires = self.expires.map(|u| u.naive_local()); let mod_person = self.actor.dereference(context).await?; @@ -155,7 +160,7 @@ impl ActivityHandler for BlockUser { match target { SiteOrCommunity::Site(_site) => { let blocked_person = Person::update( - &mut *context.conn().await?, + &mut conn, blocked_person.id, &PersonUpdateForm::builder() .banned(Some(true)) @@ -166,7 +171,7 @@ impl ActivityHandler for BlockUser { if self.remove_data.unwrap_or(false) { remove_user_data( blocked_person.id, - &mut *context.conn().await?, + &mut conn, context.settings(), context.client(), ) @@ -181,7 +186,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(&mut conn, &form).await?; } SiteOrCommunity::Community(community) => { let community_user_ban_form = CommunityPersonBanForm { @@ -189,7 +194,7 @@ impl ActivityHandler for BlockUser { person_id: blocked_person.id, expires: Some(expires), }; - CommunityPersonBan::ban(&mut *context.conn().await?, &community_user_ban_form).await?; + CommunityPersonBan::ban(&mut conn, &community_user_ban_form).await?; // Also unsubscribe them from the community, if they are subscribed let community_follower_form = CommunityFollowerForm { @@ -197,17 +202,12 @@ impl ActivityHandler for BlockUser { person_id: blocked_person.id, pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(&mut conn, &community_follower_form) .await .ok(); if self.remove_data.unwrap_or(false) { - remove_user_data_in_community( - community.id, - blocked_person.id, - &mut *context.conn().await?, - ) - .await?; + remove_user_data_in_community(community.id, blocked_person.id, &mut conn).await?; } // write to mod log @@ -219,7 +219,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(&mut conn, &form).await?; } } diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index 99c2f69a2..be03751a6 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -138,14 +138,10 @@ impl SendActivity for BanPerson { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let person = Person::read(&mut *context.conn().await?, request.person_id).await?; - let site = SiteOrCommunity::Site( - SiteView::read_local(&mut *context.conn().await?) - .await? - .site - .into(), - ); + let person = Person::read(&mut conn, request.person_id).await?; + let site = SiteOrCommunity::Site(SiteView::read_local(&mut conn).await?.site.into()); let expires = request.expires.map(naive_from_unix); // if the action affects a local user, federate to other instances @@ -186,14 +182,12 @@ impl SendActivity for BanFromCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community: ApubCommunity = - Community::read(&mut *context.conn().await?, request.community_id) - .await? - .into(); - let banned_person: ApubPerson = Person::read(&mut *context.conn().await?, request.person_id) + let community: ApubCommunity = Community::read(&mut conn, request.community_id) .await? .into(); + let banned_person: ApubPerson = Person::read(&mut conn, request.person_id).await?.into(); let expires = request.expires.map(naive_from_unix); if request.ban { diff --git a/crates/apub/src/activities/block/undo_block_user.rs b/crates/apub/src/activities/block/undo_block_user.rs index 2db1e0ce0..a5a6f98e9 100644 --- a/crates/apub/src/activities/block/undo_block_user.rs +++ b/crates/apub/src/activities/block/undo_block_user.rs @@ -38,6 +38,7 @@ impl UndoBlockUser { reason: Option, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let block = BlockUser::new(target, user, mod_, None, reason, None, context).await?; let audience = if let SiteOrCommunity::Community(c) = target { Some(c.id().into()) @@ -53,7 +54,7 @@ impl UndoBlockUser { actor: mod_.id().into(), to: vec![public()], object: block, - cc: generate_cc(target, &mut *context.conn().await?).await?, + cc: generate_cc(target, &mut conn).await?, kind: UndoType::Undo, id: id.clone(), audience, @@ -62,7 +63,7 @@ impl UndoBlockUser { let mut inboxes = vec![user.shared_inbox_or_inbox()]; match target { SiteOrCommunity::Site(_) => { - inboxes.append(&mut remote_instance_inboxes(&mut *context.conn().await?).await?); + inboxes.append(&mut remote_instance_inboxes(&mut conn).await?); send_lemmy_activity(context, undo, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -96,6 +97,7 @@ impl ActivityHandler for UndoBlockUser { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let expires = self.object.expires.map(|u| u.naive_local()); let mod_person = self.actor.dereference(context).await?; @@ -103,7 +105,7 @@ impl ActivityHandler for UndoBlockUser { match self.object.target.dereference(context).await? { SiteOrCommunity::Site(_site) => { let blocked_person = Person::update( - &mut *context.conn().await?, + &mut conn, blocked_person.id, &PersonUpdateForm::builder() .banned(Some(false)) @@ -120,7 +122,7 @@ impl ActivityHandler for UndoBlockUser { banned: Some(false), expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(&mut conn, &form).await?; } SiteOrCommunity::Community(community) => { let community_user_ban_form = CommunityPersonBanForm { @@ -128,7 +130,7 @@ impl ActivityHandler for UndoBlockUser { person_id: blocked_person.id, expires: None, }; - CommunityPersonBan::unban(&mut *context.conn().await?, &community_user_ban_form).await?; + CommunityPersonBan::unban(&mut conn, &community_user_ban_form).await?; // write to mod log let form = ModBanFromCommunityForm { @@ -139,7 +141,7 @@ impl ActivityHandler for UndoBlockUser { banned: Some(false), expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(&mut conn, &form).await?; } } diff --git a/crates/apub/src/activities/community/collection_add.rs b/crates/apub/src/activities/community/collection_add.rs index 2936b83ed..858ffff0c 100644 --- a/crates/apub/src/activities/community/collection_add.rs +++ b/crates/apub/src/activities/community/collection_add.rs @@ -117,9 +117,10 @@ impl ActivityHandler for CollectionAdd { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let (community, collection_type) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.target.into()).await?; + Community::get_by_collection_url(&mut conn, &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let new_mod = ObjectId::::from(self.object) @@ -129,17 +130,14 @@ impl ActivityHandler for CollectionAdd { // If we had to refetch the community while parsing the activity, then the new mod has already // been added. Skip it here as it would result in a duplicate key error. let new_mod_id = new_mod.id; - let moderated_communities = CommunityModerator::get_person_moderated_communities( - &mut *context.conn().await?, - new_mod_id, - ) - .await?; + let moderated_communities = + CommunityModerator::get_person_moderated_communities(&mut conn, new_mod_id).await?; if !moderated_communities.contains(&community.id) { let form = CommunityModeratorForm { community_id: community.id, person_id: new_mod.id, }; - CommunityModerator::join(&mut *context.conn().await?, &form).await?; + CommunityModerator::join(&mut conn, &form).await?; // write mod log let actor = self.actor.dereference(context).await?; @@ -149,7 +147,7 @@ impl ActivityHandler for CollectionAdd { community_id: community.id, removed: Some(false), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(&mut conn, &form).await?; } // TODO: send websocket notification about added mod } @@ -160,7 +158,7 @@ impl ActivityHandler for CollectionAdd { let form = PostUpdateForm::builder() .featured_community(Some(true)) .build(); - Post::update(&mut *context.conn().await?, post.id, &form).await?; + Post::update(&mut conn, post.id, &form).await?; } } Ok(()) @@ -176,14 +174,12 @@ impl SendActivity for AddModToCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community: ApubCommunity = - Community::read(&mut *context.conn().await?, request.community_id) - .await? - .into(); - let updated_mod: ApubPerson = Person::read(&mut *context.conn().await?, request.person_id) + let community: ApubCommunity = Community::read(&mut conn, request.community_id) .await? .into(); + let updated_mod: ApubPerson = Person::read(&mut conn, request.person_id).await?.into(); if request.added { CollectionAdd::send_add_mod( &community, @@ -213,8 +209,9 @@ impl SendActivity for FeaturePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, response.post_view.community.id) + let community = Community::read(&mut conn, response.post_view.community.id) .await? .into(); let post = response.post_view.post.clone().into(); diff --git a/crates/apub/src/activities/community/collection_remove.rs b/crates/apub/src/activities/community/collection_remove.rs index bababf351..9502db7c8 100644 --- a/crates/apub/src/activities/community/collection_remove.rs +++ b/crates/apub/src/activities/community/collection_remove.rs @@ -110,9 +110,10 @@ impl ActivityHandler for CollectionRemove { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let (community, collection_type) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.target.into()).await?; + Community::get_by_collection_url(&mut conn, &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let remove_mod = ObjectId::::from(self.object) @@ -123,7 +124,7 @@ impl ActivityHandler for CollectionRemove { community_id: community.id, person_id: remove_mod.id, }; - CommunityModerator::leave(&mut *context.conn().await?, &form).await?; + CommunityModerator::leave(&mut conn, &form).await?; // write mod log let actor = self.actor.dereference(context).await?; @@ -133,7 +134,7 @@ impl ActivityHandler for CollectionRemove { community_id: community.id, removed: Some(true), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(&mut conn, &form).await?; // TODO: send websocket notification about removed mod } @@ -144,7 +145,7 @@ impl ActivityHandler for CollectionRemove { let form = PostUpdateForm::builder() .featured_community(Some(false)) .build(); - Post::update(&mut *context.conn().await?, post.id, &form).await?; + Post::update(&mut conn, post.id, &form).await?; } } Ok(()) diff --git a/crates/apub/src/activities/community/lock_page.rs b/crates/apub/src/activities/community/lock_page.rs index 12449e9b3..2e7c1839e 100644 --- a/crates/apub/src/activities/community/lock_page.rs +++ b/crates/apub/src/activities/community/lock_page.rs @@ -58,9 +58,10 @@ impl ActivityHandler for LockPage { } async fn receive(self, context: &Data) -> Result<(), Self::Error> { + let mut conn = context.conn().await?; let form = PostUpdateForm::builder().locked(Some(true)).build(); let post = self.object.dereference(context).await?; - Post::update(&mut *context.conn().await?, post.id, &form).await?; + Post::update(&mut conn, post.id, &form).await?; Ok(()) } } @@ -94,10 +95,11 @@ impl ActivityHandler for UndoLockPage { } async fn receive(self, context: &Data) -> Result<(), Self::Error> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let form = PostUpdateForm::builder().locked(Some(false)).build(); let post = self.object.object.dereference(context).await?; - Post::update(&mut *context.conn().await?, post.id, &form).await?; + Post::update(&mut conn, post.id, &form).await?; Ok(()) } } @@ -111,6 +113,7 @@ impl SendActivity for LockPost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let id = generate_activity_id( LockType::Lock, @@ -145,8 +148,7 @@ impl SendActivity for LockPost { }; AnnouncableActivities::UndoLockPost(undo) }; - let community = - Community::read(&mut *context.conn().await?, response.post_view.community.id).await?; + let community = Community::read(&mut conn, response.post_view.community.id).await?; send_activity_in_community( activity, &local_user_view.person.into(), diff --git a/crates/apub/src/activities/community/mod.rs b/crates/apub/src/activities/community/mod.rs index 4e665e338..fa48b99e4 100644 --- a/crates/apub/src/activities/community/mod.rs +++ b/crates/apub/src/activities/community/mod.rs @@ -38,13 +38,13 @@ pub(crate) async fn send_activity_in_community( is_mod_action: bool, context: &Data, ) -> Result<(), LemmyError> { - // send to any users which are mentioned or affected directly + let mut conn = context.conn().await?; // send to any users which are mentioned or affected directly let mut inboxes = extra_inboxes; // send to user followers if !is_mod_action { inboxes.extend( - &mut PersonFollower::list_followers(&mut *context.conn().await?, actor.id) + &mut PersonFollower::list_followers(&mut conn, actor.id) .await? .into_iter() .map(|p| ApubPerson(p).shared_inbox_or_inbox()), diff --git a/crates/apub/src/activities/community/report.rs b/crates/apub/src/activities/community/report.rs index 287f27721..3babbfa26 100644 --- a/crates/apub/src/activities/community/report.rs +++ b/crates/apub/src/activities/community/report.rs @@ -122,6 +122,7 @@ impl ActivityHandler for Report { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, true, context).await?; let actor = self.actor.dereference(context).await?; match self.object.dereference(context).await? { @@ -134,7 +135,7 @@ impl ActivityHandler for Report { reason: self.summary, original_post_body: post.body.clone(), }; - PostReport::report(&mut *context.conn().await?, &report_form).await?; + PostReport::report(&mut conn, &report_form).await?; } PostOrComment::Comment(comment) => { let report_form = CommentReportForm { @@ -143,7 +144,7 @@ impl ActivityHandler for Report { original_comment_text: comment.content.clone(), reason: self.summary, }; - CommentReport::report(&mut *context.conn().await?, &report_form).await?; + CommentReport::report(&mut conn, &report_form).await?; } }; Ok(()) diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index 0f7ae297b..7f32dd55c 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -35,8 +35,9 @@ impl SendActivity for EditCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, request.community_id).await?; + let community = Community::read(&mut conn, request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } } @@ -92,17 +93,13 @@ impl ActivityHandler for UpdateCommunity { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let community = self.community(context).await?; let community_update_form = self.object.into_update_form(); - Community::update( - &mut *context.conn().await?, - community.id, - &community_update_form, - ) - .await?; + Community::update(&mut conn, community.id, &community_update_form).await?; Ok(()) } } @@ -116,8 +113,9 @@ impl SendActivity for HideCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, request.community_id).await?; + let community = Community::read(&mut conn, request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } } diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index f144df8e8..da32eee28 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -89,16 +89,12 @@ impl CreateOrUpdateNote { kind: CreateOrUpdateType, context: &Data, ) -> Result<(), LemmyError> { - // TODO: might be helpful to add a comment method to retrieve community directly + let mut conn = context.conn().await?; // TODO: might be helpful to add a comment method to retrieve community directly let post_id = comment.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; let community_id = post.community_id; - let person: ApubPerson = Person::read(&mut *context.conn().await?, person_id) - .await? - .into(); - let community: ApubCommunity = Community::read(&mut *context.conn().await?, community_id) - .await? - .into(); + let person: ApubPerson = Person::read(&mut conn, person_id).await?.into(); + let community: ApubCommunity = Community::read(&mut conn, community_id).await?.into(); let id = generate_activity_id( kind.clone(), @@ -171,6 +167,7 @@ impl ActivityHandler for CreateOrUpdateNote { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; // Need to do this check here instead of Note::from_json because we need the person who // send the activity, not the comment author. @@ -181,7 +178,7 @@ impl ActivityHandler for CreateOrUpdateNote { if distinguished != existing_comment.distinguished { let creator = self.actor.dereference(context).await?; let (post, _) = self.object.get_parents(context).await?; - is_mod_or_admin(&mut *context.conn().await?, creator.id, post.community_id).await?; + is_mod_or_admin(&mut conn, creator.id, post.community_id).await?; } } @@ -194,14 +191,14 @@ impl ActivityHandler for CreateOrUpdateNote { person_id: comment.creator_id, score: 1, }; - CommentLike::like(&mut *context.conn().await?, &like_form).await?; + CommentLike::like(&mut conn, &like_form).await?; // Calculate initial hot_rank - CommentAggregates::update_hot_rank(&mut *context.conn().await?, comment.id).await?; + CommentAggregates::update_hot_rank(&mut conn, comment.id).await?; let do_send_email = self.kind == CreateOrUpdateType::Create; let post_id = comment.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; let actor = self.actor.dereference(context).await?; // Note: diff --git a/crates/apub/src/activities/create_or_update/post.rs b/crates/apub/src/activities/create_or_update/post.rs index a59b4de4c..a43788288 100644 --- a/crates/apub/src/activities/create_or_update/post.rs +++ b/crates/apub/src/activities/create_or_update/post.rs @@ -107,14 +107,11 @@ impl CreateOrUpdatePage { kind: CreateOrUpdateType, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let post = ApubPost(post.clone()); let community_id = post.community_id; - let person: ApubPerson = Person::read(&mut *context.conn().await?, person_id) - .await? - .into(); - let community: ApubCommunity = Community::read(&mut *context.conn().await?, community_id) - .await? - .into(); + let person: ApubPerson = Person::read(&mut conn, person_id).await?.into(); + let community: ApubCommunity = Community::read(&mut conn, community_id).await?.into(); let create_or_update = CreateOrUpdatePage::new(post, &person, &community, kind, context).await?; @@ -182,6 +179,7 @@ impl ActivityHandler for CreateOrUpdatePage { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let post = ApubPost::from_json(self.object, context).await?; @@ -191,10 +189,10 @@ impl ActivityHandler for CreateOrUpdatePage { person_id: post.creator_id, score: 1, }; - PostLike::like(&mut *context.conn().await?, &like_form).await?; + PostLike::like(&mut conn, &like_form).await?; // Calculate initial hot_rank for post - PostAggregates::update_hot_rank(&mut *context.conn().await?, post.id).await?; + PostAggregates::update_hot_rank(&mut conn, post.id).await?; Ok(()) } diff --git a/crates/apub/src/activities/create_or_update/private_message.rs b/crates/apub/src/activities/create_or_update/private_message.rs index 4075a6c97..c34c68995 100644 --- a/crates/apub/src/activities/create_or_update/private_message.rs +++ b/crates/apub/src/activities/create_or_update/private_message.rs @@ -70,13 +70,10 @@ impl CreateOrUpdateChatMessage { kind: CreateOrUpdateType, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let recipient_id = private_message.recipient_id; - let sender: ApubPerson = Person::read(&mut *context.conn().await?, sender_id) - .await? - .into(); - let recipient: ApubPerson = Person::read(&mut *context.conn().await?, recipient_id) - .await? - .into(); + let sender: ApubPerson = Person::read(&mut conn, sender_id).await?.into(); + let recipient: ApubPerson = Person::read(&mut conn, recipient_id).await?.into(); let id = generate_activity_id( kind.clone(), diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index 1685fd87e..692d1d6c5 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -105,6 +105,7 @@ pub(in crate::activities) async fn receive_remove_action( reason: Option, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; match DeletableObjects::read_from_db(object, context).await? { DeletableObjects::Community(community) => { if community.local { @@ -119,9 +120,9 @@ pub(in crate::activities) async fn receive_remove_action( reason, expires: None, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(&mut conn, &form).await?; Community::update( - &mut *context.conn().await?, + &mut conn, community.id, &CommunityUpdateForm::builder().removed(Some(true)).build(), ) @@ -134,9 +135,9 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(&mut conn, &form).await?; Post::update( - &mut *context.conn().await?, + &mut conn, post.id, &PostUpdateForm::builder().removed(Some(true)).build(), ) @@ -149,9 +150,9 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(&mut conn, &form).await?; Comment::update( - &mut *context.conn().await?, + &mut conn, comment.id, &CommentUpdateForm::builder().removed(Some(true)).build(), ) diff --git a/crates/apub/src/activities/deletion/delete_user.rs b/crates/apub/src/activities/deletion/delete_user.rs index 625540291..da31bc4bc 100644 --- a/crates/apub/src/activities/deletion/delete_user.rs +++ b/crates/apub/src/activities/deletion/delete_user.rs @@ -28,15 +28,10 @@ impl SendActivity for DeleteAccount { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let actor: ApubPerson = local_user_view.person.into(); - delete_user_account( - actor.id, - &mut *context.conn().await?, - context.settings(), - context.client(), - ) - .await?; + delete_user_account(actor.id, &mut conn, context.settings(), context.client()).await?; let id = generate_activity_id( DeleteType::Delete, @@ -51,7 +46,7 @@ impl SendActivity for DeleteAccount { cc: vec![], }; - let inboxes = remote_instance_inboxes(&mut *context.conn().await?).await?; + let inboxes = remote_instance_inboxes(&mut conn).await?; send_lemmy_activity(context, delete, &actor, inboxes, true).await?; Ok(()) } @@ -80,15 +75,10 @@ impl ActivityHandler for DeleteUser { } async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, false, context).await?; let actor = self.actor.dereference(context).await?; - delete_user_account( - actor.id, - &mut *context.conn().await?, - context.settings(), - context.client(), - ) - .await?; + delete_user_account(actor.id, &mut conn, context.settings(), context.client()).await?; Ok(()) } } diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 2792f3f4f..7fdcefd35 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -63,9 +63,9 @@ impl SendActivity for DeletePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = - Community::read(&mut *context.conn().await?, response.post_view.community.id).await?; + let community = Community::read(&mut conn, response.post_view.community.id).await?; let deletable = DeletableObjects::Post(response.post_view.post.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -88,9 +88,9 @@ impl SendActivity for RemovePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = - Community::read(&mut *context.conn().await?, response.post_view.community.id).await?; + let community = Community::read(&mut conn, response.post_view.community.id).await?; let deletable = DeletableObjects::Post(response.post_view.post.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -113,13 +113,10 @@ impl SendActivity for DeleteComment { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let community_id = response.comment_view.community.id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; - let person = Person::read( - &mut *context.conn().await?, - response.comment_view.creator.id, - ) - .await?; + let community = Community::read(&mut conn, community_id).await?; + let person = Person::read(&mut conn, response.comment_view.creator.id).await?; let deletable = DeletableObjects::Comment(response.comment_view.comment.clone().into()); send_apub_delete_in_community(person, community, deletable, None, request.deleted, context) .await @@ -135,13 +132,10 @@ impl SendActivity for RemoveComment { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let comment = Comment::read(&mut *context.conn().await?, request.comment_id).await?; - let community = Community::read( - &mut *context.conn().await?, - response.comment_view.community.id, - ) - .await?; + let comment = Comment::read(&mut conn, request.comment_id).await?; + let community = Community::read(&mut conn, response.comment_view.community.id).await?; let deletable = DeletableObjects::Comment(comment.into()); send_apub_delete_in_community( local_user_view.person, @@ -184,8 +178,9 @@ impl SendActivity for DeleteCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, request.community_id).await?; + let community = Community::read(&mut conn, request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -208,8 +203,9 @@ impl SendActivity for RemoveCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, request.community_id).await?; + let community = Community::read(&mut conn, request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -261,10 +257,9 @@ async fn send_apub_delete_private_message( deleted: bool, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let recipient_id = pm.recipient_id; - let recipient: ApubPerson = Person::read(&mut *context.conn().await?, recipient_id) - .await? - .into(); + let recipient: ApubPerson = Person::read(&mut conn, recipient_id).await?.into(); let deletable = DeletableObjects::PrivateMessage(pm.into()); let inbox = vec![recipient.shared_inbox_or_inbox()]; @@ -390,6 +385,7 @@ async fn receive_delete_action( deleted: bool, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; match DeletableObjects::read_from_db(object, context).await? { DeletableObjects::Community(community) => { if community.local { @@ -400,7 +396,7 @@ async fn receive_delete_action( } Community::update( - &mut *context.conn().await?, + &mut conn, community.id, &CommunityUpdateForm::builder() .deleted(Some(deleted)) @@ -411,7 +407,7 @@ async fn receive_delete_action( DeletableObjects::Post(post) => { if deleted != post.deleted { Post::update( - &mut *context.conn().await?, + &mut conn, post.id, &PostUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -421,7 +417,7 @@ async fn receive_delete_action( DeletableObjects::Comment(comment) => { if deleted != comment.deleted { Comment::update( - &mut *context.conn().await?, + &mut conn, comment.id, &CommentUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -430,7 +426,7 @@ async fn receive_delete_action( } DeletableObjects::PrivateMessage(pm) => { PrivateMessage::update( - &mut *context.conn().await?, + &mut conn, pm.id, &PrivateMessageUpdateForm::builder() .deleted(Some(deleted)) diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index 5be780be0..654d578a1 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -97,6 +97,7 @@ impl UndoDelete { object: &Url, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; match DeletableObjects::read_from_db(object, context).await? { DeletableObjects::Community(community) => { if community.local { @@ -111,9 +112,9 @@ impl UndoDelete { reason: None, expires: None, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(&mut conn, &form).await?; Community::update( - &mut *context.conn().await?, + &mut conn, community.id, &CommunityUpdateForm::builder().removed(Some(false)).build(), ) @@ -126,9 +127,9 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(&mut conn, &form).await?; Post::update( - &mut *context.conn().await?, + &mut conn, post.id, &PostUpdateForm::builder().removed(Some(false)).build(), ) @@ -141,9 +142,9 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(&mut conn, &form).await?; Comment::update( - &mut *context.conn().await?, + &mut conn, comment.id, &CommentUpdateForm::builder().removed(Some(false)).build(), ) diff --git a/crates/apub/src/activities/following/accept.rs b/crates/apub/src/activities/following/accept.rs index 432133217..dd1c93e3d 100644 --- a/crates/apub/src/activities/following/accept.rs +++ b/crates/apub/src/activities/following/accept.rs @@ -60,14 +60,14 @@ impl ActivityHandler for AcceptFollow { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, true, context).await?; let community = self.actor.dereference(context).await?; let person = self.object.actor.dereference(context).await?; // This will throw an error if no follow was requested let community_id = community.id; let person_id = person.id; - CommunityFollower::follow_accepted(&mut *context.conn().await?, community_id, person_id) - .await?; + CommunityFollower::follow_accepted(&mut conn, community_id, person_id).await?; Ok(()) } diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index 1f7304f74..8b860e2ca 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -60,12 +60,13 @@ impl Follow { community: &ApubCommunity, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: actor.id, pending: true, }; - CommunityFollower::follow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::follow(&mut conn, &community_follower_form) .await .ok(); @@ -103,6 +104,7 @@ impl ActivityHandler for Follow { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, true, context).await?; let actor = self.actor.dereference(context).await?; let object = self.object.dereference(context).await?; @@ -113,7 +115,7 @@ impl ActivityHandler for Follow { follower_id: actor.id, pending: false, }; - PersonFollower::follow(&mut *context.conn().await?, &form).await?; + PersonFollower::follow(&mut conn, &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -121,7 +123,7 @@ impl ActivityHandler for Follow { person_id: actor.id, pending: false, }; - CommunityFollower::follow(&mut *context.conn().await?, &form).await?; + CommunityFollower::follow(&mut conn, &form).await?; } } @@ -138,8 +140,9 @@ impl SendActivity for BlockCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; - let community = Community::read(&mut *context.conn().await?, request.community_id).await?; + let community = Community::read(&mut conn, request.community_id).await?; UndoFollow::send(&local_user_view.person.into(), &community.into(), context).await } } diff --git a/crates/apub/src/activities/following/mod.rs b/crates/apub/src/activities/following/mod.rs index f29ba2717..b0c88d71a 100644 --- a/crates/apub/src/activities/following/mod.rs +++ b/crates/apub/src/activities/following/mod.rs @@ -25,12 +25,12 @@ impl SendActivity for FollowCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let person = local_user_view.person.clone().into(); - let community: ApubCommunity = - Community::read(&mut *context.conn().await?, request.community_id) - .await? - .into(); + let community: ApubCommunity = Community::read(&mut conn, request.community_id) + .await? + .into(); if community.local { Ok(()) } else if request.follow { diff --git a/crates/apub/src/activities/following/undo_follow.rs b/crates/apub/src/activities/following/undo_follow.rs index 20fb6bae9..737ae7cfe 100644 --- a/crates/apub/src/activities/following/undo_follow.rs +++ b/crates/apub/src/activities/following/undo_follow.rs @@ -71,6 +71,7 @@ impl ActivityHandler for UndoFollow { #[tracing::instrument(skip_all)] async fn receive(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; insert_activity(&self.id, &self, false, true, context).await?; let person = self.actor.dereference(context).await?; let object = self.object.object.dereference(context).await?; @@ -82,7 +83,7 @@ impl ActivityHandler for UndoFollow { follower_id: person.id, pending: false, }; - PersonFollower::unfollow(&mut *context.conn().await?, &form).await?; + PersonFollower::unfollow(&mut conn, &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -90,7 +91,7 @@ impl ActivityHandler for UndoFollow { person_id: person.id, pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &form).await?; + CommunityFollower::unfollow(&mut conn, &form).await?; } } diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index fecb01d6a..c7cf27ce7 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -53,13 +53,14 @@ pub(crate) async fn verify_person_in_community( community: &ApubCommunity, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let person = person_id.dereference(context).await?; if person.banned { return Err(LemmyError::from_message("Person is banned from site")); } let person_id = person.id; let community_id = community.id; - let is_banned = CommunityPersonBanView::get(&mut *context.conn().await?, person_id, community_id) + let is_banned = CommunityPersonBanView::get(&mut conn, person_id, community_id) .await .is_ok(); if is_banned { @@ -81,10 +82,10 @@ pub(crate) async fn verify_mod_action( community_id: CommunityId, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let mod_ = mod_id.dereference(context).await?; - let is_mod_or_admin = - CommunityView::is_mod_or_admin(&mut *context.conn().await?, mod_.id, community_id).await?; + let is_mod_or_admin = CommunityView::is_mod_or_admin(&mut conn, mod_.id, community_id).await?; if is_mod_or_admin { return Ok(()); } diff --git a/crates/apub/src/activities/voting/mod.rs b/crates/apub/src/activities/voting/mod.rs index 5ef858688..0a09c034e 100644 --- a/crates/apub/src/activities/voting/mod.rs +++ b/crates/apub/src/activities/voting/mod.rs @@ -83,11 +83,10 @@ async fn send_activity( jwt: &Sensitive, context: &Data, ) -> Result<(), LemmyError> { - let community = Community::read(&mut *context.conn().await?, community_id) - .await? - .into(); + let mut conn = context.conn().await?; + let community = Community::read(&mut conn, community_id).await?.into(); let local_user_view = local_user_view_from_jwt(jwt, context).await?; - let actor = Person::read(&mut *context.conn().await?, local_user_view.person.id) + let actor = Person::read(&mut conn, local_user_view.person.id) .await? .into(); @@ -112,6 +111,7 @@ async fn vote_comment( comment: &ApubComment, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let comment_id = comment.id; let like_form = CommentLikeForm { comment_id, @@ -120,8 +120,8 @@ async fn vote_comment( score: vote_type.into(), }; let person_id = actor.id; - CommentLike::remove(&mut *context.conn().await?, person_id, comment_id).await?; - CommentLike::like(&mut *context.conn().await?, &like_form).await?; + CommentLike::remove(&mut conn, person_id, comment_id).await?; + CommentLike::like(&mut conn, &like_form).await?; Ok(()) } @@ -132,6 +132,7 @@ async fn vote_post( post: &ApubPost, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let post_id = post.id; let like_form = PostLikeForm { post_id: post.id, @@ -139,8 +140,8 @@ async fn vote_post( score: vote_type.into(), }; let person_id = actor.id; - PostLike::remove(&mut *context.conn().await?, person_id, post_id).await?; - PostLike::like(&mut *context.conn().await?, &like_form).await?; + PostLike::remove(&mut conn, person_id, post_id).await?; + PostLike::like(&mut conn, &like_form).await?; Ok(()) } @@ -150,9 +151,10 @@ async fn undo_vote_comment( comment: &ApubComment, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let comment_id = comment.id; let person_id = actor.id; - CommentLike::remove(&mut *context.conn().await?, person_id, comment_id).await?; + CommentLike::remove(&mut conn, person_id, comment_id).await?; Ok(()) } @@ -162,8 +164,9 @@ async fn undo_vote_post( post: &ApubPost, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let post_id = post.id; let person_id = actor.id; - PostLike::remove(&mut *context.conn().await?, person_id, post_id).await?; + PostLike::remove(&mut conn, person_id, post_id).await?; Ok(()) } diff --git a/crates/apub/src/activities/voting/vote.rs b/crates/apub/src/activities/voting/vote.rs index 66affa7e5..184f22c13 100644 --- a/crates/apub/src/activities/voting/vote.rs +++ b/crates/apub/src/activities/voting/vote.rs @@ -56,9 +56,11 @@ impl ActivityHandler for Vote { #[tracing::instrument(skip_all)] async fn verify(&self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; + let community = self.community(context).await?; verify_person_in_community(&self.actor, &community, context).await?; - let enable_downvotes = LocalSite::read(&mut *context.conn().await?) + let enable_downvotes = LocalSite::read(&mut conn) .await .map(|l| l.enable_downvotes) .unwrap_or(true); diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index 0f5fd2b4d..e68cedb0c 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -22,9 +22,10 @@ impl PerformApub for GetComments { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetComments = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; let community_id = if let Some(name) = &data.community_name { @@ -46,11 +47,7 @@ impl PerformApub for GetComments { // If a parent_id is given, fetch the comment to get the path let parent_path = if let Some(parent_id) = parent_id { - Some( - Comment::read(&mut *context.conn().await?, parent_id) - .await? - .path, - ) + Some(Comment::read(&mut conn, parent_id).await?.path) } else { None }; diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 85a4055cc..cdd8a4b96 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -19,9 +19,10 @@ impl PerformApub for GetPosts { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &GetPosts = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; @@ -41,13 +42,9 @@ impl PerformApub for GetPosts { let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?; - let is_mod_or_admin = is_mod_or_admin_opt( - &mut *context.conn().await?, - local_user_view.as_ref(), - community_id, - ) - .await - .is_ok(); + let is_mod_or_admin = is_mod_or_admin_opt(&mut conn, local_user_view.as_ref(), community_id) + .await + .is_ok(); let mut conn = context.conn().await?; let posts = PostQuery::builder() diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index a3c89b2e8..860eb72d3 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -27,9 +27,10 @@ impl PerformApub for GetCommunity { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &GetCommunity = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; if data.name.is_none() && data.id.is_none() { return Err(LemmyError::from_message("no_id_given")); @@ -50,31 +51,23 @@ impl PerformApub for GetCommunity { } }; - let is_mod_or_admin = is_mod_or_admin_opt( - &mut *context.conn().await?, - local_user_view.as_ref(), - Some(community_id), - ) - .await - .is_ok(); + let is_mod_or_admin = + is_mod_or_admin_opt(&mut conn, local_user_view.as_ref(), Some(community_id)) + .await + .is_ok(); - let community_view = CommunityView::read( - &mut *context.conn().await?, - community_id, - person_id, - Some(is_mod_or_admin), - ) - .await - .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; - - let moderators = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id) + let community_view = + CommunityView::read(&mut conn, community_id, person_id, Some(is_mod_or_admin)) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; + let moderators = CommunityModeratorView::for_community(&mut conn, community_id) + .await + .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; + let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into()); - let mut site = Site::read_from_apub_id(&mut *context.conn().await?, &site_id.into()).await?; + let mut site = Site::read_from_apub_id(&mut conn, &site_id.into()).await?; // no need to include metadata for local site (its already available through other endpoints). // this also prevents us from leaking the federation private key. if let Some(s) = &site { @@ -84,8 +77,7 @@ impl PerformApub for GetCommunity { } let community_id = community_view.community.id; - let discussion_languages = - CommunityLanguage::read(&mut *context.conn().await?, community_id).await?; + let discussion_languages = CommunityLanguage::read(&mut conn, community_id).await?; let res = GetCommunityResponse { community_view, diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 69e084d54..617b43eee 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -22,6 +22,7 @@ impl PerformApub for GetPersonDetails { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let data: &GetPersonDetails = self; // Check to make sure a person name or an id is given @@ -30,7 +31,7 @@ impl PerformApub for GetPersonDetails { } let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); check_private_instance(&local_user_view, &local_site)?; @@ -53,7 +54,7 @@ impl PerformApub for GetPersonDetails { // You don't need to return settings for the user, since this comes back with GetSite // `my_user` - let person_view = PersonView::read(&mut *context.conn().await?, person_details_id).await?; + let person_view = PersonView::read(&mut conn, person_details_id).await?; let sort = data.sort; let page = data.page; @@ -109,8 +110,7 @@ impl PerformApub for GetPersonDetails { } .await?; - let moderates = - CommunityModeratorView::for_person(&mut *context.conn().await?, person_details_id).await?; + let moderates = CommunityModeratorView::for_person(&mut conn, person_details_id).await?; // Return the jwt Ok(GetPersonDetailsResponse { diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index 8a0c805e7..48551ee37 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -23,15 +23,16 @@ impl PerformApub for ResolveObject { &self, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; let person_id = local_user_view.person.id; check_private_instance(&Some(local_user_view), &local_site)?; let res = search_query_to_object_id(&self.q, context) .await .map_err(|e| e.with_message("couldnt_find_object"))?; - convert_response(res, person_id, &mut *context.conn().await?) + convert_response(res, person_id, &mut conn) .await .map_err(|e| e.with_message("couldnt_find_object")) } diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index b7e59d413..01eb219c7 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -24,10 +24,11 @@ impl PerformApub for Search { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let data: &Search = self; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(&mut conn).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index f6e9819cc..2a1e1dd49 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -35,13 +35,13 @@ where for<'de2> ::Kind: serde::Deserialize<'de2>, DbActor: ApubActor + Send + 'static, { - // remote actor + let mut conn = context.conn().await?; // remote actor if identifier.contains('@') { let (name, domain) = identifier .splitn(2, '@') .collect_tuple() .expect("invalid query"); - let actor = DbActor::read_from_name_and_domain(&mut *context.conn().await?, name, domain).await; + let actor = DbActor::read_from_name_and_domain(&mut conn, name, domain).await; if actor.is_ok() { Ok(actor?.into()) } else if local_user_view.is_some() { @@ -56,7 +56,7 @@ where else { let identifier = identifier.to_string(); Ok( - DbActor::read_from_name(&mut *context.conn().await?, &identifier, include_deleted) + DbActor::read_from_name(&mut conn, &identifier, include_deleted) .await? .into(), ) diff --git a/crates/apub/src/fetcher/post_or_comment.rs b/crates/apub/src/fetcher/post_or_comment.rs index b1628c17d..29651d488 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -89,18 +89,11 @@ impl Object for PostOrComment { #[async_trait::async_trait] impl InCommunity for PostOrComment { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let cid = match self { PostOrComment::Post(p) => p.community_id, - PostOrComment::Comment(c) => { - Post::read(&mut *context.conn().await?, c.post_id) - .await? - .community_id - } + PostOrComment::Comment(c) => Post::read(&mut conn, c.post_id).await?.community_id, }; - Ok( - Community::read(&mut *context.conn().await?, cid) - .await? - .into(), - ) + Ok(Community::read(&mut conn, cid).await?.into()) } } diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index a1101b12b..ce497b95a 100644 --- a/crates/apub/src/http/comment.rs +++ b/crates/apub/src/http/comment.rs @@ -20,8 +20,9 @@ pub(crate) async fn get_apub_comment( info: Path, context: Data, ) -> Result { + let mut conn = context.conn().await?; let id = CommentId(info.comment_id.parse::()?); - let comment: ApubComment = Comment::read(&mut *context.conn().await?, id).await?.into(); + let comment: ApubComment = Comment::read(&mut conn, id).await?.into(); if !comment.local { return Err(err_object_not_local()); } diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index f1ef6f199..7d140332b 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -32,10 +32,10 @@ pub(crate) async fn get_apub_community_http( info: web::Path, context: Data, ) -> Result { - let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, true) - .await? - .into(); + let mut conn = context.conn().await?; + let community: ApubCommunity = Community::read_from_name(&mut conn, &info.community_name, true) + .await? + .into(); if !community.deleted && !community.removed { let apub = community.into_json(&context).await?; @@ -64,8 +64,8 @@ pub(crate) async fn get_apub_community_followers( info: web::Path, context: Data, ) -> Result { - let community = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false).await?; + let mut conn = context.conn().await?; + let community = Community::read_from_name(&mut conn, &info.community_name, false).await?; let followers = GroupFollowers::new(community, &context).await?; create_apub_response(&followers) } @@ -76,10 +76,10 @@ pub(crate) async fn get_apub_community_outbox( info: web::Path, context: Data, ) -> Result { - let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) - .await? - .into(); + let mut conn = context.conn().await?; + let community: ApubCommunity = Community::read_from_name(&mut conn, &info.community_name, false) + .await? + .into(); if community.deleted || community.removed { return Err(LemmyError::from_message("deleted")); } @@ -92,10 +92,10 @@ pub(crate) async fn get_apub_community_moderators( info: web::Path, context: Data, ) -> Result { - let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) - .await? - .into(); + let mut conn = context.conn().await?; + let community: ApubCommunity = Community::read_from_name(&mut conn, &info.community_name, false) + .await? + .into(); if community.deleted || community.removed { return Err(LemmyError::from_message("deleted")); } @@ -108,10 +108,10 @@ pub(crate) async fn get_apub_community_featured( info: web::Path, context: Data, ) -> Result { - let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) - .await? - .into(); + let mut conn = context.conn().await?; + let community: ApubCommunity = Community::read_from_name(&mut conn, &info.community_name, false) + .await? + .into(); if community.deleted || community.removed { return Err(LemmyError::from_message("deleted")); } diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 9000d38f2..fea305ec3 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -82,6 +82,7 @@ pub(crate) async fn get_activity( info: web::Path, context: web::Data, ) -> Result { + let mut conn = context.conn().await?; let settings = context.settings(); let activity_id = Url::parse(&format!( "{}/activities/{}/{}", @@ -90,7 +91,7 @@ pub(crate) async fn get_activity( info.id ))? .into(); - let activity = Activity::read_from_apub_id(&mut *context.conn().await?, &activity_id).await?; + let activity = Activity::read_from_apub_id(&mut conn, &activity_id).await?; let sensitive = activity.sensitive; if !activity.local { diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index 4ae3f6967..cd83bea2d 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -28,9 +28,10 @@ pub(crate) async fn get_apub_person_http( info: web::Path, context: Data, ) -> Result { + let mut conn = context.conn().await?; let user_name = info.into_inner().user_name; // TODO: this needs to be able to read deleted persons, so that it can send tombstones - let person: ApubPerson = Person::read_from_name(&mut *context.conn().await?, &user_name, true) + let person: ApubPerson = Person::read_from_name(&mut conn, &user_name, true) .await? .into(); @@ -60,7 +61,8 @@ pub(crate) async fn get_apub_person_outbox( info: web::Path, context: Data, ) -> Result { - let person = Person::read_from_name(&mut *context.conn().await?, &info.user_name, false).await?; + let mut conn = context.conn().await?; + let person = Person::read_from_name(&mut conn, &info.user_name, false).await?; let outbox_id = generate_outbox_url(&person.actor_id)?.into(); let outbox = EmptyOutbox::new(outbox_id)?; create_apub_response(&outbox) diff --git a/crates/apub/src/http/post.rs b/crates/apub/src/http/post.rs index e1d652f2f..3ab1692c1 100644 --- a/crates/apub/src/http/post.rs +++ b/crates/apub/src/http/post.rs @@ -20,8 +20,9 @@ pub(crate) async fn get_apub_post( info: web::Path, context: Data, ) -> Result { + let mut conn = context.conn().await?; let id = PostId(info.post_id.parse::()?); - let post: ApubPost = Post::read(&mut *context.conn().await?, id).await?.into(); + let post: ApubPost = Post::read(&mut conn, id).await?.into(); if !post.local { return Err(err_object_not_local()); } diff --git a/crates/apub/src/http/site.rs b/crates/apub/src/http/site.rs index 19653f1c0..eadd9765c 100644 --- a/crates/apub/src/http/site.rs +++ b/crates/apub/src/http/site.rs @@ -19,10 +19,8 @@ use url::Url; pub(crate) async fn get_apub_site_http( context: Data, ) -> Result { - let site: ApubSite = SiteView::read_local(&mut *context.conn().await?) - .await? - .site - .into(); + let mut conn = context.conn().await?; + let site: ApubSite = SiteView::read_local(&mut conn).await?.site.into(); let apub = site.into_json(&context).await?; create_apub_response(&apub) diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 296d368e8..c16681f35 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -39,6 +39,7 @@ pub struct VerifyUrlData(pub DbPool); impl UrlVerifier for VerifyUrlData { async fn verify(&self, url: &Url) -> Result<(), &'static str> { let mut conn = get_conn(&self.0).await.expect("get connection"); + let local_site_data = fetch_local_site_data(&mut conn) .await .expect("read local site data"); diff --git a/crates/apub/src/mentions.rs b/crates/apub/src/mentions.rs index d64e24a00..24c10e8b6 100644 --- a/crates/apub/src/mentions.rs +++ b/crates/apub/src/mentions.rs @@ -45,7 +45,8 @@ pub async fn collect_non_local_mentions( community_id: ObjectId, context: &Data, ) -> Result { - let parent_creator = get_comment_parent_creator(&mut *context.conn().await?, comment).await?; + let mut conn = context.conn().await?; + let parent_creator = get_comment_parent_creator(&mut conn, comment).await?; let mut addressed_ccs: Vec = vec![community_id.into(), parent_creator.id()]; // Add the mention tag diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 5c46a5892..91577a867 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -66,8 +66,9 @@ impl Object for ApubComment { object_id: Url, context: &Data, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; Ok( - Comment::read_from_apub_id(&mut *context.conn().await?, object_id) + Comment::read_from_apub_id(&mut conn, object_id) .await? .map(Into::into), ) @@ -75,30 +76,32 @@ impl Object for ApubComment { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; if !self.deleted { let form = CommentUpdateForm::builder().deleted(Some(true)).build(); - Comment::update(&mut *context.conn().await?, self.id, &form).await?; + Comment::update(&mut conn, self.id, &form).await?; } Ok(()) } #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { + let mut conn = context.conn().await?; let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(&mut conn, creator_id).await?; let post_id = self.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; let community_id = post.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; let in_reply_to = if let Some(comment_id) = self.parent_comment_id() { - let parent_comment = Comment::read(&mut *context.conn().await?, comment_id).await?; + let parent_comment = Comment::read(&mut conn, comment_id).await?; parent_comment.ap_id.into() } else { post.ap_id.into() }; - let language = LanguageTag::new_single(self.language_id, &mut *context.conn().await?).await?; + let language = LanguageTag::new_single(self.language_id, &mut conn).await?; let maa = collect_non_local_mentions(&self, community.actor_id.clone().into(), context).await?; let note = Note { @@ -128,11 +131,12 @@ impl Object for ApubComment { expected_domain: &Url, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; verify_domains_match(note.id.inner(), expected_domain)?; verify_domains_match(note.attributed_to.inner(), note.id.inner())?; verify_is_public(¬e.to, ¬e.cc)?; let community = note.community(context).await?; - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let local_site_data = fetch_local_site_data(&mut conn).await?; check_apub_id_valid_with_strictness( note.id.inner(), @@ -154,16 +158,16 @@ impl Object for ApubComment { /// If the parent community, post and comment(s) are not known locally, these are also fetched. #[tracing::instrument(skip_all)] async fn from_json(note: Note, context: &Data) -> Result { + let mut conn = context.conn().await?; let creator = note.attributed_to.dereference(context).await?; let (post, parent_comment) = note.get_parents(context).await?; let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source); - let local_site = LocalSite::read(&mut *context.conn().await?).await.ok(); + let local_site = LocalSite::read(&mut conn).await.ok(); let slur_regex = &local_site_opt_to_slur_regex(&local_site); let content_slurs_removed = remove_slurs(&content, slur_regex); - let language_id = - LanguageTag::to_language_id_single(note.language, &mut *context.conn().await?).await?; + let language_id = LanguageTag::to_language_id_single(note.language, &mut conn).await?; let form = CommentInsertForm { creator_id: creator.id, @@ -179,12 +183,7 @@ impl Object for ApubComment { language_id, }; let parent_comment_path = parent_comment.map(|t| t.0.path); - let comment = Comment::create( - &mut *context.conn().await?, - &form, - parent_comment_path.as_ref(), - ) - .await?; + let comment = Comment::create(&mut conn, &form, parent_comment_path.as_ref()).await?; Ok(comment.into()) } } diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 15d254ffc..5b646ceeb 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -66,8 +66,9 @@ impl Object for ApubCommunity { object_id: Url, context: &Data, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; Ok( - Community::read_from_apub_id(&mut *context.conn().await?, &object_id.into()) + Community::read_from_apub_id(&mut conn, &object_id.into()) .await? .map(Into::into), ) @@ -75,8 +76,9 @@ impl Object for ApubCommunity { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; let form = CommunityUpdateForm::builder().deleted(Some(true)).build(); - Community::update(&mut *context.conn().await?, self.id, &form).await?; + Community::update(&mut conn, self.id, &form).await?; Ok(()) } @@ -128,14 +130,14 @@ impl Object for ApubCommunity { group: Group, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let instance_id = fetch_instance_actor_for_object(&group.id, context).await?; let form = Group::into_insert_form(group.clone(), instance_id); - let languages = - LanguageTag::to_language_id_multiple(group.language, &mut *context.conn().await?).await?; + let languages = LanguageTag::to_language_id_multiple(group.language, &mut conn).await?; - let community = Community::create(&mut *context.conn().await?, &form).await?; - CommunityLanguage::update(&mut *context.conn().await?, languages, community.id).await?; + let community = Community::create(&mut conn, &form).await?; + CommunityLanguage::update(&mut conn, languages, community.id).await?; let community: ApubCommunity = community.into(); @@ -186,10 +188,11 @@ impl ApubCommunity { &self, context: &LemmyContext, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; let id = self.id; - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; - let follows = CommunityFollowerView::for_community(&mut *context.conn().await?, id).await?; + let local_site_data = fetch_local_site_data(&mut conn).await?; + let follows = CommunityFollowerView::for_community(&mut conn, id).await?; let inboxes: Vec = follows .into_iter() .filter(|f| !f.follower.local) diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index 66332254a..01befebc3 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -176,6 +176,7 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object + C object_id: &T, context: &Data, ) -> Result { + let mut conn = context.conn().await?; let object_id: Url = object_id.clone().into(); let instance_id = Site::instance_actor_id_from_url(object_id); let site = ObjectId::::from(instance_id.clone()) @@ -188,7 +189,7 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object + C debug!("Failed to dereference site for {}: {}", &instance_id, e); let domain = instance_id.domain().expect("has domain"); Ok( - DbInstance::read_or_create(&mut *context.conn().await?, domain.to_string()) + DbInstance::read_or_create(&mut conn, domain.to_string()) .await? .id, ) diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index e5cd9e8e6..83cf6930e 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -68,8 +68,10 @@ impl Object for ApubPerson { object_id: Url, context: &Data, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; + Ok( - DbPerson::read_from_apub_id(&mut *context.conn().await?, &object_id.into()) + DbPerson::read_from_apub_id(&mut conn, &object_id.into()) .await? .map(Into::into), ) @@ -77,8 +79,10 @@ impl Object for ApubPerson { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; + let form = PersonUpdateForm::builder().deleted(Some(true)).build(); - DbPerson::update(&mut *context.conn().await?, self.id, &form).await?; + DbPerson::update(&mut conn, self.id, &form).await?; Ok(()) } @@ -118,7 +122,9 @@ impl Object for ApubPerson { expected_domain: &Url, context: &Data, ) -> Result<(), LemmyError> { - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let mut conn = context.conn().await?; + + let local_site_data = fetch_local_site_data(&mut conn).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs(&person.preferred_username, slur_regex)?; @@ -142,6 +148,8 @@ impl Object for ApubPerson { person: Person, context: &Data, ) -> Result { + let mut conn = context.conn().await?; + let instance_id = fetch_instance_actor_for_object(&person.id, context).await?; // Some Mastodon users have `name: ""` (empty string), need to convert that to `None` @@ -171,7 +179,7 @@ impl Object for ApubPerson { matrix_user_id: person.matrix_user_id, instance_id, }; - let person = DbPerson::upsert(&mut *context.conn().await?, &person_form).await?; + let person = DbPerson::upsert(&mut conn, &person_form).await?; Ok(person.into()) } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index f45d0685c..4f3e60237 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -82,8 +82,9 @@ impl Object for ApubPost { object_id: Url, context: &Data, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; Ok( - Post::read_from_apub_id(&mut *context.conn().await?, object_id) + Post::read_from_apub_id(&mut conn, object_id) .await? .map(Into::into), ) @@ -91,9 +92,10 @@ impl Object for ApubPost { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; if !self.deleted { let form = PostUpdateForm::builder().deleted(Some(true)).build(); - Post::update(&mut *context.conn().await?, self.id, &form).await?; + Post::update(&mut conn, self.id, &form).await?; } Ok(()) } @@ -101,11 +103,12 @@ impl Object for ApubPost { // Turn a Lemmy post into an ActivityPub page that can be sent out over the network. #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { + let mut conn = context.conn().await?; let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(&mut conn, creator_id).await?; let community_id = self.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; - let language = LanguageTag::new_single(self.language_id, &mut *context.conn().await?).await?; + let community = Community::read(&mut conn, community_id).await?; + let language = LanguageTag::new_single(self.language_id, &mut conn).await?; let page = Page { kind: PageType::Page, @@ -136,14 +139,14 @@ impl Object for ApubPost { expected_domain: &Url, context: &Data, ) -> Result<(), LemmyError> { - // We can't verify the domain in case of mod action, because the mod may be on a different - // instance from the post author. + let mut conn = context.conn().await?; // We can't verify the domain in case of mod action, because the mod may be on a different + // instance from the post author. if !page.is_mod_action(context).await? { verify_domains_match(page.id.inner(), expected_domain)?; verify_is_remote_object(page.id.inner(), context.settings())?; }; - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let local_site_data = fetch_local_site_data(&mut conn).await?; let community = page.community(context).await?; check_apub_id_valid_with_strictness( @@ -164,10 +167,11 @@ impl Object for ApubPost { #[tracing::instrument(skip_all)] async fn from_json(page: Page, context: &Data) -> Result { + let mut conn = context.conn().await?; let creator = page.creator()?.dereference(context).await?; let community = page.community(context).await?; if community.posting_restricted_to_mods { - is_mod_or_admin(&mut *context.conn().await?, creator.id, community.id).await?; + is_mod_or_admin(&mut conn, creator.id, community.id).await?; } let mut name = page .name @@ -208,14 +212,13 @@ impl Object for ApubPost { let (embed_title, embed_description, embed_video_url) = metadata_res .map(|u| (u.title, u.description, u.embed_video_url)) .unwrap_or_default(); - let local_site = LocalSite::read(&mut *context.conn().await?).await.ok(); + let local_site = LocalSite::read(&mut conn).await.ok(); let slur_regex = &local_site_opt_to_slur_regex(&local_site); let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source) .map(|s| remove_slurs(&s, slur_regex)); - let language_id = - LanguageTag::to_language_id_single(page.language, &mut *context.conn().await?).await?; + let language_id = LanguageTag::to_language_id_single(page.language, &mut conn).await?; PostInsertForm { name, @@ -251,7 +254,7 @@ impl Object for ApubPost { .build() }; - let post = Post::create(&mut *context.conn().await?, &form).await?; + let post = Post::create(&mut conn, &form).await?; // write mod log entry for lock if Page::is_locked_changed(&old_post, &page.comments_enabled) { @@ -260,7 +263,7 @@ impl Object for ApubPost { post_id: post.id, locked: Some(post.locked), }; - ModLockPost::create(&mut *context.conn().await?, &form).await?; + ModLockPost::create(&mut conn, &form).await?; } Ok(post.into()) diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index edacf5f4f..f65784023 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -59,8 +59,10 @@ impl Object for ApubPrivateMessage { object_id: Url, context: &Data, ) -> Result, LemmyError> { + let mut conn = context.conn().await?; + Ok( - PrivateMessage::read_from_apub_id(&mut *context.conn().await?, object_id) + PrivateMessage::read_from_apub_id(&mut conn, object_id) .await? .map(Into::into), ) @@ -73,11 +75,13 @@ impl Object for ApubPrivateMessage { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { + let mut conn = context.conn().await?; + let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(&mut conn, creator_id).await?; let recipient_id = self.recipient_id; - let recipient = Person::read(&mut *context.conn().await?, recipient_id).await?; + let recipient = Person::read(&mut conn, recipient_id).await?; let note = ChatMessage { r#type: ChatMessageType::ChatMessage, @@ -99,10 +103,12 @@ impl Object for ApubPrivateMessage { expected_domain: &Url, context: &Data, ) -> Result<(), LemmyError> { + let mut conn = context.conn().await?; + verify_domains_match(note.id.inner(), expected_domain)?; verify_domains_match(note.attributed_to.inner(), note.id.inner())?; - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let local_site_data = fetch_local_site_data(&mut conn).await?; check_apub_id_valid_with_strictness( note.id.inner(), @@ -122,9 +128,11 @@ impl Object for ApubPrivateMessage { note: ChatMessage, context: &Data, ) -> Result { + let mut conn = context.conn().await?; + let creator = note.attributed_to.dereference(context).await?; let recipient = note.to[0].dereference(context).await?; - check_person_block(creator.id, recipient.id, &mut *context.conn().await?).await?; + check_person_block(creator.id, recipient.id, &mut conn).await?; let form = PrivateMessageInsertForm { creator_id: creator.id, @@ -137,7 +145,7 @@ impl Object for ApubPrivateMessage { ap_id: Some(note.id.into()), local: Some(false), }; - let pm = PrivateMessage::create(&mut *context.conn().await?, &form).await?; + let pm = PrivateMessage::create(&mut conn, &form).await?; Ok(pm.into()) } } diff --git a/crates/apub/src/protocol/activities/community/collection_add.rs b/crates/apub/src/protocol/activities/community/collection_add.rs index c2d4c6a2e..fe38f07bb 100644 --- a/crates/apub/src/protocol/activities/community/collection_add.rs +++ b/crates/apub/src/protocol/activities/community/collection_add.rs @@ -34,9 +34,9 @@ pub struct CollectionAdd { #[async_trait::async_trait] impl InCommunity for CollectionAdd { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let (community, _) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.clone().target.into()) - .await?; + Community::get_by_collection_url(&mut conn, &self.clone().target.into()).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/community/collection_remove.rs b/crates/apub/src/protocol/activities/community/collection_remove.rs index 8c253a752..5d586ccae 100644 --- a/crates/apub/src/protocol/activities/community/collection_remove.rs +++ b/crates/apub/src/protocol/activities/community/collection_remove.rs @@ -34,9 +34,9 @@ pub struct CollectionRemove { #[async_trait::async_trait] impl InCommunity for CollectionRemove { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let (community, _) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.clone().target.into()) - .await?; + Community::get_by_collection_url(&mut conn, &self.clone().target.into()).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/community/lock_page.rs b/crates/apub/src/protocol/activities/community/lock_page.rs index bb3e81bf6..037843033 100644 --- a/crates/apub/src/protocol/activities/community/lock_page.rs +++ b/crates/apub/src/protocol/activities/community/lock_page.rs @@ -54,8 +54,9 @@ pub struct UndoLockPage { #[async_trait::async_trait] impl InCommunity for LockPage { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let post = self.object.dereference(context).await?; - let community = Community::read(&mut *context.conn().await?, post.community_id).await?; + let community = Community::read(&mut conn, post.community_id).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/create_or_update/note.rs b/crates/apub/src/protocol/activities/create_or_update/note.rs index b5a95ef1e..6f3197e8b 100644 --- a/crates/apub/src/protocol/activities/create_or_update/note.rs +++ b/crates/apub/src/protocol/activities/create_or_update/note.rs @@ -35,8 +35,9 @@ pub struct CreateOrUpdateNote { #[async_trait::async_trait] impl InCommunity for CreateOrUpdateNote { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let post = self.object.get_parents(context).await?.0; - let community = Community::read(&mut *context.conn().await?, post.community_id).await?; + let community = Community::read(&mut conn, post.community_id).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/deletion/delete.rs b/crates/apub/src/protocol/activities/deletion/delete.rs index 21b15ed38..fd3c8144c 100644 --- a/crates/apub/src/protocol/activities/deletion/delete.rs +++ b/crates/apub/src/protocol/activities/deletion/delete.rs @@ -45,10 +45,11 @@ pub struct Delete { #[async_trait::async_trait] impl InCommunity for Delete { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? { DeletableObjects::Community(c) => c.id, DeletableObjects::Comment(c) => { - let post = Post::read(&mut *context.conn().await?, c.post_id).await?; + let post = Post::read(&mut conn, c.post_id).await?; post.community_id } DeletableObjects::Post(p) => p.community_id, @@ -56,7 +57,7 @@ impl InCommunity for Delete { return Err(anyhow!("Private message is not part of community").into()) } }; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(&mut conn, community_id).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/collections/group_followers.rs b/crates/apub/src/protocol/collections/group_followers.rs index 055ea21a4..f477cb4fc 100644 --- a/crates/apub/src/protocol/collections/group_followers.rs +++ b/crates/apub/src/protocol/collections/group_followers.rs @@ -20,9 +20,9 @@ impl GroupFollowers { community: Community, context: &LemmyContext, ) -> Result { + let mut conn = context.conn().await?; let community_id = community.id; - let community_followers = - CommunityFollowerView::for_community(&mut *context.conn().await?, community_id).await?; + let community_followers = CommunityFollowerView::for_community(&mut conn, community_id).await?; Ok(GroupFollowers { id: generate_followers_url(&community.actor_id)?.into(), diff --git a/crates/apub/src/protocol/objects/group.rs b/crates/apub/src/protocol/objects/group.rs index 9ee092c13..a9788c1ee 100644 --- a/crates/apub/src/protocol/objects/group.rs +++ b/crates/apub/src/protocol/objects/group.rs @@ -80,7 +80,8 @@ impl Group { expected_domain: &Url, context: &LemmyContext, ) -> Result<(), LemmyError> { - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let mut conn = context.conn().await?; + let local_site_data = fetch_local_site_data(&mut conn).await?; check_apub_id_valid_with_strictness( self.id.inner(), diff --git a/crates/apub/src/protocol/objects/note.rs b/crates/apub/src/protocol/objects/note.rs index fb68a047e..dedeede14 100644 --- a/crates/apub/src/protocol/objects/note.rs +++ b/crates/apub/src/protocol/objects/note.rs @@ -58,13 +58,13 @@ impl Note { &self, context: &Data, ) -> Result<(ApubPost, Option), LemmyError> { - // Fetch parent comment chain in a box, otherwise it can cause a stack overflow. + let mut conn = context.conn().await?; // Fetch parent comment chain in a box, otherwise it can cause a stack overflow. let parent = Box::pin(self.in_reply_to.dereference(context).await?); match parent.deref() { PostOrComment::Post(p) => Ok((p.clone(), None)), PostOrComment::Comment(c) => { let post_id = c.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(&mut conn, post_id).await?; Ok((post.into(), Some(c.clone()))) } } @@ -74,8 +74,9 @@ impl Note { #[async_trait::async_trait] impl InCommunity for Note { async fn community(&self, context: &Data) -> Result { + let mut conn = context.conn().await?; let (post, _) = self.get_parents(context).await?; - let community = Community::read(&mut *context.conn().await?, post.community_id).await?; + let community = Community::read(&mut conn, post.community_id).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 9db18e371..578a1cc82 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -79,6 +79,7 @@ pub struct PersonQuery<'a> { impl<'a> PersonQuery<'a> { pub async fn list(self) -> Result, Error> { let conn = self.conn; + let mut query = person::table .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index a45f82135..beb528936 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -122,9 +122,10 @@ async fn get_feed_data( limit: i64, page: i64, ) -> Result { - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; - let mut conn = context.conn().await?; + + let site_view = SiteView::read_local(&mut conn).await?; + let posts = PostQuery::builder() .conn(&mut conn) .listing_type(Some(listing_type)) @@ -162,6 +163,8 @@ async fn get_feed( info: web::Query, context: web::Data, ) -> Result { + let mut conn = context.conn().await?; + let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?; let param: String = req.match_info().get("name").unwrap_or("none").parse()?; @@ -179,7 +182,7 @@ async fn get_feed( let builder = match request_type { RequestType::User => { get_feed_user( - &mut *context.conn().await?, + &mut conn, &info.sort_type()?, &info.get_limit(), &info.get_page(), @@ -190,7 +193,7 @@ async fn get_feed( } RequestType::Community => { get_feed_community( - &mut *context.conn().await?, + &mut conn, &info.sort_type()?, &info.get_limit(), &info.get_page(), @@ -201,7 +204,7 @@ async fn get_feed( } RequestType::Front => { get_feed_front( - &mut *context.conn().await?, + &mut conn, &jwt_secret, &info.sort_type()?, &info.get_limit(), @@ -212,13 +215,7 @@ async fn get_feed( .await } RequestType::Inbox => { - get_feed_inbox( - &mut *context.conn().await?, - &jwt_secret, - ¶m, - &protocol_and_hostname, - ) - .await + get_feed_inbox(&mut conn, &jwt_secret, ¶m, &protocol_and_hostname).await } } .map_err(ErrorBadRequest)?; diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index 307d6d02a..9be34d638 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -128,8 +128,10 @@ async fn full_res( client: web::Data, context: web::Data, ) -> Result { + let mut conn = context.conn().await?; + // block access to images if instance is private and unauthorized, public - let local_site = LocalSite::read(&mut *context.conn().await?) + let local_site = LocalSite::read(&mut conn) .await .map_err(error::ErrorBadRequest)?; if local_site.private_instance { diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index 078e92d7d..1e8d10d14 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -29,7 +29,9 @@ async fn node_info_well_known( } async fn node_info(context: web::Data) -> Result { - let site_view = SiteView::read_local(&mut *context.conn().await?) + let mut conn = context.conn().await?; + + let site_view = SiteView::read_local(&mut conn) .await .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?; diff --git a/crates/routes/src/webfinger.rs b/crates/routes/src/webfinger.rs index 96086cf8c..d914b4dcf 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -35,18 +35,19 @@ async fn get_webfinger_response( info: Query, context: Data, ) -> Result { + let mut conn = context.conn().await?; + let name = extract_webfinger_name(&info.resource, &context)?; let name_ = name.clone(); - let user_id: Option = Person::read_from_name(&mut *context.conn().await?, &name_, false) + let user_id: Option = Person::read_from_name(&mut conn, &name_, false) + .await + .ok() + .map(|c| c.actor_id.into()); + let community_id: Option = Community::read_from_name(&mut conn, &name, false) .await .ok() .map(|c| c.actor_id.into()); - let community_id: Option = - Community::read_from_name(&mut *context.conn().await?, &name, false) - .await - .ok() - .map(|c| c.actor_id.into()); // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put // community last so that it gets prioritized. For Lemmy the order doesnt matter.