Skip to content

Commit

Permalink
Move hardcoded fees to conf
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Jan 25, 2025
1 parent 49d3543 commit 39acb28
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 17 deletions.
8 changes: 4 additions & 4 deletions migrations/69.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CREATE TABLE conf(
id INTEGER PRIMARY KEY NOT NULL,
paywall_add_element_comment_price_sat TEXT NOT NULL,
paywall_boost_element_30d_price_sat TEXT NOT NULL,
paywall_boost_element_90d_price_sat TEXT NOT NULL,
paywall_boost_element_365d_price_sat TEXT NOT NULL
paywall_add_element_comment_price_sat INTEGER NOT NULL,
paywall_boost_element_30d_price_sat INTEGER NOT NULL,
paywall_boost_element_90d_price_sat INTEGER NOT NULL,
paywall_boost_element_365d_price_sat INTEGER NOT NULL
) STRICT;

INSERT INTO conf (paywall_add_element_comment_price_sat, paywall_boost_element_30d_price_sat, paywall_boost_element_90d_price_sat, paywall_boost_element_365d_price_sat) VALUES (500, 5000, 10000, 30000);
46 changes: 46 additions & 0 deletions src/conf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::Result;
use deadpool_sqlite::Pool;
use rusqlite::{Connection, Row};

pub struct Conf {
pub paywall_add_element_comment_price_sat: i64,
pub paywall_boost_element_30d_price_sat: i64,
pub paywall_boost_element_90d_price_sat: i64,
pub paywall_boost_element_365d_price_sat: i64,
}

const TABLE_NAME: &str = "conf";

const MAPPER_PROJECTION: &str = "paywall_add_element_comment_price_sat, paywall_boost_element_30d_price_sat, paywall_boost_element_90d_price_sat, paywall_boost_element_365d_price_sat";

impl Conf {
pub async fn select_async(pool: &Pool) -> Result<Conf> {
pool.get()
.await?
.interact(|conn| Conf::select(conn))
.await?
}

pub fn select(conn: &Connection) -> Result<Conf> {
let sql = format!(
r#"
SELECT {MAPPER_PROJECTION}
FROM {TABLE_NAME};
"#
);
conn.prepare(&sql)?
.query_row({}, mapper())
.map_err(Into::into)
}
}

const fn mapper() -> fn(&Row) -> rusqlite::Result<Conf> {
|row: &Row| -> rusqlite::Result<Conf> {
Ok(Conf {
paywall_add_element_comment_price_sat: row.get(0)?,
paywall_boost_element_30d_price_sat: row.get(1)?,
paywall_boost_element_90d_price_sat: row.get(2)?,
paywall_boost_element_365d_price_sat: row.get(3)?,
})
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use actix_web::middleware::{from_fn, Compress, NormalizePath};
use actix_web::{App, HttpServer};
pub use error::Error;
mod admin;
mod conf;
mod discord;
mod element;
mod error;
Expand Down
4 changes: 3 additions & 1 deletion src/rpc/paywall_add_element_comment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
conf::Conf,
element::Element,
element_comment::ElementComment,
invoice::{self},
Expand All @@ -24,6 +25,7 @@ pub struct Res {
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let conf = Conf::select_async(&pool).await?;
let element = Element::select_by_id_or_osm_id_async(&args.element_id, &pool)
.await?
.ok_or(format!(
Expand All @@ -35,7 +37,7 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Re
.await?;
let invoice = invoice::service::create(
format!("element_comment:{}:publish", comment.id),
500,
conf.paywall_add_element_comment_price_sat,
&pool,
)
.await?;
Expand Down
9 changes: 5 additions & 4 deletions src/rpc/paywall_boost_element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{element::Element, invoice, Result};
use crate::{conf::Conf, element::Element, invoice, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::{Deserialize, Serialize};
Expand All @@ -18,16 +18,17 @@ pub struct Res {
}

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)
.await?
.ok_or(format!(
"there is no element with id = {}",
&args.element_id,
))?;
let sats = match args.days {
30 => 5000,
90 => 10000,
365 => 30000,
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(
Expand Down
12 changes: 9 additions & 3 deletions src/rpc/paywall_get_add_element_comment_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::Result;
use crate::{conf::Conf, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::Data;
use serde::Serialize;
use std::sync::Arc;

pub const NAME: &str = "paywall_get_add_element_comment_quote";

Expand All @@ -8,6 +11,9 @@ pub struct Res {
pub quote_sat: i64,
}

pub async fn run() -> Result<Res> {
Ok(Res { quote_sat: 500 })
pub async fn run(pool: Data<Arc<Pool>>) -> Result<Res> {
let conf = Conf::select_async(&pool).await?;
Ok(Res {
quote_sat: conf.paywall_add_element_comment_price_sat,
})
}
14 changes: 9 additions & 5 deletions src/rpc/paywall_get_boost_element_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::Result;
use crate::{conf::Conf, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::Data;
use serde::Serialize;
use std::sync::Arc;

pub const NAME: &str = "paywall_get_boost_element_quote";

Expand All @@ -10,10 +13,11 @@ pub struct Res {
pub quote_365d_sat: i64,
}

pub async fn run() -> Result<Res> {
pub async fn run(pool: Data<Arc<Pool>>) -> Result<Res> {
let conf = Conf::select_async(&pool).await?;
Ok(Res {
quote_30d_sat: 5000,
quote_90d_sat: 10000,
quote_365d_sat: 30000,
quote_30d_sat: conf.paywall_boost_element_30d_price_sat,
quote_90d_sat: conf.paywall_boost_element_90d_price_sat,
quote_365d_sat: conf.paywall_boost_element_365d_price_sat,
})
}

0 comments on commit 39acb28

Please sign in to comment.