Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Fix download counting (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
enderger authored Feb 26, 2022
1 parent 3f671b9 commit b7c7c0e
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 129 deletions.
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
edition = "2018"
max_width = 80
65 changes: 33 additions & 32 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,6 @@
"nullable": []
}
},
"0483c9cf29bccba550dae1c602db928b83b77bca3007f0bba67f297797c8ceef": {
"query": "UPDATE mods\n SET downloads = downloads + 1\n WHERE (id = $1)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
}
},
"04dcb2565608e296502694efc0c59bc77c41175ef65c830f2fef745773f18c86": {
"query": "\n UPDATE mods\n SET moderation_message_body = NULL\n WHERE (id = $1)\n ",
"describe": {
Expand Down Expand Up @@ -863,6 +851,27 @@
"nullable": []
}
},
"331041c6a4f27f4a6ac2873332074c0127e7368c8ab803843760530d29aaef08": {
"query": "SELECT id FROM versions\n WHERE (version_number = $1 AND mod_id = $2)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Text",
"Int8"
]
},
"nullable": [
false
]
}
},
"33fc96ac71cfa382991cfb153e89da1e9f43ebf5367c28b30c336b758222307b": {
"query": "\n DELETE FROM loaders_versions\n WHERE loaders_versions.version_id = $1\n ",
"describe": {
Expand Down Expand Up @@ -1667,26 +1676,6 @@
"nullable": []
}
},
"59fd3b8da460fd1d81f3a1756fec609b05ce5d9eab035aa940d77753a341b599": {
"query": "SELECT mod_id FROM versions\n WHERE (id = $1)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "mod_id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
false
]
}
},
"5a03c653f1ff3339a01422ee4267a66157e6da9a51cc7d9beb0f87d59c3a444c": {
"query": "\n SELECT d.dependent_id, d.dependency_id, d.mod_dependency_id\n FROM versions v\n INNER JOIN dependencies d ON d.dependent_id = v.id\n WHERE v.mod_id = $1\n ",
"describe": {
Expand Down Expand Up @@ -3216,6 +3205,18 @@
"nullable": []
}
},
"9bb993b9b743c3e6d3c8a9e4753983239b056cb9d4ec26e5fc8c6d4a5122e512": {
"query": "UPDATE mods\n SET downloads = downloads + 1\n WHERE (id = $1)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
}
},
"9ceca63fb11f35f09f77bb9db175a1ac74dfcc2200c8134866922742fbbedea3": {
"query": "\n UPDATE dependencies\n SET dependency_id = $2\n WHERE dependency_id = $1\n ",
"describe": {
Expand Down
87 changes: 56 additions & 31 deletions src/routes/v1/versions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::file_hosting::FileHost;
use crate::models::ids::{ProjectId, UserId, VersionId};
use crate::models::projects::{Dependency, GameVersion, Loader, Version, VersionFile, VersionType};
use crate::models::projects::{
Dependency, GameVersion, Loader, Version, VersionFile, VersionType,
};
use crate::models::teams::Permissions;
use crate::routes::versions::{VersionIds, VersionListFilters};
use crate::routes::ApiError;
use crate::util::auth::get_user_from_headers;
use crate::{database, models, Pepper};
use crate::{database, models};
use actix_web::{delete, get, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -60,7 +62,9 @@ pub async fn version_list(
) -> Result<HttpResponse, ApiError> {
let string = info.into_inner().0;

let result = database::models::Project::get_from_slug_or_project_id(string, &**pool).await?;
let result =
database::models::Project::get_from_slug_or_project_id(string, &**pool)
.await?;

if let Some(project) = result {
let id = project.id;
Expand All @@ -79,7 +83,9 @@ pub async fn version_list(
)
.await?;

let mut versions = database::models::Version::get_many_full(version_ids, &**pool).await?;
let mut versions =
database::models::Version::get_many_full(version_ids, &**pool)
.await?;

let mut response = versions
.iter()
Expand All @@ -97,11 +103,19 @@ pub async fn version_list(
versions.sort_by(|a, b| b.date_published.cmp(&a.date_published));

// Attempt to populate versions with "auto featured" versions
if response.is_empty() && !versions.is_empty() && filters.featured.unwrap_or(false) {
let loaders = database::models::categories::Loader::list(&**pool).await?;
if response.is_empty()
&& !versions.is_empty()
&& filters.featured.unwrap_or(false)
{
let loaders =
database::models::categories::Loader::list(&**pool).await?;
let game_versions =
database::models::categories::GameVersion::list_filter(None, Some(true), &**pool)
.await?;
database::models::categories::GameVersion::list_filter(
None,
Some(true),
&**pool,
)
.await?;

let mut joined_filters = Vec::new();
for game_version in &game_versions {
Expand All @@ -117,14 +131,18 @@ pub async fn version_list(
version.game_versions.contains(&filter.0.version)
&& version.loaders.contains(&filter.1.loader)
})
.map(|version| response.push(convert_to_legacy(Version::from(version.clone()))))
.map(|version| {
response.push(convert_to_legacy(Version::from(
version.clone(),
)))
})
.unwrap_or(());
});

if response.is_empty() {
versions
.into_iter()
.for_each(|version| response.push(convert_to_legacy(Version::from(version))));
versions.into_iter().for_each(|version| {
response.push(convert_to_legacy(Version::from(version)))
});
}
}

Expand All @@ -142,11 +160,13 @@ pub async fn versions_get(
ids: web::Query<VersionIds>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
let version_ids = serde_json::from_str::<Vec<models::ids::VersionId>>(&*ids.ids)?
.into_iter()
.map(|x| x.into())
.collect();
let versions_data = database::models::Version::get_many_full(version_ids, &**pool).await?;
let version_ids =
serde_json::from_str::<Vec<models::ids::VersionId>>(&*ids.ids)?
.into_iter()
.map(|x| x.into())
.collect();
let versions_data =
database::models::Version::get_many_full(version_ids, &**pool).await?;

let mut versions = Vec::new();

Expand All @@ -163,7 +183,8 @@ pub async fn version_get(
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
let id = info.into_inner().0;
let version_data = database::models::Version::get_full(id.into(), &**pool).await?;
let version_data =
database::models::Version::get_full(id.into(), &**pool).await?;

if let Some(data) = version_data {
Ok(HttpResponse::Ok().json(convert_to_legacy(Version::from(data))))
Expand Down Expand Up @@ -211,7 +232,8 @@ pub async fn get_version_from_hash(
.await?;

if let Some(data) = version_data {
Ok(HttpResponse::Ok().json(crate::models::projects::Version::from(data)))
Ok(HttpResponse::Ok()
.json(crate::models::projects::Version::from(data)))
} else {
Ok(HttpResponse::NotFound().body(""))
}
Expand Down Expand Up @@ -287,25 +309,28 @@ pub async fn delete_file(

if let Some(row) = result {
if !user.role.is_mod() {
let team_member = database::models::TeamMember::get_from_user_id_version(
database::models::ids::VersionId(row.version_id),
user.id.into(),
&**pool,
)
.await
.map_err(ApiError::DatabaseError)?
.ok_or_else(|| {
ApiError::CustomAuthenticationError(
"You don't have permission to delete this file!".to_string(),
let team_member =
database::models::TeamMember::get_from_user_id_version(
database::models::ids::VersionId(row.version_id),
user.id.into(),
&**pool,
)
})?;
.await
.map_err(ApiError::DatabaseError)?
.ok_or_else(|| {
ApiError::CustomAuthenticationError(
"You don't have permission to delete this file!"
.to_string(),
)
})?;

if !team_member
.permissions
.contains(Permissions::DELETE_VERSION)
{
return Err(ApiError::CustomAuthenticationError(
"You don't have permission to delete this file!".to_string(),
"You don't have permission to delete this file!"
.to_string(),
));
}
}
Expand Down
Loading

0 comments on commit b7c7c0e

Please sign in to comment.