From: dullbananas Date: Tue, 11 Jul 2023 13:09:59 +0000 (-0700) Subject: Make functions work with both connection and pool (#3420) X-Git-Url: http://these/git/ui/assets/static/%7Biframely.thumbnail_url%7D?a=commitdiff_plain;h=1d38aad9d3d51ef606074d5b49a8030c49dd0e9e;p=lemmy.git Make functions work with both connection and pool (#3420) * a lot * merge * Fix stuff broken by merge * Get rid of repetitive `&mut *context.conn().await?` * Add blank lines under each line with `conn =` * Fix style mistakes (partial) * Revert "Fix style mistakes (partial)" This reverts commit 48a033b87f4fdc1ce14ff86cc019e1c703cd2741. * Revert "Add blank lines under each line with `conn =`" This reverts commit 773a6d3beba2cf89eac75913078b40c4f5190dd4. * Revert "Get rid of repetitive `&mut *context.conn().await?`" This reverts commit d2c6263ea13710177d49b2791278db5ad115fca5. * Use DbConn for CaptchaAnswer methods * DbConn trait * Remove more `&mut *` * Fix stuff * Re-run CI * try to make ci start * fix * fix * Fix api_common::utils * Fix apub::activities::block * Fix apub::api::resolve_object * Fix some things * Revert "Fix some things" This reverts commit 2bf8574bc8333d8d34ca542d61a0a5b50039c24d. * Revert "Fix apub::api::resolve_object" This reverts commit 3e4059aabbe485b2ff060bdeced8ef958ff62832. * Revert "Fix apub::activities::block" This reverts commit 3b02389abd780a7b1b8a2c89e26febdaa6a12159. * Revert "Fix api_common::utils" This reverts commit 7dc73de613a5618fa57eb06450f3699bbcb41254. * Revert "Revert "Fix api_common::utils"" This reverts commit f740f115e5457e83e53cc223e48196a2c47a9975. * Revert "Revert "Fix apub::activities::block"" This reverts commit 2ee206af7c885c10092cf209bf4a5b1d60327866. * Revert "Revert "Fix apub::api::resolve_object"" This reverts commit 96ed8bf2e9dcadae760743929498312334e23d2e. * Fix fetch_local_site_data * Fix get_comment_parent_creator * Remove unused perma deleted text * Fix routes::feeds * Fix lib.rs * Update lib.rs * rerun ci * Attempt to create custom GetConn and RunQueryDsl traits * Start over * Add GetConn trait * aaaa * Revert "aaaa" This reverts commit acc9ca1aed10c39efdd91cefece066e035a1fe80. * Revert "Revert "aaaa"" This reverts commit 443a2a00a56d152bb7eb429efd0d29a78e21b163. * still aaaaaaaaaaaaa * Return to earlier thing Revert "Add GetConn trait" This reverts commit ab4e94aea5bd9d34cbcddf017339131047e75344. * Try to use DbPool enum * Revert "Try to use DbPool enum" This reverts commit e4d1712646a52006b865a1fbe0dcf79976fdb027. * DbConn and DbPool enums (db_schema only fails to compile for tests) * fmt * Make functions take `&mut DbPool<'_>` and make db_schema tests compile * Add try_join_with_pool macro and run fix-clippy on more crates * Fix some errors * I did it * Remove function variants that take connection * rerun ci * rerun ci * rerun ci --- diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index 1478ee22..47c23d3d 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -22,18 +22,18 @@ 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(context.pool(), comment_id, None).await?; + let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - context.pool(), + &mut context.pool(), ) .await?; // Verify that only a mod or admin can distinguish a comment is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.person.id, orig_comment.community.id, ) @@ -44,13 +44,13 @@ impl Perform for DistinguishComment { let form = CommentUpdateForm::builder() .distinguished(Some(data.distinguished)) .build(); - Comment::update(context.pool(), comment_id, &form) + Comment::update(&mut context.pool(), comment_id, &form) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; let comment_id = data.comment_id; let person_id = local_user_view.person.id; - let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?; + let comment_view = CommentView::read(&mut context.pool(), 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 f44e03dc..e84c55cb 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let mut recipient_ids = Vec::::new(); @@ -34,20 +34,22 @@ impl Perform for CreateCommentLike { check_downvotes_enabled(data.score, &local_site)?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; + let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - context.pool(), + &mut context.pool(), ) .await?; // Add parent poster or commenter to recipients - let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await; + let comment_reply = CommentReply::read_by_comment(&mut context.pool(), comment_id).await; if let Ok(reply) = comment_reply { let recipient_id = reply.recipient_id; - if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await { + if let Ok(local_recipient) = + LocalUserView::read_person(&mut context.pool(), recipient_id).await + { recipient_ids.push(local_recipient.local_user.id); } } @@ -62,12 +64,12 @@ impl Perform for CreateCommentLike { // Remove any likes first let person_id = local_user_view.person.id; - CommentLike::remove(context.pool(), person_id, comment_id).await?; + CommentLike::remove(&mut context.pool(), 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(context.pool(), &like_form) + CommentLike::like(&mut context.pool(), &like_form) .await .with_lemmy_type(LemmyErrorType::CouldntLikeComment)?; } diff --git a/crates/api/src/comment/save.rs b/crates/api/src/comment/save.rs index 42c91bf0..7161c8e9 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -27,18 +27,18 @@ impl Perform for SaveComment { }; if data.save { - CommentSaved::save(context.pool(), &comment_saved_form) + CommentSaved::save(&mut context.pool(), &comment_saved_form) .await .with_lemmy_type(LemmyErrorType::CouldntSaveComment)?; } else { - CommentSaved::unsave(context.pool(), &comment_saved_form) + CommentSaved::unsave(&mut context.pool(), &comment_saved_form) .await .with_lemmy_type(LemmyErrorType::CouldntSaveComment)?; } let comment_id = data.comment_id; let person_id = local_user_view.person.id; - let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?; + let comment_view = CommentView::read(&mut context.pool(), 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 5c315783..3a89e101 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -27,16 +27,16 @@ 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), comment_id, None).await?; + let comment_view = CommentView::read(&mut context.pool(), comment_id, None).await?; - check_community_ban(person_id, comment_view.community.id, context.pool()).await?; + check_community_ban(person_id, comment_view.community.id, &mut context.pool()).await?; let report_form = CommentReportForm { creator_id: person_id, @@ -45,18 +45,19 @@ impl Perform for CreateCommentReport { reason: reason.to_owned(), }; - let report = CommentReport::report(context.pool(), &report_form) + let report = CommentReport::report(&mut context.pool(), &report_form) .await .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?; - let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?; + let comment_report_view = + CommentReportView::read(&mut context.pool(), report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &comment_report_view.creator.name, &comment_report_view.comment_creator.name, - context.pool(), + &mut context.pool(), context.settings(), ) .await?; diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index d8a4221d..baa1bf45 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -30,7 +30,7 @@ impl Perform for ListCommentReports { let page = data.page; let limit = data.limit; let comment_reports = CommentReportQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 88fd7007..11149527 100644 --- a/crates/api/src/comment_report/resolve.rs +++ b/crates/api/src/comment_report/resolve.rs @@ -24,23 +24,24 @@ impl Perform for ResolveCommentReport { let report_id = data.report_id; let person_id = local_user_view.person.id; - let report = CommentReportView::read(context.pool(), report_id, person_id).await?; + let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?; let person_id = local_user_view.person.id; - is_mod_or_admin(context.pool(), person_id, report.community.id).await?; + is_mod_or_admin(&mut context.pool(), person_id, report.community.id).await?; if data.resolved { - CommentReport::resolve(context.pool(), report_id, person_id) + CommentReport::resolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } else { - CommentReport::unresolve(context.pool(), report_id, person_id) + CommentReport::unresolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } let report_id = data.report_id; - let comment_report_view = CommentReportView::read(context.pool(), report_id, person_id).await?; + let comment_report_view = + CommentReportView::read(&mut context.pool(), 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 2d703577..08620777 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -30,8 +30,8 @@ impl Perform for AddModToCommunity { let community_id = data.community_id; // Verify that only mods or admins can add mod - is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?; - let community = Community::read(context.pool(), community_id).await?; + is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; if local_user_view.person.admin && !community.local { return Err(LemmyErrorType::NotAModerator)?; } @@ -42,11 +42,11 @@ impl Perform for AddModToCommunity { person_id: data.person_id, }; if data.added { - CommunityModerator::join(context.pool(), &community_moderator_form) + CommunityModerator::join(&mut context.pool(), &community_moderator_form) .await .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?; } else { - CommunityModerator::leave(context.pool(), &community_moderator_form) + CommunityModerator::leave(&mut context.pool(), &community_moderator_form) .await .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?; } @@ -59,12 +59,13 @@ impl Perform for AddModToCommunity { removed: Some(!data.added), }; - ModAddCommunity::create(context.pool(), &form).await?; + ModAddCommunity::create(&mut context.pool(), &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(context.pool(), community_id).await?; + let moderators = + CommunityModeratorView::for_community(&mut context.pool(), community_id).await?; Ok(AddModToCommunityResponse { moderators }) } diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 1d3865e1..33f6ef83 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -41,7 +41,7 @@ impl Perform for BanFromCommunity { let expires = data.expires.map(naive_from_unix); // Verify that only mods or admins can ban - is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?; + is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?; is_valid_body_field(&data.reason, false)?; let community_user_ban_form = CommunityPersonBanForm { @@ -51,7 +51,7 @@ impl Perform for BanFromCommunity { }; if data.ban { - CommunityPersonBan::ban(context.pool(), &community_user_ban_form) + CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form) .await .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; @@ -62,18 +62,18 @@ impl Perform for BanFromCommunity { pending: false, }; - CommunityFollower::unfollow(context.pool(), &community_follower_form) + CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) .await .ok(); } else { - CommunityPersonBan::unban(context.pool(), &community_user_ban_form) + CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form) .await .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; } // Remove/Restore their data if that's desired if remove_data { - remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?; + remove_user_data_in_community(community_id, banned_person_id, &mut context.pool()).await?; } // Mod tables @@ -86,10 +86,10 @@ impl Perform for BanFromCommunity { expires, }; - ModBanFromCommunity::create(context.pool(), &form).await?; + ModBanFromCommunity::create(&mut context.pool(), &form).await?; let person_id = data.person_id; - let person_view = PersonView::read(context.pool(), person_id).await?; + let person_view = PersonView::read(&mut context.pool(), person_id).await?; Ok(BanFromCommunityResponse { person_view, diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index 20c601eb..66d6adac 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(context.pool(), &community_block_form) + CommunityBlock::block(&mut context.pool(), &community_block_form) .await .with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?; @@ -46,17 +46,17 @@ impl Perform for BlockCommunity { pending: false, }; - CommunityFollower::unfollow(context.pool(), &community_follower_form) + CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) .await .ok(); } else { - CommunityBlock::unblock(context.pool(), &community_block_form) + CommunityBlock::unblock(&mut context.pool(), &community_block_form) .await .with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?; } let community_view = - CommunityView::read(context.pool(), community_id, Some(person_id), None).await?; + CommunityView::read(&mut context.pool(), community_id, Some(person_id), None).await?; Ok(BlockCommunityResponse { blocked: data.block, diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index 6d3d8285..a2f46eb7 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(context.pool(), community_id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; let mut community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: local_user_view.person.id, @@ -34,22 +34,22 @@ impl Perform for FollowCommunity { if data.follow { if community.local { - check_community_ban(local_user_view.person.id, community_id, context.pool()).await?; - check_community_deleted_or_removed(community_id, context.pool()).await?; + check_community_ban(local_user_view.person.id, community_id, &mut context.pool()).await?; + check_community_deleted_or_removed(community_id, &mut context.pool()).await?; - CommunityFollower::follow(context.pool(), &community_follower_form) + CommunityFollower::follow(&mut context.pool(), &community_follower_form) .await .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?; } else { // Mark as pending, the actual federation activity is sent via `SendActivity` handler community_follower_form.pending = true; - CommunityFollower::follow(context.pool(), &community_follower_form) + CommunityFollower::follow(&mut context.pool(), &community_follower_form) .await .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?; } } if !data.follow { - CommunityFollower::unfollow(context.pool(), &community_follower_form) + CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) .await .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?; } @@ -57,8 +57,8 @@ impl Perform for FollowCommunity { let community_id = data.community_id; let person_id = local_user_view.person.id; let community_view = - CommunityView::read(context.pool(), community_id, Some(person_id), None).await?; - let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?; + CommunityView::read(&mut context.pool(), community_id, Some(person_id), None).await?; + let discussion_languages = CommunityLanguage::read(&mut context.pool(), 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 ecc9d390..313e3d84 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(context.pool(), community_id, &community_form) + Community::update(&mut context.pool(), community_id, &community_form) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunityHiddenStatus)?; - ModHideCommunity::create(context.pool(), &mod_hide_community_form).await?; + ModHideCommunity::create(&mut context.pool(), &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 192db9da..2b6579c4 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -36,7 +36,7 @@ impl Perform for TransferCommunity { // Fetch the community mods let community_id = data.community_id; let mut community_mods = - CommunityModeratorView::for_community(context.pool(), community_id).await?; + CommunityModeratorView::for_community(&mut context.pool(), 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() @@ -57,7 +57,7 @@ impl Perform for TransferCommunity { // Delete all the mods let community_id = data.community_id; - CommunityModerator::delete_for_community(context.pool(), community_id).await?; + CommunityModerator::delete_for_community(&mut context.pool(), community_id).await?; // TODO: this should probably be a bulk operation // Re-add the mods, in the new order @@ -67,7 +67,7 @@ impl Perform for TransferCommunity { person_id: cmod.moderator.id, }; - CommunityModerator::join(context.pool(), &community_moderator_form) + CommunityModerator::join(&mut context.pool(), &community_moderator_form) .await .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?; } @@ -79,16 +79,17 @@ impl Perform for TransferCommunity { community_id: data.community_id, }; - ModTransferCommunity::create(context.pool(), &form).await?; + ModTransferCommunity::create(&mut context.pool(), &form).await?; let community_id = data.community_id; let person_id = local_user_view.person.id; - let community_view = CommunityView::read(context.pool(), community_id, Some(person_id), None) - .await - .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; + let community_view = + CommunityView::read(&mut context.pool(), community_id, Some(person_id), None) + .await + .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; let community_id = data.community_id; - let moderators = CommunityModeratorView::for_community(context.pool(), community_id) + let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id) .await .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index f47d1bd5..988dac27 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -94,6 +94,7 @@ mod tests { #[serial] async fn test_should_not_validate_user_token_after_password_change() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let secret = Secret::init(pool).await.unwrap(); let settings = &SETTINGS.to_owned(); diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index a58174fb..0b478ea9 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -30,7 +30,7 @@ impl Perform for AddAdmin { let added = data.added; let added_person_id = data.person_id; let added_admin = Person::update( - context.pool(), + &mut context.pool(), added_person_id, &PersonUpdateForm::builder().admin(Some(added)).build(), ) @@ -44,9 +44,9 @@ impl Perform for AddAdmin { removed: Some(!data.added), }; - ModAdd::create(context.pool(), &form).await?; + ModAdd::create(&mut context.pool(), &form).await?; - let admins = PersonView::admins(context.pool()).await?; + let admins = PersonView::admins(&mut context.pool()).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 15bc11cc..83768bc2 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -37,7 +37,7 @@ impl Perform for BanPerson { let expires = data.expires.map(naive_from_unix); let person = Person::update( - context.pool(), + &mut context.pool(), banned_person_id, &PersonUpdateForm::builder() .banned(Some(ban)) @@ -52,7 +52,7 @@ impl Perform for BanPerson { if remove_data { remove_user_data( person.id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) @@ -68,10 +68,10 @@ impl Perform for BanPerson { expires, }; - ModBan::create(context.pool(), &form).await?; + ModBan::create(&mut context.pool(), &form).await?; let person_id = data.person_id; - let person_view = PersonView::read(context.pool(), person_id).await?; + let person_view = PersonView::read(&mut context.pool(), 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 67b1f521..604219d4 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(context.pool(), target_id).await?; + let target_person_view = PersonView::read(&mut context.pool(), target_id).await?; if target_person_view.person.admin { return Err(LemmyErrorType::CantBlockAdmin)?; } if data.block { - PersonBlock::block(context.pool(), &person_block_form) + PersonBlock::block(&mut context.pool(), &person_block_form) .await .with_lemmy_type(LemmyErrorType::PersonBlockAlreadyExists)?; } else { - PersonBlock::unblock(context.pool(), &person_block_form) + PersonBlock::unblock(&mut context.pool(), &person_block_form) .await .with_lemmy_type(LemmyErrorType::PersonBlockAlreadyExists)?; } diff --git a/crates/api/src/local_user/change_password.rs b/crates/api/src/local_user/change_password.rs index ffe452c9..ea6f9b3e 100644 --- a/crates/api/src/local_user/change_password.rs +++ b/crates/api/src/local_user/change_password.rs @@ -41,7 +41,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(context.pool(), local_user_id, &new_password).await?; + LocalUser::update_password(&mut context.pool(), 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 b6da6c32..919c250f 100644 --- a/crates/api/src/local_user/change_password_after_reset.rs +++ b/crates/api/src/local_user/change_password_after_reset.rs @@ -25,7 +25,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(context.pool(), &token) + let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token) .await .map(|p| p.local_user_id)?; @@ -38,12 +38,13 @@ impl Perform for PasswordChangeAfterReset { // Update the user with the new password let password = data.password.clone(); - let updated_local_user = LocalUser::update_password(context.pool(), local_user_id, &password) - .await - .with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?; + let updated_local_user = + LocalUser::update_password(&mut context.pool(), local_user_id, &password) + .await + .with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?; // Return the jwt if login is allowed - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).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 92653cfc..5f1079ef 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), &captcha_form).await?; + let captcha = CaptchaAnswer::insert(&mut context.pool(), &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 5e2c027b..9391a4bb 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(context.pool()).await?; + let banned = PersonView::banned(&mut context.pool()).await?; Ok(Self::Response { banned }) } diff --git a/crates/api/src/local_user/login.rs b/crates/api/src/local_user/login.rs index f93ef335..4b5ea0fa 100644 --- a/crates/api/src/local_user/login.rs +++ b/crates/api/src/local_user/login.rs @@ -21,13 +21,14 @@ impl Perform for Login { async fn perform(&self, context: &Data) -> Result { let data: &Login = self; - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; // Fetch that username / email let username_or_email = data.username_or_email.clone(); - let local_user_view = LocalUserView::find_by_email_or_name(context.pool(), &username_or_email) - .await - .with_lemmy_type(LemmyErrorType::IncorrectLogin)?; + let local_user_view = + LocalUserView::find_by_email_or_name(&mut context.pool(), &username_or_email) + .await + .with_lemmy_type(LemmyErrorType::IncorrectLogin)?; // Verify the password let valid: bool = verify( @@ -53,7 +54,8 @@ impl Perform for Login { return Err(LemmyErrorType::EmailNotVerified)?; } - check_registration_application(&local_user_view, &site_view.local_site, context.pool()).await?; + check_registration_application(&local_user_view, &site_view.local_site, &mut context.pool()) + .await?; // Check the totp check_totp_2fa_valid( diff --git a/crates/api/src/local_user/notifications/list_mentions.rs b/crates/api/src/local_user/notifications/list_mentions.rs index 4c8d2cf5..1b2d3c7b 100644 --- a/crates/api/src/local_user/notifications/list_mentions.rs +++ b/crates/api/src/local_user/notifications/list_mentions.rs @@ -28,7 +28,7 @@ impl Perform for GetPersonMentions { let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); let mentions = PersonMentionQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 6d1d8f0b..79b0fe22 100644 --- a/crates/api/src/local_user/notifications/list_replies.rs +++ b/crates/api/src/local_user/notifications/list_replies.rs @@ -25,7 +25,7 @@ impl Perform for GetReplies { let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); let replies = CommentReplyQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 fada6511..d852263e 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(context.pool(), person_id) + CommentReply::mark_all_as_read(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; // Mark all user mentions as read - PersonMention::mark_all_as_read(context.pool(), person_id) + PersonMention::mark_all_as_read(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; // Mark all private_messages as read - PrivateMessage::mark_all_as_read(context.pool(), person_id) + PrivateMessage::mark_all_as_read(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; 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 668286db..35feea64 100644 --- a/crates/api/src/local_user/notifications/mark_mention_read.rs +++ b/crates/api/src/local_user/notifications/mark_mention_read.rs @@ -25,7 +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(context.pool(), person_mention_id).await?; + let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?; if local_user_view.person.id != read_person_mention.recipient_id { return Err(LemmyErrorType::CouldntUpdateComment)?; @@ -34,7 +34,7 @@ impl Perform for MarkPersonMentionAsRead { let person_mention_id = read_person_mention.id; let read = Some(data.read); PersonMention::update( - context.pool(), + &mut context.pool(), person_mention_id, &PersonMentionUpdateForm { read }, ) @@ -44,7 +44,7 @@ impl Perform for MarkPersonMentionAsRead { let person_mention_id = read_person_mention.id; let person_id = local_user_view.person.id; let person_mention_view = - PersonMentionView::read(context.pool(), person_mention_id, Some(person_id)).await?; + PersonMentionView::read(&mut context.pool(), person_mention_id, Some(person_id)).await?; Ok(PersonMentionResponse { person_mention_view, diff --git a/crates/api/src/local_user/notifications/mark_reply_read.rs b/crates/api/src/local_user/notifications/mark_reply_read.rs index 2e338367..4071a466 100644 --- a/crates/api/src/local_user/notifications/mark_reply_read.rs +++ b/crates/api/src/local_user/notifications/mark_reply_read.rs @@ -25,7 +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(context.pool(), comment_reply_id).await?; + let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?; if local_user_view.person.id != read_comment_reply.recipient_id { return Err(LemmyErrorType::CouldntUpdateComment)?; @@ -35,7 +35,7 @@ impl Perform for MarkCommentReplyAsRead { let read = Some(data.read); CommentReply::update( - context.pool(), + &mut context.pool(), comment_reply_id, &CommentReplyUpdateForm { read }, ) @@ -45,7 +45,7 @@ impl Perform for MarkCommentReplyAsRead { let comment_reply_id = read_comment_reply.id; let person_id = local_user_view.person.id; let comment_reply_view = - CommentReplyView::read(context.pool(), comment_reply_id, Some(person_id)).await?; + CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id)).await?; Ok(CommentReplyResponse { comment_reply_view }) } diff --git a/crates/api/src/local_user/notifications/unread_count.rs b/crates/api/src/local_user/notifications/unread_count.rs index 21e584d5..21622ddb 100644 --- a/crates/api/src/local_user/notifications/unread_count.rs +++ b/crates/api/src/local_user/notifications/unread_count.rs @@ -20,12 +20,12 @@ impl Perform for GetUnreadCount { let person_id = local_user_view.person.id; - let replies = CommentReplyView::get_unread_replies(context.pool(), person_id).await?; + let replies = CommentReplyView::get_unread_replies(&mut context.pool(), person_id).await?; - let mentions = PersonMentionView::get_unread_mentions(context.pool(), person_id).await?; + let mentions = PersonMentionView::get_unread_mentions(&mut context.pool(), person_id).await?; let private_messages = - PrivateMessageView::get_unread_messages(context.pool(), person_id).await?; + PrivateMessageView::get_unread_messages(&mut context.pool(), 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 c09280d8..89bedc26 100644 --- a/crates/api/src/local_user/report_count.rs +++ b/crates/api/src/local_user/report_count.rs @@ -25,13 +25,14 @@ impl Perform for GetReportCount { let community_id = data.community_id; let comment_reports = - CommentReportView::get_report_count(context.pool(), person_id, admin, community_id).await?; + CommentReportView::get_report_count(&mut context.pool(), person_id, admin, community_id) + .await?; let post_reports = - PostReportView::get_report_count(context.pool(), person_id, admin, community_id).await?; + PostReportView::get_report_count(&mut context.pool(), person_id, admin, community_id).await?; let private_message_reports = if admin && community_id.is_none() { - Some(PrivateMessageReportView::get_report_count(context.pool()).await?) + Some(PrivateMessageReportView::get_report_count(&mut context.pool()).await?) } else { None }; diff --git a/crates/api/src/local_user/reset_password.rs b/crates/api/src/local_user/reset_password.rs index 0f896f47..348035c2 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -22,13 +22,13 @@ impl Perform for PasswordReset { // Fetch that email let email = data.email.to_lowercase(); - let local_user_view = LocalUserView::find_by_email(context.pool(), &email) + let local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email) .await .with_lemmy_type(LemmyErrorType::IncorrectLogin)?; // Check for too many attempts (to limit potential abuse) let recent_resets_count = PasswordResetRequest::get_recent_password_resets_count( - context.pool(), + &mut context.pool(), local_user_view.local_user.id, ) .await?; @@ -37,7 +37,7 @@ impl Perform for PasswordReset { } // Email the pure token to the user. - send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?; + send_password_reset_email(&local_user_view, &mut context.pool(), context.settings()).await?; Ok(PasswordResetResponse {}) } } diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 578d1773..822f08d2 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(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let avatar = diesel_option_overwrite_to_url(&data.avatar)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -49,8 +49,13 @@ impl Perform for SaveUserSettings { let previous_email = local_user_view.local_user.email.clone().unwrap_or_default(); // Only send the verification email if there was an email change if previous_email.ne(email) { - send_verification_email(&local_user_view, email, context.pool(), context.settings()) - .await?; + send_verification_email( + &local_user_view, + email, + &mut context.pool(), + context.settings(), + ) + .await?; } } @@ -90,12 +95,12 @@ impl Perform for SaveUserSettings { .banner(banner) .build(); - Person::update(context.pool(), person_id, &person_form) + Person::update(&mut context.pool(), person_id, &person_form) .await .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?; if let Some(discussion_languages) = data.discussion_languages.clone() { - LocalUserLanguage::update(context.pool(), discussion_languages, local_user_id).await?; + LocalUserLanguage::update(&mut context.pool(), discussion_languages, local_user_id).await?; } // If generate_totp is Some(false), this will clear it out from the database. @@ -130,7 +135,8 @@ impl Perform for SaveUserSettings { .open_links_in_new_tab(data.open_links_in_new_tab) .build(); - let local_user_res = LocalUser::update(context.pool(), local_user_id, &local_user_form).await; + let local_user_res = + LocalUser::update(&mut context.pool(), 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 2a0b2f3c..a0c4763a 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(context.pool(), &token) + let verification = EmailVerification::read_for_token(&mut context.pool(), &token) .await .with_lemmy_type(LemmyErrorType::TokenNotFound)?; @@ -31,9 +31,9 @@ impl Perform for VerifyEmail { .build(); let local_user_id = verification.local_user_id; - LocalUser::update(context.pool(), local_user_id, &form).await?; + LocalUser::update(&mut context.pool(), local_user_id, &form).await?; - EmailVerification::delete_old_tokens_for_local_user(context.pool(), local_user_id).await?; + EmailVerification::delete_old_tokens_for_local_user(&mut context.pool(), local_user_id).await?; Ok(VerifyEmailResponse {}) } diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index 3c9e1a1c..c59eba31 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -32,20 +32,20 @@ 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(context.pool(), post_id).await?; + let orig_post = Post::read(&mut context.pool(), post_id).await?; check_community_ban( local_user_view.person.id, orig_post.community_id, - context.pool(), + &mut context.pool(), ) .await?; - check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?; if data.feature_type == PostFeatureType::Community { // Verify that only the mods can feature in community is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.person.id, orig_post.community_id, ) @@ -65,7 +65,7 @@ impl Perform for FeaturePost { .featured_local(Some(data.featured)) .build() }; - Post::update(context.pool(), post_id, &new_post).await?; + Post::update(&mut context.pool(), 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(context.pool(), &form).await?; + ModFeaturePost::create(&mut context.pool(), &form).await?; build_post_response( context, diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index 896ab5ba..025129d3 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -29,17 +29,22 @@ 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), post_id).await?; - check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?; - check_community_deleted_or_removed(post.community_id, context.pool()).await?; + check_community_ban( + local_user_view.person.id, + post.community_id, + &mut context.pool(), + ) + .await?; + check_community_deleted_or_removed(post.community_id, &mut context.pool()).await?; let like_form = PostLikeForm { post_id: data.post_id, @@ -50,18 +55,18 @@ impl Perform for CreatePostLike { // Remove any likes first let person_id = local_user_view.person.id; - PostLike::remove(context.pool(), person_id, post_id).await?; + PostLike::remove(&mut context.pool(), 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(context.pool(), &like_form) + PostLike::like(&mut context.pool(), &like_form) .await .with_lemmy_type(LemmyErrorType::CouldntLikePost)?; } // Mark the post as read - mark_post_as_read(person_id, post_id, context.pool()).await?; + mark_post_as_read(person_id, post_id, &mut context.pool()).await?; build_post_response( context, diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index 5266d544..627e9d8e 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -30,19 +30,19 @@ 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(context.pool(), post_id).await?; + let orig_post = Post::read(&mut context.pool(), post_id).await?; check_community_ban( local_user_view.person.id, orig_post.community_id, - context.pool(), + &mut context.pool(), ) .await?; - check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?; // Verify that only the mods can lock is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.person.id, orig_post.community_id, ) @@ -52,7 +52,7 @@ impl Perform for LockPost { let post_id = data.post_id; let locked = data.locked; Post::update( - context.pool(), + &mut context.pool(), post_id, &PostUpdateForm::builder().locked(Some(locked)).build(), ) @@ -64,7 +64,7 @@ impl Perform for LockPost { post_id: data.post_id, locked: Some(locked), }; - ModLockPost::create(context.pool(), &form).await?; + ModLockPost::create(&mut context.pool(), &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 0e013a88..0b777a7e 100644 --- a/crates/api/src/post/mark_read.rs +++ b/crates/api/src/post/mark_read.rs @@ -22,13 +22,13 @@ impl Perform for MarkPostAsRead { // Mark the post as read / unread if data.read { - mark_post_as_read(person_id, post_id, context.pool()).await?; + mark_post_as_read(person_id, post_id, &mut context.pool()).await?; } else { - mark_post_as_unread(person_id, post_id, context.pool()).await?; + mark_post_as_unread(person_id, post_id, &mut context.pool()).await?; } // Fetch it - let post_view = PostView::read(context.pool(), post_id, Some(person_id), None).await?; + let post_view = PostView::read(&mut context.pool(), 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 bc43610a..aa5d0c10 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -27,21 +27,21 @@ impl Perform for SavePost { }; if data.save { - PostSaved::save(context.pool(), &post_saved_form) + PostSaved::save(&mut context.pool(), &post_saved_form) .await .with_lemmy_type(LemmyErrorType::CouldntSavePost)?; } else { - PostSaved::unsave(context.pool(), &post_saved_form) + PostSaved::unsave(&mut context.pool(), &post_saved_form) .await .with_lemmy_type(LemmyErrorType::CouldntSavePost)?; } let post_id = data.post_id; let person_id = local_user_view.person.id; - let post_view = PostView::read(context.pool(), post_id, Some(person_id), None).await?; + let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), None).await?; // Mark the post as read - mark_post_as_read(person_id, post_id, context.pool()).await?; + mark_post_as_read(person_id, post_id, &mut context.pool()).await?; Ok(PostResponse { post_view }) } diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index 0915a0a8..16c994d3 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -24,16 +24,16 @@ 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), post_id, None, None).await?; + let post_view = PostView::read(&mut context.pool(), post_id, None, None).await?; - check_community_ban(person_id, post_view.community.id, context.pool()).await?; + check_community_ban(person_id, post_view.community.id, &mut context.pool()).await?; let report_form = PostReportForm { creator_id: person_id, @@ -44,18 +44,18 @@ impl Perform for CreatePostReport { reason: reason.to_owned(), }; - let report = PostReport::report(context.pool(), &report_form) + let report = PostReport::report(&mut context.pool(), &report_form) .await .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?; - let post_report_view = PostReportView::read(context.pool(), report.id, person_id).await?; + let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &post_report_view.creator.name, &post_report_view.post_creator.name, - context.pool(), + &mut context.pool(), context.settings(), ) .await?; diff --git a/crates/api/src/post_report/list.rs b/crates/api/src/post_report/list.rs index d8473f73..f7496e1a 100644 --- a/crates/api/src/post_report/list.rs +++ b/crates/api/src/post_report/list.rs @@ -30,7 +30,7 @@ impl Perform for ListPostReports { let page = data.page; let limit = data.limit; let post_reports = PostReportQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 91243c09..7500a53b 100644 --- a/crates/api/src/post_report/resolve.rs +++ b/crates/api/src/post_report/resolve.rs @@ -21,22 +21,22 @@ impl Perform for ResolvePostReport { let report_id = data.report_id; let person_id = local_user_view.person.id; - let report = PostReportView::read(context.pool(), report_id, person_id).await?; + let report = PostReportView::read(&mut context.pool(), report_id, person_id).await?; let person_id = local_user_view.person.id; - is_mod_or_admin(context.pool(), person_id, report.community.id).await?; + is_mod_or_admin(&mut context.pool(), person_id, report.community.id).await?; if data.resolved { - PostReport::resolve(context.pool(), report_id, person_id) + PostReport::resolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } else { - PostReport::unresolve(context.pool(), report_id, person_id) + PostReport::unresolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } - let post_report_view = PostReportView::read(context.pool(), report_id, person_id).await?; + let post_report_view = PostReportView::read(&mut context.pool(), 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 bb81a62c..cca37fde 100644 --- a/crates/api/src/private_message/mark_read.rs +++ b/crates/api/src/private_message/mark_read.rs @@ -26,7 +26,8 @@ impl Perform for MarkPrivateMessageAsRead { // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?; + let orig_private_message = + PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.recipient_id { return Err(LemmyErrorType::CouldntUpdatePrivateMessage)?; } @@ -35,14 +36,14 @@ impl Perform for MarkPrivateMessageAsRead { let private_message_id = data.private_message_id; let read = data.read; PrivateMessage::update( - context.pool(), + &mut context.pool(), private_message_id, &PrivateMessageUpdateForm::builder().read(Some(read)).build(), ) .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; - let view = PrivateMessageView::read(context.pool(), private_message_id).await?; + let view = PrivateMessageView::read(&mut context.pool(), 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 d732b41a..88511bcf 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -23,14 +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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), private_message_id).await?; + let private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; let report_form = PrivateMessageReportForm { creator_id: person_id, @@ -39,19 +39,19 @@ impl Perform for CreatePrivateMessageReport { reason: reason.to_owned(), }; - let report = PrivateMessageReport::report(context.pool(), &report_form) + let report = PrivateMessageReport::report(&mut context.pool(), &report_form) .await .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?; let private_message_report_view = - PrivateMessageReportView::read(context.pool(), report.id).await?; + PrivateMessageReportView::read(&mut context.pool(), report.id).await?; // Email the admins if local_site.reports_email_admins { send_new_report_email_to_admins( &private_message_report_view.creator.name, &private_message_report_view.private_message_creator.name, - context.pool(), + &mut context.pool(), context.settings(), ) .await?; diff --git a/crates/api/src/private_message_report/list.rs b/crates/api/src/private_message_report/list.rs index 62c07325..72d182d4 100644 --- a/crates/api/src/private_message_report/list.rs +++ b/crates/api/src/private_message_report/list.rs @@ -22,7 +22,7 @@ impl Perform for ListPrivateMessageReports { let page = self.page; let limit = self.limit; let private_message_reports = PrivateMessageReportQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 96461053..be346bb2 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(context.pool(), report_id, person_id) + PrivateMessageReport::resolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } else { - PrivateMessageReport::unresolve(context.pool(), report_id, person_id) + PrivateMessageReport::unresolve(&mut context.pool(), report_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } let private_message_report_view = - PrivateMessageReportView::read(context.pool(), report_id).await?; + PrivateMessageReportView::read(&mut context.pool(), 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 0d2b83bc..43383e06 100644 --- a/crates/api/src/site/federated_instances.rs +++ b/crates/api/src/site/federated_instances.rs @@ -14,9 +14,9 @@ impl Perform for GetFederatedInstances { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let federated_instances = - build_federated_instances(&site_view.local_site, context.pool()).await?; + build_federated_instances(&site_view.local_site, &mut context.pool()).await?; Ok(Self::Response { federated_instances, diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index d2368380..3781c73f 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -34,14 +34,14 @@ 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(context.pool()).await?; + let admins = PersonView::admins(&mut context.pool()).await?; if admins.len() == 1 { return Err(LemmyErrorType::CannotLeaveAdmin)?; } let person_id = local_user_view.person.id; Person::update( - context.pool(), + &mut context.pool(), person_id, &PersonUpdateForm::builder().admin(Some(false)).build(), ) @@ -54,16 +54,17 @@ impl Perform for LeaveAdmin { removed: Some(true), }; - ModAdd::create(context.pool(), &form).await?; + ModAdd::create(&mut context.pool(), &form).await?; // Reread site and admins - let site_view = SiteView::read_local(context.pool()).await?; - let admins = PersonView::admins(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; + let admins = PersonView::admins(&mut context.pool()).await?; - let all_languages = Language::read_all(context.pool()).await?; - let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?; - let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?; - let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?; + let all_languages = Language::read_all(&mut context.pool()).await?; + let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; + let taglines = Tagline::get_all(&mut context.pool(), site_view.local_site.id).await?; + let custom_emojis = + CustomEmojiView::get_all(&mut context.pool(), 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 2ca72515..a0668980 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; @@ -56,7 +56,7 @@ impl Perform for GetModlog { None => CommunityId(-1), }; let is_mod_of_community = data.community_id.is_some() - && is_mod_or_admin(context.pool(), local_person_id, community_id_value) + && is_mod_or_admin(&mut context.pool(), local_person_id, community_id_value) .await .is_ok(); let hide_modlog_names = local_site.hide_modlog_mod_names && !is_mod_of_community && !is_admin; @@ -76,43 +76,47 @@ impl Perform for GetModlog { hide_modlog_names, }; let removed_posts = match type_ { - All | ModRemovePost => ModRemovePostView::list(context.pool(), params).await?, + All | ModRemovePost => ModRemovePostView::list(&mut context.pool(), params).await?, _ => Default::default(), }; let locked_posts = match type_ { - All | ModLockPost => ModLockPostView::list(context.pool(), params).await?, + All | ModLockPost => ModLockPostView::list(&mut context.pool(), params).await?, _ => Default::default(), }; let featured_posts = match type_ { - All | ModFeaturePost => ModFeaturePostView::list(context.pool(), params).await?, + All | ModFeaturePost => ModFeaturePostView::list(&mut context.pool(), params).await?, _ => Default::default(), }; let removed_comments = match type_ { - All | ModRemoveComment => ModRemoveCommentView::list(context.pool(), params).await?, + All | ModRemoveComment => ModRemoveCommentView::list(&mut context.pool(), params).await?, _ => Default::default(), }; let banned_from_community = match type_ { - All | ModBanFromCommunity => ModBanFromCommunityView::list(context.pool(), params).await?, + All | ModBanFromCommunity => { + ModBanFromCommunityView::list(&mut context.pool(), params).await? + } _ => Default::default(), }; let added_to_community = match type_ { - All | ModAddCommunity => ModAddCommunityView::list(context.pool(), params).await?, + All | ModAddCommunity => ModAddCommunityView::list(&mut context.pool(), params).await?, _ => Default::default(), }; let transferred_to_community = match type_ { - All | ModTransferCommunity => ModTransferCommunityView::list(context.pool(), params).await?, + All | ModTransferCommunity => { + ModTransferCommunityView::list(&mut context.pool(), params).await? + } _ => Default::default(), }; let hidden_communities = match type_ { All | ModHideCommunity if other_person_id.is_none() => { - ModHideCommunityView::list(context.pool(), params).await? + ModHideCommunityView::list(&mut context.pool(), params).await? } _ => Default::default(), }; @@ -129,40 +133,40 @@ impl Perform for GetModlog { ) = if data.community_id.is_none() { ( match type_ { - All | ModBan => ModBanView::list(context.pool(), params).await?, + All | ModBan => ModBanView::list(&mut context.pool(), params).await?, _ => Default::default(), }, match type_ { - All | ModAdd => ModAddView::list(context.pool(), params).await?, + All | ModAdd => ModAddView::list(&mut context.pool(), params).await?, _ => Default::default(), }, match type_ { All | ModRemoveCommunity if other_person_id.is_none() => { - ModRemoveCommunityView::list(context.pool(), params).await? + ModRemoveCommunityView::list(&mut context.pool(), params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePerson if other_person_id.is_none() => { - AdminPurgePersonView::list(context.pool(), params).await? + AdminPurgePersonView::list(&mut context.pool(), params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeCommunity if other_person_id.is_none() => { - AdminPurgeCommunityView::list(context.pool(), params).await? + AdminPurgeCommunityView::list(&mut context.pool(), params).await? } _ => Default::default(), }, match type_ { All | AdminPurgePost if other_person_id.is_none() => { - AdminPurgePostView::list(context.pool(), params).await? + AdminPurgePostView::list(&mut context.pool(), params).await? } _ => Default::default(), }, match type_ { All | AdminPurgeComment if other_person_id.is_none() => { - AdminPurgeCommentView::list(context.pool(), params).await? + AdminPurgeCommentView::list(&mut context.pool(), params).await? } _ => Default::default(), }, diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 7beba9c0..9334961e 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(context.pool(), comment_id).await?; + let comment = Comment::read(&mut context.pool(), comment_id).await?; let post_id = comment.post_id; // TODO read comments for pictrs images and purge them - Comment::delete(context.pool(), comment_id).await?; + Comment::delete(&mut context.pool(), comment_id).await?; // Mod tables let reason = data.reason.clone(); @@ -45,7 +45,7 @@ impl Perform for PurgeComment { post_id, }; - AdminPurgeComment::create(context.pool(), &form).await?; + AdminPurgeComment::create(&mut context.pool(), &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 50482b73..56e75717 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(context.pool(), community_id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; if let Some(banner) = community.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -46,13 +46,13 @@ impl Perform for PurgeCommunity { purge_image_posts_for_community( community_id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) .await?; - Community::delete(context.pool(), community_id).await?; + Community::delete(&mut context.pool(), community_id).await?; // Mod tables let reason = data.reason.clone(); @@ -61,7 +61,7 @@ impl Perform for PurgeCommunity { reason, }; - AdminPurgeCommunity::create(context.pool(), &form).await?; + AdminPurgeCommunity::create(&mut context.pool(), &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 f0cbc7e8..fa884147 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(context.pool(), person_id).await?; + let person = Person::read(&mut context.pool(), person_id).await?; if let Some(banner) = person.banner { purge_image_from_pictrs(context.client(), context.settings(), &banner) @@ -45,13 +45,13 @@ impl Perform for PurgePerson { purge_image_posts_for_person( person_id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) .await?; - Person::delete(context.pool(), person_id).await?; + Person::delete(&mut context.pool(), person_id).await?; // Mod tables let reason = data.reason.clone(); @@ -60,7 +60,7 @@ impl Perform for PurgePerson { reason, }; - AdminPurgePerson::create(context.pool(), &form).await?; + AdminPurgePerson::create(&mut context.pool(), &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 65d390f8..6824e408 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(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), 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(context.pool(), post_id).await?; + Post::delete(&mut context.pool(), post_id).await?; // Mod tables let reason = data.reason.clone(); @@ -57,7 +57,7 @@ impl Perform for PurgePost { community_id, }; - AdminPurgePost::create(context.pool(), &form).await?; + AdminPurgePost::create(&mut context.pool(), &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 b153bd2a..1a8521ca 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(context.pool(), app_id, &app_form).await?; + RegistrationApplication::update(&mut context.pool(), app_id, &app_form).await?; // Update the local_user row let local_user_form = LocalUserUpdateForm::builder() @@ -45,10 +45,11 @@ impl Perform for ApproveRegistrationApplication { .build(); let approved_user_id = registration_application.local_user_id; - LocalUser::update(context.pool(), approved_user_id, &local_user_form).await?; + LocalUser::update(&mut context.pool(), approved_user_id, &local_user_form).await?; if data.approve { - let approved_local_user_view = LocalUserView::read(context.pool(), approved_user_id).await?; + let approved_local_user_view = + LocalUserView::read(&mut context.pool(), approved_user_id).await?; if approved_local_user_view.local_user.email.is_some() { send_application_approved_email(&approved_local_user_view, context.settings()).await?; @@ -57,7 +58,7 @@ impl Perform for ApproveRegistrationApplication { // Read the view let registration_application = - RegistrationApplicationView::read(context.pool(), app_id).await?; + RegistrationApplicationView::read(&mut context.pool(), 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 6a359957..2389ad40 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; // Make sure user is an admin is_admin(&local_user_view)?; @@ -28,7 +28,7 @@ impl Perform for ListRegistrationApplications { let page = data.page; let limit = data.limit; let registration_applications = RegistrationApplicationQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 aed07c70..593e93ff 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; // Only let admins do this is_admin(&local_user_view)?; @@ -24,7 +24,8 @@ impl Perform for GetUnreadRegistrationApplicationCount { let verified_email_only = local_site.require_email_verification; let registration_applications = - RegistrationApplicationView::get_unread_count(context.pool(), verified_email_only).await?; + RegistrationApplicationView::get_unread_count(&mut context.pool(), verified_email_only) + .await?; Ok(Self::Response { registration_applications, diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 217e05de..8b96206c 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(context.pool(), comment_id, person_id).await?; + let comment_view = CommentView::read(&mut context.pool(), comment_id, person_id).await?; Ok(CommentResponse { comment_view, recipient_ids, @@ -43,18 +43,19 @@ pub async fn build_community_response( local_user_view: LocalUserView, community_id: CommunityId, ) -> Result { - let is_mod_or_admin = is_mod_or_admin(context.pool(), local_user_view.person.id, community_id) - .await - .is_ok(); + let is_mod_or_admin = + is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id) + .await + .is_ok(); let person_id = local_user_view.person.id; let community_view = CommunityView::read( - context.pool(), + &mut context.pool(), community_id, Some(person_id), Some(is_mod_or_admin), ) .await?; - let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?; + let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?; Ok(CommunityResponse { community_view, @@ -68,11 +69,11 @@ pub async fn build_post_response( person_id: PersonId, post_id: PostId, ) -> Result { - let is_mod_or_admin = is_mod_or_admin(context.pool(), person_id, community_id) + let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), person_id, community_id) .await .is_ok(); let post_view = PostView::read( - context.pool(), + &mut context.pool(), post_id, Some(person_id), Some(is_mod_or_admin), @@ -100,7 +101,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(context.pool(), &mention_name).await; + let user_view = LocalUserView::read_from_name(&mut context.pool(), &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 @@ -115,7 +116,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(context.pool(), &user_mention_form) + PersonMention::create(&mut context.pool(), &user_mention_form) .await .ok(); @@ -135,19 +136,19 @@ pub async fn send_local_notifs( // Send comment_reply to the parent commenter / poster if let Some(parent_comment_id) = comment.parent_comment_id() { - let parent_comment = Comment::read(context.pool(), parent_comment_id).await?; + let parent_comment = Comment::read(&mut context.pool(), parent_comment_id).await?; // Get the parent commenter local_user let parent_creator_id = parent_comment.creator_id; // Only add to recipients if that person isn't blocked - let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool()) + let creator_blocked = check_person_block(person.id, parent_creator_id, &mut context.pool()) .await .is_err(); // Don't send a notif to yourself if parent_comment.creator_id != person.id && !creator_blocked { - let user_view = LocalUserView::read_person(context.pool(), parent_creator_id).await; + let user_view = LocalUserView::read_person(&mut context.pool(), parent_creator_id).await; if let Ok(parent_user_view) = user_view { recipient_ids.push(parent_user_view.local_user.id); @@ -159,7 +160,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(context.pool(), &comment_reply_form) + CommentReply::create(&mut context.pool(), &comment_reply_form) .await .ok(); @@ -178,13 +179,13 @@ pub async fn send_local_notifs( } else { // If there's no parent, its the post creator // Only add to recipients if that person isn't blocked - let creator_blocked = check_person_block(person.id, post.creator_id, context.pool()) + let creator_blocked = check_person_block(person.id, post.creator_id, &mut context.pool()) .await .is_err(); if post.creator_id != person.id && !creator_blocked { let creator_id = post.creator_id; - let parent_user = LocalUserView::read_person(context.pool(), creator_id).await; + let parent_user = LocalUserView::read_person(&mut context.pool(), creator_id).await; if let Ok(parent_user_view) = parent_user { recipient_ids.push(parent_user_view.local_user.id); @@ -196,7 +197,7 @@ pub async fn send_local_notifs( // Allow this to fail softly, since comment edits might re-update or replace it // Let the uniqueness handle this fail - CommentReply::create(context.pool(), &comment_reply_form) + CommentReply::create(&mut context.pool(), &comment_reply_form) .await .ok(); diff --git a/crates/api_common/src/context.rs b/crates/api_common/src/context.rs index 42fa7496..0d448ef9 100644 --- a/crates/api_common/src/context.rs +++ b/crates/api_common/src/context.rs @@ -1,4 +1,7 @@ -use lemmy_db_schema::{source::secret::Secret, utils::DbPool}; +use lemmy_db_schema::{ + source::secret::Secret, + utils::{ActualDbPool, DbPool}, +}; use lemmy_utils::{ rate_limit::RateLimitCell, settings::{structs::Settings, SETTINGS}, @@ -8,7 +11,7 @@ use std::sync::Arc; #[derive(Clone)] pub struct LemmyContext { - pool: DbPool, + pool: ActualDbPool, client: Arc, secret: Arc, rate_limit_cell: RateLimitCell, @@ -16,7 +19,7 @@ pub struct LemmyContext { impl LemmyContext { pub fn create( - pool: DbPool, + pool: ActualDbPool, client: ClientWithMiddleware, secret: Secret, rate_limit_cell: RateLimitCell, @@ -28,7 +31,10 @@ impl LemmyContext { rate_limit_cell, } } - pub fn pool(&self) -> &DbPool { + pub fn pool(&self) -> DbPool<'_> { + DbPool::Pool(&self.pool) + } + pub fn inner_pool(&self) -> &ActualDbPool { &self.pool } pub fn client(&self) -> &ClientWithMiddleware { diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 793cc70c..bd194c0f 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -6,7 +6,6 @@ use crate::{ }; use anyhow::Context; use chrono::NaiveDateTime; -use futures::try_join; use lemmy_db_schema::{ impls::person::is_banned, newtypes::{CommunityId, DbUrl, LocalUserId, PersonId, PostId}, @@ -50,7 +49,7 @@ use url::{ParseError, Url}; #[tracing::instrument(skip_all)] pub async fn is_mod_or_admin( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id: PersonId, community_id: CommunityId, ) -> Result<(), LemmyError> { @@ -63,7 +62,7 @@ pub async fn is_mod_or_admin( #[tracing::instrument(skip_all)] pub async fn is_mod_or_admin_opt( - pool: &DbPool, + pool: &mut DbPool<'_>, local_user_view: Option<&LocalUserView>, community_id: Option, ) -> Result<(), LemmyError> { @@ -101,7 +100,7 @@ pub fn is_top_mod( } #[tracing::instrument(skip_all)] -pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result { +pub async fn get_post(post_id: PostId, pool: &mut DbPool<'_>) -> Result { Post::read(pool, post_id) .await .with_lemmy_type(LemmyErrorType::CouldntFindPost) @@ -111,7 +110,7 @@ pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result, ) -> Result { let post_read_form = PostReadForm { post_id, person_id }; @@ -124,7 +123,7 @@ pub async fn mark_post_as_read( pub async fn mark_post_as_unread( person_id: PersonId, post_id: PostId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result { let post_read_form = PostReadForm { post_id, person_id }; @@ -142,7 +141,7 @@ pub async fn local_user_view_from_jwt( .with_lemmy_type(LemmyErrorType::NotLoggedIn)? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(context.pool(), local_user_id).await?; + let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id).await?; check_user_valid( local_user_view.person.banned, local_user_view.person.ban_expires, @@ -197,7 +196,7 @@ pub fn check_user_valid( pub async fn check_community_ban( person_id: PersonId, community_id: CommunityId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { let is_banned = CommunityPersonBanView::get(pool, person_id, community_id) .await @@ -212,7 +211,7 @@ pub async fn check_community_ban( #[tracing::instrument(skip_all)] pub async fn check_community_deleted_or_removed( community_id: CommunityId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { let community = Community::read(pool, community_id) .await @@ -236,7 +235,7 @@ pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> { pub async fn check_person_block( my_id: PersonId, potential_blocker_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { let is_blocked = PersonBlock::read(pool, potential_blocker_id, my_id) .await @@ -270,15 +269,15 @@ pub fn check_private_instance( #[tracing::instrument(skip_all)] pub async fn build_federated_instances( local_site: &LocalSite, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { if local_site.federation_enabled { // TODO I hate that this requires 3 queries - let (linked, allowed, blocked) = try_join!( - Instance::linked(pool), - Instance::allowlist(pool), - Instance::blocklist(pool) - )?; + let (linked, allowed, blocked) = lemmy_db_schema::try_join_with_pool!(pool => ( + Instance::linked, + Instance::allowlist, + Instance::blocklist + ))?; Ok(Some(FederatedInstances { linked, @@ -336,7 +335,7 @@ pub async fn send_email_to_user( pub async fn send_password_reset_email( user: &LocalUserView, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, ) -> Result<(), LemmyError> { // Generate a random token @@ -360,7 +359,7 @@ pub async fn send_password_reset_email( pub async fn send_verification_email( user: &LocalUserView, new_email: &str, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, ) -> Result<(), LemmyError> { let form = EmailVerificationForm { @@ -451,7 +450,7 @@ pub async 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, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails @@ -476,7 +475,7 @@ pub async fn send_new_applicant_email_to_admins( pub async fn send_new_report_email_to_admins( reporter_username: &str, reported_username: &str, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails @@ -497,7 +496,7 @@ pub async fn send_new_report_email_to_admins( pub async fn check_registration_application( local_user_view: &LocalUserView, local_site: &LocalSite, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { if (local_site.registration_mode == RegistrationMode::RequireApplication || local_site.registration_mode == RegistrationMode::Closed) @@ -531,7 +530,7 @@ pub fn check_private_instance_and_federation_enabled( pub async fn purge_image_posts_for_person( banned_person_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { @@ -554,7 +553,7 @@ pub async fn purge_image_posts_for_person( pub async fn purge_image_posts_for_community( banned_community_id: CommunityId, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { @@ -577,7 +576,7 @@ pub async fn purge_image_posts_for_community( pub async fn remove_user_data( banned_person_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { @@ -661,7 +660,7 @@ pub async fn remove_user_data( pub async fn remove_user_data_in_community( community_id: CommunityId, banned_person_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { // Posts Post::update_removed_for_creator(pool, banned_person_id, Some(community_id), true).await?; @@ -691,7 +690,7 @@ pub async fn remove_user_data_in_community( pub async fn delete_user_account( person_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, client: &ClientWithMiddleware, ) -> Result<(), LemmyError> { diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index c227e84d..92775772 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -44,7 +44,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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -54,11 +54,11 @@ impl PerformCrud for CreateComment { // Check for a community ban let post_id = data.post_id; - let post = get_post(post_id, context.pool()).await?; + let post = get_post(post_id, &mut context.pool()).await?; let community_id = post.community_id; - check_community_ban(local_user_view.person.id, community_id, context.pool()).await?; - check_community_deleted_or_removed(community_id, context.pool()).await?; + check_community_ban(local_user_view.person.id, community_id, &mut context.pool()).await?; + check_community_deleted_or_removed(community_id, &mut context.pool()).await?; check_post_deleted_or_removed(&post)?; // Check if post is locked, no new comments @@ -68,7 +68,7 @@ impl PerformCrud for CreateComment { // Fetch the parent, if it exists let parent_opt = if let Some(parent_id) = data.parent_id { - Comment::read(context.pool(), parent_id).await.ok() + Comment::read(&mut context.pool(), parent_id).await.ok() } else { None }; @@ -90,7 +90,7 @@ impl PerformCrud for CreateComment { let language_id = data.language_id.unwrap_or(parent_language); CommunityLanguage::is_allowed_community_language( - context.pool(), + &mut context.pool(), Some(language_id), community_id, ) @@ -105,9 +105,10 @@ impl PerformCrud for CreateComment { // Create the comment let parent_path = parent_opt.clone().map(|t| t.path); - let inserted_comment = Comment::create(context.pool(), &comment_form, parent_path.as_ref()) - .await - .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?; + let inserted_comment = + Comment::create(&mut context.pool(), &comment_form, parent_path.as_ref()) + .await + .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?; // Necessary to update the ap_id let inserted_comment_id = inserted_comment.id; @@ -119,7 +120,7 @@ impl PerformCrud for CreateComment { &protocol_and_hostname, )?; let updated_comment = Comment::update( - context.pool(), + &mut context.pool(), inserted_comment_id, &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -146,17 +147,17 @@ impl PerformCrud for CreateComment { score: 1, }; - CommentLike::like(context.pool(), &like_form) + CommentLike::like(&mut context.pool(), &like_form) .await .with_lemmy_type(LemmyErrorType::CouldntLikeComment)?; // 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(context.pool(), parent_id).await; + let comment_reply = CommentReply::read_by_comment(&mut context.pool(), parent_id).await; if let Ok(reply) = comment_reply { CommentReply::update( - context.pool(), + &mut context.pool(), reply.id, &CommentReplyUpdateForm { read: Some(true) }, ) @@ -167,10 +168,10 @@ impl PerformCrud for CreateComment { // If the parent has PersonMentions mark them as read too let person_id = local_user_view.person.id; let person_mention = - PersonMention::read_by_comment_and_person(context.pool(), parent_id, person_id).await; + PersonMention::read_by_comment_and_person(&mut context.pool(), parent_id, person_id).await; if let Ok(mention) = person_mention { PersonMention::update( - context.pool(), + &mut context.pool(), mention.id, &PersonMentionUpdateForm { read: Some(true) }, ) diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index da2403bc..c42924de 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(context.pool(), comment_id, None).await?; + let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; // Dont delete it if its already been deleted. if orig_comment.comment.deleted == data.deleted { @@ -36,7 +36,7 @@ impl PerformCrud for DeleteComment { check_community_ban( local_user_view.person.id, orig_comment.community.id, - context.pool(), + &mut context.pool(), ) .await?; @@ -48,7 +48,7 @@ impl PerformCrud for DeleteComment { // Do the delete let deleted = data.deleted; let updated_comment = Comment::update( - context.pool(), + &mut context.pool(), comment_id, &CommentUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -56,7 +56,7 @@ impl PerformCrud for DeleteComment { .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; let post_id = updated_comment.post_id; - let post = Post::read(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), 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 2ae71973..e6899fdc 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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 21cb3c8a..e87eb425 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -27,18 +27,18 @@ 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(context.pool(), comment_id, None).await?; + let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - context.pool(), + &mut context.pool(), ) .await?; // Verify that only a mod or admin can remove is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.person.id, orig_comment.community.id, ) @@ -47,7 +47,7 @@ impl PerformCrud for RemoveComment { // Do the remove let removed = data.removed; let updated_comment = Comment::update( - context.pool(), + &mut context.pool(), comment_id, &CommentUpdateForm::builder().removed(Some(removed)).build(), ) @@ -61,10 +61,10 @@ impl PerformCrud for RemoveComment { removed: Some(removed), reason: data.reason.clone(), }; - ModRemoveComment::create(context.pool(), &form).await?; + ModRemoveComment::create(&mut context.pool(), &form).await?; let post_id = updated_comment.post_id; - let post = Post::read(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), 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 7b37d900..0129e87c 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -33,15 +33,15 @@ 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; + let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?; check_community_ban( local_user_view.person.id, orig_comment.community.id, - context.pool(), + &mut context.pool(), ) .await?; @@ -52,7 +52,7 @@ impl PerformCrud for EditComment { let language_id = self.language_id; CommunityLanguage::is_allowed_community_language( - context.pool(), + &mut context.pool(), language_id, orig_comment.community.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(context.pool(), comment_id, &form) + let updated_comment = Comment::update(&mut context.pool(), comment_id, &form) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index 5532e315..77ab833b 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(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() { @@ -73,7 +73,8 @@ impl PerformCrud for CreateCommunity { &data.name, &context.settings().get_protocol_and_hostname(), )?; - let community_dupe = Community::read_from_apub_id(context.pool(), &community_actor_id).await?; + let community_dupe = + Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?; if community_dupe.is_some() { return Err(LemmyErrorType::CommunityAlreadyExists)?; } @@ -98,7 +99,7 @@ impl PerformCrud for CreateCommunity { .instance_id(site_view.site.instance_id) .build(); - let inserted_community = Community::create(context.pool(), &community_form) + let inserted_community = Community::create(&mut context.pool(), &community_form) .await .with_lemmy_type(LemmyErrorType::CommunityAlreadyExists)?; @@ -108,7 +109,7 @@ impl PerformCrud for CreateCommunity { person_id: local_user_view.person.id, }; - CommunityModerator::join(context.pool(), &community_moderator_form) + CommunityModerator::join(&mut context.pool(), &community_moderator_form) .await .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?; @@ -119,21 +120,21 @@ impl PerformCrud for CreateCommunity { pending: false, }; - CommunityFollower::follow(context.pool(), &community_follower_form) + CommunityFollower::follow(&mut context.pool(), &community_follower_form) .await .with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?; // 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(context.pool()).await?; + let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).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(LemmyErrorType::LanguageNotAllowed)?; } - CommunityLanguage::update(context.pool(), languages, community_id).await?; + CommunityLanguage::update(&mut context.pool(), 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 97641f57..d3e58d56 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(context.pool(), community_id).await?; + CommunityModeratorView::for_community(&mut context.pool(), community_id).await?; // Make sure deleter is the top mod is_top_mod(&local_user_view, &community_mods)?; @@ -34,7 +34,7 @@ impl PerformCrud for DeleteCommunity { let community_id = data.community_id; let deleted = data.deleted; Community::update( - context.pool(), + &mut context.pool(), community_id, &CommunityUpdateForm::builder() .deleted(Some(deleted)) diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index d37dd2dc..80ad6129 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); check_private_instance(&local_user_view, &local_site)?; @@ -32,7 +32,7 @@ impl PerformCrud for ListCommunities { let limit = data.limit; let local_user = local_user_view.map(|l| l.local_user); let communities = CommunityQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 fd011c6c..2bcd3d85 100644 --- a/crates/api_crud/src/community/remove.rs +++ b/crates/api_crud/src/community/remove.rs @@ -34,7 +34,7 @@ impl PerformCrud for RemoveCommunity { let community_id = data.community_id; let removed = data.removed; Community::update( - context.pool(), + &mut context.pool(), community_id, &CommunityUpdateForm::builder() .removed(Some(removed)) @@ -52,7 +52,7 @@ impl PerformCrud for RemoveCommunity { reason: data.reason.clone(), expires, }; - ModRemoveCommunity::create(context.pool(), &form).await?; + ModRemoveCommunity::create(&mut context.pool(), &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 9bef9388..62c3776f 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let icon = diesel_option_overwrite_to_url(&data.icon)?; let banner = diesel_option_overwrite_to_url(&data.banner)?; @@ -43,23 +43,24 @@ 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(context.pool(), community_id) - .await - .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; + let mods: Vec = + CommunityModeratorView::for_community(&mut context.pool(), community_id) + .await + .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; if !mods.contains(&local_user_view.person.id) { return Err(LemmyErrorType::NotAModerator)?; } let community_id = data.community_id; if let Some(languages) = data.discussion_languages.clone() { - let site_languages = SiteLanguage::read_local_raw(context.pool()).await?; + let site_languages = SiteLanguage::read_local_raw(&mut context.pool()).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(LemmyErrorType::LanguageNotAllowed)?; } - CommunityLanguage::update(context.pool(), languages, community_id).await?; + CommunityLanguage::update(&mut context.pool(), languages, community_id).await?; } let community_form = CommunityUpdateForm::builder() @@ -73,7 +74,7 @@ impl PerformCrud for EditCommunity { .build(); let community_id = data.community_id; - Community::update(context.pool(), community_id, &community_form) + Community::update(&mut context.pool(), community_id, &community_form) .await .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?; diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index 5aaf5612..dcf4fe7f 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), &emoji_form).await?; + let emoji = CustomEmoji::create(&mut context.pool(), &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(context.pool(), keywords).await?; - let view = CustomEmojiView::get(context.pool(), emoji.id).await?; + CustomEmojiKeyword::create(&mut context.pool(), keywords).await?; + let view = CustomEmojiView::get(&mut context.pool(), 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 45dc6eae..06912923 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(context.pool(), data.id).await?; + CustomEmoji::delete(&mut context.pool(), 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 f56763e9..7db3a528 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), data.id, &emoji_form).await?; - CustomEmojiKeyword::delete(context.pool(), data.id).await?; + let emoji = CustomEmoji::update(&mut context.pool(), data.id, &emoji_form).await?; + CustomEmojiKeyword::delete(&mut context.pool(), 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(context.pool(), keywords).await?; - let view = CustomEmojiView::get(context.pool(), emoji.id).await?; + CustomEmojiKeyword::create(&mut context.pool(), keywords).await?; + let view = CustomEmojiView::get(&mut context.pool(), 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 20f6cfd3..a7aafe81 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -48,7 +48,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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let slur_regex = local_site_to_slur_regex(&local_site); check_slurs(&data.name, &slur_regex)?; @@ -62,15 +62,20 @@ impl PerformCrud for CreatePost { is_valid_body_field(&data.body, true)?; check_url_scheme(&data.url)?; - check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?; - check_community_deleted_or_removed(data.community_id, context.pool()).await?; + check_community_ban( + local_user_view.person.id, + data.community_id, + &mut context.pool(), + ) + .await?; + check_community_deleted_or_removed(data.community_id, &mut context.pool()).await?; let community_id = data.community_id; - let community = Community::read(context.pool(), community_id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; if community.posting_restricted_to_mods { let community_id = data.community_id; let is_mod = CommunityView::is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.local_user.person_id, community_id, ) @@ -90,11 +95,20 @@ impl PerformCrud for CreatePost { let language_id = match data.language_id { Some(lid) => Some(lid), None => { - default_post_language(context.pool(), community_id, local_user_view.local_user.id).await? + default_post_language( + &mut context.pool(), + community_id, + local_user_view.local_user.id, + ) + .await? } }; - CommunityLanguage::is_allowed_community_language(context.pool(), language_id, community_id) - .await?; + CommunityLanguage::is_allowed_community_language( + &mut context.pool(), + language_id, + community_id, + ) + .await?; let post_form = PostInsertForm::builder() .name(data.name.trim().to_owned()) @@ -110,7 +124,7 @@ impl PerformCrud for CreatePost { .thumbnail_url(thumbnail_url) .build(); - let inserted_post = Post::create(context.pool(), &post_form) + let inserted_post = Post::create(&mut context.pool(), &post_form) .await .with_lemmy_type(LemmyErrorType::CouldntCreatePost)?; @@ -122,7 +136,7 @@ impl PerformCrud for CreatePost { &protocol_and_hostname, )?; let updated_post = Post::update( - context.pool(), + &mut context.pool(), inserted_post_id, &PostUpdateForm::builder().ap_id(Some(apub_id)).build(), ) @@ -138,12 +152,12 @@ impl PerformCrud for CreatePost { score: 1, }; - PostLike::like(context.pool(), &like_form) + PostLike::like(&mut context.pool(), &like_form) .await .with_lemmy_type(LemmyErrorType::CouldntLikePost)?; // Mark the post as read - mark_post_as_read(person_id, post_id, context.pool()).await?; + mark_post_as_read(person_id, post_id, &mut context.pool()).await?; if let Some(url) = updated_post.url.clone() { let task = async move { diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index d1124594..eaeb66c4 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(context.pool(), post_id).await?; + let orig_post = Post::read(&mut context.pool(), post_id).await?; // Dont delete it if its already been deleted. if orig_post.deleted == data.deleted { @@ -32,10 +32,10 @@ impl PerformCrud for DeletePost { check_community_ban( local_user_view.person.id, orig_post.community_id, - context.pool(), + &mut context.pool(), ) .await?; - check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?; + check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?; // Verify that only the creator can delete if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { @@ -46,7 +46,7 @@ impl PerformCrud for DeletePost { let post_id = data.post_id; let deleted = data.deleted; Post::update( - context.pool(), + &mut context.pool(), post_id, &PostUpdateForm::builder().deleted(Some(deleted)).build(), ) diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 519b748c..af19402e 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).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(context.pool(), comment_id) + Comment::read(&mut context.pool(), comment_id) .await .with_lemmy_type(LemmyErrorType::CouldntFindPost)? .post_id @@ -46,25 +46,33 @@ impl PerformCrud for GetPost { }; // Check to see if the person is a mod or admin, to show deleted / removed - let community_id = Post::read(context.pool(), post_id).await?.community_id; - let is_mod_or_admin = - is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id)) - .await - .is_ok(); + let community_id = Post::read(&mut context.pool(), post_id).await?.community_id; + let is_mod_or_admin = is_mod_or_admin_opt( + &mut context.pool(), + local_user_view.as_ref(), + Some(community_id), + ) + .await + .is_ok(); - let post_view = PostView::read(context.pool(), post_id, person_id, Some(is_mod_or_admin)) - .await - .with_lemmy_type(LemmyErrorType::CouldntFindPost)?; + let post_view = PostView::read( + &mut context.pool(), + post_id, + person_id, + Some(is_mod_or_admin), + ) + .await + .with_lemmy_type(LemmyErrorType::CouldntFindPost)?; // Mark the post as read let post_id = post_view.post.id; if let Some(person_id) = person_id { - mark_post_as_read(person_id, post_id, context.pool()).await?; + mark_post_as_read(person_id, post_id, &mut context.pool()).await?; } // Necessary for the sidebar subscribed let community_view = CommunityView::read( - context.pool(), + &mut context.pool(), community_id, person_id, Some(is_mod_or_admin), @@ -82,17 +90,18 @@ impl PerformCrud for GetPost { read_comments, ..PersonPostAggregatesForm::default() }; - PersonPostAggregates::upsert(context.pool(), &person_post_agg_form) + PersonPostAggregates::upsert(&mut context.pool(), &person_post_agg_form) .await .with_lemmy_type(LemmyErrorType::CouldntFindPost)?; } - let moderators = CommunityModeratorView::for_community(context.pool(), community_id).await?; + let moderators = + CommunityModeratorView::for_community(&mut context.pool(), community_id).await?; // Fetch the cross_posts let cross_posts = if let Some(url) = &post_view.post.url { let mut x_posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 7f379169..7950d504 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -25,18 +25,18 @@ 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(context.pool(), post_id).await?; + let orig_post = Post::read(&mut context.pool(), post_id).await?; check_community_ban( local_user_view.person.id, orig_post.community_id, - context.pool(), + &mut context.pool(), ) .await?; // Verify that only the mods can remove is_mod_or_admin( - context.pool(), + &mut context.pool(), local_user_view.person.id, orig_post.community_id, ) @@ -46,7 +46,7 @@ impl PerformCrud for RemovePost { let post_id = data.post_id; let removed = data.removed; Post::update( - context.pool(), + &mut context.pool(), post_id, &PostUpdateForm::builder().removed(Some(removed)).build(), ) @@ -59,7 +59,7 @@ impl PerformCrud for RemovePost { removed: Some(removed), reason: data.reason.clone(), }; - ModRemovePost::create(context.pool(), &form).await?; + ModRemovePost::create(&mut context.pool(), &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 93661096..fbbadbc6 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let data_url = data.url.as_ref(); @@ -53,12 +53,12 @@ impl PerformCrud for EditPost { check_url_scheme(&data.url)?; let post_id = data.post_id; - let orig_post = Post::read(context.pool(), post_id).await?; + let orig_post = Post::read(&mut context.pool(), post_id).await?; check_community_ban( local_user_view.person.id, orig_post.community_id, - context.pool(), + &mut context.pool(), ) .await?; @@ -77,7 +77,7 @@ impl PerformCrud for EditPost { let language_id = self.language_id; CommunityLanguage::is_allowed_community_language( - context.pool(), + &mut context.pool(), language_id, orig_post.community_id, ) @@ -97,7 +97,7 @@ impl PerformCrud for EditPost { .build(); let post_id = data.post_id; - Post::update(context.pool(), post_id, &post_form) + Post::update(&mut context.pool(), post_id, &post_form) .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePost)?; diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index d399ffb7..48f6bdd2 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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let content_slurs_removed = remove_slurs( &data.content.clone(), @@ -45,7 +45,12 @@ impl PerformCrud for CreatePrivateMessage { ); is_valid_body_field(&Some(content_slurs_removed.clone()), false)?; - check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?; + check_person_block( + local_user_view.person.id, + data.recipient_id, + &mut context.pool(), + ) + .await?; let private_message_form = PrivateMessageInsertForm::builder() .content(content_slurs_removed.clone()) @@ -53,9 +58,10 @@ impl PerformCrud for CreatePrivateMessage { .recipient_id(data.recipient_id) .build(); - let inserted_private_message = PrivateMessage::create(context.pool(), &private_message_form) - .await - .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; + let inserted_private_message = + PrivateMessage::create(&mut context.pool(), &private_message_form) + .await + .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; let inserted_private_message_id = inserted_private_message.id; let protocol_and_hostname = context.settings().get_protocol_and_hostname(); @@ -65,7 +71,7 @@ impl PerformCrud for CreatePrivateMessage { &protocol_and_hostname, )?; PrivateMessage::update( - context.pool(), + &mut context.pool(), inserted_private_message.id, &PrivateMessageUpdateForm::builder() .ap_id(Some(apub_id)) @@ -74,12 +80,12 @@ impl PerformCrud for CreatePrivateMessage { .await .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; - let view = PrivateMessageView::read(context.pool(), inserted_private_message.id).await?; + let view = PrivateMessageView::read(&mut context.pool(), 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(context.pool(), recipient_id).await?; + let local_recipient = LocalUserView::read_person(&mut context.pool(), 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 be9ed695..c18e94c0 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -26,7 +26,8 @@ impl PerformCrud for DeletePrivateMessage { // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?; + let orig_private_message = + PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?; } @@ -35,7 +36,7 @@ impl PerformCrud for DeletePrivateMessage { let private_message_id = data.private_message_id; let deleted = data.deleted; PrivateMessage::update( - context.pool(), + &mut context.pool(), private_message_id, &PrivateMessageUpdateForm::builder() .deleted(Some(deleted)) @@ -44,7 +45,7 @@ impl PerformCrud for DeletePrivateMessage { .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; - let view = PrivateMessageView::read(context.pool(), private_message_id).await?; + let view = PrivateMessageView::read(&mut context.pool(), 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 679cd09f..73ce034e 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() - .pool(context.pool()) + .pool(&mut context.pool()) .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 e20a7115..4abf6f3c 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -30,11 +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(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?; + let orig_private_message = + PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?; } @@ -45,7 +46,7 @@ impl PerformCrud for EditPrivateMessage { let private_message_id = data.private_message_id; PrivateMessage::update( - context.pool(), + &mut context.pool(), private_message_id, &PrivateMessageUpdateForm::builder() .content(Some(content_slurs_removed)) @@ -55,7 +56,7 @@ impl PerformCrud for EditPrivateMessage { .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; - let view = PrivateMessageView::read(context.pool(), private_message_id).await?; + let view = PrivateMessageView::read(&mut context.pool(), 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 ed433ad9..dab504c8 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -49,7 +49,7 @@ impl PerformCrud for CreateSite { async fn perform(&self, context: &Data) -> Result { let data: &CreateSite = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; // Make sure user is an admin; other types of users should not create site data... is_admin(&local_user_view)?; @@ -74,7 +74,7 @@ impl PerformCrud for CreateSite { let site_id = local_site.site_id; - Site::update(context.pool(), site_id, &site_form).await?; + Site::update(&mut context.pool(), site_id, &site_form).await?; let local_site_form = LocalSiteUpdateForm::builder() // Set the site setup to true @@ -99,7 +99,7 @@ impl PerformCrud for CreateSite { .captcha_difficulty(data.captcha_difficulty.clone()) .build(); - LocalSite::update(context.pool(), &local_site_form).await?; + LocalSite::update(&mut context.pool(), &local_site_form).await?; let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm::builder() .message(data.rate_limit_message) @@ -116,12 +116,12 @@ impl PerformCrud for CreateSite { .search_per_second(data.rate_limit_search_per_second) .build(); - LocalSiteRateLimit::update(context.pool(), &local_site_rate_limit_form).await?; + LocalSiteRateLimit::update(&mut context.pool(), &local_site_rate_limit_form).await?; - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let new_taglines = data.taglines.clone(); - let taglines = Tagline::replace(context.pool(), local_site.id, new_taglines).await?; + let taglines = Tagline::replace(&mut context.pool(), 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 4368da99..e74eeefb 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -36,9 +36,9 @@ impl PerformCrud for GetSite { async fn perform(&self, context: &Data) -> Result { let data: &GetSite = self; - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; - let admins = PersonView::admins(context.pool()).await?; + let admins = PersonView::admins(&mut context.pool()).await?; // Build the local user let my_user = if let Some(local_user_view) = @@ -47,25 +47,25 @@ 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(context.pool(), person_id) + let follows = CommunityFollowerView::for_person(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::SystemErrLogin)?; let person_id = local_user_view.person.id; - let community_blocks = CommunityBlockView::for_person(context.pool(), person_id) + let community_blocks = CommunityBlockView::for_person(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::SystemErrLogin)?; let person_id = local_user_view.person.id; - let person_blocks = PersonBlockView::for_person(context.pool(), person_id) + let person_blocks = PersonBlockView::for_person(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::SystemErrLogin)?; - let moderates = CommunityModeratorView::for_person(context.pool(), person_id) + let moderates = CommunityModeratorView::for_person(&mut context.pool(), person_id) .await .with_lemmy_type(LemmyErrorType::SystemErrLogin)?; - let discussion_languages = LocalUserLanguage::read(context.pool(), local_user_id) + let discussion_languages = LocalUserLanguage::read(&mut context.pool(), local_user_id) .await .with_lemmy_type(LemmyErrorType::SystemErrLogin)?; @@ -81,10 +81,11 @@ impl PerformCrud for GetSite { None }; - let all_languages = Language::read_all(context.pool()).await?; - let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?; - let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?; - let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?; + let all_languages = Language::read_all(&mut context.pool()).await?; + let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; + let taglines = Tagline::get_all(&mut context.pool(), site_view.local_site.id).await?; + let custom_emojis = + CustomEmojiView::get_all(&mut context.pool(), site_view.local_site.id).await?; Ok(GetSiteResponse { site_view, @@ -110,7 +111,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(context.pool(), local_user_id) + let local_user_view = LocalUserView::read(&mut context.pool(), 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 d1820d17..e5c0bc5e 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(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; let site = site_view.site; @@ -56,7 +56,7 @@ impl PerformCrud for EditSite { validate_update_payload(&local_site, data)?; if let Some(discussion_languages) = data.discussion_languages.clone() { - SiteLanguage::update(context.pool(), discussion_languages.clone(), &site).await?; + SiteLanguage::update(&mut context.pool(), discussion_languages.clone(), &site).await?; } let site_form = SiteUpdateForm::builder() @@ -68,7 +68,7 @@ impl PerformCrud for EditSite { .updated(Some(Some(naive_now()))) .build(); - Site::update(context.pool(), site.id, &site_form) + Site::update(&mut context.pool(), 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 @@ -96,7 +96,7 @@ impl PerformCrud for EditSite { .reports_email_admins(data.reports_email_admins) .build(); - let update_local_site = LocalSite::update(context.pool(), &local_site_form) + let update_local_site = LocalSite::update(&mut context.pool(), &local_site_form) .await .ok(); @@ -115,15 +115,15 @@ impl PerformCrud for EditSite { .search_per_second(data.rate_limit_search_per_second) .build(); - LocalSiteRateLimit::update(context.pool(), &local_site_rate_limit_form) + LocalSiteRateLimit::update(&mut context.pool(), &local_site_rate_limit_form) .await .ok(); // Replace the blocked and allowed instances let allowed = data.allowed_instances.clone(); - FederationAllowList::replace(context.pool(), allowed).await?; + FederationAllowList::replace(&mut context.pool(), allowed).await?; let blocked = data.blocked_instances.clone(); - FederationBlockList::replace(context.pool(), blocked).await?; + FederationBlockList::replace(&mut context.pool(), 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 @@ -137,7 +137,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(context.pool()) + LocalUser::set_all_users_registration_applications_accepted(&mut context.pool()) .await .with_lemmy_type(LemmyErrorType::CouldntSetAllRegistrationsAccepted)?; } @@ -147,15 +147,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(context.pool()) + LocalUser::set_all_users_email_verified(&mut context.pool()) .await .with_lemmy_type(LemmyErrorType::CouldntSetAllEmailVerified)?; } let new_taglines = data.taglines.clone(); - let taglines = Tagline::replace(context.pool(), local_site.id, new_taglines).await?; + let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?; - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).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 2bfd48ef..c2f42bba 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(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; let require_registration_application = local_site.registration_mode == RegistrationMode::RequireApplication; @@ -74,7 +74,7 @@ impl PerformCrud for Register { if let Some(captcha_uuid) = &data.captcha_uuid { let uuid = uuid::Uuid::parse_str(captcha_uuid)?; let check = CaptchaAnswer::check_captcha( - context.pool(), + &mut context.pool(), CheckCaptchaAnswer { uuid, answer: data.captcha_answer.clone().unwrap_or_default(), @@ -102,7 +102,7 @@ impl PerformCrud for Register { )?; if let Some(email) = &data.email { - if LocalUser::is_email_taken(context.pool(), email).await? { + if LocalUser::is_email_taken(&mut context.pool(), email).await? { return Err(LemmyErrorType::EmailAlreadyExists)?; } } @@ -123,7 +123,7 @@ impl PerformCrud for Register { .build(); // insert the person - let inserted_person = Person::create(context.pool(), &person_form) + let inserted_person = Person::create(&mut context.pool(), &person_form) .await .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?; @@ -140,7 +140,7 @@ impl PerformCrud for Register { .accepted_application(accepted_application) .build(); - let inserted_local_user = LocalUser::create(context.pool(), &local_user_form).await?; + let inserted_local_user = LocalUser::create(&mut context.pool(), &local_user_form).await?; if local_site.site_setup && require_registration_application { // Create the registration application @@ -150,12 +150,12 @@ impl PerformCrud for Register { answer: data.answer.clone().expect("must have an answer"), }; - RegistrationApplication::create(context.pool(), &form).await?; + RegistrationApplication::create(&mut context.pool(), &form).await?; } // Email the admins if local_site.application_email_admins { - send_new_applicant_email_to_admins(&data.username, context.pool(), context.settings()) + send_new_applicant_email_to_admins(&data.username, &mut context.pool(), context.settings()) .await?; } @@ -191,8 +191,13 @@ impl PerformCrud for Register { .clone() .expect("email was provided"); - send_verification_email(&local_user_view, &email, context.pool(), context.settings()) - .await?; + send_verification_email( + &local_user_view, + &email, + &mut context.pool(), + context.settings(), + ) + .await?; login_response.verify_email_sent = true; } diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index 46f9b794..f8a1e4b8 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -60,7 +60,7 @@ impl BlockUser { actor: mod_.id().into(), to: vec![public()], object: user.id().into(), - cc: generate_cc(target, context.pool()).await?, + cc: generate_cc(target, &mut context.pool()).await?, target: target.id(), kind: BlockType::Block, remove_data, @@ -97,7 +97,7 @@ impl BlockUser { match target { SiteOrCommunity::Site(_) => { - let inboxes = remote_instance_inboxes(context.pool()).await?; + let inboxes = remote_instance_inboxes(&mut context.pool()).await?; send_lemmy_activity(context, block, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -155,7 +155,7 @@ impl ActivityHandler for BlockUser { match target { SiteOrCommunity::Site(_site) => { let blocked_person = Person::update( - context.pool(), + &mut context.pool(), blocked_person.id, &PersonUpdateForm::builder() .banned(Some(true)) @@ -166,7 +166,7 @@ impl ActivityHandler for BlockUser { if self.remove_data.unwrap_or(false) { remove_user_data( blocked_person.id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) @@ -181,7 +181,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBan::create(context.pool(), &form).await?; + ModBan::create(&mut context.pool(), &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(context.pool(), &community_user_ban_form).await?; + CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form).await?; // Also unsubscribe them from the community, if they are subscribed let community_follower_form = CommunityFollowerForm { @@ -197,12 +197,13 @@ impl ActivityHandler for BlockUser { person_id: blocked_person.id, pending: false, }; - CommunityFollower::unfollow(context.pool(), &community_follower_form) + CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) .await .ok(); if self.remove_data.unwrap_or(false) { - remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?; + remove_user_data_in_community(community.id, blocked_person.id, &mut context.pool()) + .await?; } // write to mod log @@ -214,7 +215,7 @@ impl ActivityHandler for BlockUser { banned: Some(true), expires, }; - ModBanFromCommunity::create(context.pool(), &form).await?; + ModBanFromCommunity::create(&mut context.pool(), &form).await?; } } diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index a791ea40..7ee9ec17 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -118,7 +118,10 @@ impl SiteOrCommunity { } } -async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result, LemmyError> { +async fn generate_cc( + target: &SiteOrCommunity, + pool: &mut DbPool<'_>, +) -> Result, LemmyError> { Ok(match target { SiteOrCommunity::Site(_) => Site::read_remote_sites(pool) .await? @@ -139,8 +142,8 @@ 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(context.pool(), request.person_id).await?; - let site = SiteOrCommunity::Site(SiteView::read_local(context.pool()).await?.site.into()); + let person = Person::read(&mut context.pool(), request.person_id).await?; + let site = SiteOrCommunity::Site(SiteView::read_local(&mut context.pool()).await?.site.into()); let expires = request.expires.map(naive_from_unix); // if the action affects a local user, federate to other instances @@ -182,10 +185,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(context.pool(), request.community_id) + let community: ApubCommunity = Community::read(&mut context.pool(), request.community_id) .await? .into(); - let banned_person: ApubPerson = Person::read(context.pool(), request.person_id) + let banned_person: ApubPerson = Person::read(&mut context.pool(), 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 43bc7352..b31f8b4b 100644 --- a/crates/apub/src/activities/block/undo_block_user.rs +++ b/crates/apub/src/activities/block/undo_block_user.rs @@ -53,7 +53,7 @@ impl UndoBlockUser { actor: mod_.id().into(), to: vec![public()], object: block, - cc: generate_cc(target, context.pool()).await?, + cc: generate_cc(target, &mut context.pool()).await?, kind: UndoType::Undo, id: id.clone(), audience, @@ -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(context.pool()).await?); + inboxes.append(&mut remote_instance_inboxes(&mut context.pool()).await?); send_lemmy_activity(context, undo, mod_, inboxes, false).await } SiteOrCommunity::Community(c) => { @@ -103,7 +103,7 @@ impl ActivityHandler for UndoBlockUser { match self.object.target.dereference(context).await? { SiteOrCommunity::Site(_site) => { let blocked_person = Person::update( - context.pool(), + &mut context.pool(), blocked_person.id, &PersonUpdateForm::builder() .banned(Some(false)) @@ -120,7 +120,7 @@ impl ActivityHandler for UndoBlockUser { banned: Some(false), expires, }; - ModBan::create(context.pool(), &form).await?; + ModBan::create(&mut context.pool(), &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(context.pool(), &community_user_ban_form).await?; + CommunityPersonBan::unban(&mut context.pool(), &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(context.pool(), &form).await?; + ModBanFromCommunity::create(&mut context.pool(), &form).await?; } } diff --git a/crates/apub/src/activities/community/collection_add.rs b/crates/apub/src/activities/community/collection_add.rs index be5dd0fe..d08b0cb4 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(context.pool(), &self.target.into()).await?; + Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let new_mod = ObjectId::::from(self.object) @@ -130,13 +130,14 @@ impl ActivityHandler for CollectionAdd { // been added. Skip it here as it would result in a duplicate key error. let new_mod_id = new_mod.id; let moderated_communities = - CommunityModerator::get_person_moderated_communities(context.pool(), new_mod_id).await?; + CommunityModerator::get_person_moderated_communities(&mut context.pool(), new_mod_id) + .await?; if !moderated_communities.contains(&community.id) { let form = CommunityModeratorForm { community_id: community.id, person_id: new_mod.id, }; - CommunityModerator::join(context.pool(), &form).await?; + CommunityModerator::join(&mut context.pool(), &form).await?; // write mod log let actor = self.actor.dereference(context).await?; @@ -146,7 +147,7 @@ impl ActivityHandler for CollectionAdd { community_id: community.id, removed: Some(false), }; - ModAddCommunity::create(context.pool(), &form).await?; + ModAddCommunity::create(&mut context.pool(), &form).await?; } // TODO: send websocket notification about added mod } @@ -157,7 +158,7 @@ impl ActivityHandler for CollectionAdd { let form = PostUpdateForm::builder() .featured_community(Some(true)) .build(); - Post::update(context.pool(), post.id, &form).await?; + Post::update(&mut context.pool(), post.id, &form).await?; } } Ok(()) @@ -174,10 +175,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(context.pool(), request.community_id) + let community: ApubCommunity = Community::read(&mut context.pool(), request.community_id) .await? .into(); - let updated_mod: ApubPerson = Person::read(context.pool(), request.person_id) + let updated_mod: ApubPerson = Person::read(&mut context.pool(), request.person_id) .await? .into(); if request.added { @@ -210,7 +211,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(context.pool(), response.post_view.community.id) + let community = Community::read(&mut context.pool(), 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 8f920cf2..a1c443ea 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(context.pool(), &self.target.into()).await?; + Community::get_by_collection_url(&mut context.pool(), &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(context.pool(), &form).await?; + CommunityModerator::leave(&mut context.pool(), &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(context.pool(), &form).await?; + ModAddCommunity::create(&mut context.pool(), &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(context.pool(), post.id, &form).await?; + Post::update(&mut context.pool(), 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 5e4eefb8..0416b972 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(context.pool(), post.id, &form).await?; + Post::update(&mut context.pool(), 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(context.pool(), post.id, &form).await?; + Post::update(&mut context.pool(), post.id, &form).await?; Ok(()) } } @@ -145,7 +145,7 @@ impl SendActivity for LockPost { }; AnnouncableActivities::UndoLockPost(undo) }; - let community = Community::read(context.pool(), response.post_view.community.id).await?; + let community = Community::read(&mut context.pool(), 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 010bad4f..7a88b34b 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(context.pool(), actor.id) + &mut PersonFollower::list_followers(&mut context.pool(), 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 750672ed..1dffacc3 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(context.pool(), &report_form).await?; + PostReport::report(&mut context.pool(), &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(context.pool(), &report_form).await?; + CommentReport::report(&mut context.pool(), &report_form).await?; } }; Ok(()) diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index f8bbfa6c..3e697fdd 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(context.pool(), request.community_id).await?; + let community = Community::read(&mut context.pool(), request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } } @@ -97,7 +97,7 @@ impl ActivityHandler for UpdateCommunity { let community_update_form = self.object.into_update_form(); - Community::update(context.pool(), community.id, &community_update_form).await?; + Community::update(&mut context.pool(), community.id, &community_update_form).await?; Ok(()) } } @@ -112,7 +112,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(context.pool(), request.community_id).await?; + let community = Community::read(&mut context.pool(), 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 e9e56733..804f1827 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -91,10 +91,12 @@ 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(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), post_id).await?; let community_id = post.community_id; - let person: ApubPerson = Person::read(context.pool(), person_id).await?.into(); - let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into(); + let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into(); + let community: ApubCommunity = Community::read(&mut context.pool(), community_id) + .await? + .into(); let id = generate_activity_id( kind.clone(), @@ -177,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(context.pool(), creator.id, post.community_id).await?; + is_mod_or_admin(&mut context.pool(), creator.id, post.community_id).await?; } } @@ -190,14 +192,14 @@ impl ActivityHandler for CreateOrUpdateNote { person_id: comment.creator_id, score: 1, }; - CommentLike::like(context.pool(), &like_form).await?; + CommentLike::like(&mut context.pool(), &like_form).await?; // Calculate initial hot_rank - CommentAggregates::update_hot_rank(context.pool(), comment.id).await?; + CommentAggregates::update_hot_rank(&mut context.pool(), comment.id).await?; let do_send_email = self.kind == CreateOrUpdateType::Create; let post_id = comment.post_id; - let post = Post::read(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), 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 7d5ce336..e0ce0fec 100644 --- a/crates/apub/src/activities/create_or_update/post.rs +++ b/crates/apub/src/activities/create_or_update/post.rs @@ -109,8 +109,10 @@ impl CreateOrUpdatePage { ) -> Result<(), LemmyError> { let post = ApubPost(post.clone()); let community_id = post.community_id; - let person: ApubPerson = Person::read(context.pool(), person_id).await?.into(); - let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into(); + let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into(); + let community: ApubCommunity = Community::read(&mut context.pool(), community_id) + .await? + .into(); let create_or_update = CreateOrUpdatePage::new(post, &person, &community, kind, context).await?; @@ -187,10 +189,10 @@ impl ActivityHandler for CreateOrUpdatePage { person_id: post.creator_id, score: 1, }; - PostLike::like(context.pool(), &like_form).await?; + PostLike::like(&mut context.pool(), &like_form).await?; // Calculate initial hot_rank for post - PostAggregates::update_hot_rank(context.pool(), post.id).await?; + PostAggregates::update_hot_rank(&mut context.pool(), 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 691eff30..36c9785d 100644 --- a/crates/apub/src/activities/create_or_update/private_message.rs +++ b/crates/apub/src/activities/create_or_update/private_message.rs @@ -71,8 +71,10 @@ impl CreateOrUpdateChatMessage { context: &Data, ) -> Result<(), LemmyError> { let recipient_id = private_message.recipient_id; - let sender: ApubPerson = Person::read(context.pool(), sender_id).await?.into(); - let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into(); + let sender: ApubPerson = Person::read(&mut context.pool(), sender_id).await?.into(); + let recipient: ApubPerson = Person::read(&mut context.pool(), recipient_id) + .await? + .into(); let id = generate_activity_id( kind.clone(), diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index d0833092..8ad10417 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -117,9 +117,9 @@ pub(in crate::activities) async fn receive_remove_action( reason, expires: None, }; - ModRemoveCommunity::create(context.pool(), &form).await?; + ModRemoveCommunity::create(&mut context.pool(), &form).await?; Community::update( - context.pool(), + &mut context.pool(), community.id, &CommunityUpdateForm::builder().removed(Some(true)).build(), ) @@ -132,9 +132,9 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemovePost::create(context.pool(), &form).await?; + ModRemovePost::create(&mut context.pool(), &form).await?; Post::update( - context.pool(), + &mut context.pool(), post.id, &PostUpdateForm::builder().removed(Some(true)).build(), ) @@ -147,9 +147,9 @@ pub(in crate::activities) async fn receive_remove_action( removed: Some(true), reason, }; - ModRemoveComment::create(context.pool(), &form).await?; + ModRemoveComment::create(&mut context.pool(), &form).await?; Comment::update( - context.pool(), + &mut context.pool(), comment.id, &CommentUpdateForm::builder().removed(Some(true)).build(), ) diff --git a/crates/apub/src/activities/deletion/delete_user.rs b/crates/apub/src/activities/deletion/delete_user.rs index 11be132e..d74a3c8a 100644 --- a/crates/apub/src/activities/deletion/delete_user.rs +++ b/crates/apub/src/activities/deletion/delete_user.rs @@ -32,7 +32,7 @@ impl SendActivity for DeleteAccount { let actor: ApubPerson = local_user_view.person.into(); delete_user_account( actor.id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) @@ -51,7 +51,7 @@ impl SendActivity for DeleteAccount { cc: vec![], }; - let inboxes = remote_instance_inboxes(context.pool()).await?; + let inboxes = remote_instance_inboxes(&mut context.pool()).await?; send_lemmy_activity(context, delete, &actor, inboxes, true).await?; Ok(()) } @@ -84,7 +84,7 @@ impl ActivityHandler for DeleteUser { let actor = self.actor.dereference(context).await?; delete_user_account( actor.id, - context.pool(), + &mut context.pool(), context.settings(), context.client(), ) diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 3bde90f9..3b8c8b53 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -64,7 +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(context.pool(), response.post_view.community.id).await?; + let community = Community::read(&mut context.pool(), response.post_view.community.id).await?; let deletable = DeletableObjects::Post(response.post_view.post.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -88,7 +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(context.pool(), response.post_view.community.id).await?; + let community = Community::read(&mut context.pool(), 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, @@ -112,8 +112,8 @@ impl SendActivity for DeleteComment { context: &Data, ) -> Result<(), LemmyError> { let community_id = response.comment_view.community.id; - let community = Community::read(context.pool(), community_id).await?; - let person = Person::read(context.pool(), response.comment_view.creator.id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; + let person = Person::read(&mut context.pool(), response.comment_view.creator.id).await?; let deletable = DeletableObjects::Comment(response.comment_view.comment.clone().into()); send_apub_delete_in_community(person, community, deletable, None, request.deleted, context) .await @@ -130,8 +130,9 @@ 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(context.pool(), request.comment_id).await?; - let community = Community::read(context.pool(), response.comment_view.community.id).await?; + let comment = Comment::read(&mut context.pool(), request.comment_id).await?; + let community = + Community::read(&mut context.pool(), response.comment_view.community.id).await?; let deletable = DeletableObjects::Comment(comment.into()); send_apub_delete_in_community( local_user_view.person, @@ -175,7 +176,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(context.pool(), request.community_id).await?; + let community = Community::read(&mut context.pool(), request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -199,7 +200,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(context.pool(), request.community_id).await?; + let community = Community::read(&mut context.pool(), request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( local_user_view.person, @@ -252,7 +253,9 @@ async fn send_apub_delete_private_message( context: &Data, ) -> Result<(), LemmyError> { let recipient_id = pm.recipient_id; - let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into(); + let recipient: ApubPerson = Person::read(&mut context.pool(), recipient_id) + .await? + .into(); let deletable = DeletableObjects::PrivateMessage(pm.into()); let inbox = vec![recipient.shared_inbox_or_inbox()]; @@ -388,7 +391,7 @@ async fn receive_delete_action( } Community::update( - context.pool(), + &mut context.pool(), community.id, &CommunityUpdateForm::builder() .deleted(Some(deleted)) @@ -399,7 +402,7 @@ async fn receive_delete_action( DeletableObjects::Post(post) => { if deleted != post.deleted { Post::update( - context.pool(), + &mut context.pool(), post.id, &PostUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -409,7 +412,7 @@ async fn receive_delete_action( DeletableObjects::Comment(comment) => { if deleted != comment.deleted { Comment::update( - context.pool(), + &mut context.pool(), comment.id, &CommentUpdateForm::builder().deleted(Some(deleted)).build(), ) @@ -418,7 +421,7 @@ async fn receive_delete_action( } DeletableObjects::PrivateMessage(pm) => { PrivateMessage::update( - context.pool(), + &mut context.pool(), pm.id, &PrivateMessageUpdateForm::builder() .deleted(Some(deleted)) diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index e5c51481..e10bd066 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -109,9 +109,9 @@ impl UndoDelete { reason: None, expires: None, }; - ModRemoveCommunity::create(context.pool(), &form).await?; + ModRemoveCommunity::create(&mut context.pool(), &form).await?; Community::update( - context.pool(), + &mut context.pool(), community.id, &CommunityUpdateForm::builder().removed(Some(false)).build(), ) @@ -124,9 +124,9 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemovePost::create(context.pool(), &form).await?; + ModRemovePost::create(&mut context.pool(), &form).await?; Post::update( - context.pool(), + &mut context.pool(), post.id, &PostUpdateForm::builder().removed(Some(false)).build(), ) @@ -139,9 +139,9 @@ impl UndoDelete { removed: Some(false), reason: None, }; - ModRemoveComment::create(context.pool(), &form).await?; + ModRemoveComment::create(&mut context.pool(), &form).await?; Comment::update( - context.pool(), + &mut context.pool(), comment.id, &CommentUpdateForm::builder().removed(Some(false)).build(), ) diff --git a/crates/apub/src/activities/following/accept.rs b/crates/apub/src/activities/following/accept.rs index 7b9ab680..af7d6372 100644 --- a/crates/apub/src/activities/following/accept.rs +++ b/crates/apub/src/activities/following/accept.rs @@ -66,7 +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(context.pool(), community_id, person_id).await?; + CommunityFollower::follow_accepted(&mut context.pool(), 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 8a785da0..073784da 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(context.pool(), &community_follower_form) + CommunityFollower::follow(&mut context.pool(), &community_follower_form) .await .ok(); @@ -113,7 +113,7 @@ impl ActivityHandler for Follow { follower_id: actor.id, pending: false, }; - PersonFollower::follow(context.pool(), &form).await?; + PersonFollower::follow(&mut context.pool(), &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -121,7 +121,7 @@ impl ActivityHandler for Follow { person_id: actor.id, pending: false, }; - CommunityFollower::follow(context.pool(), &form).await?; + CommunityFollower::follow(&mut context.pool(), &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(context.pool(), request.community_id).await?; + let community = Community::read(&mut context.pool(), 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 a4c2fd67..06f95e3f 100644 --- a/crates/apub/src/activities/following/mod.rs +++ b/crates/apub/src/activities/following/mod.rs @@ -27,7 +27,7 @@ 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(context.pool(), request.community_id) + let community: ApubCommunity = Community::read(&mut context.pool(), request.community_id) .await? .into(); if community.local { diff --git a/crates/apub/src/activities/following/undo_follow.rs b/crates/apub/src/activities/following/undo_follow.rs index 279054d5..9f18ccfb 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(context.pool(), &form).await?; + PersonFollower::unfollow(&mut context.pool(), &form).await?; } UserOrCommunity::Community(c) => { let form = CommunityFollowerForm { @@ -90,7 +90,7 @@ impl ActivityHandler for UndoFollow { person_id: person.id, pending: false, }; - CommunityFollower::unfollow(context.pool(), &form).await?; + CommunityFollower::unfollow(&mut context.pool(), &form).await?; } } diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index 29d15701..41a66c7d 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(context.pool(), person_id, community_id) + let is_banned = CommunityPersonBanView::get(&mut context.pool(), 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(context.pool(), mod_.id, community_id).await?; + CommunityView::is_mod_or_admin(&mut context.pool(), 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 8bae0557..24250c50 100644 --- a/crates/apub/src/activities/voting/mod.rs +++ b/crates/apub/src/activities/voting/mod.rs @@ -83,9 +83,11 @@ async fn send_activity( jwt: &Sensitive, context: &Data, ) -> Result<(), LemmyError> { - let community = Community::read(context.pool(), community_id).await?.into(); + let community = Community::read(&mut context.pool(), community_id) + .await? + .into(); let local_user_view = local_user_view_from_jwt(jwt, context).await?; - let actor = Person::read(context.pool(), local_user_view.person.id) + let actor = Person::read(&mut context.pool(), local_user_view.person.id) .await? .into(); @@ -118,8 +120,8 @@ async fn vote_comment( score: vote_type.into(), }; let person_id = actor.id; - CommentLike::remove(context.pool(), person_id, comment_id).await?; - CommentLike::like(context.pool(), &like_form).await?; + CommentLike::remove(&mut context.pool(), person_id, comment_id).await?; + CommentLike::like(&mut context.pool(), &like_form).await?; Ok(()) } @@ -137,8 +139,8 @@ async fn vote_post( score: vote_type.into(), }; let person_id = actor.id; - PostLike::remove(context.pool(), person_id, post_id).await?; - PostLike::like(context.pool(), &like_form).await?; + PostLike::remove(&mut context.pool(), person_id, post_id).await?; + PostLike::like(&mut context.pool(), &like_form).await?; Ok(()) } @@ -150,7 +152,7 @@ async fn undo_vote_comment( ) -> Result<(), LemmyError> { let comment_id = comment.id; let person_id = actor.id; - CommentLike::remove(context.pool(), person_id, comment_id).await?; + CommentLike::remove(&mut context.pool(), person_id, comment_id).await?; Ok(()) } @@ -162,6 +164,6 @@ async fn undo_vote_post( ) -> Result<(), LemmyError> { let post_id = post.id; let person_id = actor.id; - PostLike::remove(context.pool(), person_id, post_id).await?; + PostLike::remove(&mut context.pool(), 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 7f36ed47..4de9a8c1 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(context.pool()) + let enable_downvotes = LocalSite::read(&mut context.pool()) .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 531a6edd..e3b7d065 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -23,7 +23,7 @@ pub async fn list_comments( context: Data, ) -> Result, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; let community_id = if let Some(name) = &data.community_name { @@ -43,7 +43,7 @@ pub async fn list_comments( // 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(context.pool(), parent_id).await?.path) + Some(Comment::read(&mut context.pool(), parent_id).await?.path) } else { None }; @@ -52,7 +52,7 @@ pub async fn list_comments( let post_id = data.post_id; let local_user = local_user_view.map(|l| l.local_user); let comments = CommentQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 929cb95c..63d6800e 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -20,7 +20,7 @@ pub async fn list_posts( context: Data, ) -> Result, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; @@ -38,12 +38,13 @@ pub async fn list_posts( let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?; - let is_mod_or_admin = is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), community_id) - .await - .is_ok(); + let is_mod_or_admin = + is_mod_or_admin_opt(&mut context.pool(), local_user_view.as_ref(), community_id) + .await + .is_ok(); let posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .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 5c8e8cac..12e17dac 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -21,7 +21,7 @@ pub async fn read_community( context: Data, ) -> Result, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; if data.name.is_none() && data.id.is_none() { return Err(LemmyErrorType::NoIdGiven)?; @@ -42,13 +42,16 @@ pub async fn read_community( } }; - let is_mod_or_admin = - is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id)) - .await - .is_ok(); + let is_mod_or_admin = is_mod_or_admin_opt( + &mut context.pool(), + local_user_view.as_ref(), + Some(community_id), + ) + .await + .is_ok(); let community_view = CommunityView::read( - context.pool(), + &mut context.pool(), community_id, person_id, Some(is_mod_or_admin), @@ -56,12 +59,12 @@ pub async fn read_community( .await .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; - let moderators = CommunityModeratorView::for_community(context.pool(), community_id) + let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id) .await .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into()); - let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?; + let mut site = Site::read_from_apub_id(&mut context.pool(), &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 { @@ -71,7 +74,7 @@ pub async fn read_community( } let community_id = community_view.community.id; - let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?; + let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?; Ok(Json(GetCommunityResponse { community_view, diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 35fd59cf..71f4abd5 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -25,7 +25,7 @@ pub async fn read_person( } let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); check_private_instance(&local_user_view, &local_site)?; @@ -46,7 +46,7 @@ pub async fn read_person( // You don't need to return settings for the user, since this comes back with GetSite // `my_user` - let person_view = PersonView::read(context.pool(), person_details_id).await?; + let person_view = PersonView::read(&mut context.pool(), person_details_id).await?; let sort = data.sort; let page = data.page; @@ -56,51 +56,52 @@ pub async fn read_person( let local_user = local_user_view.map(|l| l.local_user); let local_user_clone = local_user.clone(); - let posts_query = PostQuery::builder() - .pool(context.pool()) + let posts = PostQuery::builder() + .pool(&mut context.pool()) .sort(sort) .saved_only(saved_only) .local_user(local_user.as_ref()) .community_id(community_id) .is_mod_or_admin(is_admin) .page(page) - .limit(limit); - - // If its saved only, you don't care what creator it was - // Or, if its not saved, then you only want it for that specific creator - let posts = if !saved_only.unwrap_or(false) { - posts_query - .creator_id(Some(person_details_id)) - .build() - .list() - } else { - posts_query.build().list() - } - .await?; + .limit(limit) + .creator_id( + // If its saved only, you don't care what creator it was + // Or, if its not saved, then you only want it for that specific creator + if !saved_only.unwrap_or(false) { + Some(person_details_id) + } else { + None + }, + ) + .build() + .list() + .await?; - let comments_query = CommentQuery::builder() - .pool(context.pool()) + let comments = CommentQuery::builder() + .pool(&mut context.pool()) .local_user(local_user_clone.as_ref()) .sort(sort.map(post_to_comment_sort_type)) .saved_only(saved_only) .show_deleted_and_removed(Some(false)) .community_id(community_id) .page(page) - .limit(limit); - - // If its saved only, you don't care what creator it was - // Or, if its not saved, then you only want it for that specific creator - let comments = if !saved_only.unwrap_or(false) { - comments_query - .creator_id(Some(person_details_id)) - .build() - .list() - } else { - comments_query.build().list() - } - .await?; + .limit(limit) + .creator_id( + // If its saved only, you don't care what creator it was + // Or, if its not saved, then you only want it for that specific creator + if !saved_only.unwrap_or(false) { + Some(person_details_id) + } else { + None + }, + ) + .build() + .list() + .await?; - let moderates = CommunityModeratorView::for_person(context.pool(), person_details_id).await?; + let moderates = + CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?; // Return the jwt Ok(Json(GetPersonDetailsResponse { diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index 30f381d7..d86c28d6 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -18,14 +18,14 @@ pub async fn resolve_object( context: Data, ) -> Result, LemmyError> { let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; let person_id = local_user_view.person.id; check_private_instance(&Some(local_user_view), &local_site)?; let res = search_query_to_object_id(&data.q, &context) .await .with_lemmy_type(LemmyErrorType::CouldntFindObject)?; - convert_response(res, person_id, context.pool()) + convert_response(res, person_id, &mut context.pool()) .await .with_lemmy_type(LemmyErrorType::CouldntFindObject) } @@ -33,7 +33,7 @@ pub async fn resolve_object( async fn convert_response( object: SearchableObjects, user_id: PersonId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { use SearchableObjects::*; let removed_or_deleted; diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index 0f9a6b37..b7723a6f 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -21,7 +21,7 @@ pub async fn search( context: Data, ) -> Result, LemmyError> { let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; - let local_site = LocalSite::read(context.pool()).await?; + let local_site = LocalSite::read(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; @@ -54,7 +54,7 @@ pub async fn search( match search_type { SearchType::Posts => { posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .listing_type(listing_type) .community_id(community_id) @@ -70,7 +70,7 @@ pub async fn search( } SearchType::Comments => { comments = CommentQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) @@ -85,7 +85,7 @@ pub async fn search( } SearchType::Communities => { communities = CommunityQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .listing_type(listing_type) .search_term(Some(q)) @@ -99,7 +99,7 @@ pub async fn search( } SearchType::Users => { users = PersonQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .search_term(Some(q)) .page(page) @@ -115,7 +115,7 @@ pub async fn search( let local_user_ = local_user.clone(); posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .listing_type(listing_type) .community_id(community_id) @@ -133,7 +133,7 @@ pub async fn search( let local_user_ = local_user.clone(); comments = CommentQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) @@ -152,7 +152,7 @@ pub async fn search( vec![] } else { CommunityQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .listing_type(listing_type) .search_term(Some(q)) @@ -171,7 +171,7 @@ pub async fn search( vec![] } else { PersonQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .search_term(Some(q)) .page(page) @@ -183,7 +183,7 @@ pub async fn search( } SearchType::Url => { posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .sort(sort) .listing_type(listing_type) .community_id(community_id) diff --git a/crates/apub/src/collections/community_featured.rs b/crates/apub/src/collections/community_featured.rs index 0743a9fb..b3ee54db 100644 --- a/crates/apub/src/collections/community_featured.rs +++ b/crates/apub/src/collections/community_featured.rs @@ -29,7 +29,7 @@ impl Collection for ApubCommunityFeatured { data: &Data, ) -> Result { let ordered_items = try_join_all( - Post::list_featured_for_community(data.pool(), owner.id) + Post::list_featured_for_community(&mut data.pool(), owner.id) .await? .into_iter() .map(ApubPost::from) diff --git a/crates/apub/src/collections/community_moderators.rs b/crates/apub/src/collections/community_moderators.rs index d53f8628..336bbfb1 100644 --- a/crates/apub/src/collections/community_moderators.rs +++ b/crates/apub/src/collections/community_moderators.rs @@ -33,7 +33,7 @@ impl Collection for ApubCommunityModerators { owner: &Self::Owner, data: &Data, ) -> Result { - let moderators = CommunityModeratorView::for_community(data.pool(), owner.id).await?; + let moderators = CommunityModeratorView::for_community(&mut data.pool(), owner.id).await?; let ordered_items = moderators .into_iter() .map(|m| ObjectId::::from(m.moderator.actor_id)) @@ -63,7 +63,7 @@ impl Collection for ApubCommunityModerators { ) -> Result { let community_id = owner.id; let current_moderators = - CommunityModeratorView::for_community(data.pool(), community_id).await?; + CommunityModeratorView::for_community(&mut data.pool(), community_id).await?; // Remove old mods from database which arent in the moderators collection anymore for mod_user in ¤t_moderators { let mod_id = ObjectId::from(mod_user.moderator.actor_id.clone()); @@ -72,7 +72,7 @@ impl Collection for ApubCommunityModerators { community_id: mod_user.community.id, person_id: mod_user.moderator.id, }; - CommunityModerator::leave(data.pool(), &community_moderator_form).await?; + CommunityModerator::leave(&mut data.pool(), &community_moderator_form).await?; } } @@ -89,7 +89,7 @@ impl Collection for ApubCommunityModerators { community_id: owner.id, person_id: mod_user.id, }; - CommunityModerator::join(data.pool(), &community_moderator_form).await?; + CommunityModerator::join(&mut data.pool(), &community_moderator_form).await?; } } @@ -128,9 +128,10 @@ mod tests { let community = parse_lemmy_community(&context).await; let community_id = community.id; - let inserted_instance = Instance::read_or_create(context.pool(), "my_domain.tld".to_string()) - .await - .unwrap(); + let inserted_instance = + Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()) + .await + .unwrap(); let old_mod = PersonInsertForm::builder() .name("holly".into()) @@ -138,13 +139,13 @@ mod tests { .instance_id(inserted_instance.id) .build(); - let old_mod = Person::create(context.pool(), &old_mod).await.unwrap(); + let old_mod = Person::create(&mut context.pool(), &old_mod).await.unwrap(); let community_moderator_form = CommunityModeratorForm { community_id: community.id, person_id: old_mod.id, }; - CommunityModerator::join(context.pool(), &community_moderator_form) + CommunityModerator::join(&mut context.pool(), &community_moderator_form) .await .unwrap(); @@ -161,20 +162,25 @@ mod tests { .unwrap(); assert_eq!(context.request_count(), 0); - let current_moderators = CommunityModeratorView::for_community(context.pool(), community_id) - .await - .unwrap(); + let current_moderators = + CommunityModeratorView::for_community(&mut context.pool(), community_id) + .await + .unwrap(); assert_eq!(current_moderators.len(), 1); assert_eq!(current_moderators[0].moderator.id, new_mod.id); - Person::delete(context.pool(), old_mod.id).await.unwrap(); - Person::delete(context.pool(), new_mod.id).await.unwrap(); - Community::delete(context.pool(), community.id) + Person::delete(&mut context.pool(), old_mod.id) + .await + .unwrap(); + Person::delete(&mut context.pool(), new_mod.id) + .await + .unwrap(); + Community::delete(&mut context.pool(), community.id) .await .unwrap(); - Site::delete(context.pool(), site.id).await.unwrap(); - Instance::delete(context.pool(), inserted_instance.id) + Site::delete(&mut context.pool(), site.id).await.unwrap(); + Instance::delete(&mut context.pool(), inserted_instance.id) .await .unwrap(); } diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index c95b64aa..854db934 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -41,14 +41,16 @@ impl Collection for ApubCommunityOutbox { owner: &Self::Owner, data: &Data, ) -> Result { - let post_list: Vec = Post::list_for_community(data.pool(), owner.id) + let post_list: Vec = Post::list_for_community(&mut data.pool(), owner.id) .await? .into_iter() .map(Into::into) .collect(); let mut ordered_items = vec![]; for post in post_list { - let person = Person::read(data.pool(), post.creator_id).await?.into(); + let person = Person::read(&mut data.pool(), post.creator_id) + .await? + .into(); let create = CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?; let announcable = AnnouncableActivities::CreateOrUpdatePost(create); diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index 4f72d148..701d1038 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(context.pool(), name, domain).await; + let actor = DbActor::read_from_name_and_domain(&mut context.pool(), 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(context.pool(), &identifier, include_deleted) + DbActor::read_from_name(&mut context.pool(), &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 651abf7d..92914966 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -91,8 +91,12 @@ impl InCommunity for PostOrComment { async fn community(&self, context: &Data) -> Result { let cid = match self { PostOrComment::Post(p) => p.community_id, - PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id, + PostOrComment::Comment(c) => { + Post::read(&mut context.pool(), c.post_id) + .await? + .community_id + } }; - Ok(Community::read(context.pool(), cid).await?.into()) + Ok(Community::read(&mut context.pool(), cid).await?.into()) } } diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index 9e78da2a..66794f90 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(context.pool(), id).await?.into(); + let comment: ApubComment = Comment::read(&mut context.pool(), 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 19793674..18ad860b 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(context.pool(), &info.community_name, true) + Community::read_from_name(&mut context.pool(), &info.community_name, true) .await? .into(); @@ -64,7 +64,8 @@ pub(crate) async fn get_apub_community_followers( info: web::Path, context: Data, ) -> Result { - let community = Community::read_from_name(context.pool(), &info.community_name, false).await?; + let community = + Community::read_from_name(&mut context.pool(), &info.community_name, false).await?; let followers = GroupFollowers::new(community, &context).await?; create_apub_response(&followers) } @@ -76,7 +77,7 @@ pub(crate) async fn get_apub_community_outbox( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(context.pool(), &info.community_name, false) + Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? .into(); if community.deleted || community.removed { @@ -92,7 +93,7 @@ pub(crate) async fn get_apub_community_moderators( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(context.pool(), &info.community_name, false) + Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? .into(); if community.deleted || community.removed { @@ -108,7 +109,7 @@ pub(crate) async fn get_apub_community_featured( context: Data, ) -> Result { let community: ApubCommunity = - Community::read_from_name(context.pool(), &info.community_name, false) + Community::read_from_name(&mut context.pool(), &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 fec287e1..52a01443 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -88,7 +88,7 @@ pub(crate) async fn get_activity( info.id ))? .into(); - let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?; + let activity = Activity::read_from_apub_id(&mut context.pool(), &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 453905ee..16956ec4 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(context.pool(), &user_name, true) + let person: ApubPerson = Person::read_from_name(&mut context.pool(), &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(context.pool(), &info.user_name, false).await?; + let person = Person::read_from_name(&mut context.pool(), &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 a2e60026..4da3dc14 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(context.pool(), id).await?.into(); + let post: ApubPost = Post::read(&mut context.pool(), 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 676ce9bd..071f4aa6 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(context.pool()).await?.site.into(); + let site: ApubSite = SiteView::read_local(&mut context.pool()).await?.site.into(); let apub = site.into_json(&context).await?; create_apub_response(&apub) diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index f7ef22ee..8d818602 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -1,7 +1,6 @@ use crate::fetcher::post_or_comment::PostOrComment; use activitypub_federation::config::{Data, UrlVerifier}; use async_trait::async_trait; -use futures::future::join3; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{ source::{ @@ -10,7 +9,7 @@ use lemmy_db_schema::{ local_site::LocalSite, }, traits::Crud, - utils::DbPool, + utils::{ActualDbPool, DbPool}, }; use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult}; use moka::future::Cache; @@ -41,12 +40,12 @@ static CONTEXT: Lazy> = Lazy::new(|| { }); #[derive(Clone)] -pub struct VerifyUrlData(pub DbPool); +pub struct VerifyUrlData(pub ActualDbPool); #[async_trait] impl UrlVerifier for VerifyUrlData { async fn verify(&self, url: &Url) -> Result<(), &'static str> { - let local_site_data = local_site_data_cached(&self.0) + let local_site_data = local_site_data_cached(&mut (&self.0).into()) .await .expect("read local site data"); check_apub_id_valid(url, &local_site_data)?; @@ -102,7 +101,9 @@ pub(crate) struct LocalSiteData { blocked_instances: Vec, } -pub(crate) async fn local_site_data_cached(pool: &DbPool) -> LemmyResult> { +pub(crate) async fn local_site_data_cached( + pool: &mut DbPool<'_>, +) -> LemmyResult> { static CACHE: Lazy>> = Lazy::new(|| { Cache::builder() .max_capacity(1) @@ -112,18 +113,20 @@ pub(crate) async fn local_site_data_cached(pool: &DbPool) -> LemmyResult ( + // LocalSite may be missing + |pool| async { + Ok(LocalSite::read(pool).await.ok()) + }, + Instance::allowlist, + Instance::blocklist + ))?; Ok::<_, diesel::result::Error>(Arc::new(LocalSiteData { - // LocalSite may be missing - local_site: local_site.ok(), - allowed_instances: allowed_instances?, - blocked_instances: blocked_instances?, + local_site, + allowed_instances, + blocked_instances, })) }) .await?, @@ -144,7 +147,7 @@ pub(crate) async fn check_apub_id_valid_with_strictness( return Ok(()); } - let local_site_data = local_site_data_cached(context.pool()).await?; + let local_site_data = local_site_data_cached(&mut context.pool()).await?; check_apub_id_valid(apub_id, &local_site_data).map_err(|err| match err { "Federation disabled" => LemmyErrorType::FederationDisabled, "Domain is blocked" => LemmyErrorType::DomainBlocked, @@ -198,7 +201,7 @@ where sensitive: Some(sensitive), updated: None, }; - Activity::create(data.pool(), &form).await?; + Activity::create(&mut data.pool(), &form).await?; Ok(()) } diff --git a/crates/apub/src/mentions.rs b/crates/apub/src/mentions.rs index 088f84d0..b088dfd0 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(context.pool(), comment).await?; + let parent_creator = get_comment_parent_creator(&mut context.pool(), comment).await?; let mut addressed_ccs: Vec = vec![community_id.into(), parent_creator.id()]; // Add the mention tag @@ -92,7 +92,7 @@ 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(pool, comment))] async fn get_comment_parent_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, comment: &Comment, ) -> Result { let parent_creator_id = if let Some(parent_comment_id) = comment.parent_comment_id() { diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index da844ecc..0a318591 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -66,7 +66,7 @@ impl Object for ApubComment { context: &Data, ) -> Result, LemmyError> { Ok( - Comment::read_from_apub_id(context.pool(), object_id) + Comment::read_from_apub_id(&mut context.pool(), object_id) .await? .map(Into::into), ) @@ -76,7 +76,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(context.pool(), self.id, &form).await?; + Comment::update(&mut context.pool(), self.id, &form).await?; } Ok(()) } @@ -84,20 +84,20 @@ 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(context.pool(), creator_id).await?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let post_id = self.post_id; - let post = Post::read(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), post_id).await?; let community_id = post.community_id; - let community = Community::read(context.pool(), community_id).await?; + let community = Community::read(&mut context.pool(), community_id).await?; let in_reply_to = if let Some(comment_id) = self.parent_comment_id() { - let parent_comment = Comment::read(context.pool(), comment_id).await?; + let parent_comment = Comment::read(&mut context.pool(), comment_id).await?; parent_comment.ap_id.into() } else { post.ap_id.into() }; - let language = LanguageTag::new_single(self.language_id, context.pool()).await?; + let language = LanguageTag::new_single(self.language_id, &mut context.pool()).await?; let maa = collect_non_local_mentions(&self, community.actor_id.clone().into(), context).await?; let note = Note { @@ -152,10 +152,11 @@ impl Object for ApubComment { let content = read_from_string_or_source(¬e.content, ¬e.media_type, ¬e.source); - let local_site = LocalSite::read(context.pool()).await.ok(); + let local_site = LocalSite::read(&mut context.pool()).await.ok(); let slur_regex = &local_site_opt_to_slur_regex(&local_site); let content_slurs_removed = remove_slurs(&content, slur_regex); - let language_id = LanguageTag::to_language_id_single(note.language, context.pool()).await?; + let language_id = + LanguageTag::to_language_id_single(note.language, &mut context.pool()).await?; let form = CommentInsertForm { creator_id: creator.id, @@ -171,7 +172,7 @@ impl Object for ApubComment { language_id, }; let parent_comment_path = parent_comment.map(|t| t.0.path); - let comment = Comment::create(context.pool(), &form, parent_comment_path.as_ref()).await?; + let comment = Comment::create(&mut context.pool(), &form, parent_comment_path.as_ref()).await?; Ok(comment.into()) } } @@ -209,11 +210,15 @@ pub(crate) mod tests { } async fn cleanup(data: (ApubPerson, ApubCommunity, ApubPost, ApubSite), context: &LemmyContext) { - Post::delete(context.pool(), data.2.id).await.unwrap(); - Community::delete(context.pool(), data.1.id).await.unwrap(); - Person::delete(context.pool(), data.0.id).await.unwrap(); - Site::delete(context.pool(), data.3.id).await.unwrap(); - LocalSite::delete(context.pool()).await.unwrap(); + Post::delete(&mut context.pool(), data.2.id).await.unwrap(); + Community::delete(&mut context.pool(), data.1.id) + .await + .unwrap(); + Person::delete(&mut context.pool(), data.0.id) + .await + .unwrap(); + Site::delete(&mut context.pool(), data.3.id).await.unwrap(); + LocalSite::delete(&mut context.pool()).await.unwrap(); } #[tokio::test] @@ -238,7 +243,9 @@ pub(crate) mod tests { let to_apub = comment.into_json(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - Comment::delete(context.pool(), comment_id).await.unwrap(); + Comment::delete(&mut context.pool(), comment_id) + .await + .unwrap(); cleanup(data, &context).await; } @@ -268,7 +275,9 @@ pub(crate) mod tests { assert!(!comment.local); assert_eq!(context.request_count(), 1); - Comment::delete(context.pool(), comment.id).await.unwrap(); + Comment::delete(&mut context.pool(), 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 113fa6c0..b25a60d7 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -66,7 +66,7 @@ impl Object for ApubCommunity { context: &Data, ) -> Result, LemmyError> { Ok( - Community::read_from_apub_id(context.pool(), &object_id.into()) + Community::read_from_apub_id(&mut context.pool(), &object_id.into()) .await? .map(Into::into), ) @@ -75,15 +75,15 @@ 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(context.pool(), self.id, &form).await?; + Community::update(&mut context.pool(), self.id, &form).await?; Ok(()) } #[tracing::instrument(skip_all)] async fn into_json(self, data: &Data) -> Result { let community_id = self.id; - let langs = CommunityLanguage::read(data.pool(), community_id).await?; - let language = LanguageTag::new_multiple(langs, data.pool()).await?; + let langs = CommunityLanguage::read(&mut data.pool(), community_id).await?; + let language = LanguageTag::new_multiple(langs, &mut data.pool()).await?; let group = Group { kind: GroupType::Group, @@ -130,10 +130,11 @@ impl Object for ApubCommunity { let instance_id = fetch_instance_actor_for_object(&group.id, context).await?; let form = Group::into_insert_form(group.clone(), instance_id); - let languages = LanguageTag::to_language_id_multiple(group.language, context.pool()).await?; + let languages = + LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?; - let community = Community::create(context.pool(), &form).await?; - CommunityLanguage::update(context.pool(), languages, community.id).await?; + let community = Community::create(&mut context.pool(), &form).await?; + CommunityLanguage::update(&mut context.pool(), languages, community.id).await?; let community: ApubCommunity = community.into(); @@ -186,9 +187,9 @@ impl ApubCommunity { ) -> Result, LemmyError> { let id = self.id; - let local_site_data = local_site_data_cached(context.pool()).await?; - let follows = CommunityFollowerView::get_community_follower_inboxes(context.pool(), id).await?; - + let local_site_data = local_site_data_cached(&mut context.pool()).await?; + let follows = + CommunityFollowerView::get_community_follower_inboxes(&mut context.pool(), id).await?; let inboxes: Vec = follows .into_iter() .map(Into::into) @@ -240,9 +241,9 @@ pub(crate) mod tests { assert!(!community.local); assert_eq!(community.description.as_ref().unwrap().len(), 132); - Community::delete(context.pool(), community.id) + Community::delete(&mut context.pool(), community.id) .await .unwrap(); - Site::delete(context.pool(), site.id).await.unwrap(); + Site::delete(&mut context.pool(), site.id).await.unwrap(); } } diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index ec4066b3..026b3727 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -71,7 +71,7 @@ impl Object for ApubSite { data: &Data, ) -> Result, LemmyError> { Ok( - Site::read_from_apub_id(data.pool(), &object_id.into()) + Site::read_from_apub_id(&mut data.pool(), &object_id.into()) .await? .map(Into::into), ) @@ -84,8 +84,8 @@ impl Object for ApubSite { #[tracing::instrument(skip_all)] async fn into_json(self, data: &Data) -> Result { let site_id = self.id; - let langs = SiteLanguage::read(data.pool(), site_id).await?; - let language = LanguageTag::new_multiple(langs, data.pool()).await?; + let langs = SiteLanguage::read(&mut data.pool(), site_id).await?; + let language = LanguageTag::new_multiple(langs, &mut data.pool()).await?; let instance = Instance { kind: ApplicationType::Application, @@ -116,7 +116,7 @@ impl Object for ApubSite { check_apub_id_valid_with_strictness(apub.id.inner(), true, data).await?; verify_domains_match(expected_domain, apub.id.inner())?; - let local_site_data = local_site_data_cached(data.pool()).await?; + let local_site_data = local_site_data_cached(&mut data.pool()).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs(&apub.name, slur_regex)?; check_slurs_opt(&apub.summary, slur_regex)?; @@ -127,7 +127,7 @@ impl Object for ApubSite { #[tracing::instrument(skip_all)] async fn from_json(apub: Self::Kind, data: &Data) -> Result { let domain = apub.id.inner().domain().expect("group id has domain"); - let instance = DbInstance::read_or_create(data.pool(), domain.to_string()).await?; + let instance = DbInstance::read_or_create(&mut data.pool(), domain.to_string()).await?; let site_form = SiteInsertForm { name: apub.name.clone(), @@ -143,10 +143,10 @@ impl Object for ApubSite { private_key: None, instance_id: instance.id, }; - let languages = LanguageTag::to_language_id_multiple(apub.language, data.pool()).await?; + let languages = LanguageTag::to_language_id_multiple(apub.language, &mut data.pool()).await?; - let site = Site::create(data.pool(), &site_form).await?; - SiteLanguage::update(data.pool(), languages, &site).await?; + let site = Site::create(&mut data.pool(), &site_form).await?; + SiteLanguage::update(&mut data.pool(), languages, &site).await?; Ok(site.into()) } } @@ -186,7 +186,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(context.pool(), domain.to_string()) + DbInstance::read_or_create(&mut context.pool(), domain.to_string()) .await? .id, ) @@ -194,7 +194,7 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object + C } } -pub(crate) async fn remote_instance_inboxes(pool: &DbPool) -> Result, LemmyError> { +pub(crate) async fn remote_instance_inboxes(pool: &mut DbPool<'_>) -> Result, LemmyError> { Ok( Site::read_remote_sites(pool) .await? @@ -229,6 +229,6 @@ pub(crate) mod tests { assert_eq!(site.name, "Enterprise"); assert_eq!(site.description.as_ref().unwrap().len(), 15); - Site::delete(context.pool(), site.id).await.unwrap(); + Site::delete(&mut context.pool(), site.id).await.unwrap(); } } diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index 95a4fd20..3c2b238b 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(context.pool(), &object_id.into()) + DbPerson::read_from_apub_id(&mut context.pool(), &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(context.pool(), self.id, &form).await?; + DbPerson::update(&mut context.pool(), self.id, &form).await?; Ok(()) } @@ -118,7 +118,7 @@ impl Object for ApubPerson { expected_domain: &Url, context: &Data, ) -> Result<(), LemmyError> { - let local_site_data = local_site_data_cached(context.pool()).await?; + let local_site_data = local_site_data_cached(&mut context.pool()).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs(&person.preferred_username, slur_regex)?; check_slurs_opt(&person.name, slur_regex)?; @@ -165,7 +165,7 @@ impl Object for ApubPerson { matrix_user_id: person.matrix_user_id, instance_id, }; - let person = DbPerson::upsert(context.pool(), &person_form).await?; + let person = DbPerson::upsert(&mut context.pool(), &person_form).await?; Ok(person.into()) } @@ -256,7 +256,9 @@ pub(crate) mod tests { } async fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) { - DbPerson::delete(context.pool(), data.0.id).await.unwrap(); - Site::delete(context.pool(), data.1.id).await.unwrap(); + DbPerson::delete(&mut context.pool(), data.0.id) + .await + .unwrap(); + Site::delete(&mut context.pool(), data.1.id).await.unwrap(); } } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index d73642be..d252a8ea 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -84,7 +84,7 @@ impl Object for ApubPost { context: &Data, ) -> Result, LemmyError> { Ok( - Post::read_from_apub_id(context.pool(), object_id) + Post::read_from_apub_id(&mut context.pool(), object_id) .await? .map(Into::into), ) @@ -94,7 +94,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(context.pool(), self.id, &form).await?; + Post::update(&mut context.pool(), self.id, &form).await?; } Ok(()) } @@ -103,10 +103,10 @@ 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(context.pool(), creator_id).await?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let community_id = self.community_id; - let community = Community::read(context.pool(), community_id).await?; - let language = LanguageTag::new_single(self.language_id, context.pool()).await?; + let community = Community::read(&mut context.pool(), community_id).await?; + let language = LanguageTag::new_single(self.language_id, &mut context.pool()).await?; let page = Page { kind: PageType::Page, @@ -148,7 +148,7 @@ impl Object for ApubPost { check_apub_id_valid_with_strictness(page.id.inner(), community.local, context).await?; verify_person_in_community(&page.creator()?, &community, context).await?; - let local_site_data = local_site_data_cached(context.pool()).await?; + let local_site_data = local_site_data_cached(&mut context.pool()).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs_opt(&page.name, slur_regex)?; @@ -162,7 +162,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(context.pool(), creator.id, community.id).await?; + is_mod_or_admin(&mut context.pool(), creator.id, community.id).await?; } let mut name = page .name @@ -194,7 +194,7 @@ impl Object for ApubPost { }; check_url_scheme(&url)?; - let local_site = LocalSite::read(context.pool()).await.ok(); + let local_site = LocalSite::read(&mut context.pool()).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; @@ -225,7 +225,8 @@ impl Object for ApubPost { let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source) .map(|s| remove_slurs(&s, slur_regex)); - let language_id = LanguageTag::to_language_id_single(page.language, context.pool()).await?; + let language_id = + LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?; PostInsertForm { name, @@ -261,7 +262,7 @@ impl Object for ApubPost { .build() }; - let post = Post::create(context.pool(), &form).await?; + let post = Post::create(&mut context.pool(), &form).await?; // write mod log entry for lock if Page::is_locked_changed(&old_post, &page.comments_enabled) { @@ -270,7 +271,7 @@ impl Object for ApubPost { post_id: post.id, locked: Some(post.locked), }; - ModLockPost::create(context.pool(), &form).await?; + ModLockPost::create(&mut context.pool(), &form).await?; } Ok(post.into()) @@ -312,11 +313,13 @@ mod tests { assert!(!post.featured_community); assert_eq!(context.request_count(), 0); - Post::delete(context.pool(), post.id).await.unwrap(); - Person::delete(context.pool(), person.id).await.unwrap(); - Community::delete(context.pool(), community.id) + Post::delete(&mut context.pool(), post.id).await.unwrap(); + Person::delete(&mut context.pool(), person.id) .await .unwrap(); - Site::delete(context.pool(), site.id).await.unwrap(); + Community::delete(&mut context.pool(), community.id) + .await + .unwrap(); + Site::delete(&mut context.pool(), site.id).await.unwrap(); } } diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index e2571bd9..c986e576 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -59,7 +59,7 @@ impl Object for ApubPrivateMessage { context: &Data, ) -> Result, LemmyError> { Ok( - PrivateMessage::read_from_apub_id(context.pool(), object_id) + PrivateMessage::read_from_apub_id(&mut context.pool(), object_id) .await? .map(Into::into), ) @@ -73,10 +73,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(context.pool(), creator_id).await?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let recipient_id = self.recipient_id; - let recipient = Person::read(context.pool(), recipient_id).await?; + let recipient = Person::read(&mut context.pool(), recipient_id).await?; let note = ChatMessage { r#type: ChatMessageType::ChatMessage, @@ -116,7 +116,7 @@ impl Object for ApubPrivateMessage { ) -> Result { let creator = note.attributed_to.dereference(context).await?; let recipient = note.to[0].dereference(context).await?; - check_person_block(creator.id, recipient.id, context.pool()).await?; + check_person_block(creator.id, recipient.id, &mut context.pool()).await?; let form = PrivateMessageInsertForm { creator_id: creator.id, @@ -129,7 +129,7 @@ impl Object for ApubPrivateMessage { ap_id: Some(note.id.into()), local: Some(false), }; - let pm = PrivateMessage::create(context.pool(), &form).await?; + let pm = PrivateMessage::create(&mut context.pool(), &form).await?; Ok(pm.into()) } } @@ -174,9 +174,13 @@ mod tests { } async fn cleanup(data: (ApubPerson, ApubPerson, ApubSite), context: &Data) { - Person::delete(context.pool(), data.0.id).await.unwrap(); - Person::delete(context.pool(), data.1.id).await.unwrap(); - Site::delete(context.pool(), data.2.id).await.unwrap(); + Person::delete(&mut context.pool(), data.0.id) + .await + .unwrap(); + Person::delete(&mut context.pool(), data.1.id) + .await + .unwrap(); + Site::delete(&mut context.pool(), data.2.id).await.unwrap(); } #[tokio::test] @@ -201,7 +205,9 @@ mod tests { let to_apub = pm.into_json(&context).await.unwrap(); assert_json_include!(actual: json, expected: to_apub); - PrivateMessage::delete(context.pool(), pm_id).await.unwrap(); + PrivateMessage::delete(&mut context.pool(), pm_id) + .await + .unwrap(); cleanup(data, &context).await; } @@ -222,7 +228,9 @@ mod tests { assert_eq!(pm.content.len(), 3); assert_eq!(context.request_count(), 0); - PrivateMessage::delete(context.pool(), pm.id).await.unwrap(); + PrivateMessage::delete(&mut context.pool(), 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 f3943e31..edf67740 100644 --- a/crates/apub/src/protocol/activities/community/collection_add.rs +++ b/crates/apub/src/protocol/activities/community/collection_add.rs @@ -35,7 +35,7 @@ pub struct CollectionAdd { impl InCommunity for CollectionAdd { async fn community(&self, context: &Data) -> Result { let (community, _) = - Community::get_by_collection_url(context.pool(), &self.clone().target.into()).await?; + Community::get_by_collection_url(&mut context.pool(), &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 f69fdc6a..96095173 100644 --- a/crates/apub/src/protocol/activities/community/collection_remove.rs +++ b/crates/apub/src/protocol/activities/community/collection_remove.rs @@ -35,7 +35,7 @@ pub struct CollectionRemove { impl InCommunity for CollectionRemove { async fn community(&self, context: &Data) -> Result { let (community, _) = - Community::get_by_collection_url(context.pool(), &self.clone().target.into()).await?; + Community::get_by_collection_url(&mut context.pool(), &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 b19e3974..33b10853 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(context.pool(), post.community_id).await?; + let community = Community::read(&mut context.pool(), 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 07bbc7c8..6fabc0aa 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(context.pool(), post.community_id).await?; + let community = Community::read(&mut context.pool(), 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 89ee26d9..2a7bfeeb 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(context.pool(), c.post_id).await?; + let post = Post::read(&mut context.pool(), 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(context.pool(), community_id).await?; + let community = Community::read(&mut context.pool(), 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 964bbdf8..a3814501 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::count_community_followers(context.pool(), community_id).await?; + CommunityFollowerView::count_community_followers(&mut context.pool(), 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 e8f3d907..77cafc82 100644 --- a/crates/apub/src/protocol/objects/group.rs +++ b/crates/apub/src/protocol/objects/group.rs @@ -83,7 +83,7 @@ impl Group { check_apub_id_valid_with_strictness(self.id.inner(), true, context).await?; verify_domains_match(expected_domain, self.id.inner())?; - let local_site_data = local_site_data_cached(context.pool()).await?; + let local_site_data = local_site_data_cached(&mut context.pool()).await?; let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site); check_slurs(&self.preferred_username, slur_regex)?; diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index f35140a9..f93308bb 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -33,7 +33,7 @@ pub(crate) struct LanguageTag { impl LanguageTag { pub(crate) async fn new_single( lang: LanguageId, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { let lang = Language::read_from_id(pool, lang).await?; @@ -50,7 +50,7 @@ impl LanguageTag { pub(crate) async fn new_multiple( lang_ids: Vec, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { let mut langs = Vec::::new(); @@ -70,7 +70,7 @@ impl LanguageTag { pub(crate) async fn to_language_id_single( lang: Option, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { let identifier = lang.map(|l| l.identifier); let language = Language::read_id_from_code(pool, identifier.as_deref()).await?; @@ -80,7 +80,7 @@ impl LanguageTag { pub(crate) async fn to_language_id_multiple( langs: Vec, - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, LemmyError> { let mut language_ids = Vec::new(); diff --git a/crates/apub/src/protocol/objects/note.rs b/crates/apub/src/protocol/objects/note.rs index de90636e..f594c14e 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(context.pool(), post_id).await?; + let post = Post::read(&mut context.pool(), 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(context.pool(), post.community_id).await?; + let community = Community::read(&mut context.pool(), 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 5a5550a2..12b57222 100644 --- a/crates/db_schema/src/aggregates/comment_aggregates.rs +++ b/crates/db_schema/src/aggregates/comment_aggregates.rs @@ -8,7 +8,7 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CommentAggregates { - pub async fn read(pool: &DbPool, comment_id: CommentId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result { let conn = &mut get_conn(pool).await?; comment_aggregates::table .filter(comment_aggregates::comment_id.eq(comment_id)) @@ -16,7 +16,10 @@ impl CommentAggregates { .await } - pub async fn update_hot_rank(pool: &DbPool, comment_id: CommentId) -> Result { + pub async fn update_hot_rank( + pool: &mut DbPool<'_>, + comment_id: CommentId, + ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(comment_aggregates::table) @@ -50,6 +53,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/aggregates/community_aggregates.rs b/crates/db_schema/src/aggregates/community_aggregates.rs index 310178f1..61abd193 100644 --- a/crates/db_schema/src/aggregates/community_aggregates.rs +++ b/crates/db_schema/src/aggregates/community_aggregates.rs @@ -8,7 +8,7 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CommunityAggregates { - pub async fn read(pool: &DbPool, community_id: CommunityId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result { let conn = &mut get_conn(pool).await?; community_aggregates::table .filter(community_aggregates::community_id.eq(community_id)) @@ -37,6 +37,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/aggregates/person_aggregates.rs b/crates/db_schema/src/aggregates/person_aggregates.rs index 295f1bfb..e03497da 100644 --- a/crates/db_schema/src/aggregates/person_aggregates.rs +++ b/crates/db_schema/src/aggregates/person_aggregates.rs @@ -8,7 +8,7 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PersonAggregates { - pub async fn read(pool: &DbPool, person_id: PersonId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; person_aggregates::table .filter(person_aggregates::person_id.eq(person_id)) @@ -37,6 +37,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/aggregates/person_post_aggregates.rs b/crates/db_schema/src/aggregates/person_post_aggregates.rs index 5fe1aacf..1cbaa242 100644 --- a/crates/db_schema/src/aggregates/person_post_aggregates.rs +++ b/crates/db_schema/src/aggregates/person_post_aggregates.rs @@ -9,7 +9,10 @@ use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PersonPostAggregates { - pub async fn upsert(pool: &DbPool, form: &PersonPostAggregatesForm) -> Result { + pub async fn upsert( + pool: &mut DbPool<'_>, + form: &PersonPostAggregatesForm, + ) -> Result { let conn = &mut get_conn(pool).await?; insert_into(person_post_aggregates) .values(form) @@ -19,7 +22,11 @@ impl PersonPostAggregates { .get_result::(conn) .await } - pub async fn read(pool: &DbPool, person_id_: PersonId, post_id_: PostId) -> Result { + pub async fn read( + pool: &mut DbPool<'_>, + person_id_: PersonId, + post_id_: PostId, + ) -> Result { let conn = &mut get_conn(pool).await?; person_post_aggregates .filter(post_id.eq(post_id_).and(person_id.eq(person_id_))) diff --git a/crates/db_schema/src/aggregates/post_aggregates.rs b/crates/db_schema/src/aggregates/post_aggregates.rs index 93e6f3f7..8ce2d38f 100644 --- a/crates/db_schema/src/aggregates/post_aggregates.rs +++ b/crates/db_schema/src/aggregates/post_aggregates.rs @@ -8,7 +8,7 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl PostAggregates { - pub async fn read(pool: &DbPool, post_id: PostId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result { let conn = &mut get_conn(pool).await?; post_aggregates::table .filter(post_aggregates::post_id.eq(post_id)) @@ -16,7 +16,7 @@ impl PostAggregates { .await } - pub async fn update_hot_rank(pool: &DbPool, post_id: PostId) -> Result { + pub async fn update_hot_rank(pool: &mut DbPool<'_>, post_id: PostId) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(post_aggregates::table) @@ -53,6 +53,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await @@ -186,6 +187,7 @@ mod tests { #[serial] async fn test_soft_delete() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/aggregates/site_aggregates.rs b/crates/db_schema/src/aggregates/site_aggregates.rs index cfc14df4..1fe90e0f 100644 --- a/crates/db_schema/src/aggregates/site_aggregates.rs +++ b/crates/db_schema/src/aggregates/site_aggregates.rs @@ -7,7 +7,7 @@ use diesel::result::Error; use diesel_async::RunQueryDsl; impl SiteAggregates { - pub async fn read(pool: &DbPool) -> Result { + pub async fn read(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; site_aggregates::table.first::(conn).await } @@ -30,7 +30,9 @@ mod tests { }; use serial_test::serial; - async fn prepare_site_with_community(pool: &DbPool) -> (Instance, Person, Site, Community) { + async fn prepare_site_with_community( + pool: &mut DbPool<'_>, + ) -> (Instance, Person, Site, Community) { let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await .unwrap(); @@ -70,6 +72,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (inserted_instance, inserted_person, inserted_site, inserted_community) = prepare_site_with_community(pool).await; @@ -143,6 +146,7 @@ mod tests { #[serial] async fn test_soft_delete() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (inserted_instance, inserted_person, inserted_site, inserted_community) = prepare_site_with_community(pool).await; diff --git a/crates/db_schema/src/impls/activity.rs b/crates/db_schema/src/impls/activity.rs index 9187e736..4e581f95 100644 --- a/crates/db_schema/src/impls/activity.rs +++ b/crates/db_schema/src/impls/activity.rs @@ -13,12 +13,12 @@ impl Crud for Activity { type InsertForm = ActivityInsertForm; type UpdateForm = ActivityUpdateForm; type IdType = i32; - async fn read(pool: &DbPool, activity_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, activity_id: i32) -> Result { let conn = &mut get_conn(pool).await?; activity.find(activity_id).first::(conn).await } - async fn create(pool: &DbPool, new_activity: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, new_activity: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(activity) .values(new_activity) @@ -27,7 +27,7 @@ impl Crud for Activity { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, activity_id: i32, new_activity: &Self::UpdateForm, ) -> Result { @@ -37,7 +37,7 @@ impl Crud for Activity { .get_result::(conn) .await } - async fn delete(pool: &DbPool, activity_id: i32) -> Result { + async fn delete(pool: &mut DbPool<'_>, activity_id: i32) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(activity.find(activity_id)) .execute(conn) @@ -46,7 +46,10 @@ impl Crud for Activity { } impl Activity { - pub async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result { + pub async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: &DbUrl, + ) -> Result { let conn = &mut get_conn(pool).await?; activity .filter(ap_id.eq(object_id)) @@ -75,6 +78,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/actor_language.rs b/crates/db_schema/src/impls/actor_language.rs index 403ef717..592d62a3 100644 --- a/crates/db_schema/src/impls/actor_language.rs +++ b/crates/db_schema/src/impls/actor_language.rs @@ -25,11 +25,7 @@ use diesel::{ ExpressionMethods, QueryDsl, }; -use diesel_async::{ - pooled_connection::deadpool::Object as PooledConnection, - AsyncPgConnection, - RunQueryDsl, -}; +use diesel_async::{AsyncPgConnection, RunQueryDsl}; use lemmy_utils::error::{LemmyError, LemmyErrorType}; use tokio::sync::OnceCell; @@ -37,7 +33,7 @@ pub const UNDETERMINED_ID: LanguageId = LanguageId(0); impl LocalUserLanguage { pub async fn read( - pool: &DbPool, + pool: &mut DbPool<'_>, for_local_user_id: LocalUserId, ) -> Result, Error> { use crate::schema::local_user_language::dsl::{ @@ -67,7 +63,7 @@ impl LocalUserLanguage { /// /// If no language_id vector is given, it will show all languages pub async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, language_ids: Vec, for_local_user_id: LocalUserId, ) -> Result<(), Error> { @@ -75,7 +71,7 @@ impl LocalUserLanguage { let mut lang_ids = convert_update_languages(conn, language_ids).await?; // No need to update if languages are unchanged - let current = LocalUserLanguage::read(pool, for_local_user_id).await?; + let current = LocalUserLanguage::read(&mut conn.into(), for_local_user_id).await?; if current == lang_ids { return Ok(()); } @@ -118,7 +114,7 @@ impl LocalUserLanguage { } impl SiteLanguage { - pub async fn read_local_raw(pool: &DbPool) -> Result, Error> { + pub async fn read_local_raw(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; site::table .inner_join(local_site::table) @@ -129,27 +125,20 @@ impl SiteLanguage { .await } - async fn read_raw( - conn: &mut PooledConnection, - for_site_id: SiteId, - ) -> Result, Error> { - site_language::table + pub async fn read(pool: &mut DbPool<'_>, for_site_id: SiteId) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let langs = site_language::table .filter(site_language::site_id.eq(for_site_id)) .order(site_language::language_id) .select(site_language::language_id) .load(conn) - .await - } - - pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let langs = Self::read_raw(conn, for_site_id).await?; + .await?; convert_read_languages(conn, langs).await } pub async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, language_ids: Vec, site: &Site, ) -> Result<(), Error> { @@ -159,7 +148,7 @@ impl SiteLanguage { let lang_ids = convert_update_languages(conn, language_ids).await?; // No need to update if languages are unchanged - let current = SiteLanguage::read(pool, site.id).await?; + let current = SiteLanguage::read(&mut conn.into(), site.id).await?; if current == lang_ids { return Ok(()); } @@ -198,7 +187,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( - pool: &DbPool, + pool: &mut DbPool<'_>, for_language_id: Option, for_community_id: CommunityId, ) -> Result<(), LemmyError> { @@ -254,41 +243,34 @@ impl CommunityLanguage { Ok(()) } - async fn read_raw( - conn: &mut PooledConnection, + pub async fn read( + pool: &mut DbPool<'_>, for_community_id: CommunityId, ) -> Result, Error> { use crate::schema::community_language::dsl::{community_id, community_language, language_id}; - community_language + let conn = &mut get_conn(pool).await?; + let langs = community_language .filter(community_id.eq(for_community_id)) .order(language_id) .select(language_id) .get_results(conn) - .await - } - - pub async fn read( - pool: &DbPool, - for_community_id: CommunityId, - ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let langs = Self::read_raw(conn, for_community_id).await?; + .await?; convert_read_languages(conn, langs).await } pub async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, mut language_ids: Vec, for_community_id: CommunityId, ) -> Result<(), Error> { - let conn = &mut get_conn(pool).await?; if language_ids.is_empty() { language_ids = SiteLanguage::read_local_raw(pool).await?; } + let conn = &mut get_conn(pool).await?; let lang_ids = convert_update_languages(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(&mut conn.into(), for_community_id).await?; if current == lang_ids { return Ok(()); } @@ -321,7 +303,7 @@ impl CommunityLanguage { } pub async fn default_post_language( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, local_user_id: LocalUserId, ) -> Result, Error> { @@ -352,7 +334,7 @@ async fn convert_update_languages( ) -> Result, Error> { if language_ids.is_empty() { Ok( - Language::read_all_conn(conn) + Language::read_all(&mut conn.into()) .await? .into_iter() .map(|l| l.id) @@ -419,7 +401,7 @@ mod tests { }; use serial_test::serial; - async fn test_langs1(pool: &DbPool) -> Vec { + async fn test_langs1(pool: &mut DbPool<'_>) -> Vec { vec![ Language::read_id_from_code(pool, Some("en")) .await @@ -435,7 +417,7 @@ mod tests { .unwrap(), ] } - async fn test_langs2(pool: &DbPool) -> Vec { + async fn test_langs2(pool: &mut DbPool<'_>) -> Vec { vec![ Language::read_id_from_code(pool, Some("fi")) .await @@ -448,7 +430,7 @@ mod tests { ] } - async fn create_test_site(pool: &DbPool) -> (Site, Instance) { + async fn create_test_site(pool: &mut DbPool<'_>) -> (Site, Instance) { let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await .unwrap(); @@ -470,6 +452,7 @@ mod tests { #[serial] async fn test_convert_update_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); // call with empty vec, returns all languages let conn = &mut get_conn(pool).await.unwrap(); @@ -477,7 +460,7 @@ mod tests { assert_eq!(184, converted1.len()); // call with nonempty vec, returns same vec - let test_langs = test_langs1(pool).await; + let test_langs = test_langs1(&mut conn.into()).await; let converted2 = convert_update_languages(conn, test_langs.clone()) .await .unwrap(); @@ -488,6 +471,7 @@ mod tests { async fn test_convert_read_languages() { use crate::schema::language::dsl::{id, language}; let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); // call with all languages, returns empty vec let conn = &mut get_conn(pool).await.unwrap(); @@ -496,7 +480,7 @@ mod tests { assert_eq!(0, converted1.len()); // call with nonempty vec, returns same vec - let test_langs = test_langs1(pool).await; + let test_langs = test_langs1(&mut conn.into()).await; let converted2 = convert_read_languages(conn, test_langs.clone()) .await .unwrap(); @@ -507,6 +491,7 @@ mod tests { #[serial] async fn test_site_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (site, instance) = create_test_site(pool).await; let site_languages1 = SiteLanguage::read_local_raw(pool).await.unwrap(); @@ -531,6 +516,7 @@ mod tests { #[serial] async fn test_user_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (site, instance) = create_test_site(pool).await; let mut test_langs = test_langs1(pool).await; @@ -577,6 +563,7 @@ mod tests { #[serial] async fn test_community_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (site, instance) = create_test_site(pool).await; let test_langs = test_langs1(pool).await; SiteLanguage::update(pool, test_langs.clone(), &site) @@ -638,6 +625,7 @@ mod tests { #[serial] async fn test_default_post_language() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let (site, instance) = create_test_site(pool).await; let test_langs = test_langs1(pool).await; let test_langs2 = test_langs2(pool).await; diff --git a/crates/db_schema/src/impls/captcha_answer.rs b/crates/db_schema/src/impls/captcha_answer.rs index de5fac65..fe85b78b 100644 --- a/crates/db_schema/src/impls/captcha_answer.rs +++ b/crates/db_schema/src/impls/captcha_answer.rs @@ -15,7 +15,7 @@ use diesel::{ use diesel_async::RunQueryDsl; impl CaptchaAnswer { - pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswerForm) -> Result { + pub async fn insert(pool: &mut DbPool<'_>, captcha: &CaptchaAnswerForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(captcha_answer) @@ -24,7 +24,10 @@ impl CaptchaAnswer { .await } - pub async fn check_captcha(pool: &DbPool, to_check: CheckCaptchaAnswer) -> Result { + pub async fn check_captcha( + pool: &mut DbPool<'_>, + to_check: CheckCaptchaAnswer, + ) -> Result { let conn = &mut get_conn(pool).await?; // fetch requested captcha @@ -57,6 +60,7 @@ mod tests { #[serial] async fn test_captcha_happy_path() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted = CaptchaAnswer::insert( pool, @@ -84,6 +88,7 @@ mod tests { #[serial] async fn test_captcha_repeat_answer_fails() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted = CaptchaAnswer::insert( pool, diff --git a/crates/db_schema/src/impls/comment.rs b/crates/db_schema/src/impls/comment.rs index ea66347d..2b5b74a1 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( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -41,7 +41,7 @@ impl Comment { } pub async fn update_removed_for_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, new_removed: bool, ) -> Result, Error> { @@ -53,7 +53,7 @@ impl Comment { } pub async fn create( - pool: &DbPool, + pool: &mut DbPool<'_>, comment_form: &CommentInsertForm, parent_path: Option<&Ltree>, ) -> Result { @@ -123,7 +123,10 @@ where ca.comment_id = c.id" inserted_comment } } - pub async fn read_from_apub_id(pool: &DbPool, object_id: Url) -> Result, Error> { + pub async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: Url, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; let object_id: DbUrl = object_id.into(); Ok( @@ -153,23 +156,23 @@ impl Crud for Comment { type InsertForm = CommentInsertForm; type UpdateForm = CommentUpdateForm; type IdType = CommentId; - async fn read(pool: &DbPool, comment_id: CommentId) -> Result { + async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result { let conn = &mut get_conn(pool).await?; comment.find(comment_id).first::(conn).await } - async fn delete(pool: &DbPool, comment_id: CommentId) -> Result { + async fn delete(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(comment.find(comment_id)).execute(conn).await } /// This is unimplemented, use [[Comment::create]] - async fn create(_pool: &DbPool, _comment_form: &Self::InsertForm) -> Result { + async fn create(_pool: &mut DbPool<'_>, _comment_form: &Self::InsertForm) -> Result { unimplemented!(); } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, comment_id: CommentId, comment_form: &Self::UpdateForm, ) -> Result { @@ -185,7 +188,7 @@ impl Crud for Comment { impl Likeable for CommentLike { type Form = CommentLikeForm; type IdType = CommentId; - async fn like(pool: &DbPool, comment_like_form: &CommentLikeForm) -> Result { + async fn like(pool: &mut DbPool<'_>, comment_like_form: &CommentLikeForm) -> Result { use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id}; let conn = &mut get_conn(pool).await?; insert_into(comment_like) @@ -197,7 +200,7 @@ impl Likeable for CommentLike { .await } async fn remove( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id_: PersonId, comment_id_: CommentId, ) -> Result { @@ -216,7 +219,10 @@ impl Likeable for CommentLike { #[async_trait] impl Saveable for CommentSaved { type Form = CommentSavedForm; - async fn save(pool: &DbPool, comment_saved_form: &CommentSavedForm) -> Result { + async fn save( + pool: &mut DbPool<'_>, + comment_saved_form: &CommentSavedForm, + ) -> Result { use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id}; let conn = &mut get_conn(pool).await?; insert_into(comment_saved) @@ -227,7 +233,10 @@ impl Saveable for CommentSaved { .get_result::(conn) .await } - async fn unsave(pool: &DbPool, comment_saved_form: &CommentSavedForm) -> Result { + async fn unsave( + pool: &mut DbPool<'_>, + comment_saved_form: &CommentSavedForm, + ) -> Result { use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id}; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -269,6 +278,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/comment_reply.rs b/crates/db_schema/src/impls/comment_reply.rs index 2489c982..c5939334 100644 --- a/crates/db_schema/src/impls/comment_reply.rs +++ b/crates/db_schema/src/impls/comment_reply.rs @@ -13,7 +13,7 @@ impl Crud for CommentReply { type InsertForm = CommentReplyInsertForm; type UpdateForm = CommentReplyUpdateForm; type IdType = CommentReplyId; - async fn read(pool: &DbPool, comment_reply_id: CommentReplyId) -> Result { + async fn read(pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId) -> Result { let conn = &mut get_conn(pool).await?; comment_reply .find(comment_reply_id) @@ -21,7 +21,10 @@ impl Crud for CommentReply { .await } - async fn create(pool: &DbPool, comment_reply_form: &Self::InsertForm) -> Result { + async fn create( + pool: &mut DbPool<'_>, + comment_reply_form: &Self::InsertForm, + ) -> Result { let conn = &mut get_conn(pool).await?; // since the return here isnt utilized, we dont need to do an update @@ -36,7 +39,7 @@ impl Crud for CommentReply { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId, comment_reply_form: &Self::UpdateForm, ) -> Result { @@ -50,7 +53,7 @@ impl Crud for CommentReply { impl CommentReply { pub async fn mark_all_as_read( - pool: &DbPool, + pool: &mut DbPool<'_>, for_recipient_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -64,7 +67,10 @@ impl CommentReply { .await } - pub async fn read_by_comment(pool: &DbPool, for_comment_id: CommentId) -> Result { + pub async fn read_by_comment( + pool: &mut DbPool<'_>, + for_comment_id: CommentId, + ) -> Result { let conn = &mut get_conn(pool).await?; comment_reply .filter(comment_id.eq(for_comment_id)) @@ -93,6 +99,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/comment_report.rs b/crates/db_schema/src/impls/comment_report.rs index 18cf83a7..ff93915e 100644 --- a/crates/db_schema/src/impls/comment_report.rs +++ b/crates/db_schema/src/impls/comment_report.rs @@ -21,7 +21,10 @@ impl Reportable for CommentReport { /// /// * `conn` - the postgres connection /// * `comment_report_form` - the filled CommentReportForm to insert - async fn report(pool: &DbPool, comment_report_form: &CommentReportForm) -> Result { + async fn report( + pool: &mut DbPool<'_>, + comment_report_form: &CommentReportForm, + ) -> Result { let conn = &mut get_conn(pool).await?; insert_into(comment_report) .values(comment_report_form) @@ -35,7 +38,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( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id_: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -56,7 +59,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( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id_: Self::IdType, by_resolver_id: PersonId, ) -> Result { diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index ad9e6d47..babb9a5a 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -27,7 +27,7 @@ impl Crud for Community { type InsertForm = CommunityInsertForm; type UpdateForm = CommunityUpdateForm; type IdType = CommunityId; - async fn read(pool: &DbPool, community_id: CommunityId) -> Result { + async fn read(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result { let conn = &mut get_conn(pool).await?; community::table .find(community_id) @@ -35,19 +35,19 @@ impl Crud for Community { .await } - async fn delete(pool: &DbPool, community_id: CommunityId) -> Result { + async fn delete(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(community::table.find(community_id)) .execute(conn) .await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { - let conn = &mut get_conn(pool).await?; + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let is_new_community = match &form.actor_id { Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(), None => true, }; + let conn = &mut get_conn(pool).await?; // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible let community_ = insert_into(community::table) @@ -67,7 +67,7 @@ impl Crud for Community { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, form: &Self::UpdateForm, ) -> Result { @@ -83,7 +83,7 @@ impl Crud for Community { impl Joinable for CommunityModerator { type Form = CommunityModeratorForm; async fn join( - pool: &DbPool, + pool: &mut DbPool<'_>, community_moderator_form: &CommunityModeratorForm, ) -> Result { use crate::schema::community_moderator::dsl::community_moderator; @@ -95,7 +95,7 @@ impl Joinable for CommunityModerator { } async fn leave( - pool: &DbPool, + pool: &mut DbPool<'_>, community_moderator_form: &CommunityModeratorForm, ) -> Result { use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id}; @@ -118,7 +118,7 @@ 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( - pool: &DbPool, + pool: &mut DbPool<'_>, url: &DbUrl, ) -> Result<(Community, CollectionType), Error> { use crate::schema::community::dsl::{featured_url, moderators_url}; @@ -144,7 +144,7 @@ impl Community { impl CommunityModerator { pub async fn delete_for_community( - pool: &DbPool, + pool: &mut DbPool<'_>, for_community_id: CommunityId, ) -> Result { use crate::schema::community_moderator::dsl::{community_id, community_moderator}; @@ -156,7 +156,7 @@ impl CommunityModerator { } pub async fn leave_all_communities( - pool: &DbPool, + pool: &mut DbPool<'_>, for_person_id: PersonId, ) -> Result { use crate::schema::community_moderator::dsl::{community_moderator, person_id}; @@ -167,7 +167,7 @@ impl CommunityModerator { } pub async fn get_person_moderated_communities( - pool: &DbPool, + pool: &mut DbPool<'_>, for_person_id: PersonId, ) -> Result, Error> { use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id}; @@ -184,7 +184,7 @@ impl CommunityModerator { impl Bannable for CommunityPersonBan { type Form = CommunityPersonBanForm; async fn ban( - pool: &DbPool, + pool: &mut DbPool<'_>, community_person_ban_form: &CommunityPersonBanForm, ) -> Result { use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id}; @@ -199,7 +199,7 @@ impl Bannable for CommunityPersonBan { } async fn unban( - pool: &DbPool, + pool: &mut DbPool<'_>, community_person_ban_form: &CommunityPersonBanForm, ) -> Result { use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id}; @@ -233,7 +233,7 @@ impl CommunityFollower { #[async_trait] impl Followable for CommunityFollower { type Form = CommunityFollowerForm; - async fn follow(pool: &DbPool, form: &CommunityFollowerForm) -> Result { + async fn follow(pool: &mut DbPool<'_>, form: &CommunityFollowerForm) -> Result { use crate::schema::community_follower::dsl::{community_follower, community_id, person_id}; let conn = &mut get_conn(pool).await?; insert_into(community_follower) @@ -245,7 +245,7 @@ impl Followable for CommunityFollower { .await } async fn follow_accepted( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id_: CommunityId, person_id_: PersonId, ) -> Result { @@ -265,7 +265,7 @@ impl Followable for CommunityFollower { .get_result::(conn) .await } - async fn unfollow(pool: &DbPool, form: &CommunityFollowerForm) -> Result { + async fn unfollow(pool: &mut DbPool<'_>, form: &CommunityFollowerForm) -> Result { use crate::schema::community_follower::dsl::{community_follower, community_id, person_id}; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -280,7 +280,10 @@ impl Followable for CommunityFollower { #[async_trait] impl ApubActor for Community { - async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result, Error> { + async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: &DbUrl, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; Ok( community::table @@ -293,7 +296,7 @@ impl ApubActor for Community { } async fn read_from_name( - pool: &DbPool, + pool: &mut DbPool<'_>, community_name: &str, include_deleted: bool, ) -> Result { @@ -311,7 +314,7 @@ impl ApubActor for Community { } async fn read_from_name_and_domain( - pool: &DbPool, + pool: &mut DbPool<'_>, community_name: &str, for_domain: &str, ) -> Result { @@ -353,6 +356,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/community_block.rs b/crates/db_schema/src/impls/community_block.rs index c1f35e62..9dc21bf2 100644 --- a/crates/db_schema/src/impls/community_block.rs +++ b/crates/db_schema/src/impls/community_block.rs @@ -10,7 +10,7 @@ use diesel_async::RunQueryDsl; #[async_trait] impl Blockable for CommunityBlock { type Form = CommunityBlockForm; - async fn block(pool: &DbPool, community_block_form: &Self::Form) -> Result { + async fn block(pool: &mut DbPool<'_>, community_block_form: &Self::Form) -> Result { let conn = &mut get_conn(pool).await?; insert_into(community_block) .values(community_block_form) @@ -20,7 +20,10 @@ impl Blockable for CommunityBlock { .get_result::(conn) .await } - async fn unblock(pool: &DbPool, community_block_form: &Self::Form) -> Result { + async fn unblock( + pool: &mut DbPool<'_>, + community_block_form: &Self::Form, + ) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete( community_block diff --git a/crates/db_schema/src/impls/custom_emoji.rs b/crates/db_schema/src/impls/custom_emoji.rs index cce35dfc..05030165 100644 --- a/crates/db_schema/src/impls/custom_emoji.rs +++ b/crates/db_schema/src/impls/custom_emoji.rs @@ -14,7 +14,7 @@ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CustomEmoji { - pub async fn create(pool: &DbPool, form: &CustomEmojiInsertForm) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &CustomEmojiInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(custom_emoji) .values(form) @@ -22,7 +22,7 @@ impl CustomEmoji { .await } pub async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, emoji_id: CustomEmojiId, form: &CustomEmojiUpdateForm, ) -> Result { @@ -32,7 +32,7 @@ impl CustomEmoji { .get_result::(conn) .await } - pub async fn delete(pool: &DbPool, emoji_id: CustomEmojiId) -> Result { + pub async fn delete(pool: &mut DbPool<'_>, emoji_id: CustomEmojiId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(custom_emoji.find(emoji_id)) .execute(conn) @@ -42,7 +42,7 @@ impl CustomEmoji { impl CustomEmojiKeyword { pub async fn create( - pool: &DbPool, + pool: &mut DbPool<'_>, form: Vec, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -51,7 +51,7 @@ impl CustomEmojiKeyword { .get_results::(conn) .await } - pub async fn delete(pool: &DbPool, emoji_id: CustomEmojiId) -> Result { + pub async fn delete(pool: &mut DbPool<'_>, emoji_id: CustomEmojiId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(custom_emoji_keyword.filter(custom_emoji_id.eq(emoji_id))) .execute(conn) diff --git a/crates/db_schema/src/impls/email_verification.rs b/crates/db_schema/src/impls/email_verification.rs index af19bbba..59c5b953 100644 --- a/crates/db_schema/src/impls/email_verification.rs +++ b/crates/db_schema/src/impls/email_verification.rs @@ -19,7 +19,7 @@ use diesel::{ use diesel_async::RunQueryDsl; impl EmailVerification { - pub async fn create(pool: &DbPool, form: &EmailVerificationForm) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &EmailVerificationForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(email_verification) .values(form) @@ -27,7 +27,7 @@ impl EmailVerification { .await } - pub async fn read_for_token(pool: &DbPool, token: &str) -> Result { + pub async fn read_for_token(pool: &mut DbPool<'_>, token: &str) -> Result { let conn = &mut get_conn(pool).await?; email_verification .filter(verification_token.eq(token)) @@ -36,7 +36,7 @@ impl EmailVerification { .await } pub async fn delete_old_tokens_for_local_user( - pool: &DbPool, + pool: &mut DbPool<'_>, local_user_id_: LocalUserId, ) -> Result { let conn = &mut get_conn(pool).await?; diff --git a/crates/db_schema/src/impls/federation_allowlist.rs b/crates/db_schema/src/impls/federation_allowlist.rs index c71ffd1c..d4aed484 100644 --- a/crates/db_schema/src/impls/federation_allowlist.rs +++ b/crates/db_schema/src/impls/federation_allowlist.rs @@ -10,7 +10,7 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::{AsyncPgConnection, RunQueryDsl}; impl FederationAllowList { - pub async fn replace(pool: &DbPool, list_opt: Option>) -> Result<(), Error> { + pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option>) -> Result<(), Error> { let conn = &mut get_conn(pool).await?; conn .build_transaction() @@ -21,7 +21,7 @@ impl FederationAllowList { 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(&mut conn.into(), domain).await?; let form = FederationAllowListForm { instance_id: instance.id, @@ -59,6 +59,7 @@ mod tests { #[serial] async fn test_allowlist_insert_and_clear() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let domains = vec![ "tld1.xyz".to_string(), "tld2.xyz".to_string(), diff --git a/crates/db_schema/src/impls/federation_blocklist.rs b/crates/db_schema/src/impls/federation_blocklist.rs index 917bf57a..2a6e0671 100644 --- a/crates/db_schema/src/impls/federation_blocklist.rs +++ b/crates/db_schema/src/impls/federation_blocklist.rs @@ -10,7 +10,7 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::{AsyncPgConnection, RunQueryDsl}; impl FederationBlockList { - pub async fn replace(pool: &DbPool, list_opt: Option>) -> Result<(), Error> { + pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option>) -> Result<(), Error> { let conn = &mut get_conn(pool).await?; conn .build_transaction() @@ -21,7 +21,7 @@ impl FederationBlockList { 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(&mut conn.into(), domain).await?; let form = FederationBlockListForm { instance_id: instance.id, diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index e5a8caba..068e317f 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -5,14 +5,15 @@ use crate::{ utils::{get_conn, naive_now, DbPool}, }; 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, - domain_: String, - ) -> Result { + /// 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(pool: &mut DbPool<'_>, domain_: String) -> Result { use crate::schema::instance::domain; + let conn = &mut get_conn(pool).await?; + // First try to read the instance row and return directly if found let instance = instance::table .filter(domain.eq(&domain_)) @@ -39,25 +40,18 @@ impl Instance { e => e, } } - - /// 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(pool: &DbPool, domain: String) -> Result { - let conn = &mut get_conn(pool).await?; - Self::read_or_create_with_conn(conn, domain).await - } - pub async fn delete(pool: &DbPool, instance_id: InstanceId) -> Result { + pub async fn delete(pool: &mut DbPool<'_>, instance_id: InstanceId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(instance::table.find(instance_id)) .execute(conn) .await } #[cfg(test)] - pub async fn delete_all(pool: &DbPool) -> Result { + pub async fn delete_all(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(instance::table).execute(conn).await } - pub async fn allowlist(pool: &DbPool) -> Result, Error> { + pub async fn allowlist(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; instance::table .inner_join(federation_allowlist::table) @@ -66,7 +60,7 @@ impl Instance { .await } - pub async fn blocklist(pool: &DbPool) -> Result, Error> { + pub async fn blocklist(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; instance::table .inner_join(federation_blocklist::table) @@ -75,7 +69,7 @@ impl Instance { .await } - pub async fn linked(pool: &DbPool) -> Result, Error> { + pub async fn linked(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; instance::table .left_join(federation_blocklist::table) diff --git a/crates/db_schema/src/impls/language.rs b/crates/db_schema/src/impls/language.rs index da9e0ca9..e459d989 100644 --- a/crates/db_schema/src/impls/language.rs +++ b/crates/db_schema/src/impls/language.rs @@ -6,26 +6,22 @@ use crate::{ utils::{get_conn, DbPool}, }; use diesel::{result::Error, QueryDsl}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl Language { - pub async fn read_all(pool: &DbPool) -> Result, Error> { + pub async fn read_all(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; - Self::read_all_conn(conn).await - } - - pub async fn read_all_conn(conn: &mut AsyncPgConnection) -> Result, Error> { language.load::(conn).await } - pub async fn read_from_id(pool: &DbPool, id_: LanguageId) -> Result { + pub async fn read_from_id(pool: &mut DbPool<'_>, id_: LanguageId) -> Result { let conn = &mut get_conn(pool).await?; language.filter(id.eq(id_)).first::(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( - pool: &DbPool, + pool: &mut DbPool<'_>, code_: Option<&str>, ) -> Result, Error> { if let Some(code_) = code_ { @@ -53,6 +49,7 @@ mod tests { #[serial] async fn test_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let all = Language::read_all(pool).await.unwrap(); diff --git a/crates/db_schema/src/impls/local_site.rs b/crates/db_schema/src/impls/local_site.rs index b9e920d1..2d527c0e 100644 --- a/crates/db_schema/src/impls/local_site.rs +++ b/crates/db_schema/src/impls/local_site.rs @@ -7,25 +7,25 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::RunQueryDsl; impl LocalSite { - pub async fn create(pool: &DbPool, form: &LocalSiteInsertForm) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &LocalSiteInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(local_site) .values(form) .get_result::(conn) .await } - pub async fn read(pool: &DbPool) -> Result { + pub async fn read(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; local_site.first::(conn).await } - pub async fn update(pool: &DbPool, form: &LocalSiteUpdateForm) -> Result { + pub async fn update(pool: &mut DbPool<'_>, form: &LocalSiteUpdateForm) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(local_site) .set(form) .get_result::(conn) .await } - pub async fn delete(pool: &DbPool) -> Result { + pub async fn delete(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(local_site).execute(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 b1af5f86..0c9e96e0 100644 --- a/crates/db_schema/src/impls/local_site_rate_limit.rs +++ b/crates/db_schema/src/impls/local_site_rate_limit.rs @@ -11,19 +11,25 @@ use diesel::{dsl::insert_into, result::Error}; use diesel_async::RunQueryDsl; impl LocalSiteRateLimit { - pub async fn read(pool: &DbPool) -> Result { + pub async fn read(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; local_site_rate_limit::table.first::(conn).await } - pub async fn create(pool: &DbPool, form: &LocalSiteRateLimitInsertForm) -> Result { + pub async fn create( + pool: &mut DbPool<'_>, + form: &LocalSiteRateLimitInsertForm, + ) -> Result { let conn = &mut get_conn(pool).await?; insert_into(local_site_rate_limit::table) .values(form) .get_result::(conn) .await } - pub async fn update(pool: &DbPool, form: &LocalSiteRateLimitUpdateForm) -> Result<(), Error> { + pub async fn update( + pool: &mut DbPool<'_>, + form: &LocalSiteRateLimitUpdateForm, + ) -> Result<(), Error> { // avoid error "There are no changes to save. This query cannot be built" if form.is_empty() { return Ok(()); diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index c8ae2362..0a72811a 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( - pool: &DbPool, + pool: &mut DbPool<'_>, local_user_id: LocalUserId, new_password: &str, ) -> Result { @@ -37,7 +37,7 @@ impl LocalUser { .await } - pub async fn set_all_users_email_verified(pool: &DbPool) -> Result, Error> { + pub async fn set_all_users_email_verified(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; diesel::update(local_user) .set(email_verified.eq(true)) @@ -46,7 +46,7 @@ impl LocalUser { } pub async fn set_all_users_registration_applications_accepted( - pool: &DbPool, + pool: &mut DbPool<'_>, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; diesel::update(local_user) @@ -55,7 +55,7 @@ impl LocalUser { .await } - pub async fn is_email_taken(pool: &DbPool, email_: &str) -> Result { + pub async fn is_email_taken(pool: &mut DbPool<'_>, email_: &str) -> Result { use diesel::dsl::{exists, select}; let conn = &mut get_conn(pool).await?; select(exists(local_user.filter(email.eq(email_)))) @@ -69,17 +69,17 @@ impl Crud for LocalUser { type InsertForm = LocalUserInsertForm; type UpdateForm = LocalUserUpdateForm; type IdType = LocalUserId; - async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result { + async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { let conn = &mut get_conn(pool).await?; local_user.find(local_user_id).first::(conn).await } - async fn delete(pool: &DbPool, local_user_id: LocalUserId) -> Result { + async fn delete(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(local_user.find(local_user_id)) .execute(conn) .await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; let mut form_with_encrypted_password = form.clone(); let password_hash = @@ -104,7 +104,7 @@ impl Crud for LocalUser { Ok(local_user_) } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, local_user_id: LocalUserId, form: &Self::UpdateForm, ) -> Result { diff --git a/crates/db_schema/src/impls/moderator.rs b/crates/db_schema/src/impls/moderator.rs index ab8398cf..32201b3c 100644 --- a/crates/db_schema/src/impls/moderator.rs +++ b/crates/db_schema/src/impls/moderator.rs @@ -42,13 +42,13 @@ impl Crud for ModRemovePost { type InsertForm = ModRemovePostForm; type UpdateForm = ModRemovePostForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; let conn = &mut get_conn(pool).await?; mod_remove_post.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModRemovePostForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModRemovePostForm) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_post) @@ -57,7 +57,11 @@ impl Crud for ModRemovePost { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModRemovePostForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModRemovePostForm, + ) -> Result { use crate::schema::mod_remove_post::dsl::mod_remove_post; let conn = &mut get_conn(pool).await?; diesel::update(mod_remove_post.find(from_id)) @@ -72,13 +76,13 @@ impl Crud for ModLockPost { type InsertForm = ModLockPostForm; type UpdateForm = ModLockPostForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; let conn = &mut get_conn(pool).await?; mod_lock_post.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModLockPostForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModLockPostForm) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; let conn = &mut get_conn(pool).await?; insert_into(mod_lock_post) @@ -87,7 +91,11 @@ impl Crud for ModLockPost { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModLockPostForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModLockPostForm, + ) -> Result { use crate::schema::mod_lock_post::dsl::mod_lock_post; let conn = &mut get_conn(pool).await?; diesel::update(mod_lock_post.find(from_id)) @@ -102,13 +110,13 @@ impl Crud for ModFeaturePost { type InsertForm = ModFeaturePostForm; type UpdateForm = ModFeaturePostForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; let conn = &mut get_conn(pool).await?; mod_feature_post.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModFeaturePostForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModFeaturePostForm) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; let conn = &mut get_conn(pool).await?; insert_into(mod_feature_post) @@ -117,7 +125,11 @@ impl Crud for ModFeaturePost { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModFeaturePostForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModFeaturePostForm, + ) -> Result { use crate::schema::mod_feature_post::dsl::mod_feature_post; let conn = &mut get_conn(pool).await?; diesel::update(mod_feature_post.find(from_id)) @@ -132,13 +144,13 @@ impl Crud for ModRemoveComment { type InsertForm = ModRemoveCommentForm; type UpdateForm = ModRemoveCommentForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; let conn = &mut get_conn(pool).await?; mod_remove_comment.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModRemoveCommentForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommentForm) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_comment) @@ -147,7 +159,11 @@ impl Crud for ModRemoveComment { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModRemoveCommentForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModRemoveCommentForm, + ) -> Result { use crate::schema::mod_remove_comment::dsl::mod_remove_comment; let conn = &mut get_conn(pool).await?; diesel::update(mod_remove_comment.find(from_id)) @@ -162,13 +178,13 @@ impl Crud for ModRemoveCommunity { type InsertForm = ModRemoveCommunityForm; type UpdateForm = ModRemoveCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; let conn = &mut get_conn(pool).await?; mod_remove_community.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModRemoveCommunityForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommunityForm) -> Result { use crate::schema::mod_remove_community::dsl::mod_remove_community; let conn = &mut get_conn(pool).await?; insert_into(mod_remove_community) @@ -178,7 +194,7 @@ impl Crud for ModRemoveCommunity { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, from_id: i32, form: &ModRemoveCommunityForm, ) -> Result { @@ -196,7 +212,7 @@ impl Crud for ModBanFromCommunity { type InsertForm = ModBanFromCommunityForm; type UpdateForm = ModBanFromCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; let conn = &mut get_conn(pool).await?; mod_ban_from_community @@ -205,7 +221,7 @@ impl Crud for ModBanFromCommunity { .await } - async fn create(pool: &DbPool, form: &ModBanFromCommunityForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModBanFromCommunityForm) -> Result { use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community; let conn = &mut get_conn(pool).await?; insert_into(mod_ban_from_community) @@ -215,7 +231,7 @@ impl Crud for ModBanFromCommunity { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, from_id: i32, form: &ModBanFromCommunityForm, ) -> Result { @@ -233,13 +249,13 @@ impl Crud for ModBan { type InsertForm = ModBanForm; type UpdateForm = ModBanForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_ban::dsl::mod_ban; let conn = &mut get_conn(pool).await?; mod_ban.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModBanForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::mod_ban; let conn = &mut get_conn(pool).await?; insert_into(mod_ban) @@ -248,7 +264,7 @@ impl Crud for ModBan { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModBanForm) -> Result { + async fn update(pool: &mut DbPool<'_>, from_id: i32, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::mod_ban; let conn = &mut get_conn(pool).await?; diesel::update(mod_ban.find(from_id)) @@ -264,13 +280,13 @@ impl Crud for ModHideCommunity { type UpdateForm = ModHideCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; let conn = &mut get_conn(pool).await?; mod_hide_community.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModHideCommunityForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModHideCommunityForm) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; let conn = &mut get_conn(pool).await?; insert_into(mod_hide_community) @@ -279,7 +295,11 @@ impl Crud for ModHideCommunity { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModHideCommunityForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModHideCommunityForm, + ) -> Result { use crate::schema::mod_hide_community::dsl::mod_hide_community; let conn = &mut get_conn(pool).await?; diesel::update(mod_hide_community.find(from_id)) @@ -294,13 +314,13 @@ impl Crud for ModAddCommunity { type InsertForm = ModAddCommunityForm; type UpdateForm = ModAddCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; let conn = &mut get_conn(pool).await?; mod_add_community.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModAddCommunityForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModAddCommunityForm) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; let conn = &mut get_conn(pool).await?; insert_into(mod_add_community) @@ -309,7 +329,11 @@ impl Crud for ModAddCommunity { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModAddCommunityForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &ModAddCommunityForm, + ) -> Result { use crate::schema::mod_add_community::dsl::mod_add_community; let conn = &mut get_conn(pool).await?; diesel::update(mod_add_community.find(from_id)) @@ -324,7 +348,7 @@ impl Crud for ModTransferCommunity { type InsertForm = ModTransferCommunityForm; type UpdateForm = ModTransferCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_transfer_community::dsl::mod_transfer_community; let conn = &mut get_conn(pool).await?; mod_transfer_community @@ -333,7 +357,7 @@ impl Crud for ModTransferCommunity { .await } - async fn create(pool: &DbPool, form: &ModTransferCommunityForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModTransferCommunityForm) -> Result { use crate::schema::mod_transfer_community::dsl::mod_transfer_community; let conn = &mut get_conn(pool).await?; insert_into(mod_transfer_community) @@ -343,7 +367,7 @@ impl Crud for ModTransferCommunity { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, from_id: i32, form: &ModTransferCommunityForm, ) -> Result { @@ -361,13 +385,13 @@ impl Crud for ModAdd { type InsertForm = ModAddForm; type UpdateForm = ModAddForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::mod_add::dsl::mod_add; let conn = &mut get_conn(pool).await?; mod_add.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &ModAddForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::mod_add; let conn = &mut get_conn(pool).await?; insert_into(mod_add) @@ -376,7 +400,7 @@ impl Crud for ModAdd { .await } - async fn update(pool: &DbPool, from_id: i32, form: &ModAddForm) -> Result { + async fn update(pool: &mut DbPool<'_>, from_id: i32, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::mod_add; let conn = &mut get_conn(pool).await?; diesel::update(mod_add.find(from_id)) @@ -391,13 +415,13 @@ impl Crud for AdminPurgePerson { type InsertForm = AdminPurgePersonForm; type UpdateForm = AdminPurgePersonForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; let conn = &mut get_conn(pool).await?; admin_purge_person.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_person) @@ -406,7 +430,11 @@ impl Crud for AdminPurgePerson { .await } - async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_person::dsl::admin_purge_person; let conn = &mut get_conn(pool).await?; diesel::update(admin_purge_person.find(from_id)) @@ -421,7 +449,7 @@ impl Crud for AdminPurgeCommunity { type InsertForm = AdminPurgeCommunityForm; type UpdateForm = AdminPurgeCommunityForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; let conn = &mut get_conn(pool).await?; admin_purge_community @@ -430,7 +458,7 @@ impl Crud for AdminPurgeCommunity { .await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_community) @@ -439,7 +467,11 @@ impl Crud for AdminPurgeCommunity { .await } - async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_community::dsl::admin_purge_community; let conn = &mut get_conn(pool).await?; diesel::update(admin_purge_community.find(from_id)) @@ -454,13 +486,13 @@ impl Crud for AdminPurgePost { type InsertForm = AdminPurgePostForm; type UpdateForm = AdminPurgePostForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; let conn = &mut get_conn(pool).await?; admin_purge_post.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_post) @@ -469,7 +501,11 @@ impl Crud for AdminPurgePost { .await } - async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_post::dsl::admin_purge_post; let conn = &mut get_conn(pool).await?; diesel::update(admin_purge_post.find(from_id)) @@ -484,13 +520,13 @@ impl Crud for AdminPurgeComment { type InsertForm = AdminPurgeCommentForm; type UpdateForm = AdminPurgeCommentForm; type IdType = i32; - async fn read(pool: &DbPool, from_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; let conn = &mut get_conn(pool).await?; admin_purge_comment.find(from_id).first::(conn).await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; let conn = &mut get_conn(pool).await?; insert_into(admin_purge_comment) @@ -499,7 +535,11 @@ impl Crud for AdminPurgeComment { .await } - async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result { + async fn update( + pool: &mut DbPool<'_>, + from_id: i32, + form: &Self::InsertForm, + ) -> Result { use crate::schema::admin_purge_comment::dsl::admin_purge_comment; let conn = &mut get_conn(pool).await?; diesel::update(admin_purge_comment.find(from_id)) @@ -548,6 +588,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/password_reset_request.rs b/crates/db_schema/src/impls/password_reset_request.rs index 85ad4cf0..3d1a1e8e 100644 --- a/crates/db_schema/src/impls/password_reset_request.rs +++ b/crates/db_schema/src/impls/password_reset_request.rs @@ -24,14 +24,14 @@ impl Crud for PasswordResetRequest { type InsertForm = PasswordResetRequestForm; type UpdateForm = PasswordResetRequestForm; type IdType = i32; - async fn read(pool: &DbPool, password_reset_request_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, password_reset_request_id: i32) -> Result { let conn = &mut get_conn(pool).await?; password_reset_request .find(password_reset_request_id) .first::(conn) .await } - async fn create(pool: &DbPool, form: &PasswordResetRequestForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &PasswordResetRequestForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(password_reset_request) .values(form) @@ -39,7 +39,7 @@ impl Crud for PasswordResetRequest { .await } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, password_reset_request_id: i32, form: &PasswordResetRequestForm, ) -> Result { @@ -53,7 +53,7 @@ impl Crud for PasswordResetRequest { impl PasswordResetRequest { pub async fn create_token( - pool: &DbPool, + pool: &mut DbPool<'_>, from_local_user_id: LocalUserId, token: &str, ) -> Result { @@ -68,7 +68,10 @@ impl PasswordResetRequest { Self::create(pool, &form).await } - pub async fn read_from_token(pool: &DbPool, token: &str) -> Result { + pub async fn read_from_token( + pool: &mut DbPool<'_>, + token: &str, + ) -> Result { let conn = &mut get_conn(pool).await?; let mut hasher = Sha256::new(); hasher.update(token); @@ -81,7 +84,7 @@ impl PasswordResetRequest { } pub async fn get_recent_password_resets_count( - pool: &DbPool, + pool: &mut DbPool<'_>, user_id: LocalUserId, ) -> Result { let conn = &mut get_conn(pool).await?; @@ -120,6 +123,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index 5c23f807..bd9ad405 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -19,7 +19,7 @@ impl Crud for Person { type InsertForm = PersonInsertForm; type UpdateForm = PersonUpdateForm; type IdType = PersonId; - async fn read(pool: &DbPool, person_id: PersonId) -> Result { + async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; person::table .filter(person::deleted.eq(false)) @@ -27,13 +27,13 @@ impl Crud for Person { .first::(conn) .await } - async fn delete(pool: &DbPool, person_id: PersonId) -> Result { + async fn delete(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(person::table.find(person_id)) .execute(conn) .await } - async fn create(pool: &DbPool, form: &PersonInsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(person::table) .values(form) @@ -41,7 +41,7 @@ impl Crud for Person { .await } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id: PersonId, form: &PersonUpdateForm, ) -> Result { @@ -57,7 +57,7 @@ impl Person { /// Update or insert the person. /// /// This is necessary for federation, because Activitypub doesnt distinguish between these actions. - pub async fn upsert(pool: &DbPool, form: &PersonInsertForm) -> Result { + pub async fn upsert(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(person::table) .values(form) @@ -67,7 +67,7 @@ impl Person { .get_result::(conn) .await } - pub async fn delete_account(pool: &DbPool, person_id: PersonId) -> Result { + pub async fn delete_account(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; // Set the local user info to none @@ -104,7 +104,10 @@ pub fn is_banned(banned_: bool, expires: Option) -> bool #[async_trait] impl ApubActor for Person { - async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result, Error> { + async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: &DbUrl, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; Ok( person::table @@ -118,7 +121,7 @@ impl ApubActor for Person { } async fn read_from_name( - pool: &DbPool, + pool: &mut DbPool<'_>, from_name: &str, include_deleted: bool, ) -> Result { @@ -134,7 +137,7 @@ impl ApubActor for Person { } async fn read_from_name_and_domain( - pool: &DbPool, + pool: &mut DbPool<'_>, person_name: &str, for_domain: &str, ) -> Result { @@ -153,7 +156,7 @@ impl ApubActor for Person { #[async_trait] impl Followable for PersonFollower { type Form = PersonFollowerForm; - async fn follow(pool: &DbPool, form: &PersonFollowerForm) -> Result { + async fn follow(pool: &mut DbPool<'_>, form: &PersonFollowerForm) -> Result { use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id}; let conn = &mut get_conn(pool).await?; insert_into(person_follower) @@ -164,10 +167,10 @@ impl Followable for PersonFollower { .get_result::(conn) .await } - async fn follow_accepted(_: &DbPool, _: CommunityId, _: PersonId) -> Result { + async fn follow_accepted(_: &mut DbPool<'_>, _: CommunityId, _: PersonId) -> Result { unimplemented!() } - async fn unfollow(pool: &DbPool, form: &PersonFollowerForm) -> Result { + async fn unfollow(pool: &mut DbPool<'_>, form: &PersonFollowerForm) -> Result { use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id}; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -182,7 +185,7 @@ impl Followable for PersonFollower { impl PersonFollower { pub async fn list_followers( - pool: &DbPool, + pool: &mut DbPool<'_>, for_person_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -211,6 +214,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await @@ -271,6 +275,7 @@ mod tests { #[serial] async fn follow() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await .unwrap(); diff --git a/crates/db_schema/src/impls/person_block.rs b/crates/db_schema/src/impls/person_block.rs index 9cd27556..0d125cd5 100644 --- a/crates/db_schema/src/impls/person_block.rs +++ b/crates/db_schema/src/impls/person_block.rs @@ -10,7 +10,7 @@ use diesel_async::RunQueryDsl; impl PersonBlock { pub async fn read( - pool: &DbPool, + pool: &mut DbPool<'_>, for_person_id: PersonId, for_recipient_id: PersonId, ) -> Result { @@ -26,7 +26,10 @@ impl PersonBlock { #[async_trait] impl Blockable for PersonBlock { type Form = PersonBlockForm; - async fn block(pool: &DbPool, person_block_form: &PersonBlockForm) -> Result { + async fn block( + pool: &mut DbPool<'_>, + person_block_form: &PersonBlockForm, + ) -> Result { let conn = &mut get_conn(pool).await?; insert_into(person_block) .values(person_block_form) @@ -36,7 +39,7 @@ impl Blockable for PersonBlock { .get_result::(conn) .await } - async fn unblock(pool: &DbPool, person_block_form: &Self::Form) -> Result { + async fn unblock(pool: &mut DbPool<'_>, person_block_form: &Self::Form) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete( person_block diff --git a/crates/db_schema/src/impls/person_mention.rs b/crates/db_schema/src/impls/person_mention.rs index 559340e7..25b199e6 100644 --- a/crates/db_schema/src/impls/person_mention.rs +++ b/crates/db_schema/src/impls/person_mention.rs @@ -13,7 +13,7 @@ impl Crud for PersonMention { type InsertForm = PersonMentionInsertForm; type UpdateForm = PersonMentionUpdateForm; type IdType = PersonMentionId; - async fn read(pool: &DbPool, person_mention_id: PersonMentionId) -> Result { + async fn read(pool: &mut DbPool<'_>, person_mention_id: PersonMentionId) -> Result { let conn = &mut get_conn(pool).await?; person_mention .find(person_mention_id) @@ -21,7 +21,10 @@ impl Crud for PersonMention { .await } - async fn create(pool: &DbPool, person_mention_form: &Self::InsertForm) -> Result { + async fn create( + pool: &mut DbPool<'_>, + person_mention_form: &Self::InsertForm, + ) -> Result { let conn = &mut get_conn(pool).await?; // since the return here isnt utilized, we dont need to do an update // but get_result doesnt return the existing row here @@ -35,7 +38,7 @@ impl Crud for PersonMention { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, person_mention_id: PersonMentionId, person_mention_form: &Self::UpdateForm, ) -> Result { @@ -49,7 +52,7 @@ impl Crud for PersonMention { impl PersonMention { pub async fn mark_all_as_read( - pool: &DbPool, + pool: &mut DbPool<'_>, for_recipient_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -64,7 +67,7 @@ impl PersonMention { } pub async fn read_by_comment_and_person( - pool: &DbPool, + pool: &mut DbPool<'_>, for_comment_id: CommentId, for_recipient_id: PersonId, ) -> Result { @@ -97,6 +100,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 6a4d53d3..b775860a 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -38,17 +38,17 @@ impl Crud for Post { type InsertForm = PostInsertForm; type UpdateForm = PostUpdateForm; type IdType = PostId; - async fn read(pool: &DbPool, post_id: PostId) -> Result { + async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result { let conn = &mut get_conn(pool).await?; post.find(post_id).first::(conn).await } - async fn delete(pool: &DbPool, post_id: PostId) -> Result { + async fn delete(pool: &mut DbPool<'_>, post_id: PostId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(post.find(post_id)).execute(conn).await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(post) .values(form) @@ -60,7 +60,7 @@ impl Crud for Post { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, post_id: PostId, new_post: &Self::UpdateForm, ) -> Result { @@ -74,7 +74,7 @@ impl Crud for Post { impl Post { pub async fn list_for_community( - pool: &DbPool, + pool: &mut DbPool<'_>, the_community_id: CommunityId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -90,7 +90,7 @@ impl Post { } pub async fn list_featured_for_community( - pool: &DbPool, + pool: &mut DbPool<'_>, the_community_id: CommunityId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -106,7 +106,7 @@ impl Post { } pub async fn permadelete_for_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -124,7 +124,7 @@ impl Post { } pub async fn update_removed_for_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, for_community_id: Option, new_removed: bool, @@ -148,7 +148,10 @@ impl Post { person_id == post_creator_id } - pub async fn read_from_apub_id(pool: &DbPool, object_id: Url) -> Result, Error> { + pub async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: Url, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; let object_id: DbUrl = object_id.into(); Ok( @@ -162,7 +165,7 @@ impl Post { } pub async fn fetch_pictrs_posts_for_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -177,7 +180,7 @@ impl Post { /// Sets the url and thumbnails fields to None pub async fn remove_pictrs_post_images_and_thumbnails_for_creator( - pool: &DbPool, + pool: &mut DbPool<'_>, for_creator_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -197,7 +200,7 @@ impl Post { } pub async fn fetch_pictrs_posts_for_community( - pool: &DbPool, + pool: &mut DbPool<'_>, for_community_id: CommunityId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -211,7 +214,7 @@ impl Post { /// Sets the url and thumbnails fields to None pub async fn remove_pictrs_post_images_and_thumbnails_for_community( - pool: &DbPool, + pool: &mut DbPool<'_>, for_community_id: CommunityId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -235,7 +238,7 @@ impl Post { impl Likeable for PostLike { type Form = PostLikeForm; type IdType = PostId; - async fn like(pool: &DbPool, post_like_form: &PostLikeForm) -> Result { + async fn like(pool: &mut DbPool<'_>, post_like_form: &PostLikeForm) -> Result { use crate::schema::post_like::dsl::{person_id, post_id, post_like}; let conn = &mut get_conn(pool).await?; insert_into(post_like) @@ -246,7 +249,11 @@ impl Likeable for PostLike { .get_result::(conn) .await } - async fn remove(pool: &DbPool, person_id: PersonId, post_id: PostId) -> Result { + async fn remove( + pool: &mut DbPool<'_>, + person_id: PersonId, + post_id: PostId, + ) -> Result { use crate::schema::post_like::dsl; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -262,7 +269,7 @@ impl Likeable for PostLike { #[async_trait] impl Saveable for PostSaved { type Form = PostSavedForm; - async fn save(pool: &DbPool, post_saved_form: &PostSavedForm) -> Result { + async fn save(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result { use crate::schema::post_saved::dsl::{person_id, post_id, post_saved}; let conn = &mut get_conn(pool).await?; insert_into(post_saved) @@ -273,7 +280,7 @@ impl Saveable for PostSaved { .get_result::(conn) .await } - async fn unsave(pool: &DbPool, post_saved_form: &PostSavedForm) -> Result { + async fn unsave(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result { use crate::schema::post_saved::dsl::{person_id, post_id, post_saved}; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -289,7 +296,10 @@ impl Saveable for PostSaved { #[async_trait] impl Readable for PostRead { type Form = PostReadForm; - async fn mark_as_read(pool: &DbPool, post_read_form: &PostReadForm) -> Result { + async fn mark_as_read( + pool: &mut DbPool<'_>, + post_read_form: &PostReadForm, + ) -> Result { use crate::schema::post_read::dsl::{person_id, post_id, post_read}; let conn = &mut get_conn(pool).await?; insert_into(post_read) @@ -301,7 +311,10 @@ impl Readable for PostRead { .await } - async fn mark_as_unread(pool: &DbPool, post_read_form: &PostReadForm) -> Result { + async fn mark_as_unread( + pool: &mut DbPool<'_>, + post_read_form: &PostReadForm, + ) -> Result { use crate::schema::post_read::dsl::{person_id, post_id, post_read}; let conn = &mut get_conn(pool).await?; diesel::delete( @@ -342,6 +355,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/post_report.rs b/crates/db_schema/src/impls/post_report.rs index ece7d090..b839d62d 100644 --- a/crates/db_schema/src/impls/post_report.rs +++ b/crates/db_schema/src/impls/post_report.rs @@ -18,7 +18,7 @@ impl Reportable for PostReport { type Form = PostReportForm; type IdType = PostReportId; - async fn report(pool: &DbPool, post_report_form: &PostReportForm) -> Result { + async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(post_report) .values(post_report_form) @@ -27,7 +27,7 @@ impl Reportable for PostReport { } async fn resolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -43,7 +43,7 @@ impl Reportable for PostReport { } async fn unresolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { diff --git a/crates/db_schema/src/impls/private_message.rs b/crates/db_schema/src/impls/private_message.rs index cf678227..2febaa76 100644 --- a/crates/db_schema/src/impls/private_message.rs +++ b/crates/db_schema/src/impls/private_message.rs @@ -15,7 +15,10 @@ impl Crud for PrivateMessage { type InsertForm = PrivateMessageInsertForm; type UpdateForm = PrivateMessageUpdateForm; type IdType = PrivateMessageId; - async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result { + async fn read( + pool: &mut DbPool<'_>, + private_message_id: PrivateMessageId, + ) -> Result { let conn = &mut get_conn(pool).await?; private_message .find(private_message_id) @@ -23,7 +26,7 @@ impl Crud for PrivateMessage { .await } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(private_message) .values(form) @@ -35,7 +38,7 @@ impl Crud for PrivateMessage { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, private_message_id: PrivateMessageId, form: &Self::UpdateForm, ) -> Result { @@ -45,7 +48,7 @@ impl Crud for PrivateMessage { .get_result::(conn) .await } - async fn delete(pool: &DbPool, pm_id: Self::IdType) -> Result { + async fn delete(pool: &mut DbPool<'_>, pm_id: Self::IdType) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(private_message.find(pm_id)) .execute(conn) @@ -55,7 +58,7 @@ impl Crud for PrivateMessage { impl PrivateMessage { pub async fn mark_all_as_read( - pool: &DbPool, + pool: &mut DbPool<'_>, for_recipient_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -70,7 +73,7 @@ impl PrivateMessage { } pub async fn read_from_apub_id( - pool: &DbPool, + pool: &mut DbPool<'_>, object_id: Url, ) -> Result, LemmyError> { let conn = &mut get_conn(pool).await?; @@ -103,6 +106,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_schema/src/impls/private_message_report.rs b/crates/db_schema/src/impls/private_message_report.rs index 5f2498c2..ca218796 100644 --- a/crates/db_schema/src/impls/private_message_report.rs +++ b/crates/db_schema/src/impls/private_message_report.rs @@ -18,7 +18,10 @@ impl Reportable for PrivateMessageReport { type Form = PrivateMessageReportForm; type IdType = PrivateMessageReportId; - async fn report(pool: &DbPool, pm_report_form: &PrivateMessageReportForm) -> Result { + async fn report( + pool: &mut DbPool<'_>, + pm_report_form: &PrivateMessageReportForm, + ) -> Result { let conn = &mut get_conn(pool).await?; insert_into(private_message_report) .values(pm_report_form) @@ -27,7 +30,7 @@ impl Reportable for PrivateMessageReport { } async fn resolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { @@ -43,7 +46,7 @@ impl Reportable for PrivateMessageReport { } async fn unresolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, by_resolver_id: PersonId, ) -> Result { diff --git a/crates/db_schema/src/impls/registration_application.rs b/crates/db_schema/src/impls/registration_application.rs index 208b01d0..c8c62288 100644 --- a/crates/db_schema/src/impls/registration_application.rs +++ b/crates/db_schema/src/impls/registration_application.rs @@ -18,7 +18,7 @@ impl Crud for RegistrationApplication { type UpdateForm = RegistrationApplicationUpdateForm; type IdType = i32; - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(registration_application) .values(form) @@ -26,13 +26,13 @@ impl Crud for RegistrationApplication { .await } - async fn read(pool: &DbPool, id_: Self::IdType) -> Result { + async fn read(pool: &mut DbPool<'_>, id_: Self::IdType) -> Result { let conn = &mut get_conn(pool).await?; registration_application.find(id_).first::(conn).await } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, id_: Self::IdType, form: &Self::UpdateForm, ) -> Result { @@ -43,7 +43,7 @@ impl Crud for RegistrationApplication { .await } - async fn delete(pool: &DbPool, id_: Self::IdType) -> Result { + async fn delete(pool: &mut DbPool<'_>, id_: Self::IdType) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(registration_application.find(id_)) .execute(conn) @@ -53,7 +53,7 @@ impl Crud for RegistrationApplication { impl RegistrationApplication { pub async fn find_by_local_user_id( - pool: &DbPool, + pool: &mut DbPool<'_>, local_user_id_: LocalUserId, ) -> Result { let conn = &mut get_conn(pool).await?; diff --git a/crates/db_schema/src/impls/secret.rs b/crates/db_schema/src/impls/secret.rs index 4e0d9c1b..f21c6c48 100644 --- a/crates/db_schema/src/impls/secret.rs +++ b/crates/db_schema/src/impls/secret.rs @@ -9,11 +9,11 @@ use diesel_async::RunQueryDsl; impl Secret { /// Initialize the Secrets from the DB. /// Warning: You should only call this once. - pub async fn init(pool: &DbPool) -> Result { + pub async fn init(pool: &mut DbPool<'_>) -> Result { Self::read_secrets(pool).await } - async fn read_secrets(pool: &DbPool) -> Result { + async fn read_secrets(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; secret.first::(conn).await } diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index 3363edc9..806def96 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -19,16 +19,16 @@ impl Crud for Site { type IdType = SiteId; /// Use SiteView::read_local, or Site::read_from_apub_id instead - async fn read(_pool: &DbPool, _site_id: SiteId) -> Result { + async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result { unimplemented!() } - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result { - let conn = &mut get_conn(pool).await?; + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let is_new_site = match &form.actor_id { Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(), None => true, }; + let conn = &mut get_conn(pool).await?; // Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible let site_ = insert_into(site) @@ -48,7 +48,7 @@ impl Crud for Site { } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, site_id: SiteId, new_site: &Self::UpdateForm, ) -> Result { @@ -59,14 +59,17 @@ impl Crud for Site { .await } - async fn delete(pool: &DbPool, site_id: SiteId) -> Result { + async fn delete(pool: &mut DbPool<'_>, site_id: SiteId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(site.find(site_id)).execute(conn).await } } impl Site { - pub async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result, Error> { + pub async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: &DbUrl, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; Ok( site @@ -79,7 +82,7 @@ impl Site { } // TODO this needs fixed - pub async fn read_remote_sites(pool: &DbPool) -> Result, Error> { + pub async fn read_remote_sites(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; site.order_by(id).offset(1).get_results::(conn).await } diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index ec985a3c..be4860e1 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -9,7 +9,7 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl}; impl Tagline { pub async fn replace( - pool: &DbPool, + pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, list_content: Option>, ) -> Result, Error> { @@ -32,12 +32,12 @@ impl Tagline { .get_result::(conn) .await?; } - Self::get_all_conn(conn, for_local_site_id).await + Self::get_all(&mut conn.into(), for_local_site_id).await }) as _ }) .await } else { - Self::get_all_conn(conn, for_local_site_id).await + Self::get_all(&mut conn.into(), for_local_site_id).await } } @@ -45,17 +45,14 @@ impl Tagline { diesel::delete(tagline).execute(conn).await } - async fn get_all_conn( - conn: &mut AsyncPgConnection, + pub async fn get_all( + pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; tagline .filter(local_site_id.eq(for_local_site_id)) .get_results::(conn) .await } - pub async fn get_all(pool: &DbPool, for_local_site_id: LocalSiteId) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - Self::get_all_conn(conn, for_local_site_id).await - } } diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 12b92b3d..0ba0cbff 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -9,17 +9,21 @@ pub trait Crud { type InsertForm; type UpdateForm; type IdType; - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result where Self: Sized; - async fn read(pool: &DbPool, id: Self::IdType) -> Result + async fn read(pool: &mut DbPool<'_>, 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(pool: &DbPool, id: Self::IdType, form: &Self::UpdateForm) -> Result + async fn update( + pool: &mut DbPool<'_>, + id: Self::IdType, + form: &Self::UpdateForm, + ) -> Result where Self: Sized; - async fn delete(_pool: &DbPool, _id: Self::IdType) -> Result + async fn delete(_pool: &mut DbPool<'_>, _id: Self::IdType) -> Result where Self: Sized, Self::IdType: Send, @@ -31,17 +35,17 @@ pub trait Crud { #[async_trait] pub trait Followable { type Form; - async fn follow(pool: &DbPool, form: &Self::Form) -> Result + async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn follow_accepted( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, person_id: PersonId, ) -> Result where Self: Sized; - async fn unfollow(pool: &DbPool, form: &Self::Form) -> Result + async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -49,10 +53,10 @@ pub trait Followable { #[async_trait] pub trait Joinable { type Form; - async fn join(pool: &DbPool, form: &Self::Form) -> Result + async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn leave(pool: &DbPool, form: &Self::Form) -> Result + async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -61,11 +65,11 @@ pub trait Joinable { pub trait Likeable { type Form; type IdType; - async fn like(pool: &DbPool, form: &Self::Form) -> Result + async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn remove( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id: PersonId, item_id: Self::IdType, ) -> Result @@ -76,10 +80,10 @@ pub trait Likeable { #[async_trait] pub trait Bannable { type Form; - async fn ban(pool: &DbPool, form: &Self::Form) -> Result + async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unban(pool: &DbPool, form: &Self::Form) -> Result + async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -87,10 +91,10 @@ pub trait Bannable { #[async_trait] pub trait Saveable { type Form; - async fn save(pool: &DbPool, form: &Self::Form) -> Result + async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unsave(pool: &DbPool, form: &Self::Form) -> Result + async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -98,10 +102,10 @@ pub trait Saveable { #[async_trait] pub trait Blockable { type Form; - async fn block(pool: &DbPool, form: &Self::Form) -> Result + async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unblock(pool: &DbPool, form: &Self::Form) -> Result + async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -109,10 +113,10 @@ pub trait Blockable { #[async_trait] pub trait Readable { type Form; - async fn mark_as_read(pool: &DbPool, form: &Self::Form) -> Result + async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn mark_as_unread(pool: &DbPool, form: &Self::Form) -> Result + async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -121,18 +125,18 @@ pub trait Readable { pub trait Reportable { type Form; type IdType; - async fn report(pool: &DbPool, form: &Self::Form) -> Result + async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn resolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, resolver_id: PersonId, ) -> Result where Self: Sized; async fn unresolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, resolver_id: PersonId, ) -> Result @@ -149,20 +153,23 @@ pub trait JoinView { #[async_trait] pub trait ApubActor { - async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result, Error> + async fn read_from_apub_id( + pool: &mut DbPool<'_>, + 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( - pool: &DbPool, + pool: &mut DbPool<'_>, actor_name: &str, include_deleted: bool, ) -> Result where Self: Sized; async fn read_from_name_and_domain( - pool: &DbPool, + pool: &mut DbPool<'_>, actor_name: &str, protocol_domain: &str, ) -> Result diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 26754055..5d5bfafa 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -39,6 +39,7 @@ use rustls::{ use std::{ env, env::VarError, + ops::{Deref, DerefMut}, sync::Arc, time::{Duration, SystemTime}, }; @@ -49,10 +50,101 @@ const FETCH_LIMIT_DEFAULT: i64 = 10; pub const FETCH_LIMIT_MAX: i64 = 50; const POOL_TIMEOUT: Option = Some(Duration::from_secs(5)); -pub type DbPool = Pool; +pub type ActualDbPool = Pool; -pub async fn get_conn(pool: &DbPool) -> Result, DieselError> { - pool.get().await.map_err(|e| QueryBuilderError(e.into())) +/// References a pool or connection. Functions must take `&mut DbPool<'_>` to allow implicit reborrowing. +/// +/// https://github.com/rust-lang/rfcs/issues/1403 +pub enum DbPool<'a> { + Pool(&'a ActualDbPool), + Conn(&'a mut AsyncPgConnection), +} + +pub enum DbConn<'a> { + Pool(PooledConnection), + Conn(&'a mut AsyncPgConnection), +} + +pub async fn get_conn<'a, 'b: 'a>(pool: &'a mut DbPool<'b>) -> Result, DieselError> { + Ok(match pool { + DbPool::Pool(pool) => DbConn::Pool(pool.get().await.map_err(|e| QueryBuilderError(e.into()))?), + DbPool::Conn(conn) => DbConn::Conn(conn), + }) +} + +impl<'a> Deref for DbConn<'a> { + type Target = AsyncPgConnection; + + fn deref(&self) -> &Self::Target { + match self { + DbConn::Pool(conn) => conn.deref(), + DbConn::Conn(conn) => conn.deref(), + } + } +} + +impl<'a> DerefMut for DbConn<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + match self { + DbConn::Pool(conn) => conn.deref_mut(), + DbConn::Conn(conn) => conn.deref_mut(), + } + } +} + +// Allows functions that take `DbPool<'_>` to be called in a transaction by passing `&mut conn.into()` +impl<'a> From<&'a mut AsyncPgConnection> for DbPool<'a> { + fn from(value: &'a mut AsyncPgConnection) -> Self { + DbPool::Conn(value) + } +} + +impl<'a, 'b: 'a> From<&'a mut DbConn<'b>> for DbPool<'a> { + fn from(value: &'a mut DbConn<'b>) -> Self { + DbPool::Conn(value.deref_mut()) + } +} + +impl<'a> From<&'a ActualDbPool> for DbPool<'a> { + fn from(value: &'a ActualDbPool) -> Self { + DbPool::Pool(value) + } +} + +/// Runs multiple async functions that take `&mut DbPool<'_>` as input and return `Result`. Only works when the `futures` crate is listed in `Cargo.toml`. +/// +/// `$pool` is the value given to each function. +/// +/// A `Result` is returned (not in a `Future`, so don't use `.await`). The `Ok` variant contains a tuple with the values returned by the given functions. +/// +/// The functions run concurrently if `$pool` has the `DbPool::Pool` variant. +#[macro_export] +macro_rules! try_join_with_pool { + ($pool:ident => ($($func:expr),+)) => {{ + // Check type + let _: &mut $crate::utils::DbPool<'_> = $pool; + + match $pool { + // Run concurrently with `try_join` + $crate::utils::DbPool::Pool(__pool) => ::futures::try_join!( + $(async { + let mut __dbpool = $crate::utils::DbPool::Pool(__pool); + ($func)(&mut __dbpool).await + }),+ + ), + // Run sequentially + $crate::utils::DbPool::Conn(__conn) => async { + Ok(($({ + let mut __dbpool = $crate::utils::DbPool::Conn(__conn); + // `?` prevents the error type from being inferred in an `async` block, so `match` is used instead + match ($func)(&mut __dbpool).await { + ::core::result::Result::Ok(__v) => __v, + ::core::result::Result::Err(__v) => return ::core::result::Result::Err(__v), + } + }),+)) + }.await, + } + }}; } pub fn get_database_url_from_env() -> Result { @@ -144,7 +236,9 @@ pub fn diesel_option_overwrite_to_url_create( } } -async fn build_db_pool_settings_opt(settings: Option<&Settings>) -> Result { +async fn build_db_pool_settings_opt( + settings: Option<&Settings>, +) -> Result { let db_url = get_database_url(settings); let pool_size = settings.map(|s| s.database.pool_size).unwrap_or(5); // We only support TLS with sslmode=require currently @@ -223,11 +317,11 @@ pub fn run_migrations(db_url: &str) { info!("Database migrations complete."); } -pub async fn build_db_pool(settings: &Settings) -> Result { +pub async fn build_db_pool(settings: &Settings) -> Result { build_db_pool_settings_opt(Some(settings)).await } -pub async fn build_db_pool_for_tests() -> DbPool { +pub async fn build_db_pool_for_tests() -> ActualDbPool { build_db_pool_settings_opt(None) .await .expect("db pool missing") diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 4a5397b3..9c50eb67 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( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: CommentReportId, my_person_id: PersonId, ) -> Result { @@ -96,7 +96,7 @@ impl CommentReportView { /// Returns the current unresolved post report count for the communities you mod pub async fn get_report_count( - pool: &DbPool, + pool: &mut DbPool<'_>, my_person_id: PersonId, admin: bool, community_id: Option, @@ -139,9 +139,9 @@ impl CommentReportView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentReportQuery<'a> { +pub struct CommentReportQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, #[builder(!default)] my_person_id: PersonId, #[builder(!default)] @@ -152,7 +152,7 @@ pub struct CommentReportQuery<'a> { unresolved_only: Option, } -impl<'a> CommentReportQuery<'a> { +impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; @@ -293,6 +293,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index f3b1f83a..3556f850 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( - pool: &DbPool, + pool: &mut DbPool<'_>, comment_id: CommentId, my_person_id: Option, ) -> Result { @@ -158,9 +158,9 @@ impl CommentView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentQuery<'a> { +pub struct CommentQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, listing_type: Option, sort: Option, community_id: Option, @@ -176,7 +176,7 @@ pub struct CommentQuery<'a> { max_depth: Option, } -impl<'a> CommentQuery<'a> { +impl<'a, 'b: 'a> CommentQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; @@ -431,7 +431,7 @@ mod tests { inserted_community: Community, } - async fn init_data(pool: &DbPool) -> Data { + async fn init_data(pool: &mut DbPool<'_>) -> Data { let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await .unwrap(); @@ -594,6 +594,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let expected_comment_view_no_person = expected_comment_view(&data, pool).await; @@ -651,6 +652,7 @@ mod tests { #[serial] async fn test_comment_tree() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let top_path = data.inserted_comment_0.path.clone(); @@ -727,6 +729,7 @@ mod tests { #[serial] async fn test_languages() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; // by default, user has all languages enabled and should see all comments @@ -781,7 +784,7 @@ mod tests { cleanup(data, pool).await; } - async fn cleanup(data: Data, pool: &DbPool) { + async fn cleanup(data: Data, pool: &mut DbPool<'_>) { CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id) .await .unwrap(); @@ -804,7 +807,7 @@ mod tests { .unwrap(); } - async fn expected_comment_view(data: &Data, pool: &DbPool) -> CommentView { + async fn expected_comment_view(data: &Data, pool: &mut DbPool<'_>) -> CommentView { let agg = CommentAggregates::read(pool, data.inserted_comment_0.id) .await .unwrap(); diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 66d583e3..d83fa991 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(pool: &DbPool, emoji_id: CustomEmojiId) -> Result { + pub async fn get(pool: &mut DbPool<'_>, emoji_id: CustomEmojiId) -> Result { let conn = &mut get_conn(pool).await?; let emojis = custom_emoji::table .find(emoji_id) @@ -35,7 +35,10 @@ impl CustomEmojiView { } } - pub async fn get_all(pool: &DbPool, for_local_site_id: LocalSiteId) -> Result, Error> { + pub async fn get_all( + pool: &mut DbPool<'_>, + for_local_site_id: LocalSiteId, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; let emojis = custom_emoji::table .filter(custom_emoji::local_site_id.eq(for_local_site_id)) diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index f37986cc..567ca3fe 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(pool: &DbPool, local_user_id: LocalUserId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { let conn = &mut get_conn(pool).await?; let (local_user, person, counts) = local_user::table @@ -34,7 +34,7 @@ impl LocalUserView { }) } - pub async fn read_person(pool: &DbPool, person_id: PersonId) -> Result { + pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; let (local_user, person, counts) = local_user::table .filter(person::id.eq(person_id)) @@ -54,7 +54,7 @@ impl LocalUserView { }) } - pub async fn read_from_name(pool: &DbPool, name: &str) -> Result { + pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result { let conn = &mut get_conn(pool).await?; let (local_user, person, counts) = local_user::table .filter(lower(person::name).eq(name.to_lowercase())) @@ -74,7 +74,10 @@ impl LocalUserView { }) } - pub async fn find_by_email_or_name(pool: &DbPool, name_or_email: &str) -> Result { + pub async fn find_by_email_or_name( + pool: &mut DbPool<'_>, + name_or_email: &str, + ) -> Result { let conn = &mut get_conn(pool).await?; let (local_user, person, counts) = local_user::table .inner_join(person::table) @@ -98,7 +101,7 @@ impl LocalUserView { }) } - pub async fn find_by_email(pool: &DbPool, from_email: &str) -> Result { + pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result { let conn = &mut get_conn(pool).await?; let (local_user, person, counts) = local_user::table .inner_join(person::table) @@ -118,7 +121,7 @@ impl LocalUserView { }) } - pub async fn list_admins_with_emails(pool: &DbPool) -> Result, Error> { + pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = local_user::table .filter(person::admin.eq(true)) diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 19822c79..1219e0db 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( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: PostReportId, my_person_id: PersonId, ) -> Result { @@ -121,7 +121,7 @@ impl PostReportView { /// returns the current unresolved post report count for the communities you mod pub async fn get_report_count( - pool: &DbPool, + pool: &mut DbPool<'_>, my_person_id: PersonId, admin: bool, community_id: Option, @@ -161,9 +161,9 @@ impl PostReportView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PostReportQuery<'a> { +pub struct PostReportQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, #[builder(!default)] my_person_id: PersonId, #[builder(!default)] @@ -174,7 +174,7 @@ pub struct PostReportQuery<'a> { unresolved_only: Option, } -impl<'a> PostReportQuery<'a> { +impl<'a, 'b: 'a> PostReportQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); @@ -288,6 +288,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index d1e974d8..07433667 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( - pool: &DbPool, + pool: &mut DbPool<'_>, post_id: PostId, my_person_id: Option, is_mod_or_admin: Option, @@ -195,9 +195,9 @@ impl PostView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PostQuery<'a> { +pub struct PostQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, listing_type: Option, sort: Option, creator_id: Option, @@ -212,7 +212,7 @@ pub struct PostQuery<'a> { limit: Option, } -impl<'a> PostQuery<'a> { +impl<'a, 'b: 'a> PostQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; @@ -510,7 +510,7 @@ mod tests { inserted_post: Post, } - async fn init_data(pool: &DbPool) -> Data { + async fn init_data(pool: &mut DbPool<'_>) -> Data { let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await .unwrap(); @@ -608,6 +608,7 @@ mod tests { #[serial] async fn post_listing_with_person() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let local_user_form = LocalUserUpdateForm::builder() @@ -676,6 +677,7 @@ mod tests { #[serial] async fn post_listing_no_person() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let read_post_listing_multiple_no_person = PostQuery::builder() @@ -713,6 +715,7 @@ mod tests { #[serial] async fn post_listing_block_community() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let community_block = CommunityBlockForm { @@ -743,6 +746,7 @@ mod tests { #[serial] async fn post_listing_like() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let post_like_form = PostLikeForm { @@ -809,6 +813,7 @@ mod tests { #[serial] async fn post_listing_person_language() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; let spanish_id = Language::read_id_from_code(pool, Some("es")) @@ -890,6 +895,7 @@ mod tests { #[serial] async fn post_listings_deleted() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let data = init_data(pool).await; // Delete the post @@ -930,7 +936,7 @@ mod tests { cleanup(data, pool).await; } - async fn cleanup(data: Data, pool: &DbPool) { + async fn cleanup(data: Data, pool: &mut DbPool<'_>) { let num_deleted = Post::delete(pool, data.inserted_post.id).await.unwrap(); Community::delete(pool, data.inserted_community.id) .await @@ -946,7 +952,7 @@ mod tests { assert_eq!(1, num_deleted); } - async fn expected_post_view(data: &Data, pool: &DbPool) -> PostView { + async fn expected_post_view(data: &Data, pool: &mut DbPool<'_>) -> PostView { let (inserted_person, inserted_community, inserted_post) = ( &data.inserted_person, &data.inserted_community, diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 2bcad6f8..74209f33 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(pool: &DbPool, report_id: PrivateMessageReportId) -> Result { + pub async fn read( + pool: &mut DbPool<'_>, + report_id: PrivateMessageReportId, + ) -> Result { let conn = &mut get_conn(pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); @@ -64,7 +67,7 @@ impl PrivateMessageReportView { } /// Returns the current unresolved post report count for the communities you mod - pub async fn get_report_count(pool: &DbPool) -> Result { + pub async fn get_report_count(pool: &mut DbPool<'_>) -> Result { use diesel::dsl::count; let conn = &mut get_conn(pool).await?; @@ -80,15 +83,15 @@ impl PrivateMessageReportView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PrivateMessageReportQuery<'a> { +pub struct PrivateMessageReportQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, page: Option, limit: Option, unresolved_only: Option, } -impl<'a> PrivateMessageReportQuery<'a> { +impl<'a, 'b: 'a> PrivateMessageReportQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); @@ -166,6 +169,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 76b817a9..3d8fd42d 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -22,7 +22,10 @@ use typed_builder::TypedBuilder; type PrivateMessageViewTuple = (PrivateMessage, Person, Person); impl PrivateMessageView { - pub async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result { + pub async fn read( + pool: &mut DbPool<'_>, + private_message_id: PrivateMessageId, + ) -> Result { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); @@ -49,7 +52,10 @@ impl PrivateMessageView { } /// Gets the number of unread messages - pub async fn get_unread_messages(pool: &DbPool, my_person_id: PersonId) -> Result { + pub async fn get_unread_messages( + pool: &mut DbPool<'_>, + my_person_id: PersonId, + ) -> Result { use diesel::dsl::count; let conn = &mut get_conn(pool).await?; private_message::table @@ -64,9 +70,9 @@ impl PrivateMessageView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PrivateMessageQuery<'a> { +pub struct PrivateMessageQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, #[builder(!default)] recipient_id: PersonId, unread_only: Option, @@ -74,7 +80,7 @@ pub struct PrivateMessageQuery<'a> { limit: Option, } -impl<'a> PrivateMessageQuery<'a> { +impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let person_alias_1 = diesel::alias!(person as person1); diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 9963ed46..ad6a7e9d 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(pool: &DbPool, registration_application_id: i32) -> Result { + pub async fn read( + pool: &mut DbPool<'_>, + registration_application_id: i32, + ) -> Result { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); @@ -58,7 +61,10 @@ impl RegistrationApplicationView { } /// Returns the current unread registration_application count - pub async fn get_unread_count(pool: &DbPool, verified_email_only: bool) -> Result { + pub async fn get_unread_count( + pool: &mut DbPool<'_>, + verified_email_only: bool, + ) -> Result { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); @@ -85,16 +91,16 @@ impl RegistrationApplicationView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct RegistrationApplicationQuery<'a> { +pub struct RegistrationApplicationQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, unread_only: Option, verified_email_only: Option, page: Option, limit: Option, } -impl<'a> RegistrationApplicationQuery<'a> { +impl<'a, 'b: 'a> RegistrationApplicationQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let person_alias_1 = diesel::alias!(person as person1); @@ -179,6 +185,7 @@ mod tests { #[serial] async fn test_crud() { let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) .await diff --git a/crates/db_views/src/site_view.rs b/crates/db_views/src/site_view.rs index a1d0231a..f80666ba 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(pool: &DbPool) -> Result { + pub async fn read_local(pool: &mut DbPool<'_>) -> Result { let conn = &mut get_conn(pool).await?; let (mut site, local_site, local_site_rate_limit, counts) = site::table .inner_join(local_site::table) diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 42c3a53e..08cc5a45 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( - pool: &DbPool, + pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId, my_person_id: Option, ) -> Result { @@ -155,7 +155,10 @@ impl CommentReplyView { } /// Gets the number of unread replies - pub async fn get_unread_replies(pool: &DbPool, my_person_id: PersonId) -> Result { + pub async fn get_unread_replies( + pool: &mut DbPool<'_>, + my_person_id: PersonId, + ) -> Result { use diesel::dsl::count; let conn = &mut get_conn(pool).await?; @@ -174,9 +177,9 @@ impl CommentReplyView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommentReplyQuery<'a> { +pub struct CommentReplyQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, my_person_id: Option, recipient_id: Option, sort: Option, @@ -186,7 +189,7 @@ pub struct CommentReplyQuery<'a> { limit: Option, } -impl<'a> CommentReplyQuery<'a> { +impl<'a, 'b: 'a> CommentReplyQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; diff --git a/crates/db_views_actor/src/community_block_view.rs b/crates/db_views_actor/src/community_block_view.rs index d87bea37..5c8903d9 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(pool: &DbPool, person_id: PersonId) -> Result, Error> { + pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = community_block::table .inner_join(person::table) diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index 11baddc0..c3050387 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -21,7 +21,7 @@ sql_function!(fn coalesce(x: diesel::sql_types::Nullable, community_id: CommunityId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -37,7 +37,7 @@ impl CommunityFollowerView { Ok(res) } pub async fn count_community_followers( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, ) -> Result { let conn = &mut get_conn(pool).await?; @@ -50,7 +50,7 @@ impl CommunityFollowerView { Ok(res) } - pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { + pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = community_follower::table .inner_join(community::table) diff --git a/crates/db_views_actor/src/community_moderator_view.rs b/crates/db_views_actor/src/community_moderator_view.rs index 113efe4b..2725565f 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 is_community_moderator( - pool: &DbPool, + pool: &mut DbPool<'_>, find_community_id: CommunityId, find_person_id: PersonId, ) -> Result { @@ -31,7 +31,10 @@ impl CommunityModeratorView { .get_result::(conn) .await } - pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result, Error> { + pub async fn for_community( + pool: &mut DbPool<'_>, + community_id: CommunityId, + ) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = community_moderator::table .inner_join(community::table) @@ -45,7 +48,7 @@ impl CommunityModeratorView { Ok(res.into_iter().map(Self::from_tuple).collect()) } - pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { + pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = community_moderator::table .inner_join(community::table) @@ -62,7 +65,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(pool: &DbPool) -> Result, Error> { + pub async fn get_community_first_mods(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let res = community_moderator::table .inner_join(community::table) 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 35eacf0f..705b4bf7 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( - pool: &DbPool, + pool: &mut DbPool<'_>, from_person_id: PersonId, from_community_id: CommunityId, ) -> Result { diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 7f05afca..74aba52f 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( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, my_person_id: Option, is_mod_or_admin: Option, @@ -86,7 +86,7 @@ impl CommunityView { } pub async fn is_mod_or_admin( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id: PersonId, community_id: CommunityId, ) -> Result { @@ -102,9 +102,9 @@ impl CommunityView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct CommunityQuery<'a> { +pub struct CommunityQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, listing_type: Option, sort: Option, local_user: Option<&'a LocalUser>, @@ -115,7 +115,7 @@ pub struct CommunityQuery<'a> { limit: Option, } -impl<'a> CommunityQuery<'a> { +impl<'a, 'b: 'a> CommunityQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { use SortType::*; diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index 6e10ee7b..b317740d 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(pool: &DbPool, person_id: PersonId) -> Result, Error> { + pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { let conn = &mut get_conn(pool).await?; let target_person_alias = diesel::alias!(person as person1); diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 803abdb3..caa91160 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( - pool: &DbPool, + pool: &mut DbPool<'_>, person_mention_id: PersonMentionId, my_person_id: Option, ) -> Result { @@ -156,7 +156,10 @@ impl PersonMentionView { } /// Gets the number of unread mentions - pub async fn get_unread_mentions(pool: &DbPool, my_person_id: PersonId) -> Result { + pub async fn get_unread_mentions( + pool: &mut DbPool<'_>, + my_person_id: PersonId, + ) -> Result { use diesel::dsl::count; let conn = &mut get_conn(pool).await?; @@ -174,9 +177,9 @@ impl PersonMentionView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PersonMentionQuery<'a> { +pub struct PersonMentionQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, my_person_id: Option, recipient_id: Option, sort: Option, @@ -186,7 +189,7 @@ pub struct PersonMentionQuery<'a> { limit: Option, } -impl<'a> PersonMentionQuery<'a> { +impl<'a, 'b: 'a> PersonMentionQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 06215123..3aee145c 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -24,7 +24,7 @@ use typed_builder::TypedBuilder; type PersonViewTuple = (Person, PersonAggregates); impl PersonView { - pub async fn read(pool: &DbPool, person_id: PersonId) -> Result { + pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; let res = person::table .find(person_id) @@ -35,7 +35,7 @@ impl PersonView { Ok(Self::from_tuple(res)) } - pub async fn is_admin(pool: &DbPool, person_id: PersonId) -> Result { + pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { use schema::person::dsl::{admin, id, person}; let conn = &mut get_conn(pool).await?; let is_admin = person @@ -45,7 +45,7 @@ impl PersonView { .await?; Ok(is_admin) } - pub async fn admins(pool: &DbPool) -> Result, Error> { + pub async fn admins(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admins = person::table .inner_join(person_aggregates::table) @@ -59,7 +59,7 @@ impl PersonView { Ok(admins.into_iter().map(Self::from_tuple).collect()) } - pub async fn banned(pool: &DbPool) -> Result, Error> { + pub async fn banned(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let banned = person::table .inner_join(person_aggregates::table) @@ -81,16 +81,16 @@ impl PersonView { #[derive(TypedBuilder)] #[builder(field_defaults(default))] -pub struct PersonQuery<'a> { +pub struct PersonQuery<'a, 'b: 'a> { #[builder(!default)] - pool: &'a DbPool, + pool: &'a mut DbPool<'b>, sort: Option, search_term: Option, page: Option, limit: Option, } -impl<'a> PersonQuery<'a> { +impl<'a, 'b: 'a> PersonQuery<'a, 'b> { pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let mut query = person::table 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 12e47fbb..ccfc2892 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; 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 e3a7a789..20c4ae35 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; 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 fff5b2d5..f29947ae 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 e37e66b1..8dc3940c 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 6df6376f..c1166b6d 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index ef408ce3..7094db12 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 c7fb7395..ceed974b 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index 7bdf0dcf..835804e6 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 66bd4264..16a645f2 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 c2c6294b..66de2a9d 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); 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 15ff7855..b970a3f6 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); 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 0d33974b..946e0eb3 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; 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)); 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 50f99e52..c9ca4ce1 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); let show_mod_names = !params.hide_modlog_names; 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 fff5df46..74cd3c48 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); 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 60da8e83..d84ed87d 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(pool: &DbPool, params: ModlogListParams) -> Result, Error> { + pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result, Error> { let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 2d894c68..8429f860 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -122,10 +122,10 @@ async fn get_feed_data( limit: i64, page: i64, ) -> Result { - let site_view = SiteView::read_local(context.pool()).await?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let posts = PostQuery::builder() - .pool(context.pool()) + .pool(&mut context.pool()) .listing_type(Some(listing_type)) .sort(Some(sort_type)) .limit(Some(limit)) @@ -178,7 +178,7 @@ async fn get_feed( let builder = match request_type { RequestType::User => { get_feed_user( - context.pool(), + &mut context.pool(), &info.sort_type()?, &info.get_limit(), &info.get_page(), @@ -189,7 +189,7 @@ async fn get_feed( } RequestType::Community => { get_feed_community( - context.pool(), + &mut context.pool(), &info.sort_type()?, &info.get_limit(), &info.get_page(), @@ -200,7 +200,7 @@ async fn get_feed( } RequestType::Front => { get_feed_front( - context.pool(), + &mut context.pool(), &jwt_secret, &info.sort_type()?, &info.get_limit(), @@ -211,7 +211,13 @@ async fn get_feed( .await } RequestType::Inbox => { - get_feed_inbox(context.pool(), &jwt_secret, ¶m, &protocol_and_hostname).await + get_feed_inbox( + &mut context.pool(), + &jwt_secret, + ¶m, + &protocol_and_hostname, + ) + .await } } .map_err(ErrorBadRequest)?; @@ -227,7 +233,7 @@ async fn get_feed( #[tracing::instrument(skip_all)] async fn get_feed_user( - pool: &DbPool, + pool: &mut DbPool<'_>, sort_type: &SortType, limit: &i64, page: &i64, @@ -262,7 +268,7 @@ async fn get_feed_user( #[tracing::instrument(skip_all)] async fn get_feed_community( - pool: &DbPool, + pool: &mut DbPool<'_>, sort_type: &SortType, limit: &i64, page: &i64, @@ -300,7 +306,7 @@ async fn get_feed_community( #[tracing::instrument(skip_all)] async fn get_feed_front( - pool: &DbPool, + pool: &mut DbPool<'_>, jwt_secret: &str, sort_type: &SortType, limit: &i64, @@ -341,7 +347,7 @@ async fn get_feed_front( #[tracing::instrument(skip_all)] async fn get_feed_inbox( - pool: &DbPool, + pool: &mut DbPool<'_>, jwt_secret: &str, jwt: &str, protocol_and_hostname: &str, diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index 9b7801f2..b79a38ff 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(context.pool()) + let local_site = LocalSite::read(&mut context.pool()) .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 124e1ea4..ef654462 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(context.pool()) + let site_view = SiteView::read_local(&mut context.pool()) .await .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?; diff --git a/crates/routes/src/webfinger.rs b/crates/routes/src/webfinger.rs index 79de351a..72adc950 100644 --- a/crates/routes/src/webfinger.rs +++ b/crates/routes/src/webfinger.rs @@ -38,11 +38,11 @@ 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(context.pool(), &name_, false) + let user_id: Option = Person::read_from_name(&mut context.pool(), &name_, false) .await .ok() .map(|c| c.actor_id.into()); - let community_id: Option = Community::read_from_name(context.pool(), &name, false) + let community_id: Option = Community::read_from_name(&mut context.pool(), &name, false) .await .ok() .map(|c| c.actor_id.into()); diff --git a/src/code_migrations.rs b/src/code_migrations.rs index c69ce591..ae7df397 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -39,7 +39,10 @@ use lemmy_utils::{error::LemmyError, settings::structs::Settings}; use tracing::info; use url::Url; -pub async fn run_advanced_migrations(pool: &DbPool, settings: &Settings) -> Result<(), LemmyError> { +pub async fn run_advanced_migrations( + pool: &mut DbPool<'_>, + settings: &Settings, +) -> Result<(), LemmyError> { let protocol_and_hostname = &settings.get_protocol_and_hostname(); user_updates_2020_04_02(pool, protocol_and_hostname).await?; community_updates_2020_04_02(pool, protocol_and_hostname).await?; @@ -56,7 +59,7 @@ pub async fn run_advanced_migrations(pool: &DbPool, settings: &Settings) -> Resu } async fn user_updates_2020_04_02( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::person::dsl::{actor_id, local, person}; @@ -94,7 +97,7 @@ async fn user_updates_2020_04_02( } async fn community_updates_2020_04_02( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::community::dsl::{actor_id, community, local}; @@ -133,7 +136,7 @@ async fn community_updates_2020_04_02( } async fn post_updates_2020_04_03( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::post::dsl::{ap_id, local, post}; @@ -168,7 +171,7 @@ async fn post_updates_2020_04_03( } async fn comment_updates_2020_04_03( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::comment::dsl::{ap_id, comment, local}; @@ -203,7 +206,7 @@ async fn comment_updates_2020_04_03( } async fn private_message_updates_2020_05_05( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::private_message::dsl::{ap_id, local, private_message}; @@ -240,7 +243,7 @@ async fn private_message_updates_2020_05_05( } async fn post_thumbnail_url_updates_2020_07_27( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { use lemmy_db_schema::schema::post::dsl::{post, thumbnail_url}; @@ -271,7 +274,7 @@ 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(pool: &DbPool) -> Result<(), LemmyError> { +async fn apub_columns_2021_02_02(pool: &mut DbPool<'_>) -> Result<(), LemmyError> { let conn = &mut get_conn(pool).await?; info!("Running apub_columns_2021_02_02"); { @@ -329,7 +332,7 @@ async fn apub_columns_2021_02_02(pool: &DbPool) -> 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( - pool: &DbPool, + pool: &mut DbPool<'_>, protocol_and_hostname: &str, ) -> Result<(), LemmyError> { info!("Running instance_actor_2021_09_29"); @@ -358,7 +361,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(pool: &DbPool) -> Result<(), LemmyError> { +async fn regenerate_public_keys_2022_07_05(pool: &mut DbPool<'_>) -> Result<(), LemmyError> { let conn = &mut get_conn(pool).await?; info!("Running regenerate_public_keys_2022_07_05"); @@ -380,7 +383,7 @@ async fn regenerate_public_keys_2022_07_05(pool: &DbPool) -> Result<(), LemmyErr .public_key(Some(key_pair.public_key)) .private_key(Some(Some(key_pair.private_key))) .build(); - Community::update(pool, community_.id, &form).await?; + Community::update(&mut conn.into(), community_.id, &form).await?; } } @@ -413,7 +416,7 @@ async fn regenerate_public_keys_2022_07_05(pool: &DbPool) -> Result<(), LemmyErr /// 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( - pool: &DbPool, + pool: &mut DbPool<'_>, settings: &Settings, ) -> Result<(), LemmyError> { info!("Running initialize_local_site_2022_10_10"); diff --git a/src/lib.rs b/src/lib.rs index 9e081376..b50298b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,15 +75,15 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> { let pool = build_db_pool(&settings).await?; // Run the Code-required migrations - run_advanced_migrations(&pool, &settings).await?; + run_advanced_migrations(&mut (&pool).into(), &settings).await?; // Initialize the secrets - let secret = Secret::init(&pool) + let secret = Secret::init(&mut (&pool).into()) .await .expect("Couldn't initialize secrets."); // Make sure the local site is set up. - let site_view = SiteView::read_local(&pool) + let site_view = SiteView::read_local(&mut (&pool).into()) .await .expect("local site not set up"); let local_site = site_view.local_site; @@ -153,7 +153,7 @@ pub async fn start_lemmy_server() -> Result<(), LemmyError> { .retry_count(settings.retry_count) .debug(*SYNCHRONOUS_FEDERATION) .http_signature_compat(true) - .url_verifier(Box::new(VerifyUrlData(context.pool().clone()))) + .url_verifier(Box::new(VerifyUrlData(context.inner_pool().clone()))) .build() .await?; diff --git a/src/prometheus_metrics.rs b/src/prometheus_metrics.rs index 891ef243..9e0ffde3 100644 --- a/src/prometheus_metrics.rs +++ b/src/prometheus_metrics.rs @@ -107,7 +107,7 @@ fn create_db_pool_metrics() -> DbPoolMetrics { } async fn collect_db_pool_metrics(context: &PromContext) { - let pool_status = context.lemmy.pool().status(); + let pool_status = context.lemmy.inner_pool().status(); context .db_pool_metrics .max_size