From 3021e2c9b867a235dab3a2ff80d15098ec750cf5 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 30 Jun 2023 18:23:03 +0000 Subject: [PATCH] Revert "Fix style mistakes (partial)" This reverts commit 48a033b87f4fdc1ce14ff86cc019e1c703cd2741. --- crates/api/src/comment_report/list.rs | 4 ++-- crates/api/src/lib.rs | 1 + .../local_user/notifications/list_mentions.rs | 4 ++-- .../local_user/notifications/list_replies.rs | 4 ++-- crates/api/src/post_report/list.rs | 4 ++-- crates/api/src/private_message_report/list.rs | 4 ++-- .../site/registration_applications/list.rs | 2 ++ crates/api_crud/src/community/list.rs | 2 ++ crates/api_crud/src/site/create.rs | 1 + crates/api_crud/src/site/read.rs | 4 +--- .../activities/create_or_update/comment.rs | 1 - crates/apub/src/api/list_comments.rs | 2 ++ crates/apub/src/api/list_posts.rs | 2 ++ crates/apub/src/api/read_person.rs | 4 ++++ crates/apub/src/api/search.rs | 2 ++ .../src/collections/community_featured.rs | 4 +--- .../src/collections/community_moderators.rs | 12 ++++------ .../apub/src/collections/community_outbox.rs | 6 ++--- crates/apub/src/lib.rs | 4 +--- crates/apub/src/objects/community.rs | 6 ++--- crates/apub/src/objects/instance.rs | 24 +++++++------------ 21 files changed, 45 insertions(+), 52 deletions(-) diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index a635b4330..d642c85af 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -19,8 +19,6 @@ impl Perform for ListCommentReports { &self, context: &Data, ) -> Result { - let mut conn = context.conn().await?; - let data: &ListCommentReports = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -31,6 +29,8 @@ impl Perform for ListCommentReports { let page = data.page; let limit = data.limit; + let mut conn = context.conn().await?; + let comment_reports = CommentReportQuery::builder() .conn(&mut conn) .my_person_id(person_id) diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 632e2c3bd..a3d42c046 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -70,6 +70,7 @@ mod tests { #[serial] async fn test_should_not_validate_user_token_after_password_change() { let conn = &mut build_db_conn_for_tests().await; + let secret = Secret::init(conn).await.unwrap(); let settings = &SETTINGS.to_owned(); diff --git a/crates/api/src/local_user/notifications/list_mentions.rs b/crates/api/src/local_user/notifications/list_mentions.rs index 2743e46c3..31b47f830 100644 --- a/crates/api/src/local_user/notifications/list_mentions.rs +++ b/crates/api/src/local_user/notifications/list_mentions.rs @@ -17,8 +17,6 @@ impl Perform for GetPersonMentions { &self, context: &Data, ) -> Result { - let mut conn = context.conn().await?; - let data: &GetPersonMentions = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -29,6 +27,8 @@ impl Perform for GetPersonMentions { let person_id = Some(local_user_view.person.id); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); + let mut conn = context.conn().await?; + let mentions = PersonMentionQuery::builder() .conn(&mut conn) .recipient_id(person_id) diff --git a/crates/api/src/local_user/notifications/list_replies.rs b/crates/api/src/local_user/notifications/list_replies.rs index 304a40cc8..f328fca5d 100644 --- a/crates/api/src/local_user/notifications/list_replies.rs +++ b/crates/api/src/local_user/notifications/list_replies.rs @@ -14,8 +14,6 @@ impl Perform for GetReplies { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { - let mut conn = context.conn().await?; - let data: &GetReplies = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -26,6 +24,8 @@ impl Perform for GetReplies { let person_id = Some(local_user_view.person.id); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); + let mut conn = context.conn().await?; + let replies = CommentReplyQuery::builder() .conn(&mut conn) .recipient_id(person_id) diff --git a/crates/api/src/post_report/list.rs b/crates/api/src/post_report/list.rs index 0b1df6b37..1b13d5f27 100644 --- a/crates/api/src/post_report/list.rs +++ b/crates/api/src/post_report/list.rs @@ -19,8 +19,6 @@ impl Perform for ListPostReports { &self, context: &Data, ) -> Result { - let mut conn = context.conn().await?; - let data: &ListPostReports = self; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; @@ -31,6 +29,8 @@ impl Perform for ListPostReports { let page = data.page; let limit = data.limit; + let mut conn = context.conn().await?; + let post_reports = PostReportQuery::builder() .conn(&mut conn) .my_person_id(person_id) diff --git a/crates/api/src/private_message_report/list.rs b/crates/api/src/private_message_report/list.rs index 5f872c27c..a5ee76f85 100644 --- a/crates/api/src/private_message_report/list.rs +++ b/crates/api/src/private_message_report/list.rs @@ -14,8 +14,6 @@ impl Perform for ListPrivateMessageReports { #[tracing::instrument(skip(context))] async fn perform(&self, context: &Data) -> Result { - let mut conn = context.conn().await?; - let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; is_admin(&local_user_view)?; @@ -23,6 +21,8 @@ impl Perform for ListPrivateMessageReports { let unresolved_only = self.unresolved_only; let page = self.page; let limit = self.limit; + let mut conn = context.conn().await?; + let private_message_reports = PrivateMessageReportQuery::builder() .conn(&mut conn) .unresolved_only(unresolved_only) diff --git a/crates/api/src/site/registration_applications/list.rs b/crates/api/src/site/registration_applications/list.rs index 5d6c8cf86..cbbb92743 100644 --- a/crates/api/src/site/registration_applications/list.rs +++ b/crates/api/src/site/registration_applications/list.rs @@ -29,6 +29,8 @@ impl Perform for ListRegistrationApplications { let page = data.page; let limit = data.limit; + let mut conn = context.conn().await?; + let registration_applications = RegistrationApplicationQuery::builder() .conn(&mut conn) .unread_only(unread_only) diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index c86e27eee..0727f4cf2 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -33,6 +33,8 @@ impl PerformCrud for ListCommunities { let page = data.page; let limit = data.limit; let local_user = local_user_view.map(|l| l.local_user); + let mut conn = context.conn().await?; + let communities = CommunityQuery::builder() .conn(&mut conn) .listing_type(listing_type) diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 2f70f746b..5d840ceda 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -50,6 +50,7 @@ impl PerformCrud for CreateSite { let mut conn = context.conn().await?; let data: &CreateSite = self; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(&mut conn).await?; diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 79ff4b5a4..eff53a344 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -102,15 +102,13 @@ async fn local_user_settings_view_from_jwt_opt( jwt: Option<&Sensitive>, context: &LemmyContext, ) -> Option { - let mut conn = context.conn().await().ok()?; - match jwt { Some(jwt) => { let claims = Claims::decode(jwt.as_ref(), &context.secret().jwt_secret) .ok()? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(&mut conn, local_user_id) + let local_user_view = LocalUserView::read(&mut *context.conn().await.ok()?, local_user_id) .await .ok()?; check_user_valid( diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index c87a741ef..923fd5edf 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -90,7 +90,6 @@ impl CreateOrUpdateNote { context: &Data, ) -> Result<(), LemmyError> { let mut conn = context.conn().await?; - // TODO: might be helpful to add a comment method to retrieve community directly let post_id = comment.post_id; let post = Post::read(&mut conn, post_id).await?; diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index 65c0d5989..6738b9c22 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -56,6 +56,8 @@ impl PerformApub for GetComments { let parent_path_cloned = parent_path.clone(); let post_id = data.post_id; let local_user = local_user_view.map(|l| l.local_user); + let mut conn = context.conn().await?; + let comments = CommentQuery::builder() .conn(&mut conn) .listing_type(Some(listing_type)) diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index c3bd00dc5..38776c00b 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -47,6 +47,8 @@ impl PerformApub for GetPosts { .await .is_ok(); + let mut conn = context.conn().await?; + let posts = PostQuery::builder() .conn(&mut conn) .local_user(local_user_view.map(|l| l.local_user).as_ref()) diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 06948e61b..60d1e38dd 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -65,6 +65,8 @@ impl PerformApub for GetPersonDetails { let local_user = local_user_view.map(|l| l.local_user); let local_user_clone = local_user.clone(); + let mut conn = context.conn().await?; + let posts_query = PostQuery::builder() .conn(&mut conn) .sort(sort) @@ -87,6 +89,8 @@ impl PerformApub for GetPersonDetails { } .await?; + let mut conn = context.conn().await?; + let comments_query = CommentQuery::builder() .conn(&mut conn) .local_user(local_user_clone.as_ref()) diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index f89e163f0..57e09de42 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -58,6 +58,8 @@ impl PerformApub for Search { }; let creator_id = data.creator_id; let local_user = local_user_view.map(|l| l.local_user); + let mut conn = context.conn().await?; + match search_type { SearchType::Posts => { posts = PostQuery::builder() diff --git a/crates/apub/src/collections/community_featured.rs b/crates/apub/src/collections/community_featured.rs index d395883c8..66673eeea 100644 --- a/crates/apub/src/collections/community_featured.rs +++ b/crates/apub/src/collections/community_featured.rs @@ -28,10 +28,8 @@ impl Collection for ApubCommunityFeatured { owner: &Self::Owner, data: &Data, ) -> Result { - let mut conn = data.conn().await?; - let ordered_items = try_join_all( - Post::list_featured_for_community(&mut conn, owner.id) + Post::list_featured_for_community(&mut *data.conn().await?, 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 13cc3f524..d178ffaf0 100644 --- a/crates/apub/src/collections/community_moderators.rs +++ b/crates/apub/src/collections/community_moderators.rs @@ -33,10 +33,8 @@ impl Collection for ApubCommunityModerators { owner: &Self::Owner, data: &Data, ) -> Result { - let mut conn = data.conn().await?; - let moderators = - CommunityModeratorView::for_community(&mut conn, owner.id).await?; + CommunityModeratorView::for_community(&mut *data.conn().await?, owner.id).await?; let ordered_items = moderators .into_iter() .map(|m| ObjectId::::from(m.moderator.actor_id)) @@ -64,11 +62,9 @@ impl Collection for ApubCommunityModerators { owner: &Self::Owner, data: &Data, ) -> Result { - let mut conn = data.conn().await?; - let community_id = owner.id; let current_moderators = - CommunityModeratorView::for_community(&mut conn, community_id).await?; + CommunityModeratorView::for_community(&mut *data.conn().await?, 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()); @@ -77,7 +73,7 @@ impl Collection for ApubCommunityModerators { community_id: mod_user.community.id, person_id: mod_user.moderator.id, }; - CommunityModerator::leave(&mut conn, &community_moderator_form).await?; + CommunityModerator::leave(&mut *data.conn().await?, &community_moderator_form).await?; } } @@ -94,7 +90,7 @@ impl Collection for ApubCommunityModerators { community_id: owner.id, person_id: mod_user.id, }; - CommunityModerator::join(&mut conn, &community_moderator_form).await?; + CommunityModerator::join(&mut *data.conn().await?, &community_moderator_form).await?; } } diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index 6a5582637..8ef0ce0be 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -41,16 +41,14 @@ impl Collection for ApubCommunityOutbox { owner: &Self::Owner, data: &Data, ) -> Result { - let mut conn = data.conn().await?; - - let post_list: Vec = Post::list_for_community(&mut conn, owner.id) + let post_list: Vec = Post::list_for_community(&mut *data.conn().await?, owner.id) .await? .into_iter() .map(Into::into) .collect(); let mut ordered_items = vec![]; for post in post_list { - let person = Person::read(&mut conn, post.creator_id) + let person = Person::read(&mut *data.conn().await?, post.creator_id) .await? .into(); let create = diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 16300d6d7..c16681f35 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -169,8 +169,6 @@ async fn insert_activity( where T: Serialize, { - let mut conn = data.conn().await?; - let ap_id = ap_id.clone().into(); let form = ActivityInsertForm { ap_id, @@ -179,7 +177,7 @@ where sensitive: Some(sensitive), updated: None, }; - Activity::create(&mut conn, &form).await?; + Activity::create(&mut *data.conn().await?, &form).await?; Ok(()) } diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index 4f0f2abb8..e5a1f69a7 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -86,11 +86,9 @@ impl Object for ApubCommunity { #[tracing::instrument(skip_all)] async fn into_json(self, data: &Data) -> Result { - let mut conn = data.conn().await?; - let community_id = self.id; - let langs = CommunityLanguage::read(&mut conn, community_id).await?; - let language = LanguageTag::new_multiple(langs, &mut conn).await?; + let langs = CommunityLanguage::read(&mut *data.conn().await?, community_id).await?; + let language = LanguageTag::new_multiple(langs, &mut *data.conn().await?).await?; let group = Group { kind: GroupType::Group, diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index 1c4cb6cf0..bd8212886 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -70,10 +70,8 @@ impl Object for ApubSite { object_id: Url, data: &Data, ) -> Result, LemmyError> { - let mut conn = data.conn().await?; - Ok( - Site::read_from_apub_id(&mut conn, &object_id.into()) + Site::read_from_apub_id(&mut *data.conn().await?, &object_id.into()) .await? .map(Into::into), ) @@ -85,11 +83,9 @@ impl Object for ApubSite { #[tracing::instrument(skip_all)] async fn into_json(self, data: &Data) -> Result { - let mut conn = data.conn().await?; - let site_id = self.id; - let langs = SiteLanguage::read(&mut conn, site_id).await?; - let language = LanguageTag::new_multiple(langs, &mut conn).await?; + let langs = SiteLanguage::read(&mut *data.conn().await?, site_id).await?; + let language = LanguageTag::new_multiple(langs, &mut *data.conn().await?).await?; let instance = Instance { kind: ApplicationType::Application, @@ -117,9 +113,7 @@ impl Object for ApubSite { expected_domain: &Url, data: &Data, ) -> Result<(), LemmyError> { - let mut conn = data.conn().await?; - - let local_site_data = fetch_local_site_data(&mut conn).await?; + let local_site_data = fetch_local_site_data(&mut *data.conn().await?).await?; check_apub_id_valid_with_strictness(apub.id.inner(), true, &local_site_data, data.settings())?; verify_domains_match(expected_domain, apub.id.inner())?; @@ -133,10 +127,8 @@ impl Object for ApubSite { #[tracing::instrument(skip_all)] async fn from_json(apub: Self::Kind, data: &Data) -> Result { - let mut conn = data.conn().await?; - let domain = apub.id.inner().domain().expect("group id has domain"); - let instance = DbInstance::read_or_create(&mut conn, domain.to_string()).await?; + let instance = DbInstance::read_or_create(&mut *data.conn().await?, domain.to_string()).await?; let site_form = SiteInsertForm { name: apub.name.clone(), @@ -153,10 +145,10 @@ impl Object for ApubSite { instance_id: instance.id, }; let languages = - LanguageTag::to_language_id_multiple(apub.language, &mut conn).await?; + LanguageTag::to_language_id_multiple(apub.language, &mut *data.conn().await?).await?; - let site = Site::create(&mut conn, &site_form).await?; - SiteLanguage::update(&mut conn, languages, &site).await?; + let site = Site::create(&mut *data.conn().await?, &site_form).await?; + SiteLanguage::update(&mut *data.conn().await?, languages, &site).await?; Ok(site.into()) } }