Public discord webhook (#492)

This commit is contained in:
Geometrically
2022-12-06 19:51:03 -07:00
committed by GitHub
parent e96d23cc3f
commit e809f77461
16 changed files with 1391 additions and 905 deletions

View File

@@ -226,6 +226,8 @@ pub enum ApiError {
Crypto(String),
#[error("Payments Error: {0}")]
Payments(String),
#[error("Discord Error: {0}")]
DiscordError(String),
}
impl actix_web::ResponseError for ApiError {
@@ -272,6 +274,9 @@ impl actix_web::ResponseError for ApiError {
ApiError::Payments(..) => {
actix_web::http::StatusCode::FAILED_DEPENDENCY
}
ApiError::DiscordError(..) => {
actix_web::http::StatusCode::FAILED_DEPENDENCY
}
}
}
@@ -294,6 +299,7 @@ impl actix_web::ResponseError for ApiError {
ApiError::Analytics(..) => "analytics_error",
ApiError::Crypto(..) => "crypto_error",
ApiError::Payments(..) => "payments_error",
ApiError::DiscordError(..) => "discord_error",
},
description: &self.to_string(),
},

View File

@@ -277,6 +277,7 @@ pub async fn project_create(
&***file_host,
&flame_anvil_queue,
&mut uploaded_files,
&*client,
)
.await;
@@ -334,6 +335,7 @@ pub async fn project_create_inner(
file_host: &dyn FileHost,
flame_anvil_queue: &Mutex<FlameAnvilQueue>,
uploaded_files: &mut Vec<UploadedFile>,
pool: &PgPool,
) -> Result<HttpResponse, CreateError> {
// The base URL for files uploaded to backblaze
let cdn_url = dotenvy::var("CDN_URL")?;
@@ -817,7 +819,8 @@ pub async fn project_create_inner(
if let Ok(webhook_url) = dotenvy::var("MODERATION_DISCORD_WEBHOOK")
{
crate::util::webhook::send_discord_webhook(
response.clone(),
response.id,
pool,
webhook_url,
)
.await

View File

@@ -499,7 +499,8 @@ pub async fn project_edit(
dotenvy::var("MODERATION_DISCORD_WEBHOOK")
{
crate::util::webhook::send_discord_webhook(
Project::from(project_item.clone()),
project_item.inner.id.into(),
&*pool,
webhook_url,
)
.await
@@ -507,7 +508,9 @@ pub async fn project_edit(
}
}
if status.is_approved() {
if status.is_approved()
&& !project_item.inner.status.is_approved()
{
sqlx::query!(
"
UPDATE mods
@@ -520,6 +523,31 @@ pub async fn project_edit(
.await?;
}
if status.is_searchable() && !project_item.inner.webhook_sent {
if let Ok(webhook_url) =
dotenvy::var("PUBLIC_DISCORD_WEBHOOK")
{
crate::util::webhook::send_discord_webhook(
project_item.inner.id.into(),
&*pool,
webhook_url,
)
.await
.ok();
sqlx::query!(
"
UPDATE mods
SET webhook_sent = TRUE
WHERE id = $1
",
id as database::models::ids::ProjectId,
)
.execute(&mut *transaction)
.await?;
}
}
sqlx::query!(
"
UPDATE mods

View File

@@ -231,8 +231,8 @@ pub async fn game_version_list(
.into_iter()
.map(|x| GameVersionQueryData {
version: x.version,
version_type: x.version_type,
date: x.date,
version_type: x.type_,
date: x.created,
major: x.major,
})
.collect();

View File

@@ -133,6 +133,7 @@ pub async fn mod_create(
&***file_host,
&flame_anvil_queue,
&mut uploaded_files,
&*client,
)
.await;

View File

@@ -29,6 +29,10 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use validator::Validate;
fn default_requested_status() -> VersionStatus {
VersionStatus::Listed
}
#[derive(Serialize, Deserialize, Validate, Clone)]
pub struct InitialVersionData {
#[serde(alias = "mod_id")]
@@ -59,6 +63,7 @@ pub struct InitialVersionData {
pub loaders: Vec<Loader>,
pub featured: bool,
pub primary_file: Option<String>,
#[serde(default = "default_requested_status")]
pub status: VersionStatus,
}