Add MoneroSubscription payment option

This commit is contained in:
silverpill 2022-08-25 21:51:18 +00:00
parent 703cae0a43
commit 423eec0a2c
6 changed files with 62 additions and 15 deletions

View file

@ -663,6 +663,12 @@ paths:
enum:
- ethereum
- monero
price:
description: Subscription price (only for Monero)
type: number
payout_address:
description: Payout address (only for Monero)
type: string
required:
- type
responses:

View file

@ -67,22 +67,27 @@ pub fn attach_payment_option(
user_id: &Uuid,
payment_option: PaymentOption,
) -> ActorAttachment {
match payment_option {
let (name, href) = match payment_option {
// Local actors can't have payment links
PaymentOption::Link(_) => unimplemented!(),
PaymentOption::EthereumSubscription(_) => {
let name = "EthereumSubscription".to_string();
let subscription_page_url =
get_subscription_page_url(instance_url, user_id);
ActorAttachment {
object_type: LINK.to_string(),
name: name,
value: None,
href: Some(subscription_page_url),
signature_algorithm: None,
signature_value: None,
}
let href = get_subscription_page_url(instance_url, user_id);
(name, href)
},
PaymentOption::MoneroSubscription(_) => {
let name = "MoneroSubscription".to_string();
let href = get_subscription_page_url(instance_url, user_id);
(name, href)
},
};
ActorAttachment {
object_type: LINK.to_string(),
name: name,
value: None,
href: Some(href),
signature_algorithm: None,
signature_value: None,
}
}

View file

@ -98,7 +98,9 @@ impl Account {
.map(|option| {
match option {
PaymentOption::Link(link) => link.href,
PaymentOption::EthereumSubscription(_) => {
PaymentOption::EthereumSubscription(_) |
PaymentOption::MoneroSubscription(_) =>
{
get_subscription_page_url(instance_url, &profile.id)
},
}

View file

@ -11,5 +11,5 @@ pub enum SubscriptionSettings {
#[serde(rename = "ethereum")]
Ethereum,
#[serde(rename = "monero")]
Monero { },
Monero { price: u64, payout_address: String },
}

View file

@ -14,6 +14,7 @@ use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::profiles::queries::update_profile;
use crate::models::profiles::types::{
MoneroSubscription,
PaymentOption,
PaymentType,
ProfileUpdateData,
@ -86,8 +87,21 @@ pub async fn subscriptions_enabled(
));
};
},
SubscriptionSettings::Monero { } => {
todo!();
SubscriptionSettings::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));
};
},
};
if let Some(payment_option) = maybe_payment_option {

View file

@ -47,6 +47,7 @@ json_to_sql!(IdentityProofs);
pub enum PaymentType {
Link,
EthereumSubscription,
MoneroSubscription,
}
impl From<&PaymentType> for i16 {
@ -54,6 +55,7 @@ impl From<&PaymentType> for i16 {
match payment_type {
PaymentType::Link => 1,
PaymentType::EthereumSubscription => 2,
PaymentType::MoneroSubscription => 3,
}
}
}
@ -65,6 +67,7 @@ impl TryFrom<i16> for PaymentType {
let payment_type = match value {
1 => Self::Link,
2 => Self::EthereumSubscription,
3 => Self::MoneroSubscription,
_ => return Err(ConversionError),
};
Ok(payment_type)
@ -82,10 +85,18 @@ pub struct EthereumSubscription {
chain_id: ChainId,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MoneroSubscription {
pub chain_id: ChainId,
pub price: u64, // piconeros per second
pub payout_address: String,
}
#[derive(Clone, Debug)]
pub enum PaymentOption {
Link(PaymentLink),
EthereumSubscription(EthereumSubscription),
MoneroSubscription(MoneroSubscription),
}
impl PaymentOption {
@ -97,6 +108,7 @@ impl PaymentOption {
match self {
Self::Link(_) => PaymentType::Link,
Self::EthereumSubscription(_) => PaymentType::EthereumSubscription,
Self::MoneroSubscription(_) => PaymentType::MoneroSubscription,
}
}
}
@ -124,6 +136,11 @@ impl<'de> Deserialize<'de> for PaymentOption {
.map_err(DeserializerError::custom)?;
Self::EthereumSubscription(payment_info)
},
PaymentType::MoneroSubscription => {
let payment_info = MoneroSubscription::deserialize(value)
.map_err(DeserializerError::custom)?;
Self::MoneroSubscription(payment_info)
},
};
Ok(payment_option)
}
@ -142,6 +159,9 @@ impl Serialize for PaymentOption {
Self::EthereumSubscription(payment_info) => {
payment_info.serialize(FlatMapSerializer(&mut map))?
},
Self::MoneroSubscription(payment_info) => {
payment_info.serialize(FlatMapSerializer(&mut map))?
},
};
map.end()
}