From 51777c3f33734adb87e63ca7094551666fbd2ce9 Mon Sep 17 00:00:00 2001 From: Emma Alexia Triphora Date: Sun, 24 Sep 2023 12:04:58 -0400 Subject: [PATCH] Replace moderation project queue with moderation action log (#718) --- src/main.rs | 72 ------------------------------- src/routes/v2/project_creation.rs | 14 ------ src/routes/v2/projects.rs | 32 ++++++++------ 3 files changed, 20 insertions(+), 98 deletions(-) diff --git a/src/main.rs b/src/main.rs index 427311b4..5a6aed60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use crate::ratelimit::middleware::RateLimiter; use crate::util::cors::default_cors; use crate::util::env::{parse_strings_from_var, parse_var}; use actix_web::{web, App, HttpServer}; -use chrono::{DateTime, Utc}; use deadpool_redis::{Config, Runtime}; use env_logger::Env; use log::{error, info, warn}; @@ -181,77 +180,6 @@ async fn main() -> std::io::Result<()> { } }); - // Reminding moderators to review projects which have been in the queue longer than 40hr - let pool_ref = pool.clone(); - let redis_ref = redis_pool.clone(); - let webhook_message_sent = Arc::new(Mutex::new(Vec::<( - database::models::ProjectId, - DateTime, - )>::new())); - - scheduler.run(std::time::Duration::from_secs(10 * 60), move || { - let pool_ref = pool_ref.clone(); - let redis_ref = redis_ref.clone(); - let webhook_message_sent_ref = webhook_message_sent.clone(); - info!("Checking reviewed projects submitted more than 40hrs ago"); - - async move { - let do_steps = async { - use futures::TryStreamExt; - - let project_ids = sqlx::query!( - " - SELECT id FROM mods - WHERE status = $1 AND queued < NOW() - INTERVAL '40 hours' - ORDER BY updated ASC - ", - crate::models::projects::ProjectStatus::Processing.as_str(), - ) - .fetch_many(&pool_ref) - .try_filter_map(|e| async { - Ok(e.right().map(|m| database::models::ProjectId(m.id))) - }) - .try_collect::>() - .await?; - - let mut webhook_message_sent_ref = webhook_message_sent_ref.lock().await; - - webhook_message_sent_ref.retain(|x| Utc::now() - x.1 < chrono::Duration::hours(12)); - - for project in project_ids { - if webhook_message_sent_ref.iter().any(|x| x.0 == project) { continue; } - - if let Ok(webhook_url) = - dotenvy::var("MODERATION_DISCORD_WEBHOOK") - { - util::webhook::send_discord_webhook( - project.into(), - &pool_ref, - &redis_ref, - webhook_url, - Some("<@&783155186491195394> This project has been in the queue for over 40 hours!".to_string()), - ) - .await - .ok(); - - webhook_message_sent_ref.push((project, Utc::now())); - } - } - - Ok::<(), routes::ApiError>(()) - }; - - if let Err(e) = do_steps.await { - warn!( - "Checking reviewed projects submitted more than 40hrs ago failed: {:?}", - e - ); - } - - info!("Finished checking reviewed projects submitted more than 40hrs ago"); - } - }); - scheduler::schedule_versions(&mut scheduler, pool.clone()); let download_queue = web::Data::new(DownloadQueue::new()); diff --git a/src/routes/v2/project_creation.rs b/src/routes/v2/project_creation.rs index 906f210a..2b5e879e 100644 --- a/src/routes/v2/project_creation.rs +++ b/src/routes/v2/project_creation.rs @@ -875,20 +875,6 @@ async fn project_create_inner( monetization_status: MonetizationStatus::Monetized, }; - if status == ProjectStatus::Processing { - if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK") { - crate::util::webhook::send_discord_webhook( - response.id, - pool, - redis, - webhook_url, - None, - ) - .await - .ok(); - } - } - Ok(HttpResponse::Ok().json(response)) } } diff --git a/src/routes/v2/projects.rs b/src/routes/v2/projects.rs index 73f166b8..665ac333 100644 --- a/src/routes/v2/projects.rs +++ b/src/routes/v2/projects.rs @@ -493,18 +493,6 @@ pub async fn project_edit( ) .execute(&mut *transaction) .await?; - - if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK") { - crate::util::webhook::send_discord_webhook( - project_item.inner.id.into(), - &pool, - &redis, - webhook_url, - None, - ) - .await - .ok(); - } } if status.is_approved() && !project_item.inner.status.is_approved() { @@ -545,6 +533,26 @@ pub async fn project_edit( } } + if user.role.is_mod() { + if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK") { + crate::util::webhook::send_discord_webhook( + project_item.inner.id.into(), + &pool, + &redis, + webhook_url, + Some( + format!( + "**[{}]({}/user/{})** changed project status from **{}** to **{}**", + user.username, dotenvy::var("SITE_URL")?, user.username, &project_item.inner.status, status + ) + .to_string(), + ), + ) + .await + .ok(); + } + } + if team_member.map(|x| !x.accepted).unwrap_or(true) { let notified_members = sqlx::query!( "