Skip to content

Commit

Permalink
Refactor paywalled boost_element rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Feb 3, 2025
1 parent a9aaf51 commit f8b3910
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
17 changes: 9 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
),
Expand Down
10 changes: 10 additions & 0 deletions src/rpc/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum RpcMethod {
AddElementComment,
PaywallAddElementCommentQuote,
PaywallAddElementComment,
PaywallGetBoostElementQuote,
PaywallBoostElement,
GenerateElementIssues,
SyncElements,
GenerateElementIcons,
Expand Down Expand Up @@ -164,6 +166,14 @@ async fn handle(req: Json<Value>, pool: Data<Pool>, conf: Data<Conf>) -> 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?,
Expand Down
26 changes: 15 additions & 11 deletions src/rpc/paywall_boost_element.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand All @@ -17,22 +17,26 @@ pub struct Res {
pub payment_request: String,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
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<Params>,
pool: Data<Arc<Pool>>,
conf: Data<Arc<Conf>>,
) -> Result<Res> {
run_internal(params, &pool, &conf).await
}

pub async fn run_internal(params: Params, pool: &Pool, conf: &Conf) -> Result<Res> {
Element::select_by_id_or_osm_id_async(&params.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,
)
Expand Down
8 changes: 5 additions & 3 deletions src/rpc/paywall_get_boost_element_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{conf::Conf, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::Data;
use serde::Serialize;
use std::sync::Arc;
Expand All @@ -13,8 +12,11 @@ pub struct Res {
pub quote_365d_sat: i64,
}

pub async fn run(pool: Data<Arc<Pool>>) -> Result<Res> {
let conf = Conf::select_async(&pool).await?;
pub async fn run(conf: Data<Arc<Conf>>) -> Result<Res> {
run_internal(&conf).await
}

pub async fn run_internal(conf: &Conf) -> Result<Res> {
Ok(Res {
quote_30d_sat: conf.paywall_boost_element_30d_price_sat,
quote_90d_sat: conf.paywall_boost_element_90d_price_sat,
Expand Down

0 comments on commit f8b3910

Please sign in to comment.