From f8b391033848f853d61493a78a47e6a9a75ed72d Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Mon, 3 Feb 2025 14:40:37 +0700 Subject: [PATCH] Refactor paywalled boost_element rpc --- src/main.rs | 17 +++++++------- src/rpc/handler.rs | 10 +++++++++ src/rpc/paywall_boost_element.rs | 26 +++++++++++++--------- src/rpc/paywall_get_boost_element_quote.rs | 8 ++++--- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index ef4b61d..de5a340 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,14 @@ async fn main() -> Result<()> { rpc::get_boosted_elements::run, ) .with_method(rpc::boost_element::NAME, rpc::boost_element::run) + .with_method( + rpc::paywall_get_boost_element_quote::NAME, + rpc::paywall_get_boost_element_quote::run, + ) + .with_method( + rpc::paywall_boost_element::NAME, + rpc::paywall_boost_element::run, + ) .with_method( rpc::add_element_comment::NAME, rpc::add_element_comment::run, @@ -143,6 +151,7 @@ async fn main() -> Result<()> { rpc::get_trending_communities::NAME, rpc::get_trending_communities::run, ) + // user .with_method("get_user_activity", rpc::get_user_activity::run) .with_method("set_user_tag", rpc::set_user_tag::run) .with_method("remove_user_tag", rpc::remove_user_tag::run) @@ -158,14 +167,6 @@ async fn main() -> Result<()> { .with_method("generate_invoice", rpc::generate_invoice::run) .with_method(rpc::get_invoice::NAME, rpc::get_invoice::run) .with_method("sync_unpaid_invoices", rpc::sync_unpaid_invoices::run) - .with_method( - rpc::paywall_get_boost_element_quote::NAME, - rpc::paywall_get_boost_element_quote::run, - ) - .with_method( - rpc::paywall_boost_element::NAME, - rpc::paywall_boost_element::run, - ) .finish() .into_actix_web_service(), ), diff --git a/src/rpc/handler.rs b/src/rpc/handler.rs index 00c1625..3140539 100644 --- a/src/rpc/handler.rs +++ b/src/rpc/handler.rs @@ -29,6 +29,8 @@ pub enum RpcMethod { AddElementComment, PaywallAddElementCommentQuote, PaywallAddElementComment, + PaywallGetBoostElementQuote, + PaywallBoostElement, GenerateElementIssues, SyncElements, GenerateElementIcons, @@ -164,6 +166,14 @@ async fn handle(req: Json, pool: Data, conf: Data) -> Result< super::paywall_add_element_comment::run_internal(params(req.params)?, &pool, &conf) .await?, ), + RpcMethod::PaywallGetBoostElementQuote => RpcResponse::from( + req.id.clone(), + super::paywall_get_boost_element_quote::run_internal(&conf).await?, + ), + RpcMethod::PaywallBoostElement => RpcResponse::from( + req.id.clone(), + super::paywall_boost_element::run_internal(params(req.params)?, &pool, &conf).await?, + ), RpcMethod::GenerateElementIssues => RpcResponse::from( req.id.clone(), super::generate_element_issues::run_internal(params(req.params)?, &pool, &conf).await?, diff --git a/src/rpc/paywall_boost_element.rs b/src/rpc/paywall_boost_element.rs index 3cf4cfc..f978c9a 100644 --- a/src/rpc/paywall_boost_element.rs +++ b/src/rpc/paywall_boost_element.rs @@ -1,13 +1,13 @@ use crate::{conf::Conf, element::Element, invoice, Result}; use deadpool_sqlite::Pool; -use jsonrpc_v2::{Data, Params}; +use jsonrpc_v2::Data; use serde::{Deserialize, Serialize}; use std::sync::Arc; pub const NAME: &str = "paywall_boost_element"; #[derive(Deserialize)] -pub struct Args { +pub struct Params { pub element_id: String, pub days: i64, } @@ -17,22 +17,26 @@ pub struct Res { pub payment_request: String, } -pub async fn run(Params(args): Params, pool: Data>) -> Result { - let conf = Conf::select_async(&pool).await?; - Element::select_by_id_or_osm_id_async(&args.element_id, &pool) +pub async fn run( + jsonrpc_v2::Params(params): jsonrpc_v2::Params, + pool: Data>, + conf: Data>, +) -> Result { + run_internal(params, &pool, &conf).await +} + +pub async fn run_internal(params: Params, pool: &Pool, conf: &Conf) -> Result { + Element::select_by_id_or_osm_id_async(¶ms.element_id, &pool) .await? - .ok_or(format!( - "there is no element with id = {}", - &args.element_id, - ))?; - let sats = match args.days { + .ok_or("Element not found")?; + let sats = match params.days { 30 => conf.paywall_boost_element_30d_price_sat, 90 => conf.paywall_boost_element_90d_price_sat, 365 => conf.paywall_boost_element_365d_price_sat, _ => Err("Invalid duration")?, }; let invoice = invoice::service::create( - format!("element_boost:{}:{}", args.element_id, args.days), + format!("element_boost:{}:{}", params.element_id, params.days), sats, &pool, ) diff --git a/src/rpc/paywall_get_boost_element_quote.rs b/src/rpc/paywall_get_boost_element_quote.rs index bfcf197..a3abf87 100644 --- a/src/rpc/paywall_get_boost_element_quote.rs +++ b/src/rpc/paywall_get_boost_element_quote.rs @@ -1,5 +1,4 @@ use crate::{conf::Conf, Result}; -use deadpool_sqlite::Pool; use jsonrpc_v2::Data; use serde::Serialize; use std::sync::Arc; @@ -13,8 +12,11 @@ pub struct Res { pub quote_365d_sat: i64, } -pub async fn run(pool: Data>) -> Result { - let conf = Conf::select_async(&pool).await?; +pub async fn run(conf: Data>) -> Result { + run_internal(&conf).await +} + +pub async fn run_internal(conf: &Conf) -> Result { Ok(Res { quote_30d_sat: conf.paywall_boost_element_30d_price_sat, quote_90d_sat: conf.paywall_boost_element_90d_price_sat,