From 3de14ae55c019beea6b4ee222749f198a5e37212 Mon Sep 17 00:00:00 2001 From: Celeo Date: Mon, 21 Oct 2024 21:49:41 -0700 Subject: [PATCH] New feedback Discord notifications --- vzdv-site/src/endpoints/mod.rs | 47 +++++++++++++++++++++++++---- vzdv-site/templates/changelog.jinja | 1 + vzdv.empty.toml | 1 + vzdv.example.toml | 1 + vzdv/src/config.rs | 1 + 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/vzdv-site/src/endpoints/mod.rs b/vzdv-site/src/endpoints/mod.rs index f616a66..366f468 100644 --- a/vzdv-site/src/endpoints/mod.rs +++ b/vzdv-site/src/endpoints/mod.rs @@ -10,13 +10,17 @@ use axum::{ routing::{get, post}, Form, Router, }; -use log::info; +use log::{error, info}; use minijinja::{context, Environment}; use serde::Deserialize; +use serde_json::json; use std::sync::Arc; use tower_http::services::ServeDir; use tower_sessions::Session; -use vzdv::sql::{self, Controller}; +use vzdv::{ + sql::{self, Controller}, + GENERAL_HTTP_CLIENT, +}; pub mod admin; pub mod airspace; @@ -88,16 +92,47 @@ async fn page_feedback_form_post( .bind(user_info.cid) .execute(&state.db) .await?; - info!( - "{} submitted feedback for {}", - user_info.cid, feedback.controller - ); flashed_messages::push_flashed_message( session, flashed_messages::MessageLevel::Success, "Feedback submitted, thank you!", ) .await?; + info!( + "{} submitted feedback for {}", + user_info.cid, feedback.controller + ); + let notification_webhook = state.config.discord.webhooks.new_feedback.clone(); + let for_controller: Controller = sqlx::query_as(sql::GET_CONTROLLER_BY_CID) + .bind(feedback.controller) + .fetch_one(&state.db) + .await?; + let message = format!( + "New {} feedback posted for {} {} ({}) on **{}** by {}:\n\t{}", + feedback.rating, + for_controller.first_name, + for_controller.last_name, + for_controller + .operating_initials + .unwrap_or_else(|| String::from("??")), + feedback.position, + user_info.cid, + if feedback.comments.len() >= 100 { + format!("{} ...", &feedback.comments[0..100]) + } else { + feedback.comments + } + ); + tokio::spawn(async move { + let res = GENERAL_HTTP_CLIENT + .post(¬ification_webhook) + .json(&json!({ "content": message })) + .send() + .await; + if let Err(e) = res { + error!("Could not send info to new feedback webhook: {e}"); + } + }); } else { flashed_messages::push_flashed_message( session, diff --git a/vzdv-site/templates/changelog.jinja b/vzdv-site/templates/changelog.jinja index de264d2..8947d04 100644 --- a/vzdv-site/templates/changelog.jinja +++ b/vzdv-site/templates/changelog.jinja @@ -23,6 +23,7 @@
  • Audit reporting for some events to another Discord webhook.
  • Approved feedback from the ADH site imported here.
  • Controllers can see their own feedback on their controller page.
  • +
  • Sr Staff notifications of new controller feedback.
  • diff --git a/vzdv.empty.toml b/vzdv.empty.toml index 1e677aa..5668b4b 100644 --- a/vzdv.empty.toml +++ b/vzdv.empty.toml @@ -41,6 +41,7 @@ redirect_uri = "" [discord.webhooks] staffing_request = "" +new_feedback = "" feedback = "" new_visitor_app = "" errors = "" diff --git a/vzdv.example.toml b/vzdv.example.toml index 0b7d1bd..af7cefc 100644 --- a/vzdv.example.toml +++ b/vzdv.example.toml @@ -172,6 +172,7 @@ redirect_uri = "" [discord.webhooks] staffing_request = "" +new_feedback = "" feedback = "" new_visitor_app = "" errors = "" diff --git a/vzdv/src/config.rs b/vzdv/src/config.rs index f7cb5b4..3a85c9a 100644 --- a/vzdv/src/config.rs +++ b/vzdv/src/config.rs @@ -89,6 +89,7 @@ pub struct ConfigDiscordAuth { #[derive(Debug, Clone, Deserialize, Default)] pub struct ConfigDiscordWebhooks { pub staffing_request: String, + pub new_feedback: String, pub feedback: String, pub new_visitor_app: String, pub errors: String,