From 6ff2ec3dac91a3d78b2935e5cca035211b5b0626 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 20 Sep 2024 09:20:40 -0400 Subject: [PATCH] Get rid of a lot of pointless mut form initializations. - Fixes #5036 --- .../site/registration_applications/tests.rs | 12 +-- crates/api_crud/src/comment/create.rs | 7 +- crates/api_crud/src/community/create.rs | 36 ++++---- crates/api_crud/src/post/create.rs | 22 ++--- crates/apub/src/http/community.rs | 18 ++-- crates/apub/src/objects/community.rs | 46 +++++----- crates/apub/src/objects/post.rs | 24 +++--- crates/db_schema/src/impls/instance.rs | 6 +- crates/db_views/src/comment_view.rs | 70 +++++++++------- crates/db_views/src/post_view.rs | 83 +++++++++++-------- crates/federate/src/lib.rs | 6 +- crates/federate/src/worker.rs | 6 +- src/code_migrations.rs | 37 +++++---- src/scheduled_tasks.rs | 6 +- 14 files changed, 214 insertions(+), 165 deletions(-) diff --git a/crates/api/src/site/registration_applications/tests.rs b/crates/api/src/site/registration_applications/tests.rs index b41f355e9..a334d8393 100644 --- a/crates/api/src/site/registration_applications/tests.rs +++ b/crates/api/src/site/registration_applications/tests.rs @@ -63,11 +63,13 @@ async fn create_test_site(context: &Data) -> LemmyResult<(Instance // Create a local site, since this is necessary for determining if email verification is // required - let mut local_site_form = LocalSiteInsertForm::new(site.id); - local_site_form.require_email_verification = Some(true); - local_site_form.application_question = Some(".".to_string()); - local_site_form.registration_mode = Some(RegistrationMode::RequireApplication); - local_site_form.site_setup = Some(true); + let local_site_form = LocalSiteInsertForm { + require_email_verification: Some(true), + application_question: Some(".".to_string()), + registration_mode: Some(RegistrationMode::RequireApplication), + site_setup: Some(true), + ..LocalSiteInsertForm::new(site.id) + }; let local_site = LocalSite::create(pool, &local_site_form).await.unwrap(); // Required to have a working local SiteView when updating the site to change email verification diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index be3f98107..b4e52d3f9 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -111,9 +111,10 @@ pub async fn create_comment( } }; - let mut comment_form = - CommentInsertForm::new(local_user_view.person.id, data.post_id, content.clone()); - comment_form.language_id = language_id; + let comment_form = CommentInsertForm { + language_id, + ..CommentInsertForm::new(local_user_view.person.id, data.post_id, content.clone()) + }; // Create the comment let parent_path = parent_opt.clone().map(|t| t.path); diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index bf21f5260..4e3e356e0 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -88,23 +88,25 @@ pub async fn create_community( // When you create a community, make sure the user becomes a moderator and a follower let keypair = generate_actor_keypair()?; - let mut community_form = CommunityInsertForm::new( - site_view.site.instance_id, - data.name.clone(), - data.title.clone(), - keypair.public_key, - ); - community_form.description = description; - community_form.icon = icon; - community_form.banner = banner; - community_form.nsfw = data.nsfw; - community_form.actor_id = Some(community_actor_id.clone()); - community_form.private_key = Some(keypair.private_key); - community_form.followers_url = Some(generate_followers_url(&community_actor_id)?); - community_form.inbox_url = Some(generate_inbox_url(&community_actor_id)?); - community_form.shared_inbox_url = Some(generate_shared_inbox_url(context.settings())?); - community_form.posting_restricted_to_mods = data.posting_restricted_to_mods; - community_form.visibility = data.visibility; + let community_form = CommunityInsertForm { + description, + icon, + banner, + nsfw: data.nsfw, + actor_id: Some(community_actor_id.clone()), + private_key: Some(keypair.private_key), + followers_url: Some(generate_followers_url(&community_actor_id)?), + inbox_url: Some(generate_inbox_url(&community_actor_id)?), + shared_inbox_url: Some(generate_shared_inbox_url(context.settings())?), + posting_restricted_to_mods: data.posting_restricted_to_mods, + visibility: data.visibility, + ..CommunityInsertForm::new( + site_view.site.instance_id, + data.name.clone(), + data.title.clone(), + keypair.public_key, + ) + }; let inserted_community = Community::create(&mut context.pool(), &community_form) .await diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index f8f3e89df..e5ecab60c 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -130,16 +130,18 @@ pub async fn create_post( } }; - let mut post_form = PostInsertForm::new( - data.name.trim().to_string(), - local_user_view.person.id, - data.community_id, - ); - post_form.url = url.map(Into::into); - post_form.body = body; - post_form.alt_text = data.alt_text.clone(); - post_form.nsfw = data.nsfw; - post_form.language_id = language_id; + let post_form = PostInsertForm { + url: url.map(Into::into), + body, + alt_text: data.alt_text.clone(), + nsfw: data.nsfw, + language_id, + ..PostInsertForm::new( + data.name.trim().to_string(), + local_user_view.person.id, + data.community_id, + ) + }; let inserted_post = Post::create(&mut context.pool(), &post_form) .await diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index 38b96df30..da72319d5 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -151,14 +151,16 @@ pub(crate) mod tests { Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?; create_local_site(context, instance.id).await?; - let mut community_form = CommunityInsertForm::new( - instance.id, - "testcom6".to_string(), - "nada".to_owned(), - "pubkey".to_string(), - ); - community_form.deleted = Some(deleted); - community_form.visibility = Some(visibility); + let community_form = CommunityInsertForm { + deleted: Some(deleted), + visibility: Some(visibility), + ..CommunityInsertForm::new( + instance.id, + "testcom6".to_string(), + "nada".to_owned(), + "pubkey".to_string(), + ) + }; let community = Community::create(&mut context.pool(), &community_form).await?; Ok((instance, community)) } diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index f2cca00df..d7e2490a7 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -151,28 +151,30 @@ impl Object for ApubCommunity { let icon = proxy_image_link_opt_apub(group.icon.map(|i| i.url), context).await?; let banner = proxy_image_link_opt_apub(group.image.map(|i| i.url), context).await?; - let mut form = CommunityInsertForm::new( - instance_id, - group.preferred_username.clone(), - group.name.unwrap_or(group.preferred_username.clone()), - group.public_key.public_key_pem, - ); - form.published = group.published; - form.updated = group.updated; - form.deleted = Some(false); - form.nsfw = Some(group.sensitive.unwrap_or(false)); - form.actor_id = Some(group.id.into()); - form.local = Some(false); - form.last_refreshed_at = Some(naive_now()); - form.icon = icon; - form.banner = banner; - form.description = description; - form.followers_url = group.followers.clone().map(Into::into); - form.inbox_url = Some(group.inbox.into()); - form.shared_inbox_url = group.endpoints.map(|e| e.shared_inbox.into()); - form.moderators_url = group.attributed_to.clone().map(Into::into); - form.posting_restricted_to_mods = group.posting_restricted_to_mods; - form.featured_url = group.featured.clone().map(Into::into); + let form = CommunityInsertForm { + published: group.published, + updated: group.updated, + deleted: Some(false), + nsfw: Some(group.sensitive.unwrap_or(false)), + actor_id: Some(group.id.into()), + local: Some(false), + last_refreshed_at: Some(naive_now()), + icon, + banner, + description, + followers_url: group.followers.clone().map(Into::into), + inbox_url: Some(group.inbox.into()), + shared_inbox_url: group.endpoints.map(|e| e.shared_inbox.into()), + moderators_url: group.attributed_to.clone().map(Into::into), + posting_restricted_to_mods: group.posting_restricted_to_mods, + featured_url: group.featured.clone().map(Into::into), + ..CommunityInsertForm::new( + instance_id, + group.preferred_username.clone(), + group.name.unwrap_or(group.preferred_username.clone()), + group.public_key.public_key_pem, + ) + }; let languages = LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?; diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index c3d2309c4..f86bed802 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -247,17 +247,19 @@ impl Object for ApubPost { let language_id = LanguageTag::to_language_id_single(page.language, &mut context.pool()).await?; - let mut form = PostInsertForm::new(name, creator.id, community.id); - form.url = url.map(Into::into); - form.body = body; - form.alt_text = alt_text; - form.published = page.published.map(Into::into); - form.updated = page.updated.map(Into::into); - form.deleted = Some(false); - form.nsfw = page.sensitive; - form.ap_id = Some(page.id.clone().into()); - form.local = Some(false); - form.language_id = language_id; + let form = PostInsertForm { + url: url.map(Into::into), + body, + alt_text, + published: page.published.map(Into::into), + updated: page.updated.map(Into::into), + deleted: Some(false), + nsfw: page.sensitive, + ap_id: Some(page.id.clone().into()), + local: Some(false), + language_id, + ..PostInsertForm::new(name, creator.id, community.id) + }; let timestamp = page.updated.or(page.published).unwrap_or_else(naive_now); let post = Post::insert_apub(&mut context.pool(), timestamp, &form).await?; diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index 95985269c..adde5482b 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -51,8 +51,10 @@ impl Instance { Some(i) => Ok(i), None => { // Instance not in database yet, insert it - let mut form = InstanceForm::new(domain_); - form.updated = Some(naive_now()); + let form = InstanceForm { + updated: Some(naive_now()), + ..InstanceForm::new(domain_) + }; insert_into(instance::table) .values(&form) // Necessary because this method may be called concurrently for the same domain. This diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index ca1d8828c..5757ab298 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -515,53 +515,63 @@ mod tests { // 3 4 // \ // 5 - let mut comment_form_0 = CommentInsertForm::new( - inserted_timmy_person.id, - inserted_post.id, - "Comment 0".into(), - ); - comment_form_0.language_id = english_id; + let comment_form_0 = CommentInsertForm { + language_id: english_id, + ..CommentInsertForm::new( + inserted_timmy_person.id, + inserted_post.id, + "Comment 0".into(), + ) + }; let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await?; - let mut comment_form_1 = CommentInsertForm::new( - inserted_sara_person.id, - inserted_post.id, - "Comment 1, A test blocked comment".into(), - ); - comment_form_1.language_id = english_id; + let comment_form_1 = CommentInsertForm { + language_id: english_id, + ..CommentInsertForm::new( + inserted_sara_person.id, + inserted_post.id, + "Comment 1, A test blocked comment".into(), + ) + }; let inserted_comment_1 = Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path)).await?; let finnish_id = Language::read_id_from_code(pool, Some("fi")).await?; - let mut comment_form_2 = CommentInsertForm::new( - inserted_timmy_person.id, - inserted_post.id, - "Comment 2".into(), - ); - comment_form_2.language_id = finnish_id; + let comment_form_2 = CommentInsertForm { + language_id: finnish_id, + ..CommentInsertForm::new( + inserted_timmy_person.id, + inserted_post.id, + "Comment 2".into(), + ) + }; let inserted_comment_2 = Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path)).await?; - let mut comment_form_3 = CommentInsertForm::new( - inserted_timmy_person.id, - inserted_post.id, - "Comment 3".into(), - ); - comment_form_3.language_id = english_id; + let comment_form_3 = CommentInsertForm { + language_id: english_id, + ..CommentInsertForm::new( + inserted_timmy_person.id, + inserted_post.id, + "Comment 3".into(), + ) + }; let _inserted_comment_3 = Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path)).await?; let polish_id = Language::read_id_from_code(pool, Some("pl")) .await? .ok_or(LemmyErrorType::LanguageNotAllowed)?; - let mut comment_form_4 = CommentInsertForm::new( - inserted_timmy_person.id, - inserted_post.id, - "Comment 4".into(), - ); - comment_form_4.language_id = Some(polish_id); + let comment_form_4 = CommentInsertForm { + language_id: Some(polish_id), + ..CommentInsertForm::new( + inserted_timmy_person.id, + inserted_post.id, + "Comment 4".into(), + ) + }; let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path)).await?; diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 8175f88a1..425fc0f2f 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -850,12 +850,14 @@ mod tests { ) .await?; - let mut post_from_blocked_person = PostInsertForm::new( - POST_BY_BLOCKED_PERSON.to_string(), - inserted_blocked_person.id, - inserted_community.id, - ); - post_from_blocked_person.language_id = Some(LanguageId(1)); + let post_from_blocked_person = PostInsertForm { + language_id: Some(LanguageId(1)), + ..PostInsertForm::new( + POST_BY_BLOCKED_PERSON.to_string(), + inserted_blocked_person.id, + inserted_community.id, + ) + }; Post::create(pool, &post_from_blocked_person).await?; // block that person @@ -867,9 +869,10 @@ mod tests { PersonBlock::block(pool, &person_block).await?; // A sample post - let mut new_post = - PostInsertForm::new(POST.to_string(), inserted_person.id, inserted_community.id); - new_post.language_id = Some(LanguageId(47)); + let new_post = PostInsertForm { + language_id: Some(LanguageId(47)), + ..PostInsertForm::new(POST.to_string(), inserted_person.id, inserted_community.id) + }; let inserted_post = Post::create(pool, &new_post).await?; let new_bot_post = PostInsertForm::new( @@ -1031,13 +1034,15 @@ mod tests { let data = init_data(pool).await?; // A post which contains the search them 'Post' not in the title (but in the body) - let mut new_post = PostInsertForm::new( - POST_WITH_ANOTHER_TITLE.to_string(), - data.local_user_view.person.id, - data.inserted_community.id, - ); - new_post.language_id = Some(LanguageId(47)); - new_post.body = Some("Post".to_string()); + let new_post = PostInsertForm { + language_id: Some(LanguageId(47)), + body: Some("Post".to_string()), + ..PostInsertForm::new( + POST_WITH_ANOTHER_TITLE.to_string(), + data.local_user_view.person.id, + data.inserted_community.id, + ) + }; let inserted_post = Post::create(pool, &new_post).await?; @@ -1267,12 +1272,14 @@ mod tests { .await? .expect("french should exist"); - let mut post_spanish = PostInsertForm::new( - EL_POSTO.to_string(), - data.local_user_view.person.id, - data.inserted_community.id, - ); - post_spanish.language_id = Some(spanish_id); + let post_spanish = PostInsertForm { + language_id: Some(spanish_id), + ..PostInsertForm::new( + EL_POSTO.to_string(), + data.local_user_view.person.id, + data.inserted_community.id, + ) + }; Post::create(pool, &post_spanish).await?; let post_listings_all = data.default_post_query().list(&data.site, pool).await?; @@ -1408,12 +1415,14 @@ mod tests { ); let inserted_community = Community::create(pool, &community_form).await?; - let mut post_form = PostInsertForm::new( - POST_FROM_BLOCKED_INSTANCE.to_string(), - data.inserted_bot.id, - inserted_community.id, - ); - post_form.language_id = Some(LanguageId(1)); + let post_form = PostInsertForm { + language_id: Some(LanguageId(1)), + ..PostInsertForm::new( + POST_FROM_BLOCKED_INSTANCE.to_string(), + data.inserted_bot.id, + inserted_community.id, + ) + }; let post_from_blocked_instance = Post::create(pool, &post_form).await?; // no instance block, should return all posts @@ -1471,14 +1480,16 @@ mod tests { // and featured for comments in 0..10 { for _ in 0..15 { - let mut post_form = PostInsertForm::new( - "keep Christ in Christmas".to_owned(), - data.local_user_view.person.id, - inserted_community.id, - ); - post_form.featured_local = Some((comments % 2) == 0); - post_form.featured_community = Some((comments % 2) == 0); - post_form.published = Some(Utc::now() - Duration::from_secs(comments % 3)); + let post_form = PostInsertForm { + featured_local: Some((comments % 2) == 0), + featured_community: Some((comments % 2) == 0), + published: Some(Utc::now() - Duration::from_secs(comments % 3)), + ..PostInsertForm::new( + "keep Christ in Christmas".to_owned(), + data.local_user_view.person.id, + inserted_community.id, + ) + }; let inserted_post = Post::create(pool, &post_form).await?; inserted_post_ids.push(inserted_post.id); diff --git a/crates/federate/src/lib.rs b/crates/federate/src/lib.rs index c891ad300..0e43ef018 100644 --- a/crates/federate/src/lib.rs +++ b/crates/federate/src/lib.rs @@ -354,8 +354,10 @@ mod test { let mut data = TestData::init(1, 1).await?; let instance = &data.instances[0]; - let mut form = InstanceForm::new(instance.domain.clone()); - form.updated = DateTime::from_timestamp(0, 0); + let form = InstanceForm { + updated: DateTime::from_timestamp(0, 0), + ..InstanceForm::new(instance.domain.clone()) + }; Instance::update(&mut data.context.pool(), instance.id, form).await?; data.run().await?; diff --git a/crates/federate/src/worker.rs b/crates/federate/src/worker.rs index fefe2ec23..56f42bb30 100644 --- a/crates/federate/src/worker.rs +++ b/crates/federate/src/worker.rs @@ -290,8 +290,10 @@ impl InstanceWorker { if updated.add(Days::new(1)) < Utc::now() { self.instance.updated = Some(Utc::now()); - let mut form = InstanceForm::new(self.instance.domain.clone()); - form.updated = Some(naive_now()); + let form = InstanceForm { + updated: Some(naive_now()), + ..InstanceForm::new(self.instance.domain.clone()) + }; Instance::update(&mut self.pool(), self.instance.id, form).await?; } Ok(()) diff --git a/src/code_migrations.rs b/src/code_migrations.rs index c998747aa..e78f8bf0b 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -485,31 +485,38 @@ async fn initialize_local_site_2022_10_10( .clone() .map(|s| s.site_name) .unwrap_or_else(|| "New Site".to_string()); - let mut site_form = SiteInsertForm::new(name, instance.id); - site_form.actor_id = Some(site_actor_id.clone().into()); - site_form.last_refreshed_at = Some(naive_now()); - site_form.inbox_url = Some(generate_shared_inbox_url(settings)?); - site_form.private_key = Some(site_key_pair.private_key); - site_form.public_key = Some(site_key_pair.public_key); + let site_form = SiteInsertForm { + actor_id: Some(site_actor_id.clone().into()), + last_refreshed_at: Some(naive_now()), + inbox_url: Some(generate_shared_inbox_url(settings)?), + private_key: Some(site_key_pair.private_key), + public_key: Some(site_key_pair.public_key), + + ..SiteInsertForm::new(name, instance.id) + }; let site = Site::create(pool, &site_form).await?; // Finally create the local_site row - let mut local_site_form = LocalSiteInsertForm::new(site.id); - local_site_form.site_setup = Some(settings.setup.is_some()); + let local_site_form = LocalSiteInsertForm { + site_setup: Some(settings.setup.is_some()), + ..LocalSiteInsertForm::new(site.id) + }; let local_site = LocalSite::create(pool, &local_site_form).await?; // Create the rate limit table - let mut local_site_rate_limit_form = LocalSiteRateLimitInsertForm::new(local_site.id); + let local_site_rate_limit_form = LocalSiteRateLimitInsertForm { + message: Some(999), + post: Some(999), + register: Some(999), + image: Some(999), + comment: Some(999), + search: Some(999), + ..LocalSiteRateLimitInsertForm::new(local_site.id) + }; // TODO these have to be set, because the database defaults are too low for the federation // tests to pass, and there's no way to live update the rate limits without restarting the // server. // This can be removed once live rate limits are enabled. - local_site_rate_limit_form.message = Some(999); - local_site_rate_limit_form.post = Some(999); - local_site_rate_limit_form.register = Some(999); - local_site_rate_limit_form.image = Some(999); - local_site_rate_limit_form.comment = Some(999); - local_site_rate_limit_form.search = Some(999); LocalSiteRateLimit::create(pool, &local_site_rate_limit_form).await?; Ok(()) diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index 883c588c6..fb7fdc555 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -493,8 +493,10 @@ async fn build_update_instance_form( // not every Fediverse instance has a valid Nodeinfo endpoint (its not required for // Activitypub). That's why we always need to mark instances as updated if they are // alive. - let mut instance_form = InstanceForm::new(domain.to_string()); - instance_form.updated = Some(naive_now()); + let instance_form = InstanceForm { + updated: Some(naive_now()), + ..InstanceForm::new(domain.to_string()) + }; // First, fetch their /.well-known/nodeinfo, then extract the correct nodeinfo link from it let well_known_url = format!("https://{}/.well-known/nodeinfo", domain);