Skip to content

Commit

Permalink
fix: only send webhook after successful mod update
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 6, 2025
1 parent e45c124 commit 4489377
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
83 changes: 48 additions & 35 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use serde::Deserialize;
use sqlx::{types::ipnetwork::IpNetwork, Acquire};

use crate::{
extractors::auth::Auth, types::{
extractors::auth::Auth,
types::{
api::{ApiError, ApiResponse},
mod_json::{split_version_and_compare, ModJson},
models::{
Expand All @@ -16,7 +17,9 @@ use crate::{
mod_version::{self, ModVersion},
mod_version_status::ModVersionStatusEnum,
},
}, webhook::send_webhook, AppData
},
webhook::send_webhook,
AppData,
};

#[derive(Deserialize)]
Expand Down Expand Up @@ -92,9 +95,8 @@ pub async fn get_version_index(
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;

let has_extended_permissions = match auth.developer() {
Ok(dev) => dev.admin ||
Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false
Ok(dev) => dev.admin || Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false,
};

let mut result = ModVersion::get_index(
Expand Down Expand Up @@ -130,9 +132,8 @@ pub async fn get_one(
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;

let has_extended_permissions = match auth.developer() {
Ok(dev) => dev.admin ||
Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false
Ok(dev) => dev.admin || Developer::has_access_to_mod(dev.id, &path.id, &mut pool).await?,
_ => false,
};

let mut version = {
Expand Down Expand Up @@ -202,16 +203,18 @@ pub async fn download_version(
};
let net: IpNetwork = ip.parse().or(Err(ApiError::InternalError))?;

if let Ok((downloaded_version, downloaded_mod)) = download::create_download(
net, mod_version.id, &mod_version.mod_id, &mut pool
).await {
if let Ok((downloaded_version, downloaded_mod)) =
download::create_download(net, mod_version.id, &mod_version.mod_id, &mut pool).await
{
let name = mod_version.mod_id.clone();
let version = mod_version.version.clone();

// only accepted mods can have their download counts incremented
// we'll just fix this once they're updated anyways

if (downloaded_version || downloaded_mod) && mod_version.status == ModVersionStatusEnum::Accepted {
if (downloaded_version || downloaded_mod)
&& mod_version.status == ModVersionStatusEnum::Accepted
{
tokio::spawn(async move {
if downloaded_version {
// we must nest more
Expand Down Expand Up @@ -264,45 +267,53 @@ pub async fn create_version(
}

// remove invalid characters from link - they break the location header on download
let download_link: String = payload.download_link.chars()
.filter(|c| c.is_ascii() && *c != '\0').collect();
let download_link: String = payload
.download_link
.chars()
.filter(|c| c.is_ascii() && *c != '\0')
.collect();

let mut file_path = download_geode_file(&download_link).await?;
let json = ModJson::from_zip(&mut file_path, &download_link, dev.verified)
.map_err(|err| {
log::error!("Failed to parse mod.json: {}", err);
ApiError::FilesystemError
})?;
let json = ModJson::from_zip(&mut file_path, &download_link, dev.verified).map_err(|err| {
log::error!("Failed to parse mod.json: {}", err);
ApiError::FilesystemError
})?;
if json.id != path.id {
return Err(ApiError::BadRequest(format!(
"Request id {} does not match mod.json id {}",
path.id, json.id
)));
}

json.validate()?;
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
if let Err(e) = Mod::new_version(&json, &dev, &mut transaction).await {
transaction
.rollback()
.await
.or(Err(ApiError::TransactionError))?;
return Err(e);
}

if dev.verified {
send_webhook(
json.id.clone(),
json.name.clone(),
json.version.clone(),
true,
Developer { id: dev.id, username: dev.username.clone(), display_name: dev.display_name.clone(), is_owner: true },
Developer {
id: dev.id,
username: dev.username.clone(),
display_name: dev.display_name.clone(),
is_owner: true,
},
dev.clone(),
data.webhook_url.clone(),
data.app_url.clone()
data.app_url.clone(),
)
.await;
}

json.validate()?;
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
if let Err(e) = Mod::new_version(&json, dev, &mut transaction).await {
transaction
.rollback()
.await
.or(Err(ApiError::TransactionError))?;
return Err(e);
}
transaction
.commit()
.await
Expand All @@ -327,8 +338,9 @@ pub async fn update_version(
path.version.as_str(),
false,
false,
&mut pool
).await?;
&mut pool,
)
.await?;
let approved_count = ModVersion::get_accepted_count(version.mod_id.as_str(), &mut pool).await?;
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
let id = match sqlx::query!(
Expand Down Expand Up @@ -368,7 +380,7 @@ pub async fn update_version(
.commit()
.await
.or(Err(ApiError::TransactionError))?;

if payload.status == ModVersionStatusEnum::Accepted {
let is_update = approved_count > 0;

Expand All @@ -385,8 +397,9 @@ pub async fn update_version(
owner.as_ref().unwrap().clone(),
dev.clone(),
data.webhook_url.clone(),
data.app_url.clone()
).await;
data.app_url.clone(),
)
.await;
}

Ok(HttpResponse::NoContent())
Expand Down
26 changes: 13 additions & 13 deletions src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Mod {
FROM (
select m.id, m.download_count, row_number() over(partition by m.id) rn
FROM mods m
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
WHERE mvs.status = 'accepted'
) q
Expand Down Expand Up @@ -616,7 +616,7 @@ impl Mod {
FROM mods m
INNER JOIN mod_versions mv ON m.id = mv.mod_id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
WHERE m.id = $1
WHERE m.id = $1
AND ($2 = false OR mvs.status = 'accepted')"#,
id,
only_accepted
Expand Down Expand Up @@ -737,7 +737,7 @@ impl Mod {

pub async fn new_version(
json: &ModJson,
developer: FetchedDeveloper,
developer: &FetchedDeveloper,
pool: &mut PgConnection,
) -> Result<(), ApiError> {
let result = sqlx::query!(
Expand Down Expand Up @@ -861,9 +861,9 @@ impl Mod {
}
match sqlx::query_as!(
QueryResult,
"SELECT m.image
"SELECT m.image
FROM mods m
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
WHERE m.id = $1",
id
Expand Down Expand Up @@ -981,7 +981,7 @@ impl Mod {
}

let counts = match sqlx::query!(
"select
"select
count(1) filter (where mvs.status = ANY(array['accepted', 'pending']::mod_version_status[])) as not_rejected,
count(1) filter (where mvs.status = 'rejected') as rejected,
count(1) filter (where mvs.status = 'accepted') as validated
Expand Down Expand Up @@ -1318,20 +1318,20 @@ impl Mod {
pool: &mut PgConnection,
) -> Result<Vec<ModUpdate>, ApiError> {
let mut builder: QueryBuilder<Postgres> = QueryBuilder::new(
r#"SELECT
q.id,
q.inner_version as version,
r#"SELECT
q.id,
q.inner_version as version,
q.mod_version_id
FROM (
SELECT m.id,
SELECT m.id,
mv.id as mod_version_id,
mv.version as inner_version,
row_number() over (partition by m.id order by mv.id desc) rn
row_number() over (partition by m.id order by mv.id desc) rn
FROM mods m
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_versions mv ON mv.mod_id = m.id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
INNER JOIN mod_gd_versions mgv ON mv.id = mgv.mod_id
WHERE mvs.status = 'accepted'
WHERE mvs.status = 'accepted'
AND mgv.platform = "#,
);
builder.push_bind(platforms);
Expand Down

0 comments on commit 4489377

Please sign in to comment.