Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions crates/api/src/site/registration_applications/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 19 additions & 17 deletions crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions crates/apub/src/http/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
46 changes: 24 additions & 22 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
24 changes: 13 additions & 11 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
6 changes: 4 additions & 2 deletions crates/db_schema/src/impls/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 40 additions & 30 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
Loading