Skip to content

Commit

Permalink
Remove TypedBuilder in favor of derive_new (fixes #4863)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Sep 13, 2024
1 parent dea6ee4 commit ec7f902
Show file tree
Hide file tree
Showing 55 changed files with 690 additions and 769 deletions.
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ anyhow = { version = "1.0.86", features = [
"backtrace",
] } # backtrace is on by default on nightly, but not stable rust
diesel_ltree = "0.3.1"
typed-builder = "0.19.1"
serial_test = "3.1.1"
tokio = { version = "1.39.2", features = ["full"] }
regex = "1.10.5"
Expand Down
21 changes: 7 additions & 14 deletions crates/api/src/site/registration_applications/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,21 @@ async fn create_test_site(context: &Data<LemmyContext>) -> LemmyResult<(Instance
.await?
.unwrap();

let site_form = SiteInsertForm::builder()
.name("test site".to_string())
.instance_id(inserted_instance.id)
.build();
let site_form = SiteInsertForm::new("test site".to_string(), inserted_instance.id);
let site = Site::create(pool, &site_form).await.unwrap();

// Create a local site, since this is necessary for determining if email verification is
// required
let local_site_form = LocalSiteInsertForm::builder()
.site_id(site.id)
.require_email_verification(Some(true))
.application_question(Some(".".to_string()))
.registration_mode(Some(RegistrationMode::RequireApplication))
.site_setup(Some(true))
.build();
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 = LocalSite::create(pool, &local_site_form).await.unwrap();

// Required to have a working local SiteView when updating the site to change email verification
// requirement or registration mode
let rate_limit_form = LocalSiteRateLimitInsertForm::builder()
.local_site_id(local_site.id)
.build();
let rate_limit_form = LocalSiteRateLimitInsertForm::new(local_site.id);
LocalSiteRateLimit::create(pool, &rate_limit_form)
.await
.unwrap();
Expand Down
9 changes: 3 additions & 6 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ pub async fn create_comment(
}
};

let comment_form = CommentInsertForm::builder()
.content(content.clone())
.post_id(data.post_id)
.creator_id(local_user_view.person.id)
.language_id(language_id)
.build();
let mut comment_form =
CommentInsertForm::new(local_user_view.person.id, data.post_id, content.clone());
comment_form.language_id = language_id;

// Create the comment
let parent_path = parent_opt.clone().map(|t| t.path);
Expand Down
34 changes: 17 additions & 17 deletions crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ 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 community_form = CommunityInsertForm::builder()
.name(data.name.clone())
.title(data.title.clone())
.description(description)
.icon(icon)
.banner(banner)
.nsfw(data.nsfw)
.actor_id(Some(community_actor_id.clone()))
.private_key(Some(keypair.private_key))
.public_key(keypair.public_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)
.instance_id(site_view.site.instance_id)
.visibility(data.visibility)
.build();
let mut community_form = CommunityInsertForm::new(
site_view.site.instance_id,
data.name.clone(),
data.title.clone(),
keypair.private_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.public_key = keypair.public_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 inserted_community = Community::create(&mut context.pool(), &community_form)
.await
Expand Down
20 changes: 9 additions & 11 deletions crates/api_crud/src/custom_emoji/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ pub async fn create_custom_emoji(
// Make sure user is an admin
is_admin(&local_user_view)?;

let emoji_form = CustomEmojiInsertForm::builder()
.local_site_id(local_site.id)
.shortcode(data.shortcode.to_lowercase().trim().to_string())
.alt_text(data.alt_text.to_string())
.category(data.category.to_string())
.image_url(data.clone().image_url.into())
.build();
let emoji_form = CustomEmojiInsertForm::new(
local_site.id,
data.shortcode.to_lowercase().trim().to_string(),
data.clone().image_url.into(),
data.alt_text.to_string(),
data.category.to_string(),
);
let emoji = CustomEmoji::create(&mut context.pool(), &emoji_form).await?;
let mut keywords = vec![];
for keyword in &data.keywords {
let keyword_form = CustomEmojiKeywordInsertForm::builder()
.custom_emoji_id(emoji.id)
.keyword(keyword.to_lowercase().trim().to_string())
.build();
let keyword_form =
CustomEmojiKeywordInsertForm::new(emoji.id, keyword.to_lowercase().trim().to_string());
keywords.push(keyword_form);
}
CustomEmojiKeyword::create(&mut context.pool(), keywords).await?;
Expand Down
18 changes: 8 additions & 10 deletions crates/api_crud/src/custom_emoji/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ pub async fn update_custom_emoji(
// Make sure user is an admin
is_admin(&local_user_view)?;

let emoji_form = CustomEmojiUpdateForm::builder()
.local_site_id(local_site.id)
.alt_text(data.alt_text.to_string())
.category(data.category.to_string())
.image_url(data.clone().image_url.into())
.build();
let emoji_form = CustomEmojiUpdateForm::new(
local_site.id,
data.clone().image_url.into(),
data.alt_text.to_string(),
data.category.to_string(),
);
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()
.custom_emoji_id(emoji.id)
.keyword(keyword.to_lowercase().trim().to_string())
.build();
let keyword_form =
CustomEmojiKeywordInsertForm::new(emoji.id, keyword.to_lowercase().trim().to_string());
keywords.push(keyword_form);
}
CustomEmojiKeyword::create(&mut context.pool(), keywords).await?;
Expand Down
20 changes: 10 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,16 @@ pub async fn create_post(
}
};

let post_form = PostInsertForm::builder()
.name(data.name.trim().to_string())
.url(url.map(Into::into))
.body(body)
.alt_text(data.alt_text.clone())
.community_id(data.community_id)
.creator_id(local_user_view.person.id)
.nsfw(data.nsfw)
.language_id(language_id)
.build();
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 inserted_post = Post::create(&mut context.pool(), &post_form)
.await
Expand Down
10 changes: 5 additions & 5 deletions crates/api_crud/src/private_message/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ pub async fn create_private_message(
)
.await?;

let private_message_form = PrivateMessageInsertForm::builder()
.content(content.clone())
.creator_id(local_user_view.person.id)
.recipient_id(data.recipient_id)
.build();
let private_message_form = PrivateMessageInsertForm::new(
local_user_view.person.id,
data.recipient_id,
content.clone(),
);

let inserted_private_message = PrivateMessage::create(&mut context.pool(), &private_message_form)
.await
Expand Down
22 changes: 12 additions & 10 deletions crates/apub/src/api/user_settings_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,12 @@ mod tests {
let export_user =
create_user("hanna".to_string(), Some("my bio".to_string()), &context).await?;

let community_form = CommunityInsertForm::builder()
.name("testcom".to_string())
.title("testcom".to_string())
.instance_id(export_user.person.instance_id)
.build();
let community_form = CommunityInsertForm::new(
export_user.person.instance_id,
"testcom".to_string(),
"testcom".to_string(),
"pubkey".to_string(),
);
let community = Community::create(&mut context.pool(), &community_form).await?;
let follower_form = CommunityFollowerForm {
community_id: community.id,
Expand Down Expand Up @@ -412,11 +413,12 @@ mod tests {
let export_user =
create_user("hanna".to_string(), Some("my bio".to_string()), &context).await?;

let community_form = CommunityInsertForm::builder()
.name("testcom".to_string())
.title("testcom".to_string())
.instance_id(export_user.person.instance_id)
.build();
let community_form = CommunityInsertForm::new(
export_user.person.instance_id,
"testcom".to_string(),
"testcom".to_string(),
"pubkey".to_string(),
);
let community = Community::create(&mut context.pool(), &community_form).await?;
let follower_form = CommunityFollowerForm {
community_id: community.id,
Expand Down
27 changes: 11 additions & 16 deletions crates/apub/src/http/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ 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 community_form = CommunityInsertForm::builder()
.name("testcom6".to_string())
.title("nada".to_owned())
.public_key("pubkey".to_string())
.instance_id(instance.id)
.deleted(Some(deleted))
.visibility(Some(visibility))
.build();
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 = Community::create(&mut context.pool(), &community_form).await?;
Ok((instance, community))
}
Expand All @@ -169,18 +169,13 @@ pub(crate) mod tests {
instance_id: InstanceId,
) -> LemmyResult<()> {
// Create a local site, since this is necessary for community fetching.
let site_form = SiteInsertForm::builder()
.name("test site".to_string())
.instance_id(instance_id)
.build();
let site_form = SiteInsertForm::new("test site".to_string(), instance_id);
let site = Site::create(&mut context.pool(), &site_form).await?;

let local_site_form = LocalSiteInsertForm::builder().site_id(site.id).build();
let local_site_form = LocalSiteInsertForm::new(site.id);
let local_site = LocalSite::create(&mut context.pool(), &local_site_form).await?;
let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder()
.local_site_id(local_site.id)
.build();

let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::new(local_site.id);
LocalSiteRateLimit::create(&mut context.pool(), &local_site_rate_limit_form).await?;
Ok(())
}
Expand Down
43 changes: 21 additions & 22 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,28 @@ 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 form = CommunityInsertForm {
name: group.preferred_username.clone(),
title: group.name.unwrap_or(group.preferred_username.clone()),
description,
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),
public_key: group.public_key.public_key_pem,
last_refreshed_at: Some(naive_now()),
icon,
banner,
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,
let mut form = CommunityInsertForm::new(
instance_id,
featured_url: group.featured.clone().map(Into::into),
..Default::default()
};
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 languages =
LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?;

Expand Down
Loading

0 comments on commit ec7f902

Please sign in to comment.