use derive_new::new instead of TypedBuilder

pull/4881/head
privacyguard 2024-07-07 01:48:10 +03:00
parent d9c7e96f31
commit 45c0a0030a
5 changed files with 42 additions and 49 deletions

View File

@ -36,23 +36,23 @@ pub async fn create_oauth_provider(
reader.read(&mut id_bytes); reader.read(&mut id_bytes);
let cloned_data = data.clone(); let cloned_data = data.clone();
let oauth_provider_form = OAuthProviderInsertForm::builder() let oauth_provider_form = OAuthProviderInsertForm {
.id(OAuthProviderId(i64::from_ne_bytes(id_bytes))) id: OAuthProviderId(i64::from_ne_bytes(id_bytes)),
.display_name(cloned_data.display_name) display_name: cloned_data.display_name,
.issuer(Url::parse(&cloned_data.issuer)?.into()) issuer: Url::parse(&cloned_data.issuer)?.into(),
.authorization_endpoint(Url::parse(&cloned_data.authorization_endpoint)?.into()) authorization_endpoint: Url::parse(&cloned_data.authorization_endpoint)?.into(),
.token_endpoint(Url::parse(&cloned_data.token_endpoint)?.into()) token_endpoint: Url::parse(&cloned_data.token_endpoint)?.into(),
.userinfo_endpoint(Url::parse(&cloned_data.userinfo_endpoint)?.into()) userinfo_endpoint: Url::parse(&cloned_data.userinfo_endpoint)?.into(),
.id_claim(cloned_data.id_claim) id_claim: cloned_data.id_claim,
.name_claim(cloned_data.name_claim) name_claim: cloned_data.name_claim,
.client_id(data.client_id.to_string()) client_id: data.client_id.to_string(),
.client_secret(data.client_secret.to_string()) client_secret: data.client_secret.to_string(),
.scopes(data.scopes.to_string()) scopes: data.scopes.to_string(),
.auto_verify_email(data.auto_verify_email) auto_verify_email: data.auto_verify_email,
.auto_approve_application(data.auto_approve_application) auto_approve_application: data.auto_approve_application,
.account_linking_enabled(data.account_linking_enabled) account_linking_enabled: data.account_linking_enabled,
.enabled(data.enabled) enabled: data.enabled,
.build(); };
let unsafe_oauth_provider = let unsafe_oauth_provider =
UnsafeOAuthProvider::create(&mut context.pool(), &oauth_provider_form).await?; UnsafeOAuthProvider::create(&mut context.pool(), &oauth_provider_form).await?;
Ok(Json(OAuthProvider::from_unsafe(&unsafe_oauth_provider))) Ok(Json(OAuthProvider::from_unsafe(&unsafe_oauth_provider)))

View File

@ -20,27 +20,28 @@ pub async fn update_oauth_provider(
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let cloned_data = data.clone(); let cloned_data = data.clone();
let oauth_provider_form = OAuthProviderUpdateForm::builder() let oauth_provider_form = OAuthProviderUpdateForm {
.display_name(cloned_data.display_name) display_name: cloned_data.display_name,
.authorization_endpoint(Url::parse(&cloned_data.authorization_endpoint)?.into()) authorization_endpoint: Url::parse(&cloned_data.authorization_endpoint)?.into(),
.token_endpoint(Url::parse(&cloned_data.token_endpoint)?.into()) token_endpoint: Url::parse(&cloned_data.token_endpoint)?.into(),
.userinfo_endpoint(Url::parse(&cloned_data.userinfo_endpoint)?.into()) userinfo_endpoint: Url::parse(&cloned_data.userinfo_endpoint)?.into(),
.id_claim(data.id_claim.to_string()) id_claim: data.id_claim.to_string(),
.name_claim(data.name_claim.to_string()) name_claim: data.name_claim.to_string(),
.client_secret(if !data.client_secret.is_empty() { client_secret: if !data.client_secret.is_empty() {
Some(data.client_secret.to_string()) Some(data.client_secret.to_string())
} else { } else {
None None
}) },
.scopes(data.scopes.to_string()) scopes: data.scopes.to_string(),
.auto_verify_email(data.auto_verify_email) auto_verify_email: data.auto_verify_email,
.auto_approve_application(data.auto_approve_application) auto_approve_application: data.auto_approve_application,
.account_linking_enabled(data.account_linking_enabled) account_linking_enabled: data.account_linking_enabled,
.enabled(data.enabled) enabled: data.enabled,
.updated(naive_now()); updated: naive_now(),
};
let update_result = let update_result =
UnsafeOAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form.build()).await?; UnsafeOAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form).await?;
let unsafe_oauth_provider = UnsafeOAuthProvider::read(&mut context.pool(), update_result.id) let unsafe_oauth_provider = UnsafeOAuthProvider::read(&mut context.pool(), update_result.id)
.await? .await?
.ok_or(LemmyErrorType::CouldntFindOauthProvider)?; .ok_or(LemmyErrorType::CouldntFindOauthProvider)?;

View File

@ -390,11 +390,8 @@ pub async fn authenticate_with_oauth(
if oauth_provider.account_linking_enabled { if oauth_provider.account_linking_enabled {
// Link with OAUTH => Login user // Link with OAUTH => Login user
let oauth_account_form = OAuthAccountInsertForm::builder() let oauth_account_form =
.local_user_id(user_view.local_user.id) OAuthAccountInsertForm::new(user_view.local_user.id, oauth_provider.id, oauth_user_id);
.oauth_provider_id(oauth_provider.id)
.oauth_user_id(oauth_user_id)
.build();
OAuthAccount::create(&mut context.pool(), &oauth_account_form) OAuthAccount::create(&mut context.pool(), &oauth_account_form)
.await .await
@ -452,11 +449,8 @@ pub async fn authenticate_with_oauth(
.ok_or(LemmyErrorType::OauthLoginFailed)?; .ok_or(LemmyErrorType::OauthLoginFailed)?;
// Create the oauth account // Create the oauth account
let oauth_account_form = OAuthAccountInsertForm::builder() let oauth_account_form =
.local_user_id(local_user.id) OAuthAccountInsertForm::new(local_user.id, oauth_provider.id, oauth_user_id);
.oauth_provider_id(oauth_provider.id)
.oauth_user_id(oauth_user_id)
.build();
OAuthAccount::create(&mut context.pool(), &oauth_account_form) OAuthAccount::create(&mut context.pool(), &oauth_account_form)
.await .await

View File

@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
#[cfg(feature = "full")] #[cfg(feature = "full")]
use ts_rs::TS; use ts_rs::TS;
use typed_builder::TypedBuilder;
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
@ -24,7 +23,7 @@ pub struct OAuthAccount {
pub updated: Option<DateTime<Utc>>, pub updated: Option<DateTime<Utc>>,
} }
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone, derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = oauth_account))] #[cfg_attr(feature = "full", diesel(table_name = oauth_account))]
pub struct OAuthAccountInsertForm { pub struct OAuthAccountInsertForm {
@ -33,7 +32,7 @@ pub struct OAuthAccountInsertForm {
pub oauth_user_id: String, pub oauth_user_id: String,
} }
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone, derive_new::new)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = oauth_account))] #[cfg_attr(feature = "full", diesel(table_name = oauth_account))]
pub struct OAuthAccountUpdateForm { pub struct OAuthAccountUpdateForm {

View File

@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
#[cfg(feature = "full")] #[cfg(feature = "full")]
use ts_rs::TS; use ts_rs::TS;
use typed_builder::TypedBuilder;
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
@ -110,7 +109,7 @@ pub struct OAuthProvider {
pub updated: Option<DateTime<Utc>>, pub updated: Option<DateTime<Utc>>,
} }
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))]
#[cfg_attr(feature = "full", diesel(table_name = oauth_provider))] #[cfg_attr(feature = "full", diesel(table_name = oauth_provider))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
@ -136,7 +135,7 @@ pub struct OAuthProviderInsertForm {
pub enabled: bool, pub enabled: bool,
} }
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))]
#[cfg_attr(feature = "full", diesel(table_name = oauth_provider))] #[cfg_attr(feature = "full", diesel(table_name = oauth_provider))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]