diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 504319a..9c0f4e2 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -646,6 +646,20 @@ paths: description: User's wallet address is not known or not verified 418: description: Blockchain integration is not enabled + /api/v1/subscriptions/options: + get: + summary: Get list of subscription options + security: + - tokenAuth: [] + responses: + 200: + description: Successful operation + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/SubscriptionOption' /api/v1/subscriptions/enable: post: summary: Enable subscriptions @@ -655,22 +669,7 @@ paths: content: application/json: schema: - type: object - properties: - type: - description: Subscription type - type: string - enum: - - ethereum - - monero - price: - description: Subscription price (only for Monero) - type: number - payout_address: - description: Payout address (only for Monero) - type: string - required: - - type + $ref: '#/components/schemas/SubscriptionOption' responses: 200: description: Successful operation @@ -1133,6 +1132,23 @@ components: description: The date when subscription expires. type: string format: dateTime + SubscriptionOption: + type: object + properties: + type: + description: Subscription type + type: string + enum: + - ethereum + - monero + price: + description: Subscription price (only for Monero) + type: number + payout_address: + description: Payout address (only for Monero) + type: string + required: + - type Tag: type: object properties: diff --git a/src/mastodon_api/subscriptions/types.rs b/src/mastodon_api/subscriptions/types.rs index fadafca..eb266ae 100644 --- a/src/mastodon_api/subscriptions/types.rs +++ b/src/mastodon_api/subscriptions/types.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::models::invoices::types::DbInvoice; +use crate::models::profiles::types::PaymentOption; #[derive(Deserialize)] pub struct InvoiceData { @@ -33,11 +34,23 @@ pub struct SubscriptionQueryParams { pub price: u64, } -#[derive(Deserialize)] -#[serde(tag = "type")] -pub enum SubscriptionSettings { - #[serde(rename = "ethereum")] +#[derive(Deserialize, Serialize)] +#[serde(tag = "type", rename_all = "kebab-case")] +pub enum SubscriptionOption { Ethereum, - #[serde(rename = "monero")] Monero { price: u64, payout_address: String }, } + +impl SubscriptionOption { + pub fn from_payment_option(payment_option: PaymentOption) -> Option { + let settings = match payment_option { + PaymentOption::Link(_) => return None, + PaymentOption::EthereumSubscription(_) => Self::Ethereum, + PaymentOption::MoneroSubscription(payment_info) => Self::Monero { + price: payment_info.price, + payout_address: payment_info.payout_address, + }, + }; + Some(settings) + } +} diff --git a/src/mastodon_api/subscriptions/views.rs b/src/mastodon_api/subscriptions/views.rs index 7ca7d9b..41c37bf 100644 --- a/src/mastodon_api/subscriptions/views.rs +++ b/src/mastodon_api/subscriptions/views.rs @@ -1,4 +1,4 @@ -use actix_web::{post, web, HttpResponse, Scope}; +use actix_web::{get, post, web, HttpResponse, Scope}; use actix_web_httpauth::extractors::bearer::BearerAuth; use crate::activitypub::builders::update_person::prepare_update_person; @@ -30,7 +30,7 @@ use super::types::{ Invoice, InvoiceData, SubscriptionQueryParams, - SubscriptionSettings, + SubscriptionOption, }; pub async fn authorize_subscription( @@ -59,19 +59,33 @@ pub async fn authorize_subscription( Ok(HttpResponse::Ok().json(signature)) } +#[get("/options")] +async fn get_subscription_options( + auth: BearerAuth, + db_pool: web::Data, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let current_user = get_current_user(db_client, auth.token()).await?; + let options: Vec = current_user.profile + .payment_options.into_inner().into_iter() + .filter_map(SubscriptionOption::from_payment_option) + .collect(); + Ok(HttpResponse::Ok().json(options)) +} + pub async fn subscriptions_enabled( auth: BearerAuth, config: web::Data, db_pool: web::Data, maybe_blockchain: web::Data>, - subscription_settings: web::Json, + subscription_option: web::Json, ) -> Result { 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_settings.into_inner() { - SubscriptionSettings::Ethereum => { + match subscription_option.into_inner() { + SubscriptionOption::Ethereum => { let ethereum_config = config.blockchain() .and_then(|conf| conf.ethereum_config()) .ok_or(HttpError::NotSupported)?; @@ -95,7 +109,7 @@ pub async fn subscriptions_enabled( )); }; }, - SubscriptionSettings::Monero { price, payout_address } => { + SubscriptionOption::Monero { price, payout_address } => { let monero_config = config.blockchain() .and_then(|conf| conf.monero_config()) .ok_or(HttpError::NotSupported)?; @@ -161,6 +175,7 @@ async fn create_invoice_view( pub fn subscription_api_scope() -> Scope { web::scope("/api/v1/subscriptions") .route("/authorize", web::get().to(authorize_subscription)) + .service(get_subscription_options) .route("/enable", web::post().to(subscriptions_enabled)) .service(create_invoice_view) }