diff --git a/src/element/mod.rs b/src/element/mod.rs index f8948ea..29e6326 100644 --- a/src/element/mod.rs +++ b/src/element/mod.rs @@ -1,7 +1,6 @@ pub mod model; pub use model::Element; pub mod admin; -pub mod rpc; pub mod service; pub mod v2; pub mod v3; diff --git a/src/element/rpc.rs b/src/element/rpc.rs deleted file mode 100644 index 872b41a..0000000 --- a/src/element/rpc.rs +++ /dev/null @@ -1,112 +0,0 @@ -use crate::discord; -use crate::Result; -use crate::{auth::Token, element::model::Element}; -use deadpool_sqlite::Pool; -use jsonrpc_v2::{Data, Params}; -use serde::Deserialize; -use serde_json::Value; -use std::sync::Arc; -use tracing::info; - -#[derive(Deserialize)] -pub struct GetArgs { - pub token: String, - pub id: String, -} - -pub async fn get(Params(args): Params, pool: Data>) -> Result { - pool.get() - .await? - .interact(move |conn| Token::select_by_secret(&args.token, conn)) - .await?? - .unwrap(); - let element = pool - .get() - .await? - .interact(move |conn| Element::select_by_id_or_osm_id(&args.id, conn)) - .await?? - .unwrap(); - Ok(element) -} - -#[derive(Deserialize)] -pub struct SetTagArgs { - pub token: String, - pub id: String, - pub name: String, - pub value: Value, -} - -pub async fn set_tag(Params(args): Params, pool: Data>) -> Result { - 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| Element::select_by_id_or_osm_id(&args.id, conn)) - .await?? - .unwrap(); - let cloned_name = args.name.clone(); - let cloned_value = args.value.clone(); - let element = pool - .get() - .await? - .interact(move |conn| Element::set_tag(element.id, &cloned_name, &cloned_value, conn)) - .await??; - let log_message = format!( - "{} set tag {} = {} for element {} https://api.btcmap.org/v3/elements/{}", - token.owner, - args.name, - serde_json::to_string(&args.value)?, - element.name(), - element.id, - ); - info!(log_message); - discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; - Ok(element) -} - -#[derive(Deserialize)] -pub struct RemoveTagArgs { - pub token: String, - pub id: String, - pub tag: String, -} - -pub async fn remove_tag( - Params(args): Params, - pool: Data>, -) -> Result { - 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| Element::select_by_id_or_osm_id(&args.id, conn)) - .await?? - .unwrap(); - let cloned_tag = args.tag.clone(); - let element = pool - .get() - .await? - .interact(move |conn| Element::remove_tag(element.id, &cloned_tag, conn)) - .await??; - let log_message = format!( - "{} removed tag {} from element {} https://api.btcmap.org/v3/elements/{}", - token.owner, - args.tag, - element.name(), - element.id, - ); - info!(log_message); - discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; - Ok(element) -} diff --git a/src/rpc/add_element_comment.rs b/src/rpc/add_element_comment.rs index c113914..ccd057d 100644 --- a/src/rpc/add_element_comment.rs +++ b/src/rpc/add_element_comment.rs @@ -9,7 +9,7 @@ use tracing::info; pub struct Args { pub token: String, pub id: String, - pub review: String, + pub comment: String, } pub async fn run(Params(args): Params, pool: Data>) -> Result { @@ -25,18 +25,18 @@ pub async fn run(Params(args): Params, pool: Data>) -> Result, pool: Data>) -> Result { + pool.get() + .await? + .interact(move |conn| Token::select_by_secret(&args.token, conn)) + .await?? + .unwrap(); + let element = pool + .get() + .await? + .interact(move |conn| Element::select_by_id_or_osm_id(&args.id, conn)) + .await?? + .unwrap(); + Ok(element) +} diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 086cc93..185d254 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -3,8 +3,11 @@ pub mod add_element_comment; pub mod boost_element; pub mod generate_element_issues; pub mod get_area; +pub mod get_element; pub mod get_trending_communities; pub mod get_trending_countries; pub mod remove_area; pub mod remove_area_tag; +pub mod remove_element_tag; pub mod set_area_tag; +pub mod set_element_tag; diff --git a/src/rpc/remove_element_tag.rs b/src/rpc/remove_element_tag.rs new file mode 100644 index 0000000..c3bc8fe --- /dev/null +++ b/src/rpc/remove_element_tag.rs @@ -0,0 +1,46 @@ +use crate::discord; +use crate::Result; +use crate::{auth::Token, element::model::Element}; +use deadpool_sqlite::Pool; +use jsonrpc_v2::{Data, Params}; +use serde::Deserialize; +use std::sync::Arc; +use tracing::info; + +#[derive(Deserialize)] +pub struct Args { + pub token: String, + pub id: String, + pub tag: String, +} + +pub async fn run(Params(args): Params, pool: Data>) -> Result { + 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| Element::select_by_id_or_osm_id(&args.id, conn)) + .await?? + .unwrap(); + let cloned_tag = args.tag.clone(); + let element = pool + .get() + .await? + .interact(move |conn| Element::remove_tag(element.id, &cloned_tag, conn)) + .await??; + let log_message = format!( + "{} removed tag {} from element {} https://api.btcmap.org/v3/elements/{}", + token.owner, + args.tag, + element.name(), + element.id, + ); + info!(log_message); + discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; + Ok(element) +} diff --git a/src/rpc/set_element_tag.rs b/src/rpc/set_element_tag.rs new file mode 100644 index 0000000..0f86780 --- /dev/null +++ b/src/rpc/set_element_tag.rs @@ -0,0 +1,50 @@ +use crate::discord; +use crate::Result; +use crate::{auth::Token, element::model::Element}; +use deadpool_sqlite::Pool; +use jsonrpc_v2::{Data, Params}; +use serde::Deserialize; +use serde_json::Value; +use std::sync::Arc; +use tracing::info; + +#[derive(Deserialize)] +pub struct Args { + pub token: String, + pub id: String, + pub name: String, + pub value: Value, +} + +pub async fn run(Params(args): Params, pool: Data>) -> Result { + 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| Element::select_by_id_or_osm_id(&args.id, conn)) + .await?? + .unwrap(); + let cloned_name = args.name.clone(); + let cloned_value = args.value.clone(); + let element = pool + .get() + .await? + .interact(move |conn| Element::set_tag(element.id, &cloned_name, &cloned_value, conn)) + .await??; + let log_message = format!( + "{} set tag {} = {} for element {} https://api.btcmap.org/v3/elements/{}", + token.owner, + args.name, + serde_json::to_string(&args.value)?, + element.name(), + element.id, + ); + info!(log_message); + discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; + Ok(element) +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 81fcacb..3fa2496 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -81,9 +81,9 @@ 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("getelement", element::rpc::get) - .with_method("setelementtag", element::rpc::set_tag) - .with_method("removeelementtag", element::rpc::remove_tag) + .with_method("getelement", rpc::get_element::run) + .with_method("setelementtag", rpc::set_element_tag::run) + .with_method("removeelementtag", rpc::remove_element_tag::run) .with_method("boostelement", rpc::boost_element::run) .with_method("addelementcomment", rpc::add_element_comment::run) .with_method("generateelementissues", rpc::generate_element_issues::run)