diff --git a/src/api/quotes.rs b/src/api/quotes.rs index f22d257..6955fcb 100644 --- a/src/api/quotes.rs +++ b/src/api/quotes.rs @@ -53,6 +53,8 @@ pub struct QuoteCreateForm { pub context: Option, pub location: Option, pub public: bool, + #[serde(default)] + pub discord_webhook: bool, } pub async fn create( @@ -85,5 +87,12 @@ pub async fn create( LogEntry::new(&mut tx, u, LogAction::CreateQuote { id: q.id }).await?; tx.commit().await?; + + if form.discord_webhook { + if let Some(ref url) = state.conf.read().await.discord_webhook { + q.post_msg_webhook(url.clone()); + } + } + Ok((StatusCode::CREATED, Json(q)).into_response()) } diff --git a/src/web/pages/quotes/add.rs b/src/web/pages/quotes/add.rs index d986bed..f6abf24 100644 --- a/src/web/pages/quotes/add.rs +++ b/src/web/pages/quotes/add.rs @@ -35,6 +35,7 @@ pub async fn page( None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()), }; let names = Name::get_all(&mut *conn).await?; + let feature_webhooks = state.conf.read().await.discord_webhook.is_some(); Ok(base( "Add Quote | Mnemosyne", @@ -91,6 +92,15 @@ pub async fn page( class="px-2 py-1 w-full mb-2 bg-neutral-950/50 rounded border border-neutral-200/25"; } } + @if feature_webhooks { + div class="flex flex-col justify-center mt-5" { + label class="flex items-center gap-2 cursor-pointer" { + input type="checkbox" name="discord_webhook" value="true" checked + class="w-4 h-4 cursor-pointer"; + span {"Send to Discord"} + } + } + } button type="submit" class="border mt-auto mb-2 cursor-pointer rounded h-fit px-2 py-1 bg-neutral-200/5 border-neutral-200/25 hover:border-neutral-200/45 hover:bg-neutral-200/15" { "Submit" } @@ -140,6 +150,7 @@ pub struct IncomingQuote { location: String, time: String, context: String, + discord_webhook: Option, } pub async fn form( State(state): State, @@ -178,8 +189,11 @@ pub async fn form( LogEntry::new(&mut *tx, u, LogAction::CreateQuote { id: q.id }).await?; tx.commit().await?; - if let Some(ref url) = state.conf.read().await.discord_webhook { - q.post_msg_webhook(url.clone()); + let should_send_webhook = form.discord_webhook.as_deref() == Some("true"); + if should_send_webhook { + if let Some(ref url) = state.conf.read().await.discord_webhook { + q.post_msg_webhook(url.clone()); + } } Ok(Redirect::to("/dashboard").into_response())