From 5db6b556cce37291ef1fc049f4e3a002c45d92bd Mon Sep 17 00:00:00 2001 From: dull b Date: Sat, 1 Jul 2023 03:04:38 +0000 Subject: [PATCH] DbConn trait --- crates/api/src/comment/distinguish.rs | 6 +- crates/api/src/comment/like.rs | 13 +- crates/api/src/comment/save.rs | 6 +- crates/api/src/comment_report/create.rs | 8 +- crates/api/src/comment_report/list.rs | 2 +- crates/api/src/comment_report/resolve.rs | 10 +- crates/api/src/community/add_mod.rs | 10 +- crates/api/src/community/ban.rs | 10 +- crates/api/src/community/block.rs | 6 +- crates/api/src/community/follow.rs | 9 +- crates/api/src/community/hide.rs | 4 +- crates/api/src/community/transfer.rs | 15 +- crates/api/src/lib.rs | 18 +- crates/api/src/local_user/add_admin.rs | 4 +- crates/api/src/local_user/ban_person.rs | 4 +- crates/api/src/local_user/block.rs | 6 +- crates/api/src/local_user/change_password.rs | 2 +- .../local_user/change_password_after_reset.rs | 6 +- crates/api/src/local_user/get_captcha.rs | 4 +- crates/api/src/local_user/list_banned.rs | 2 +- crates/api/src/local_user/login.rs | 4 +- .../local_user/notifications/list_mentions.rs | 2 +- .../local_user/notifications/list_replies.rs | 2 +- .../local_user/notifications/mark_all_read.rs | 6 +- .../notifications/mark_mention_read.rs | 3 +- .../notifications/mark_reply_read.rs | 3 +- .../local_user/notifications/unread_count.rs | 8 +- crates/api/src/local_user/report_count.rs | 4 +- crates/api/src/local_user/reset_password.rs | 2 +- crates/api/src/local_user/save_settings.rs | 6 +- crates/api/src/local_user/verify_email.rs | 6 +- crates/api/src/post/feature.rs | 6 +- crates/api/src/post/like.rs | 8 +- crates/api/src/post/lock.rs | 4 +- crates/api/src/post/mark_read.rs | 3 +- crates/api/src/post/save.rs | 7 +- crates/api/src/post_report/create.rs | 8 +- crates/api/src/post_report/list.rs | 2 +- crates/api/src/post_report/resolve.rs | 10 +- crates/api/src/private_message/mark_read.rs | 4 +- .../api/src/private_message_report/create.rs | 9 +- crates/api/src/private_message_report/list.rs | 2 +- .../api/src/private_message_report/resolve.rs | 6 +- crates/api/src/site/federated_instances.rs | 2 +- crates/api/src/site/leave_admin.rs | 16 +- crates/api/src/site/mod_log.rs | 36 +- crates/api/src/site/purge/comment.rs | 6 +- crates/api/src/site/purge/community.rs | 6 +- crates/api/src/site/purge/person.rs | 6 +- crates/api/src/site/purge/post.rs | 6 +- .../site/registration_applications/approve.rs | 6 +- .../site/registration_applications/list.rs | 4 +- .../registration_applications/unread_count.rs | 2 +- crates/api_common/src/build_response.rs | 22 +- crates/api_common/src/utils.rs | 114 +++--- crates/api_crud/src/comment/create.rs | 11 +- crates/api_crud/src/comment/delete.rs | 4 +- crates/api_crud/src/comment/read.rs | 2 +- crates/api_crud/src/comment/remove.rs | 6 +- crates/api_crud/src/comment/update.rs | 6 +- crates/api_crud/src/community/create.rs | 14 +- crates/api_crud/src/community/delete.rs | 2 +- crates/api_crud/src/community/list.rs | 4 +- crates/api_crud/src/community/remove.rs | 2 +- crates/api_crud/src/community/update.rs | 10 +- crates/api_crud/src/custom_emoji/create.rs | 8 +- crates/api_crud/src/custom_emoji/delete.rs | 2 +- crates/api_crud/src/custom_emoji/update.rs | 10 +- crates/api_crud/src/post/create.rs | 8 +- crates/api_crud/src/post/delete.rs | 2 +- crates/api_crud/src/post/read.rs | 12 +- crates/api_crud/src/post/remove.rs | 4 +- crates/api_crud/src/post/update.rs | 6 +- crates/api_crud/src/private_message/create.rs | 10 +- crates/api_crud/src/private_message/delete.rs | 4 +- crates/api_crud/src/private_message/read.rs | 2 +- crates/api_crud/src/private_message/update.rs | 6 +- crates/api_crud/src/site/create.rs | 13 +- crates/api_crud/src/site/read.rs | 29 +- crates/api_crud/src/site/update.rs | 21 +- crates/api_crud/src/user/create.rs | 11 +- .../apub/src/activities/block/block_user.rs | 10 +- crates/apub/src/activities/block/mod.rs | 20 +- .../src/activities/block/undo_block_user.rs | 8 +- .../activities/community/collection_add.rs | 19 +- .../activities/community/collection_remove.rs | 8 +- .../src/activities/community/lock_page.rs | 7 +- crates/apub/src/activities/community/mod.rs | 2 +- .../apub/src/activities/community/report.rs | 4 +- .../apub/src/activities/community/update.rs | 4 +- .../activities/create_or_update/comment.rs | 16 +- .../src/activities/create_or_update/post.rs | 10 +- .../create_or_update/private_message.rs | 6 +- crates/apub/src/activities/deletion/delete.rs | 6 +- .../src/activities/deletion/delete_user.rs | 2 +- crates/apub/src/activities/deletion/mod.rs | 16 +- .../src/activities/deletion/undo_delete.rs | 6 +- .../apub/src/activities/following/accept.rs | 3 +- .../apub/src/activities/following/follow.rs | 8 +- crates/apub/src/activities/following/mod.rs | 7 +- .../src/activities/following/undo_follow.rs | 4 +- crates/apub/src/activities/mod.rs | 4 +- crates/apub/src/activities/voting/mod.rs | 16 +- crates/apub/src/activities/voting/vote.rs | 2 +- crates/apub/src/api/list_comments.rs | 10 +- crates/apub/src/api/list_posts.rs | 4 +- crates/apub/src/api/read_community.rs | 14 +- crates/apub/src/api/read_person.rs | 10 +- crates/apub/src/api/resolve_object.rs | 12 +- crates/apub/src/api/search.rs | 20 +- .../src/collections/community_moderators.rs | 18 +- crates/apub/src/fetcher/mod.rs | 4 +- crates/apub/src/fetcher/post_or_comment.rs | 8 +- crates/apub/src/http/comment.rs | 2 +- crates/apub/src/http/community.rs | 10 +- crates/apub/src/http/mod.rs | 2 +- crates/apub/src/http/person.rs | 4 +- crates/apub/src/http/post.rs | 2 +- crates/apub/src/http/site.rs | 2 +- crates/apub/src/lib.rs | 10 +- crates/apub/src/mentions.rs | 10 +- crates/apub/src/objects/comment.rs | 30 +- crates/apub/src/objects/community.rs | 16 +- crates/apub/src/objects/instance.rs | 8 +- crates/apub/src/objects/person.rs | 12 +- crates/apub/src/objects/post.rs | 26 +- crates/apub/src/objects/private_message.rs | 20 +- .../activities/community/collection_add.rs | 3 +- .../activities/community/collection_remove.rs | 3 +- .../activities/community/lock_page.rs | 2 +- .../activities/create_or_update/note.rs | 2 +- .../protocol/activities/deletion/delete.rs | 4 +- .../protocol/collections/group_followers.rs | 2 +- crates/apub/src/protocol/objects/group.rs | 2 +- crates/apub/src/protocol/objects/mod.rs | 16 +- crates/apub/src/protocol/objects/note.rs | 4 +- .../src/aggregates/comment_aggregates.rs | 68 ++-- .../src/aggregates/community_aggregates.rs | 84 +++-- .../src/aggregates/person_aggregates.rs | 90 +++-- .../src/aggregates/person_post_aggregates.rs | 11 +- .../src/aggregates/post_aggregates.rs | 77 ++-- .../src/aggregates/site_aggregates.rs | 55 +-- crates/db_schema/src/impls/activity.rs | 47 ++- crates/db_schema/src/impls/actor_language.rs | 293 ++++++++------- crates/db_schema/src/impls/captcha_answer.rs | 24 +- crates/db_schema/src/impls/comment.rs | 121 ++++--- crates/db_schema/src/impls/comment_reply.rs | 72 ++-- crates/db_schema/src/impls/comment_report.rs | 12 +- crates/db_schema/src/impls/community.rs | 139 ++++---- crates/db_schema/src/impls/community_block.rs | 11 +- crates/db_schema/src/impls/custom_emoji.rs | 20 +- .../db_schema/src/impls/email_verification.rs | 12 +- .../src/impls/federation_allowlist.rs | 28 +- .../src/impls/federation_blocklist.rs | 14 +- crates/db_schema/src/impls/instance.rs | 32 +- crates/db_schema/src/impls/language.rs | 22 +- crates/db_schema/src/impls/local_site.rs | 16 +- .../src/impls/local_site_rate_limit.rs | 15 +- crates/db_schema/src/impls/local_user.rs | 45 +-- crates/db_schema/src/impls/moderator.rs | 337 ++++++++++-------- .../src/impls/password_reset_request.rs | 46 +-- crates/db_schema/src/impls/person.rs | 91 ++--- crates/db_schema/src/impls/person_block.rs | 15 +- crates/db_schema/src/impls/person_mention.rs | 62 ++-- crates/db_schema/src/impls/post.rs | 128 ++++--- crates/db_schema/src/impls/post_report.rs | 12 +- crates/db_schema/src/impls/private_message.rs | 57 +-- .../src/impls/private_message_report.rs | 12 +- .../src/impls/registration_application.rs | 23 +- crates/db_schema/src/impls/secret.rs | 8 +- crates/db_schema/src/impls/site.rs | 30 +- crates/db_schema/src/impls/tagline.rs | 24 +- crates/db_schema/src/traits.rs | 53 +-- crates/db_schema/src/utils.rs | 7 +- crates/db_views/src/comment_report_view.rs | 81 +++-- crates/db_views/src/comment_view.rs | 159 +++++---- crates/db_views/src/custom_emoji_view.rs | 8 +- crates/db_views/src/local_user_view.rs | 24 +- crates/db_views/src/post_report_view.rs | 76 ++-- crates/db_views/src/post_view.rs | 162 +++++---- .../src/private_message_report_view.rs | 47 +-- crates/db_views/src/private_message_view.rs | 18 +- .../src/registration_application_view.rs | 97 +++-- crates/db_views/src/site_view.rs | 4 +- .../db_views_actor/src/comment_reply_view.rs | 21 +- .../src/community_block_view.rs | 4 +- .../src/community_follower_view.rs | 8 +- .../src/community_moderator_view.rs | 12 +- .../src/community_person_ban_view.rs | 4 +- crates/db_views_actor/src/community_view.rs | 21 +- .../db_views_actor/src/person_block_view.rs | 4 +- .../db_views_actor/src/person_mention_view.rs | 18 +- crates/db_views_actor/src/person_view.rs | 22 +- .../src/admin_purge_comment_view.rs | 4 +- .../src/admin_purge_community_view.rs | 4 +- .../src/admin_purge_person_view.rs | 4 +- .../src/admin_purge_post_view.rs | 4 +- .../src/mod_add_community_view.rs | 4 +- crates/db_views_moderator/src/mod_add_view.rs | 4 +- .../src/mod_ban_from_community_view.rs | 4 +- crates/db_views_moderator/src/mod_ban_view.rs | 4 +- .../src/mod_feature_post_view.rs | 4 +- .../src/mod_hide_community_view.rs | 4 +- .../src/mod_lock_post_view.rs | 4 +- .../src/mod_remove_comment_view.rs | 4 +- .../src/mod_remove_community_view.rs | 4 +- .../src/mod_remove_post_view.rs | 4 +- .../src/mod_transfer_community_view.rs | 4 +- crates/routes/src/feeds.rs | 38 +- crates/routes/src/images.rs | 2 +- crates/routes/src/nodeinfo.rs | 2 +- crates/routes/src/webfinger.rs | 11 +- src/code_migrations.rs | 98 ++--- src/lib.rs | 6 +- 214 files changed, 2371 insertions(+), 2024 deletions(-) diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index ef513243b..2514d79c3 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -22,7 +22,7 @@ impl Perform for DistinguishComment { 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(context.conn().await?, comment_id, None).await?; check_community_ban( local_user_view.person.id, @@ -44,14 +44,14 @@ impl Perform for DistinguishComment { let form = CommentUpdateForm::builder() .distinguished(Some(data.distinguished)) .build(); - Comment::update(&mut *context.conn().await?, comment_id, &form) + Comment::update(context.conn().await?, 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?; + CommentView::read(context.conn().await?, 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..74ba45f7b 100644 --- a/crates/api/src/comment/like.rs +++ b/crates/api/src/comment/like.rs @@ -25,7 +25,7 @@ impl Perform for CreateCommentLike { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { let data: &CreateCommentLike = self; - let local_site = LocalSite::read(&mut *context.conn().await?).await?; + let local_site = LocalSite::read(context.conn().await?).await?; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let mut recipient_ids = Vec::::new(); @@ -34,7 +34,7 @@ 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(context.conn().await?, comment_id, None).await?; check_community_ban( local_user_view.person.id, @@ -44,12 +44,11 @@ impl Perform for CreateCommentLike { .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(context.conn().await?, 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 + LocalUserView::read_person(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, &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..8fa0e8e4a 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -27,11 +27,11 @@ impl Perform for SaveComment { }; if data.save { - CommentSaved::save(&mut *context.conn().await?, &comment_saved_form) + CommentSaved::save(context.conn().await?, &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(context.conn().await?, &comment_saved_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?; } @@ -39,7 +39,7 @@ impl Perform for SaveComment { 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?; + CommentView::read(context.conn().await?, 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..9df5e70dc 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -27,14 +27,14 @@ impl Perform for CreateCommentReport { ) -> Result { 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(context.conn().await?).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(context.conn().await?, comment_id, None).await?; check_community_ban( person_id, @@ -50,12 +50,12 @@ impl Perform for CreateCommentReport { reason: reason.to_owned(), }; - let report = CommentReport::report(&mut *context.conn().await?, &report_form) + let report = CommentReport::report(context.conn().await?, &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?; + CommentReportView::read(context.conn().await?, report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index 61568794c..99e3fb66c 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -31,7 +31,7 @@ impl Perform for ListCommentReports { let limit = data.limit; let mut conn = context.conn().await?; let comment_reports = CommentReportQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .my_person_id(person_id) .admin(admin) .community_id(community_id) diff --git a/crates/api/src/comment_report/resolve.rs b/crates/api/src/comment_report/resolve.rs index 496184d58..5089403ea 100644 --- a/crates/api/src/comment_report/resolve.rs +++ b/crates/api/src/comment_report/resolve.rs @@ -24,24 +24,24 @@ impl Perform for ResolveCommentReport { 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(context.conn().await?, 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(context.conn().await?, person_id, report.community.id).await?; if data.resolved { - CommentReport::resolve(&mut *context.conn().await?, report_id, person_id) + CommentReport::resolve(context.conn().await?, 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(context.conn().await?, 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?; + CommentReportView::read(context.conn().await?, 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..40211719b 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -36,7 +36,7 @@ impl Perform for AddModToCommunity { community_id, ) .await?; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(context.conn().await?, community_id).await?; if local_user_view.person.admin && !community.local { return Err(LemmyError::from_message("not_a_moderator")); } @@ -47,11 +47,11 @@ impl Perform for AddModToCommunity { person_id: data.person_id, }; if data.added { - CommunityModerator::join(&mut *context.conn().await?, &community_moderator_form) + CommunityModerator::join(context.conn().await?, &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(context.conn().await?, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; } @@ -64,13 +64,13 @@ impl Perform for AddModToCommunity { removed: Some(!data.added), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(context.conn().await?, &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?; + CommunityModeratorView::for_community(context.conn().await?, community_id).await?; Ok(AddModToCommunityResponse { moderators }) } diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 93a588641..73e773289 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -56,7 +56,7 @@ impl Perform for BanFromCommunity { }; if data.ban { - CommunityPersonBan::ban(&mut *context.conn().await?, &community_user_ban_form) + CommunityPersonBan::ban(context.conn().await?, &community_user_ban_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?; @@ -67,11 +67,11 @@ impl Perform for BanFromCommunity { pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(context.conn().await?, &community_follower_form) .await .ok(); } else { - CommunityPersonBan::unban(&mut *context.conn().await?, &community_user_ban_form) + CommunityPersonBan::unban(context.conn().await?, &community_user_ban_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?; } @@ -92,10 +92,10 @@ impl Perform for BanFromCommunity { expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(context.conn().await?, &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(context.conn().await?, 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..13df3dc59 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -35,7 +35,7 @@ impl Perform for BlockCommunity { }; if data.block { - CommunityBlock::block(&mut *context.conn().await?, &community_block_form) + CommunityBlock::block(context.conn().await?, &community_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?; @@ -46,11 +46,11 @@ impl Perform for BlockCommunity { pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(context.conn().await?, &community_follower_form) .await .ok(); } else { - CommunityBlock::unblock(&mut *context.conn().await?, &community_block_form) + CommunityBlock::unblock(context.conn().await?, &community_block_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?; } diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index cacedeeba..abf70264d 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -25,7 +25,7 @@ impl Perform for FollowCommunity { 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(context.conn().await?, community_id).await?; let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: local_user_view.person.id, @@ -41,12 +41,12 @@ impl Perform for FollowCommunity { .await?; check_community_deleted_or_removed(community_id, &mut *context.conn().await?).await?; - CommunityFollower::follow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::follow(context.conn().await?, &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(context.conn().await?, &community_follower_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?; } @@ -60,8 +60,7 @@ impl Perform for FollowCommunity { None, ) .await?; - let discussion_languages = - CommunityLanguage::read(&mut *context.conn().await?, community_id).await?; + let discussion_languages = CommunityLanguage::read(context.conn().await?, 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..61c7b4f85 100644 --- a/crates/api/src/community/hide.rs +++ b/crates/api/src/community/hide.rs @@ -39,11 +39,11 @@ impl Perform for HideCommunity { }; let community_id = data.community_id; - Community::update(&mut *context.conn().await?, community_id, &community_form) + Community::update(context.conn().await?, 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(context.conn().await?, &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..9800480a1 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -33,7 +33,7 @@ impl Perform for TransferCommunity { // Fetch the community mods let community_id = data.community_id; let mut community_mods = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + CommunityModeratorView::for_community(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, &community_moderator_form) .await .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?; } @@ -76,7 +76,7 @@ impl Perform for TransferCommunity { community_id: data.community_id, }; - ModTransferCommunity::create(&mut *context.conn().await?, &form).await?; + ModTransferCommunity::create(context.conn().await?, &form).await?; let community_id = data.community_id; let person_id = local_user_view.person.id; @@ -90,10 +90,9 @@ impl Perform for TransferCommunity { .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(context.conn().await?, 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/lib.rs b/crates/api/src/lib.rs index 1d98d6ce1..079d85bc2 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -94,11 +94,11 @@ mod tests { #[tokio::test] #[serial] async fn test_should_not_validate_user_token_after_password_change() { - let conn = &mut build_db_conn_for_tests().await; - let secret = Secret::init(conn).await.unwrap(); + let mut conn = build_db_conn_for_tests().await; + let secret = Secret::init(&mut *conn).await.unwrap(); let settings = &SETTINGS.to_owned(); - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -108,14 +108,16 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let local_user_form = LocalUserInsertForm::builder() .person_id(inserted_person.id) .password_encrypted("123456".to_string()) .build(); - let inserted_local_user = LocalUser::create(conn, &local_user_form).await.unwrap(); + let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form) + .await + .unwrap(); let jwt = Claims::jwt( inserted_local_user.id.0, @@ -129,13 +131,15 @@ mod tests { // The check should fail, since the validator time is now newer than the jwt issue time let updated_local_user = - LocalUser::update_password(conn, inserted_local_user.id, "password111") + LocalUser::update_password(&mut *conn, inserted_local_user.id, "password111") .await .unwrap(); let check_after = check_validator_time(&updated_local_user.validator_time, &claims); assert!(check_after.is_err()); - let num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); + let num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); assert_eq!(1, num_deleted); } } diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index e9cd562f2..cc5eb1b46 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -44,9 +44,9 @@ impl Perform for AddAdmin { removed: Some(!data.added), }; - ModAdd::create(&mut *context.conn().await?, &form).await?; + ModAdd::create(context.conn().await?, &form).await?; - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let admins = PersonView::admins(context.conn().await?).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..410aa1b78 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -68,10 +68,10 @@ impl Perform for BanPerson { expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(context.conn().await?, &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(context.conn().await?, 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..d9eb1b72f 100644 --- a/crates/api/src/local_user/block.rs +++ b/crates/api/src/local_user/block.rs @@ -34,18 +34,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(context.conn().await?, 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(context.conn().await?, &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(context.conn().await?, &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..30d6a4986 100644 --- a/crates/api/src/local_user/change_password.rs +++ b/crates/api/src/local_user/change_password.rs @@ -38,7 +38,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(context.conn().await?, 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..afa9b550b 100644 --- a/crates/api/src/local_user/change_password_after_reset.rs +++ b/crates/api/src/local_user/change_password_after_reset.rs @@ -22,7 +22,7 @@ impl Perform for PasswordChangeAfterReset { // 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(context.conn().await?, &token) .await .map(|p| p.local_user_id)?; @@ -36,12 +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) + LocalUser::update_password(context.conn().await?, 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(context.conn().await?).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 83667df81..52fd5bda6 100644 --- a/crates/api/src/local_user/get_captcha.rs +++ b/crates/api/src/local_user/get_captcha.rs @@ -17,7 +17,7 @@ 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 local_site = LocalSite::read(context.conn().await?).await?; if !local_site.captcha_enabled { return Ok(GetCaptchaResponse { ok: None }); @@ -37,7 +37,7 @@ impl Perform for GetCaptcha { let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer }; // Stores the captcha item in the db - let captcha = CaptchaAnswer::insert(&mut *context.conn().await?, &captcha_form).await?; + let captcha = CaptchaAnswer::insert(context.conn().await?, &captcha_form).await?; Ok(GetCaptchaResponse { ok: Some(CaptchaResponse { diff --git a/crates/api/src/local_user/list_banned.rs b/crates/api/src/local_user/list_banned.rs index 773b1f493..1ecb0845f 100644 --- a/crates/api/src/local_user/list_banned.rs +++ b/crates/api/src/local_user/list_banned.rs @@ -19,7 +19,7 @@ impl Perform for GetBannedPersons { // Make sure user is an admin is_admin(&local_user_view)?; - let banned = PersonView::banned(&mut *context.conn().await?).await?; + let banned = PersonView::banned(context.conn().await?).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..e72fb6dec 100644 --- a/crates/api/src/local_user/login.rs +++ b/crates/api/src/local_user/login.rs @@ -17,12 +17,12 @@ impl Perform for Login { async fn perform(&self, context: &Data) -> Result { let data: &Login = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).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) + LocalUserView::find_by_email_or_name(context.conn().await?, &username_or_email) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; diff --git a/crates/api/src/local_user/notifications/list_mentions.rs b/crates/api/src/local_user/notifications/list_mentions.rs index 7b6d9b6cf..b35699b56 100644 --- a/crates/api/src/local_user/notifications/list_mentions.rs +++ b/crates/api/src/local_user/notifications/list_mentions.rs @@ -29,7 +29,7 @@ impl Perform for GetPersonMentions { let mut conn = context.conn().await?; let mentions = PersonMentionQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .recipient_id(person_id) .my_person_id(person_id) .sort(sort) diff --git a/crates/api/src/local_user/notifications/list_replies.rs b/crates/api/src/local_user/notifications/list_replies.rs index bb427c4e4..b41341764 100644 --- a/crates/api/src/local_user/notifications/list_replies.rs +++ b/crates/api/src/local_user/notifications/list_replies.rs @@ -26,7 +26,7 @@ impl Perform for GetReplies { let mut conn = context.conn().await?; let replies = CommentReplyQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .recipient_id(person_id) .my_person_id(person_id) .sort(sort) 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..358135aeb 100644 --- a/crates/api/src/local_user/notifications/mark_all_read.rs +++ b/crates/api/src/local_user/notifications/mark_all_read.rs @@ -23,17 +23,17 @@ impl Perform for MarkAllAsRead { 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(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, 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..51c1ee64b 100644 --- a/crates/api/src/local_user/notifications/mark_mention_read.rs +++ b/crates/api/src/local_user/notifications/mark_mention_read.rs @@ -25,8 +25,7 @@ impl Perform for MarkPersonMentionAsRead { 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(context.conn().await?, person_mention_id).await?; if local_user_view.person.id != read_person_mention.recipient_id { return Err(LemmyError::from_message("couldnt_update_comment")); 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..c9837940d 100644 --- a/crates/api/src/local_user/notifications/mark_reply_read.rs +++ b/crates/api/src/local_user/notifications/mark_reply_read.rs @@ -25,8 +25,7 @@ impl Perform for MarkCommentReplyAsRead { 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(context.conn().await?, comment_reply_id).await?; if local_user_view.person.id != read_comment_reply.recipient_id { return Err(LemmyError::from_message("couldnt_update_comment")); diff --git a/crates/api/src/local_user/notifications/unread_count.rs b/crates/api/src/local_user/notifications/unread_count.rs index 49c78dc9c..db66e298c 100644 --- a/crates/api/src/local_user/notifications/unread_count.rs +++ b/crates/api/src/local_user/notifications/unread_count.rs @@ -20,14 +20,12 @@ impl Perform for GetUnreadCount { 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(context.conn().await?, person_id).await?; - let mentions = - PersonMentionView::get_unread_mentions(&mut *context.conn().await?, person_id).await?; + let mentions = PersonMentionView::get_unread_mentions(context.conn().await?, person_id).await?; let private_messages = - PrivateMessageView::get_unread_messages(&mut *context.conn().await?, person_id).await?; + PrivateMessageView::get_unread_messages(context.conn().await?, 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..cdd082597 100644 --- a/crates/api/src/local_user/report_count.rs +++ b/crates/api/src/local_user/report_count.rs @@ -33,11 +33,11 @@ impl Perform for GetReportCount { .await?; let post_reports = - PostReportView::get_report_count(&mut *context.conn().await?, person_id, admin, community_id) + PostReportView::get_report_count(context.conn().await?, 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(context.conn().await?).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..b3f467fad 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -22,7 +22,7 @@ impl Perform for PasswordReset { // 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(context.conn().await?, &email) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 0c8974029..f35809f92 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -35,7 +35,7 @@ impl Perform for SaveUserSettings { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let avatar = diesel_option_overwrite_to_url(&data.avatar)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -95,7 +95,7 @@ impl Perform for SaveUserSettings { .banner(banner) .build(); - Person::update(&mut *context.conn().await?, person_id, &person_form) + Person::update(context.conn().await?, person_id, &person_form) .await .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?; @@ -140,7 +140,7 @@ impl Perform for SaveUserSettings { .build(); let local_user_res = - LocalUser::update(&mut *context.conn().await?, local_user_id, &local_user_form).await; + LocalUser::update(context.conn().await?, 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..fd3f8f6ee 100644 --- a/crates/api/src/local_user/verify_email.rs +++ b/crates/api/src/local_user/verify_email.rs @@ -19,7 +19,7 @@ impl Perform for VerifyEmail { async fn perform(&self, context: &Data) -> Result { let token = self.token.clone(); - let verification = EmailVerification::read_for_token(&mut *context.conn().await?, &token) + let verification = EmailVerification::read_for_token(context.conn().await?, &token) .await .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?; @@ -31,9 +31,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(context.conn().await?, local_user_id, &form).await?; - EmailVerification::delete_old_tokens_for_local_user(&mut *context.conn().await?, local_user_id) + EmailVerification::delete_old_tokens_for_local_user(context.conn().await?, local_user_id) .await?; Ok(VerifyEmailResponse {}) diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index b4bded456..6020a88de 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -32,7 +32,7 @@ impl Perform for FeaturePost { 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(context.conn().await?, post_id).await?; check_community_ban( local_user_view.person.id, @@ -65,7 +65,7 @@ impl Perform for FeaturePost { .featured_local(Some(data.featured)) .build() }; - Post::update(&mut *context.conn().await?, post_id, &new_post).await?; + Post::update(context.conn().await?, post_id, &new_post).await?; // Mod tables let form = ModFeaturePostForm { @@ -75,7 +75,7 @@ impl Perform for FeaturePost { is_featured_community: data.feature_type == PostFeatureType::Community, }; - ModFeaturePost::create(&mut *context.conn().await?, &form).await?; + ModFeaturePost::create(context.conn().await?, &form).await?; build_post_response( context, diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index f0c88a875..31709dcc0 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -29,14 +29,14 @@ impl Perform for CreatePostLike { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).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(context.conn().await?, post_id).await?; check_community_ban( local_user_view.person.id, @@ -55,12 +55,12 @@ 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(context.conn().await?, 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(context.conn().await?, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?; } diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index 638630cad..beb5cf03c 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -30,7 +30,7 @@ impl Perform for LockPost { 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(context.conn().await?, post_id).await?; check_community_ban( local_user_view.person.id, @@ -64,7 +64,7 @@ impl Perform for LockPost { post_id: data.post_id, locked: Some(locked), }; - ModLockPost::create(&mut *context.conn().await?, &form).await?; + ModLockPost::create(context.conn().await?, &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..8fe8614a7 100644 --- a/crates/api/src/post/mark_read.rs +++ b/crates/api/src/post/mark_read.rs @@ -28,8 +28,7 @@ impl Perform for MarkPostAsRead { } // Fetch it - let post_view = - PostView::read(&mut *context.conn().await?, post_id, Some(person_id), None).await?; + let post_view = PostView::read(context.conn().await?, 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..20a7cc8ce 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -27,19 +27,18 @@ impl Perform for SavePost { }; if data.save { - PostSaved::save(&mut *context.conn().await?, &post_saved_form) + PostSaved::save(context.conn().await?, &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(context.conn().await?, &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(context.conn().await?, 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?; diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index ca89d32dc..7084a11ac 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -24,14 +24,14 @@ impl Perform for CreatePostReport { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).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(context.conn().await?, post_id, None, None).await?; check_community_ban( person_id, @@ -49,12 +49,12 @@ impl Perform for CreatePostReport { reason: reason.to_owned(), }; - let report = PostReport::report(&mut *context.conn().await?, &report_form) + let report = PostReport::report(context.conn().await?, &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?; + PostReportView::read(context.conn().await?, report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { diff --git a/crates/api/src/post_report/list.rs b/crates/api/src/post_report/list.rs index 3a8688075..646163347 100644 --- a/crates/api/src/post_report/list.rs +++ b/crates/api/src/post_report/list.rs @@ -31,7 +31,7 @@ impl Perform for ListPostReports { let limit = data.limit; let mut conn = context.conn().await?; let post_reports = PostReportQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .my_person_id(person_id) .admin(admin) .community_id(community_id) diff --git a/crates/api/src/post_report/resolve.rs b/crates/api/src/post_report/resolve.rs index 93c537c91..4e148deb9 100644 --- a/crates/api/src/post_report/resolve.rs +++ b/crates/api/src/post_report/resolve.rs @@ -21,23 +21,23 @@ impl Perform for ResolvePostReport { 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(context.conn().await?, 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(context.conn().await?, person_id, report.community.id).await?; if data.resolved { - PostReport::resolve(&mut *context.conn().await?, report_id, person_id) + PostReport::resolve(context.conn().await?, 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(context.conn().await?, 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?; + PostReportView::read(context.conn().await?, 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..61aa2e718 100644 --- a/crates/api/src/private_message/mark_read.rs +++ b/crates/api/src/private_message/mark_read.rs @@ -27,7 +27,7 @@ impl Perform for MarkPrivateMessageAsRead { // Checking permissions let private_message_id = data.private_message_id; let orig_private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + PrivateMessage::read(context.conn().await?, private_message_id).await?; if local_user_view.person.id != orig_private_message.recipient_id { return Err(LemmyError::from_message("couldnt_update_private_message")); } @@ -43,7 +43,7 @@ impl Perform for MarkPrivateMessageAsRead { .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(context.conn().await?, 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..cc824e3c5 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -23,15 +23,14 @@ impl Perform for CreatePrivateMessageReport { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).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(context.conn().await?, private_message_id).await?; let report_form = PrivateMessageReportForm { creator_id: person_id, @@ -40,12 +39,12 @@ impl Perform for CreatePrivateMessageReport { reason: reason.to_owned(), }; - let report = PrivateMessageReport::report(&mut *context.conn().await?, &report_form) + let report = PrivateMessageReport::report(context.conn().await?, &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?; + PrivateMessageReportView::read(context.conn().await?, report.id).await?; // Email the admins if local_site.reports_email_admins { diff --git a/crates/api/src/private_message_report/list.rs b/crates/api/src/private_message_report/list.rs index 16e358b40..2e3e380d4 100644 --- a/crates/api/src/private_message_report/list.rs +++ b/crates/api/src/private_message_report/list.rs @@ -23,7 +23,7 @@ impl Perform for ListPrivateMessageReports { let limit = self.limit; let mut conn = context.conn().await?; let private_message_reports = PrivateMessageReportQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .unresolved_only(unresolved_only) .page(page) .limit(limit) diff --git a/crates/api/src/private_message_report/resolve.rs b/crates/api/src/private_message_report/resolve.rs index f6ebf3dd4..ee9b51ec0 100644 --- a/crates/api/src/private_message_report/resolve.rs +++ b/crates/api/src/private_message_report/resolve.rs @@ -22,17 +22,17 @@ 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(context.conn().await?, 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(context.conn().await?, 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?; + PrivateMessageReportView::read(context.conn().await?, 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..9eee65d5e 100644 --- a/crates/api/src/site/federated_instances.rs +++ b/crates/api/src/site/federated_instances.rs @@ -14,7 +14,7 @@ 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 site_view = SiteView::read_local(context.conn().await?).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..02b8e7f05 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -31,7 +31,7 @@ impl Perform for LeaveAdmin { 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(context.conn().await?).await?; if admins.len() == 1 { return Err(LemmyError::from_message("cannot_leave_admin")); } @@ -51,17 +51,17 @@ impl Perform for LeaveAdmin { removed: Some(true), }; - ModAdd::create(&mut *context.conn().await?, &form).await?; + ModAdd::create(context.conn().await?, &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(context.conn().await?).await?; + let admins = PersonView::admins(context.conn().await?).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 all_languages = Language::read_all(context.conn().await?).await?; + let discussion_languages = SiteLanguage::read_local_raw(context.conn().await?).await?; + let taglines = Tagline::get_all(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?; + CustomEmojiView::get_all(context.conn().await?, 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..1ae6ac1b0 100644 --- a/crates/api/src/site/mod_log.rs +++ b/crates/api/src/site/mod_log.rs @@ -40,7 +40,7 @@ impl Perform for GetModlog { 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(context.conn().await?).await?; check_private_instance(&local_user_view, &local_site)?; @@ -80,51 +80,47 @@ 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(context.conn().await?, params).await?, _ => Default::default(), }; let locked_posts = match type_ { - All | ModLockPost => ModLockPostView::list(&mut *context.conn().await?, params).await?, + All | ModLockPost => ModLockPostView::list(context.conn().await?, params).await?, _ => Default::default(), }; let featured_posts = match type_ { - All | ModFeaturePost => ModFeaturePostView::list(&mut *context.conn().await?, params).await?, + All | ModFeaturePost => ModFeaturePostView::list(context.conn().await?, params).await?, _ => Default::default(), }; let removed_comments = match type_ { - All | ModRemoveComment => { - ModRemoveCommentView::list(&mut *context.conn().await?, params).await? - } + All | ModRemoveComment => ModRemoveCommentView::list(context.conn().await?, params).await?, _ => Default::default(), }; let banned_from_community = match type_ { All | ModBanFromCommunity => { - ModBanFromCommunityView::list(&mut *context.conn().await?, params).await? + ModBanFromCommunityView::list(context.conn().await?, params).await? } _ => Default::default(), }; let added_to_community = match type_ { - All | ModAddCommunity => { - ModAddCommunityView::list(&mut *context.conn().await?, params).await? - } + All | ModAddCommunity => ModAddCommunityView::list(context.conn().await?, params).await?, _ => Default::default(), }; let transferred_to_community = match type_ { All | ModTransferCommunity => { - ModTransferCommunityView::list(&mut *context.conn().await?, params).await? + ModTransferCommunityView::list(context.conn().await?, 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(context.conn().await?, params).await? } _ => Default::default(), }; @@ -141,40 +137,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(context.conn().await?, params).await?, _ => Default::default(), }, match type_ { - All | ModAdd => ModAddView::list(&mut *context.conn().await?, params).await?, + All | ModAdd => ModAddView::list(context.conn().await?, params).await?, _ => Default::default(), }, match type_ { All | ModRemoveCommunity if other_person_id.is_none() => { - ModRemoveCommunityView::list(&mut *context.conn().await?, params).await? + ModRemoveCommunityView::list(context.conn().await?, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePerson if other_person_id.is_none() => { - AdminPurgePersonView::list(&mut *context.conn().await?, params).await? + AdminPurgePersonView::list(context.conn().await?, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeCommunity if other_person_id.is_none() => { - AdminPurgeCommunityView::list(&mut *context.conn().await?, params).await? + AdminPurgeCommunityView::list(context.conn().await?, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePost if other_person_id.is_none() => { - AdminPurgePostView::list(&mut *context.conn().await?, params).await? + AdminPurgePostView::list(context.conn().await?, params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeComment if other_person_id.is_none() => { - AdminPurgeCommentView::list(&mut *context.conn().await?, params).await? + AdminPurgeCommentView::list(context.conn().await?, params).await? } _ => Default::default(), }, diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 444f80f20..e8dfb0dde 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -29,13 +29,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(context.conn().await?, 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(context.conn().await?, comment_id).await?; // Mod tables let reason = data.reason.clone(); @@ -45,7 +45,7 @@ impl Perform for PurgeComment { post_id, }; - AdminPurgeComment::create(&mut *context.conn().await?, &form).await?; + AdminPurgeComment::create(context.conn().await?, &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..e904fc95d 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -30,7 +30,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(context.conn().await?, community_id).await?; if let Some(banner) = community.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -52,7 +52,7 @@ impl Perform for PurgeCommunity { ) .await?; - Community::delete(&mut *context.conn().await?, community_id).await?; + Community::delete(context.conn().await?, community_id).await?; // Mod tables let reason = data.reason.clone(); @@ -61,7 +61,7 @@ impl Perform for PurgeCommunity { reason, }; - AdminPurgeCommunity::create(&mut *context.conn().await?, &form).await?; + AdminPurgeCommunity::create(context.conn().await?, &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..0f8a0a25f 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -29,7 +29,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(context.conn().await?, person_id).await?; if let Some(banner) = person.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -51,7 +51,7 @@ impl Perform for PurgePerson { ) .await?; - Person::delete(&mut *context.conn().await?, person_id).await?; + Person::delete(context.conn().await?, person_id).await?; // Mod tables let reason = data.reason.clone(); @@ -60,7 +60,7 @@ impl Perform for PurgePerson { reason, }; - AdminPurgePerson::create(&mut *context.conn().await?, &form).await?; + AdminPurgePerson::create(context.conn().await?, &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..7b9bba859 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -30,7 +30,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(context.conn().await?, post_id).await?; // Purge image if let Some(url) = post.url { @@ -47,7 +47,7 @@ impl Perform for PurgePost { let community_id = post.community_id; - Post::delete(&mut *context.conn().await?, post_id).await?; + Post::delete(context.conn().await?, post_id).await?; // Mod tables let reason = data.reason.clone(); @@ -57,7 +57,7 @@ impl Perform for PurgePost { community_id, }; - AdminPurgePost::create(&mut *context.conn().await?, &form).await?; + AdminPurgePost::create(context.conn().await?, &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..59193e7b6 100644 --- a/crates/api/src/site/registration_applications/approve.rs +++ b/crates/api/src/site/registration_applications/approve.rs @@ -37,7 +37,7 @@ impl Perform for ApproveRegistrationApplication { }; let registration_application = - RegistrationApplication::update(&mut *context.conn().await?, app_id, &app_form).await?; + RegistrationApplication::update(context.conn().await?, app_id, &app_form).await?; // Update the local_user row let local_user_form = LocalUserUpdateForm::builder() @@ -54,7 +54,7 @@ impl Perform for ApproveRegistrationApplication { if data.approve { let approved_local_user_view = - LocalUserView::read(&mut *context.conn().await?, approved_user_id).await?; + LocalUserView::read(context.conn().await?, approved_user_id).await?; if approved_local_user_view.local_user.email.is_some() { send_application_approved_email(&approved_local_user_view, context.settings())?; @@ -63,7 +63,7 @@ impl Perform for ApproveRegistrationApplication { // Read the view let registration_application = - RegistrationApplicationView::read(&mut *context.conn().await?, app_id).await?; + RegistrationApplicationView::read(context.conn().await?, 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..658095eb4 100644 --- a/crates/api/src/site/registration_applications/list.rs +++ b/crates/api/src/site/registration_applications/list.rs @@ -17,7 +17,7 @@ impl Perform for ListRegistrationApplications { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -29,7 +29,7 @@ impl Perform for ListRegistrationApplications { let limit = data.limit; let mut conn = context.conn().await?; let registration_applications = RegistrationApplicationQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .unread_only(unread_only) .verified_email_only(Some(verified_email_only)) .page(page) diff --git a/crates/api/src/site/registration_applications/unread_count.rs b/crates/api/src/site/registration_applications/unread_count.rs index 20b3b5567..02ee033d4 100644 --- a/crates/api/src/site/registration_applications/unread_count.rs +++ b/crates/api/src/site/registration_applications/unread_count.rs @@ -16,7 +16,7 @@ impl Perform for GetUnreadRegistrationApplicationCount { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; // Only let admins do this is_admin(&local_user_view)?; diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 61294c53b..c9c3e440d 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -30,7 +30,7 @@ pub async fn build_comment_response( recipient_ids: Vec, ) -> Result { 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(context.conn().await?, comment_id, person_id).await?; Ok(CommentResponse { comment_view, recipient_ids, @@ -58,8 +58,7 @@ pub async fn build_community_response( Some(is_mod_or_admin), ) .await?; - let discussion_languages = - CommunityLanguage::read(&mut *context.conn().await?, community_id).await?; + let discussion_languages = CommunityLanguage::read(context.conn().await?, community_id).await?; Ok(CommunityResponse { community_view, @@ -73,7 +72,7 @@ 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 is_mod_or_admin = is_mod_or_admin(context.conn().await?, person_id, community_id) .await .is_ok(); let post_view = PostView::read( @@ -105,7 +104,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(context.conn().await?, &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 +119,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(context.conn().await?, &user_mention_form) .await .ok(); @@ -139,7 +138,7 @@ 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(context.conn().await?, parent_comment_id).await?; // Get the parent commenter local_user let parent_creator_id = parent_comment.creator_id; @@ -152,8 +151,7 @@ pub async fn send_local_notifs( // 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(context.conn().await?, parent_creator_id).await; if let Ok(parent_user_view) = user_view { recipient_ids.push(parent_user_view.local_user.id); @@ -165,7 +163,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(context.conn().await?, &comment_reply_form) .await .ok(); @@ -190,7 +188,7 @@ pub async fn send_local_notifs( 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(context.conn().await?, creator_id).await; if let Ok(parent_user_view) = parent_user { recipient_ids.push(parent_user_view.local_user.id); @@ -202,7 +200,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(context.conn().await?, &comment_reply_form) .await .ok(); diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 51fa0066e..c65294bdd 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -50,11 +50,11 @@ use url::{ParseError, Url}; #[tracing::instrument(skip_all)] pub async fn is_mod_or_admin( - conn: &mut DbConn, + mut conn: impl DbConn, person_id: PersonId, community_id: CommunityId, ) -> Result<(), LemmyError> { - let is_mod_or_admin = CommunityView::is_mod_or_admin(conn, person_id, community_id).await?; + let is_mod_or_admin = CommunityView::is_mod_or_admin(&mut *conn, person_id, community_id).await?; if !is_mod_or_admin { return Err(LemmyError::from_message("not_a_mod_or_admin")); } @@ -63,13 +63,13 @@ pub async fn is_mod_or_admin( #[tracing::instrument(skip_all)] pub async fn is_mod_or_admin_opt( - conn: &mut DbConn, + mut conn: impl DbConn, local_user_view: Option<&LocalUserView>, community_id: Option, ) -> Result<(), LemmyError> { if let Some(local_user_view) = local_user_view { if let Some(community_id) = community_id { - is_mod_or_admin(conn, local_user_view.person.id, community_id).await + is_mod_or_admin(&mut *conn, local_user_view.person.id, community_id).await } else { is_admin(local_user_view) } @@ -101,8 +101,8 @@ pub fn is_top_mod( } #[tracing::instrument(skip_all)] -pub async fn get_post(post_id: PostId, conn: &mut DbConn) -> Result { - Post::read(conn, post_id) +pub async fn get_post(post_id: PostId, mut conn: impl DbConn) -> Result { + Post::read(&mut *conn, post_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post")) } @@ -111,11 +111,11 @@ pub async fn get_post(post_id: PostId, conn: &mut DbConn) -> Result Result { let post_read_form = PostReadForm { post_id, person_id }; - PostRead::mark_as_read(conn, &post_read_form) + PostRead::mark_as_read(&mut *conn, &post_read_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read")) } @@ -124,11 +124,11 @@ pub async fn mark_post_as_read( pub async fn mark_post_as_unread( person_id: PersonId, post_id: PostId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result { let post_read_form = PostReadForm { post_id, person_id }; - PostRead::mark_as_unread(conn, &post_read_form) + PostRead::mark_as_unread(&mut *conn, &post_read_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read")) } @@ -142,7 +142,7 @@ pub async fn local_user_view_from_jwt( .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(context.conn().await?, local_user_id).await?; check_user_valid( local_user_view.person.banned, local_user_view.person.ban_expires, @@ -197,9 +197,9 @@ pub fn check_user_valid( pub async fn check_community_ban( person_id: PersonId, community_id: CommunityId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result<(), LemmyError> { - let is_banned = CommunityPersonBanView::get(conn, person_id, community_id) + let is_banned = CommunityPersonBanView::get(&mut *conn, person_id, community_id) .await .is_ok(); if is_banned { @@ -212,9 +212,9 @@ pub async fn check_community_ban( #[tracing::instrument(skip_all)] pub async fn check_community_deleted_or_removed( community_id: CommunityId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result<(), LemmyError> { - let community = Community::read(conn, community_id) + let community = Community::read(&mut *conn, community_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; if community.deleted || community.removed { @@ -236,9 +236,9 @@ pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> { pub async fn check_person_block( my_id: PersonId, potential_blocker_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result<(), LemmyError> { - let is_blocked = PersonBlock::read(conn, potential_blocker_id, my_id) + let is_blocked = PersonBlock::read(&mut *conn, potential_blocker_id, my_id) .await .is_ok(); if is_blocked { @@ -279,9 +279,9 @@ pub async fn build_federated_instances( if local_site.federation_enabled { // TODO I hate that this requires 3 queries let (linked, allowed, blocked) = try_join!( - Instance::linked(conn_0), - Instance::allowlist(conn_1), - Instance::blocklist(conn_2) + Instance::linked(&mut *conn_0), + Instance::allowlist(&mut *conn_1), + Instance::blocklist(&mut *conn_2) )?; Ok(Some(FederatedInstances { @@ -338,7 +338,7 @@ pub fn send_email_to_user( pub async fn send_password_reset_email( user: &LocalUserView, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { // Generate a random token @@ -347,7 +347,7 @@ pub async fn send_password_reset_email( // Insert the row let token2 = token.clone(); let local_user_id = user.local_user.id; - PasswordResetRequest::create_token(conn, local_user_id, &token2).await?; + PasswordResetRequest::create_token(&mut *conn, local_user_id, &token2).await?; let email = &user.local_user.email.clone().expect("email"); let lang = get_interface_language(user); @@ -362,7 +362,7 @@ pub async fn send_password_reset_email( pub async fn send_verification_email( user: &LocalUserView, new_email: &str, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { let form = EmailVerificationForm { @@ -375,7 +375,7 @@ pub async fn send_verification_email( settings.get_protocol_and_hostname(), &form.verification_token ); - EmailVerification::create(conn, &form).await?; + EmailVerification::create(&mut *conn, &form).await?; let lang = get_interface_language(user); let subject = lang.verify_email_subject(&settings.hostname); @@ -453,11 +453,11 @@ pub fn send_application_approved_email( /// Send a new applicant email notification to all admins pub async fn send_new_applicant_email_to_admins( applicant_username: &str, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails - let admins = LocalUserView::list_admins_with_emails(conn).await?; + let admins = LocalUserView::list_admins_with_emails(&mut *conn).await?; let applications_link = &format!( "{}/registration_applications", @@ -478,11 +478,11 @@ pub async fn send_new_applicant_email_to_admins( pub async fn send_new_report_email_to_admins( reporter_username: &str, reported_username: &str, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails - let admins = LocalUserView::list_admins_with_emails(conn).await?; + let admins = LocalUserView::list_admins_with_emails(&mut *conn).await?; let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),); @@ -499,7 +499,7 @@ pub async fn send_new_report_email_to_admins( pub async fn check_registration_application( local_user_view: &LocalUserView, local_site: &LocalSite, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result<(), LemmyError> { if (local_site.registration_mode == RegistrationMode::RequireApplication || local_site.registration_mode == RegistrationMode::Closed) @@ -508,7 +508,8 @@ pub async fn check_registration_application( { // Fetch the registration, see if its denied let local_user_id = local_user_view.local_user.id; - let registration = RegistrationApplication::find_by_local_user_id(conn, local_user_id).await?; + let registration = + RegistrationApplication::find_by_local_user_id(&mut *conn, local_user_id).await?; if let Some(deny_reason) = registration.deny_reason { let lang = get_interface_language(local_user_view); let registration_denied_message = format!("{}: {}", lang.registration_denied(), &deny_reason); @@ -533,11 +534,11 @@ pub fn check_private_instance_and_federation_enabled( pub async fn purge_image_posts_for_person( banned_person_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { - let posts = Post::fetch_pictrs_posts_for_creator(conn, banned_person_id).await?; + let posts = Post::fetch_pictrs_posts_for_creator(&mut *conn, banned_person_id).await?; for post in posts { if let Some(url) = post.url { purge_image_from_pictrs(client, settings, &url).await.ok(); @@ -549,18 +550,18 @@ pub async fn purge_image_posts_for_person( } } - Post::remove_pictrs_post_images_and_thumbnails_for_creator(conn, banned_person_id).await?; + Post::remove_pictrs_post_images_and_thumbnails_for_creator(&mut *conn, banned_person_id).await?; Ok(()) } pub async fn purge_image_posts_for_community( banned_community_id: CommunityId, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { - let posts = Post::fetch_pictrs_posts_for_community(conn, banned_community_id).await?; + let posts = Post::fetch_pictrs_posts_for_community(&mut *conn, banned_community_id).await?; for post in posts { if let Some(url) = post.url { purge_image_from_pictrs(client, settings, &url).await.ok(); @@ -572,19 +573,20 @@ pub async fn purge_image_posts_for_community( } } - Post::remove_pictrs_post_images_and_thumbnails_for_community(conn, banned_community_id).await?; + Post::remove_pictrs_post_images_and_thumbnails_for_community(&mut *conn, banned_community_id) + .await?; Ok(()) } pub async fn remove_user_data( banned_person_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { // Purge user images - let person = Person::read(conn, banned_person_id).await?; + let person = Person::read(&mut *conn, banned_person_id).await?; if let Some(avatar) = person.avatar { purge_image_from_pictrs(client, settings, &avatar) .await @@ -598,7 +600,7 @@ pub async fn remove_user_data( // Update the fields to None Person::update( - conn, + &mut *conn, banned_person_id, &PersonUpdateForm::builder() .avatar(Some(None)) @@ -608,15 +610,15 @@ pub async fn remove_user_data( .await?; // Posts - Post::update_removed_for_creator(conn, banned_person_id, None, true).await?; + Post::update_removed_for_creator(&mut *conn, banned_person_id, None, true).await?; // Purge image posts - purge_image_posts_for_person(banned_person_id, conn, settings, client).await?; + purge_image_posts_for_person(banned_person_id, &mut *conn, settings, client).await?; // Communities // Remove all communities where they're the top mod // for now, remove the communities manually - let first_mod_communities = CommunityModeratorView::get_community_first_mods(conn).await?; + let first_mod_communities = CommunityModeratorView::get_community_first_mods(&mut *conn).await?; // Filter to only this banned users top communities let banned_user_first_communities: Vec = first_mod_communities @@ -627,7 +629,7 @@ pub async fn remove_user_data( for first_mod_community in banned_user_first_communities { let community_id = first_mod_community.community.id; Community::update( - conn, + &mut *conn, community_id, &CommunityUpdateForm::builder().removed(Some(true)).build(), ) @@ -644,7 +646,7 @@ pub async fn remove_user_data( } // Update the fields to None Community::update( - conn, + &mut *conn, community_id, &CommunityUpdateForm::builder() .icon(Some(None)) @@ -655,7 +657,7 @@ pub async fn remove_user_data( } // Comments - Comment::update_removed_for_creator(conn, banned_person_id, true).await?; + Comment::update_removed_for_creator(&mut *conn, banned_person_id, true).await?; Ok(()) } @@ -663,15 +665,15 @@ pub async fn remove_user_data( pub async fn remove_user_data_in_community( community_id: CommunityId, banned_person_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result<(), LemmyError> { // Posts - Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true).await?; + Post::update_removed_for_creator(&mut *conn, banned_person_id, Some(community_id), true).await?; // Comments // TODO Diesel doesn't allow updates with joins, so this has to be a loop let comments = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .creator_id(Some(banned_person_id)) .community_id(Some(community_id)) .limit(Some(i64::MAX)) @@ -682,7 +684,7 @@ pub async fn remove_user_data_in_community( for comment_view in &comments { let comment_id = comment_view.comment.id; Comment::update( - conn, + &mut *conn, comment_id, &CommentUpdateForm::builder().removed(Some(true)).build(), ) @@ -694,12 +696,12 @@ pub async fn remove_user_data_in_community( pub async fn delete_user_account( person_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { // Delete their images - let person = Person::read(conn, person_id).await?; + let person = Person::read(&mut *conn, person_id).await?; if let Some(avatar) = person.avatar { purge_image_from_pictrs(client, settings, &avatar) .await @@ -713,22 +715,22 @@ pub async fn delete_user_account( // No need to update avatar and banner, those are handled in Person::delete_account // Comments - Comment::permadelete_for_creator(conn, person_id) + Comment::permadelete_for_creator(&mut *conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; // Posts - Post::permadelete_for_creator(conn, person_id) + Post::permadelete_for_creator(&mut *conn, person_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_post"))?; // Purge image posts - purge_image_posts_for_person(person_id, conn, settings, client).await?; + purge_image_posts_for_person(person_id, &mut *conn, settings, client).await?; // Leave communities they mod - CommunityModerator::leave_all_communities(conn, person_id).await?; + CommunityModerator::leave_all_communities(&mut *conn, person_id).await?; - Person::delete_account(conn, person_id).await?; + Person::delete_account(&mut *conn, person_id).await?; Ok(()) } diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 79ed5be2b..db9dfc8ff 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -43,7 +43,7 @@ impl PerformCrud for CreateComment { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -72,9 +72,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(context.conn().await?, parent_id).await.ok() } else { None }; @@ -156,15 +154,14 @@ impl PerformCrud for CreateComment { score: 1, }; - CommentLike::like(&mut *context.conn().await?, &like_form) + CommentLike::like(context.conn().await?, &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(context.conn().await?, parent_id).await; if let Ok(reply) = comment_reply { CommentReply::update( &mut *context.conn().await?, diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index 6f33530ee..f62b13730 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -26,7 +26,7 @@ impl PerformCrud for DeleteComment { 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(context.conn().await?, comment_id, None).await?; // Dont delete it if its already been deleted. if orig_comment.comment.deleted == data.deleted { @@ -56,7 +56,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(context.conn().await?, 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..573a9004e 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -17,7 +17,7 @@ impl PerformCrud for GetComment { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).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..05e40e023 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -27,7 +27,7 @@ impl PerformCrud for RemoveComment { 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(context.conn().await?, comment_id, None).await?; check_community_ban( local_user_view.person.id, @@ -61,10 +61,10 @@ impl PerformCrud for RemoveComment { removed: Some(removed), reason: data.reason.clone(), }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(context.conn().await?, &form).await?; let post_id = updated_comment.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(context.conn().await?, 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..9a60ef3cb 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -33,10 +33,10 @@ impl PerformCrud for EditComment { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).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(context.conn().await?, comment_id, None).await?; check_community_ban( local_user_view.person.id, @@ -72,7 +72,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(context.conn().await?, 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..28a97b526 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -48,7 +48,7 @@ impl PerformCrud for CreateCommunity { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let local_site = site_view.local_site; if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() { @@ -76,7 +76,7 @@ impl PerformCrud for CreateCommunity { &context.settings().get_protocol_and_hostname(), )?; let community_dupe = - Community::read_from_apub_id(&mut *context.conn().await?, &community_actor_id).await?; + Community::read_from_apub_id(context.conn().await?, &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(context.conn().await?, &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(context.conn().await?, &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(context.conn().await?, &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(context.conn().await?).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(context.conn().await?, 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..f29d9788a 100644 --- a/crates/api_crud/src/community/delete.rs +++ b/crates/api_crud/src/community/delete.rs @@ -25,7 +25,7 @@ impl PerformCrud for DeleteCommunity { // Fetch the community mods let community_id = data.community_id; let community_mods = - CommunityModeratorView::for_community(&mut *context.conn().await?, community_id).await?; + CommunityModeratorView::for_community(context.conn().await?, community_id).await?; // Make sure deleter is the top mod is_top_mod(&local_user_view, &community_mods)?; diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index 6ab31fd0c..5f11e5878 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -20,7 +20,7 @@ impl PerformCrud for ListCommunities { ) -> Result { 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(context.conn().await?).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); check_private_instance(&local_user_view, &local_site)?; @@ -33,7 +33,7 @@ impl PerformCrud for ListCommunities { let local_user = local_user_view.map(|l| l.local_user); let mut conn = context.conn().await?; let communities = CommunityQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .listing_type(listing_type) .show_nsfw(show_nsfw) .sort(sort) diff --git a/crates/api_crud/src/community/remove.rs b/crates/api_crud/src/community/remove.rs index d93800b33..d187781dc 100644 --- a/crates/api_crud/src/community/remove.rs +++ b/crates/api_crud/src/community/remove.rs @@ -49,7 +49,7 @@ impl PerformCrud for RemoveCommunity { reason: data.reason.clone(), expires, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(context.conn().await?, &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..75abbaf43 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -30,7 +30,7 @@ impl PerformCrud for EditCommunity { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let icon = diesel_option_overwrite_to_url(&data.icon)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -44,7 +44,7 @@ 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) + CommunityModeratorView::for_community(context.conn().await?, community_id) .await .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; if !mods.contains(&local_user_view.person.id) { @@ -53,14 +53,14 @@ impl PerformCrud for EditCommunity { 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(context.conn().await?).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(context.conn().await?, 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(context.conn().await?, 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..e2a71d80a 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -22,7 +22,7 @@ impl PerformCrud for CreateCustomEmoji { 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(context.conn().await?).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -33,7 +33,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(context.conn().await?, &emoji_form).await?; let mut keywords = vec![]; for keyword in &data.keywords { let keyword_form = CustomEmojiKeywordInsertForm::builder() @@ -42,8 +42,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(context.conn().await?, keywords).await?; + let view = CustomEmojiView::get(context.conn().await?, 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..95e9d4c6b 100644 --- a/crates/api_crud/src/custom_emoji/delete.rs +++ b/crates/api_crud/src/custom_emoji/delete.rs @@ -22,7 +22,7 @@ impl PerformCrud for DeleteCustomEmoji { // Make sure user is an admin is_admin(&local_user_view)?; - CustomEmoji::delete(&mut *context.conn().await?, data.id).await?; + CustomEmoji::delete(context.conn().await?, 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..f3ca1b152 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -22,7 +22,7 @@ impl PerformCrud for EditCustomEmoji { 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(context.conn().await?).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -32,8 +32,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(context.conn().await?, data.id, &emoji_form).await?; + CustomEmojiKeyword::delete(context.conn().await?, data.id).await?; let mut keywords = vec![]; for keyword in &data.keywords { let keyword_form = CustomEmojiKeywordInsertForm::builder() @@ -42,8 +42,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(context.conn().await?, keywords).await?; + let view = CustomEmojiView::get(context.conn().await?, 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 b4cedfba6..57e8a0899 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -46,7 +46,7 @@ impl PerformCrud for CreatePost { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let slur_regex = local_site_to_slur_regex(&local_site); check_slurs(&data.name, &slur_regex)?; @@ -68,7 +68,7 @@ impl PerformCrud for CreatePost { check_community_deleted_or_removed(data.community_id, &mut *context.conn().await?).await?; let community_id = data.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(context.conn().await?, community_id).await?; if community.posting_restricted_to_mods { let community_id = data.community_id; let is_mod = CommunityView::is_mod_or_admin( @@ -121,7 +121,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(context.conn().await?, &post_form).await { Ok(post) => post, Err(e) => { let err_type = if e.to_string() == "value too long for type character varying(200)" { @@ -158,7 +158,7 @@ impl PerformCrud for CreatePost { score: 1, }; - PostLike::like(&mut *context.conn().await?, &like_form) + PostLike::like(context.conn().await?, &like_form) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?; diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index 4f9341647..9a7c49e65 100644 --- a/crates/api_crud/src/post/delete.rs +++ b/crates/api_crud/src/post/delete.rs @@ -22,7 +22,7 @@ impl PerformCrud for DeletePost { 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(context.conn().await?, post_id).await?; // Dont delete it if its already been deleted. if orig_post.deleted == data.deleted { diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 232adaf7f..d4e447a84 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -27,7 +27,7 @@ impl PerformCrud for GetPost { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; check_private_instance(&local_user_view, &local_site)?; @@ -37,7 +37,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(context.conn().await?, comment_id) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))? .post_id @@ -46,7 +46,7 @@ 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) + let community_id = Post::read(context.conn().await?, post_id) .await? .community_id; let is_mod_or_admin = is_mod_or_admin_opt( @@ -92,18 +92,18 @@ impl PerformCrud for GetPost { read_comments, ..PersonPostAggregatesForm::default() }; - PersonPostAggregates::upsert(&mut *context.conn().await?, &person_post_agg_form) + PersonPostAggregates::upsert(context.conn().await?, &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?; + CommunityModeratorView::for_community(context.conn().await?, 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(context.conn().await?) .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..d88ea1512 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -25,7 +25,7 @@ impl PerformCrud for RemovePost { 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(context.conn().await?, post_id).await?; check_community_ban( local_user_view.person.id, @@ -59,7 +59,7 @@ impl PerformCrud for RemovePost { removed: Some(removed), reason: data.reason.clone(), }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(context.conn().await?, &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 20d1b39a1..2bc6da787 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -32,7 +32,7 @@ impl PerformCrud for EditPost { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let data_url = data.url.as_ref(); @@ -52,7 +52,7 @@ 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(context.conn().await?, post_id).await?; check_community_ban( local_user_view.person.id, @@ -96,7 +96,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(context.conn().await?, 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..7c37f6743 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -37,7 +37,7 @@ impl PerformCrud for CreatePrivateMessage { ) -> Result { 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(context.conn().await?).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -59,7 +59,7 @@ impl PerformCrud for CreatePrivateMessage { .build(); let inserted_private_message = - match PrivateMessage::create(&mut *context.conn().await?, &private_message_form).await { + match PrivateMessage::create(context.conn().await?, &private_message_form).await { Ok(private_message) => private_message, Err(e) => { return Err(LemmyError::from_error_message( @@ -86,14 +86,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(context.conn().await?, 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(context.conn().await?, 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..b5c49b442 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -27,7 +27,7 @@ impl PerformCrud for DeletePrivateMessage { // Checking permissions let private_message_id = data.private_message_id; let orig_private_message = - PrivateMessage::read(&mut *context.conn().await?, private_message_id).await?; + PrivateMessage::read(context.conn().await?, 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")); } @@ -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(context.conn().await?, 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..7b58517df 100644 --- a/crates/api_crud/src/private_message/read.rs +++ b/crates/api_crud/src/private_message/read.rs @@ -25,7 +25,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(context.conn().await?) .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..86c0568b0 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -30,12 +30,12 @@ impl PerformCrud for EditPrivateMessage { ) -> Result { 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(context.conn().await?).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?; + PrivateMessage::read(context.conn().await?, 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")); } @@ -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(context.conn().await?, 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..8efba6752 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -50,7 +50,7 @@ impl PerformCrud for CreateSite { 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(context.conn().await?).await?; // Make sure user is an admin; other types of users should not create site data... is_admin(&local_user_view)?; @@ -75,7 +75,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(context.conn().await?, site_id, &site_form).await?; let local_site_form = LocalSiteUpdateForm::builder() // Set the site setup to true @@ -100,7 +100,7 @@ impl PerformCrud for CreateSite { .captcha_difficulty(data.captcha_difficulty.clone()) .build(); - LocalSite::update(&mut *context.conn().await?, &local_site_form).await?; + LocalSite::update(context.conn().await?, &local_site_form).await?; let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder() .message(data.rate_limit_message) @@ -117,13 +117,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(context.conn().await?, &local_site_rate_limit_form).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).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(context.conn().await?, 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..a07caea0f 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -32,9 +32,9 @@ impl PerformCrud for GetSite { async fn perform(&self, context: &Data) -> Result { let data: &GetSite = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).await?; - let admins = PersonView::admins(&mut *context.conn().await?).await?; + let admins = PersonView::admins(context.conn().await?).await?; // Build the local user let my_user = if let Some(local_user_view) = @@ -43,28 +43,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(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, 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(context.conn().await?, local_user_id) + .await + .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?; Some(MyUserInfo { local_user_view, @@ -78,11 +77,11 @@ 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 all_languages = Language::read_all(context.conn().await?).await?; + let discussion_languages = SiteLanguage::read_local_raw(context.conn().await?).await?; + let taglines = Tagline::get_all(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?; + CustomEmojiView::get_all(context.conn().await?, site_view.local_site.id).await?; Ok(GetSiteResponse { site_view, @@ -108,7 +107,7 @@ async fn local_user_settings_view_from_jwt_opt( .ok()? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(&mut *context.conn().await.ok()?, local_user_id) + let local_user_view = LocalUserView::read(context.conn().await.ok()?, local_user_id) .await .ok()?; check_user_valid( diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 32115aea1..4f41c4208 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -46,7 +46,7 @@ impl PerformCrud for EditSite { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; let local_site = site_view.local_site; let site = site_view.site; @@ -73,7 +73,7 @@ impl PerformCrud for EditSite { .updated(Some(Some(naive_now()))) .build(); - Site::update(&mut *context.conn().await?, site.id, &site_form) + Site::update(context.conn().await?, 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,7 +101,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) + let update_local_site = LocalSite::update(context.conn().await?, &local_site_form) .await .ok(); @@ -120,15 +120,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(context.conn().await?, &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(context.conn().await?, allowed).await?; let blocked = data.blocked_instances.clone(); - FederationBlockList::replace(&mut *context.conn().await?, blocked).await?; + FederationBlockList::replace(context.conn().await?, 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 +142,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(context.conn().await?) .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?; } @@ -152,16 +152,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(context.conn().await?) .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(context.conn().await?, local_site.id, new_taglines).await?; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).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 d75a3c72d..7f7957645 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -45,7 +45,7 @@ impl PerformCrud for Register { async fn perform(&self, context: &Data) -> Result { let data: &Register = self; - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).await?; let local_site = site_view.local_site; let require_registration_application = local_site.registration_mode == RegistrationMode::RequireApplication; @@ -104,7 +104,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(context.conn().await?, email).await? { return Err(LemmyError::from_message("email_already_exists")); } } @@ -125,7 +125,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(context.conn().await?, &person_form) .await .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?; @@ -142,8 +142,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(context.conn().await?, &local_user_form).await?; if local_site.site_setup && require_registration_application { // Create the registration application @@ -153,7 +152,7 @@ impl PerformCrud for Register { answer: data.answer.clone().expect("must have an answer"), }; - RegistrationApplication::create(&mut *context.conn().await?, &form).await?; + RegistrationApplication::create(context.conn().await?, &form).await?; } // Email the admins diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index a143344b3..2442564bc 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -97,7 +97,7 @@ impl BlockUser { match target { SiteOrCommunity::Site(_) => { - let inboxes = remote_instance_inboxes(&mut *context.conn().await?).await?; + let inboxes = remote_instance_inboxes(context.conn().await?).await?; send_lemmy_activity(context, block, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -181,7 +181,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(context.conn().await?, &form).await?; } SiteOrCommunity::Community(community) => { let community_user_ban_form = CommunityPersonBanForm { @@ -189,7 +189,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(context.conn().await?, &community_user_ban_form).await?; // Also unsubscribe them from the community, if they are subscribed let community_follower_form = CommunityFollowerForm { @@ -197,7 +197,7 @@ impl ActivityHandler for BlockUser { person_id: blocked_person.id, pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::unfollow(context.conn().await?, &community_follower_form) .await .ok(); @@ -219,7 +219,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(context.conn().await?, &form).await?; } } diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index 99c2f69a2..6a152dda7 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -118,9 +118,12 @@ impl SiteOrCommunity { } } -async fn generate_cc(target: &SiteOrCommunity, conn: &mut DbConn) -> Result, LemmyError> { +async fn generate_cc( + target: &SiteOrCommunity, + mut conn: impl DbConn, +) -> Result, LemmyError> { Ok(match target { - SiteOrCommunity::Site(_) => Site::read_remote_sites(conn) + SiteOrCommunity::Site(_) => Site::read_remote_sites(&mut *conn) .await? .into_iter() .map(|s| s.actor_id.into()) @@ -139,9 +142,9 @@ impl SendActivity for BanPerson { context: &Data, ) -> Result<(), LemmyError> { 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 person = Person::read(context.conn().await?, request.person_id).await?; let site = SiteOrCommunity::Site( - SiteView::read_local(&mut *context.conn().await?) + SiteView::read_local(context.conn().await?) .await? .site .into(), @@ -187,11 +190,10 @@ impl SendActivity for BanFromCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, request.community_id) + .await? + .into(); + let banned_person: ApubPerson = Person::read(context.conn().await?, request.person_id) .await? .into(); let expires = request.expires.map(naive_from_unix); diff --git a/crates/apub/src/activities/block/undo_block_user.rs b/crates/apub/src/activities/block/undo_block_user.rs index 2db1e0ce0..592dc8ffa 100644 --- a/crates/apub/src/activities/block/undo_block_user.rs +++ b/crates/apub/src/activities/block/undo_block_user.rs @@ -62,7 +62,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(context.conn().await?).await?); send_lemmy_activity(context, undo, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -120,7 +120,7 @@ impl ActivityHandler for UndoBlockUser { banned: Some(false), expires, }; - ModBan::create(&mut *context.conn().await?, &form).await?; + ModBan::create(context.conn().await?, &form).await?; } SiteOrCommunity::Community(community) => { let community_user_ban_form = CommunityPersonBanForm { @@ -128,7 +128,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(context.conn().await?, &community_user_ban_form).await?; // write to mod log let form = ModBanFromCommunityForm { @@ -139,7 +139,7 @@ impl ActivityHandler for UndoBlockUser { banned: Some(false), expires, }; - ModBanFromCommunity::create(&mut *context.conn().await?, &form).await?; + ModBanFromCommunity::create(context.conn().await?, &form).await?; } } diff --git a/crates/apub/src/activities/community/collection_add.rs b/crates/apub/src/activities/community/collection_add.rs index 2936b83ed..96ac8f99f 100644 --- a/crates/apub/src/activities/community/collection_add.rs +++ b/crates/apub/src/activities/community/collection_add.rs @@ -119,7 +119,7 @@ impl ActivityHandler for CollectionAdd { async fn receive(self, context: &Data) -> Result<(), LemmyError> { 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(context.conn().await?, &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let new_mod = ObjectId::::from(self.object) @@ -139,7 +139,7 @@ impl ActivityHandler for CollectionAdd { community_id: community.id, person_id: new_mod.id, }; - CommunityModerator::join(&mut *context.conn().await?, &form).await?; + CommunityModerator::join(context.conn().await?, &form).await?; // write mod log let actor = self.actor.dereference(context).await?; @@ -149,7 +149,7 @@ impl ActivityHandler for CollectionAdd { community_id: community.id, removed: Some(false), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(context.conn().await?, &form).await?; } // TODO: send websocket notification about added mod } @@ -160,7 +160,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(context.conn().await?, post.id, &form).await?; } } Ok(()) @@ -177,11 +177,10 @@ impl SendActivity for AddModToCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, request.community_id) + .await? + .into(); + let updated_mod: ApubPerson = Person::read(context.conn().await?, request.person_id) .await? .into(); if request.added { @@ -214,7 +213,7 @@ impl SendActivity for FeaturePost { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, 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..d6e67be27 100644 --- a/crates/apub/src/activities/community/collection_remove.rs +++ b/crates/apub/src/activities/community/collection_remove.rs @@ -112,7 +112,7 @@ impl ActivityHandler for CollectionRemove { async fn receive(self, context: &Data) -> Result<(), LemmyError> { 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(context.conn().await?, &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let remove_mod = ObjectId::::from(self.object) @@ -123,7 +123,7 @@ impl ActivityHandler for CollectionRemove { community_id: community.id, person_id: remove_mod.id, }; - CommunityModerator::leave(&mut *context.conn().await?, &form).await?; + CommunityModerator::leave(context.conn().await?, &form).await?; // write mod log let actor = self.actor.dereference(context).await?; @@ -133,7 +133,7 @@ impl ActivityHandler for CollectionRemove { community_id: community.id, removed: Some(true), }; - ModAddCommunity::create(&mut *context.conn().await?, &form).await?; + ModAddCommunity::create(context.conn().await?, &form).await?; // TODO: send websocket notification about removed mod } @@ -144,7 +144,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(context.conn().await?, 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..3b460f9f3 100644 --- a/crates/apub/src/activities/community/lock_page.rs +++ b/crates/apub/src/activities/community/lock_page.rs @@ -60,7 +60,7 @@ impl ActivityHandler for LockPage { async fn receive(self, context: &Data) -> Result<(), Self::Error> { 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(context.conn().await?, post.id, &form).await?; Ok(()) } } @@ -97,7 +97,7 @@ impl ActivityHandler for UndoLockPage { 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(context.conn().await?, post.id, &form).await?; Ok(()) } } @@ -145,8 +145,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(context.conn().await?, 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..3555437a8 100644 --- a/crates/apub/src/activities/community/mod.rs +++ b/crates/apub/src/activities/community/mod.rs @@ -44,7 +44,7 @@ pub(crate) async fn send_activity_in_community( // send to user followers if !is_mod_action { inboxes.extend( - &mut PersonFollower::list_followers(&mut *context.conn().await?, actor.id) + &mut PersonFollower::list_followers(context.conn().await?, 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..f46d25a1f 100644 --- a/crates/apub/src/activities/community/report.rs +++ b/crates/apub/src/activities/community/report.rs @@ -134,7 +134,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(context.conn().await?, &report_form).await?; } PostOrComment::Comment(comment) => { let report_form = CommentReportForm { @@ -143,7 +143,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(context.conn().await?, &report_form).await?; } }; Ok(()) diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index 0f7ae297b..6eaa09e0b 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -36,7 +36,7 @@ impl SendActivity for EditCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } } @@ -117,7 +117,7 @@ impl SendActivity for HideCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, 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..cfbef4e30 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -91,12 +91,10 @@ impl CreateOrUpdateNote { ) -> Result<(), LemmyError> { // 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(context.conn().await?, 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) + let person: ApubPerson = Person::read(context.conn().await?, person_id).await?.into(); + let community: ApubCommunity = Community::read(context.conn().await?, community_id) .await? .into(); @@ -181,7 +179,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(context.conn().await?, creator.id, post.community_id).await?; } } @@ -194,14 +192,14 @@ impl ActivityHandler for CreateOrUpdateNote { person_id: comment.creator_id, score: 1, }; - CommentLike::like(&mut *context.conn().await?, &like_form).await?; + CommentLike::like(context.conn().await?, &like_form).await?; // Calculate initial hot_rank - CommentAggregates::update_hot_rank(&mut *context.conn().await?, comment.id).await?; + CommentAggregates::update_hot_rank(context.conn().await?, 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(context.conn().await?, 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..385b80e51 100644 --- a/crates/apub/src/activities/create_or_update/post.rs +++ b/crates/apub/src/activities/create_or_update/post.rs @@ -109,10 +109,8 @@ impl CreateOrUpdatePage { ) -> Result<(), LemmyError> { 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) + let person: ApubPerson = Person::read(context.conn().await?, person_id).await?.into(); + let community: ApubCommunity = Community::read(context.conn().await?, community_id) .await? .into(); @@ -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(context.conn().await?, &like_form).await?; // Calculate initial hot_rank for post - PostAggregates::update_hot_rank(&mut *context.conn().await?, post.id).await?; + PostAggregates::update_hot_rank(context.conn().await?, 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..22a2321b3 100644 --- a/crates/apub/src/activities/create_or_update/private_message.rs +++ b/crates/apub/src/activities/create_or_update/private_message.rs @@ -71,10 +71,8 @@ impl CreateOrUpdateChatMessage { context: &Data, ) -> Result<(), LemmyError> { 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) + let sender: ApubPerson = Person::read(context.conn().await?, sender_id).await?.into(); + let recipient: ApubPerson = Person::read(context.conn().await?, recipient_id) .await? .into(); diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index 1685fd87e..d2305aa77 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -119,7 +119,7 @@ pub(in crate::activities) async fn receive_remove_action( reason, expires: None, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(context.conn().await?, &form).await?; Community::update( &mut *context.conn().await?, community.id, @@ -134,7 +134,7 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(context.conn().await?, &form).await?; Post::update( &mut *context.conn().await?, post.id, @@ -149,7 +149,7 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(context.conn().await?, &form).await?; Comment::update( &mut *context.conn().await?, comment.id, diff --git a/crates/apub/src/activities/deletion/delete_user.rs b/crates/apub/src/activities/deletion/delete_user.rs index 625540291..1c2264f92 100644 --- a/crates/apub/src/activities/deletion/delete_user.rs +++ b/crates/apub/src/activities/deletion/delete_user.rs @@ -51,7 +51,7 @@ impl SendActivity for DeleteAccount { cc: vec![], }; - let inboxes = remote_instance_inboxes(&mut *context.conn().await?).await?; + let inboxes = remote_instance_inboxes(context.conn().await?).await?; send_lemmy_activity(context, delete, &actor, inboxes, true).await?; Ok(()) } diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 2792f3f4f..d1d65500c 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -64,8 +64,7 @@ impl SendActivity for DeletePost { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, 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, @@ -89,8 +88,7 @@ impl SendActivity for RemovePost { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, 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, @@ -114,7 +112,7 @@ impl SendActivity for DeleteComment { context: &Data, ) -> Result<(), LemmyError> { let community_id = response.comment_view.community.id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(context.conn().await?, community_id).await?; let person = Person::read( &mut *context.conn().await?, response.comment_view.creator.id, @@ -136,7 +134,7 @@ impl SendActivity for RemoveComment { context: &Data, ) -> Result<(), LemmyError> { 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 comment = Comment::read(context.conn().await?, request.comment_id).await?; let community = Community::read( &mut *context.conn().await?, response.comment_view.community.id, @@ -185,7 +183,7 @@ impl SendActivity for DeleteCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -209,7 +207,7 @@ impl SendActivity for RemoveCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -262,7 +260,7 @@ async fn send_apub_delete_private_message( context: &Data, ) -> Result<(), LemmyError> { let recipient_id = pm.recipient_id; - let recipient: ApubPerson = Person::read(&mut *context.conn().await?, recipient_id) + let recipient: ApubPerson = Person::read(context.conn().await?, recipient_id) .await? .into(); diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index 5be780be0..be13d9d52 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -111,7 +111,7 @@ impl UndoDelete { reason: None, expires: None, }; - ModRemoveCommunity::create(&mut *context.conn().await?, &form).await?; + ModRemoveCommunity::create(context.conn().await?, &form).await?; Community::update( &mut *context.conn().await?, community.id, @@ -126,7 +126,7 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemovePost::create(&mut *context.conn().await?, &form).await?; + ModRemovePost::create(context.conn().await?, &form).await?; Post::update( &mut *context.conn().await?, post.id, @@ -141,7 +141,7 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemoveComment::create(&mut *context.conn().await?, &form).await?; + ModRemoveComment::create(context.conn().await?, &form).await?; Comment::update( &mut *context.conn().await?, comment.id, diff --git a/crates/apub/src/activities/following/accept.rs b/crates/apub/src/activities/following/accept.rs index 432133217..7bf38cd7b 100644 --- a/crates/apub/src/activities/following/accept.rs +++ b/crates/apub/src/activities/following/accept.rs @@ -66,8 +66,7 @@ impl ActivityHandler for AcceptFollow { // 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(context.conn().await?, 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..2b0d987da 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -65,7 +65,7 @@ impl Follow { person_id: actor.id, pending: true, }; - CommunityFollower::follow(&mut *context.conn().await?, &community_follower_form) + CommunityFollower::follow(context.conn().await?, &community_follower_form) .await .ok(); @@ -113,7 +113,7 @@ impl ActivityHandler for Follow { follower_id: actor.id, pending: false, }; - PersonFollower::follow(&mut *context.conn().await?, &form).await?; + PersonFollower::follow(context.conn().await?, &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -121,7 +121,7 @@ impl ActivityHandler for Follow { person_id: actor.id, pending: false, }; - CommunityFollower::follow(&mut *context.conn().await?, &form).await?; + CommunityFollower::follow(context.conn().await?, &form).await?; } } @@ -139,7 +139,7 @@ impl SendActivity for BlockCommunity { context: &Data, ) -> Result<(), LemmyError> { 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(context.conn().await?, 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..67e000b64 100644 --- a/crates/apub/src/activities/following/mod.rs +++ b/crates/apub/src/activities/following/mod.rs @@ -27,10 +27,9 @@ impl SendActivity for FollowCommunity { ) -> Result<(), LemmyError> { 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(context.conn().await?, 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..a1d4392f4 100644 --- a/crates/apub/src/activities/following/undo_follow.rs +++ b/crates/apub/src/activities/following/undo_follow.rs @@ -82,7 +82,7 @@ impl ActivityHandler for UndoFollow { follower_id: person.id, pending: false, }; - PersonFollower::unfollow(&mut *context.conn().await?, &form).await?; + PersonFollower::unfollow(context.conn().await?, &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -90,7 +90,7 @@ impl ActivityHandler for UndoFollow { person_id: person.id, pending: false, }; - CommunityFollower::unfollow(&mut *context.conn().await?, &form).await?; + CommunityFollower::unfollow(context.conn().await?, &form).await?; } } diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index fecb01d6a..b38e3a1c6 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -59,7 +59,7 @@ pub(crate) async fn verify_person_in_community( } 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(context.conn().await?, person_id, community_id) .await .is_ok(); if is_banned { @@ -84,7 +84,7 @@ pub(crate) async fn verify_mod_action( 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?; + CommunityView::is_mod_or_admin(context.conn().await?, 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..ccf11bcab 100644 --- a/crates/apub/src/activities/voting/mod.rs +++ b/crates/apub/src/activities/voting/mod.rs @@ -83,11 +83,11 @@ async fn send_activity( jwt: &Sensitive, context: &Data, ) -> Result<(), LemmyError> { - let community = Community::read(&mut *context.conn().await?, community_id) + let community = Community::read(context.conn().await?, 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(context.conn().await?, local_user_view.person.id) .await? .into(); @@ -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(context.conn().await?, person_id, comment_id).await?; + CommentLike::like(context.conn().await?, &like_form).await?; Ok(()) } @@ -139,8 +139,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(context.conn().await?, person_id, post_id).await?; + PostLike::like(context.conn().await?, &like_form).await?; Ok(()) } @@ -152,7 +152,7 @@ async fn undo_vote_comment( ) -> Result<(), LemmyError> { let comment_id = comment.id; let person_id = actor.id; - CommentLike::remove(&mut *context.conn().await?, person_id, comment_id).await?; + CommentLike::remove(context.conn().await?, person_id, comment_id).await?; Ok(()) } @@ -164,6 +164,6 @@ async fn undo_vote_post( ) -> Result<(), LemmyError> { let post_id = post.id; let person_id = actor.id; - PostLike::remove(&mut *context.conn().await?, person_id, post_id).await?; + PostLike::remove(context.conn().await?, 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..8b6ed2a9d 100644 --- a/crates/apub/src/activities/voting/vote.rs +++ b/crates/apub/src/activities/voting/vote.rs @@ -58,7 +58,7 @@ impl ActivityHandler for Vote { async fn verify(&self, context: &Data) -> Result<(), LemmyError> { 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(context.conn().await?) .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..101dfea80 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -24,7 +24,7 @@ impl PerformApub for GetComments { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; check_private_instance(&local_user_view, &local_site)?; let community_id = if let Some(name) = &data.community_name { @@ -46,11 +46,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(context.conn().await?, parent_id).await?.path) } else { None }; @@ -60,7 +56,7 @@ impl PerformApub for GetComments { let local_user = local_user_view.map(|l| l.local_user); let mut conn = context.conn().await?; let comments = CommentQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .listing_type(Some(listing_type)) .sort(sort) .max_depth(max_depth) diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 85a4055cc..bd8889a5e 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -21,7 +21,7 @@ impl PerformApub for GetPosts { async fn perform(&self, context: &Data) -> Result { 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(context.conn().await?).await?; check_private_instance(&local_user_view, &local_site)?; @@ -51,7 +51,7 @@ impl PerformApub for GetPosts { let mut conn = context.conn().await?; let posts = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .local_user(local_user_view.map(|l| l.local_user).as_ref()) .listing_type(Some(listing_type)) .sort(sort) diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index a3c89b2e8..e34be994a 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -29,7 +29,7 @@ impl PerformApub for GetCommunity { ) -> Result { 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(context.conn().await?).await?; if data.name.is_none() && data.id.is_none() { return Err(LemmyError::from_message("no_id_given")); @@ -67,14 +67,13 @@ impl PerformApub for GetCommunity { .await .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; - 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(context.conn().await?, 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(context.conn().await?, &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 +83,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(context.conn().await?, 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..ac4097735 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -30,7 +30,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(context.conn().await?).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 +53,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(context.conn().await?, person_details_id).await?; let sort = data.sort; let page = data.page; @@ -65,7 +65,7 @@ impl PerformApub for GetPersonDetails { let mut conn = context.conn().await?; let posts_query = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .saved_only(saved_only) .local_user(local_user.as_ref()) @@ -88,7 +88,7 @@ impl PerformApub for GetPersonDetails { let mut conn = context.conn().await?; let comments_query = CommentQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .local_user(local_user_clone.as_ref()) .sort(sort.map(post_to_comment_sort_type)) .saved_only(saved_only) @@ -110,7 +110,7 @@ impl PerformApub for GetPersonDetails { .await?; let moderates = - CommunityModeratorView::for_person(&mut *context.conn().await?, person_details_id).await?; + CommunityModeratorView::for_person(context.conn().await?, 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..34d195ed7 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -24,7 +24,7 @@ impl PerformApub for ResolveObject { context: &Data, ) -> Result { 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(context.conn().await?).await?; let person_id = local_user_view.person.id; check_private_instance(&Some(local_user_view), &local_site)?; @@ -40,7 +40,7 @@ impl PerformApub for ResolveObject { async fn convert_response( object: SearchableObjects, user_id: PersonId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result { use SearchableObjects::*; let removed_or_deleted; @@ -48,19 +48,19 @@ async fn convert_response( match object { Person(p) => { removed_or_deleted = p.deleted; - res.person = Some(PersonView::read(conn, p.id).await?) + res.person = Some(PersonView::read(&mut *conn, p.id).await?) } Community(c) => { removed_or_deleted = c.deleted || c.removed; - res.community = Some(CommunityView::read(conn, c.id, Some(user_id), None).await?) + res.community = Some(CommunityView::read(&mut *conn, c.id, Some(user_id), None).await?) } Post(p) => { removed_or_deleted = p.deleted || p.removed; - res.post = Some(PostView::read(conn, p.id, Some(user_id), None).await?) + res.post = Some(PostView::read(&mut *conn, p.id, Some(user_id), None).await?) } Comment(c) => { removed_or_deleted = c.deleted || c.removed; - res.comment = Some(CommentView::read(conn, c.id, Some(user_id)).await?) + res.comment = Some(CommentView::read(&mut *conn, c.id, Some(user_id)).await?) } }; // if the object was deleted from database, dont return it diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index b7e59d413..5b58203a6 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -27,7 +27,7 @@ impl PerformApub for Search { 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(context.conn().await?).await?; check_private_instance(&local_user_view, &local_site)?; @@ -60,7 +60,7 @@ impl PerformApub for Search { match search_type { SearchType::Posts => { posts = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .listing_type(listing_type) .community_id(community_id) @@ -76,7 +76,7 @@ impl PerformApub for Search { } SearchType::Comments => { comments = CommentQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) @@ -91,7 +91,7 @@ impl PerformApub for Search { } SearchType::Communities => { communities = CommunityQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .listing_type(listing_type) .search_term(Some(q)) @@ -105,7 +105,7 @@ impl PerformApub for Search { } SearchType::Users => { users = PersonQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .search_term(Some(q)) .page(page) @@ -121,7 +121,7 @@ impl PerformApub for Search { let local_user_ = local_user.clone(); posts = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .listing_type(listing_type) .community_id(community_id) @@ -139,7 +139,7 @@ impl PerformApub for Search { let local_user_ = local_user.clone(); comments = CommentQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) @@ -158,7 +158,7 @@ impl PerformApub for Search { vec![] } else { CommunityQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .listing_type(listing_type) .search_term(Some(q)) @@ -177,7 +177,7 @@ impl PerformApub for Search { vec![] } else { PersonQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .search_term(Some(q)) .page(page) @@ -189,7 +189,7 @@ impl PerformApub for Search { } SearchType::Url => { posts = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .sort(sort) .listing_type(listing_type) .community_id(community_id) diff --git a/crates/apub/src/collections/community_moderators.rs b/crates/apub/src/collections/community_moderators.rs index d178ffaf0..bc3c6bd19 100644 --- a/crates/apub/src/collections/community_moderators.rs +++ b/crates/apub/src/collections/community_moderators.rs @@ -130,7 +130,7 @@ mod tests { let community_id = community.id; let inserted_instance = Instance::read_or_create( - &mut context.conn().await.unwrap(), + &mut *context.conn().await.unwrap(), "my_domain.tld".to_string(), ) .await @@ -142,7 +142,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let old_mod = Person::create(&mut context.conn().await.unwrap(), &old_mod) + let old_mod = Person::create(context.conn().await.unwrap(), &old_mod) .await .unwrap(); let community_moderator_form = CommunityModeratorForm { @@ -151,7 +151,7 @@ mod tests { }; CommunityModerator::join( - &mut context.conn().await.unwrap(), + &mut *context.conn().await.unwrap(), &community_moderator_form, ) .await @@ -171,26 +171,26 @@ mod tests { assert_eq!(context.request_count(), 0); let current_moderators = - CommunityModeratorView::for_community(&mut context.conn().await.unwrap(), community_id) + CommunityModeratorView::for_community(context.conn().await.unwrap(), community_id) .await .unwrap(); assert_eq!(current_moderators.len(), 1); assert_eq!(current_moderators[0].moderator.id, new_mod.id); - Person::delete(&mut context.conn().await.unwrap(), old_mod.id) + Person::delete(context.conn().await.unwrap(), old_mod.id) .await .unwrap(); - Person::delete(&mut context.conn().await.unwrap(), new_mod.id) + Person::delete(context.conn().await.unwrap(), new_mod.id) .await .unwrap(); - Community::delete(&mut context.conn().await.unwrap(), community.id) + Community::delete(context.conn().await.unwrap(), community.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), site.id) + Site::delete(context.conn().await.unwrap(), site.id) .await .unwrap(); - Instance::delete(&mut context.conn().await.unwrap(), inserted_instance.id) + Instance::delete(context.conn().await.unwrap(), inserted_instance.id) .await .unwrap(); } diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index f6e9819cc..a20e52cd0 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -41,7 +41,7 @@ where .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(context.conn().await?, 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(context.conn().await?, &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..613270472 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -92,15 +92,11 @@ impl InCommunity for PostOrComment { let cid = match self { PostOrComment::Post(p) => p.community_id, PostOrComment::Comment(c) => { - Post::read(&mut *context.conn().await?, c.post_id) + Post::read(context.conn().await?, c.post_id) .await? .community_id } }; - Ok( - Community::read(&mut *context.conn().await?, cid) - .await? - .into(), - ) + Ok(Community::read(context.conn().await?, cid).await?.into()) } } diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index a1101b12b..bb8be331a 100644 --- a/crates/apub/src/http/comment.rs +++ b/crates/apub/src/http/comment.rs @@ -21,7 +21,7 @@ pub(crate) async fn get_apub_comment( context: Data, ) -> Result { let id = CommentId(info.comment_id.parse::()?); - let comment: ApubComment = Comment::read(&mut *context.conn().await?, id).await?.into(); + let comment: ApubComment = Comment::read(context.conn().await?, 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..38a9080a7 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -33,7 +33,7 @@ pub(crate) async fn get_apub_community_http( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, true) + Community::read_from_name(context.conn().await?, &info.community_name, true) .await? .into(); @@ -65,7 +65,7 @@ pub(crate) async fn get_apub_community_followers( context: Data, ) -> Result { let community = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false).await?; + Community::read_from_name(context.conn().await?, &info.community_name, false).await?; let followers = GroupFollowers::new(community, &context).await?; create_apub_response(&followers) } @@ -77,7 +77,7 @@ pub(crate) async fn get_apub_community_outbox( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) + Community::read_from_name(context.conn().await?, &info.community_name, false) .await? .into(); if community.deleted || community.removed { @@ -93,7 +93,7 @@ pub(crate) async fn get_apub_community_moderators( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) + Community::read_from_name(context.conn().await?, &info.community_name, false) .await? .into(); if community.deleted || community.removed { @@ -109,7 +109,7 @@ pub(crate) async fn get_apub_community_featured( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(&mut *context.conn().await?, &info.community_name, false) + Community::read_from_name(context.conn().await?, &info.community_name, false) .await? .into(); if community.deleted || community.removed { diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 9000d38f2..54aee8c87 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -90,7 +90,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(context.conn().await?, &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..d581ceb61 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -30,7 +30,7 @@ pub(crate) async fn get_apub_person_http( ) -> Result { 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(context.conn().await?, &user_name, true) .await? .into(); @@ -60,7 +60,7 @@ 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 person = Person::read_from_name(context.conn().await?, &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..4398ac7f6 100644 --- a/crates/apub/src/http/post.rs +++ b/crates/apub/src/http/post.rs @@ -21,7 +21,7 @@ pub(crate) async fn get_apub_post( context: Data, ) -> Result { let id = PostId(info.post_id.parse::()?); - let post: ApubPost = Post::read(&mut *context.conn().await?, id).await?.into(); + let post: ApubPost = Post::read(context.conn().await?, 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..bf680bff8 100644 --- a/crates/apub/src/http/site.rs +++ b/crates/apub/src/http/site.rs @@ -19,7 +19,7 @@ use url::Url; pub(crate) async fn get_apub_site_http( context: Data, ) -> Result { - let site: ApubSite = SiteView::read_local(&mut *context.conn().await?) + let site: ApubSite = SiteView::read_local(context.conn().await?) .await? .site .into(); diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 296d368e8..319d1d45b 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -39,7 +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) + let local_site_data = fetch_local_site_data(&mut *conn) .await .expect("read local site data"); check_apub_id_valid(url, &local_site_data)?; @@ -99,12 +99,12 @@ pub(crate) struct LocalSiteData { } pub(crate) async fn fetch_local_site_data( - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result { // LocalSite may be missing - let local_site = LocalSite::read(conn).await.ok(); - let allowed_instances = Instance::allowlist(conn).await?; - let blocked_instances = Instance::blocklist(conn).await?; + let local_site = LocalSite::read(&mut *conn).await.ok(); + let allowed_instances = Instance::allowlist(&mut *conn).await?; + let blocked_instances = Instance::blocklist(&mut *conn).await?; Ok(LocalSiteData { local_site, diff --git a/crates/apub/src/mentions.rs b/crates/apub/src/mentions.rs index d64e24a00..dd3fa5bd3 100644 --- a/crates/apub/src/mentions.rs +++ b/crates/apub/src/mentions.rs @@ -45,7 +45,7 @@ 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 parent_creator = get_comment_parent_creator(context.conn().await?, comment).await?; let mut addressed_ccs: Vec = vec![community_id.into(), parent_creator.id()]; // Add the mention tag @@ -92,16 +92,16 @@ pub async fn collect_non_local_mentions( /// top-level comment, the creator of the post, otherwise the creator of the parent comment. #[tracing::instrument(skip(conn, comment))] async fn get_comment_parent_creator( - conn: &mut DbConn, + mut conn: impl DbConn, comment: &Comment, ) -> Result { let parent_creator_id = if let Some(parent_comment_id) = comment.parent_comment_id() { - let parent_comment = Comment::read(conn, parent_comment_id).await?; + let parent_comment = Comment::read(&mut *conn, parent_comment_id).await?; parent_comment.creator_id } else { let parent_post_id = comment.post_id; - let parent_post = Post::read(conn, parent_post_id).await?; + let parent_post = Post::read(&mut *conn, parent_post_id).await?; parent_post.creator_id }; - Ok(Person::read(conn, parent_creator_id).await?.into()) + Ok(Person::read(&mut *conn, parent_creator_id).await?.into()) } diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 5c46a5892..2ac125479 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -67,7 +67,7 @@ impl Object for ApubComment { context: &Data, ) -> Result, LemmyError> { Ok( - Comment::read_from_apub_id(&mut *context.conn().await?, object_id) + Comment::read_from_apub_id(context.conn().await?, object_id) .await? .map(Into::into), ) @@ -77,7 +77,7 @@ impl Object for ApubComment { async fn delete(self, context: &Data) -> Result<(), LemmyError> { if !self.deleted { let form = CommentUpdateForm::builder().deleted(Some(true)).build(); - Comment::update(&mut *context.conn().await?, self.id, &form).await?; + Comment::update(context.conn().await?, self.id, &form).await?; } Ok(()) } @@ -85,15 +85,15 @@ impl Object for ApubComment { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(context.conn().await?, creator_id).await?; let post_id = self.post_id; - let post = Post::read(&mut *context.conn().await?, post_id).await?; + let post = Post::read(context.conn().await?, post_id).await?; let community_id = post.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(context.conn().await?, 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(context.conn().await?, comment_id).await?; parent_comment.ap_id.into() } else { post.ap_id.into() @@ -132,7 +132,7 @@ impl Object for ApubComment { 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(context.conn().await?).await?; check_apub_id_valid_with_strictness( note.id.inner(), @@ -159,7 +159,7 @@ impl Object for ApubComment { 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(context.conn().await?).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 = @@ -222,19 +222,19 @@ pub(crate) mod tests { } async fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) { - Post::delete(&mut context.conn().await.unwrap(), data.2.id) + Post::delete(context.conn().await.unwrap(), data.2.id) .await .unwrap(); - Community::delete(&mut context.conn().await.unwrap(), data.1.id) + Community::delete(context.conn().await.unwrap(), data.1.id) .await .unwrap(); - Person::delete(&mut context.conn().await.unwrap(), data.0.id) + Person::delete(context.conn().await.unwrap(), data.0.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), data.3.id) + Site::delete(context.conn().await.unwrap(), data.3.id) .await .unwrap(); - LocalSite::delete(&mut context.conn().await.unwrap()) + LocalSite::delete(context.conn().await.unwrap()) .await .unwrap(); } @@ -261,7 +261,7 @@ pub(crate) mod tests { let to_apub = comment.into_json(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - Comment::delete(&mut context.conn().await.unwrap(), comment_id) + Comment::delete(context.conn().await.unwrap(), comment_id) .await .unwrap(); cleanup(data, &context).await; @@ -293,7 +293,7 @@ pub(crate) mod tests { assert!(!comment.local); assert_eq!(context.request_count(), 1); - Comment::delete(&mut context.conn().await.unwrap(), comment.id) + Comment::delete(context.conn().await.unwrap(), comment.id) .await .unwrap(); cleanup(data, &context).await; diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 15d254ffc..069b94177 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -67,7 +67,7 @@ impl Object for ApubCommunity { context: &Data, ) -> Result, LemmyError> { Ok( - Community::read_from_apub_id(&mut *context.conn().await?, &object_id.into()) + Community::read_from_apub_id(context.conn().await?, &object_id.into()) .await? .map(Into::into), ) @@ -76,7 +76,7 @@ impl Object for ApubCommunity { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { let form = CommunityUpdateForm::builder().deleted(Some(true)).build(); - Community::update(&mut *context.conn().await?, self.id, &form).await?; + Community::update(context.conn().await?, self.id, &form).await?; Ok(()) } @@ -134,8 +134,8 @@ impl Object for ApubCommunity { let languages = LanguageTag::to_language_id_multiple(group.language, &mut *context.conn().await?).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(context.conn().await?, &form).await?; + CommunityLanguage::update(context.conn().await?, languages, community.id).await?; let community: ApubCommunity = community.into(); @@ -188,8 +188,8 @@ impl ApubCommunity { ) -> Result, LemmyError> { 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(context.conn().await?).await?; + let follows = CommunityFollowerView::for_community(context.conn().await?, id).await?; let inboxes: Vec = follows .into_iter() .filter(|f| !f.follower.local) @@ -251,10 +251,10 @@ pub(crate) mod tests { assert!(!community.local); assert_eq!(community.description.as_ref().unwrap().len(), 132); - Community::delete(&mut context.conn().await.unwrap(), community.id) + Community::delete(context.conn().await.unwrap(), community.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), site.id) + Site::delete(context.conn().await.unwrap(), site.id) .await .unwrap(); } diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index 66332254a..9afbe85b7 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -188,7 +188,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(context.conn().await?, domain.to_string()) .await? .id, ) @@ -196,9 +196,9 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object + C } } -pub(crate) async fn remote_instance_inboxes(conn: &mut DbConn) -> Result, LemmyError> { +pub(crate) async fn remote_instance_inboxes(mut conn: impl DbConn) -> Result, LemmyError> { Ok( - Site::read_remote_sites(conn) + Site::read_remote_sites(&mut *conn) .await? .into_iter() .map(|s| ApubSite::from(s).shared_inbox_or_inbox()) @@ -231,7 +231,7 @@ pub(crate) mod tests { assert_eq!(site.name, "Enterprise"); assert_eq!(site.description.as_ref().unwrap().len(), 15); - Site::delete(&mut context.conn().await.unwrap(), site.id) + Site::delete(context.conn().await.unwrap(), site.id) .await .unwrap(); } diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index e5cd9e8e6..0462020e3 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -69,7 +69,7 @@ impl Object for ApubPerson { context: &Data, ) -> Result, LemmyError> { Ok( - DbPerson::read_from_apub_id(&mut *context.conn().await?, &object_id.into()) + DbPerson::read_from_apub_id(context.conn().await?, &object_id.into()) .await? .map(Into::into), ) @@ -78,7 +78,7 @@ impl Object for ApubPerson { #[tracing::instrument(skip_all)] async fn delete(self, context: &Data) -> Result<(), LemmyError> { let form = PersonUpdateForm::builder().deleted(Some(true)).build(); - DbPerson::update(&mut *context.conn().await?, self.id, &form).await?; + DbPerson::update(context.conn().await?, self.id, &form).await?; Ok(()) } @@ -118,7 +118,7 @@ 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 local_site_data = fetch_local_site_data(context.conn().await?).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs(&person.preferred_username, slur_regex)?; @@ -171,7 +171,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(context.conn().await?, &person_form).await?; Ok(person.into()) } @@ -262,10 +262,10 @@ pub(crate) mod tests { } async fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) { - DbPerson::delete(&mut context.conn().await.unwrap(), data.0.id) + DbPerson::delete(context.conn().await.unwrap(), data.0.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), data.1.id) + Site::delete(context.conn().await.unwrap(), data.1.id) .await .unwrap(); } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index d33fcc7f8..03c5d2878 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -83,7 +83,7 @@ impl Object for ApubPost { context: &Data, ) -> Result, LemmyError> { Ok( - Post::read_from_apub_id(&mut *context.conn().await?, object_id) + Post::read_from_apub_id(context.conn().await?, object_id) .await? .map(Into::into), ) @@ -93,7 +93,7 @@ impl Object for ApubPost { async fn delete(self, context: &Data) -> Result<(), LemmyError> { if !self.deleted { let form = PostUpdateForm::builder().deleted(Some(true)).build(); - Post::update(&mut *context.conn().await?, self.id, &form).await?; + Post::update(context.conn().await?, self.id, &form).await?; } Ok(()) } @@ -102,9 +102,9 @@ impl Object for ApubPost { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(context.conn().await?, creator_id).await?; let community_id = self.community_id; - let community = Community::read(&mut *context.conn().await?, community_id).await?; + let community = Community::read(context.conn().await?, community_id).await?; let language = LanguageTag::new_single(self.language_id, &mut *context.conn().await?).await?; let page = Page { @@ -143,7 +143,7 @@ impl Object for ApubPost { 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(context.conn().await?).await?; let community = page.community(context).await?; check_apub_id_valid_with_strictness( @@ -167,7 +167,7 @@ impl Object for ApubPost { 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(context.conn().await?, creator.id, community.id).await?; } let mut name = page .name @@ -198,7 +198,7 @@ impl Object for ApubPost { None }; - let local_site = LocalSite::read(&mut *context.conn().await?).await.ok(); + let local_site = LocalSite::read(context.conn().await?).await.ok(); let allow_sensitive = local_site_opt_to_sensitive(&local_site); let page_is_sensitive = page.sensitive.unwrap_or(false); let include_image = allow_sensitive || !page_is_sensitive; @@ -266,7 +266,7 @@ impl Object for ApubPost { .build() }; - let post = Post::create(&mut *context.conn().await?, &form).await?; + let post = Post::create(context.conn().await?, &form).await?; // write mod log entry for lock if Page::is_locked_changed(&old_post, &page.comments_enabled) { @@ -275,7 +275,7 @@ impl Object for ApubPost { post_id: post.id, locked: Some(post.locked), }; - ModLockPost::create(&mut *context.conn().await?, &form).await?; + ModLockPost::create(context.conn().await?, &form).await?; } Ok(post.into()) @@ -317,16 +317,16 @@ mod tests { assert!(!post.featured_community); assert_eq!(context.request_count(), 0); - Post::delete(&mut context.conn().await.unwrap(), post.id) + Post::delete(context.conn().await.unwrap(), post.id) .await .unwrap(); - Person::delete(&mut context.conn().await.unwrap(), person.id) + Person::delete(context.conn().await.unwrap(), person.id) .await .unwrap(); - Community::delete(&mut context.conn().await.unwrap(), community.id) + Community::delete(context.conn().await.unwrap(), community.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), site.id) + Site::delete(context.conn().await.unwrap(), site.id) .await .unwrap(); } diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index edacf5f4f..db333f270 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -60,7 +60,7 @@ impl Object for ApubPrivateMessage { context: &Data, ) -> Result, LemmyError> { Ok( - PrivateMessage::read_from_apub_id(&mut *context.conn().await?, object_id) + PrivateMessage::read_from_apub_id(context.conn().await?, object_id) .await? .map(Into::into), ) @@ -74,10 +74,10 @@ impl Object for ApubPrivateMessage { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> Result { let creator_id = self.creator_id; - let creator = Person::read(&mut *context.conn().await?, creator_id).await?; + let creator = Person::read(context.conn().await?, creator_id).await?; let recipient_id = self.recipient_id; - let recipient = Person::read(&mut *context.conn().await?, recipient_id).await?; + let recipient = Person::read(context.conn().await?, recipient_id).await?; let note = ChatMessage { r#type: ChatMessageType::ChatMessage, @@ -102,7 +102,7 @@ impl Object for ApubPrivateMessage { 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(context.conn().await?).await?; check_apub_id_valid_with_strictness( note.id.inner(), @@ -137,7 +137,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(context.conn().await?, &form).await?; Ok(pm.into()) } } @@ -182,13 +182,13 @@ mod tests { } async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &Data) { - Person::delete(&mut context.conn().await.unwrap(), data.0.id) + Person::delete(context.conn().await.unwrap(), data.0.id) .await .unwrap(); - Person::delete(&mut context.conn().await.unwrap(), data.1.id) + Person::delete(context.conn().await.unwrap(), data.1.id) .await .unwrap(); - Site::delete(&mut context.conn().await.unwrap(), data.2.id) + Site::delete(context.conn().await.unwrap(), data.2.id) .await .unwrap(); } @@ -215,7 +215,7 @@ mod tests { let to_apub = pm.into_json(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - PrivateMessage::delete(&mut context.conn().await.unwrap(), pm_id) + PrivateMessage::delete(context.conn().await.unwrap(), pm_id) .await .unwrap(); cleanup(data, &context).await; @@ -238,7 +238,7 @@ mod tests { assert_eq!(pm.content.len(), 3); assert_eq!(context.request_count(), 0); - PrivateMessage::delete(&mut context.conn().await.unwrap(), pm.id) + PrivateMessage::delete(context.conn().await.unwrap(), pm.id) .await .unwrap(); cleanup(data, &context).await; diff --git a/crates/apub/src/protocol/activities/community/collection_add.rs b/crates/apub/src/protocol/activities/community/collection_add.rs index c2d4c6a2e..b32dc577d 100644 --- a/crates/apub/src/protocol/activities/community/collection_add.rs +++ b/crates/apub/src/protocol/activities/community/collection_add.rs @@ -35,8 +35,7 @@ pub struct CollectionAdd { impl InCommunity for CollectionAdd { async fn community(&self, context: &Data) -> Result { let (community, _) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.clone().target.into()) - .await?; + Community::get_by_collection_url(context.conn().await?, &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..3561db3f2 100644 --- a/crates/apub/src/protocol/activities/community/collection_remove.rs +++ b/crates/apub/src/protocol/activities/community/collection_remove.rs @@ -35,8 +35,7 @@ pub struct CollectionRemove { impl InCommunity for CollectionRemove { async fn community(&self, context: &Data) -> Result { let (community, _) = - Community::get_by_collection_url(&mut *context.conn().await?, &self.clone().target.into()) - .await?; + Community::get_by_collection_url(context.conn().await?, &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..12882646c 100644 --- a/crates/apub/src/protocol/activities/community/lock_page.rs +++ b/crates/apub/src/protocol/activities/community/lock_page.rs @@ -55,7 +55,7 @@ pub struct UndoLockPage { impl InCommunity for LockPage { async fn community(&self, context: &Data) -> Result { let post = self.object.dereference(context).await?; - let community = Community::read(&mut *context.conn().await?, post.community_id).await?; + let community = Community::read(context.conn().await?, 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..82fee38af 100644 --- a/crates/apub/src/protocol/activities/create_or_update/note.rs +++ b/crates/apub/src/protocol/activities/create_or_update/note.rs @@ -36,7 +36,7 @@ pub struct CreateOrUpdateNote { impl InCommunity for CreateOrUpdateNote { async fn community(&self, context: &Data) -> Result { 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(context.conn().await?, 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..3ffb158d5 100644 --- a/crates/apub/src/protocol/activities/deletion/delete.rs +++ b/crates/apub/src/protocol/activities/deletion/delete.rs @@ -48,7 +48,7 @@ impl InCommunity for Delete { 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(context.conn().await?, c.post_id).await?; post.community_id } DeletableObjects::Post(p) => p.community_id, @@ -56,7 +56,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(context.conn().await?, 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..f0f69ad11 100644 --- a/crates/apub/src/protocol/collections/group_followers.rs +++ b/crates/apub/src/protocol/collections/group_followers.rs @@ -22,7 +22,7 @@ impl GroupFollowers { ) -> Result { let community_id = community.id; let community_followers = - CommunityFollowerView::for_community(&mut *context.conn().await?, community_id).await?; + CommunityFollowerView::for_community(context.conn().await?, 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..b3bd5d08c 100644 --- a/crates/apub/src/protocol/objects/group.rs +++ b/crates/apub/src/protocol/objects/group.rs @@ -80,7 +80,7 @@ impl Group { expected_domain: &Url, context: &LemmyContext, ) -> Result<(), LemmyError> { - let local_site_data = fetch_local_site_data(&mut *context.conn().await?).await?; + let local_site_data = fetch_local_site_data(context.conn().await?).await?; check_apub_id_valid_with_strictness( self.id.inner(), diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index cc8807cb1..c304a2b51 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -33,9 +33,9 @@ pub(crate) struct LanguageTag { impl LanguageTag { pub(crate) async fn new_single( lang: LanguageId, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result, LemmyError> { - let lang = Language::read_from_id(conn, lang).await?; + let lang = Language::read_from_id(&mut *conn, lang).await?; // undetermined if lang.id == UNDETERMINED_ID { @@ -50,12 +50,12 @@ impl LanguageTag { pub(crate) async fn new_multiple( lang_ids: Vec, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result, LemmyError> { let mut langs = Vec::::new(); for l in lang_ids { - langs.push(Language::read_from_id(conn, l).await?); + langs.push(Language::read_from_id(&mut *conn, l).await?); } let langs = langs @@ -70,23 +70,23 @@ impl LanguageTag { pub(crate) async fn to_language_id_single( lang: Option, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result, LemmyError> { let identifier = lang.map(|l| l.identifier); - let language = Language::read_id_from_code(conn, identifier.as_deref()).await?; + let language = Language::read_id_from_code(&mut *conn, identifier.as_deref()).await?; Ok(language) } pub(crate) async fn to_language_id_multiple( langs: Vec, - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result, LemmyError> { let mut language_ids = Vec::new(); for l in langs { let id = l.identifier; - language_ids.push(Language::read_id_from_code(conn, Some(&id)).await?); + language_ids.push(Language::read_id_from_code(&mut *conn, Some(&id)).await?); } Ok(language_ids.into_iter().flatten().collect()) diff --git a/crates/apub/src/protocol/objects/note.rs b/crates/apub/src/protocol/objects/note.rs index fb68a047e..1526cf699 100644 --- a/crates/apub/src/protocol/objects/note.rs +++ b/crates/apub/src/protocol/objects/note.rs @@ -64,7 +64,7 @@ impl Note { 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(context.conn().await?, post_id).await?; Ok((post.into(), Some(c.clone()))) } } @@ -75,7 +75,7 @@ impl Note { impl InCommunity for Note { async fn community(&self, context: &Data) -> Result { let (post, _) = self.get_parents(context).await?; - let community = Community::read(&mut *context.conn().await?, post.community_id).await?; + let community = Community::read(context.conn().await?, post.community_id).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/db_schema/src/aggregates/comment_aggregates.rs b/crates/db_schema/src/aggregates/comment_aggregates.rs index 829af1a0f..8c913ff40 100644 --- a/crates/db_schema/src/aggregates/comment_aggregates.rs +++ b/crates/db_schema/src/aggregates/comment_aggregates.rs @@ -8,21 +8,24 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CommentAggregates { - pub async fn read(conn: &mut DbConn, comment_id: CommentId) -> Result { + pub async fn read(mut conn: impl DbConn, comment_id: CommentId) -> Result { comment_aggregates::table .filter(comment_aggregates::comment_id.eq(comment_id)) - .first::(conn) + .first::(&mut *conn) .await } - pub async fn update_hot_rank(conn: &mut DbConn, comment_id: CommentId) -> Result { + pub async fn update_hot_rank( + mut conn: impl DbConn, + comment_id: CommentId, + ) -> Result { diesel::update(comment_aggregates::table) .filter(comment_aggregates::comment_id.eq(comment_id)) .set(comment_aggregates::hot_rank.eq(hot_rank( comment_aggregates::score, comment_aggregates::published, ))) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -46,9 +49,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -58,7 +61,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let another_person = PersonInsertForm::builder() .name("jerry_comment_agg".into()) @@ -66,7 +69,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let another_inserted_person = Person::create(conn, &another_person).await.unwrap(); + let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL_comment_agg".into()) @@ -75,7 +78,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -83,7 +86,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -91,7 +94,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let child_comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -99,10 +104,13 @@ mod tests { .post_id(inserted_post.id) .build(); - let _inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) - .await - .unwrap(); + let _inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); let comment_like = CommentLikeForm { comment_id: inserted_comment.id, @@ -111,9 +119,9 @@ mod tests { score: 1, }; - CommentLike::like(conn, &comment_like).await.unwrap(); + CommentLike::like(&mut *conn, &comment_like).await.unwrap(); - let comment_aggs_before_delete = CommentAggregates::read(conn, inserted_comment.id) + let comment_aggs_before_delete = CommentAggregates::read(&mut *conn, inserted_comment.id) .await .unwrap(); @@ -129,9 +137,11 @@ mod tests { score: -1, }; - CommentLike::like(conn, &comment_dislike).await.unwrap(); + CommentLike::like(&mut *conn, &comment_dislike) + .await + .unwrap(); - let comment_aggs_after_dislike = CommentAggregates::read(conn, inserted_comment.id) + let comment_aggs_after_dislike = CommentAggregates::read(&mut *conn, inserted_comment.id) .await .unwrap(); @@ -140,10 +150,10 @@ mod tests { assert_eq!(1, comment_aggs_after_dislike.downvotes); // Remove the first comment like - CommentLike::remove(conn, inserted_person.id, inserted_comment.id) + CommentLike::remove(&mut *conn, inserted_person.id, inserted_comment.id) .await .unwrap(); - let after_like_remove = CommentAggregates::read(conn, inserted_comment.id) + let after_like_remove = CommentAggregates::read(&mut *conn, inserted_comment.id) .await .unwrap(); assert_eq!(-1, after_like_remove.score); @@ -151,25 +161,29 @@ mod tests { assert_eq!(1, after_like_remove.downvotes); // Remove the parent post - Post::delete(conn, inserted_post.id).await.unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); // Should be none found, since the post was deleted - let after_delete = CommentAggregates::read(conn, inserted_comment.id).await; + let after_delete = CommentAggregates::read(&mut *conn, inserted_comment.id).await; assert!(after_delete.is_err()); // This should delete all the associated rows, and fire triggers - Person::delete(conn, another_inserted_person.id) + Person::delete(&mut *conn, another_inserted_person.id) + .await + .unwrap(); + let person_num_deleted = Person::delete(&mut *conn, inserted_person.id) .await .unwrap(); - let person_num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); assert_eq!(1, person_num_deleted); // Delete the community - let community_num_deleted = Community::delete(conn, inserted_community.id) + let community_num_deleted = Community::delete(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, community_num_deleted); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); } } diff --git a/crates/db_schema/src/aggregates/community_aggregates.rs b/crates/db_schema/src/aggregates/community_aggregates.rs index 32215692a..61039b8ce 100644 --- a/crates/db_schema/src/aggregates/community_aggregates.rs +++ b/crates/db_schema/src/aggregates/community_aggregates.rs @@ -8,10 +8,10 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CommunityAggregates { - pub async fn read(conn: &mut DbConn, community_id: CommunityId) -> Result { + pub async fn read(mut conn: impl DbConn, community_id: CommunityId) -> Result { community_aggregates::table .filter(community_aggregates::community_id.eq(community_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -35,9 +35,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -47,7 +47,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let another_person = PersonInsertForm::builder() .name("jerry_community_agg".into()) @@ -55,7 +55,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let another_inserted_person = Person::create(conn, &another_person).await.unwrap(); + let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL_community_agg".into()) @@ -64,7 +64,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let another_community = CommunityInsertForm::builder() .name("TIL_community_agg_2".into()) @@ -73,7 +73,9 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let another_inserted_community = Community::create(conn, &another_community).await.unwrap(); + let another_inserted_community = Community::create(&mut *conn, &another_community) + .await + .unwrap(); let first_person_follow = CommunityFollowerForm { community_id: inserted_community.id, @@ -81,7 +83,7 @@ mod tests { pending: false, }; - CommunityFollower::follow(conn, &first_person_follow) + CommunityFollower::follow(&mut *conn, &first_person_follow) .await .unwrap(); @@ -91,7 +93,7 @@ mod tests { pending: false, }; - CommunityFollower::follow(conn, &second_person_follow) + CommunityFollower::follow(&mut *conn, &second_person_follow) .await .unwrap(); @@ -101,7 +103,7 @@ mod tests { pending: false, }; - CommunityFollower::follow(conn, &another_community_follow) + CommunityFollower::follow(&mut *conn, &another_community_follow) .await .unwrap(); @@ -111,7 +113,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -119,7 +121,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let child_comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -127,79 +131,87 @@ mod tests { .post_id(inserted_post.id) .build(); - let _inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) + let _inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); + + let community_aggregates_before_delete = + CommunityAggregates::read(&mut *conn, inserted_community.id) .await .unwrap(); - let community_aggregates_before_delete = CommunityAggregates::read(conn, inserted_community.id) - .await - .unwrap(); - assert_eq!(2, community_aggregates_before_delete.subscribers); assert_eq!(1, community_aggregates_before_delete.posts); assert_eq!(2, community_aggregates_before_delete.comments); // Test the other community - let another_community_aggs = CommunityAggregates::read(conn, another_inserted_community.id) - .await - .unwrap(); + let another_community_aggs = + CommunityAggregates::read(&mut *conn, another_inserted_community.id) + .await + .unwrap(); assert_eq!(1, another_community_aggs.subscribers); assert_eq!(0, another_community_aggs.posts); assert_eq!(0, another_community_aggs.comments); // Unfollow test - CommunityFollower::unfollow(conn, &second_person_follow) + CommunityFollower::unfollow(&mut *conn, &second_person_follow) .await .unwrap(); - let after_unfollow = CommunityAggregates::read(conn, inserted_community.id) + let after_unfollow = CommunityAggregates::read(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, after_unfollow.subscribers); // Follow again just for the later tests - CommunityFollower::follow(conn, &second_person_follow) + CommunityFollower::follow(&mut *conn, &second_person_follow) .await .unwrap(); - let after_follow_again = CommunityAggregates::read(conn, inserted_community.id) + let after_follow_again = CommunityAggregates::read(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(2, after_follow_again.subscribers); // Remove a parent comment (the comment count should also be 0) - Post::delete(conn, inserted_post.id).await.unwrap(); - let after_parent_post_delete = CommunityAggregates::read(conn, inserted_community.id) + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + let after_parent_post_delete = CommunityAggregates::read(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(0, after_parent_post_delete.comments); assert_eq!(0, after_parent_post_delete.posts); // Remove the 2nd person - Person::delete(conn, another_inserted_person.id) + Person::delete(&mut *conn, another_inserted_person.id) .await .unwrap(); - let after_person_delete = CommunityAggregates::read(conn, inserted_community.id) + let after_person_delete = CommunityAggregates::read(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, after_person_delete.subscribers); // This should delete all the associated rows, and fire triggers - let person_num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); + let person_num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); assert_eq!(1, person_num_deleted); // Delete the community - let community_num_deleted = Community::delete(conn, inserted_community.id) + let community_num_deleted = Community::delete(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, community_num_deleted); - let another_community_num_deleted = Community::delete(conn, another_inserted_community.id) - .await - .unwrap(); + let another_community_num_deleted = + Community::delete(&mut *conn, another_inserted_community.id) + .await + .unwrap(); assert_eq!(1, another_community_num_deleted); // Should be none found, since the creator was deleted - let after_delete = CommunityAggregates::read(conn, inserted_community.id).await; + let after_delete = CommunityAggregates::read(&mut *conn, inserted_community.id).await; assert!(after_delete.is_err()); } } diff --git a/crates/db_schema/src/aggregates/person_aggregates.rs b/crates/db_schema/src/aggregates/person_aggregates.rs index ac6ed09e9..f30006ed1 100644 --- a/crates/db_schema/src/aggregates/person_aggregates.rs +++ b/crates/db_schema/src/aggregates/person_aggregates.rs @@ -8,10 +8,10 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PersonAggregates { - pub async fn read(conn: &mut DbConn, person_id: PersonId) -> Result { + pub async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result { person_aggregates::table .filter(person_aggregates::person_id.eq(person_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -35,9 +35,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -47,7 +47,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let another_person = PersonInsertForm::builder() .name("jerry_user_agg".into()) @@ -55,7 +55,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let another_inserted_person = Person::create(conn, &another_person).await.unwrap(); + let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL_site_agg".into()) @@ -64,7 +64,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -72,7 +72,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let post_like = PostLikeForm { post_id: inserted_post.id, @@ -80,7 +80,7 @@ mod tests { score: 1, }; - let _inserted_post_like = PostLike::like(conn, &post_like).await.unwrap(); + let _inserted_post_like = PostLike::like(&mut *conn, &post_like).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -88,7 +88,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let mut comment_like = CommentLikeForm { comment_id: inserted_comment.id, @@ -97,7 +99,7 @@ mod tests { score: 1, }; - let _inserted_comment_like = CommentLike::like(conn, &comment_like).await.unwrap(); + let _inserted_comment_like = CommentLike::like(&mut *conn, &comment_like).await.unwrap(); let child_comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -105,10 +107,13 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) - .await - .unwrap(); + let inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); let child_comment_like = CommentLikeForm { comment_id: inserted_child_comment.id, @@ -117,9 +122,11 @@ mod tests { score: 1, }; - let _inserted_child_comment_like = CommentLike::like(conn, &child_comment_like).await.unwrap(); + let _inserted_child_comment_like = CommentLike::like(&mut *conn, &child_comment_like) + .await + .unwrap(); - let person_aggregates_before_delete = PersonAggregates::read(conn, inserted_person.id) + let person_aggregates_before_delete = PersonAggregates::read(&mut *conn, inserted_person.id) .await .unwrap(); @@ -129,41 +136,48 @@ mod tests { assert_eq!(2, person_aggregates_before_delete.comment_score); // Remove a post like - PostLike::remove(conn, inserted_person.id, inserted_post.id) + PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id) .await .unwrap(); - let after_post_like_remove = PersonAggregates::read(conn, inserted_person.id) + let after_post_like_remove = PersonAggregates::read(&mut *conn, inserted_person.id) .await .unwrap(); assert_eq!(0, after_post_like_remove.post_score); // Remove a parent comment (the scores should also be removed) - Comment::delete(conn, inserted_comment.id).await.unwrap(); - Comment::delete(conn, inserted_child_comment.id) + Comment::delete(&mut *conn, inserted_comment.id) .await .unwrap(); - let after_parent_comment_delete = PersonAggregates::read(conn, inserted_person.id) + Comment::delete(&mut *conn, inserted_child_comment.id) + .await + .unwrap(); + let after_parent_comment_delete = PersonAggregates::read(&mut *conn, inserted_person.id) .await .unwrap(); assert_eq!(0, after_parent_comment_delete.comment_count); assert_eq!(0, after_parent_comment_delete.comment_score); // Add in the two comments again, then delete the post. - let new_parent_comment = Comment::create(conn, &comment_form, None).await.unwrap(); - let _new_child_comment = - Comment::create(conn, &child_comment_form, Some(&new_parent_comment.path)) - .await - .unwrap(); + let new_parent_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); + let _new_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&new_parent_comment.path), + ) + .await + .unwrap(); comment_like.comment_id = new_parent_comment.id; - CommentLike::like(conn, &comment_like).await.unwrap(); - let after_comment_add = PersonAggregates::read(conn, inserted_person.id) + CommentLike::like(&mut *conn, &comment_like).await.unwrap(); + let after_comment_add = PersonAggregates::read(&mut *conn, inserted_person.id) .await .unwrap(); assert_eq!(2, after_comment_add.comment_count); assert_eq!(1, after_comment_add.comment_score); - Post::delete(conn, inserted_post.id).await.unwrap(); - let after_post_delete = PersonAggregates::read(conn, inserted_person.id) + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + let after_post_delete = PersonAggregates::read(&mut *conn, inserted_person.id) .await .unwrap(); assert_eq!(0, after_post_delete.comment_score); @@ -172,22 +186,26 @@ mod tests { assert_eq!(0, after_post_delete.post_count); // This should delete all the associated rows, and fire triggers - let person_num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); + let person_num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); assert_eq!(1, person_num_deleted); - Person::delete(conn, another_inserted_person.id) + Person::delete(&mut *conn, another_inserted_person.id) .await .unwrap(); // Delete the community - let community_num_deleted = Community::delete(conn, inserted_community.id) + let community_num_deleted = Community::delete(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, community_num_deleted); // Should be none found - let after_delete = PersonAggregates::read(conn, inserted_person.id).await; + let after_delete = PersonAggregates::read(&mut *conn, inserted_person.id).await; assert!(after_delete.is_err()); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); } } diff --git a/crates/db_schema/src/aggregates/person_post_aggregates.rs b/crates/db_schema/src/aggregates/person_post_aggregates.rs index 72bb4e6a2..ecf2e39fc 100644 --- a/crates/db_schema/src/aggregates/person_post_aggregates.rs +++ b/crates/db_schema/src/aggregates/person_post_aggregates.rs @@ -9,23 +9,26 @@ use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PersonPostAggregates { - pub async fn upsert(conn: &mut DbConn, form: &PersonPostAggregatesForm) -> Result { + pub async fn upsert( + mut conn: impl DbConn, + form: &PersonPostAggregatesForm, + ) -> Result { insert_into(person_post_aggregates) .values(form) .on_conflict((person_id, post_id)) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, person_id_: PersonId, post_id_: PostId, ) -> Result { person_post_aggregates .filter(post_id.eq(post_id_).and(person_id.eq(person_id_))) - .first::(conn) + .first::(&mut *conn) .await } } diff --git a/crates/db_schema/src/aggregates/post_aggregates.rs b/crates/db_schema/src/aggregates/post_aggregates.rs index 244c44ec9..fe9652e10 100644 --- a/crates/db_schema/src/aggregates/post_aggregates.rs +++ b/crates/db_schema/src/aggregates/post_aggregates.rs @@ -8,14 +8,14 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PostAggregates { - pub async fn read(conn: &mut DbConn, post_id: PostId) -> Result { + pub async fn read(mut conn: impl DbConn, post_id: PostId) -> Result { post_aggregates::table .filter(post_aggregates::post_id.eq(post_id)) - .first::(conn) + .first::(&mut *conn) .await } - pub async fn update_hot_rank(conn: &mut DbConn, post_id: PostId) -> Result { + pub async fn update_hot_rank(mut conn: impl DbConn, post_id: PostId) -> Result { diesel::update(post_aggregates::table) .filter(post_aggregates::post_id.eq(post_id)) .set(( @@ -25,7 +25,7 @@ impl PostAggregates { post_aggregates::newest_comment_time_necro, )), )) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -49,9 +49,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -61,7 +61,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let another_person = PersonInsertForm::builder() .name("jerry_community_agg".into()) @@ -69,7 +69,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let another_inserted_person = Person::create(conn, &another_person).await.unwrap(); + let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL_community_agg".into()) @@ -78,7 +78,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -86,7 +86,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -94,7 +94,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let child_comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -102,10 +104,13 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) - .await - .unwrap(); + let inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); let post_like = PostLikeForm { post_id: inserted_post.id, @@ -113,9 +118,11 @@ mod tests { score: 1, }; - PostLike::like(conn, &post_like).await.unwrap(); + PostLike::like(&mut *conn, &post_like).await.unwrap(); - let post_aggs_before_delete = PostAggregates::read(conn, inserted_post.id).await.unwrap(); + let post_aggs_before_delete = PostAggregates::read(&mut *conn, inserted_post.id) + .await + .unwrap(); assert_eq!(2, post_aggs_before_delete.comments); assert_eq!(1, post_aggs_before_delete.score); @@ -129,9 +136,11 @@ mod tests { score: -1, }; - PostLike::like(conn, &post_dislike).await.unwrap(); + PostLike::like(&mut *conn, &post_dislike).await.unwrap(); - let post_aggs_after_dislike = PostAggregates::read(conn, inserted_post.id).await.unwrap(); + let post_aggs_after_dislike = PostAggregates::read(&mut *conn, inserted_post.id) + .await + .unwrap(); assert_eq!(2, post_aggs_after_dislike.comments); assert_eq!(0, post_aggs_after_dislike.score); @@ -139,43 +148,53 @@ mod tests { assert_eq!(1, post_aggs_after_dislike.downvotes); // Remove the comments - Comment::delete(conn, inserted_comment.id).await.unwrap(); - Comment::delete(conn, inserted_child_comment.id) + Comment::delete(&mut *conn, inserted_comment.id) + .await + .unwrap(); + Comment::delete(&mut *conn, inserted_child_comment.id) + .await + .unwrap(); + let after_comment_delete = PostAggregates::read(&mut *conn, inserted_post.id) .await .unwrap(); - let after_comment_delete = PostAggregates::read(conn, inserted_post.id).await.unwrap(); assert_eq!(0, after_comment_delete.comments); assert_eq!(0, after_comment_delete.score); assert_eq!(1, after_comment_delete.upvotes); assert_eq!(1, after_comment_delete.downvotes); // Remove the first post like - PostLike::remove(conn, inserted_person.id, inserted_post.id) + PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id) + .await + .unwrap(); + let after_like_remove = PostAggregates::read(&mut *conn, inserted_post.id) .await .unwrap(); - let after_like_remove = PostAggregates::read(conn, inserted_post.id).await.unwrap(); assert_eq!(0, after_like_remove.comments); assert_eq!(-1, after_like_remove.score); assert_eq!(0, after_like_remove.upvotes); assert_eq!(1, after_like_remove.downvotes); // This should delete all the associated rows, and fire triggers - Person::delete(conn, another_inserted_person.id) + Person::delete(&mut *conn, another_inserted_person.id) + .await + .unwrap(); + let person_num_deleted = Person::delete(&mut *conn, inserted_person.id) .await .unwrap(); - let person_num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); assert_eq!(1, person_num_deleted); // Delete the community - let community_num_deleted = Community::delete(conn, inserted_community.id) + let community_num_deleted = Community::delete(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, community_num_deleted); // Should be none found, since the creator was deleted - let after_delete = PostAggregates::read(conn, inserted_post.id).await; + let after_delete = PostAggregates::read(&mut *conn, inserted_post.id).await; assert!(after_delete.is_err()); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); } } diff --git a/crates/db_schema/src/aggregates/site_aggregates.rs b/crates/db_schema/src/aggregates/site_aggregates.rs index 498a266fa..a1bcf5bdd 100644 --- a/crates/db_schema/src/aggregates/site_aggregates.rs +++ b/crates/db_schema/src/aggregates/site_aggregates.rs @@ -3,8 +3,8 @@ use diesel::result::Error; use diesel_async::RunQueryDsl; impl SiteAggregates { - pub async fn read(conn: &mut DbConn) -> Result { - site_aggregates::table.first::(conn).await + pub async fn read(mut conn: impl DbConn) -> Result { + site_aggregates::table.first::(&mut *conn).await } } @@ -28,9 +28,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -40,14 +40,14 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let site_form = SiteInsertForm::builder() .name("test_site".into()) .instance_id(inserted_instance.id) .build(); - let inserted_site = Site::create(conn, &site_form).await.unwrap(); + let inserted_site = Site::create(&mut *conn, &site_form).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL_site_agg".into()) @@ -56,7 +56,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -65,8 +65,8 @@ mod tests { .build(); // Insert two of those posts - let inserted_post = Post::create(conn, &new_post).await.unwrap(); - let _inserted_post_again = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); + let _inserted_post_again = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -75,7 +75,9 @@ mod tests { .build(); // Insert two of those comments - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let child_comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -83,12 +85,15 @@ mod tests { .post_id(inserted_post.id) .build(); - let _inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) - .await - .unwrap(); + let _inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); - let site_aggregates_before_delete = SiteAggregates::read(conn).await.unwrap(); + let site_aggregates_before_delete = SiteAggregates::read(&mut *conn).await.unwrap(); // TODO: this is unstable, sometimes it returns 0 users, sometimes 1 //assert_eq!(0, site_aggregates_before_delete.users); @@ -97,29 +102,33 @@ mod tests { assert_eq!(2, site_aggregates_before_delete.comments); // Try a post delete - Post::delete(conn, inserted_post.id).await.unwrap(); - let site_aggregates_after_post_delete = SiteAggregates::read(conn).await.unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + let site_aggregates_after_post_delete = SiteAggregates::read(&mut *conn).await.unwrap(); assert_eq!(1, site_aggregates_after_post_delete.posts); assert_eq!(0, site_aggregates_after_post_delete.comments); // This shouuld delete all the associated rows, and fire triggers - let person_num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); + let person_num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); assert_eq!(1, person_num_deleted); // Delete the community - let community_num_deleted = Community::delete(conn, inserted_community.id) + let community_num_deleted = Community::delete(&mut *conn, inserted_community.id) .await .unwrap(); assert_eq!(1, community_num_deleted); // Site should still exist, it can without a site creator. - let after_delete_creator = SiteAggregates::read(conn).await; + let after_delete_creator = SiteAggregates::read(&mut *conn).await; assert!(after_delete_creator.is_ok()); - Site::delete(conn, inserted_site.id).await.unwrap(); - let after_delete_site = SiteAggregates::read(conn).await; + Site::delete(&mut *conn, inserted_site.id).await.unwrap(); + let after_delete_site = SiteAggregates::read(&mut *conn).await; assert!(after_delete_site.is_err()); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); } } diff --git a/crates/db_schema/src/impls/activity.rs b/crates/db_schema/src/impls/activity.rs index 6edd490e0..672d831ff 100644 --- a/crates/db_schema/src/impls/activity.rs +++ b/crates/db_schema/src/impls/activity.rs @@ -13,39 +13,42 @@ impl Crud for Activity { type InsertForm = ActivityInsertForm; type UpdateForm = ActivityUpdateForm; type IdType = i32; - async fn read(conn: &mut DbConn, activity_id: i32) -> Result { - activity.find(activity_id).first::(conn).await + async fn read(mut conn: impl DbConn, activity_id: i32) -> Result { + activity.find(activity_id).first::(&mut *conn).await } - async fn create(conn: &mut DbConn, new_activity: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, new_activity: &Self::InsertForm) -> Result { insert_into(activity) .values(new_activity) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, activity_id: i32, new_activity: &Self::UpdateForm, ) -> Result { diesel::update(activity.find(activity_id)) .set(new_activity) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, activity_id: i32) -> Result { + async fn delete(mut conn: impl DbConn, activity_id: i32) -> Result { diesel::delete(activity.find(activity_id)) - .execute(conn) + .execute(&mut *conn) .await } } impl Activity { - pub async fn read_from_apub_id(conn: &mut DbConn, object_id: &DbUrl) -> Result { + pub async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: &DbUrl, + ) -> Result { activity .filter(ap_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -69,9 +72,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -81,7 +84,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_creator = Person::create(conn, &creator_form).await.unwrap(); + let inserted_creator = Person::create(&mut *conn, &creator_form).await.unwrap(); let ap_id_: DbUrl = Url::parse( "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c", @@ -110,7 +113,7 @@ mod tests { updated: None, }; - let inserted_activity = Activity::create(conn, &activity_form).await.unwrap(); + let inserted_activity = Activity::create(&mut *conn, &activity_form).await.unwrap(); let expected_activity = Activity { ap_id: ap_id_.clone(), @@ -122,10 +125,18 @@ mod tests { updated: None, }; - let read_activity = Activity::read(conn, inserted_activity.id).await.unwrap(); - let read_activity_by_apub_id = Activity::read_from_apub_id(conn, &ap_id_).await.unwrap(); - Person::delete(conn, inserted_creator.id).await.unwrap(); - Activity::delete(conn, inserted_activity.id).await.unwrap(); + let read_activity = Activity::read(&mut *conn, inserted_activity.id) + .await + .unwrap(); + let read_activity_by_apub_id = Activity::read_from_apub_id(&mut *conn, &ap_id_) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_creator.id) + .await + .unwrap(); + Activity::delete(&mut *conn, inserted_activity.id) + .await + .unwrap(); assert_eq!(expected_activity, read_activity); assert_eq!(expected_activity, read_activity_by_apub_id); diff --git a/crates/db_schema/src/impls/actor_language.rs b/crates/db_schema/src/impls/actor_language.rs index c730d725d..cc4cc5389 100644 --- a/crates/db_schema/src/impls/actor_language.rs +++ b/crates/db_schema/src/impls/actor_language.rs @@ -25,7 +25,7 @@ use diesel::{ ExpressionMethods, QueryDsl, }; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; use lemmy_utils::error::LemmyError; use tokio::sync::OnceCell; @@ -33,7 +33,7 @@ pub const UNDETERMINED_ID: LanguageId = LanguageId(0); impl LocalUserLanguage { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, for_local_user_id: LocalUserId, ) -> Result, Error> { use crate::schema::local_user_language::dsl::{ @@ -50,9 +50,9 @@ impl LocalUserLanguage { .filter(local_user_id.eq(for_local_user_id)) .order(language_id) .select(language_id) - .get_results(conn) + .get_results(&mut *conn) .await?; - convert_read_languages(conn, langs).await + convert_read_languages(&mut *conn, langs).await }) as _ }) .await @@ -62,14 +62,14 @@ impl LocalUserLanguage { /// /// If no language_id vector is given, it will show all languages pub async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, language_ids: Vec, for_local_user_id: LocalUserId, ) -> Result<(), Error> { - let mut lang_ids = convert_update_languages(conn, language_ids).await?; + let mut lang_ids = convert_update_languages(&mut *conn, language_ids).await?; // No need to update if languages are unchanged - let current = LocalUserLanguage::read(conn, for_local_user_id).await?; + let current = LocalUserLanguage::read(&mut *conn, for_local_user_id).await?; if current == lang_ids { return Ok(()); } @@ -91,7 +91,7 @@ impl LocalUserLanguage { use crate::schema::local_user_language::dsl::{local_user_id, local_user_language}; // Clear the current user languages delete(local_user_language.filter(local_user_id.eq(for_local_user_id))) - .execute(conn) + .execute(&mut *conn) .await?; for l in lang_ids { @@ -101,7 +101,7 @@ impl LocalUserLanguage { }; insert_into(local_user_language) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } Ok(()) @@ -112,45 +112,42 @@ impl LocalUserLanguage { } impl SiteLanguage { - pub async fn read_local_raw(conn: &mut DbConn) -> Result, Error> { + pub async fn read_local_raw(mut conn: impl DbConn) -> Result, Error> { site::table .inner_join(local_site::table) .inner_join(site_language::table) .order(site_language::id) .select(site_language::language_id) - .load(conn) + .load(&mut *conn) .await } - async fn read_raw( - conn: &mut AsyncPgConnection, - for_site_id: SiteId, - ) -> Result, Error> { + async fn read_raw(mut conn: impl DbConn, for_site_id: SiteId) -> Result, Error> { site_language::table .filter(site_language::site_id.eq(for_site_id)) .order(site_language::language_id) .select(site_language::language_id) - .load(conn) + .load(&mut *conn) .await } - pub async fn read(conn: &mut DbConn, for_site_id: SiteId) -> Result, Error> { - let langs = Self::read_raw(conn, for_site_id).await?; + pub async fn read(mut conn: impl DbConn, for_site_id: SiteId) -> Result, Error> { + let langs = Self::read_raw(&mut *conn, for_site_id).await?; - convert_read_languages(conn, langs).await + convert_read_languages(&mut *conn, langs).await } pub async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, language_ids: Vec, site: &Site, ) -> Result<(), Error> { let for_site_id = site.id; let instance_id = site.instance_id; - let lang_ids = convert_update_languages(conn, language_ids).await?; + let lang_ids = convert_update_languages(&mut *conn, language_ids).await?; // No need to update if languages are unchanged - let current = SiteLanguage::read(conn, site.id).await?; + let current = SiteLanguage::read(&mut *conn, site.id).await?; if current == lang_ids { return Ok(()); } @@ -163,7 +160,7 @@ impl SiteLanguage { // Clear the current languages delete(site_language.filter(site_id.eq(for_site_id))) - .execute(conn) + .execute(&mut *conn) .await?; for l in lang_ids { @@ -173,11 +170,11 @@ impl SiteLanguage { }; insert_into(site_language) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } - CommunityLanguage::limit_languages(conn, instance_id).await?; + CommunityLanguage::limit_languages(&mut *conn, instance_id).await?; Ok(()) }) as _ @@ -189,7 +186,7 @@ impl SiteLanguage { impl CommunityLanguage { /// Returns true if the given language is one of configured languages for given community pub async fn is_allowed_community_language( - conn: &mut DbConn, + mut conn: impl DbConn, for_language_id: Option, for_community_id: CommunityId, ) -> Result<(), LemmyError> { @@ -201,7 +198,7 @@ impl CommunityLanguage { .filter(language_id.eq(for_language_id)) .filter(community_id.eq(for_community_id)), )) - .get_result(conn) + .get_result(&mut *conn) .await?; if is_allowed { @@ -219,7 +216,7 @@ impl CommunityLanguage { /// community language, and it shouldnt be possible to post content in languages which are not /// allowed by local site. async fn limit_languages( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, for_instance_id: InstanceId, ) -> Result<(), Error> { use crate::schema::{ @@ -233,19 +230,19 @@ impl CommunityLanguage { .filter(c::instance_id.eq(for_instance_id)) .filter(sl::language_id.is_null()) .select(cl::language_id) - .get_results(conn) + .get_results(&mut *conn) .await?; for c in community_languages { delete(cl::community_language.filter(cl::language_id.eq(c))) - .execute(conn) + .execute(&mut *conn) .await?; } Ok(()) } async fn read_raw( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, for_community_id: CommunityId, ) -> Result, Error> { use crate::schema::community_language::dsl::{community_id, community_language, language_id}; @@ -253,30 +250,30 @@ impl CommunityLanguage { .filter(community_id.eq(for_community_id)) .order(language_id) .select(language_id) - .get_results(conn) + .get_results(&mut *conn) .await } pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, for_community_id: CommunityId, ) -> Result, Error> { - let langs = Self::read_raw(conn, for_community_id).await?; - convert_read_languages(conn, langs).await + let langs = Self::read_raw(&mut *conn, for_community_id).await?; + convert_read_languages(&mut *conn, langs).await } pub async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, mut language_ids: Vec, for_community_id: CommunityId, ) -> Result<(), Error> { if language_ids.is_empty() { - language_ids = SiteLanguage::read_local_raw(conn).await?; + language_ids = SiteLanguage::read_local_raw(&mut *conn).await?; } - let lang_ids = convert_update_languages(conn, language_ids).await?; + let lang_ids = convert_update_languages(&mut *conn, language_ids).await?; // No need to update if languages are unchanged - let current = CommunityLanguage::read_raw(conn, for_community_id).await?; + let current = CommunityLanguage::read_raw(&mut *conn, for_community_id).await?; if current == lang_ids { return Ok(()); } @@ -288,7 +285,7 @@ impl CommunityLanguage { use crate::schema::community_language::dsl::{community_id, community_language}; // Clear the current languages delete(community_language.filter(community_id.eq(for_community_id))) - .execute(conn) + .execute(&mut *conn) .await?; for l in lang_ids { @@ -298,7 +295,7 @@ impl CommunityLanguage { }; insert_into(community_language) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } Ok(()) @@ -309,7 +306,7 @@ impl CommunityLanguage { } pub async fn default_post_language( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, local_user_id: LocalUserId, ) -> Result, Error> { @@ -319,7 +316,7 @@ pub async fn default_post_language( .filter(ul::local_user_id.eq(local_user_id)) .filter(cl::community_id.eq(community_id)) .select(cl::language_id) - .get_results::(conn) + .get_results::(&mut *conn) .await?; if intersection.len() == 1 { @@ -334,12 +331,12 @@ pub async fn default_post_language( /// If no language is given, set all languages async fn convert_update_languages( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, language_ids: Vec, ) -> Result, Error> { if language_ids.is_empty() { Ok( - Language::read_all_conn(conn) + Language::read_all_conn(&mut *conn) .await? .into_iter() .map(|l| l.id) @@ -352,7 +349,7 @@ async fn convert_update_languages( /// If all languages are returned, return empty vec instead async fn convert_read_languages( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, language_ids: Vec, ) -> Result, Error> { static ALL_LANGUAGES_COUNT: OnceCell = OnceCell::const_new(); @@ -361,7 +358,7 @@ async fn convert_read_languages( use crate::schema::language::dsl::{id, language}; let count: i64 = language .select(count(id)) - .first(conn) + .first(&mut *conn) .await .expect("read number of languages"); count as usize @@ -405,37 +402,37 @@ mod tests { }; use serial_test::serial; - async fn test_langs1(conn: &mut DbConn) -> Vec { + async fn test_langs1(mut conn: impl DbConn) -> Vec { vec![ - Language::read_id_from_code(conn, Some("en")) + Language::read_id_from_code(&mut *conn, Some("en")) .await .unwrap() .unwrap(), - Language::read_id_from_code(conn, Some("fr")) + Language::read_id_from_code(&mut *conn, Some("fr")) .await .unwrap() .unwrap(), - Language::read_id_from_code(conn, Some("ru")) + Language::read_id_from_code(&mut *conn, Some("ru")) .await .unwrap() .unwrap(), ] } - async fn test_langs2(conn: &mut DbConn) -> Vec { + async fn test_langs2(mut conn: impl DbConn) -> Vec { vec![ - Language::read_id_from_code(conn, Some("fi")) + Language::read_id_from_code(&mut *conn, Some("fi")) .await .unwrap() .unwrap(), - Language::read_id_from_code(conn, Some("se")) + Language::read_id_from_code(&mut *conn, Some("se")) .await .unwrap() .unwrap(), ] } - async fn create_test_site(conn: &mut DbConn) -> (Site, Instance) { - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + async fn create_test_site(mut conn: impl DbConn) -> (Site, Instance) { + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -443,11 +440,13 @@ mod tests { .name("test site".to_string()) .instance_id(inserted_instance.id) .build(); - let site = Site::create(conn, &site_form).await.unwrap(); + let site = Site::create(&mut *conn, &site_form).await.unwrap(); // Create a local site, since this is necessary for local languages let local_site_form = LocalSiteInsertForm::builder().site_id(site.id).build(); - LocalSite::create(conn, &local_site_form).await.unwrap(); + LocalSite::create(&mut *conn, &local_site_form) + .await + .unwrap(); (site, inserted_instance) } @@ -455,15 +454,15 @@ mod tests { #[tokio::test] #[serial] async fn test_convert_update_languages() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; // call with empty vec, returns all languages - let converted1 = convert_update_languages(conn, vec![]).await.unwrap(); + let converted1 = convert_update_languages(&mut *conn, vec![]).await.unwrap(); assert_eq!(184, converted1.len()); // call with nonempty vec, returns same vec - let test_langs = test_langs1(conn).await; - let converted2 = convert_update_languages(conn, test_langs.clone()) + let test_langs = test_langs1(&mut *conn).await; + let converted2 = convert_update_languages(&mut *conn, test_langs.clone()) .await .unwrap(); assert_eq!(test_langs, converted2); @@ -472,16 +471,16 @@ mod tests { #[serial] async fn test_convert_read_languages() { use crate::schema::language::dsl::{id, language}; - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; // call with all languages, returns empty vec - let all_langs = language.select(id).get_results(conn).await.unwrap(); - let converted1: Vec = convert_read_languages(conn, all_langs).await.unwrap(); + let all_langs = language.select(id).get_results(&mut *conn).await.unwrap(); + let converted1: Vec = convert_read_languages(&mut *conn, all_langs).await.unwrap(); assert_eq!(0, converted1.len()); // call with nonempty vec, returns same vec - let test_langs = test_langs1(conn).await; - let converted2 = convert_read_languages(conn, test_langs.clone()) + let test_langs = test_langs1(&mut *conn).await; + let converted2 = convert_read_languages(&mut *conn, test_langs.clone()) .await .unwrap(); assert_eq!(test_langs, converted2); @@ -490,35 +489,35 @@ mod tests { #[tokio::test] #[serial] async fn test_site_languages() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let (site, instance) = create_test_site(conn).await; - let site_languages1 = SiteLanguage::read_local_raw(conn).await.unwrap(); + let (site, instance) = create_test_site(&mut *conn).await; + let site_languages1 = SiteLanguage::read_local_raw(&mut *conn).await.unwrap(); // site is created with all languages assert_eq!(184, site_languages1.len()); - let test_langs = test_langs1(conn).await; - SiteLanguage::update(conn, test_langs.clone(), &site) + let test_langs = test_langs1(&mut *conn).await; + SiteLanguage::update(&mut *conn, test_langs.clone(), &site) .await .unwrap(); - let site_languages2 = SiteLanguage::read_local_raw(conn).await.unwrap(); + let site_languages2 = SiteLanguage::read_local_raw(&mut *conn).await.unwrap(); // after update, site only has new languages assert_eq!(test_langs, site_languages2); - Site::delete(conn, site.id).await.unwrap(); - Instance::delete(conn, instance.id).await.unwrap(); - LocalSite::delete(conn).await.unwrap(); + Site::delete(&mut *conn, site.id).await.unwrap(); + Instance::delete(&mut *conn, instance.id).await.unwrap(); + LocalSite::delete(&mut *conn).await.unwrap(); } #[tokio::test] #[serial] async fn test_user_languages() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let (site, instance) = create_test_site(conn).await; - let mut test_langs = test_langs1(conn).await; - SiteLanguage::update(conn, test_langs.clone(), &site) + let (site, instance) = create_test_site(&mut *conn).await; + let mut test_langs = test_langs1(&mut *conn).await; + SiteLanguage::update(&mut *conn, test_langs.clone(), &site) .await .unwrap(); @@ -527,14 +526,18 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(instance.id) .build(); - let person = Person::create(conn, &person_form).await.unwrap(); + let person = Person::create(&mut *conn, &person_form).await.unwrap(); let local_user_form = LocalUserInsertForm::builder() .person_id(person.id) .password_encrypted("my_pw".to_string()) .build(); - let local_user = LocalUser::create(conn, &local_user_form).await.unwrap(); - let local_user_langs1 = LocalUserLanguage::read(conn, local_user.id).await.unwrap(); + let local_user = LocalUser::create(&mut *conn, &local_user_form) + .await + .unwrap(); + let local_user_langs1 = LocalUserLanguage::read(&mut *conn, local_user.id) + .await + .unwrap(); // new user should be initialized with site languages and undetermined //test_langs.push(UNDETERMINED_ID); @@ -543,35 +546,37 @@ mod tests { assert_eq!(test_langs, local_user_langs1); // update user languages - let test_langs2 = test_langs2(conn).await; - LocalUserLanguage::update(conn, test_langs2, local_user.id) + let test_langs2 = test_langs2(&mut *conn).await; + LocalUserLanguage::update(&mut *conn, test_langs2, local_user.id) + .await + .unwrap(); + let local_user_langs2 = LocalUserLanguage::read(&mut *conn, local_user.id) .await .unwrap(); - let local_user_langs2 = LocalUserLanguage::read(conn, local_user.id).await.unwrap(); assert_eq!(3, local_user_langs2.len()); - Person::delete(conn, person.id).await.unwrap(); - LocalUser::delete(conn, local_user.id).await.unwrap(); - Site::delete(conn, site.id).await.unwrap(); - LocalSite::delete(conn).await.unwrap(); - Instance::delete(conn, instance.id).await.unwrap(); + Person::delete(&mut *conn, person.id).await.unwrap(); + LocalUser::delete(&mut *conn, local_user.id).await.unwrap(); + Site::delete(&mut *conn, site.id).await.unwrap(); + LocalSite::delete(&mut *conn).await.unwrap(); + Instance::delete(&mut *conn, instance.id).await.unwrap(); } #[tokio::test] #[serial] async fn test_community_languages() { - let conn = &mut build_db_conn_for_tests().await; - let (site, instance) = create_test_site(conn).await; - let test_langs = test_langs1(conn).await; - SiteLanguage::update(conn, test_langs.clone(), &site) + let mut conn = build_db_conn_for_tests().await; + let (site, instance) = create_test_site(&mut *conn).await; + let test_langs = test_langs1(&mut *conn).await; + SiteLanguage::update(&mut *conn, test_langs.clone(), &site) .await .unwrap(); - let read_site_langs = SiteLanguage::read(conn, site.id).await.unwrap(); + let read_site_langs = SiteLanguage::read(&mut *conn, site.id).await.unwrap(); assert_eq!(test_langs, read_site_langs); // Test the local ones are the same - let read_local_site_langs = SiteLanguage::read_local_raw(conn).await.unwrap(); + let read_local_site_langs = SiteLanguage::read_local_raw(&mut *conn).await.unwrap(); assert_eq!(test_langs, read_local_site_langs); let community_form = CommunityInsertForm::builder() @@ -580,51 +585,65 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(instance.id) .build(); - let community = Community::create(conn, &community_form).await.unwrap(); - let community_langs1 = CommunityLanguage::read(conn, community.id).await.unwrap(); + let community = Community::create(&mut *conn, &community_form) + .await + .unwrap(); + let community_langs1 = CommunityLanguage::read(&mut *conn, community.id) + .await + .unwrap(); // community is initialized with site languages assert_eq!(test_langs, community_langs1); - let allowed_lang1 = - CommunityLanguage::is_allowed_community_language(conn, Some(test_langs[0]), community.id) - .await; + let allowed_lang1 = CommunityLanguage::is_allowed_community_language( + &mut *conn, + Some(test_langs[0]), + community.id, + ) + .await; assert!(allowed_lang1.is_ok()); - let test_langs2 = test_langs2(conn).await; - let allowed_lang2 = - CommunityLanguage::is_allowed_community_language(conn, Some(test_langs2[0]), community.id) - .await; + let test_langs2 = test_langs2(&mut *conn).await; + let allowed_lang2 = CommunityLanguage::is_allowed_community_language( + &mut *conn, + Some(test_langs2[0]), + community.id, + ) + .await; assert!(allowed_lang2.is_err()); // limit site languages to en, fi. after this, community languages should be updated to // intersection of old languages (en, fr, ru) and (en, fi), which is only fi. - SiteLanguage::update(conn, vec![test_langs[0], test_langs2[0]], &site) + SiteLanguage::update(&mut *conn, vec![test_langs[0], test_langs2[0]], &site) + .await + .unwrap(); + let community_langs2 = CommunityLanguage::read(&mut *conn, community.id) .await .unwrap(); - let community_langs2 = CommunityLanguage::read(conn, community.id).await.unwrap(); assert_eq!(vec![test_langs[0]], community_langs2); // update community languages to different ones - CommunityLanguage::update(conn, test_langs2.clone(), community.id) + CommunityLanguage::update(&mut *conn, test_langs2.clone(), community.id) + .await + .unwrap(); + let community_langs3 = CommunityLanguage::read(&mut *conn, community.id) .await .unwrap(); - let community_langs3 = CommunityLanguage::read(conn, community.id).await.unwrap(); assert_eq!(test_langs2, community_langs3); - Community::delete(conn, community.id).await.unwrap(); - Site::delete(conn, site.id).await.unwrap(); - LocalSite::delete(conn).await.unwrap(); - Instance::delete(conn, instance.id).await.unwrap(); + Community::delete(&mut *conn, community.id).await.unwrap(); + Site::delete(&mut *conn, site.id).await.unwrap(); + LocalSite::delete(&mut *conn).await.unwrap(); + Instance::delete(&mut *conn, instance.id).await.unwrap(); } #[tokio::test] #[serial] async fn test_default_post_language() { - let conn = &mut build_db_conn_for_tests().await; - let (site, instance) = create_test_site(conn).await; - let test_langs = test_langs1(conn).await; - let test_langs2 = test_langs2(conn).await; + let mut conn = build_db_conn_for_tests().await; + let (site, instance) = create_test_site(&mut *conn).await; + let test_langs = test_langs1(&mut *conn).await; + let test_langs2 = test_langs2(&mut *conn).await; let community_form = CommunityInsertForm::builder() .name("test community".to_string()) @@ -632,8 +651,10 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(instance.id) .build(); - let community = Community::create(conn, &community_form).await.unwrap(); - CommunityLanguage::update(conn, test_langs, community.id) + let community = Community::create(&mut *conn, &community_form) + .await + .unwrap(); + CommunityLanguage::update(&mut *conn, test_langs, community.id) .await .unwrap(); @@ -642,53 +663,55 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(instance.id) .build(); - let person = Person::create(conn, &person_form).await.unwrap(); + let person = Person::create(&mut *conn, &person_form).await.unwrap(); let local_user_form = LocalUserInsertForm::builder() .person_id(person.id) .password_encrypted("my_pw".to_string()) .build(); - let local_user = LocalUser::create(conn, &local_user_form).await.unwrap(); - LocalUserLanguage::update(conn, test_langs2, local_user.id) + let local_user = LocalUser::create(&mut *conn, &local_user_form) + .await + .unwrap(); + LocalUserLanguage::update(&mut *conn, test_langs2, local_user.id) .await .unwrap(); // no overlap in user/community languages, so defaults to undetermined - let def1 = default_post_language(conn, community.id, local_user.id) + let def1 = default_post_language(&mut *conn, community.id, local_user.id) .await .unwrap(); assert_eq!(None, def1); - let ru = Language::read_id_from_code(conn, Some("ru")) + let ru = Language::read_id_from_code(&mut *conn, Some("ru")) .await .unwrap() .unwrap(); let test_langs3 = vec![ ru, - Language::read_id_from_code(conn, Some("fi")) + Language::read_id_from_code(&mut *conn, Some("fi")) .await .unwrap() .unwrap(), - Language::read_id_from_code(conn, Some("se")) + Language::read_id_from_code(&mut *conn, Some("se")) .await .unwrap() .unwrap(), UNDETERMINED_ID, ]; - LocalUserLanguage::update(conn, test_langs3, local_user.id) + LocalUserLanguage::update(&mut *conn, test_langs3, local_user.id) .await .unwrap(); // this time, both have ru as common lang - let def2 = default_post_language(conn, community.id, local_user.id) + let def2 = default_post_language(&mut *conn, community.id, local_user.id) .await .unwrap(); assert_eq!(Some(ru), def2); - Person::delete(conn, person.id).await.unwrap(); - Community::delete(conn, community.id).await.unwrap(); - LocalUser::delete(conn, local_user.id).await.unwrap(); - Site::delete(conn, site.id).await.unwrap(); - LocalSite::delete(conn).await.unwrap(); - Instance::delete(conn, instance.id).await.unwrap(); + Person::delete(&mut *conn, person.id).await.unwrap(); + Community::delete(&mut *conn, community.id).await.unwrap(); + LocalUser::delete(&mut *conn, local_user.id).await.unwrap(); + Site::delete(&mut *conn, site.id).await.unwrap(); + LocalSite::delete(&mut *conn).await.unwrap(); + Instance::delete(&mut *conn, instance.id).await.unwrap(); } } diff --git a/crates/db_schema/src/impls/captcha_answer.rs b/crates/db_schema/src/impls/captcha_answer.rs index 9500d31e5..3605c24f4 100644 --- a/crates/db_schema/src/impls/captcha_answer.rs +++ b/crates/db_schema/src/impls/captcha_answer.rs @@ -15,15 +15,15 @@ use diesel::{ use diesel_async::RunQueryDsl; impl CaptchaAnswer { - pub async fn insert(conn: &mut DbConn, captcha: &CaptchaAnswerForm) -> Result { + pub async fn insert(mut conn: impl DbConn, captcha: &CaptchaAnswerForm) -> Result { insert_into(captcha_answer) .values(captcha) - .get_result::(conn) + .get_result::(&mut *conn) .await } pub async fn check_captcha( - conn: &mut DbConn, + mut conn: impl DbConn, to_check: CheckCaptchaAnswer, ) -> Result { // fetch requested captcha @@ -32,12 +32,12 @@ impl CaptchaAnswer { .filter((uuid).eq(to_check.uuid)) .filter(lower(answer).eq(to_check.answer.to_lowercase().clone())), )) - .get_result::(conn) + .get_result::(&mut *conn) .await?; // delete checked captcha delete(captcha_answer.filter(uuid.eq(to_check.uuid))) - .execute(conn) + .execute(&mut *conn) .await?; Ok(captcha_exists) @@ -55,10 +55,10 @@ mod tests { #[tokio::test] #[serial] async fn test_captcha_happy_path() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; let inserted = CaptchaAnswer::insert( - conn, + &mut *conn, &CaptchaAnswerForm { answer: "XYZ".to_string(), }, @@ -67,7 +67,7 @@ mod tests { .expect("should not fail to insert captcha"); let result = CaptchaAnswer::check_captcha( - conn, + &mut *conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(), @@ -82,10 +82,10 @@ mod tests { #[tokio::test] #[serial] async fn test_captcha_repeat_answer_fails() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; let inserted = CaptchaAnswer::insert( - conn, + &mut *conn, &CaptchaAnswerForm { answer: "XYZ".to_string(), }, @@ -94,7 +94,7 @@ mod tests { .expect("should not fail to insert captcha"); let _result = CaptchaAnswer::check_captcha( - conn, + &mut *conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(), @@ -103,7 +103,7 @@ mod tests { .await; let result_repeat = CaptchaAnswer::check_captcha( - conn, + &mut *conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(), diff --git a/crates/db_schema/src/impls/comment.rs b/crates/db_schema/src/impls/comment.rs index 4150c1348..4f9902531 100644 --- a/crates/db_schema/src/impls/comment.rs +++ b/crates/db_schema/src/impls/comment.rs @@ -25,7 +25,7 @@ use url::Url; impl Comment { pub async fn permadelete_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, ) -> Result, Error> { diesel::update(comment.filter(creator_id.eq(for_creator_id))) @@ -34,23 +34,23 @@ impl Comment { deleted.eq(true), updated.eq(naive_now()), )) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn update_removed_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, new_removed: bool, ) -> Result, Error> { diesel::update(comment.filter(creator_id.eq(for_creator_id))) .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn create( - conn: &mut DbConn, + mut conn: impl DbConn, comment_form: &CommentInsertForm, parent_path: Option<&Ltree>, ) -> Result { @@ -60,7 +60,7 @@ impl Comment { .on_conflict(ap_id) .do_update() .set(comment_form) - .get_result::(conn) + .get_result::(&mut *conn) .await; if let Ok(comment_insert) = inserted_comment { @@ -78,7 +78,7 @@ impl Comment { let updated_comment = diesel::update(comment.find(comment_id)) .set(path.eq(ltree)) - .get_result::(conn) + .get_result::(&mut *conn) .await; // Update the child count for the parent comment_aggregates @@ -110,7 +110,9 @@ from ( where ca.comment_id = c.id" ); - sql_query(update_child_count_stmt).execute(conn).await?; + sql_query(update_child_count_stmt) + .execute(&mut *conn) + .await?; } } updated_comment @@ -118,12 +120,15 @@ where ca.comment_id = c.id" inserted_comment } } - pub async fn read_from_apub_id(conn: &mut DbConn, object_id: Url) -> Result, Error> { + pub async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: Url, + ) -> Result, Error> { let object_id: DbUrl = object_id.into(); Ok( comment .filter(ap_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -147,27 +152,29 @@ impl Crud for Comment { type InsertForm = CommentInsertForm; type UpdateForm = CommentUpdateForm; type IdType = CommentId; - async fn read(conn: &mut DbConn, comment_id: CommentId) -> Result { - comment.find(comment_id).first::(conn).await + async fn read(mut conn: impl DbConn, comment_id: CommentId) -> Result { + comment.find(comment_id).first::(&mut *conn).await } - async fn delete(conn: &mut DbConn, comment_id: CommentId) -> Result { - diesel::delete(comment.find(comment_id)).execute(conn).await + async fn delete(mut conn: impl DbConn, comment_id: CommentId) -> Result { + diesel::delete(comment.find(comment_id)) + .execute(&mut *conn) + .await } /// This is unimplemented, use [[Comment::create]] - async fn create(_conn: &mut DbConn, _comment_form: &Self::InsertForm) -> Result { + async fn create(_conn: impl DbConn, _comment_form: &Self::InsertForm) -> Result { unimplemented!(); } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, comment_id: CommentId, comment_form: &Self::UpdateForm, ) -> Result { diesel::update(comment.find(comment_id)) .set(comment_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -176,18 +183,18 @@ impl Crud for Comment { impl Likeable for CommentLike { type Form = CommentLikeForm; type IdType = CommentId; - async fn like(conn: &mut DbConn, comment_like_form: &CommentLikeForm) -> Result { + async fn like(mut conn: impl DbConn, comment_like_form: &CommentLikeForm) -> Result { use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id}; insert_into(comment_like) .values(comment_like_form) .on_conflict((comment_id, person_id)) .do_update() .set(comment_like_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn remove( - conn: &mut DbConn, + mut conn: impl DbConn, person_id_: PersonId, comment_id_: CommentId, ) -> Result { @@ -197,7 +204,7 @@ impl Likeable for CommentLike { .filter(comment_id.eq(comment_id_)) .filter(person_id.eq(person_id_)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -205,18 +212,21 @@ impl Likeable for CommentLike { #[async_trait] impl Saveable for CommentSaved { type Form = CommentSavedForm; - async fn save(conn: &mut DbConn, comment_saved_form: &CommentSavedForm) -> Result { + async fn save( + mut conn: impl DbConn, + comment_saved_form: &CommentSavedForm, + ) -> Result { use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id}; insert_into(comment_saved) .values(comment_saved_form) .on_conflict((comment_id, person_id)) .do_update() .set(comment_saved_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn unsave( - conn: &mut DbConn, + mut conn: impl DbConn, comment_saved_form: &CommentSavedForm, ) -> Result { use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id}; @@ -225,7 +235,7 @@ impl Saveable for CommentSaved { .filter(comment_id.eq(comment_saved_form.comment_id)) .filter(person_id.eq(comment_saved_form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -258,9 +268,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -270,7 +280,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community".to_string()) @@ -279,7 +289,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -287,7 +297,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -295,7 +305,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let expected_comment = Comment { id: inserted_comment.id, @@ -319,10 +331,13 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_child_comment = - Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)) - .await - .unwrap(); + let inserted_child_comment = Comment::create( + &mut *conn, + &child_comment_form, + Some(&inserted_comment.path), + ) + .await + .unwrap(); // Comment Like let comment_like_form = CommentLikeForm { @@ -332,7 +347,9 @@ mod tests { score: 1, }; - let inserted_comment_like = CommentLike::like(conn, &comment_like_form).await.unwrap(); + let inserted_comment_like = CommentLike::like(&mut *conn, &comment_like_form) + .await + .unwrap(); let expected_comment_like = CommentLike { id: inserted_comment_like.id, @@ -349,7 +366,9 @@ mod tests { person_id: inserted_person.id, }; - let inserted_comment_saved = CommentSaved::save(conn, &comment_saved_form).await.unwrap(); + let inserted_comment_saved = CommentSaved::save(&mut *conn, &comment_saved_form) + .await + .unwrap(); let expected_comment_saved = CommentSaved { id: inserted_comment_saved.id, @@ -362,27 +381,35 @@ mod tests { .content(Some("A test comment".into())) .build(); - let updated_comment = Comment::update(conn, inserted_comment.id, &comment_update_form) + let updated_comment = Comment::update(&mut *conn, inserted_comment.id, &comment_update_form) .await .unwrap(); - let read_comment = Comment::read(conn, inserted_comment.id).await.unwrap(); - let like_removed = CommentLike::remove(conn, inserted_person.id, inserted_comment.id) + let read_comment = Comment::read(&mut *conn, inserted_comment.id) .await .unwrap(); - let saved_removed = CommentSaved::unsave(conn, &comment_saved_form) + let like_removed = CommentLike::remove(&mut *conn, inserted_person.id, inserted_comment.id) .await .unwrap(); - let num_deleted = Comment::delete(conn, inserted_comment.id).await.unwrap(); - Comment::delete(conn, inserted_child_comment.id) + let saved_removed = CommentSaved::unsave(&mut *conn, &comment_saved_form) .await .unwrap(); - Post::delete(conn, inserted_post.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + let num_deleted = Comment::delete(&mut *conn, inserted_comment.id) + .await + .unwrap(); + Comment::delete(&mut *conn, inserted_child_comment.id) + .await + .unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_comment, read_comment); assert_eq!(expected_comment, inserted_comment); diff --git a/crates/db_schema/src/impls/comment_reply.rs b/crates/db_schema/src/impls/comment_reply.rs index d915bc4f3..4569082f3 100644 --- a/crates/db_schema/src/impls/comment_reply.rs +++ b/crates/db_schema/src/impls/comment_reply.rs @@ -13,14 +13,17 @@ impl Crud for CommentReply { type InsertForm = CommentReplyInsertForm; type UpdateForm = CommentReplyUpdateForm; type IdType = CommentReplyId; - async fn read(conn: &mut DbConn, comment_reply_id: CommentReplyId) -> Result { + async fn read(mut conn: impl DbConn, comment_reply_id: CommentReplyId) -> Result { comment_reply .find(comment_reply_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, comment_reply_form: &Self::InsertForm) -> Result { + async fn create( + mut conn: impl DbConn, + comment_reply_form: &Self::InsertForm, + ) -> Result { // since the return here isnt utilized, we dont need to do an update // but get_result doesnt return the existing row here insert_into(comment_reply) @@ -28,25 +31,25 @@ impl Crud for CommentReply { .on_conflict((recipient_id, comment_id)) .do_update() .set(comment_reply_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, comment_reply_id: CommentReplyId, comment_reply_form: &Self::UpdateForm, ) -> Result { diesel::update(comment_reply.find(comment_reply_id)) .set(comment_reply_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } impl CommentReply { pub async fn mark_all_as_read( - conn: &mut DbConn, + mut conn: impl DbConn, for_recipient_id: PersonId, ) -> Result, Error> { diesel::update( @@ -55,17 +58,17 @@ impl CommentReply { .filter(read.eq(false)), ) .set(read.eq(true)) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn read_by_comment( - conn: &mut DbConn, + mut conn: impl DbConn, for_comment_id: CommentId, ) -> Result { comment_reply .filter(comment_id.eq(for_comment_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -89,9 +92,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -101,7 +104,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let recipient_form = PersonInsertForm::builder() .name("terrylakes recipient".into()) @@ -109,7 +112,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap(); + let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community lake".to_string()) @@ -118,7 +121,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -126,7 +129,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -134,7 +137,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let comment_reply_form = CommentReplyInsertForm { recipient_id: inserted_recipient.id, @@ -142,7 +147,7 @@ mod tests { read: None, }; - let inserted_reply = CommentReply::create(conn, &comment_reply_form) + let inserted_reply = CommentReply::create(&mut *conn, &comment_reply_form) .await .unwrap(); @@ -154,21 +159,32 @@ mod tests { published: inserted_reply.published, }; - let read_reply = CommentReply::read(conn, inserted_reply.id).await.unwrap(); + let read_reply = CommentReply::read(&mut *conn, inserted_reply.id) + .await + .unwrap(); let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) }; - let updated_reply = CommentReply::update(conn, inserted_reply.id, &comment_reply_update_form) - .await - .unwrap(); + let updated_reply = + CommentReply::update(&mut *conn, inserted_reply.id, &comment_reply_update_form) + .await + .unwrap(); - Comment::delete(conn, inserted_comment.id).await.unwrap(); - Post::delete(conn, inserted_post.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + Comment::delete(&mut *conn, inserted_comment.id) + .await + .unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_recipient.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Person::delete(conn, inserted_recipient.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_reply, read_reply); assert_eq!(expected_reply, inserted_reply); diff --git a/crates/db_schema/src/impls/comment_report.rs b/crates/db_schema/src/impls/comment_report.rs index 7eb43907f..e78bae0d6 100644 --- a/crates/db_schema/src/impls/comment_report.rs +++ b/crates/db_schema/src/impls/comment_report.rs @@ -22,12 +22,12 @@ impl Reportable for CommentReport { /// * `conn` - the postgres connection /// * `comment_report_form` - the filled CommentReportForm to insert async fn report( - conn: &mut DbConn, + mut conn: impl DbConn, comment_report_form: &CommentReportForm, ) -> Result { insert_into(comment_report) .values(comment_report_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } @@ -37,7 +37,7 @@ impl Reportable for CommentReport { /// * `report_id` - the id of the report to resolve /// * `by_resolver_id` - the id of the user resolving the report async fn resolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id_: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -47,7 +47,7 @@ impl Reportable for CommentReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } @@ -57,7 +57,7 @@ impl Reportable for CommentReport { /// * `report_id` - the id of the report to unresolve /// * `by_resolver_id` - the id of the user unresolving the report async fn unresolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id_: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -67,7 +67,7 @@ impl Reportable for CommentReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 10d00592d..6b0cb196c 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -27,22 +27,24 @@ impl Crud for Community { type InsertForm = CommunityInsertForm; type UpdateForm = CommunityUpdateForm; type IdType = CommunityId; - async fn read(conn: &mut DbConn, community_id: CommunityId) -> Result { + async fn read(mut conn: impl DbConn, community_id: CommunityId) -> Result { community::table .find(community_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, community_id: CommunityId) -> Result { + async fn delete(mut conn: impl DbConn, community_id: CommunityId) -> Result { diesel::delete(community::table.find(community_id)) - .execute(conn) + .execute(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { let is_new_community = match &form.actor_id { - Some(id) => Community::read_from_apub_id(conn, id).await?.is_none(), + Some(id) => Community::read_from_apub_id(&mut *conn, id) + .await? + .is_none(), None => true, }; @@ -52,25 +54,25 @@ impl Crud for Community { .on_conflict(community::actor_id) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; // Initialize languages for new community if is_new_community { - CommunityLanguage::update(conn, vec![], community_.id).await?; + CommunityLanguage::update(&mut *conn, vec![], community_.id).await?; } Ok(community_) } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, form: &Self::UpdateForm, ) -> Result { diesel::update(community::table.find(community_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -79,18 +81,18 @@ impl Crud for Community { impl Joinable for CommunityModerator { type Form = CommunityModeratorForm; async fn join( - conn: &mut DbConn, + mut conn: impl DbConn, community_moderator_form: &CommunityModeratorForm, ) -> Result { use crate::schema::community_moderator::dsl::community_moderator; insert_into(community_moderator) .values(community_moderator_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn leave( - conn: &mut DbConn, + mut conn: impl DbConn, community_moderator_form: &CommunityModeratorForm, ) -> Result { use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id}; @@ -99,7 +101,7 @@ impl Joinable for CommunityModerator { .filter(community_id.eq(community_moderator_form.community_id)) .filter(person_id.eq(community_moderator_form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -112,21 +114,21 @@ pub enum CollectionType { impl Community { /// Get the community which has a given moderators or featured url, also return the collection type pub async fn get_by_collection_url( - conn: &mut DbConn, + mut conn: impl DbConn, url: &DbUrl, ) -> Result<(Community, CollectionType), Error> { use crate::schema::community::dsl::{featured_url, moderators_url}; use CollectionType::*; let res = community::table .filter(moderators_url.eq(url)) - .first::(conn) + .first::(&mut *conn) .await; if let Ok(c) = res { return Ok((c, Moderators)); } let res = community::table .filter(featured_url.eq(url)) - .first::(conn) + .first::(&mut *conn) .await; if let Ok(c) = res { return Ok((c, Featured)); @@ -137,35 +139,35 @@ impl Community { impl CommunityModerator { pub async fn delete_for_community( - conn: &mut DbConn, + mut conn: impl DbConn, for_community_id: CommunityId, ) -> Result { use crate::schema::community_moderator::dsl::{community_id, community_moderator}; diesel::delete(community_moderator.filter(community_id.eq(for_community_id))) - .execute(conn) + .execute(&mut *conn) .await } pub async fn leave_all_communities( - conn: &mut DbConn, + mut conn: impl DbConn, for_person_id: PersonId, ) -> Result { use crate::schema::community_moderator::dsl::{community_moderator, person_id}; diesel::delete(community_moderator.filter(person_id.eq(for_person_id))) - .execute(conn) + .execute(&mut *conn) .await } pub async fn get_person_moderated_communities( - conn: &mut DbConn, + mut conn: impl DbConn, for_person_id: PersonId, ) -> Result, Error> { use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id}; community_moderator .filter(person_id.eq(for_person_id)) .select(community_id) - .load::(conn) + .load::(&mut *conn) .await } } @@ -174,7 +176,7 @@ impl CommunityModerator { impl Bannable for CommunityPersonBan { type Form = CommunityPersonBanForm; async fn ban( - conn: &mut DbConn, + mut conn: impl DbConn, community_person_ban_form: &CommunityPersonBanForm, ) -> Result { use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id}; @@ -183,12 +185,12 @@ impl Bannable for CommunityPersonBan { .on_conflict((community_id, person_id)) .do_update() .set(community_person_ban_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn unban( - conn: &mut DbConn, + mut conn: impl DbConn, community_person_ban_form: &CommunityPersonBanForm, ) -> Result { use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id}; @@ -197,7 +199,7 @@ impl Bannable for CommunityPersonBan { .filter(community_id.eq(community_person_ban_form.community_id)) .filter(person_id.eq(community_person_ban_form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -221,18 +223,18 @@ impl CommunityFollower { #[async_trait] impl Followable for CommunityFollower { type Form = CommunityFollowerForm; - async fn follow(conn: &mut DbConn, form: &CommunityFollowerForm) -> Result { + async fn follow(mut conn: impl DbConn, form: &CommunityFollowerForm) -> Result { use crate::schema::community_follower::dsl::{community_follower, community_id, person_id}; insert_into(community_follower) .values(form) .on_conflict((community_id, person_id)) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn follow_accepted( - conn: &mut DbConn, + mut conn: impl DbConn, community_id_: CommunityId, person_id_: PersonId, ) -> Result { @@ -248,28 +250,31 @@ impl Followable for CommunityFollower { .filter(person_id.eq(person_id_)), ) .set(pending.eq(false)) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn unfollow(conn: &mut DbConn, form: &CommunityFollowerForm) -> Result { + async fn unfollow(mut conn: impl DbConn, form: &CommunityFollowerForm) -> Result { use crate::schema::community_follower::dsl::{community_follower, community_id, person_id}; diesel::delete( community_follower .filter(community_id.eq(&form.community_id)) .filter(person_id.eq(&form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } #[async_trait] impl ApubActor for Community { - async fn read_from_apub_id(conn: &mut DbConn, object_id: &DbUrl) -> Result, Error> { + async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: &DbUrl, + ) -> Result, Error> { Ok( community::table .filter(community::actor_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -277,7 +282,7 @@ impl ApubActor for Community { } async fn read_from_name( - conn: &mut DbConn, + mut conn: impl DbConn, community_name: &str, include_deleted: bool, ) -> Result { @@ -290,11 +295,11 @@ impl ApubActor for Community { .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)); } - q.first::(conn).await + q.first::(&mut *conn).await } async fn read_from_name_and_domain( - conn: &mut DbConn, + mut conn: impl DbConn, community_name: &str, for_domain: &str, ) -> Result { @@ -303,7 +308,7 @@ impl ApubActor for Community { .filter(lower(community::name).eq(community_name.to_lowercase())) .filter(instance::domain.eq(for_domain)) .select(community::all_columns) - .first::(conn) + .first::(&mut *conn) .await } } @@ -334,9 +339,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -346,7 +351,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("TIL".into()) @@ -355,7 +360,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let expected_community = Community { id: inserted_community.id, @@ -390,9 +395,10 @@ mod tests { pending: false, }; - let inserted_community_follower = CommunityFollower::follow(conn, &community_follower_form) - .await - .unwrap(); + let inserted_community_follower = + CommunityFollower::follow(&mut *conn, &community_follower_form) + .await + .unwrap(); let expected_community_follower = CommunityFollower { id: inserted_community_follower.id, @@ -407,9 +413,10 @@ mod tests { person_id: inserted_person.id, }; - let inserted_community_moderator = CommunityModerator::join(conn, &community_moderator_form) - .await - .unwrap(); + let inserted_community_moderator = + CommunityModerator::join(&mut *conn, &community_moderator_form) + .await + .unwrap(); let expected_community_moderator = CommunityModerator { id: inserted_community_moderator.id, @@ -424,9 +431,10 @@ mod tests { expires: None, }; - let inserted_community_person_ban = CommunityPersonBan::ban(conn, &community_person_ban_form) - .await - .unwrap(); + let inserted_community_person_ban = + CommunityPersonBan::ban(&mut *conn, &community_person_ban_form) + .await + .unwrap(); let expected_community_person_ban = CommunityPersonBan { id: inserted_community_person_ban.id, @@ -436,29 +444,36 @@ mod tests { expires: None, }; - let read_community = Community::read(conn, inserted_community.id).await.unwrap(); + let read_community = Community::read(&mut *conn, inserted_community.id) + .await + .unwrap(); let update_community_form = CommunityUpdateForm::builder() .title(Some("nada".to_owned())) .build(); - let updated_community = Community::update(conn, inserted_community.id, &update_community_form) - .await - .unwrap(); + let updated_community = + Community::update(&mut *conn, inserted_community.id, &update_community_form) + .await + .unwrap(); - let ignored_community = CommunityFollower::unfollow(conn, &community_follower_form) + let ignored_community = CommunityFollower::unfollow(&mut *conn, &community_follower_form) .await .unwrap(); - let left_community = CommunityModerator::leave(conn, &community_moderator_form) + let left_community = CommunityModerator::leave(&mut *conn, &community_moderator_form) .await .unwrap(); - let unban = CommunityPersonBan::unban(conn, &community_person_ban_form) + let unban = CommunityPersonBan::unban(&mut *conn, &community_person_ban_form) .await .unwrap(); - let num_deleted = Community::delete(conn, inserted_community.id) + let num_deleted = Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_community, read_community); assert_eq!(expected_community, inserted_community); diff --git a/crates/db_schema/src/impls/community_block.rs b/crates/db_schema/src/impls/community_block.rs index 5f82f4dc4..041440244 100644 --- a/crates/db_schema/src/impls/community_block.rs +++ b/crates/db_schema/src/impls/community_block.rs @@ -10,22 +10,25 @@ use diesel_async::RunQueryDsl; #[async_trait] impl Blockable for CommunityBlock { type Form = CommunityBlockForm; - async fn block(conn: &mut DbConn, community_block_form: &Self::Form) -> Result { + async fn block(mut conn: impl DbConn, community_block_form: &Self::Form) -> Result { insert_into(community_block) .values(community_block_form) .on_conflict((person_id, community_id)) .do_update() .set(community_block_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn unblock(conn: &mut DbConn, community_block_form: &Self::Form) -> Result { + async fn unblock( + mut conn: impl DbConn, + community_block_form: &Self::Form, + ) -> Result { diesel::delete( community_block .filter(person_id.eq(community_block_form.person_id)) .filter(community_id.eq(community_block_form.community_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/custom_emoji.rs b/crates/db_schema/src/impls/custom_emoji.rs index 571a5db86..dee033859 100644 --- a/crates/db_schema/src/impls/custom_emoji.rs +++ b/crates/db_schema/src/impls/custom_emoji.rs @@ -14,42 +14,42 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CustomEmoji { - pub async fn create(conn: &mut DbConn, form: &CustomEmojiInsertForm) -> Result { + pub async fn create(mut conn: impl DbConn, form: &CustomEmojiInsertForm) -> Result { insert_into(custom_emoji) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } pub async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, emoji_id: CustomEmojiId, form: &CustomEmojiUpdateForm, ) -> Result { diesel::update(custom_emoji.find(emoji_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn delete(conn: &mut DbConn, emoji_id: CustomEmojiId) -> Result { + pub async fn delete(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result { diesel::delete(custom_emoji.find(emoji_id)) - .execute(conn) + .execute(&mut *conn) .await } } impl CustomEmojiKeyword { pub async fn create( - conn: &mut DbConn, + mut conn: impl DbConn, form: Vec, ) -> Result, Error> { insert_into(custom_emoji_keyword) .values(form) - .get_results::(conn) + .get_results::(&mut *conn) .await } - pub async fn delete(conn: &mut DbConn, emoji_id: CustomEmojiId) -> Result { + pub async fn delete(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result { diesel::delete(custom_emoji_keyword.filter(custom_emoji_id.eq(emoji_id))) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/email_verification.rs b/crates/db_schema/src/impls/email_verification.rs index 3bdacb03b..85262cb0a 100644 --- a/crates/db_schema/src/impls/email_verification.rs +++ b/crates/db_schema/src/impls/email_verification.rs @@ -19,26 +19,26 @@ use diesel::{ use diesel_async::RunQueryDsl; impl EmailVerification { - pub async fn create(conn: &mut DbConn, form: &EmailVerificationForm) -> Result { + pub async fn create(mut conn: impl DbConn, form: &EmailVerificationForm) -> Result { insert_into(email_verification) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn read_for_token(conn: &mut DbConn, token: &str) -> Result { + pub async fn read_for_token(mut conn: impl DbConn, token: &str) -> Result { email_verification .filter(verification_token.eq(token)) .filter(published.gt(now - 7.days())) - .first::(conn) + .first::(&mut *conn) .await } pub async fn delete_old_tokens_for_local_user( - conn: &mut DbConn, + mut conn: impl DbConn, local_user_id_: LocalUserId, ) -> Result { diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_))) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/federation_allowlist.rs b/crates/db_schema/src/impls/federation_allowlist.rs index 15b4c67d0..b5cb05317 100644 --- a/crates/db_schema/src/impls/federation_allowlist.rs +++ b/crates/db_schema/src/impls/federation_allowlist.rs @@ -7,20 +7,20 @@ use crate::{ utils::DbConn, }; use diesel::{dsl::insert_into, result::Error}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl FederationAllowList { - pub async fn replace(conn: &mut DbConn, list_opt: Option>) -> Result<(), Error> { + pub async fn replace(mut conn: impl DbConn, list_opt: Option>) -> Result<(), Error> { conn .build_transaction() .run(|conn| { Box::pin(async move { if let Some(list) = list_opt { - Self::clear(conn).await?; + Self::clear(&mut *conn).await?; for domain in list { // Upsert all of these as instances - let instance = Instance::read_or_create_with_conn(conn, domain).await?; + let instance = Instance::read_or_create_with_conn(&mut *conn, domain).await?; let form = FederationAllowListForm { instance_id: instance.id, @@ -28,7 +28,7 @@ impl FederationAllowList { }; insert_into(federation_allowlist::table) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } Ok(()) @@ -40,9 +40,9 @@ impl FederationAllowList { .await } - async fn clear(conn: &mut AsyncPgConnection) -> Result { + async fn clear(mut conn: impl DbConn) -> Result { diesel::delete(federation_allowlist::table) - .execute(conn) + .execute(&mut *conn) .await } } @@ -57,7 +57,7 @@ mod tests { #[tokio::test] #[serial] async fn test_allowlist_insert_and_clear() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; let domains = vec![ "tld1.xyz".to_string(), "tld2.xyz".to_string(), @@ -66,9 +66,11 @@ mod tests { let allowed = Some(domains.clone()); - FederationAllowList::replace(conn, allowed).await.unwrap(); + FederationAllowList::replace(&mut *conn, allowed) + .await + .unwrap(); - let allows = Instance::allowlist(conn).await.unwrap(); + let allows = Instance::allowlist(&mut *conn).await.unwrap(); let allows_domains = allows .iter() .map(|i| i.domain.clone()) @@ -80,13 +82,13 @@ mod tests { // Now test clearing them via Some(empty vec) let clear_allows = Some(Vec::new()); - FederationAllowList::replace(conn, clear_allows) + FederationAllowList::replace(&mut *conn, clear_allows) .await .unwrap(); - let allows = Instance::allowlist(conn).await.unwrap(); + let allows = Instance::allowlist(&mut *conn).await.unwrap(); assert_eq!(0, allows.len()); - Instance::delete_all(conn).await.unwrap(); + Instance::delete_all(&mut *conn).await.unwrap(); } } diff --git a/crates/db_schema/src/impls/federation_blocklist.rs b/crates/db_schema/src/impls/federation_blocklist.rs index 8c2558858..00d64e495 100644 --- a/crates/db_schema/src/impls/federation_blocklist.rs +++ b/crates/db_schema/src/impls/federation_blocklist.rs @@ -7,20 +7,20 @@ use crate::{ utils::DbConn, }; use diesel::{dsl::insert_into, result::Error}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl FederationBlockList { - pub async fn replace(conn: &mut DbConn, list_opt: Option>) -> Result<(), Error> { + pub async fn replace(mut conn: impl DbConn, list_opt: Option>) -> Result<(), Error> { conn .build_transaction() .run(|conn| { Box::pin(async move { if let Some(list) = list_opt { - Self::clear(conn).await?; + Self::clear(&mut *conn).await?; for domain in list { // Upsert all of these as instances - let instance = Instance::read_or_create_with_conn(conn, domain).await?; + let instance = Instance::read_or_create_with_conn(&mut *conn, domain).await?; let form = FederationBlockListForm { instance_id: instance.id, @@ -28,7 +28,7 @@ impl FederationBlockList { }; insert_into(federation_blocklist::table) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } Ok(()) @@ -40,9 +40,9 @@ impl FederationBlockList { .await } - async fn clear(conn: &mut AsyncPgConnection) -> Result { + async fn clear(mut conn: impl DbConn) -> Result { diesel::delete(federation_blocklist::table) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index 60695ea2f..1a81f7125 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -5,18 +5,18 @@ use crate::{ utils::{naive_now, DbConn}, }; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl Instance { pub(crate) async fn read_or_create_with_conn( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, domain_: String, ) -> Result { use crate::schema::instance::domain; // First try to read the instance row and return directly if found let instance = instance::table .filter(domain.eq(&domain_)) - .first::(conn) + .first::(&mut *conn) .await; match instance { Ok(i) => Ok(i), @@ -33,7 +33,7 @@ impl Instance { .on_conflict(instance::domain) .do_update() .set(&form) - .get_result::(conn) + .get_result::(&mut *conn) .await } e => e, @@ -42,40 +42,40 @@ impl Instance { /// Attempt to read Instance column for the given domain. If it doesnt exist, insert a new one. /// There is no need for update as the domain of an existing instance cant change. - pub async fn read_or_create(conn: &mut DbConn, domain: String) -> Result { - Self::read_or_create_with_conn(conn, domain).await + pub async fn read_or_create(mut conn: impl DbConn, domain: String) -> Result { + Self::read_or_create_with_conn(&mut *conn, domain).await } - pub async fn delete(conn: &mut DbConn, instance_id: InstanceId) -> Result { + pub async fn delete(mut conn: impl DbConn, instance_id: InstanceId) -> Result { diesel::delete(instance::table.find(instance_id)) - .execute(conn) + .execute(&mut *conn) .await } #[cfg(test)] - pub async fn delete_all(conn: &mut DbConn) -> Result { - diesel::delete(instance::table).execute(conn).await + pub async fn delete_all(mut conn: impl DbConn) -> Result { + diesel::delete(instance::table).execute(&mut *conn).await } - pub async fn allowlist(conn: &mut DbConn) -> Result, Error> { + pub async fn allowlist(mut conn: impl DbConn) -> Result, Error> { instance::table .inner_join(federation_allowlist::table) .select(instance::all_columns) - .get_results(conn) + .get_results(&mut *conn) .await } - pub async fn blocklist(conn: &mut DbConn) -> Result, Error> { + pub async fn blocklist(mut conn: impl DbConn) -> Result, Error> { instance::table .inner_join(federation_blocklist::table) .select(instance::all_columns) - .get_results(conn) + .get_results(&mut *conn) .await } - pub async fn linked(conn: &mut DbConn) -> Result, Error> { + pub async fn linked(mut conn: impl DbConn) -> Result, Error> { instance::table .left_join(federation_blocklist::table) .filter(federation_blocklist::id.is_null()) .select(instance::all_columns) - .get_results(conn) + .get_results(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/language.rs b/crates/db_schema/src/impls/language.rs index 13c8a6c35..0cf01785d 100644 --- a/crates/db_schema/src/impls/language.rs +++ b/crates/db_schema/src/impls/language.rs @@ -6,31 +6,31 @@ use crate::{ utils::DbConn, }; use diesel::{result::Error, QueryDsl}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl Language { - pub async fn read_all(conn: &mut DbConn) -> Result, Error> { - Self::read_all_conn(conn).await + pub async fn read_all(mut conn: impl DbConn) -> Result, Error> { + Self::read_all_conn(&mut *conn).await } - pub async fn read_all_conn(conn: &mut AsyncPgConnection) -> Result, Error> { - language.load::(conn).await + pub async fn read_all_conn(mut conn: impl DbConn) -> Result, Error> { + language.load::(&mut *conn).await } - pub async fn read_from_id(conn: &mut DbConn, id_: LanguageId) -> Result { - language.filter(id.eq(id_)).first::(conn).await + pub async fn read_from_id(mut conn: impl DbConn, id_: LanguageId) -> Result { + language.filter(id.eq(id_)).first::(&mut *conn).await } /// Attempts to find the given language code and return its ID. If not found, returns none. pub async fn read_id_from_code( - conn: &mut DbConn, + mut conn: impl DbConn, code_: Option<&str>, ) -> Result, Error> { if let Some(code_) = code_ { Ok( language .filter(code.eq(code_)) - .first::(conn) + .first::(&mut *conn) .await .map(|l| l.id) .ok(), @@ -49,9 +49,9 @@ mod tests { #[tokio::test] #[serial] async fn test_languages() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let all = Language::read_all(conn).await.unwrap(); + let all = Language::read_all(&mut *conn).await.unwrap(); assert_eq!(184, all.len()); assert_eq!("ak", all[5].code); diff --git a/crates/db_schema/src/impls/local_site.rs b/crates/db_schema/src/impls/local_site.rs index fd4980023..24bbfd397 100644 --- a/crates/db_schema/src/impls/local_site.rs +++ b/crates/db_schema/src/impls/local_site.rs @@ -7,22 +7,22 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::RunQueryDsl; impl LocalSite { - pub async fn create(conn: &mut DbConn, form: &LocalSiteInsertForm) -> Result { + pub async fn create(mut conn: impl DbConn, form: &LocalSiteInsertForm) -> Result { insert_into(local_site) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn read(conn: &mut DbConn) -> Result { - local_site.first::(conn).await + pub async fn read(mut conn: impl DbConn) -> Result { + local_site.first::(&mut *conn).await } - pub async fn update(conn: &mut DbConn, form: &LocalSiteUpdateForm) -> Result { + pub async fn update(mut conn: impl DbConn, form: &LocalSiteUpdateForm) -> Result { diesel::update(local_site) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn delete(conn: &mut DbConn) -> Result { - diesel::delete(local_site).execute(conn).await + pub async fn delete(mut conn: impl DbConn) -> Result { + diesel::delete(local_site).execute(&mut *conn).await } } diff --git a/crates/db_schema/src/impls/local_site_rate_limit.rs b/crates/db_schema/src/impls/local_site_rate_limit.rs index 26c32c0bb..e310124fb 100644 --- a/crates/db_schema/src/impls/local_site_rate_limit.rs +++ b/crates/db_schema/src/impls/local_site_rate_limit.rs @@ -11,27 +11,30 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::RunQueryDsl; impl LocalSiteRateLimit { - pub async fn read(conn: &mut DbConn) -> Result { - local_site_rate_limit::table.first::(conn).await + pub async fn read(mut conn: impl DbConn) -> Result { + local_site_rate_limit::table.first::(&mut *conn).await } pub async fn create( - conn: &mut DbConn, + mut conn: impl DbConn, form: &LocalSiteRateLimitInsertForm, ) -> Result { insert_into(local_site_rate_limit::table) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn update(conn: &mut DbConn, form: &LocalSiteRateLimitUpdateForm) -> Result<(), Error> { + pub async fn update( + mut conn: impl DbConn, + form: &LocalSiteRateLimitUpdateForm, + ) -> Result<(), Error> { // avoid error "There are no changes to save. This query cannot be built" if form.is_empty() { return Ok(()); } diesel::update(local_site_rate_limit::table) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; Ok(()) } diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index b8d07d733..f94082ea8 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -21,7 +21,7 @@ use diesel_async::RunQueryDsl; impl LocalUser { pub async fn update_password( - conn: &mut DbConn, + mut conn: impl DbConn, local_user_id: LocalUserId, new_password: &str, ) -> Result { @@ -32,30 +32,30 @@ impl LocalUser { password_encrypted.eq(password_hash), validator_time.eq(naive_now()), )) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn set_all_users_email_verified(conn: &mut DbConn) -> Result, Error> { + pub async fn set_all_users_email_verified(mut conn: impl DbConn) -> Result, Error> { diesel::update(local_user) .set(email_verified.eq(true)) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn set_all_users_registration_applications_accepted( - conn: &mut DbConn, + mut conn: impl DbConn, ) -> Result, Error> { diesel::update(local_user) .set(accepted_application.eq(true)) - .get_results::(conn) + .get_results::(&mut *conn) .await } - pub async fn is_email_taken(conn: &mut DbConn, email_: &str) -> Result { + pub async fn is_email_taken(mut conn: impl DbConn, email_: &str) -> Result { use diesel::dsl::{exists, select}; select(exists(local_user.filter(email.eq(email_)))) - .get_result(conn) + .get_result(&mut *conn) .await } } @@ -65,15 +65,18 @@ impl Crud for LocalUser { type InsertForm = LocalUserInsertForm; type UpdateForm = LocalUserUpdateForm; type IdType = LocalUserId; - async fn read(conn: &mut DbConn, local_user_id: LocalUserId) -> Result { - local_user.find(local_user_id).first::(conn).await - } - async fn delete(conn: &mut DbConn, local_user_id: LocalUserId) -> Result { - diesel::delete(local_user.find(local_user_id)) - .execute(conn) + async fn read(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result { + local_user + .find(local_user_id) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn delete(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result { + diesel::delete(local_user.find(local_user_id)) + .execute(&mut *conn) + .await + } + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { let mut form_with_encrypted_password = form.clone(); let password_hash = hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password"); @@ -81,29 +84,29 @@ impl Crud for LocalUser { let local_user_ = insert_into(local_user) .values(form_with_encrypted_password) - .get_result::(conn) + .get_result::(&mut *conn) .await?; - let site_languages = SiteLanguage::read_local_raw(conn).await; + let site_languages = SiteLanguage::read_local_raw(&mut *conn).await; if let Ok(langs) = site_languages { // if site exists, init user with site languages - LocalUserLanguage::update(conn, langs, local_user_.id).await?; + LocalUserLanguage::update(&mut *conn, langs, local_user_.id).await?; } else { // otherwise, init with all languages (this only happens during tests and // for first admin user, which is created before site) - LocalUserLanguage::update(conn, vec![], local_user_.id).await?; + LocalUserLanguage::update(&mut *conn, vec![], local_user_.id).await?; } Ok(local_user_) } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, local_user_id: LocalUserId, form: &Self::UpdateForm, ) -> Result { diesel::update(local_user.find(local_user_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/moderator.rs b/crates/db_schema/src/impls/moderator.rs index 995b8660f..e6193c941 100644 --- a/crates/db_schema/src/impls/moderator.rs +++ b/crates/db_schema/src/impls/moderator.rs @@ -42,28 +42,31 @@ impl Crud for ModRemovePost { type InsertForm = ModRemovePostForm; type UpdateForm = ModRemovePostForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; - mod_remove_post.find(from_id).first::(conn).await + mod_remove_post + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModRemovePostForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModRemovePostForm) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; insert_into(mod_remove_post) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModRemovePostForm, ) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; diesel::update(mod_remove_post.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -73,24 +76,28 @@ impl Crud for ModLockPost { type InsertForm = ModLockPostForm; type UpdateForm = ModLockPostForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; - mod_lock_post.find(from_id).first::(conn).await + mod_lock_post.find(from_id).first::(&mut *conn).await } - async fn create(conn: &mut DbConn, form: &ModLockPostForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModLockPostForm) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; insert_into(mod_lock_post) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &ModLockPostForm) -> Result { + async fn update( + mut conn: impl DbConn, + from_id: i32, + form: &ModLockPostForm, + ) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; diesel::update(mod_lock_post.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -100,28 +107,31 @@ impl Crud for ModFeaturePost { type InsertForm = ModFeaturePostForm; type UpdateForm = ModFeaturePostForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; - mod_feature_post.find(from_id).first::(conn).await + mod_feature_post + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModFeaturePostForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModFeaturePostForm) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; insert_into(mod_feature_post) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModFeaturePostForm, ) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; diesel::update(mod_feature_post.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -131,28 +141,31 @@ impl Crud for ModRemoveComment { type InsertForm = ModRemoveCommentForm; type UpdateForm = ModRemoveCommentForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; - mod_remove_comment.find(from_id).first::(conn).await + mod_remove_comment + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModRemoveCommentForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModRemoveCommentForm) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; insert_into(mod_remove_comment) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModRemoveCommentForm, ) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; diesel::update(mod_remove_comment.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -162,28 +175,31 @@ impl Crud for ModRemoveCommunity { type InsertForm = ModRemoveCommunityForm; type UpdateForm = ModRemoveCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; - mod_remove_community.find(from_id).first::(conn).await + mod_remove_community + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModRemoveCommunityForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModRemoveCommunityForm) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; insert_into(mod_remove_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModRemoveCommunityForm, ) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; diesel::update(mod_remove_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -193,31 +209,31 @@ impl Crud for ModBanFromCommunity { type InsertForm = ModBanFromCommunityForm; type UpdateForm = ModBanFromCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; mod_ban_from_community .find(from_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &ModBanFromCommunityForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModBanFromCommunityForm) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; insert_into(mod_ban_from_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModBanFromCommunityForm, ) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; diesel::update(mod_ban_from_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -227,24 +243,24 @@ impl Crud for ModBan { type InsertForm = ModBanForm; type UpdateForm = ModBanForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_ban::dsl::mod_ban; - mod_ban.find(from_id).first::(conn).await + mod_ban.find(from_id).first::(&mut *conn).await } - async fn create(conn: &mut DbConn, form: &ModBanForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::mod_ban; insert_into(mod_ban) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &ModBanForm) -> Result { + async fn update(mut conn: impl DbConn, from_id: i32, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::mod_ban; diesel::update(mod_ban.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -255,28 +271,31 @@ impl Crud for ModHideCommunity { type UpdateForm = ModHideCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; - mod_hide_community.find(from_id).first::(conn).await + mod_hide_community + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModHideCommunityForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModHideCommunityForm) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; insert_into(mod_hide_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModHideCommunityForm, ) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; diesel::update(mod_hide_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -286,28 +305,31 @@ impl Crud for ModAddCommunity { type InsertForm = ModAddCommunityForm; type UpdateForm = ModAddCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; - mod_add_community.find(from_id).first::(conn).await + mod_add_community + .find(from_id) + .first::(&mut *conn) + .await } - async fn create(conn: &mut DbConn, form: &ModAddCommunityForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModAddCommunityForm) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; insert_into(mod_add_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModAddCommunityForm, ) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; diesel::update(mod_add_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -317,31 +339,31 @@ impl Crud for ModTransferCommunity { type InsertForm = ModTransferCommunityForm; type UpdateForm = ModTransferCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_transfer_community::dsl::mod_transfer_community; mod_transfer_community .find(from_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &ModTransferCommunityForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModTransferCommunityForm) -> Result { use crate::schema::mod_transfer_community::dsl::mod_transfer_community; insert_into(mod_transfer_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, from_id: i32, form: &ModTransferCommunityForm, ) -> Result { use crate::schema::mod_transfer_community::dsl::mod_transfer_community; diesel::update(mod_transfer_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -351,24 +373,24 @@ impl Crud for ModAdd { type InsertForm = ModAddForm; type UpdateForm = ModAddForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::mod_add::dsl::mod_add; - mod_add.find(from_id).first::(conn).await + mod_add.find(from_id).first::(&mut *conn).await } - async fn create(conn: &mut DbConn, form: &ModAddForm) -> Result { + async fn create(mut conn: impl DbConn, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::mod_add; insert_into(mod_add) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &ModAddForm) -> Result { + async fn update(mut conn: impl DbConn, from_id: i32, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::mod_add; diesel::update(mod_add.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -378,24 +400,31 @@ impl Crud for AdminPurgePerson { type InsertForm = AdminPurgePersonForm; type UpdateForm = AdminPurgePersonForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; - admin_purge_person.find(from_id).first::(conn).await - } - - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { - use crate::schema::admin_purge_person::dsl::admin_purge_person; - insert_into(admin_purge_person) - .values(form) - .get_result::(conn) + admin_purge_person + .find(from_id) + .first::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { + use crate::schema::admin_purge_person::dsl::admin_purge_person; + insert_into(admin_purge_person) + .values(form) + .get_result::(&mut *conn) + .await + } + + async fn update( + mut conn: impl DbConn, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; diesel::update(admin_purge_person.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -405,27 +434,31 @@ impl Crud for AdminPurgeCommunity { type InsertForm = AdminPurgeCommunityForm; type UpdateForm = AdminPurgeCommunityForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; admin_purge_community .find(from_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; insert_into(admin_purge_community) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &Self::InsertForm) -> Result { + async fn update( + mut conn: impl DbConn, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; diesel::update(admin_purge_community.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -435,24 +468,31 @@ impl Crud for AdminPurgePost { type InsertForm = AdminPurgePostForm; type UpdateForm = AdminPurgePostForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; - admin_purge_post.find(from_id).first::(conn).await - } - - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { - use crate::schema::admin_purge_post::dsl::admin_purge_post; - insert_into(admin_purge_post) - .values(form) - .get_result::(conn) + admin_purge_post + .find(from_id) + .first::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { + use crate::schema::admin_purge_post::dsl::admin_purge_post; + insert_into(admin_purge_post) + .values(form) + .get_result::(&mut *conn) + .await + } + + async fn update( + mut conn: impl DbConn, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; diesel::update(admin_purge_post.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -462,24 +502,31 @@ impl Crud for AdminPurgeComment { type InsertForm = AdminPurgeCommentForm; type UpdateForm = AdminPurgeCommentForm; type IdType = i32; - async fn read(conn: &mut DbConn, from_id: i32) -> Result { + async fn read(mut conn: impl DbConn, from_id: i32) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; - admin_purge_comment.find(from_id).first::(conn).await - } - - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { - use crate::schema::admin_purge_comment::dsl::admin_purge_comment; - insert_into(admin_purge_comment) - .values(form) - .get_result::(conn) + admin_purge_comment + .find(from_id) + .first::(&mut *conn) .await } - async fn update(conn: &mut DbConn, from_id: i32, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { + use crate::schema::admin_purge_comment::dsl::admin_purge_comment; + insert_into(admin_purge_comment) + .values(form) + .get_result::(&mut *conn) + .await + } + + async fn update( + mut conn: impl DbConn, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; diesel::update(admin_purge_comment.find(from_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -522,9 +569,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -534,7 +581,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_mod = Person::create(conn, &new_mod).await.unwrap(); + let inserted_mod = Person::create(&mut *conn, &new_mod).await.unwrap(); let new_person = PersonInsertForm::builder() .name("jim2".into()) @@ -542,7 +589,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("mod_community".to_string()) @@ -551,7 +598,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post thweep".into()) @@ -559,7 +606,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -567,7 +614,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); // Now the actual tests @@ -578,10 +627,10 @@ mod tests { reason: None, removed: None, }; - let inserted_mod_remove_post = ModRemovePost::create(conn, &mod_remove_post_form) + let inserted_mod_remove_post = ModRemovePost::create(&mut *conn, &mod_remove_post_form) .await .unwrap(); - let read_mod_remove_post = ModRemovePost::read(conn, inserted_mod_remove_post.id) + let read_mod_remove_post = ModRemovePost::read(&mut *conn, inserted_mod_remove_post.id) .await .unwrap(); let expected_mod_remove_post = ModRemovePost { @@ -600,10 +649,10 @@ mod tests { post_id: inserted_post.id, locked: None, }; - let inserted_mod_lock_post = ModLockPost::create(conn, &mod_lock_post_form) + let inserted_mod_lock_post = ModLockPost::create(&mut *conn, &mod_lock_post_form) .await .unwrap(); - let read_mod_lock_post = ModLockPost::read(conn, inserted_mod_lock_post.id) + let read_mod_lock_post = ModLockPost::read(&mut *conn, inserted_mod_lock_post.id) .await .unwrap(); let expected_mod_lock_post = ModLockPost { @@ -622,10 +671,10 @@ mod tests { featured: false, is_featured_community: true, }; - let inserted_mod_feature_post = ModFeaturePost::create(conn, &mod_feature_post_form) + let inserted_mod_feature_post = ModFeaturePost::create(&mut *conn, &mod_feature_post_form) .await .unwrap(); - let read_mod_feature_post = ModFeaturePost::read(conn, inserted_mod_feature_post.id) + let read_mod_feature_post = ModFeaturePost::read(&mut *conn, inserted_mod_feature_post.id) .await .unwrap(); let expected_mod_feature_post = ModFeaturePost { @@ -645,12 +694,14 @@ mod tests { reason: None, removed: None, }; - let inserted_mod_remove_comment = ModRemoveComment::create(conn, &mod_remove_comment_form) - .await - .unwrap(); - let read_mod_remove_comment = ModRemoveComment::read(conn, inserted_mod_remove_comment.id) - .await - .unwrap(); + let inserted_mod_remove_comment = + ModRemoveComment::create(&mut *conn, &mod_remove_comment_form) + .await + .unwrap(); + let read_mod_remove_comment = + ModRemoveComment::read(&mut *conn, inserted_mod_remove_comment.id) + .await + .unwrap(); let expected_mod_remove_comment = ModRemoveComment { id: inserted_mod_remove_comment.id, comment_id: inserted_comment.id, @@ -670,11 +721,11 @@ mod tests { expires: None, }; let inserted_mod_remove_community = - ModRemoveCommunity::create(conn, &mod_remove_community_form) + ModRemoveCommunity::create(&mut *conn, &mod_remove_community_form) .await .unwrap(); let read_mod_remove_community = - ModRemoveCommunity::read(conn, inserted_mod_remove_community.id) + ModRemoveCommunity::read(&mut *conn, inserted_mod_remove_community.id) .await .unwrap(); let expected_mod_remove_community = ModRemoveCommunity { @@ -698,11 +749,11 @@ mod tests { expires: None, }; let inserted_mod_ban_from_community = - ModBanFromCommunity::create(conn, &mod_ban_from_community_form) + ModBanFromCommunity::create(&mut *conn, &mod_ban_from_community_form) .await .unwrap(); let read_mod_ban_from_community = - ModBanFromCommunity::read(conn, inserted_mod_ban_from_community.id) + ModBanFromCommunity::read(&mut *conn, inserted_mod_ban_from_community.id) .await .unwrap(); let expected_mod_ban_from_community = ModBanFromCommunity { @@ -725,8 +776,8 @@ mod tests { banned: None, expires: None, }; - let inserted_mod_ban = ModBan::create(conn, &mod_ban_form).await.unwrap(); - let read_mod_ban = ModBan::read(conn, inserted_mod_ban.id).await.unwrap(); + let inserted_mod_ban = ModBan::create(&mut *conn, &mod_ban_form).await.unwrap(); + let read_mod_ban = ModBan::read(&mut *conn, inserted_mod_ban.id).await.unwrap(); let expected_mod_ban = ModBan { id: inserted_mod_ban.id, mod_person_id: inserted_mod.id, @@ -745,10 +796,10 @@ mod tests { community_id: inserted_community.id, removed: None, }; - let inserted_mod_add_community = ModAddCommunity::create(conn, &mod_add_community_form) + let inserted_mod_add_community = ModAddCommunity::create(&mut *conn, &mod_add_community_form) .await .unwrap(); - let read_mod_add_community = ModAddCommunity::read(conn, inserted_mod_add_community.id) + let read_mod_add_community = ModAddCommunity::read(&mut *conn, inserted_mod_add_community.id) .await .unwrap(); let expected_mod_add_community = ModAddCommunity { @@ -767,8 +818,8 @@ mod tests { other_person_id: inserted_person.id, removed: None, }; - let inserted_mod_add = ModAdd::create(conn, &mod_add_form).await.unwrap(); - let read_mod_add = ModAdd::read(conn, inserted_mod_add.id).await.unwrap(); + let inserted_mod_add = ModAdd::create(&mut *conn, &mod_add_form).await.unwrap(); + let read_mod_add = ModAdd::read(&mut *conn, inserted_mod_add.id).await.unwrap(); let expected_mod_add = ModAdd { id: inserted_mod_add.id, mod_person_id: inserted_mod.id, @@ -777,14 +828,20 @@ mod tests { when_: inserted_mod_add.when_, }; - Comment::delete(conn, inserted_comment.id).await.unwrap(); - Post::delete(conn, inserted_post.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + Comment::delete(&mut *conn, inserted_comment.id) + .await + .unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_mod.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Person::delete(conn, inserted_mod.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_mod_remove_post, read_mod_remove_post); assert_eq!(expected_mod_lock_post, read_mod_lock_post); diff --git a/crates/db_schema/src/impls/password_reset_request.rs b/crates/db_schema/src/impls/password_reset_request.rs index a7737e719..fb2b3022f 100644 --- a/crates/db_schema/src/impls/password_reset_request.rs +++ b/crates/db_schema/src/impls/password_reset_request.rs @@ -24,33 +24,33 @@ impl Crud for PasswordResetRequest { type InsertForm = PasswordResetRequestForm; type UpdateForm = PasswordResetRequestForm; type IdType = i32; - async fn read(conn: &mut DbConn, password_reset_request_id: i32) -> Result { + async fn read(mut conn: impl DbConn, password_reset_request_id: i32) -> Result { password_reset_request .find(password_reset_request_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &PasswordResetRequestForm) -> Result { + async fn create(mut conn: impl DbConn, form: &PasswordResetRequestForm) -> Result { insert_into(password_reset_request) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, password_reset_request_id: i32, form: &PasswordResetRequestForm, ) -> Result { diesel::update(password_reset_request.find(password_reset_request_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } impl PasswordResetRequest { pub async fn create_token( - conn: &mut DbConn, + mut conn: impl DbConn, from_local_user_id: LocalUserId, token: &str, ) -> Result { @@ -63,10 +63,10 @@ impl PasswordResetRequest { token_encrypted: token_hash, }; - Self::create(conn, &form).await + Self::create(&mut *conn, &form).await } pub async fn read_from_token( - conn: &mut DbConn, + mut conn: impl DbConn, token: &str, ) -> Result { let mut hasher = Sha256::new(); @@ -75,19 +75,19 @@ impl PasswordResetRequest { password_reset_request .filter(token_encrypted.eq(token_hash)) .filter(published.gt(now - 1.days())) - .first::(conn) + .first::(&mut *conn) .await } pub async fn get_recent_password_resets_count( - conn: &mut DbConn, + mut conn: impl DbConn, user_id: LocalUserId, ) -> Result { password_reset_request .filter(local_user_id.eq(user_id)) .filter(published.gt(now - 1.days())) .count() - .get_result(conn) + .get_result(&mut *conn) .await } } @@ -117,9 +117,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -129,20 +129,22 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let new_local_user = LocalUserInsertForm::builder() .person_id(inserted_person.id) .password_encrypted("pass".to_string()) .build(); - let inserted_local_user = LocalUser::create(conn, &new_local_user).await.unwrap(); + let inserted_local_user = LocalUser::create(&mut *conn, &new_local_user) + .await + .unwrap(); let token = "nope"; let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce"; let inserted_password_reset_request = - PasswordResetRequest::create_token(conn, inserted_local_user.id, token) + PasswordResetRequest::create_token(&mut *conn, inserted_local_user.id, token) .await .unwrap(); @@ -153,11 +155,15 @@ mod tests { published: inserted_password_reset_request.published, }; - let read_password_reset_request = PasswordResetRequest::read_from_token(conn, token) + let read_password_reset_request = PasswordResetRequest::read_from_token(&mut *conn, token) + .await + .unwrap(); + let num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - let num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_password_reset_request, read_password_reset_request); assert_eq!( diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index ef68a66b1..bfa2cb682 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -19,32 +19,32 @@ impl Crud for Person { type InsertForm = PersonInsertForm; type UpdateForm = PersonUpdateForm; type IdType = PersonId; - async fn read(conn: &mut DbConn, person_id: PersonId) -> Result { + async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result { person::table .filter(person::deleted.eq(false)) .find(person_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, person_id: PersonId) -> Result { + async fn delete(mut conn: impl DbConn, person_id: PersonId) -> Result { diesel::delete(person::table.find(person_id)) - .execute(conn) + .execute(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &PersonInsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &PersonInsertForm) -> Result { insert_into(person::table) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, person_id: PersonId, form: &PersonUpdateForm, ) -> Result { diesel::update(person::table.find(person_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -53,23 +53,23 @@ impl Person { /// Update or insert the person. /// /// This is necessary for federation, because Activitypub doesnt distinguish between these actions. - pub async fn upsert(conn: &mut DbConn, form: &PersonInsertForm) -> Result { + pub async fn upsert(mut conn: impl DbConn, form: &PersonInsertForm) -> Result { insert_into(person::table) .values(form) .on_conflict(person::actor_id) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - pub async fn delete_account(conn: &mut DbConn, person_id: PersonId) -> Result { + pub async fn delete_account(mut conn: impl DbConn, person_id: PersonId) -> Result { // Set the local user info to none diesel::update(local_user::table.filter(local_user::person_id.eq(person_id))) .set(( local_user::email.eq::>(None), local_user::validator_time.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await?; diesel::update(person::table.find(person_id)) @@ -82,7 +82,7 @@ impl Person { person::deleted.eq(true), person::updated.eq(naive_now()), )) - .get_result::(conn) + .get_result::(&mut *conn) .await } } @@ -97,12 +97,15 @@ pub fn is_banned(banned_: bool, expires: Option) -> bool #[async_trait] impl ApubActor for Person { - async fn read_from_apub_id(conn: &mut DbConn, object_id: &DbUrl) -> Result, Error> { + async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: &DbUrl, + ) -> Result, Error> { Ok( person::table .filter(person::deleted.eq(false)) .filter(person::actor_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -110,7 +113,7 @@ impl ApubActor for Person { } async fn read_from_name( - conn: &mut DbConn, + mut conn: impl DbConn, from_name: &str, include_deleted: bool, ) -> Result { @@ -121,11 +124,11 @@ impl ApubActor for Person { if !include_deleted { q = q.filter(person::deleted.eq(false)) } - q.first::(conn).await + q.first::(&mut *conn).await } async fn read_from_name_and_domain( - conn: &mut DbConn, + mut conn: impl DbConn, person_name: &str, for_domain: &str, ) -> Result { @@ -134,7 +137,7 @@ impl ApubActor for Person { .filter(lower(person::name).eq(person_name.to_lowercase())) .filter(instance::domain.eq(for_domain)) .select(person::all_columns) - .first::(conn) + .first::(&mut *conn) .await } } @@ -142,41 +145,41 @@ impl ApubActor for Person { #[async_trait] impl Followable for PersonFollower { type Form = PersonFollowerForm; - async fn follow(conn: &mut DbConn, form: &PersonFollowerForm) -> Result { + async fn follow(mut conn: impl DbConn, form: &PersonFollowerForm) -> Result { use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id}; insert_into(person_follower) .values(form) .on_conflict((follower_id, person_id)) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn follow_accepted(_: &mut DbConn, _: CommunityId, _: PersonId) -> Result { + async fn follow_accepted(_: impl DbConn, _: CommunityId, _: PersonId) -> Result { unimplemented!() } - async fn unfollow(conn: &mut DbConn, form: &PersonFollowerForm) -> Result { + async fn unfollow(mut conn: impl DbConn, form: &PersonFollowerForm) -> Result { use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id}; diesel::delete( person_follower .filter(follower_id.eq(&form.follower_id)) .filter(person_id.eq(&form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } impl PersonFollower { pub async fn list_followers( - conn: &mut DbConn, + mut conn: impl DbConn, for_person_id: PersonId, ) -> Result, Error> { person_follower::table .inner_join(person::table.on(person_follower::follower_id.eq(person::id))) .filter(person_follower::person_id.eq(for_person_id)) .select(person::all_columns) - .load(conn) + .load(&mut *conn) .await } } @@ -196,9 +199,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -208,7 +211,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let expected_person = Person { id: inserted_person.id, @@ -235,17 +238,21 @@ mod tests { instance_id: inserted_instance.id, }; - let read_person = Person::read(conn, inserted_person.id).await.unwrap(); + let read_person = Person::read(&mut *conn, inserted_person.id).await.unwrap(); let update_person_form = PersonUpdateForm::builder() .actor_id(Some(inserted_person.actor_id.clone())) .build(); - let updated_person = Person::update(conn, inserted_person.id, &update_person_form) + let updated_person = Person::update(&mut *conn, inserted_person.id, &update_person_form) .await .unwrap(); - let num_deleted = Person::delete(conn, inserted_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + let num_deleted = Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); assert_eq!(expected_person, read_person); assert_eq!(expected_person, inserted_person); @@ -256,8 +263,8 @@ mod tests { #[tokio::test] #[serial] async fn follow() { - let conn = &mut build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let mut conn = build_db_conn_for_tests().await; + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -266,30 +273,34 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let person_1 = Person::create(conn, &person_form_1).await.unwrap(); + let person_1 = Person::create(&mut *conn, &person_form_1).await.unwrap(); let person_form_2 = PersonInsertForm::builder() .name("michele".into()) .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let person_2 = Person::create(conn, &person_form_2).await.unwrap(); + let person_2 = Person::create(&mut *conn, &person_form_2).await.unwrap(); let follow_form = PersonFollowerForm { person_id: person_1.id, follower_id: person_2.id, pending: false, }; - let person_follower = PersonFollower::follow(conn, &follow_form).await.unwrap(); + let person_follower = PersonFollower::follow(&mut *conn, &follow_form) + .await + .unwrap(); assert_eq!(person_1.id, person_follower.person_id); assert_eq!(person_2.id, person_follower.follower_id); assert!(!person_follower.pending); - let followers = PersonFollower::list_followers(conn, person_1.id) + let followers = PersonFollower::list_followers(&mut *conn, person_1.id) .await .unwrap(); assert_eq!(vec![person_2], followers); - let unfollow = PersonFollower::unfollow(conn, &follow_form).await.unwrap(); + let unfollow = PersonFollower::unfollow(&mut *conn, &follow_form) + .await + .unwrap(); assert_eq!(1, unfollow); } } diff --git a/crates/db_schema/src/impls/person_block.rs b/crates/db_schema/src/impls/person_block.rs index fda0ba7a5..83d72cc5c 100644 --- a/crates/db_schema/src/impls/person_block.rs +++ b/crates/db_schema/src/impls/person_block.rs @@ -10,14 +10,14 @@ use diesel_async::RunQueryDsl; impl PersonBlock { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, for_person_id: PersonId, for_recipient_id: PersonId, ) -> Result { person_block .filter(person_id.eq(for_person_id)) .filter(target_id.eq(for_recipient_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -25,22 +25,25 @@ impl PersonBlock { #[async_trait] impl Blockable for PersonBlock { type Form = PersonBlockForm; - async fn block(conn: &mut DbConn, person_block_form: &PersonBlockForm) -> Result { + async fn block( + mut conn: impl DbConn, + person_block_form: &PersonBlockForm, + ) -> Result { insert_into(person_block) .values(person_block_form) .on_conflict((person_id, target_id)) .do_update() .set(person_block_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn unblock(conn: &mut DbConn, person_block_form: &Self::Form) -> Result { + async fn unblock(mut conn: impl DbConn, person_block_form: &Self::Form) -> Result { diesel::delete( person_block .filter(person_id.eq(person_block_form.person_id)) .filter(target_id.eq(person_block_form.target_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/person_mention.rs b/crates/db_schema/src/impls/person_mention.rs index f0a7b1ec7..a6ec74426 100644 --- a/crates/db_schema/src/impls/person_mention.rs +++ b/crates/db_schema/src/impls/person_mention.rs @@ -13,15 +13,15 @@ impl Crud for PersonMention { type InsertForm = PersonMentionInsertForm; type UpdateForm = PersonMentionUpdateForm; type IdType = PersonMentionId; - async fn read(conn: &mut DbConn, person_mention_id: PersonMentionId) -> Result { + async fn read(mut conn: impl DbConn, person_mention_id: PersonMentionId) -> Result { person_mention .find(person_mention_id) - .first::(conn) + .first::(&mut *conn) .await } async fn create( - conn: &mut DbConn, + mut conn: impl DbConn, person_mention_form: &Self::InsertForm, ) -> Result { // since the return here isnt utilized, we dont need to do an update @@ -31,25 +31,25 @@ impl Crud for PersonMention { .on_conflict((recipient_id, comment_id)) .do_update() .set(person_mention_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, person_mention_id: PersonMentionId, person_mention_form: &Self::UpdateForm, ) -> Result { diesel::update(person_mention.find(person_mention_id)) .set(person_mention_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } } impl PersonMention { pub async fn mark_all_as_read( - conn: &mut DbConn, + mut conn: impl DbConn, for_recipient_id: PersonId, ) -> Result, Error> { diesel::update( @@ -58,19 +58,19 @@ impl PersonMention { .filter(read.eq(false)), ) .set(read.eq(true)) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn read_by_comment_and_person( - conn: &mut DbConn, + mut conn: impl DbConn, for_comment_id: CommentId, for_recipient_id: PersonId, ) -> Result { person_mention .filter(comment_id.eq(for_comment_id)) .filter(recipient_id.eq(for_recipient_id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -94,9 +94,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -106,7 +106,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let recipient_form = PersonInsertForm::builder() .name("terrylakes recipient".into()) @@ -114,7 +114,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap(); + let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community lake".to_string()) @@ -123,7 +123,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -131,7 +131,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment".into()) @@ -139,7 +139,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); let person_mention_form = PersonMentionInsertForm { recipient_id: inserted_recipient.id, @@ -147,7 +149,7 @@ mod tests { read: None, }; - let inserted_mention = PersonMention::create(conn, &person_mention_form) + let inserted_mention = PersonMention::create(&mut *conn, &person_mention_form) .await .unwrap(); @@ -159,23 +161,31 @@ mod tests { published: inserted_mention.published, }; - let read_mention = PersonMention::read(conn, inserted_mention.id) + let read_mention = PersonMention::read(&mut *conn, inserted_mention.id) .await .unwrap(); let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) }; let updated_mention = - PersonMention::update(conn, inserted_mention.id, &person_mention_update_form) + PersonMention::update(&mut *conn, inserted_mention.id, &person_mention_update_form) .await .unwrap(); - Comment::delete(conn, inserted_comment.id).await.unwrap(); - Post::delete(conn, inserted_post.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + Comment::delete(&mut *conn, inserted_comment.id) + .await + .unwrap(); + Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_recipient.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Person::delete(conn, inserted_recipient.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_mention, read_mention); assert_eq!(expected_mention, inserted_mention); diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 10b8f0e47..11af8eb14 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -38,39 +38,39 @@ impl Crud for Post { type InsertForm = PostInsertForm; type UpdateForm = PostUpdateForm; type IdType = PostId; - async fn read(conn: &mut DbConn, post_id: PostId) -> Result { - post.find(post_id).first::(conn).await + async fn read(mut conn: impl DbConn, post_id: PostId) -> Result { + post.find(post_id).first::(&mut *conn).await } - async fn delete(conn: &mut DbConn, post_id: PostId) -> Result { - diesel::delete(post.find(post_id)).execute(conn).await + async fn delete(mut conn: impl DbConn, post_id: PostId) -> Result { + diesel::delete(post.find(post_id)).execute(&mut *conn).await } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { insert_into(post) .values(form) .on_conflict(ap_id) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, post_id: PostId, new_post: &Self::UpdateForm, ) -> Result { diesel::update(post.find(post_id)) .set(new_post) - .get_result::(conn) + .get_result::(&mut *conn) .await } } impl Post { pub async fn list_for_community( - conn: &mut DbConn, + mut conn: impl DbConn, the_community_id: CommunityId, ) -> Result, Error> { post @@ -80,12 +80,12 @@ impl Post { .then_order_by(featured_community.desc()) .then_order_by(published.desc()) .limit(FETCH_LIMIT_MAX) - .load::(conn) + .load::(&mut *conn) .await } pub async fn list_featured_for_community( - conn: &mut DbConn, + mut conn: impl DbConn, the_community_id: CommunityId, ) -> Result, Error> { post @@ -95,12 +95,12 @@ impl Post { .filter(featured_community.eq(true)) .then_order_by(published.desc()) .limit(FETCH_LIMIT_MAX) - .load::(conn) + .load::(&mut *conn) .await } pub async fn permadelete_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, ) -> Result, Error> { let _perma_deleted = "*Permananently Deleted*"; @@ -114,12 +114,12 @@ impl Post { deleted.eq(true), updated.eq(naive_now()), )) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn update_removed_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, for_community_id: Option, new_removed: bool, @@ -133,7 +133,7 @@ impl Post { update .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_results::(conn) + .get_results::(&mut *conn) .await } @@ -141,12 +141,15 @@ impl Post { person_id == post_creator_id } - pub async fn read_from_apub_id(conn: &mut DbConn, object_id: Url) -> Result, Error> { + pub async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: Url, + ) -> Result, Error> { let object_id: DbUrl = object_id.into(); Ok( post .filter(ap_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -154,7 +157,7 @@ impl Post { } pub async fn fetch_pictrs_posts_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, ) -> Result, Error> { let pictrs_search = "%pictrs/image%"; @@ -162,13 +165,13 @@ impl Post { post .filter(creator_id.eq(for_creator_id)) .filter(url.like(pictrs_search)) - .load::(conn) + .load::(&mut *conn) .await } /// Sets the url and thumbnails fields to None pub async fn remove_pictrs_post_images_and_thumbnails_for_creator( - conn: &mut DbConn, + mut conn: impl DbConn, for_creator_id: PersonId, ) -> Result, Error> { let pictrs_search = "%pictrs/image%"; @@ -182,25 +185,25 @@ impl Post { url.eq::>(None), thumbnail_url.eq::>(None), )) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn fetch_pictrs_posts_for_community( - conn: &mut DbConn, + mut conn: impl DbConn, for_community_id: CommunityId, ) -> Result, Error> { let pictrs_search = "%pictrs/image%"; post .filter(community_id.eq(for_community_id)) .filter(url.like(pictrs_search)) - .load::(conn) + .load::(&mut *conn) .await } /// Sets the url and thumbnails fields to None pub async fn remove_pictrs_post_images_and_thumbnails_for_community( - conn: &mut DbConn, + mut conn: impl DbConn, for_community_id: CommunityId, ) -> Result, Error> { let pictrs_search = "%pictrs/image%"; @@ -214,7 +217,7 @@ impl Post { url.eq::>(None), thumbnail_url.eq::>(None), )) - .get_results::(conn) + .get_results::(&mut *conn) .await } } @@ -223,24 +226,28 @@ impl Post { impl Likeable for PostLike { type Form = PostLikeForm; type IdType = PostId; - async fn like(conn: &mut DbConn, post_like_form: &PostLikeForm) -> Result { + async fn like(mut conn: impl DbConn, post_like_form: &PostLikeForm) -> Result { use crate::schema::post_like::dsl::{person_id, post_id, post_like}; insert_into(post_like) .values(post_like_form) .on_conflict((post_id, person_id)) .do_update() .set(post_like_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn remove(conn: &mut DbConn, person_id: PersonId, post_id: PostId) -> Result { + async fn remove( + mut conn: impl DbConn, + person_id: PersonId, + post_id: PostId, + ) -> Result { use crate::schema::post_like::dsl; diesel::delete( dsl::post_like .filter(dsl::post_id.eq(post_id)) .filter(dsl::person_id.eq(person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -248,24 +255,24 @@ impl Likeable for PostLike { #[async_trait] impl Saveable for PostSaved { type Form = PostSavedForm; - async fn save(conn: &mut DbConn, post_saved_form: &PostSavedForm) -> Result { + async fn save(mut conn: impl DbConn, post_saved_form: &PostSavedForm) -> Result { use crate::schema::post_saved::dsl::{person_id, post_id, post_saved}; insert_into(post_saved) .values(post_saved_form) .on_conflict((post_id, person_id)) .do_update() .set(post_saved_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn unsave(conn: &mut DbConn, post_saved_form: &PostSavedForm) -> Result { + async fn unsave(mut conn: impl DbConn, post_saved_form: &PostSavedForm) -> Result { use crate::schema::post_saved::dsl::{person_id, post_id, post_saved}; diesel::delete( post_saved .filter(post_id.eq(post_saved_form.post_id)) .filter(person_id.eq(post_saved_form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -273,19 +280,22 @@ impl Saveable for PostSaved { #[async_trait] impl Readable for PostRead { type Form = PostReadForm; - async fn mark_as_read(conn: &mut DbConn, post_read_form: &PostReadForm) -> Result { + async fn mark_as_read( + mut conn: impl DbConn, + post_read_form: &PostReadForm, + ) -> Result { use crate::schema::post_read::dsl::{person_id, post_id, post_read}; insert_into(post_read) .values(post_read_form) .on_conflict((post_id, person_id)) .do_update() .set(post_read_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn mark_as_unread( - conn: &mut DbConn, + mut conn: impl DbConn, post_read_form: &PostReadForm, ) -> Result { use crate::schema::post_read::dsl::{person_id, post_id, post_read}; @@ -294,7 +304,7 @@ impl Readable for PostRead { .filter(post_id.eq(post_read_form.post_id)) .filter(person_id.eq(post_read_form.person_id)), ) - .execute(conn) + .execute(&mut *conn) .await } } @@ -326,9 +336,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -338,7 +348,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community_3".to_string()) @@ -347,7 +357,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post".into()) @@ -355,7 +365,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let expected_post = Post { id: inserted_post.id, @@ -388,7 +398,7 @@ mod tests { score: 1, }; - let inserted_post_like = PostLike::like(conn, &post_like_form).await.unwrap(); + let inserted_post_like = PostLike::like(&mut *conn, &post_like_form).await.unwrap(); let expected_post_like = PostLike { id: inserted_post_like.id, @@ -404,7 +414,7 @@ mod tests { person_id: inserted_person.id, }; - let inserted_post_saved = PostSaved::save(conn, &post_saved_form).await.unwrap(); + let inserted_post_saved = PostSaved::save(&mut *conn, &post_saved_form).await.unwrap(); let expected_post_saved = PostSaved { id: inserted_post_saved.id, @@ -419,7 +429,9 @@ mod tests { person_id: inserted_person.id, }; - let inserted_post_read = PostRead::mark_as_read(conn, &post_read_form).await.unwrap(); + let inserted_post_read = PostRead::mark_as_read(&mut *conn, &post_read_form) + .await + .unwrap(); let expected_post_read = PostRead { id: inserted_post_read.id, @@ -428,28 +440,34 @@ mod tests { published: inserted_post_read.published, }; - let read_post = Post::read(conn, inserted_post.id).await.unwrap(); + let read_post = Post::read(&mut *conn, inserted_post.id).await.unwrap(); let new_post_update = PostUpdateForm::builder() .name(Some("A test post".into())) .build(); - let updated_post = Post::update(conn, inserted_post.id, &new_post_update) + let updated_post = Post::update(&mut *conn, inserted_post.id, &new_post_update) .await .unwrap(); - let like_removed = PostLike::remove(conn, inserted_person.id, inserted_post.id) + let like_removed = PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id) .await .unwrap(); - let saved_removed = PostSaved::unsave(conn, &post_saved_form).await.unwrap(); - let read_removed = PostRead::mark_as_unread(conn, &post_read_form) + let saved_removed = PostSaved::unsave(&mut *conn, &post_saved_form) .await .unwrap(); - let num_deleted = Post::delete(conn, inserted_post.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + let read_removed = PostRead::mark_as_unread(&mut *conn, &post_read_form) + .await + .unwrap(); + let num_deleted = Post::delete(&mut *conn, inserted_post.id).await.unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); assert_eq!(expected_post, read_post); assert_eq!(expected_post, inserted_post); diff --git a/crates/db_schema/src/impls/post_report.rs b/crates/db_schema/src/impls/post_report.rs index 025953fee..6095be934 100644 --- a/crates/db_schema/src/impls/post_report.rs +++ b/crates/db_schema/src/impls/post_report.rs @@ -18,15 +18,15 @@ impl Reportable for PostReport { type Form = PostReportForm; type IdType = PostReportId; - async fn report(conn: &mut DbConn, post_report_form: &PostReportForm) -> Result { + async fn report(mut conn: impl DbConn, post_report_form: &PostReportForm) -> Result { insert_into(post_report) .values(post_report_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn resolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -36,12 +36,12 @@ impl Reportable for PostReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } async fn unresolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -51,7 +51,7 @@ impl Reportable for PostReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/private_message.rs b/crates/db_schema/src/impls/private_message.rs index 2fa98707b..7cf0bb4ad 100644 --- a/crates/db_schema/src/impls/private_message.rs +++ b/crates/db_schema/src/impls/private_message.rs @@ -15,43 +15,46 @@ impl Crud for PrivateMessage { type InsertForm = PrivateMessageInsertForm; type UpdateForm = PrivateMessageUpdateForm; type IdType = PrivateMessageId; - async fn read(conn: &mut DbConn, private_message_id: PrivateMessageId) -> Result { + async fn read( + mut conn: impl DbConn, + private_message_id: PrivateMessageId, + ) -> Result { private_message .find(private_message_id) - .first::(conn) + .first::(&mut *conn) .await } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { insert_into(private_message) .values(form) .on_conflict(ap_id) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, private_message_id: PrivateMessageId, form: &Self::UpdateForm, ) -> Result { diesel::update(private_message.find(private_message_id)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, pm_id: Self::IdType) -> Result { + async fn delete(mut conn: impl DbConn, pm_id: Self::IdType) -> Result { diesel::delete(private_message.find(pm_id)) - .execute(conn) + .execute(&mut *conn) .await } } impl PrivateMessage { pub async fn mark_all_as_read( - conn: &mut DbConn, + mut conn: impl DbConn, for_recipient_id: PersonId, ) -> Result, Error> { diesel::update( @@ -60,19 +63,19 @@ impl PrivateMessage { .filter(read.eq(false)), ) .set(read.eq(true)) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn read_from_apub_id( - conn: &mut DbConn, + mut conn: impl DbConn, object_id: Url, ) -> Result, LemmyError> { let object_id: DbUrl = object_id.into(); Ok( private_message .filter(ap_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -96,9 +99,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -108,7 +111,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_creator = Person::create(conn, &creator_form).await.unwrap(); + let inserted_creator = Person::create(&mut *conn, &creator_form).await.unwrap(); let recipient_form = PersonInsertForm::builder() .name("recipient_pm".into()) @@ -116,7 +119,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap(); + let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap(); let private_message_form = PrivateMessageInsertForm::builder() .content("A test private message".into()) @@ -124,7 +127,7 @@ mod tests { .recipient_id(inserted_recipient.id) .build(); - let inserted_private_message = PrivateMessage::create(conn, &private_message_form) + let inserted_private_message = PrivateMessage::create(&mut *conn, &private_message_form) .await .unwrap(); @@ -141,7 +144,7 @@ mod tests { local: true, }; - let read_private_message = PrivateMessage::read(conn, inserted_private_message.id) + let read_private_message = PrivateMessage::read(&mut *conn, inserted_private_message.id) .await .unwrap(); @@ -149,7 +152,7 @@ mod tests { .content(Some("A test private message".into())) .build(); let updated_private_message = PrivateMessage::update( - conn, + &mut *conn, inserted_private_message.id, &private_message_update_form, ) @@ -157,7 +160,7 @@ mod tests { .unwrap(); let deleted_private_message = PrivateMessage::update( - conn, + &mut *conn, inserted_private_message.id, &PrivateMessageUpdateForm::builder() .deleted(Some(true)) @@ -166,15 +169,21 @@ mod tests { .await .unwrap(); let marked_read_private_message = PrivateMessage::update( - conn, + &mut *conn, inserted_private_message.id, &PrivateMessageUpdateForm::builder().read(Some(true)).build(), ) .await .unwrap(); - Person::delete(conn, inserted_creator.id).await.unwrap(); - Person::delete(conn, inserted_recipient.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Person::delete(&mut *conn, inserted_creator.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_recipient.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); assert_eq!(expected_private_message, read_private_message); assert_eq!(expected_private_message, updated_private_message); diff --git a/crates/db_schema/src/impls/private_message_report.rs b/crates/db_schema/src/impls/private_message_report.rs index 8733cf9ed..e4b8880a7 100644 --- a/crates/db_schema/src/impls/private_message_report.rs +++ b/crates/db_schema/src/impls/private_message_report.rs @@ -19,17 +19,17 @@ impl Reportable for PrivateMessageReport { type IdType = PrivateMessageReportId; async fn report( - conn: &mut DbConn, + mut conn: impl DbConn, pm_report_form: &PrivateMessageReportForm, ) -> Result { insert_into(private_message_report) .values(pm_report_form) - .get_result::(conn) + .get_result::(&mut *conn) .await } async fn resolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -39,12 +39,12 @@ impl Reportable for PrivateMessageReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } async fn unresolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -54,7 +54,7 @@ impl Reportable for PrivateMessageReport { resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) - .execute(conn) + .execute(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/registration_application.rs b/crates/db_schema/src/impls/registration_application.rs index 47f531efe..96ccb27a9 100644 --- a/crates/db_schema/src/impls/registration_application.rs +++ b/crates/db_schema/src/impls/registration_application.rs @@ -18,43 +18,46 @@ impl Crud for RegistrationApplication { type UpdateForm = RegistrationApplicationUpdateForm; type IdType = i32; - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { insert_into(registration_application) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn read(conn: &mut DbConn, id_: Self::IdType) -> Result { - registration_application.find(id_).first::(conn).await + async fn read(mut conn: impl DbConn, id_: Self::IdType) -> Result { + registration_application + .find(id_) + .first::(&mut *conn) + .await } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, id_: Self::IdType, form: &Self::UpdateForm, ) -> Result { diesel::update(registration_application.find(id_)) .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, id_: Self::IdType) -> Result { + async fn delete(mut conn: impl DbConn, id_: Self::IdType) -> Result { diesel::delete(registration_application.find(id_)) - .execute(conn) + .execute(&mut *conn) .await } } impl RegistrationApplication { pub async fn find_by_local_user_id( - conn: &mut DbConn, + mut conn: impl DbConn, local_user_id_: LocalUserId, ) -> Result { registration_application .filter(local_user_id.eq(local_user_id_)) - .first::(conn) + .first::(&mut *conn) .await } } diff --git a/crates/db_schema/src/impls/secret.rs b/crates/db_schema/src/impls/secret.rs index 0dc5b68c3..174e9c881 100644 --- a/crates/db_schema/src/impls/secret.rs +++ b/crates/db_schema/src/impls/secret.rs @@ -5,11 +5,11 @@ use diesel_async::RunQueryDsl; impl Secret { /// Initialize the Secrets from the DB. /// Warning: You should only call this once. - pub async fn init(conn: &mut DbConn) -> Result { - Self::read_secrets(conn).await + pub async fn init(mut conn: impl DbConn) -> Result { + Self::read_secrets(&mut *conn).await } - async fn read_secrets(conn: &mut DbConn) -> Result { - secret.first::(conn).await + async fn read_secrets(mut conn: impl DbConn) -> Result { + secret.first::(&mut *conn).await } } diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index d82bad77a..495f1d5e3 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -19,13 +19,13 @@ impl Crud for Site { type IdType = SiteId; /// Use SiteView::read_local, or Site::read_from_apub_id instead - async fn read(_conn: &mut DbConn, _site_id: SiteId) -> Result { + async fn read(_conn: impl DbConn, _site_id: SiteId) -> Result { unimplemented!() } - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result { + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result { let is_new_site = match &form.actor_id { - Some(id_) => Site::read_from_apub_id(conn, id_).await?.is_none(), + Some(id_) => Site::read_from_apub_id(&mut *conn, id_).await?.is_none(), None => true, }; @@ -35,42 +35,42 @@ impl Crud for Site { .on_conflict(actor_id) .do_update() .set(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; // initialize languages if site is newly created if is_new_site { // initialize with all languages - SiteLanguage::update(conn, vec![], &site_).await?; + SiteLanguage::update(&mut *conn, vec![], &site_).await?; } Ok(site_) } async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, site_id: SiteId, new_site: &Self::UpdateForm, ) -> Result { diesel::update(site.find(site_id)) .set(new_site) - .get_result::(conn) + .get_result::(&mut *conn) .await } - async fn delete(conn: &mut DbConn, site_id: SiteId) -> Result { - diesel::delete(site.find(site_id)).execute(conn).await + async fn delete(mut conn: impl DbConn, site_id: SiteId) -> Result { + diesel::delete(site.find(site_id)).execute(&mut *conn).await } } impl Site { pub async fn read_from_apub_id( - conn: &mut DbConn, + mut conn: impl DbConn, object_id: &DbUrl, ) -> Result, Error> { Ok( site .filter(actor_id.eq(object_id)) - .first::(conn) + .first::(&mut *conn) .await .ok() .map(Into::into), @@ -78,8 +78,12 @@ impl Site { } // TODO this needs fixed - pub async fn read_remote_sites(conn: &mut DbConn) -> Result, Error> { - site.order_by(id).offset(1).get_results::(conn).await + pub async fn read_remote_sites(mut conn: impl DbConn) -> Result, Error> { + site + .order_by(id) + .offset(1) + .get_results::(&mut *conn) + .await } /// Instance actor is at the root path, so we simply need to clear the path and other unnecessary diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 1c83e3f85..c29ca9b34 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -5,11 +5,11 @@ use crate::{ utils::DbConn, }; use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl Tagline { pub async fn replace( - conn: &mut DbConn, + mut conn: impl DbConn, for_local_site_id: LocalSiteId, list_content: Option>, ) -> Result, Error> { @@ -18,7 +18,7 @@ impl Tagline { .build_transaction() .run(|conn| { Box::pin(async move { - Self::clear(conn).await?; + Self::clear(&mut *conn).await?; for item in list { let form = TaglineForm { @@ -28,35 +28,35 @@ impl Tagline { }; insert_into(tagline) .values(form) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } - Self::get_all_conn(conn, for_local_site_id).await + Self::get_all_conn(&mut *conn, for_local_site_id).await }) as _ }) .await } else { - Self::get_all_conn(conn, for_local_site_id).await + Self::get_all_conn(&mut *conn, for_local_site_id).await } } - async fn clear(conn: &mut AsyncPgConnection) -> Result { - diesel::delete(tagline).execute(conn).await + async fn clear(mut conn: impl DbConn) -> Result { + diesel::delete(tagline).execute(&mut *conn).await } async fn get_all_conn( - conn: &mut AsyncPgConnection, + mut conn: impl DbConn, for_local_site_id: LocalSiteId, ) -> Result, Error> { tagline .filter(local_site_id.eq(for_local_site_id)) - .get_results::(conn) + .get_results::(&mut *conn) .await } pub async fn get_all( - conn: &mut DbConn, + mut conn: impl DbConn, for_local_site_id: LocalSiteId, ) -> Result, Error> { - Self::get_all_conn(conn, for_local_site_id).await + Self::get_all_conn(&mut *conn, for_local_site_id).await } } diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 09117742c..0aab15e57 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -9,21 +9,21 @@ pub trait Crud { type InsertForm; type UpdateForm; type IdType; - async fn create(conn: &mut DbConn, form: &Self::InsertForm) -> Result + async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result where Self: Sized; - async fn read(conn: &mut DbConn, id: Self::IdType) -> Result + async fn read(mut conn: impl DbConn, id: Self::IdType) -> Result where Self: Sized; /// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. async fn update( - conn: &mut DbConn, + mut conn: impl DbConn, id: Self::IdType, form: &Self::UpdateForm, ) -> Result where Self: Sized; - async fn delete(_conn: &mut DbConn, _id: Self::IdType) -> Result + async fn delete(_conn: impl DbConn, _id: Self::IdType) -> Result where Self: Sized, Self::IdType: Send, @@ -35,17 +35,17 @@ pub trait Crud { #[async_trait] pub trait Followable { type Form; - async fn follow(conn: &mut DbConn, form: &Self::Form) -> Result + async fn follow(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; async fn follow_accepted( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, person_id: PersonId, ) -> Result where Self: Sized; - async fn unfollow(conn: &mut DbConn, form: &Self::Form) -> Result + async fn unfollow(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -53,10 +53,10 @@ pub trait Followable { #[async_trait] pub trait Joinable { type Form; - async fn join(conn: &mut DbConn, form: &Self::Form) -> Result + async fn join(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; - async fn leave(conn: &mut DbConn, form: &Self::Form) -> Result + async fn leave(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -65,11 +65,11 @@ pub trait Joinable { pub trait Likeable { type Form; type IdType; - async fn like(conn: &mut DbConn, form: &Self::Form) -> Result + async fn like(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; async fn remove( - conn: &mut DbConn, + mut conn: impl DbConn, person_id: PersonId, item_id: Self::IdType, ) -> Result @@ -80,10 +80,10 @@ pub trait Likeable { #[async_trait] pub trait Bannable { type Form; - async fn ban(conn: &mut DbConn, form: &Self::Form) -> Result + async fn ban(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; - async fn unban(conn: &mut DbConn, form: &Self::Form) -> Result + async fn unban(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -91,10 +91,10 @@ pub trait Bannable { #[async_trait] pub trait Saveable { type Form; - async fn save(conn: &mut DbConn, form: &Self::Form) -> Result + async fn save(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; - async fn unsave(conn: &mut DbConn, form: &Self::Form) -> Result + async fn unsave(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -102,10 +102,10 @@ pub trait Saveable { #[async_trait] pub trait Blockable { type Form; - async fn block(conn: &mut DbConn, form: &Self::Form) -> Result + async fn block(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; - async fn unblock(conn: &mut DbConn, form: &Self::Form) -> Result + async fn unblock(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -113,10 +113,10 @@ pub trait Blockable { #[async_trait] pub trait Readable { type Form; - async fn mark_as_read(conn: &mut DbConn, form: &Self::Form) -> Result + async fn mark_as_read(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; - async fn mark_as_unread(conn: &mut DbConn, form: &Self::Form) -> Result + async fn mark_as_unread(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; } @@ -125,18 +125,18 @@ pub trait Readable { pub trait Reportable { type Form; type IdType; - async fn report(conn: &mut DbConn, form: &Self::Form) -> Result + async fn report(mut conn: impl DbConn, form: &Self::Form) -> Result where Self: Sized; async fn resolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, resolver_id: PersonId, ) -> Result where Self: Sized; async fn unresolve( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: Self::IdType, resolver_id: PersonId, ) -> Result @@ -153,20 +153,23 @@ pub trait JoinView { #[async_trait] pub trait ApubActor { - async fn read_from_apub_id(conn: &mut DbConn, object_id: &DbUrl) -> Result, Error> + async fn read_from_apub_id( + mut conn: impl DbConn, + object_id: &DbUrl, + ) -> Result, Error> where Self: Sized; /// - actor_name is the name of the community or user to read. /// - include_deleted, if true, will return communities or users that were deleted/removed async fn read_from_name( - conn: &mut DbConn, + mut conn: impl DbConn, actor_name: &str, include_deleted: bool, ) -> Result where Self: Sized; async fn read_from_name_and_domain( - conn: &mut DbConn, + mut conn: impl DbConn, actor_name: &str, protocol_domain: &str, ) -> Result diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index f344df6a8..9dcfb3751 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -23,6 +23,7 @@ use diesel_async::{ deadpool::{Object as PooledConnection, Pool}, AsyncDieselConnectionManager, }, + AsyncConnection, }; use diesel_migrations::EmbeddedMigrations; use futures_util::{future::BoxFuture, FutureExt}; @@ -47,9 +48,11 @@ pub const FETCH_LIMIT_MAX: i64 = 50; const POOL_TIMEOUT: Option = Some(Duration::from_secs(5)); pub type DbPool = Pool; -pub type DbConn = AsyncPgConnection; pub type DbPooledConn = PooledConnection; +pub trait DbConn: std::ops::DerefMut + Send {} +impl + Send> DbConn for T {} + pub async fn get_conn(pool: &DbPool) -> Result, DieselError> { pool.get().await.map_err(|e| QueryBuilderError(e.into())) } @@ -218,7 +221,7 @@ pub fn run_migrations(db_url: &str) { let mut conn = PgConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {db_url}: {e}")); info!("Running Database migrations (This may take a long time)..."); - let _ = &mut conn + let _ = &mut *conn .run_pending_migrations(MIGRATIONS) .unwrap_or_else(|e| panic!("Couldn't run DB Migrations: {e}")); info!("Database migrations complete."); diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 443dab042..af242ee88 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -40,7 +40,7 @@ impl CommentReportView { /// /// * `report_id` - the report id to obtain pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: CommentReportId, my_person_id: PersonId, ) -> Result { @@ -86,7 +86,7 @@ impl CommentReportView { comment_like::score.nullable(), person_alias_2.fields(person::all_columns).nullable(), )) - .first::<::JoinTuple>(conn) + .first::<::JoinTuple>(&mut *conn) .await?; Ok(Self::from_tuple(res)) @@ -94,7 +94,7 @@ impl CommentReportView { /// Returns the current unresolved post report count for the communities you mod pub async fn get_report_count( - conn: &mut DbConn, + mut conn: impl DbConn, my_person_id: PersonId, admin: bool, community_id: Option, @@ -122,12 +122,12 @@ impl CommentReportView { ), ) .select(count(comment_report::id)) - .first::(conn) + .first::(&mut *conn) .await } else { query .select(count(comment_report::id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -135,9 +135,9 @@ impl CommentReportView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentReportQuery<'a> { +pub struct CommentReportQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, #[builder(!default)] my_person_id: PersonId, #[builder(!default)] @@ -148,9 +148,9 @@ pub struct CommentReportQuery<'a> { unresolved_only: Option, } -impl<'a> CommentReportQuery<'a> { +impl CommentReportQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); @@ -225,11 +225,11 @@ impl<'a> CommentReportQuery<'a> { .and(community_moderator::person_id.eq(self.my_person_id)), ), ) - .load::<::JoinTuple>(conn) + .load::<::JoinTuple>(&mut *conn) .await? } else { query - .load::<::JoinTuple>(conn) + .load::<::JoinTuple>(&mut *conn) .await? }; @@ -288,9 +288,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -300,7 +300,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_timmy = Person::create(conn, &new_person).await.unwrap(); + let inserted_timmy = Person::create(&mut *conn, &new_person).await.unwrap(); let new_person_2 = PersonInsertForm::builder() .name("sara_crv".into()) @@ -308,7 +308,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_sara = Person::create(conn, &new_person_2).await.unwrap(); + let inserted_sara = Person::create(&mut *conn, &new_person_2).await.unwrap(); // Add a third person, since new ppl can only report something once. let new_person_3 = PersonInsertForm::builder() @@ -317,7 +317,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_jessica = Person::create(conn, &new_person_3).await.unwrap(); + let inserted_jessica = Person::create(&mut *conn, &new_person_3).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community crv".to_string()) @@ -326,7 +326,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); // Make timmy a mod let timmy_moderator_form = CommunityModeratorForm { @@ -334,7 +334,7 @@ mod tests { person_id: inserted_timmy.id, }; - let _inserted_moderator = CommunityModerator::join(conn, &timmy_moderator_form) + let _inserted_moderator = CommunityModerator::join(&mut *conn, &timmy_moderator_form) .await .unwrap(); @@ -344,7 +344,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let comment_form = CommentInsertForm::builder() .content("A test comment 32".into()) @@ -352,7 +352,9 @@ mod tests { .post_id(inserted_post.id) .build(); - let inserted_comment = Comment::create(conn, &comment_form, None).await.unwrap(); + let inserted_comment = Comment::create(&mut *conn, &comment_form, None) + .await + .unwrap(); // sara reports let sara_report_form = CommentReportForm { @@ -362,7 +364,7 @@ mod tests { reason: "from sara".into(), }; - let inserted_sara_report = CommentReport::report(conn, &sara_report_form) + let inserted_sara_report = CommentReport::report(&mut *conn, &sara_report_form) .await .unwrap(); @@ -374,16 +376,16 @@ mod tests { reason: "from jessica".into(), }; - let inserted_jessica_report = CommentReport::report(conn, &jessica_report_form) + let inserted_jessica_report = CommentReport::report(&mut *conn, &jessica_report_form) .await .unwrap(); - let agg = CommentAggregates::read(conn, inserted_comment.id) + let agg = CommentAggregates::read(&mut *conn, inserted_comment.id) .await .unwrap(); let read_jessica_report_view = - CommentReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id) + CommentReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); let expected_jessica_report_view = CommentReportView { @@ -510,7 +512,7 @@ mod tests { // Do a batch read of timmys reports let reports = CommentReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .my_person_id(inserted_timmy.id) .admin(false) .build() @@ -527,17 +529,18 @@ mod tests { ); // Make sure the counts are correct - let report_count = CommentReportView::get_report_count(conn, inserted_timmy.id, false, None) - .await - .unwrap(); + let report_count = + CommentReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None) + .await + .unwrap(); assert_eq!(2, report_count); // Try to resolve the report - CommentReport::resolve(conn, inserted_jessica_report.id, inserted_timmy.id) + CommentReport::resolve(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); let read_jessica_report_view_after_resolve = - CommentReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id) + CommentReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); @@ -586,7 +589,7 @@ mod tests { // Do a batch read of timmys reports // It should only show saras, which is unresolved let reports_after_resolve = CommentReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .my_person_id(inserted_timmy.id) .admin(false) .unresolved_only(Some(true)) @@ -599,17 +602,21 @@ mod tests { // Make sure the counts are correct let report_count_after_resolved = - CommentReportView::get_report_count(conn, inserted_timmy.id, false, None) + CommentReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None) .await .unwrap(); assert_eq!(1, report_count_after_resolved); - Person::delete(conn, inserted_timmy.id).await.unwrap(); - Person::delete(conn, inserted_sara.id).await.unwrap(); - Person::delete(conn, inserted_jessica.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + Person::delete(&mut *conn, inserted_timmy.id).await.unwrap(); + Person::delete(&mut *conn, inserted_sara.id).await.unwrap(); + Person::delete(&mut *conn, inserted_jessica.id) + .await + .unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); } } diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index dcc114f07..c01c3ecb6 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -57,7 +57,7 @@ type CommentViewTuple = ( impl CommentView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, comment_id: CommentId, my_person_id: Option, ) -> Result { @@ -128,7 +128,7 @@ impl CommentView { person_block::all_columns.nullable(), comment_like::score.nullable(), )) - .first::(conn) + .first::(&mut *conn) .await?; // If a person is given, then my_vote, if None, should be 0, not null @@ -156,9 +156,9 @@ impl CommentView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentQuery<'a> { +pub struct CommentQuery<'a, Conn> { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, listing_type: Option, sort: Option, community_id: Option, @@ -174,9 +174,9 @@ pub struct CommentQuery<'a> { max_depth: Option, } -impl<'a> CommentQuery<'a> { +impl<'a, Conn: DbConn> CommentQuery<'a, Conn> { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; // The left join below will return None in this case let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); @@ -354,7 +354,7 @@ impl<'a> CommentQuery<'a> { let res = query .limit(limit) .offset(offset) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(CommentView::from_tuple).collect()) @@ -425,8 +425,8 @@ mod tests { inserted_community: Community, } - async fn init_data(conn: &mut DbConn) -> Data { - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + async fn init_data(mut conn: impl DbConn) -> Data { + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -435,19 +435,21 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let local_user_form = LocalUserInsertForm::builder() .person_id(inserted_person.id) .password_encrypted(String::new()) .build(); - let inserted_local_user = LocalUser::create(conn, &local_user_form).await.unwrap(); + let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form) + .await + .unwrap(); let new_person_2 = PersonInsertForm::builder() .name("sara".into()) .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let inserted_person_2 = Person::create(conn, &new_person_2).await.unwrap(); + let inserted_person_2 = Person::create(&mut *conn, &new_person_2).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community 5".to_string()) @@ -456,7 +458,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); let new_post = PostInsertForm::builder() .name("A test post 2".into()) @@ -464,8 +466,10 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); - let english_id = Language::read_id_from_code(conn, Some("en")).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); + let english_id = Language::read_id_from_code(&mut *conn, Some("en")) + .await + .unwrap(); // Create a comment tree with this hierarchy // 0 @@ -482,7 +486,9 @@ mod tests { .language_id(english_id) .build(); - let inserted_comment_0 = Comment::create(conn, &comment_form_0, None).await.unwrap(); + let inserted_comment_0 = Comment::create(&mut *conn, &comment_form_0, None) + .await + .unwrap(); let comment_form_1 = CommentInsertForm::builder() .content("Comment 1, A test blocked comment".into()) @@ -491,11 +497,14 @@ mod tests { .language_id(english_id) .build(); - let inserted_comment_1 = Comment::create(conn, &comment_form_1, Some(&inserted_comment_0.path)) + let inserted_comment_1 = + Comment::create(&mut *conn, &comment_form_1, Some(&inserted_comment_0.path)) + .await + .unwrap(); + + let finnish_id = Language::read_id_from_code(&mut *conn, Some("fi")) .await .unwrap(); - - let finnish_id = Language::read_id_from_code(conn, Some("fi")).await.unwrap(); let comment_form_2 = CommentInsertForm::builder() .content("Comment 2".into()) .creator_id(inserted_person.id) @@ -503,9 +512,10 @@ mod tests { .language_id(finnish_id) .build(); - let inserted_comment_2 = Comment::create(conn, &comment_form_2, Some(&inserted_comment_0.path)) - .await - .unwrap(); + let inserted_comment_2 = + Comment::create(&mut *conn, &comment_form_2, Some(&inserted_comment_0.path)) + .await + .unwrap(); let comment_form_3 = CommentInsertForm::builder() .content("Comment 3".into()) @@ -515,11 +525,11 @@ mod tests { .build(); let _inserted_comment_3 = - Comment::create(conn, &comment_form_3, Some(&inserted_comment_1.path)) + Comment::create(&mut *conn, &comment_form_3, Some(&inserted_comment_1.path)) .await .unwrap(); - let polish_id = Language::read_id_from_code(conn, Some("pl")) + let polish_id = Language::read_id_from_code(&mut *conn, Some("pl")) .await .unwrap() .unwrap(); @@ -530,9 +540,10 @@ mod tests { .language_id(Some(polish_id)) .build(); - let inserted_comment_4 = Comment::create(conn, &comment_form_4, Some(&inserted_comment_1.path)) - .await - .unwrap(); + let inserted_comment_4 = + Comment::create(&mut *conn, &comment_form_4, Some(&inserted_comment_1.path)) + .await + .unwrap(); let comment_form_5 = CommentInsertForm::builder() .content("Comment 5".into()) @@ -541,7 +552,7 @@ mod tests { .build(); let _inserted_comment_5 = - Comment::create(conn, &comment_form_5, Some(&inserted_comment_4.path)) + Comment::create(&mut *conn, &comment_form_5, Some(&inserted_comment_4.path)) .await .unwrap(); @@ -550,7 +561,7 @@ mod tests { target_id: inserted_person_2.id, }; - let inserted_block = PersonBlock::block(conn, &timmy_blocks_sara_form) + let inserted_block = PersonBlock::block(&mut *conn, &timmy_blocks_sara_form) .await .unwrap(); @@ -569,7 +580,9 @@ mod tests { score: 1, }; - let _inserted_comment_like = CommentLike::like(conn, &comment_like_form).await.unwrap(); + let _inserted_comment_like = CommentLike::like(&mut *conn, &comment_like_form) + .await + .unwrap(); Data { inserted_instance, @@ -587,16 +600,16 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; - let expected_comment_view_no_person = expected_comment_view(&data, conn).await; + let expected_comment_view_no_person = expected_comment_view(&data, &mut *conn).await; let mut expected_comment_view_with_person = expected_comment_view_no_person.clone(); expected_comment_view_with_person.my_vote = Some(1); let read_comment_views_no_person = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(CommentSortType::Old)) .post_id(Some(data.inserted_post.id)) .build() @@ -610,7 +623,7 @@ mod tests { ); let read_comment_views_with_person = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(CommentSortType::Old)) .post_id(Some(data.inserted_post.id)) .local_user(Some(&data.inserted_local_user)) @@ -628,7 +641,7 @@ mod tests { assert_eq!(5, read_comment_views_with_person.len()); let read_comment_from_blocked_person = CommentView::read( - conn, + &mut *conn, data.inserted_comment_1.id, Some(data.inserted_person.id), ) @@ -638,18 +651,18 @@ mod tests { // Make sure block set the creator blocked assert!(read_comment_from_blocked_person.creator_blocked); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn test_comment_tree() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; let top_path = data.inserted_comment_0.path.clone(); let read_comment_views_top_path = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .post_id(Some(data.inserted_post.id)) .parent_path(Some(top_path)) .build() @@ -659,7 +672,7 @@ mod tests { let child_path = data.inserted_comment_1.path.clone(); let read_comment_views_child_path = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .post_id(Some(data.inserted_post.id)) .parent_path(Some(child_path)) .build() @@ -680,7 +693,7 @@ mod tests { assert!(!child_comments.contains(&data.inserted_comment_2)); let read_comment_views_top_max_depth = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .post_id(Some(data.inserted_post.id)) .max_depth(Some(1)) .build() @@ -690,14 +703,14 @@ mod tests { // Make sure a depth limited one only has the top comment assert_eq!( - expected_comment_view(&data, conn).await, + expected_comment_view(&data, &mut *conn).await, read_comment_views_top_max_depth[0] ); assert_eq!(1, read_comment_views_top_max_depth.len()); let child_path = data.inserted_comment_1.path.clone(); let read_comment_views_parent_max_depth = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .post_id(Some(data.inserted_post.id)) .parent_path(Some(child_path)) .max_depth(Some(1)) @@ -714,19 +727,19 @@ mod tests { .eq("Comment 3")); assert_eq!(3, read_comment_views_parent_max_depth.len()); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn test_languages() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; // by default, user has all languages enabled and should see all comments // (except from blocked user) let all_languages = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .local_user(Some(&data.inserted_local_user)) .build() .list() @@ -735,15 +748,15 @@ mod tests { assert_eq!(5, all_languages.len()); // change user lang to finnish, should only show one post in finnish and one undetermined - let finnish_id = Language::read_id_from_code(conn, Some("fi")) + let finnish_id = Language::read_id_from_code(&mut *conn, Some("fi")) .await .unwrap() .unwrap(); - LocalUserLanguage::update(conn, vec![finnish_id], data.inserted_local_user.id) + LocalUserLanguage::update(&mut *conn, vec![finnish_id], data.inserted_local_user.id) .await .unwrap(); let finnish_comments = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .local_user(Some(&data.inserted_local_user)) .build() .list() @@ -760,11 +773,15 @@ mod tests { ); // now show all comments with undetermined language (which is the default value) - LocalUserLanguage::update(conn, vec![UNDETERMINED_ID], data.inserted_local_user.id) - .await - .unwrap(); + LocalUserLanguage::update( + &mut *conn, + vec![UNDETERMINED_ID], + data.inserted_local_user.id, + ) + .await + .unwrap(); let undetermined_comment = CommentQuery::builder() - .conn(conn) + .conn(&mut *conn) .local_user(Some(&data.inserted_local_user)) .build() .list() @@ -772,34 +789,42 @@ mod tests { .unwrap(); assert_eq!(1, undetermined_comment.len()); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } - async fn cleanup(data: Data, conn: &mut DbConn) { - CommentLike::remove(conn, data.inserted_person.id, data.inserted_comment_0.id) + async fn cleanup(data: Data, mut conn: impl DbConn) { + CommentLike::remove( + &mut *conn, + data.inserted_person.id, + data.inserted_comment_0.id, + ) + .await + .unwrap(); + Comment::delete(&mut *conn, data.inserted_comment_0.id) .await .unwrap(); - Comment::delete(conn, data.inserted_comment_0.id) + Comment::delete(&mut *conn, data.inserted_comment_1.id) .await .unwrap(); - Comment::delete(conn, data.inserted_comment_1.id) + Post::delete(&mut *conn, data.inserted_post.id) .await .unwrap(); - Post::delete(conn, data.inserted_post.id).await.unwrap(); - Community::delete(conn, data.inserted_community.id) + Community::delete(&mut *conn, data.inserted_community.id) .await .unwrap(); - Person::delete(conn, data.inserted_person.id).await.unwrap(); - Person::delete(conn, data.inserted_person_2.id) + Person::delete(&mut *conn, data.inserted_person.id) .await .unwrap(); - Instance::delete(conn, data.inserted_instance.id) + Person::delete(&mut *conn, data.inserted_person_2.id) + .await + .unwrap(); + Instance::delete(&mut *conn, data.inserted_instance.id) .await .unwrap(); } - async fn expected_comment_view(data: &Data, conn: &mut DbConn) -> CommentView { - let agg = CommentAggregates::read(conn, data.inserted_comment_0.id) + async fn expected_comment_view(data: &Data, mut conn: impl DbConn) -> CommentView { + let agg = CommentAggregates::read(&mut *conn, data.inserted_comment_0.id) .await .unwrap(); CommentView { diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 127529954..eae0b09e0 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -12,7 +12,7 @@ use std::collections::HashMap; type CustomEmojiTuple = (CustomEmoji, Option); impl CustomEmojiView { - pub async fn get(conn: &mut DbConn, emoji_id: CustomEmojiId) -> Result { + pub async fn get(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result { let emojis = custom_emoji::table .find(emoji_id) .left_join( @@ -22,7 +22,7 @@ impl CustomEmojiView { custom_emoji::all_columns, custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want) )) - .load::(conn) + .load::(&mut *conn) .await?; if let Some(emoji) = CustomEmojiView::from_tuple_to_vec(emojis) .into_iter() @@ -35,7 +35,7 @@ impl CustomEmojiView { } pub async fn get_all( - conn: &mut DbConn, + mut conn: impl DbConn, for_local_site_id: LocalSiteId, ) -> Result, Error> { let emojis = custom_emoji::table @@ -49,7 +49,7 @@ impl CustomEmojiView { custom_emoji::all_columns, custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want) )) - .load::(conn) + .load::(&mut *conn) .await?; Ok(CustomEmojiView::from_tuple_to_vec(emojis)) diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index 25683a8d1..cf689d6c6 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::{ type LocalUserViewTuple = (LocalUser, Person, PersonAggregates); impl LocalUserView { - pub async fn read(conn: &mut DbConn, local_user_id: LocalUserId) -> Result { + pub async fn read(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result { let (local_user, person, counts) = local_user::table .find(local_user_id) .inner_join(person::table) @@ -23,7 +23,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { local_user, @@ -32,7 +32,7 @@ impl LocalUserView { }) } - pub async fn read_person(conn: &mut DbConn, person_id: PersonId) -> Result { + pub async fn read_person(mut conn: impl DbConn, person_id: PersonId) -> Result { let (local_user, person, counts) = local_user::table .filter(person::id.eq(person_id)) .inner_join(person::table) @@ -42,7 +42,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { local_user, @@ -51,7 +51,7 @@ impl LocalUserView { }) } - pub async fn read_from_name(conn: &mut DbConn, name: &str) -> Result { + pub async fn read_from_name(mut conn: impl DbConn, name: &str) -> Result { let (local_user, person, counts) = local_user::table .filter(lower(person::name).eq(name.to_lowercase())) .inner_join(person::table) @@ -61,7 +61,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { local_user, @@ -71,7 +71,7 @@ impl LocalUserView { } pub async fn find_by_email_or_name( - conn: &mut DbConn, + mut conn: impl DbConn, name_or_email: &str, ) -> Result { let (local_user, person, counts) = local_user::table @@ -87,7 +87,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { local_user, @@ -96,7 +96,7 @@ impl LocalUserView { }) } - pub async fn find_by_email(conn: &mut DbConn, from_email: &str) -> Result { + pub async fn find_by_email(mut conn: impl DbConn, from_email: &str) -> Result { let (local_user, person, counts) = local_user::table .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) @@ -106,7 +106,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { local_user, @@ -115,7 +115,7 @@ impl LocalUserView { }) } - pub async fn list_admins_with_emails(conn: &mut DbConn) -> Result, Error> { + pub async fn list_admins_with_emails(mut conn: impl DbConn) -> Result, Error> { let res = local_user::table .filter(person::admin.eq(true)) .filter(local_user::email.is_not_null()) @@ -126,7 +126,7 @@ impl LocalUserView { person::all_columns, person_aggregates::all_columns, )) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(LocalUserView::from_tuple).collect()) diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 2b9d218d0..ca49a37f5 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -49,7 +49,7 @@ impl PostReportView { /// /// * `report_id` - the report id to obtain pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, report_id: PostReportId, my_person_id: PersonId, ) -> Result { @@ -100,7 +100,7 @@ impl PostReportView { post_aggregates::all_columns, person_alias_2.fields(person::all_columns.nullable()), )) - .first::(conn) + .first::(&mut *conn) .await?; let my_vote = post_like; @@ -120,7 +120,7 @@ impl PostReportView { /// returns the current unresolved post report count for the communities you mod pub async fn get_report_count( - conn: &mut DbConn, + mut conn: impl DbConn, my_person_id: PersonId, admin: bool, community_id: Option, @@ -146,12 +146,12 @@ impl PostReportView { ), ) .select(count(post_report::id)) - .first::(conn) + .first::(&mut *conn) .await } else { query .select(count(post_report::id)) - .first::(conn) + .first::(&mut *conn) .await } } @@ -159,9 +159,9 @@ impl PostReportView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PostReportQuery<'a> { +pub struct PostReportQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, #[builder(!default)] my_person_id: PersonId, #[builder(!default)] @@ -172,9 +172,9 @@ pub struct PostReportQuery<'a> { unresolved_only: Option, } -impl<'a> PostReportQuery<'a> { +impl PostReportQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = post_report::table @@ -238,10 +238,10 @@ impl<'a> PostReportQuery<'a> { .and(community_moderator::person_id.eq(self.my_person_id)), ), ) - .load::(conn) + .load::(&mut *conn) .await? } else { - query.load::(conn).await? + query.load::(&mut *conn).await? }; Ok(res.into_iter().map(PostReportView::from_tuple).collect()) @@ -285,9 +285,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -297,7 +297,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_timmy = Person::create(conn, &new_person).await.unwrap(); + let inserted_timmy = Person::create(&mut *conn, &new_person).await.unwrap(); let new_person_2 = PersonInsertForm::builder() .name("sara_prv".into()) @@ -305,7 +305,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_sara = Person::create(conn, &new_person_2).await.unwrap(); + let inserted_sara = Person::create(&mut *conn, &new_person_2).await.unwrap(); // Add a third person, since new ppl can only report something once. let new_person_3 = PersonInsertForm::builder() @@ -314,7 +314,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_jessica = Person::create(conn, &new_person_3).await.unwrap(); + let inserted_jessica = Person::create(&mut *conn, &new_person_3).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test community prv".to_string()) @@ -323,7 +323,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); // Make timmy a mod let timmy_moderator_form = CommunityModeratorForm { @@ -331,7 +331,7 @@ mod tests { person_id: inserted_timmy.id, }; - let _inserted_moderator = CommunityModerator::join(conn, &timmy_moderator_form) + let _inserted_moderator = CommunityModerator::join(&mut *conn, &timmy_moderator_form) .await .unwrap(); @@ -341,7 +341,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); // sara reports let sara_report_form = PostReportForm { @@ -353,7 +353,9 @@ mod tests { reason: "from sara".into(), }; - let inserted_sara_report = PostReport::report(conn, &sara_report_form).await.unwrap(); + let inserted_sara_report = PostReport::report(&mut *conn, &sara_report_form) + .await + .unwrap(); // jessica reports let jessica_report_form = PostReportForm { @@ -365,14 +367,16 @@ mod tests { reason: "from jessica".into(), }; - let inserted_jessica_report = PostReport::report(conn, &jessica_report_form) + let inserted_jessica_report = PostReport::report(&mut *conn, &jessica_report_form) .await .unwrap(); - let agg = PostAggregates::read(conn, inserted_post.id).await.unwrap(); + let agg = PostAggregates::read(&mut *conn, inserted_post.id) + .await + .unwrap(); let read_jessica_report_view = - PostReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id) + PostReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); let expected_jessica_report_view = PostReportView { @@ -504,7 +508,7 @@ mod tests { // Do a batch read of timmys reports let reports = PostReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .my_person_id(inserted_timmy.id) .admin(false) .build() @@ -521,17 +525,17 @@ mod tests { ); // Make sure the counts are correct - let report_count = PostReportView::get_report_count(conn, inserted_timmy.id, false, None) + let report_count = PostReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None) .await .unwrap(); assert_eq!(2, report_count); // Try to resolve the report - PostReport::resolve(conn, inserted_jessica_report.id, inserted_timmy.id) + PostReport::resolve(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); let read_jessica_report_view_after_resolve = - PostReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id) + PostReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id) .await .unwrap(); @@ -578,7 +582,7 @@ mod tests { // Do a batch read of timmys reports // It should only show saras, which is unresolved let reports_after_resolve = PostReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .my_person_id(inserted_timmy.id) .admin(false) .unresolved_only(Some(true)) @@ -590,17 +594,21 @@ mod tests { // Make sure the counts are correct let report_count_after_resolved = - PostReportView::get_report_count(conn, inserted_timmy.id, false, None) + PostReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None) .await .unwrap(); assert_eq!(1, report_count_after_resolved); - Person::delete(conn, inserted_timmy.id).await.unwrap(); - Person::delete(conn, inserted_sara.id).await.unwrap(); - Person::delete(conn, inserted_jessica.id).await.unwrap(); - Community::delete(conn, inserted_community.id) + Person::delete(&mut *conn, inserted_timmy.id).await.unwrap(); + Person::delete(&mut *conn, inserted_sara.id).await.unwrap(); + Person::delete(&mut *conn, inserted_jessica.id) + .await + .unwrap(); + Community::delete(&mut *conn, inserted_community.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); } } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index c4e21c60c..824e42137 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -65,7 +65,7 @@ sql_function!(fn coalesce(x: sql_types::Nullable, y: sql_type impl PostView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, post_id: PostId, my_person_id: Option, is_mod_or_admin: Option, @@ -165,7 +165,7 @@ impl PostView { creator_blocked, post_like, unread_comments, - ) = query.first::(conn).await?; + ) = query.first::(&mut *conn).await?; // If a person is given, then my_vote, if None, should be 0, not null // Necessary to differentiate between other person's votes @@ -193,9 +193,9 @@ impl PostView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PostQuery<'a> { +pub struct PostQuery<'a, Conn> { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, listing_type: Option, sort: Option, creator_id: Option, @@ -210,9 +210,9 @@ pub struct PostQuery<'a> { limit: Option, } -impl<'a> PostQuery<'a> { +impl<'a, Conn: DbConn> PostQuery<'a, Conn> { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; // The left join below will return None in this case let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); @@ -448,7 +448,7 @@ impl<'a> PostQuery<'a> { debug!("Post View Query: {:?}", debug_query::(&query)); - let res = query.load::(conn).await?; + let res = query.load::(&mut *conn).await?; Ok(res.into_iter().map(PostView::from_tuple).collect()) } @@ -508,8 +508,8 @@ mod tests { inserted_post: Post, } - async fn init_data(conn: &mut DbConn) -> Data { - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + async fn init_data(mut conn: impl DbConn) -> Data { + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -521,13 +521,15 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_person = Person::create(conn, &new_person).await.unwrap(); + let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap(); let local_user_form = LocalUserInsertForm::builder() .person_id(inserted_person.id) .password_encrypted(String::new()) .build(); - let inserted_local_user = LocalUser::create(conn, &local_user_form).await.unwrap(); + let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form) + .await + .unwrap(); let new_bot = PersonInsertForm::builder() .name("mybot".to_string()) @@ -536,7 +538,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_bot = Person::create(conn, &new_bot).await.unwrap(); + let inserted_bot = Person::create(&mut *conn, &new_bot).await.unwrap(); let new_community = CommunityInsertForm::builder() .name("test_community_3".to_string()) @@ -545,7 +547,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_community = Community::create(conn, &new_community).await.unwrap(); + let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap(); // Test a person block, make sure the post query doesn't include their post let blocked_person = PersonInsertForm::builder() @@ -554,7 +556,7 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_blocked_person = Person::create(conn, &blocked_person).await.unwrap(); + let inserted_blocked_person = Person::create(&mut *conn, &blocked_person).await.unwrap(); let post_from_blocked_person = PostInsertForm::builder() .name("blocked_person_post".to_string()) @@ -563,7 +565,9 @@ mod tests { .language_id(Some(LanguageId(1))) .build(); - Post::create(conn, &post_from_blocked_person).await.unwrap(); + Post::create(&mut *conn, &post_from_blocked_person) + .await + .unwrap(); // block that person let person_block = PersonBlockForm { @@ -571,7 +575,7 @@ mod tests { target_id: inserted_blocked_person.id, }; - PersonBlock::block(conn, &person_block).await.unwrap(); + PersonBlock::block(&mut *conn, &person_block).await.unwrap(); // A sample post let new_post = PostInsertForm::builder() @@ -581,7 +585,7 @@ mod tests { .language_id(Some(LanguageId(47))) .build(); - let inserted_post = Post::create(conn, &new_post).await.unwrap(); + let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap(); let new_bot_post = PostInsertForm::builder() .name("test bot post".to_string()) @@ -589,7 +593,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let _inserted_bot_post = Post::create(conn, &new_bot_post).await.unwrap(); + let _inserted_bot_post = Post::create(&mut *conn, &new_bot_post).await.unwrap(); Data { inserted_instance, @@ -605,19 +609,19 @@ mod tests { #[tokio::test] #[serial] async fn post_listing_with_person() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; let local_user_form = LocalUserUpdateForm::builder() .show_bot_accounts(Some(false)) .build(); let inserted_local_user = - LocalUser::update(conn, data.inserted_local_user.id, &local_user_form) + LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form) .await .unwrap(); let read_post_listing = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) .local_user(Some(&inserted_local_user)) @@ -627,7 +631,7 @@ mod tests { .unwrap(); let post_listing_single_with_person = PostView::read( - conn, + &mut *conn, data.inserted_post.id, Some(data.inserted_person.id), None, @@ -635,7 +639,7 @@ mod tests { .await .unwrap(); - let mut expected_post_listing_with_user = expected_post_view(&data, conn).await; + let mut expected_post_listing_with_user = expected_post_view(&data, &mut *conn).await; // Should be only one person, IE the bot post, and blocked should be missing assert_eq!(1, read_post_listing.len()); @@ -651,12 +655,12 @@ mod tests { .show_bot_accounts(Some(true)) .build(); let inserted_local_user = - LocalUser::update(conn, data.inserted_local_user.id, &local_user_form) + LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form) .await .unwrap(); let post_listings_with_bots = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) .local_user(Some(&inserted_local_user)) @@ -667,17 +671,17 @@ mod tests { // should include bot post which has "undetermined" language assert_eq!(2, post_listings_with_bots.len()); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn post_listing_no_person() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; let read_post_listing_multiple_no_person = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) .build() @@ -686,11 +690,11 @@ mod tests { .unwrap(); let read_post_listing_single_no_person = - PostView::read(conn, data.inserted_post.id, None, None) + PostView::read(&mut *conn, data.inserted_post.id, None, None) .await .unwrap(); - let expected_post_listing_no_person = expected_post_view(&data, conn).await; + let expected_post_listing_no_person = expected_post_view(&data, &mut *conn).await; // Should be 2 posts, with the bot post, and the blocked assert_eq!(3, read_post_listing_multiple_no_person.len()); @@ -704,23 +708,25 @@ mod tests { read_post_listing_single_no_person ); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn post_listing_block_community() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; let community_block = CommunityBlockForm { person_id: data.inserted_person.id, community_id: data.inserted_community.id, }; - CommunityBlock::block(conn, &community_block).await.unwrap(); + CommunityBlock::block(&mut *conn, &community_block) + .await + .unwrap(); let read_post_listings_with_person_after_block = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) .local_user(Some(&data.inserted_local_user)) @@ -731,17 +737,17 @@ mod tests { // Should be 0 posts after the community block assert_eq!(0, read_post_listings_with_person_after_block.len()); - CommunityBlock::unblock(conn, &community_block) + CommunityBlock::unblock(&mut *conn, &community_block) .await .unwrap(); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn post_listing_like() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; let post_like_form = PostLikeForm { post_id: data.inserted_post.id, @@ -749,7 +755,7 @@ mod tests { score: 1, }; - let inserted_post_like = PostLike::like(conn, &post_like_form).await.unwrap(); + let inserted_post_like = PostLike::like(&mut *conn, &post_like_form).await.unwrap(); let expected_post_like = PostLike { id: inserted_post_like.id, @@ -761,7 +767,7 @@ mod tests { assert_eq!(expected_post_like, inserted_post_like); let post_listing_single_with_person = PostView::read( - conn, + &mut *conn, data.inserted_post.id, Some(data.inserted_person.id), None, @@ -769,7 +775,7 @@ mod tests { .await .unwrap(); - let mut expected_post_with_upvote = expected_post_view(&data, conn).await; + let mut expected_post_with_upvote = expected_post_view(&data, &mut *conn).await; expected_post_with_upvote.my_vote = Some(1); expected_post_with_upvote.counts.score = 1; expected_post_with_upvote.counts.upvotes = 1; @@ -779,12 +785,12 @@ mod tests { .show_bot_accounts(Some(false)) .build(); let inserted_local_user = - LocalUser::update(conn, data.inserted_local_user.id, &local_user_form) + LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form) .await .unwrap(); let read_post_listing = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) .local_user(Some(&inserted_local_user)) @@ -796,20 +802,20 @@ mod tests { assert_eq!(expected_post_with_upvote, read_post_listing[0]); - let like_removed = PostLike::remove(conn, data.inserted_person.id, data.inserted_post.id) + let like_removed = PostLike::remove(&mut *conn, data.inserted_person.id, data.inserted_post.id) .await .unwrap(); assert_eq!(1, like_removed); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn post_listing_person_language() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; - let spanish_id = Language::read_id_from_code(conn, Some("es")) + let spanish_id = Language::read_id_from_code(&mut *conn, Some("es")) .await .unwrap() .unwrap(); @@ -820,10 +826,10 @@ mod tests { .language_id(Some(spanish_id)) .build(); - Post::create(conn, &post_spanish).await.unwrap(); + Post::create(&mut *conn, &post_spanish).await.unwrap(); let post_listings_all = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .local_user(Some(&data.inserted_local_user)) .build() @@ -834,16 +840,16 @@ mod tests { // no language filters specified, all posts should be returned assert_eq!(3, post_listings_all.len()); - let french_id = Language::read_id_from_code(conn, Some("fr")) + let french_id = Language::read_id_from_code(&mut *conn, Some("fr")) .await .unwrap() .unwrap(); - LocalUserLanguage::update(conn, vec![french_id], data.inserted_local_user.id) + LocalUserLanguage::update(&mut *conn, vec![french_id], data.inserted_local_user.id) .await .unwrap(); let post_listing_french = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .local_user(Some(&data.inserted_local_user)) .build() @@ -858,14 +864,14 @@ mod tests { .any(|p| p.post.language_id == french_id)); LocalUserLanguage::update( - conn, + &mut *conn, vec![french_id, UNDETERMINED_ID], data.inserted_local_user.id, ) .await .unwrap(); let post_listings_french_und = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .local_user(Some(&data.inserted_local_user)) .build() @@ -881,18 +887,18 @@ mod tests { ); assert_eq!(french_id, post_listings_french_und[1].post.language_id); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } #[tokio::test] #[serial] async fn post_listings_deleted() { - let conn = &mut build_db_conn_for_tests().await; - let data = init_data(conn).await; + let mut conn = build_db_conn_for_tests().await; + let data = init_data(&mut *conn).await; // Delete the post Post::update( - conn, + &mut *conn, data.inserted_post.id, &PostUpdateForm::builder().deleted(Some(true)).build(), ) @@ -901,7 +907,7 @@ mod tests { // Make sure you don't see the deleted post in the results let post_listings_no_admin = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .local_user(Some(&data.inserted_local_user)) .is_mod_or_admin(Some(false)) @@ -914,7 +920,7 @@ mod tests { // Make sure they see both let post_listings_is_admin = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(SortType::New)) .local_user(Some(&data.inserted_local_user)) .is_mod_or_admin(Some(true)) @@ -925,32 +931,40 @@ mod tests { assert_eq!(2, post_listings_is_admin.len()); - cleanup(data, conn).await; + cleanup(data, &mut *conn).await; } - async fn cleanup(data: Data, conn: &mut DbConn) { - let num_deleted = Post::delete(conn, data.inserted_post.id).await.unwrap(); - Community::delete(conn, data.inserted_community.id) + async fn cleanup(data: Data, mut conn: impl DbConn) { + let num_deleted = Post::delete(&mut *conn, data.inserted_post.id) .await .unwrap(); - Person::delete(conn, data.inserted_person.id).await.unwrap(); - Person::delete(conn, data.inserted_bot.id).await.unwrap(); - Person::delete(conn, data.inserted_blocked_person.id) + Community::delete(&mut *conn, data.inserted_community.id) .await .unwrap(); - Instance::delete(conn, data.inserted_instance.id) + Person::delete(&mut *conn, data.inserted_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, data.inserted_bot.id) + .await + .unwrap(); + Person::delete(&mut *conn, data.inserted_blocked_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, data.inserted_instance.id) .await .unwrap(); assert_eq!(1, num_deleted); } - async fn expected_post_view(data: &Data, conn: &mut DbConn) -> PostView { + async fn expected_post_view(data: &Data, mut conn: impl DbConn) -> PostView { let (inserted_person, inserted_community, inserted_post) = ( &data.inserted_person, &data.inserted_community, &data.inserted_post, ); - let agg = PostAggregates::read(conn, inserted_post.id).await.unwrap(); + let agg = PostAggregates::read(&mut *conn, inserted_post.id) + .await + .unwrap(); PostView { post: Post { diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 62a007eaf..10d3dd8a0 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -26,7 +26,10 @@ impl PrivateMessageReportView { /// returns the PrivateMessageReportView for the provided report_id /// /// * `report_id` - the report id to obtain - pub async fn read(conn: &mut DbConn, report_id: PrivateMessageReportId) -> Result { + pub async fn read( + mut conn: impl DbConn, + report_id: PrivateMessageReportId, + ) -> Result { let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (private_message_report, private_message, private_message_creator, creator, resolver) = @@ -50,7 +53,7 @@ impl PrivateMessageReportView { person_alias_1.fields(person::all_columns), person_alias_2.fields(person::all_columns).nullable(), )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self { @@ -63,7 +66,7 @@ impl PrivateMessageReportView { } /// Returns the current unresolved post report count for the communities you mod - pub async fn get_report_count(conn: &mut DbConn) -> Result { + pub async fn get_report_count(mut conn: impl DbConn) -> Result { use diesel::dsl::count; private_message_report::table @@ -71,24 +74,24 @@ impl PrivateMessageReportView { .filter(private_message_report::resolved.eq(false)) .into_boxed() .select(count(private_message_report::id)) - .first::(conn) + .first::(&mut *conn) .await } } #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PrivateMessageReportQuery<'a> { +pub struct PrivateMessageReportQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, page: Option, limit: Option, unresolved_only: Option, } -impl<'a> PrivateMessageReportQuery<'a> { +impl PrivateMessageReportQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = private_message_report::table @@ -121,7 +124,9 @@ impl<'a> PrivateMessageReportQuery<'a> { .limit(limit) .offset(offset); - let res = query.load::(conn).await?; + let res = query + .load::(&mut *conn) + .await?; Ok( res @@ -163,9 +168,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -174,14 +179,14 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let inserted_timmy = Person::create(conn, &new_person_1).await.unwrap(); + let inserted_timmy = Person::create(&mut *conn, &new_person_1).await.unwrap(); let new_person_2 = PersonInsertForm::builder() .name("jessica_mrv".into()) .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let inserted_jessica = Person::create(conn, &new_person_2).await.unwrap(); + let inserted_jessica = Person::create(&mut *conn, &new_person_2).await.unwrap(); // timmy sends private message to jessica let pm_form = PrivateMessageInsertForm::builder() @@ -189,7 +194,7 @@ mod tests { .recipient_id(inserted_jessica.id) .content("something offensive".to_string()) .build(); - let pm = PrivateMessage::create(conn, &pm_form).await.unwrap(); + let pm = PrivateMessage::create(&mut *conn, &pm_form).await.unwrap(); // jessica reports private message let pm_report_form = PrivateMessageReportForm { @@ -198,12 +203,12 @@ mod tests { private_message_id: pm.id, reason: "its offensive".to_string(), }; - let pm_report = PrivateMessageReport::report(conn, &pm_report_form) + let pm_report = PrivateMessageReport::report(&mut *conn, &pm_report_form) .await .unwrap(); let reports = PrivateMessageReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .build() .list() .await @@ -220,15 +225,15 @@ mod tests { .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) .build(); - let inserted_admin = Person::create(conn, &new_person_3).await.unwrap(); + let inserted_admin = Person::create(&mut *conn, &new_person_3).await.unwrap(); // admin resolves the report (after taking appropriate action) - PrivateMessageReport::resolve(conn, pm_report.id, inserted_admin.id) + PrivateMessageReport::resolve(&mut *conn, pm_report.id, inserted_admin.id) .await .unwrap(); let reports = PrivateMessageReportQuery::builder() - .conn(conn) + .conn(&mut *conn) .unresolved_only(Some(false)) .build() .list() @@ -242,6 +247,8 @@ mod tests { reports[0].resolver.as_ref().unwrap().name ); - Instance::delete(conn, inserted_instance.id).await.unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) + .await + .unwrap(); } } diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index f110b0b6c..f85b4207e 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -23,7 +23,7 @@ type PrivateMessageViewTuple = (PrivateMessage, Person, Person); impl PrivateMessageView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, private_message_id: PrivateMessageId, ) -> Result { let person_alias_1 = diesel::alias!(person as person1); @@ -40,7 +40,7 @@ impl PrivateMessageView { person::all_columns, person_alias_1.fields(person::all_columns), )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(PrivateMessageView { @@ -52,7 +52,7 @@ impl PrivateMessageView { /// Gets the number of unread messages pub async fn get_unread_messages( - conn: &mut DbConn, + mut conn: impl DbConn, my_person_id: PersonId, ) -> Result { use diesel::dsl::count; @@ -61,16 +61,16 @@ impl PrivateMessageView { .filter(private_message::recipient_id.eq(my_person_id)) .filter(private_message::deleted.eq(false)) .select(count(private_message::id)) - .first::(conn) + .first::(&mut *conn) .await } } #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PrivateMessageQuery<'a> { +pub struct PrivateMessageQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, #[builder(!default)] recipient_id: PersonId, unread_only: Option, @@ -78,9 +78,9 @@ pub struct PrivateMessageQuery<'a> { limit: Option, } -impl<'a> PrivateMessageQuery<'a> { +impl PrivateMessageQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let person_alias_1 = diesel::alias!(person as person1); let mut query = private_message::table @@ -123,7 +123,7 @@ impl<'a> PrivateMessageQuery<'a> { debug_query::(&query) ); - let res = query.load::(conn).await?; + let res = query.load::(&mut *conn).await?; Ok( res diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index f5aaa21c9..ef39d33a2 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -24,7 +24,10 @@ type RegistrationApplicationViewTuple = (RegistrationApplication, LocalUser, Person, Option); impl RegistrationApplicationView { - pub async fn read(conn: &mut DbConn, registration_application_id: i32) -> Result { + pub async fn read( + mut conn: impl DbConn, + registration_application_id: i32, + ) -> Result { let person_alias_1 = diesel::alias!(person as person1); let (registration_application, creator_local_user, creator, admin) = @@ -45,7 +48,7 @@ impl RegistrationApplicationView { person::all_columns, person_alias_1.fields(person::all_columns).nullable(), )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(RegistrationApplicationView { @@ -58,7 +61,7 @@ impl RegistrationApplicationView { /// Returns the current unread registration_application count pub async fn get_unread_count( - conn: &mut DbConn, + mut conn: impl DbConn, verified_email_only: bool, ) -> Result { let person_alias_1 = diesel::alias!(person as person1); @@ -79,25 +82,25 @@ impl RegistrationApplicationView { query .select(count(registration_application::id)) - .first::(conn) + .first::(&mut *conn) .await } } #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct RegistrationApplicationQuery<'a> { +pub struct RegistrationApplicationQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, unread_only: Option, verified_email_only: Option, page: Option, limit: Option, } -impl<'a> RegistrationApplicationQuery<'a> { +impl RegistrationApplicationQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table @@ -131,7 +134,9 @@ impl<'a> RegistrationApplicationQuery<'a> { .offset(offset) .order_by(registration_application::published.desc()); - let res = query.load::(conn).await?; + let res = query + .load::(&mut *conn) + .await?; Ok( res @@ -179,9 +184,9 @@ mod tests { #[tokio::test] #[serial] async fn test_crud() { - let conn = &mut build_db_conn_for_tests().await; + let mut conn = build_db_conn_for_tests().await; - let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string()) + let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string()) .await .unwrap(); @@ -192,14 +197,16 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_timmy_person = Person::create(conn, &timmy_person_form).await.unwrap(); + let inserted_timmy_person = Person::create(&mut *conn, &timmy_person_form) + .await + .unwrap(); let timmy_local_user_form = LocalUserInsertForm::builder() .person_id(inserted_timmy_person.id) .password_encrypted("nada".to_string()) .build(); - let _inserted_timmy_local_user = LocalUser::create(conn, &timmy_local_user_form) + let _inserted_timmy_local_user = LocalUser::create(&mut *conn, &timmy_local_user_form) .await .unwrap(); @@ -209,14 +216,14 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_sara_person = Person::create(conn, &sara_person_form).await.unwrap(); + let inserted_sara_person = Person::create(&mut *conn, &sara_person_form).await.unwrap(); let sara_local_user_form = LocalUserInsertForm::builder() .person_id(inserted_sara_person.id) .password_encrypted("nada".to_string()) .build(); - let inserted_sara_local_user = LocalUser::create(conn, &sara_local_user_form) + let inserted_sara_local_user = LocalUser::create(&mut *conn, &sara_local_user_form) .await .unwrap(); @@ -226,11 +233,11 @@ mod tests { answer: "LET ME IIIIINN".to_string(), }; - let sara_app = RegistrationApplication::create(conn, &sara_app_form) + let sara_app = RegistrationApplication::create(&mut *conn, &sara_app_form) .await .unwrap(); - let read_sara_app_view = RegistrationApplicationView::read(conn, sara_app.id) + let read_sara_app_view = RegistrationApplicationView::read(&mut *conn, sara_app.id) .await .unwrap(); @@ -240,14 +247,14 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let inserted_jess_person = Person::create(conn, &jess_person_form).await.unwrap(); + let inserted_jess_person = Person::create(&mut *conn, &jess_person_form).await.unwrap(); let jess_local_user_form = LocalUserInsertForm::builder() .person_id(inserted_jess_person.id) .password_encrypted("nada".to_string()) .build(); - let inserted_jess_local_user = LocalUser::create(conn, &jess_local_user_form) + let inserted_jess_local_user = LocalUser::create(&mut *conn, &jess_local_user_form) .await .unwrap(); @@ -257,11 +264,11 @@ mod tests { answer: "LET ME IIIIINN".to_string(), }; - let jess_app = RegistrationApplication::create(conn, &jess_app_form) + let jess_app = RegistrationApplication::create(&mut *conn, &jess_app_form) .await .unwrap(); - let read_jess_app_view = RegistrationApplicationView::read(conn, jess_app.id) + let read_jess_app_view = RegistrationApplicationView::read(&mut *conn, jess_app.id) .await .unwrap(); @@ -320,7 +327,7 @@ mod tests { // Do a batch read of the applications let apps = RegistrationApplicationQuery::builder() - .conn(conn) + .conn(&mut *conn) .unread_only(Some(true)) .build() .list() @@ -333,7 +340,7 @@ mod tests { ); // Make sure the counts are correct - let unread_count = RegistrationApplicationView::get_unread_count(conn, false) + let unread_count = RegistrationApplicationView::get_unread_count(&mut *conn, false) .await .unwrap(); assert_eq!(unread_count, 2); @@ -344,7 +351,7 @@ mod tests { deny_reason: None, }; - RegistrationApplication::update(conn, sara_app.id, &approve_form) + RegistrationApplication::update(&mut *conn, sara_app.id, &approve_form) .await .unwrap(); @@ -353,13 +360,18 @@ mod tests { .accepted_application(Some(true)) .build(); - LocalUser::update(conn, inserted_sara_local_user.id, &approve_local_user_form) - .await - .unwrap(); + LocalUser::update( + &mut *conn, + inserted_sara_local_user.id, + &approve_local_user_form, + ) + .await + .unwrap(); - let read_sara_app_view_after_approve = RegistrationApplicationView::read(conn, sara_app.id) - .await - .unwrap(); + let read_sara_app_view_after_approve = + RegistrationApplicationView::read(&mut *conn, sara_app.id) + .await + .unwrap(); // Make sure the columns changed expected_sara_app_view @@ -396,7 +408,7 @@ mod tests { // Do a batch read of apps again // It should show only jessicas which is unresolved let apps_after_resolve = RegistrationApplicationQuery::builder() - .conn(conn) + .conn(&mut *conn) .unread_only(Some(true)) .build() .list() @@ -405,25 +417,32 @@ mod tests { assert_eq!(apps_after_resolve, vec![read_jess_app_view]); // Make sure the counts are correct - let unread_count_after_approve = RegistrationApplicationView::get_unread_count(conn, false) - .await - .unwrap(); + let unread_count_after_approve = + RegistrationApplicationView::get_unread_count(&mut *conn, false) + .await + .unwrap(); assert_eq!(unread_count_after_approve, 1); // Make sure the not undenied_only has all the apps let all_apps = RegistrationApplicationQuery::builder() - .conn(conn) + .conn(&mut *conn) .build() .list() .await .unwrap(); assert_eq!(all_apps.len(), 2); - Person::delete(conn, inserted_timmy_person.id) + Person::delete(&mut *conn, inserted_timmy_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_sara_person.id) + .await + .unwrap(); + Person::delete(&mut *conn, inserted_jess_person.id) + .await + .unwrap(); + Instance::delete(&mut *conn, inserted_instance.id) .await .unwrap(); - Person::delete(conn, inserted_sara_person.id).await.unwrap(); - Person::delete(conn, inserted_jess_person.id).await.unwrap(); - Instance::delete(conn, inserted_instance.id).await.unwrap(); } } diff --git a/crates/db_views/src/site_view.rs b/crates/db_views/src/site_view.rs index 47d88c63e..24a8a6150 100644 --- a/crates/db_views/src/site_view.rs +++ b/crates/db_views/src/site_view.rs @@ -9,7 +9,7 @@ use lemmy_db_schema::{ }; impl SiteView { - pub async fn read_local(conn: &mut DbConn) -> Result { + pub async fn read_local(mut conn: impl DbConn) -> Result { let (mut site, local_site, local_site_rate_limit, counts) = site::table .inner_join(local_site::table) .inner_join( @@ -22,7 +22,7 @@ impl SiteView { local_site_rate_limit::all_columns, site_aggregates::all_columns, )) - .first::<(Site, LocalSite, LocalSiteRateLimit, SiteAggregates)>(conn) + .first::<(Site, LocalSite, LocalSiteRateLimit, SiteAggregates)>(&mut *conn) .await?; site.private_key = None; diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 6931dad7e..3521bc9f2 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -55,7 +55,7 @@ type CommentReplyViewTuple = ( impl CommentReplyView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, comment_reply_id: CommentReplyId, my_person_id: Option, ) -> Result { @@ -134,7 +134,7 @@ impl CommentReplyView { person_block::all_columns.nullable(), comment_like::score.nullable(), )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(CommentReplyView { @@ -154,7 +154,10 @@ impl CommentReplyView { } /// Gets the number of unread replies - pub async fn get_unread_replies(conn: &mut DbConn, my_person_id: PersonId) -> Result { + pub async fn get_unread_replies( + mut conn: impl DbConn, + my_person_id: PersonId, + ) -> Result { use diesel::dsl::count; comment_reply::table @@ -164,16 +167,16 @@ impl CommentReplyView { .filter(comment::deleted.eq(false)) .filter(comment::removed.eq(false)) .select(count(comment_reply::id)) - .first::(conn) + .first::(&mut *conn) .await } } #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentReplyQuery<'a> { +pub struct CommentReplyQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, my_person_id: Option, recipient_id: Option, sort: Option, @@ -183,9 +186,9 @@ pub struct CommentReplyQuery<'a> { limit: Option, } -impl<'a> CommentReplyQuery<'a> { +impl CommentReplyQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let person_alias_1 = diesel::alias!(person as person1); @@ -274,7 +277,7 @@ impl<'a> CommentReplyQuery<'a> { let res = query .limit(limit) .offset(offset) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(CommentReplyView::from_tuple).collect()) diff --git a/crates/db_views_actor/src/community_block_view.rs b/crates/db_views_actor/src/community_block_view.rs index 7b602b7bd..fb6f2882a 100644 --- a/crates/db_views_actor/src/community_block_view.rs +++ b/crates/db_views_actor/src/community_block_view.rs @@ -12,7 +12,7 @@ use lemmy_db_schema::{ type CommunityBlockViewTuple = (Person, Community); impl CommunityBlockView { - pub async fn for_person(conn: &mut DbConn, person_id: PersonId) -> Result, Error> { + pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result, Error> { let res = community_block::table .inner_join(person::table) .inner_join(community::table) @@ -21,7 +21,7 @@ impl CommunityBlockView { .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) .order_by(community_block::published) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index d61ecc739..f41f9ec68 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -13,7 +13,7 @@ type CommunityFollowerViewTuple = (Community, Person); impl CommunityFollowerView { pub async fn for_community( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, ) -> Result, Error> { let res = community_follower::table @@ -22,13 +22,13 @@ impl CommunityFollowerView { .select((community::all_columns, person::all_columns)) .filter(community_follower::community_id.eq(community_id)) .order_by(community::title) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) } - pub async fn for_person(conn: &mut DbConn, person_id: PersonId) -> Result, Error> { + pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result, Error> { let res = community_follower::table .inner_join(community::table) .inner_join(person::table) @@ -37,7 +37,7 @@ impl CommunityFollowerView { .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) .order_by(community::title) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) diff --git a/crates/db_views_actor/src/community_moderator_view.rs b/crates/db_views_actor/src/community_moderator_view.rs index b752f459e..5aa902fb0 100644 --- a/crates/db_views_actor/src/community_moderator_view.rs +++ b/crates/db_views_actor/src/community_moderator_view.rs @@ -13,7 +13,7 @@ type CommunityModeratorViewTuple = (Community, Person); impl CommunityModeratorView { pub async fn for_community( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, ) -> Result, Error> { let res = community_moderator::table @@ -21,13 +21,13 @@ impl CommunityModeratorView { .inner_join(person::table) .select((community::all_columns, person::all_columns)) .filter(community_moderator::community_id.eq(community_id)) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) } - pub async fn for_person(conn: &mut DbConn, person_id: PersonId) -> Result, Error> { + pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result, Error> { let res = community_moderator::table .inner_join(community::table) .inner_join(person::table) @@ -35,7 +35,7 @@ impl CommunityModeratorView { .filter(community_moderator::person_id.eq(person_id)) .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) @@ -43,7 +43,7 @@ impl CommunityModeratorView { /// Finds all communities first mods / creators /// Ideally this should be a group by, but diesel doesn't support it yet - pub async fn get_community_first_mods(conn: &mut DbConn) -> Result, Error> { + pub async fn get_community_first_mods(mut conn: impl DbConn) -> Result, Error> { let res = community_moderator::table .inner_join(community::table) .inner_join(person::table) @@ -55,7 +55,7 @@ impl CommunityModeratorView { community_moderator::community_id, community_moderator::person_id, )) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) diff --git a/crates/db_views_actor/src/community_person_ban_view.rs b/crates/db_views_actor/src/community_person_ban_view.rs index 7a0e94125..b58bc4935 100644 --- a/crates/db_views_actor/src/community_person_ban_view.rs +++ b/crates/db_views_actor/src/community_person_ban_view.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::{ impl CommunityPersonBanView { pub async fn get( - conn: &mut DbConn, + mut conn: impl DbConn, from_person_id: PersonId, from_community_id: CommunityId, ) -> Result { @@ -21,7 +21,7 @@ impl CommunityPersonBanView { .filter(community_person_ban::community_id.eq(from_community_id)) .filter(community_person_ban::person_id.eq(from_person_id)) .order_by(community_person_ban::published) - .first::<(Community, Person)>(conn) + .first::<(Community, Person)>(&mut *conn) .await?; Ok(CommunityPersonBanView { community, person }) diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 7f83c6639..e81bf06c5 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -34,7 +34,7 @@ type CommunityViewTuple = ( impl CommunityView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, community_id: CommunityId, my_person_id: Option, is_mod_or_admin: Option, @@ -74,7 +74,8 @@ impl CommunityView { .filter(community::deleted.eq(false)); } - let (community, counts, follower, blocked) = query.first::(conn).await?; + let (community, counts, follower, blocked) = + query.first::(&mut *conn).await?; Ok(CommunityView { community, @@ -85,11 +86,11 @@ impl CommunityView { } pub async fn is_mod_or_admin( - conn: &mut DbConn, + mut conn: impl DbConn, person_id: PersonId, community_id: CommunityId, ) -> Result { - let is_mod = CommunityModeratorView::for_community(conn, community_id) + let is_mod = CommunityModeratorView::for_community(&mut *conn, community_id) .await .map(|v| { v.into_iter() @@ -102,7 +103,7 @@ impl CommunityView { return Ok(true); } - let is_admin = PersonView::admins(conn) + let is_admin = PersonView::admins(&mut *conn) .await .map(|v| { v.into_iter() @@ -117,9 +118,9 @@ impl CommunityView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommunityQuery<'a> { +pub struct CommunityQuery<'a, Conn> { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, listing_type: Option, sort: Option, local_user: Option<&'a LocalUser>, @@ -130,9 +131,9 @@ pub struct CommunityQuery<'a> { limit: Option, } -impl<'a> CommunityQuery<'a> { +impl<'a, Conn: DbConn> CommunityQuery<'a, Conn> { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; // The left join below will return None in this case let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); @@ -213,7 +214,7 @@ impl<'a> CommunityQuery<'a> { let res = query .limit(limit) .offset(offset) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(CommunityView::from_tuple).collect()) diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index ef658b523..8253ab510 100644 --- a/crates/db_views_actor/src/person_block_view.rs +++ b/crates/db_views_actor/src/person_block_view.rs @@ -12,7 +12,7 @@ use lemmy_db_schema::{ type PersonBlockViewTuple = (Person, Person); impl PersonBlockView { - pub async fn for_person(conn: &mut DbConn, person_id: PersonId) -> Result, Error> { + pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result, Error> { let target_person_alias = diesel::alias!(person as person1); let res = person_block::table @@ -27,7 +27,7 @@ impl PersonBlockView { .filter(person_block::person_id.eq(person_id)) .filter(target_person_alias.field(person::deleted).eq(false)) .order_by(person_block::published) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(Self::from_tuple).collect()) diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index b9fa4e5e1..dde63823c 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -56,7 +56,7 @@ type PersonMentionViewTuple = ( impl PersonMentionView { pub async fn read( - conn: &mut DbConn, + mut conn: impl DbConn, person_mention_id: PersonMentionId, my_person_id: Option, ) -> Result { @@ -135,7 +135,7 @@ impl PersonMentionView { person_block::all_columns.nullable(), comment_like::score.nullable(), )) - .first::(conn) + .first::(&mut *conn) .await?; Ok(PersonMentionView { @@ -156,7 +156,7 @@ impl PersonMentionView { /// Gets the number of unread mentions pub async fn get_unread_mentions( - conn: &mut DbConn, + mut conn: impl DbConn, my_person_id: PersonId, ) -> Result { use diesel::dsl::count; @@ -168,16 +168,16 @@ impl PersonMentionView { .filter(comment::deleted.eq(false)) .filter(comment::removed.eq(false)) .select(count(person_mention::id)) - .first::(conn) + .first::(&mut *conn) .await } } #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PersonMentionQuery<'a> { +pub struct PersonMentionQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, my_person_id: Option, recipient_id: Option, sort: Option, @@ -187,9 +187,9 @@ pub struct PersonMentionQuery<'a> { limit: Option, } -impl<'a> PersonMentionQuery<'a> { +impl PersonMentionQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let person_alias_1 = diesel::alias!(person as person1); @@ -283,7 +283,7 @@ impl<'a> PersonMentionQuery<'a> { let res = query .limit(limit) .offset(offset) - .load::(conn) + .load::(&mut *conn) .await?; Ok(res.into_iter().map(PersonMentionView::from_tuple).collect()) diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 9db18e371..fd29294be 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -23,30 +23,30 @@ use typed_builder::TypedBuilder; type PersonViewTuple = (Person, PersonAggregates); impl PersonView { - pub async fn read(conn: &mut DbConn, person_id: PersonId) -> Result { + pub async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result { let res = person::table .find(person_id) .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) - .first::(conn) + .first::(&mut *conn) .await?; Ok(Self::from_tuple(res)) } - pub async fn admins(conn: &mut DbConn) -> Result, Error> { + pub async fn admins(mut conn: impl DbConn) -> Result, Error> { let admins = person::table .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) .filter(person::admin.eq(true)) .filter(person::deleted.eq(false)) .order_by(person::published) - .load::(conn) + .load::(&mut *conn) .await?; Ok(admins.into_iter().map(Self::from_tuple).collect()) } - pub async fn banned(conn: &mut DbConn) -> Result, Error> { + pub async fn banned(mut conn: impl DbConn) -> Result, Error> { let banned = person::table .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) @@ -58,7 +58,7 @@ impl PersonView { ), ) .filter(person::deleted.eq(false)) - .load::(conn) + .load::(&mut *conn) .await?; Ok(banned.into_iter().map(Self::from_tuple).collect()) @@ -67,18 +67,18 @@ impl PersonView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PersonQuery<'a> { +pub struct PersonQuery { #[builder(!default)] - conn: &'a mut DbConn, + conn: Conn, sort: Option, search_term: Option, page: Option, limit: Option, } -impl<'a> PersonQuery<'a> { +impl PersonQuery { pub async fn list(self) -> Result, Error> { - let conn = self.conn; + let mut conn = self.conn; let mut query = person::table .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) @@ -133,7 +133,7 @@ impl<'a> PersonQuery<'a> { let (limit, offset) = limit_and_offset(self.page, self.limit)?; query = query.limit(limit).offset(offset); - let res = query.load::(conn).await?; + let res = query.load::(&mut *conn).await?; Ok(res.into_iter().map(PersonView::from_tuple).collect()) } diff --git a/crates/db_views_moderator/src/admin_purge_comment_view.rs b/crates/db_views_moderator/src/admin_purge_comment_view.rs index e97f858ff..bac5795bd 100644 --- a/crates/db_views_moderator/src/admin_purge_comment_view.rs +++ b/crates/db_views_moderator/src/admin_purge_comment_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option, Post); impl AdminPurgeCommentView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -49,7 +49,7 @@ impl AdminPurgeCommentView { .limit(limit) .offset(offset) .order_by(admin_purge_comment::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/admin_purge_community_view.rs b/crates/db_views_moderator/src/admin_purge_community_view.rs index 85a2f46ef..da5e92e9e 100644 --- a/crates/db_views_moderator/src/admin_purge_community_view.rs +++ b/crates/db_views_moderator/src/admin_purge_community_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option); impl AdminPurgeCommunityView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -47,7 +47,7 @@ impl AdminPurgeCommunityView { .limit(limit) .offset(offset) .order_by(admin_purge_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/admin_purge_person_view.rs b/crates/db_views_moderator/src/admin_purge_person_view.rs index c835c2273..bea6562e0 100644 --- a/crates/db_views_moderator/src/admin_purge_person_view.rs +++ b/crates/db_views_moderator/src/admin_purge_person_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type AdminPurgePersonViewTuple = (AdminPurgePerson, Option); impl AdminPurgePersonView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -46,7 +46,7 @@ impl AdminPurgePersonView { .limit(limit) .offset(offset) .order_by(admin_purge_person::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/admin_purge_post_view.rs b/crates/db_views_moderator/src/admin_purge_post_view.rs index ea3900200..eec64384d 100644 --- a/crates/db_views_moderator/src/admin_purge_post_view.rs +++ b/crates/db_views_moderator/src/admin_purge_post_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type AdminPurgePostViewTuple = (AdminPurgePost, Option, Community); impl AdminPurgePostView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -48,7 +48,7 @@ impl AdminPurgePostView { .limit(limit) .offset(offset) .order_by(admin_purge_post::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_add_community_view.rs b/crates/db_views_moderator/src/mod_add_community_view.rs index 848d321e7..a8e4e243f 100644 --- a/crates/db_views_moderator/src/mod_add_community_view.rs +++ b/crates/db_views_moderator/src/mod_add_community_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModAddCommunityViewTuple = (ModAddCommunity, Option, Community, Person); impl ModAddCommunityView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -61,7 +61,7 @@ impl ModAddCommunityView { .limit(limit) .offset(offset) .order_by(mod_add_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index c41ab333d..c1f977cba 100644 --- a/crates/db_views_moderator/src/mod_add_view.rs +++ b/crates/db_views_moderator/src/mod_add_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModAddViewTuple = (ModAdd, Option, Person); impl ModAddView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -53,7 +53,7 @@ impl ModAddView { .limit(limit) .offset(offset) .order_by(mod_add::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_ban_from_community_view.rs b/crates/db_views_moderator/src/mod_ban_from_community_view.rs index b0503cad3..1045eb70d 100644 --- a/crates/db_views_moderator/src/mod_ban_from_community_view.rs +++ b/crates/db_views_moderator/src/mod_ban_from_community_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModBanFromCommunityViewTuple = (ModBanFromCommunity, Option, Community, Person); impl ModBanFromCommunityView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -62,7 +62,7 @@ impl ModBanFromCommunityView { .limit(limit) .offset(offset) .order_by(mod_ban_from_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index 4b28e6b47..abb6f0a1e 100644 --- a/crates/db_views_moderator/src/mod_ban_view.rs +++ b/crates/db_views_moderator/src/mod_ban_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModBanViewTuple = (ModBan, Option, Person); impl ModBanView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -53,7 +53,7 @@ impl ModBanView { .limit(limit) .offset(offset) .order_by(mod_ban::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_feature_post_view.rs b/crates/db_views_moderator/src/mod_feature_post_view.rs index 7ab33fda3..851ae3162 100644 --- a/crates/db_views_moderator/src/mod_feature_post_view.rs +++ b/crates/db_views_moderator/src/mod_feature_post_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModFeaturePostViewTuple = (ModFeaturePost, Option, Post, Community); impl ModFeaturePostView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -60,7 +60,7 @@ impl ModFeaturePostView { .limit(limit) .offset(offset) .order_by(mod_feature_post::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_hide_community_view.rs b/crates/db_views_moderator/src/mod_hide_community_view.rs index d351b5e2d..2c0428893 100644 --- a/crates/db_views_moderator/src/mod_hide_community_view.rs +++ b/crates/db_views_moderator/src/mod_hide_community_view.rs @@ -21,7 +21,7 @@ type ModHideCommunityViewTuple = (ModHideCommunity, Option, Community); impl ModHideCommunityView { // Pass in mod_id as admin_id because only admins can do this action - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -53,7 +53,7 @@ impl ModHideCommunityView { .limit(limit) .offset(offset) .order_by(mod_hide_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_lock_post_view.rs b/crates/db_views_moderator/src/mod_lock_post_view.rs index 78b77282c..696dccbdb 100644 --- a/crates/db_views_moderator/src/mod_lock_post_view.rs +++ b/crates/db_views_moderator/src/mod_lock_post_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModLockPostViewTuple = (ModLockPost, Option, Post, Community); impl ModLockPostView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -60,7 +60,7 @@ impl ModLockPostView { .limit(limit) .offset(offset) .order_by(mod_lock_post::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_remove_comment_view.rs b/crates/db_views_moderator/src/mod_remove_comment_view.rs index 3fde5eca8..e504bf275 100644 --- a/crates/db_views_moderator/src/mod_remove_comment_view.rs +++ b/crates/db_views_moderator/src/mod_remove_comment_view.rs @@ -33,7 +33,7 @@ type ModRemoveCommentViewTuple = ( ); impl ModRemoveCommentView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -76,7 +76,7 @@ impl ModRemoveCommentView { .limit(limit) .offset(offset) .order_by(mod_remove_comment::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_remove_community_view.rs b/crates/db_views_moderator/src/mod_remove_community_view.rs index a918d2fb9..bf3a7236f 100644 --- a/crates/db_views_moderator/src/mod_remove_community_view.rs +++ b/crates/db_views_moderator/src/mod_remove_community_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModRemoveCommunityTuple = (ModRemoveCommunity, Option, Community); impl ModRemoveCommunityView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; let show_mod_names_expr = show_mod_names.as_sql::(); @@ -48,7 +48,7 @@ impl ModRemoveCommunityView { .limit(limit) .offset(offset) .order_by(mod_remove_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_remove_post_view.rs b/crates/db_views_moderator/src/mod_remove_post_view.rs index b6a101274..b947fad16 100644 --- a/crates/db_views_moderator/src/mod_remove_post_view.rs +++ b/crates/db_views_moderator/src/mod_remove_post_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModRemovePostViewTuple = (ModRemovePost, Option, Post, Community); impl ModRemovePostView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -60,7 +60,7 @@ impl ModRemovePostView { .limit(limit) .offset(offset) .order_by(mod_remove_post::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/db_views_moderator/src/mod_transfer_community_view.rs b/crates/db_views_moderator/src/mod_transfer_community_view.rs index 80a9b4835..e013a1504 100644 --- a/crates/db_views_moderator/src/mod_transfer_community_view.rs +++ b/crates/db_views_moderator/src/mod_transfer_community_view.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ type ModTransferCommunityViewTuple = (ModTransferCommunity, Option, Community, Person); impl ModTransferCommunityView { - pub async fn list(conn: &mut DbConn, params: ModlogListParams) -> Result, Error> { + pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result, Error> { let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; @@ -62,7 +62,7 @@ impl ModTransferCommunityView { .limit(limit) .offset(offset) .order_by(mod_transfer_community::when_.desc()) - .load::(conn) + .load::(&mut *conn) .await?; let results = res.into_iter().map(Self::from_tuple).collect(); diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index a45f82135..af42bf072 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -122,11 +122,11 @@ async fn get_feed_data( limit: i64, page: i64, ) -> Result { - let site_view = SiteView::read_local(&mut *context.conn().await?).await?; + let site_view = SiteView::read_local(context.conn().await?).await?; let mut conn = context.conn().await?; let posts = PostQuery::builder() - .conn(&mut conn) + .conn(&mut *conn) .listing_type(Some(listing_type)) .sort(Some(sort_type)) .limit(Some(limit)) @@ -234,18 +234,18 @@ async fn get_feed( #[tracing::instrument(skip_all)] async fn get_feed_user( - conn: &mut DbConn, + mut conn: impl DbConn, sort_type: &SortType, limit: &i64, page: &i64, user_name: &str, protocol_and_hostname: &str, ) -> Result { - let site_view = SiteView::read_local(conn).await?; - let person = Person::read_from_name(conn, user_name, false).await?; + let site_view = SiteView::read_local(&mut *conn).await?; + let person = Person::read_from_name(&mut *conn, user_name, false).await?; let posts = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .listing_type(Some(ListingType::All)) .sort(Some(*sort_type)) .creator_id(Some(person.id)) @@ -269,18 +269,18 @@ async fn get_feed_user( #[tracing::instrument(skip_all)] async fn get_feed_community( - conn: &mut DbConn, + mut conn: impl DbConn, sort_type: &SortType, limit: &i64, page: &i64, community_name: &str, protocol_and_hostname: &str, ) -> Result { - let site_view = SiteView::read_local(conn).await?; - let community = Community::read_from_name(conn, community_name, false).await?; + let site_view = SiteView::read_local(&mut *conn).await?; + let community = Community::read_from_name(&mut *conn, community_name, false).await?; let posts = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .sort(Some(*sort_type)) .community_id(Some(community.id)) .limit(Some(*limit)) @@ -307,7 +307,7 @@ async fn get_feed_community( #[tracing::instrument(skip_all)] async fn get_feed_front( - conn: &mut DbConn, + mut conn: impl DbConn, jwt_secret: &str, sort_type: &SortType, limit: &i64, @@ -315,12 +315,12 @@ async fn get_feed_front( jwt: &str, protocol_and_hostname: &str, ) -> Result { - let site_view = SiteView::read_local(conn).await?; + let site_view = SiteView::read_local(&mut *conn).await?; let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub); - let local_user = LocalUser::read(conn, local_user_id).await?; + let local_user = LocalUser::read(&mut *conn, local_user_id).await?; let posts = PostQuery::builder() - .conn(conn) + .conn(&mut *conn) .listing_type(Some(ListingType::Subscribed)) .local_user(Some(&local_user)) .sort(Some(*sort_type)) @@ -348,21 +348,21 @@ async fn get_feed_front( #[tracing::instrument(skip_all)] async fn get_feed_inbox( - conn: &mut DbConn, + mut conn: impl DbConn, jwt_secret: &str, jwt: &str, protocol_and_hostname: &str, ) -> Result { - let site_view = SiteView::read_local(conn).await?; + let site_view = SiteView::read_local(&mut *conn).await?; let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub); - let local_user = LocalUser::read(conn, local_user_id).await?; + let local_user = LocalUser::read(&mut *conn, local_user_id).await?; let person_id = local_user.person_id; let show_bot_accounts = local_user.show_bot_accounts; let sort = CommentSortType::New; let replies = CommentReplyQuery::builder() - .conn(conn) + .conn(&mut *conn) .recipient_id(Some(person_id)) .my_person_id(Some(person_id)) .show_bot_accounts(Some(show_bot_accounts)) @@ -373,7 +373,7 @@ async fn get_feed_inbox( .await?; let mentions = PersonMentionQuery::builder() - .conn(conn) + .conn(&mut *conn) .recipient_id(Some(person_id)) .my_person_id(Some(person_id)) .show_bot_accounts(Some(show_bot_accounts)) diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index 307d6d02a..788191a13 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -129,7 +129,7 @@ async fn full_res( context: web::Data, ) -> Result { // 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(context.conn().await?) .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..d945a6251 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -29,7 +29,7 @@ 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 site_view = SiteView::read_local(context.conn().await?) .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..7bda270ce 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -38,15 +38,14 @@ async fn get_webfinger_response( 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(context.conn().await?, &name_, false) + .await + .ok() + .map(|c| c.actor_id.into()); + let community_id: Option = Community::read_from_name(context.conn().await?, &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. diff --git a/src/code_migrations.rs b/src/code_migrations.rs index 886fa4a5b..ddc96741c 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -40,26 +40,26 @@ use tracing::info; use url::Url; pub async fn run_advanced_migrations( - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { let protocol_and_hostname = &settings.get_protocol_and_hostname(); - user_updates_2020_04_02(conn, protocol_and_hostname).await?; - community_updates_2020_04_02(conn, protocol_and_hostname).await?; - post_updates_2020_04_03(conn, protocol_and_hostname).await?; - comment_updates_2020_04_03(conn, protocol_and_hostname).await?; - private_message_updates_2020_05_05(conn, protocol_and_hostname).await?; - post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname).await?; - apub_columns_2021_02_02(conn).await?; - instance_actor_2022_01_28(conn, protocol_and_hostname).await?; - regenerate_public_keys_2022_07_05(conn).await?; - initialize_local_site_2022_10_10(conn, settings).await?; + user_updates_2020_04_02(&mut *conn, protocol_and_hostname).await?; + community_updates_2020_04_02(&mut *conn, protocol_and_hostname).await?; + post_updates_2020_04_03(&mut *conn, protocol_and_hostname).await?; + comment_updates_2020_04_03(&mut *conn, protocol_and_hostname).await?; + private_message_updates_2020_05_05(&mut *conn, protocol_and_hostname).await?; + post_thumbnail_url_updates_2020_07_27(&mut *conn, protocol_and_hostname).await?; + apub_columns_2021_02_02(&mut *conn).await?; + instance_actor_2022_01_28(&mut *conn, protocol_and_hostname).await?; + regenerate_public_keys_2022_07_05(&mut *conn).await?; + initialize_local_site_2022_10_10(&mut *conn, settings).await?; Ok(()) } async fn user_updates_2020_04_02( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::person::dsl::{actor_id, local, person}; @@ -70,7 +70,7 @@ async fn user_updates_2020_04_02( let incorrect_persons = person .filter(actor_id.like("http://changeme%")) .filter(local.eq(true)) - .load::(conn) + .load::(&mut *conn) .await?; for cperson in &incorrect_persons { @@ -87,7 +87,7 @@ async fn user_updates_2020_04_02( .last_refreshed_at(Some(naive_now())) .build(); - Person::update(conn, cperson.id, &form).await?; + Person::update(&mut *conn, cperson.id, &form).await?; } info!("{} person rows updated.", incorrect_persons.len()); @@ -96,7 +96,7 @@ async fn user_updates_2020_04_02( } async fn community_updates_2020_04_02( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::community::dsl::{actor_id, community, local}; @@ -107,7 +107,7 @@ async fn community_updates_2020_04_02( let incorrect_communities = community .filter(actor_id.like("http://changeme%")) .filter(local.eq(true)) - .load::(conn) + .load::(&mut *conn) .await?; for ccommunity in &incorrect_communities { @@ -125,7 +125,7 @@ async fn community_updates_2020_04_02( .last_refreshed_at(Some(naive_now())) .build(); - Community::update(conn, ccommunity.id, &form).await?; + Community::update(&mut *conn, ccommunity.id, &form).await?; } info!("{} community rows updated.", incorrect_communities.len()); @@ -134,7 +134,7 @@ async fn community_updates_2020_04_02( } async fn post_updates_2020_04_03( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::post::dsl::{ap_id, local, post}; @@ -145,7 +145,7 @@ async fn post_updates_2020_04_03( let incorrect_posts = post .filter(ap_id.like("http://changeme%")) .filter(local.eq(true)) - .load::(conn) + .load::(&mut *conn) .await?; for cpost in &incorrect_posts { @@ -155,7 +155,7 @@ async fn post_updates_2020_04_03( protocol_and_hostname, )?; Post::update( - conn, + &mut *conn, cpost.id, &PostUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -168,7 +168,7 @@ async fn post_updates_2020_04_03( } async fn comment_updates_2020_04_03( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::comment::dsl::{ap_id, comment, local}; @@ -179,7 +179,7 @@ async fn comment_updates_2020_04_03( let incorrect_comments = comment .filter(ap_id.like("http://changeme%")) .filter(local.eq(true)) - .load::(conn) + .load::(&mut *conn) .await?; for ccomment in &incorrect_comments { @@ -189,7 +189,7 @@ async fn comment_updates_2020_04_03( protocol_and_hostname, )?; Comment::update( - conn, + &mut *conn, ccomment.id, &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -202,7 +202,7 @@ async fn comment_updates_2020_04_03( } async fn private_message_updates_2020_05_05( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::private_message::dsl::{ap_id, local, private_message}; @@ -213,7 +213,7 @@ async fn private_message_updates_2020_05_05( let incorrect_pms = private_message .filter(ap_id.like("http://changeme%")) .filter(local.eq(true)) - .load::(conn) + .load::(&mut *conn) .await?; for cpm in &incorrect_pms { @@ -223,7 +223,7 @@ async fn private_message_updates_2020_05_05( protocol_and_hostname, )?; PrivateMessage::update( - conn, + &mut *conn, cpm.id, &PrivateMessageUpdateForm::builder() .ap_id(Some(apub_id)) @@ -238,7 +238,7 @@ async fn private_message_updates_2020_05_05( } async fn post_thumbnail_url_updates_2020_07_27( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::post::dsl::{post, thumbnail_url}; @@ -258,7 +258,7 @@ async fn post_thumbnail_url_updates_2020_07_27( .concat(thumbnail_url), ), ) - .get_results::(conn) + .get_results::(&mut *conn) .await?; info!("{} Post thumbnail_url rows updated.", res.len()); @@ -268,13 +268,13 @@ async fn post_thumbnail_url_updates_2020_07_27( /// We are setting inbox and follower URLs for local and remote actors alike, because for now /// all federated instances are also Lemmy and use the same URL scheme. -async fn apub_columns_2021_02_02(conn: &mut DbConn) -> Result<(), LemmyError> { +async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError> { info!("Running apub_columns_2021_02_02"); { use lemmy_db_schema::schema::person::dsl::{inbox_url, person, shared_inbox_url}; let persons = person .filter(inbox_url.like("http://changeme%")) - .load::(conn) + .load::(&mut *conn) .await?; for p in &persons { @@ -285,7 +285,7 @@ async fn apub_columns_2021_02_02(conn: &mut DbConn) -> Result<(), LemmyError> { inbox_url.eq(inbox_url_), shared_inbox_url.eq(shared_inbox_url_), )) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } } @@ -299,7 +299,7 @@ async fn apub_columns_2021_02_02(conn: &mut DbConn) -> Result<(), LemmyError> { }; let communities = community .filter(inbox_url.like("http://changeme%")) - .load::(conn) + .load::(&mut *conn) .await?; for c in &communities { @@ -312,7 +312,7 @@ async fn apub_columns_2021_02_02(conn: &mut DbConn) -> Result<(), LemmyError> { inbox_url.eq(inbox_url_), shared_inbox_url.eq(shared_inbox_url_), )) - .get_result::(conn) + .get_result::(&mut *conn) .await?; } } @@ -325,11 +325,11 @@ async fn apub_columns_2021_02_02(conn: &mut DbConn) -> Result<(), LemmyError> { /// Before this point, there is only a single value in the site table which refers to the local /// Lemmy instance, so thats all we need to update. async fn instance_actor_2022_01_28( - conn: &mut DbConn, + mut conn: impl DbConn, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { info!("Running instance_actor_2021_09_29"); - if let Ok(site_view) = SiteView::read_local(conn).await { + if let Ok(site_view) = SiteView::read_local(&mut *conn).await { let site = site_view.site; // if site already has public key, we dont need to do anything here if !site.public_key.is_empty() { @@ -344,7 +344,7 @@ async fn instance_actor_2022_01_28( .private_key(Some(Some(key_pair.private_key))) .public_key(Some(key_pair.public_key)) .build(); - Site::update(conn, site.id, &site_form).await?; + Site::update(&mut *conn, site.id, &site_form).await?; } Ok(()) } @@ -354,7 +354,7 @@ async fn instance_actor_2022_01_28( /// key field is empty, generate a new keypair. It would be possible to regenerate only the pubkey, /// but thats more complicated and has no benefit, as federation is already broken for these actors. /// https://github.com/LemmyNet/lemmy/issues/2347 -async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), LemmyError> { +async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(), LemmyError> { info!("Running regenerate_public_keys_2022_07_05"); { @@ -363,7 +363,7 @@ async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), Lemm let communities: Vec = community .filter(local.eq(true)) .filter(public_key.eq("")) - .load::(conn) + .load::(&mut *conn) .await?; for community_ in communities { info!( @@ -375,7 +375,7 @@ async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), Lemm .public_key(Some(key_pair.public_key)) .private_key(Some(Some(key_pair.private_key))) .build(); - Community::update(conn, community_.id, &form).await?; + Community::update(&mut *conn, community_.id, &form).await?; } } @@ -385,7 +385,7 @@ async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), Lemm let persons = person .filter(local.eq(true)) .filter(public_key.eq("")) - .load::(conn) + .load::(&mut *conn) .await?; for person_ in persons { info!( @@ -397,7 +397,7 @@ async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), Lemm .public_key(Some(key_pair.public_key)) .private_key(Some(Some(key_pair.private_key))) .build(); - Person::update(conn, person_.id, &form).await?; + Person::update(&mut *conn, person_.id, &form).await?; } } Ok(()) @@ -408,13 +408,13 @@ async fn regenerate_public_keys_2022_07_05(conn: &mut DbConn) -> Result<(), Lemm /// If a site already exists, the DB migration should generate a local_site row. /// This will only be run for brand new sites. async fn initialize_local_site_2022_10_10( - conn: &mut DbConn, + mut conn: impl DbConn, settings: &Settings, ) -> Result<(), LemmyError> { info!("Running initialize_local_site_2022_10_10"); // Check to see if local_site exists - if LocalSite::read(conn).await.is_ok() { + if LocalSite::read(&mut *conn).await.is_ok() { return Ok(()); } info!("No Local Site found, creating it."); @@ -424,7 +424,7 @@ async fn initialize_local_site_2022_10_10( .expect("must have domain"); // Upsert this to the instance table - let instance = Instance::read_or_create(conn, domain).await?; + let instance = Instance::read_or_create(&mut *conn, domain).await?; if let Some(setup) = &settings.setup { let person_keypair = generate_actor_keypair()?; @@ -445,14 +445,14 @@ async fn initialize_local_site_2022_10_10( .inbox_url(Some(generate_inbox_url(&person_actor_id)?)) .shared_inbox_url(Some(generate_shared_inbox_url(&person_actor_id)?)) .build(); - let person_inserted = Person::create(conn, &person_form).await?; + let person_inserted = Person::create(&mut *conn, &person_form).await?; let local_user_form = LocalUserInsertForm::builder() .person_id(person_inserted.id) .password_encrypted(setup.admin_password.clone()) .email(setup.admin_email.clone()) .build(); - LocalUser::create(conn, &local_user_form).await?; + LocalUser::create(&mut *conn, &local_user_form).await?; }; // Add an entry for the site table @@ -474,14 +474,14 @@ async fn initialize_local_site_2022_10_10( .private_key(Some(site_key_pair.private_key)) .public_key(Some(site_key_pair.public_key)) .build(); - let site = Site::create(conn, &site_form).await?; + let site = Site::create(&mut *conn, &site_form).await?; // Finally create the local_site row let local_site_form = LocalSiteInsertForm::builder() .site_id(site.id) .site_setup(Some(settings.setup.is_some())) .build(); - let local_site = LocalSite::create(conn, &local_site_form).await?; + let local_site = LocalSite::create(&mut *conn, &local_site_form).await?; // Create the rate limit table let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder() @@ -497,7 +497,7 @@ async fn initialize_local_site_2022_10_10( .search(Some(999)) .local_site_id(local_site.id) .build(); - LocalSiteRateLimit::create(conn, &local_site_rate_limit_form).await?; + LocalSiteRateLimit::create(&mut *conn, &local_site_rate_limit_form).await?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index ac02d9c26..037b12324 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,15 +56,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> { let mut conn = get_conn(&pool).await?; // Run the Code-required migrations - run_advanced_migrations(&mut conn, &settings).await?; + run_advanced_migrations(&mut *conn, &settings).await?; // Initialize the secrets - let secret = Secret::init(&mut conn) + let secret = Secret::init(&mut *conn) .await .expect("Couldn't initialize secrets."); // Make sure the local site is set up. - let site_view = SiteView::read_local(&mut conn) + let site_view = SiteView::read_local(&mut *conn) .await .expect("local site not set up"); let local_site = site_view.local_site;