Webhook update (#498)

* Update webhook

* Run clippy
This commit is contained in:
Geometrically 2022-12-08 19:44:46 -07:00 committed by GitHub
parent 30b29de8ce
commit 5c7b175e90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 8 deletions

View File

@ -457,11 +457,11 @@ pub async fn project_edit(
)); ));
} }
if !user.role.is_mod() if !(user.role.is_mod()
&& !(!project_item.inner.status.is_approved() || !project_item.inner.status.is_approved()
&& status == &ProjectStatus::Processing && status == &ProjectStatus::Processing
|| project_item.inner.status.is_approved() || project_item.inner.status.is_approved()
&& status.can_be_requested()) && status.can_be_requested())
{ {
return Err(ApiError::CustomAuthentication( return Err(ApiError::CustomAuthentication(
"You don't have permission to set this status!" "You don't have permission to set this status!"

View File

@ -57,6 +57,17 @@ struct DiscordWebhook {
pub embeds: Vec<DiscordEmbed>, pub embeds: Vec<DiscordEmbed>,
} }
const PLUGIN_LOADERS: &[&str] = &[
"bukkit",
"spigot",
"paper",
"purpur",
"bungeecord",
"waterfall",
"velocity",
"sponge",
];
pub async fn send_discord_webhook( pub async fn send_discord_webhook(
project_id: ProjectId, project_id: ProjectId,
pool: &PgPool, pool: &PgPool,
@ -125,8 +136,8 @@ pub async fn send_discord_webhook(
if !loaders.is_empty() { if !loaders.is_empty() {
let mut formatted_loaders: String = String::new(); let mut formatted_loaders: String = String::new();
for loader in loaders { for loader in &loaders {
let emoji_id: i64 = match &*loader { let emoji_id: i64 = match &**loader {
"bukkit" => 1049793345481883689, "bukkit" => 1049793345481883689,
"bungeecord" => 1049793347067314220, "bungeecord" => 1049793347067314220,
"fabric" => 1049793348719890532, "fabric" => 1049793348719890532,
@ -170,6 +181,12 @@ pub async fn send_discord_webhook(
}); });
} }
let mut project_type = project.project_type;
if loaders.iter().all(|x| PLUGIN_LOADERS.contains(&&**x)) {
project_type = "plugin".to_string();
}
let embed = DiscordEmbed { let embed = DiscordEmbed {
author: Some(DiscordEmbedAuthor { author: Some(DiscordEmbedAuthor {
name: project.username.clone(), name: project.username.clone(),
@ -183,7 +200,7 @@ pub async fn send_discord_webhook(
url: format!( url: format!(
"{}/{}/{}", "{}/{}/{}",
dotenvy::var("SITE_URL").unwrap_or_default(), dotenvy::var("SITE_URL").unwrap_or_default(),
project.project_type, project_type,
project.slug.unwrap_or_else(|| project_id.to_string()) project.slug.unwrap_or_else(|| project_id.to_string())
), ),
title: project.title, title: project.title,
@ -203,7 +220,10 @@ pub async fn send_discord_webhook(
} }
.map(|x| DiscordEmbedImage { url: Some(x) }), .map(|x| DiscordEmbedImage { url: Some(x) }),
footer: Some(DiscordEmbedFooter { footer: Some(DiscordEmbedFooter {
text: "Modrinth".to_string(), text: format!(
"{project_type}{} on Modrinth",
project_type.remove(0).to_uppercase()
),
icon_url: Some( icon_url: Some(
"https://cdn-raw.modrinth.com/modrinth-new.png".to_string(), "https://cdn-raw.modrinth.com/modrinth-new.png".to_string(),
), ),