Skip to content

Commit

Permalink
Fix issue with boosts
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 4, 2024
1 parent 3b5a996 commit fe223a3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 60 deletions.
55 changes: 0 additions & 55 deletions src/element/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ use crate::Result;
use crate::{auth::Token, element::model::Element};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use rusqlite::Connection;
use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc;
use time::format_description::well_known::Iso8601;
use time::Duration;
use time::OffsetDateTime;
use tracing::info;

#[derive(Deserialize)]
Expand Down Expand Up @@ -116,57 +112,6 @@ pub async fn remove_tag(
Ok(element)
}

#[derive(Deserialize)]
pub struct BoostArgs {
pub token: String,
pub id: String,
pub days: i64,
}

pub async fn boost(Params(args): Params<BoostArgs>, pool: Data<Arc<Pool>>) -> Result<Element> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let element = pool
.get()
.await?
.interact(move |conn| _boost(&args.id, args.days, conn))
.await??;
let log_message = format!(
"{} boosted element {} https://api.btcmap.org/v3/elements/{} for {} days",
token.owner,
element.name(),
element.id,
args.days,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(element)
}

fn _boost(id_or_osm_id: &str, days: i64, conn: &Connection) -> Result<Element> {
let element = Element::select_by_id_or_osm_id(id_or_osm_id, conn)?.unwrap();
let boost_expires = element.tag("boost:expires");
let boost_expires = match boost_expires {
Value::String(v) => {
OffsetDateTime::parse(v, &Iso8601::DEFAULT).unwrap_or(OffsetDateTime::now_utc())
}
_ => OffsetDateTime::now_utc(),
};

let boost_expires = boost_expires.checked_add(Duration::days(days)).unwrap();
let element = Element::set_tag(
element.id,
"boost:expires",
&Value::String(boost_expires.format(&Iso8601::DEFAULT)?),
&conn,
)?;
Ok(element)
}

#[derive(Deserialize)]
pub struct CreateReviewArgs {
pub token: String,
Expand Down
2 changes: 1 addition & 1 deletion src/element/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub fn generate_issues(elements: Vec<&Element>, conn: &Connection) -> Result<Gen
}
}
let finished_at = OffsetDateTime::now_utc();
Ok(GenerateIssuesResult{
Ok(GenerateIssuesResult {
started_at,
finished_at,
time_s: (finished_at - started_at).as_seconds_f64(),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
mod area;
mod review;
mod vacuum;
mod rpc;
mod vacuum;

pub type Result<T, E = Error> = std::result::Result<T, E>;

Expand Down
64 changes: 64 additions & 0 deletions src/rpc/boost_element.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::{auth::Token, discord, element::Element, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use rusqlite::Connection;
use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc;
use time::{format_description::well_known::Iso8601, Duration, OffsetDateTime};
use tracing::info;

#[derive(Deserialize)]
pub struct Args {
pub token: String,
pub id: String,
pub days: i64,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Element> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let element = pool
.get()
.await?
.interact(move |conn| _boost(&args.id, args.days, conn))
.await??;
let log_message = format!(
"{} boosted element {} https://api.btcmap.org/v3/elements/{} for {} days",
token.owner,
element.name(),
element.id,
args.days,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(element)
}

fn _boost(id_or_osm_id: &str, days: i64, conn: &Connection) -> Result<Element> {
let element = Element::select_by_id_or_osm_id(id_or_osm_id, conn)?.unwrap();
let boost_expires = element.tag("boost:expires");
let boost_expires = match boost_expires {
Value::String(v) => {
OffsetDateTime::parse(v, &Iso8601::DEFAULT).unwrap_or(OffsetDateTime::now_utc())
}
_ => OffsetDateTime::now_utc(),
};
let boost_expires = if boost_expires < OffsetDateTime::now_utc() {
OffsetDateTime::now_utc()
} else {
boost_expires
};
let boost_expires = boost_expires.checked_add(Duration::days(days)).unwrap();
let element = Element::set_tag(
element.id,
"boost:expires",
&Value::String(boost_expires.format(&Iso8601::DEFAULT)?),
&conn,
)?;
Ok(element)
}
3 changes: 2 additions & 1 deletion src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod generate_element_issues;
pub mod boost_element;
pub mod generate_element_issues;
4 changes: 2 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub async fn run() -> Result<()> {
.with_method("getelement", element::rpc::get)
.with_method("setelementtag", element::rpc::set_tag)
.with_method("removeelementtag", element::rpc::remove_tag)
.with_method("boostelement", element::rpc::boost)
.with_method("createelementreview", element::rpc::create_review)
.with_method("boostelement", rpc::boost_element::run)
.with_method("addelementreview", element::rpc::create_review)
.with_method("generateelementissues", rpc::generate_element_issues::run)
.with_method("createarea", area::rpc::create)
.with_method("getarea", area::rpc::get)
Expand Down

0 comments on commit fe223a3

Please sign in to comment.