diff --git a/docs/openapi.yaml b/docs/openapi.yaml index febbfed..62f3bcb 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -687,7 +687,7 @@ paths: items: $ref: '#/components/schemas/SubscriptionOption' post: - summary: Enable subscriptions + summary: Enable subscriptions or update subscription settings security: - tokenAuth: [] requestBody: diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index b0df6f4..a39c0b0 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -267,18 +267,7 @@ async fn create_identity_proof( value: proof_data.signature.clone(), }; let mut profile_data = ProfileUpdateData::from(¤t_user.profile); - match profile_data.identity_proofs.iter_mut() - .find(|item| item.issuer == proof.issuer) { - Some(mut item) => { - // Replace - item.proof_type = proof.proof_type; - item.value = proof.value; - }, - None => { - // Add new proof - profile_data.identity_proofs.push(proof); - }, - }; + profile_data.add_identity_proof(proof); current_user.profile = update_profile( db_client, ¤t_user.id, diff --git a/src/mastodon_api/subscriptions/views.rs b/src/mastodon_api/subscriptions/views.rs index 29fa33f..87e9204 100644 --- a/src/mastodon_api/subscriptions/views.rs +++ b/src/mastodon_api/subscriptions/views.rs @@ -79,7 +79,7 @@ async fn get_subscription_options( } #[post("/options")] -pub async fn subscriptions_enabled( +pub async fn register_subscription_option( auth: BearerAuth, config: web::Data, db_pool: web::Data, @@ -89,8 +89,7 @@ pub async fn subscriptions_enabled( let db_client = &**get_database_client(&db_pool).await?; let mut current_user = get_current_user(db_client, auth.token()).await?; - let mut maybe_payment_option = None; - match subscription_option.into_inner() { + let maybe_payment_option = match subscription_option.into_inner() { SubscriptionOption::Ethereum => { let ethereum_config = config.blockchain() .and_then(|conf| conf.ethereum_config()) @@ -100,9 +99,12 @@ pub async fn subscriptions_enabled( let wallet_address = current_user .public_wallet_address(&Currency::Ethereum) .ok_or(HttpError::PermissionError)?; - if !current_user.profile.payment_options + if current_user.profile.payment_options .any(PaymentType::EthereumSubscription) { + // Ignore attempts to update payment option + None + } else { let is_registered = is_registered_recipient( contract_set, &wallet_address, @@ -110,32 +112,26 @@ pub async fn subscriptions_enabled( if !is_registered { return Err(ValidationError("recipient is not registered").into()); }; - maybe_payment_option = Some(PaymentOption::ethereum_subscription( + Some(PaymentOption::ethereum_subscription( ethereum_config.chain_id.clone(), - )); - }; + )) + } }, SubscriptionOption::Monero { price, payout_address } => { let monero_config = config.blockchain() .and_then(|conf| conf.monero_config()) .ok_or(HttpError::NotSupported)?; - if !current_user.profile.payment_options - .any(PaymentType::MoneroSubscription) - { - let payment_info = MoneroSubscription { - chain_id: monero_config.chain_id.clone(), - price, - payout_address, - }; - maybe_payment_option = - Some(PaymentOption::MoneroSubscription(payment_info)); + let payment_info = MoneroSubscription { + chain_id: monero_config.chain_id.clone(), + price, + payout_address, }; + Some(PaymentOption::MoneroSubscription(payment_info)) }, }; if let Some(payment_option) = maybe_payment_option { - // Add payment option to profile let mut profile_data = ProfileUpdateData::from(¤t_user.profile); - profile_data.payment_options.push(payment_option); + profile_data.add_payment_option(payment_option); current_user.profile = update_profile( db_client, ¤t_user.id, @@ -214,7 +210,7 @@ pub fn subscription_api_scope() -> Scope { web::scope("/api/v1/subscriptions") .service(authorize_subscription) .service(get_subscription_options) - .service(subscriptions_enabled) + .service(register_subscription_option) .service(find_subscription) .service(create_invoice_view) .service(get_invoice) diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index e29f40c..e295077 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -359,6 +359,22 @@ pub struct ProfileUpdateData { } impl ProfileUpdateData { + /// Adds new identity proof + /// or replaces the existing one if it has the same issuer. + pub fn add_identity_proof(&mut self, proof: IdentityProof) -> () { + self.identity_proofs.retain(|item| item.issuer != proof.issuer); + self.identity_proofs.push(proof); + } + + /// Adds new payment option + /// or replaces the existing one if it has the same type. + pub fn add_payment_option(&mut self, option: PaymentOption) -> () { + self.payment_options.retain(|item| { + item.payment_type() != option.payment_type() + }); + self.payment_options.push(option); + } + pub fn clean(&mut self) -> Result<(), ValidationError> { if let Some(display_name) = &self.display_name { validate_display_name(display_name)?;