Skip to content

Commit

Permalink
Add user tag management RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 16, 2024
1 parent 4b7662a commit 38541a4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/event/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ pub async fn on_new_event(event: &Event, conn: &Connection) -> Result<()> {

let now = OffsetDateTime::now_utc();
let now: String = now.format(&Rfc3339)?;
user.set_tag("osm:sync:date", &Value::String(now), &conn)?;
User::set_tag(user.id, "osm:sync:date", &Value::String(now), &conn)?;
}
None => {
warn!(user.osm_data.id, "User no longer exists on OSM");
user.set_tag("osm:missing", &Value::Bool(true), &conn)?;
User::set_tag(user.id, "osm:missing", &Value::Bool(true), &conn)?;
}
},
Err(e) => error!("Failed to fetch user {} {}", user.osm_data.id, e),
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ async fn main() -> Result<()> {
.with_method("add_area", rpc::add_area::run)
.with_method("get_area", rpc::get_area::run)
.with_method("set_area_tag", rpc::set_area_tag::run)
.with_method("set_user_tag", rpc::set_user_tag::run)
.with_method("remove_area_tag", rpc::remove_area_tag::run)
.with_method("remove_user_tag", rpc::remove_user_tag::run)
.with_method("get_trending_countries", rpc::get_trending_countries::run)
.with_method(
"get_most_commented_countries",
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ pub mod remove_admin_action;
pub mod remove_area;
pub mod remove_area_tag;
pub mod remove_element_tag;
pub mod remove_user_tag;
pub mod search;
pub mod set_area_icon;
pub mod set_area_tag;
pub mod set_element_tag;
pub mod set_user_tag;
pub mod sync_elements;
49 changes: 49 additions & 0 deletions src/rpc/remove_user_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::{admin, discord, user::User, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::sync::Arc;
use tracing::info;

const NAME: &str = "remove_user_tag";

#[derive(Deserialize, Clone)]
pub struct Args {
pub password: String,
pub user_name: String,
pub tag_name: String,
}

#[derive(Serialize)]
pub struct Res {
pub id: i64,
pub tags: Map<String, Value>,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let admin = admin::service::check_rpc(&args.password, NAME, &pool).await?;
let cloned_args = args.clone();
let user = pool
.get()
.await?
.interact(move |conn| User::select_by_name(&cloned_args.user_name, conn))
.await??
.ok_or(format!("There is no user with name = {}", args.user_name))?;
let user = pool
.get()
.await?
.interact(move |conn| User::remove_tag(user.id, &cloned_args.tag_name, conn))
.await??
.ok_or(format!("There is no user with name = {}", args.user_name))?;
let log_message = format!(
"{} removed tag {} for user {} https://api.btcmap.org/v3/users/{}",
admin.name, args.tag_name, args.user_name, user.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(Res {
id: user.id,
tags: user.tags,
})
}
55 changes: 55 additions & 0 deletions src/rpc/set_user_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::{admin, discord, user::User, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::sync::Arc;
use tracing::info;

const NAME: &str = "set_user_tag";

#[derive(Deserialize, Clone)]
pub struct Args {
pub password: String,
pub user_name: String,
pub tag_name: String,
pub tag_value: Value,
}

#[derive(Serialize)]
pub struct Res {
pub id: i64,
pub tags: Map<String, Value>,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let admin = admin::service::check_rpc(&args.password, NAME, &pool).await?;
let cloned_args = args.clone();
let user = pool
.get()
.await?
.interact(move |conn| User::select_by_name(&cloned_args.user_name, conn))
.await??
.ok_or(format!("There is no user with name = {}", args.user_name))?;
let user = pool
.get()
.await?
.interact(move |conn| {
User::set_tag(user.id, &cloned_args.tag_name, &cloned_args.tag_value, conn)
})
.await??;
let log_message = format!(
"{} set tag {} = {} for user {} https://api.btcmap.org/v3/users/{}",
admin.name,
args.tag_name,
serde_json::to_string(&args.tag_value)?,
args.user_name,
user.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(Res {
id: user.id,
tags: user.tags,
})
}
27 changes: 22 additions & 5 deletions src/user/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ impl User {
Ok(res)
}

pub fn set_tag(&self, name: &str, value: &Value, conn: &Connection) -> Result<User> {
User::_set_tag(self.id, name, value, conn)
}

pub fn _set_tag(id: i64, name: &str, value: &Value, conn: &Connection) -> Result<User> {
pub fn set_tag(id: i64, name: &str, value: &Value, conn: &Connection) -> Result<User> {
let mut patch_set = HashMap::new();
patch_set.insert(name.into(), value.clone());
User::patch_tags(id, &patch_set, conn)
Expand All @@ -173,6 +169,27 @@ impl User {
.ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?)
}

pub fn remove_tag(id: i64, name: &str, conn: &Connection) -> Result<Option<User>> {
let query = format!(
r#"
UPDATE user
SET tags = json_remove(tags, :name)
WHERE id = :id
"#
);
#[cfg(not(test))]
sleep(Duration::from_millis(10));
conn.execute(
&query,
named_params! {
":id": id,
":name": format!("$.{name}"),
},
)?;
let res = User::select_by_id(id, &conn)?;
Ok(res)
}

pub fn set_osm_data(id: i64, osm_data: &OsmUser, conn: &Connection) -> Result<()> {
let query = r#"
UPDATE user
Expand Down

0 comments on commit 38541a4

Please sign in to comment.