You've already forked AstralRinth
forked from didirus/AstralRinth
Dependencies, fix panic on version get, Filtered Versions route (#153)
This commit is contained in:
@@ -10,7 +10,7 @@ pub struct VersionBuilder {
|
||||
pub version_number: String,
|
||||
pub changelog: String,
|
||||
pub files: Vec<VersionFileBuilder>,
|
||||
pub dependencies: Vec<VersionId>,
|
||||
pub dependencies: Vec<(VersionId, String)>,
|
||||
pub game_versions: Vec<GameVersionId>,
|
||||
pub loaders: Vec<LoaderId>,
|
||||
pub release_channel: ChannelId,
|
||||
@@ -107,11 +107,12 @@ impl VersionBuilder {
|
||||
for dependency in self.dependencies {
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO dependencies (dependent_id, dependency_id)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO dependencies (dependent_id, dependency_id, dependency_type)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
self.version_id as VersionId,
|
||||
dependency as VersionId,
|
||||
dependency.0 as VersionId,
|
||||
dependency.1,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -350,6 +351,8 @@ impl Version {
|
||||
|
||||
pub async fn get_mod_versions<'a, E>(
|
||||
mod_id: ModId,
|
||||
game_versions: Option<Vec<String>>,
|
||||
loaders: Option<Vec<String>>,
|
||||
exec: E,
|
||||
) -> Result<Vec<VersionId>, sqlx::Error>
|
||||
where
|
||||
@@ -359,11 +362,19 @@ impl Version {
|
||||
|
||||
let vec = sqlx::query!(
|
||||
"
|
||||
SELECT id FROM versions
|
||||
WHERE mod_id = $1
|
||||
ORDER BY date_published ASC
|
||||
SELECT version.id FROM (
|
||||
SELECT DISTINCT ON(v.id) v.id, v.date_published FROM versions v
|
||||
INNER JOIN game_versions_versions gvv ON gvv.joining_version_id = v.id
|
||||
INNER JOIN game_versions gv on gvv.game_version_id = gv.id AND (cardinality($2::varchar[]) = 0 OR gv.version = ANY($2::varchar[]))
|
||||
INNER JOIN loaders_versions lv ON lv.version_id = v.id
|
||||
INNER JOIN loaders l on lv.loader_id = l.id AND (cardinality($3::varchar[]) = 0 OR l.loader = ANY($3::varchar[]))
|
||||
WHERE v.mod_id = $1
|
||||
) AS version
|
||||
ORDER BY version.date_published ASC
|
||||
",
|
||||
mod_id as ModId,
|
||||
&game_versions.unwrap_or_default(),
|
||||
&loaders.unwrap_or_default(),
|
||||
)
|
||||
.fetch_many(exec)
|
||||
.try_filter_map(|e| async { Ok(e.right().map(|v| VersionId(v.id))) })
|
||||
@@ -468,7 +479,8 @@ impl Version {
|
||||
rc.channel release_channel, v.featured featured,
|
||||
STRING_AGG(DISTINCT gv.version, ',') game_versions, STRING_AGG(DISTINCT l.loader, ',') loaders,
|
||||
STRING_AGG(DISTINCT f.id || ', ' || f.filename || ', ' || f.is_primary || ', ' || f.url, ' ,') files,
|
||||
STRING_AGG(DISTINCT h.algorithm || ', ' || encode(h.hash, 'escape') || ', ' || h.file_id, ' ,') hashes
|
||||
STRING_AGG(DISTINCT h.algorithm || ', ' || encode(h.hash, 'escape') || ', ' || h.file_id, ' ,') hashes,
|
||||
STRING_AGG(DISTINCT d.dependency_id || ', ' || d.dependency_type, ' ,') dependencies
|
||||
FROM versions v
|
||||
INNER JOIN release_channels rc on v.release_channel = rc.id
|
||||
LEFT OUTER JOIN game_versions_versions gvv on v.id = gvv.joining_version_id
|
||||
@@ -477,6 +489,7 @@ impl Version {
|
||||
LEFT OUTER JOIN loaders l on lv.loader_id = l.id
|
||||
LEFT OUTER JOIN files f on v.id = f.version_id
|
||||
LEFT OUTER JOIN hashes h on f.id = h.file_id
|
||||
LEFT OUTER JOIN dependencies d on v.id = d.dependent_id
|
||||
WHERE v.id = $1
|
||||
GROUP BY v.id, rc.id;
|
||||
",
|
||||
@@ -490,13 +503,57 @@ impl Version {
|
||||
|
||||
v.hashes.unwrap_or_default().split(" ,").for_each(|f| {
|
||||
let hash: Vec<&str> = f.split(", ").collect();
|
||||
hashes.push((
|
||||
FileId(hash[2].parse().unwrap_or(0)),
|
||||
hash[0].to_string(),
|
||||
hash[1].to_string().into_bytes(),
|
||||
));
|
||||
|
||||
if hash.len() >= 3 {
|
||||
hashes.push((
|
||||
FileId(hash[2].parse().unwrap_or(0)),
|
||||
hash[0].to_string(),
|
||||
hash[1].to_string().into_bytes(),
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
let mut files = Vec::new();
|
||||
|
||||
v.files.unwrap_or_default().split(" ,").for_each(|f| {
|
||||
let file: Vec<&str> = f.split(", ").collect();
|
||||
|
||||
if file.len() >= 4 {
|
||||
let file_id = FileId(file[0].parse().unwrap_or(0));
|
||||
let mut file_hashes = HashMap::new();
|
||||
|
||||
for hash in &hashes {
|
||||
if (hash.0).0 == file_id.0 {
|
||||
file_hashes.insert(hash.1.clone(), hash.2.clone());
|
||||
}
|
||||
}
|
||||
|
||||
files.push(QueryFile {
|
||||
id: file_id,
|
||||
url: file[3].to_string(),
|
||||
filename: file[1].to_string(),
|
||||
hashes: file_hashes,
|
||||
primary: file[3].parse().unwrap_or(false),
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
let mut dependencies = Vec::new();
|
||||
|
||||
v.dependencies
|
||||
.unwrap_or_default()
|
||||
.split(" ,")
|
||||
.for_each(|f| {
|
||||
let dependency: Vec<&str> = f.split(", ").collect();
|
||||
|
||||
if dependency.len() >= 2 {
|
||||
dependencies.push((
|
||||
VersionId(dependency[0].parse().unwrap_or(0)),
|
||||
dependency[1].to_string(),
|
||||
))
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Some(QueryVersion {
|
||||
id: VersionId(v.id),
|
||||
mod_id: ModId(v.mod_id),
|
||||
@@ -508,30 +565,7 @@ impl Version {
|
||||
date_published: v.date_published,
|
||||
downloads: v.downloads,
|
||||
release_channel: v.release_channel,
|
||||
files: v
|
||||
.files
|
||||
.unwrap_or_default()
|
||||
.split(" ,")
|
||||
.map(|f| {
|
||||
let file: Vec<&str> = f.split(", ").collect();
|
||||
let file_id = FileId(file[0].parse().unwrap_or(0));
|
||||
let mut file_hashes = HashMap::new();
|
||||
|
||||
for hash in &hashes {
|
||||
if (hash.0).0 == file_id.0 {
|
||||
file_hashes.insert(hash.1.clone(), hash.2.clone());
|
||||
}
|
||||
}
|
||||
|
||||
QueryFile {
|
||||
id: file_id,
|
||||
url: file[3].to_string(),
|
||||
filename: file[1].to_string(),
|
||||
hashes: file_hashes,
|
||||
primary: file[3].parse().unwrap_or(false),
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
files,
|
||||
game_versions: v
|
||||
.game_versions
|
||||
.unwrap_or_default()
|
||||
@@ -545,6 +579,7 @@ impl Version {
|
||||
.map(|x| x.to_string())
|
||||
.collect(),
|
||||
featured: v.featured,
|
||||
dependencies,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
@@ -568,7 +603,8 @@ impl Version {
|
||||
rc.channel release_channel, v.featured featured,
|
||||
STRING_AGG(DISTINCT gv.version, ',') game_versions, STRING_AGG(DISTINCT l.loader, ',') loaders,
|
||||
STRING_AGG(DISTINCT f.id || ', ' || f.filename || ', ' || f.is_primary || ', ' || f.url, ' ,') files,
|
||||
STRING_AGG(DISTINCT h.algorithm || ', ' || encode(h.hash, 'escape') || ', ' || h.file_id, ' ,') hashes
|
||||
STRING_AGG(DISTINCT h.algorithm || ', ' || encode(h.hash, 'escape') || ', ' || h.file_id, ' ,') hashes,
|
||||
STRING_AGG(DISTINCT d.dependency_id || ', ' || d.dependency_type, ' ,') dependencies
|
||||
FROM versions v
|
||||
INNER JOIN release_channels rc on v.release_channel = rc.id
|
||||
LEFT OUTER JOIN game_versions_versions gvv on v.id = gvv.joining_version_id
|
||||
@@ -577,6 +613,7 @@ impl Version {
|
||||
LEFT OUTER JOIN loaders l on lv.loader_id = l.id
|
||||
LEFT OUTER JOIN files f on v.id = f.version_id
|
||||
LEFT OUTER JOIN hashes h on f.id = h.file_id
|
||||
LEFT OUTER JOIN dependencies d on v.id = d.dependent_id
|
||||
WHERE v.id IN (SELECT * FROM UNNEST($1::bigint[]))
|
||||
GROUP BY v.id, rc.id;
|
||||
",
|
||||
@@ -589,7 +626,45 @@ impl Version {
|
||||
|
||||
v.hashes.unwrap_or_default().split(" ,").for_each(|f| {
|
||||
let hash : Vec<&str> = f.split(", ").collect();
|
||||
hashes.push((FileId(hash[2].parse().unwrap_or(0)), hash[0].to_string(), hash[1].to_string().into_bytes()));
|
||||
|
||||
if hashes.len() >= 3 {
|
||||
hashes.push((FileId(hash[2].parse().unwrap_or(0)), hash[0].to_string(), hash[1].to_string().into_bytes()));
|
||||
}
|
||||
});
|
||||
|
||||
let mut files = Vec::new();
|
||||
|
||||
v.files.unwrap_or_default().split(" ,").for_each(|f| {
|
||||
let file : Vec<&str> = f.split(", ").collect();
|
||||
|
||||
if file.len() >= 4 {
|
||||
let file_id = FileId(file[0].parse().unwrap_or(0));
|
||||
let mut file_hashes = HashMap::new();
|
||||
|
||||
for hash in &hashes {
|
||||
if (hash.0).0 == file_id.0 {
|
||||
file_hashes.insert(hash.1.clone(), hash.2.clone());
|
||||
}
|
||||
}
|
||||
|
||||
files.push(QueryFile {
|
||||
id: file_id,
|
||||
url: file[3].to_string(),
|
||||
filename: file[1].to_string(),
|
||||
hashes: file_hashes,
|
||||
primary: file[3].parse().unwrap_or(false)
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
let mut dependencies = Vec::new();
|
||||
|
||||
v.dependencies.unwrap_or_default().split(" ,").for_each(|f| {
|
||||
let dependency: Vec<&str> = f.split(", ").collect();
|
||||
|
||||
if dependency.len() >= 2 {
|
||||
dependencies.push((VersionId(dependency[0].parse().unwrap_or(0)), dependency[1].to_string()))
|
||||
}
|
||||
});
|
||||
|
||||
QueryVersion {
|
||||
@@ -603,29 +678,11 @@ impl Version {
|
||||
date_published: v.date_published,
|
||||
downloads: v.downloads,
|
||||
release_channel: v.release_channel,
|
||||
files: v.files.unwrap_or_default()
|
||||
.split(" ,").map(|f| {
|
||||
let file : Vec<&str> = f.split(", ").collect();
|
||||
let file_id = FileId(file[0].parse().unwrap_or(0));
|
||||
let mut file_hashes = HashMap::new();
|
||||
|
||||
for hash in &hashes {
|
||||
if (hash.0).0 == file_id.0 {
|
||||
file_hashes.insert(hash.1.clone(), hash.2.clone());
|
||||
}
|
||||
}
|
||||
|
||||
QueryFile {
|
||||
id: file_id,
|
||||
url: file[3].to_string(),
|
||||
filename: file[1].to_string(),
|
||||
hashes: file_hashes,
|
||||
primary: file[3].parse().unwrap_or(false)
|
||||
}
|
||||
}).collect(),
|
||||
files,
|
||||
game_versions: v.game_versions.unwrap_or_default().split(",").map(|x| x.to_string()).collect(),
|
||||
loaders: v.loaders.unwrap_or_default().split(",").map(|x| x.to_string()).collect(),
|
||||
featured: v.featured,
|
||||
dependencies,
|
||||
}
|
||||
}))
|
||||
})
|
||||
@@ -669,6 +726,7 @@ pub struct QueryVersion {
|
||||
pub game_versions: Vec<String>,
|
||||
pub loaders: Vec<String>,
|
||||
pub featured: bool,
|
||||
pub dependencies: Vec<(VersionId, String)>,
|
||||
}
|
||||
|
||||
pub struct QueryFile {
|
||||
|
||||
@@ -208,7 +208,7 @@ pub struct Version {
|
||||
/// A list of files available for download for this version.
|
||||
pub files: Vec<VersionFile>,
|
||||
/// A list of mods that this version depends on.
|
||||
pub dependencies: Vec<VersionId>,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
/// A list of versions of Minecraft that this version of the mod supports.
|
||||
pub game_versions: Vec<GameVersion>,
|
||||
/// The loaders that this version works on
|
||||
@@ -229,6 +229,16 @@ pub struct VersionFile {
|
||||
pub primary: bool,
|
||||
}
|
||||
|
||||
/// A dependency which describes what versions are required, break support, or are optional to the
|
||||
/// version's functionality
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Dependency {
|
||||
/// The filename of the file.
|
||||
pub version_id: VersionId,
|
||||
/// Whether the file is the primary file of a version
|
||||
pub dependency_type: DependencyType,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum VersionType {
|
||||
@@ -258,6 +268,44 @@ impl VersionType {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum DependencyType {
|
||||
Required,
|
||||
Optional,
|
||||
Incompatible,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DependencyType {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
DependencyType::Required => write!(fmt, "required"),
|
||||
DependencyType::Optional => write!(fmt, "optional"),
|
||||
DependencyType::Incompatible => write!(fmt, "incompatible"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DependencyType {
|
||||
// These are constant, so this can remove unneccessary allocations (`to_string`)
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
DependencyType::Required => "required",
|
||||
DependencyType::Optional => "optional",
|
||||
DependencyType::Incompatible => "incompatible",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_str(string: &str) -> DependencyType {
|
||||
match string {
|
||||
"required" => DependencyType::Required,
|
||||
"optional" => DependencyType::Optional,
|
||||
"incompatible" => DependencyType::Incompatible,
|
||||
_ => DependencyType::Required,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A specific version of Minecraft
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(transparent)]
|
||||
|
||||
@@ -611,7 +611,7 @@ async fn create_initial_version(
|
||||
let dependencies = version_data
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|x| (*x).into())
|
||||
.map(|x| ((x.version_id).into(), x.dependency_type.to_string()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let version = models::version_item::VersionBuilder {
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::database::models;
|
||||
use crate::database::models::version_item::{VersionBuilder, VersionFileBuilder};
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models::mods::{
|
||||
GameVersion, ModId, ModLoader, Version, VersionFile, VersionId, VersionType,
|
||||
Dependency, GameVersion, ModId, ModLoader, Version, VersionFile, VersionId, VersionType,
|
||||
};
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::routes::mod_creation::{CreateError, UploadedFile};
|
||||
@@ -21,7 +21,7 @@ pub struct InitialVersionData {
|
||||
pub version_number: String,
|
||||
pub version_title: String,
|
||||
pub version_body: Option<String>,
|
||||
pub dependencies: Vec<VersionId>,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
pub game_versions: Vec<GameVersion>,
|
||||
pub release_channel: VersionType,
|
||||
pub loaders: Vec<ModLoader>,
|
||||
@@ -221,6 +221,12 @@ async fn version_create_inner(
|
||||
loaders.push(id);
|
||||
}
|
||||
|
||||
let dependencies = version_create_data
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|x| ((x.version_id).into(), x.dependency_type.to_string()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
version_builder = Some(VersionBuilder {
|
||||
version_id: version_id.into(),
|
||||
mod_id: version_create_data.mod_id.unwrap().into(),
|
||||
@@ -232,11 +238,7 @@ async fn version_create_inner(
|
||||
.clone()
|
||||
.unwrap_or_else(|| "".to_string()),
|
||||
files: Vec::new(),
|
||||
dependencies: version_create_data
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|x| (*x).into())
|
||||
.collect::<Vec<_>>(),
|
||||
dependencies,
|
||||
game_versions,
|
||||
loaders,
|
||||
release_channel,
|
||||
|
||||
@@ -2,6 +2,7 @@ use super::ApiError;
|
||||
use crate::auth::get_user_from_headers;
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models;
|
||||
use crate::models::mods::{Dependency, DependencyType};
|
||||
use crate::models::teams::Permissions;
|
||||
use crate::{database, Pepper};
|
||||
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
|
||||
@@ -10,14 +11,17 @@ use sqlx::PgPool;
|
||||
use std::borrow::Borrow;
|
||||
use std::sync::Arc;
|
||||
|
||||
// TODO: this needs filtering, and a better response type
|
||||
// Currently it only gives a list of ids, which have to be
|
||||
// requested manually. This route could give a list of the
|
||||
// ids as well as the supported versions and loaders, or
|
||||
// other info that is needed for selecting the right version.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct VersionListFilters {
|
||||
pub game_versions: Option<Vec<String>>,
|
||||
pub loaders: Option<Vec<String>>,
|
||||
pub featured: Option<bool>,
|
||||
}
|
||||
|
||||
#[get("version")]
|
||||
pub async fn version_list(
|
||||
info: web::Path<(models::ids::ModId,)>,
|
||||
web::Query(filters): web::Query<VersionListFilters>,
|
||||
pool: web::Data<PgPool>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let id = info.into_inner().0.into();
|
||||
@@ -32,14 +36,29 @@ pub async fn version_list(
|
||||
.exists;
|
||||
|
||||
if mod_exists.unwrap_or(false) {
|
||||
let mod_data = database::models::Version::get_mod_versions(id, &**pool)
|
||||
let version_ids = database::models::Version::get_mod_versions(
|
||||
id,
|
||||
filters.game_versions,
|
||||
filters.loaders,
|
||||
&**pool,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| ApiError::DatabaseError(e.into()))?;
|
||||
|
||||
let versions = database::models::Version::get_many_full(version_ids, &**pool)
|
||||
.await
|
||||
.map_err(|e| ApiError::DatabaseError(e.into()))?;
|
||||
|
||||
let response = mod_data
|
||||
.into_iter()
|
||||
.map(|v| v.into())
|
||||
.collect::<Vec<models::ids::VersionId>>();
|
||||
let mut response = Vec::new();
|
||||
for version in versions {
|
||||
if let Some(featured) = filters.featured {
|
||||
if featured {
|
||||
response.push(convert_version(version))
|
||||
}
|
||||
} else {
|
||||
response.push(convert_version(version))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(response))
|
||||
} else {
|
||||
@@ -132,7 +151,14 @@ fn convert_version(data: database::models::version_item::QueryVersion) -> models
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
dependencies: Vec::new(), // TODO: dependencies
|
||||
dependencies: data
|
||||
.dependencies
|
||||
.into_iter()
|
||||
.map(|d| Dependency {
|
||||
version_id: d.0.into(),
|
||||
dependency_type: DependencyType::from_str(&*d.1),
|
||||
})
|
||||
.collect(),
|
||||
game_versions: data
|
||||
.game_versions
|
||||
.into_iter()
|
||||
@@ -152,7 +178,7 @@ pub struct EditVersion {
|
||||
pub version_number: Option<String>,
|
||||
pub changelog: Option<String>,
|
||||
pub version_type: Option<models::mods::VersionType>,
|
||||
pub dependencies: Option<Vec<models::ids::VersionId>>,
|
||||
pub dependencies: Option<Vec<Dependency>>,
|
||||
pub game_versions: Option<Vec<models::mods::GameVersion>>,
|
||||
pub loaders: Option<Vec<models::mods::ModLoader>>,
|
||||
pub featured: Option<bool>,
|
||||
@@ -272,15 +298,17 @@ pub async fn version_edit(
|
||||
.map_err(|e| ApiError::DatabaseError(e.into()))?;
|
||||
|
||||
for dependency in dependencies {
|
||||
let dependency_id: database::models::ids::VersionId = dependency.clone().into();
|
||||
let dependency_id: database::models::ids::VersionId =
|
||||
dependency.version_id.clone().into();
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO dependencies (dependent_id, dependency_id)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO dependencies (dependent_id, dependency_id, dependency_type)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
id as database::models::ids::VersionId,
|
||||
dependency_id as database::models::ids::VersionId,
|
||||
dependency.dependency_type.as_str()
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user