diff --git a/Cargo.lock b/Cargo.lock index ab9526b..be47f5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "arrayvec" version = "0.7.2" @@ -199,6 +208,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits 0.2.15", + "time 0.1.45", + "wasm-bindgen", + "winapi", +] + [[package]] name = "cipher" version = "0.3.0" @@ -217,6 +241,22 @@ dependencies = [ "cc", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + [[package]] name = "cpufeatures" version = "0.2.5" @@ -254,6 +294,50 @@ dependencies = [ "typenum", ] +[[package]] +name = "cxx" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86d3488e7665a7a483b57e25bdd90d0aeb2bc7608c8d0346acf2ad3f1caf1d62" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48fcaf066a053a41a81dfb14d57d99738b767febb8b735c3016e469fac5da690" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2ef98b8b717a829ca5603af80e1f9e2e48013ab227b68ef37872ef84ee479bf" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dashmap" version = "5.4.0" @@ -476,7 +560,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -591,6 +675,30 @@ dependencies = [ "tokio-rustls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "idna" version = "0.3.0" @@ -644,6 +752,15 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "lock_api" version = "0.4.9" @@ -704,6 +821,7 @@ name = "memejoin-rs" version = "0.1.1-alpha" dependencies = [ "axum", + "chrono", "futures", "serde", "serde_json", @@ -748,7 +866,7 @@ checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.42.0", ] @@ -771,6 +889,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits 0.2.15", +] + [[package]] name = "num-traits" version = "0.1.43" @@ -1132,6 +1260,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" + [[package]] name = "sct" version = "0.7.0" @@ -1238,7 +1372,7 @@ dependencies = [ "serde", "serde-value", "serde_json", - "time", + "time 0.3.17", "tokio", "tracing", "typemap_rev", @@ -1407,6 +1541,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.38" @@ -1436,6 +1579,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.17" @@ -1722,6 +1876,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "universal-hash" version = "0.4.1" @@ -1787,6 +1947,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1917,6 +2083,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 32e6ff3..bdb37ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] axum = "0.6.9" +chrono = "0.4.23" futures = "0.3.26" serde = "1.0.152" serde_json = "1.0.93" diff --git a/src/main.rs b/src/main.rs index daac78a..c7c3646 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod routes; pub mod settings; use axum::http::{HeaderValue, Method}; -use axum::routing::get; +use axum::routing::{get, post}; use axum::Router; use futures::StreamExt; use songbird::tracks::TrackQueue; @@ -120,6 +120,8 @@ fn spawn_api(settings: Arc>) -> tokio::task::JoinHandle<()> { .route("/health", get(routes::health)) .route("/me/:user", get(routes::me)) .route("/intros/:guild", get(routes::intros)) + .route("/intros/:guild/:channel/:user/:intro", post(routes::add_intro_to_user)) + .route("/intros/:guild/:channel/:user/:intro/remove", post(routes::remove_intro_to_user)) .layer( CorsLayer::new() .allow_origin("*".parse::().unwrap()) @@ -210,12 +212,24 @@ async fn spawn_bot(settings: Arc>) -> Vec match songbird::ytdl(&intro.url).await { diff --git a/src/routes.rs b/src/routes.rs index b374e79..4618791 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,6 +7,7 @@ use axum::{ use serde::Serialize; use serde_json::{json, Value}; use tokio::sync::Mutex; +use tracing::error; use crate::settings::{GuildSettings, Intro, IntroIndex, Settings, UserSettings}; @@ -46,6 +47,49 @@ pub(crate) async fn health(State(state): State>>) -> Json>>, + Path((guild, channel, user, intro_index)): Path<(u64, String, String, usize)>, +) { + let mut settings = state.lock().await; + + let Some(guild) = settings.guilds.get_mut(&guild) else { return; }; + let Some(channel) = guild.channels.get_mut(&channel) else { return; }; + let Some(user) = channel.users.get_mut(&user) else { return; }; + + user.intros.push(IntroIndex { + index: intro_index, + volume: 20, + }); + + if let Err(err) = settings.save() { + error!("Failed to save config: {err:?}"); + } +} + +pub(crate) async fn remove_intro_to_user( + State(state): State>>, + Path((guild, channel, user, intro_index)): Path<(u64, String, String, usize)>, +) { + let mut settings = state.lock().await; + + let Some(guild) = settings.guilds.get_mut(&guild) else { return; }; + let Some(channel) = guild.channels.get_mut(&channel) else { return; }; + let Some(user) = channel.users.get_mut(&user) else { return; }; + + if let Some(index) = user + .intros + .iter() + .position(|intro| intro_index == intro.index) + { + user.intros.remove(index); + } + + if let Err(err) = settings.save() { + error!("Failed to save config: {err:?}"); + } +} + pub(crate) async fn intros( State(state): State>>, Path(guild): Path, diff --git a/src/settings.rs b/src/settings.rs index a959330..e7cf9fc 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -16,6 +16,23 @@ impl TypeMapKey for Settings { type Value = Arc; } +impl Settings { + pub(crate) fn save(&self) -> Result<(), std::io::Error> { + let serialized = serde_json::to_string_pretty(&self)?; + + std::fs::copy( + "./config/settings.json", + format!( + "./config/{}-settings.json.old", + chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S") + ), + )?; + std::fs::write("./config/settings.json", serialized)?; + + Ok(()) + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct GuildSettings {