Skip to content

Commit

Permalink
Refactor RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 5, 2024
1 parent 5054353 commit 2423027
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 120 deletions.
1 change: 0 additions & 1 deletion src/element/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
112 changes: 0 additions & 112 deletions src/element/rpc.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/rpc/add_element_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Args>, pool: Data<Arc<Pool>>) -> Result<ElementComment> {
Expand All @@ -25,18 +25,18 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<El
.interact(move |conn| Element::select_by_id_or_osm_id(&args.id, conn))
.await??
.unwrap();
let cloned_review = args.review.clone();
let cloned_comment = args.comment.clone();
let review = pool
.get()
.await?
.interact(move |conn| ElementComment::insert(element.id, &cloned_review, conn))
.interact(move |conn| ElementComment::insert(element.id, &cloned_comment, conn))
.await??;
let log_message = format!(
"{} added a comment to element {} ({}): {}",
token.owner,
element.name(),
element.id,
args.review,
args.comment,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Expand Down
27 changes: 27 additions & 0 deletions src/rpc/get_element.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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;

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

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Element> {
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)
}
3 changes: 3 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
46 changes: 46 additions & 0 deletions src/rpc/remove_element_tag.rs
Original file line number Diff line number Diff line change
@@ -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<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| 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)
}
50 changes: 50 additions & 0 deletions src/rpc/set_element_tag.rs
Original file line number Diff line number Diff line change
@@ -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<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| 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)
}
6 changes: 3 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2423027

Please sign in to comment.