Get rid of a lot of pointless mut form initializations. (#5037)

* Get rid of a lot of pointless mut form initializations.

- Fixes #5036

* Fix clippy.
This commit is contained in:
Dessalines 2024-09-23 06:05:18 -04:00 committed by GitHub
parent 25df9d255b
commit 458bb60144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 214 additions and 165 deletions

View File

@ -63,11 +63,13 @@ async fn create_test_site(context: &Data<LemmyContext>) -> 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

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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))
}

View File

@ -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?;

View File

@ -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?;

View File

@ -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

View File

@ -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?;

View File

@ -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);

View File

@ -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?;

View File

@ -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(())

View File

@ -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(())

View File

@ -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 mut 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);