Skip to content

Commit

Permalink
Add boost element RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 27, 2024
1 parent 3044e57 commit 3143b12
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 64 deletions.
45 changes: 0 additions & 45 deletions src/boost/mod.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/element/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod model;
pub use model::Element;
pub mod admin;
pub mod rpc;
pub mod service;
pub mod v2;
pub mod v3;
6 changes: 5 additions & 1 deletion src/element/model.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use crate::Result;
use crate::{osm::overpass::OverpassElement, Error};
use rusqlite::{named_params, Connection, OptionalExtension, Row};
use serde::Serialize;
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::time::Instant;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use tracing::{debug, info};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Element {
pub id: i64,
pub overpass_data: OverpassElement,
pub tags: HashMap<String, Value>,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<OffsetDateTime>,
}

Expand Down
67 changes: 67 additions & 0 deletions src/element/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::discord;
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)]
pub struct BoostElementArgs {
pub token: String,
pub element_id: String,
pub days: i64,
}

pub async fn boost(
Params(args): Params<BoostElementArgs>,
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.element_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)
}
18 changes: 0 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
mod area;
mod boost;
mod lint;
mod sync;
mod vacuum;
Expand Down Expand Up @@ -145,23 +144,6 @@ async fn main() -> ExitCode {
return ExitCode::FAILURE;
}
}
"boost" => {
if let Err(e) = boost::run(
args.get(2).unwrap_or(&String::new()),
args.get(3)
.unwrap_or(&String::new())
.parse::<i64>()
.unwrap(),
args.get(4)
.unwrap_or(&String::new())
.parse::<i64>()
.unwrap(),
&conn,
) {
error!(?e, "Failed to boost element");
return ExitCode::FAILURE;
}
}
"update-areas-tag" => {
if let Err(e) = command::update_areas_tag::run(args) {
error!(?e, "Failed to add areas tag");
Expand Down
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub async fn run() -> Result<()> {
service("rpc").guard(actix_web::guard::Post()).finish(
jsonrpc_v2::Server::new()
.with_data(jsonrpc_v2::Data::new(pool.clone()))
.with_method("boostelement", element::rpc::boost)
.with_method("createarea", area::rpc::create)
.with_method("getarea", area::rpc::get)
.with_method("setareatag", area::rpc::set_tag)
Expand Down

0 comments on commit 3143b12

Please sign in to comment.