You've already forked AstralRinth
forked from didirus/AstralRinth
Search test + v3 (#731)
* search patch for accurate loader/gv filtering * backup * basic search test * finished test * incomplete commit; backing up * Working multipat reroute backup * working rough draft v3 * most tests passing * works * search v2 conversion * added some tags.rs v2 conversions * Worked through warnings, unwraps, prints * refactors * new search test * version files changes fixes * redesign to revs * removed old caches * removed games * fmt clippy * merge conflicts * fmt, prepare * moved v2 routes over to v3 * fixes; tests passing * project type changes * moved files over * fmt, clippy, prepare, etc * loaders to loader_fields, added tests * fmt, clippy, prepare * fixed sorting bug * reversed back- wrong order for consistency * fmt; clippy; prepare --------- Co-authored-by: Jai A <jaiagr+gpg@pm.me>
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
use crate::database::models::legacy_loader_fields::MinecraftGameVersion;
|
||||
use crate::database::models::loader_fields::VersionField;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::pack::PackFormat;
|
||||
use crate::models::projects::{FileType, GameVersion, Loader};
|
||||
use crate::models::projects::{FileType, Loader};
|
||||
use crate::validate::datapack::DataPackValidator;
|
||||
use crate::validate::fabric::FabricValidator;
|
||||
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
|
||||
@@ -36,6 +40,8 @@ pub enum ValidationError {
|
||||
InvalidInput(std::borrow::Cow<'static, str>),
|
||||
#[error("Error while managing threads")]
|
||||
Blocking(#[from] actix_web::error::BlockingError),
|
||||
#[error("Error while querying database")]
|
||||
Database(#[from] DatabaseError),
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
@@ -66,7 +72,7 @@ pub enum SupportedGameVersions {
|
||||
PastDate(DateTime<Utc>),
|
||||
Range(DateTime<Utc>, DateTime<Utc>),
|
||||
#[allow(dead_code)]
|
||||
Custom(Vec<GameVersion>),
|
||||
Custom(Vec<MinecraftGameVersion>),
|
||||
}
|
||||
|
||||
pub trait Validator: Sync {
|
||||
@@ -102,13 +108,53 @@ static VALIDATORS: &[&dyn Validator] = &[
|
||||
];
|
||||
|
||||
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn validate_file(
|
||||
data: bytes::Bytes,
|
||||
file_extension: String,
|
||||
loaders: Vec<Loader>,
|
||||
file_type: Option<FileType>,
|
||||
version_fields: Vec<VersionField>,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
redis: &RedisPool,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
// TODO: This needs to be revisited or removed with v3.
|
||||
// Currently, it checks if the loader is the modpack loader, and extracts the pack data from it.
|
||||
// This (and the funnction that calls this) should be refactored such that
|
||||
// - validators are removed (or altogether reworked)
|
||||
// - if a mrpack is uploaded, the pack data is extracted and usable to extract dependencies automatically
|
||||
|
||||
// TODO: A test needs to be written for this.
|
||||
match loaders {
|
||||
loaders if loaders == vec![Loader("mrpack".to_string())] => {
|
||||
let game_versions = version_fields
|
||||
.into_iter()
|
||||
.find_map(|v| MinecraftGameVersion::try_from_version_field(&v).ok())
|
||||
.unwrap_or_default();
|
||||
let all_game_versions =
|
||||
MinecraftGameVersion::list_transaction(&mut *transaction, redis).await?;
|
||||
validate_minecraft_file(
|
||||
data,
|
||||
file_extension,
|
||||
"modpack".to_string(),
|
||||
loaders,
|
||||
game_versions,
|
||||
all_game_versions,
|
||||
file_type,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => Ok(ValidationResult::Pass),
|
||||
}
|
||||
}
|
||||
|
||||
async fn validate_minecraft_file(
|
||||
data: bytes::Bytes,
|
||||
file_extension: String,
|
||||
mut project_type: String,
|
||||
mut loaders: Vec<Loader>,
|
||||
game_versions: Vec<GameVersion>,
|
||||
all_game_versions: Vec<crate::database::models::categories::GameVersion>,
|
||||
game_versions: Vec<MinecraftGameVersion>,
|
||||
all_game_versions: Vec<MinecraftGameVersion>,
|
||||
file_type: Option<FileType>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
actix_web::web::block(move || {
|
||||
@@ -162,9 +208,10 @@ pub async fn validate_file(
|
||||
.await?
|
||||
}
|
||||
|
||||
// Write tests for this
|
||||
fn game_version_supported(
|
||||
game_versions: &[GameVersion],
|
||||
all_game_versions: &[crate::database::models::categories::GameVersion],
|
||||
game_versions: &[MinecraftGameVersion],
|
||||
all_game_versions: &[MinecraftGameVersion],
|
||||
supported_game_versions: SupportedGameVersions,
|
||||
) -> bool {
|
||||
match supported_game_versions {
|
||||
@@ -172,19 +219,21 @@ fn game_version_supported(
|
||||
SupportedGameVersions::PastDate(date) => game_versions.iter().any(|x| {
|
||||
all_game_versions
|
||||
.iter()
|
||||
.find(|y| y.version == x.0)
|
||||
.find(|y| y.version == x.version)
|
||||
.map(|x| x.created > date)
|
||||
.unwrap_or(false)
|
||||
}),
|
||||
SupportedGameVersions::Range(before, after) => game_versions.iter().any(|x| {
|
||||
all_game_versions
|
||||
.iter()
|
||||
.find(|y| y.version == x.0)
|
||||
.find(|y| y.version == x.version)
|
||||
.map(|x| x.created > before && x.created < after)
|
||||
.unwrap_or(false)
|
||||
}),
|
||||
SupportedGameVersions::Custom(versions) => {
|
||||
versions.iter().any(|x| game_versions.contains(x))
|
||||
let version_ids = versions.iter().map(|gv| gv.id).collect::<Vec<_>>();
|
||||
let game_version_ids: Vec<_> = game_versions.iter().map(|gv| gv.id).collect::<Vec<_>>();
|
||||
version_ids.iter().any(|x| game_version_ids.contains(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user