diff --git a/gazebot/Cargo.lock b/gazebot/Cargo.lock index a3a3819..f749079 100644 --- a/gazebot/Cargo.lock +++ b/gazebot/Cargo.lock @@ -557,6 +557,7 @@ version = "0.1.0" dependencies = [ "dotenvy", "reqwest 0.12.9", + "scc", "serenity", "shuttle-runtime", "shuttle-serenity", @@ -1730,6 +1731,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "scc" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94b13f8ea6177672c49d12ed964cca44836f59621981b04a3e26b87e675181de" +dependencies = [ + "sdd", +] + [[package]] name = "schannel" version = "0.1.26" @@ -1755,6 +1765,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sdd" +version = "3.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "478f121bb72bbf63c52c93011ea1791dca40140dfe13f8336c4c5ac952c33aa9" + [[package]] name = "secrecy" version = "0.8.0" diff --git a/gazebot/Cargo.toml b/gazebot/Cargo.toml index f54316f..7c1af9b 100644 --- a/gazebot/Cargo.toml +++ b/gazebot/Cargo.toml @@ -11,4 +11,5 @@ serenity = { version = "0.12", default-features = false, features = ["client", " tokio = { version = "1", features = ["macros", "rt-multi-thread"] } shuttle-serenity = "0.48.0" shuttle-runtime = "0.48.0" -reqwest = { version = "0.12", features = ["json"] } \ No newline at end of file +reqwest = { version = "0.12", features = ["json"] } +scc = "2" \ No newline at end of file diff --git a/gazebot/src/main.rs b/gazebot/src/main.rs index d735f12..5bb4223 100644 --- a/gazebot/src/main.rs +++ b/gazebot/src/main.rs @@ -1,10 +1,12 @@ use reqwest::header::AUTHORIZATION; -use serenity::all::ActivityData; -use serenity::async_trait; -use serenity::json::json; -use serenity::model::channel::Message; -use serenity::model::prelude::*; -use serenity::prelude::*; +use scc::HashMap as ConcurrentHashMap; +use serenity::{ + all::ActivityData, + async_trait, + json::Value as JsonValue, + model::{channel::Message, prelude::*}, + prelude::*, +}; use shuttle_runtime::SecretStore; const ADMIN_USER_ID: u64 = 853064602904166430; @@ -12,6 +14,7 @@ const ADMIN_USER_ID: u64 = 853064602904166430; struct Handler { http: reqwest::Client, secrets: SecretStore, + notes: ConcurrentHashMap, } #[async_trait] @@ -26,14 +29,29 @@ impl EventHandler for Handler { } let mut note_content = msg.content.clone(); - + const BSKY_TAG: &str = ".nobsky"; let no_bsky_posse = note_content.contains(BSKY_TAG); if no_bsky_posse { note_content = note_content.replace(BSKY_TAG, ""); } - let note_data = json!({"content": note_content.trim(), "bskyPosse": !no_bsky_posse}); + let mut note_data = serenity::json::JsonMap::new(); + note_data.insert( + "content".to_string(), + JsonValue::String(note_content.trim().to_string()), + ); + note_data.insert("bskyPosse".to_string(), JsonValue::Bool(!no_bsky_posse)); + // add replyTo if we are replying to a previous message + if let Some(reply_msg) = msg.referenced_message.as_deref() { + if let Some(reply_note_id) = self + .notes + .read_async(&reply_msg.id, |_, v| v.to_owned()) + .await + { + note_data.insert("replyTo".to_string(), JsonValue::String(reply_note_id)); + } + } let resp = self .http .post("https://gaze.systems/log/create") @@ -42,7 +60,7 @@ impl EventHandler for Handler { .send() .await .and_then(|resp| resp.error_for_status()); - + let resp = match resp { Ok(r) => r, Err(why) => { @@ -52,10 +70,17 @@ impl EventHandler for Handler { }; let note_resp = resp.json::().await.unwrap(); - let created_note_id = note_resp["noteId"].as_str().expect("note id must be a string"); + let created_note_id = note_resp["noteId"] + .as_str() + .expect("note id must be a string"); tracing::info!("succesfully created note with id {created_note_id}"); - let mut reply_content = format!("created log at https://gaze.systems/log?id={created_note_id}"); + self.notes + .upsert_async(msg.id, created_note_id.to_string()) + .await; + + let mut reply_content = + format!("created log at https://gaze.systems/log?id={created_note_id}"); let errors = note_resp["errors"].as_array().expect("must be array"); if !errors.is_empty() { reply_content.push_str("\n\nerrors:"); @@ -90,6 +115,7 @@ async fn serenity( .event_handler(Handler { http: reqwest::Client::new(), secrets, + notes: ConcurrentHashMap::new(), }) .await .expect("Err creating client");