Cleans up and removes TODOs, adds tests (#844)

* removes version ordering from v2; simplifies now-unecessary three-level faceting

* resolved some todos

* test for game version updating

* merge fixes; display_categories fix
This commit is contained in:
Wyatt Verchere
2024-01-11 18:11:27 -08:00
committed by GitHub
parent 76c885f080
commit ef31c0c0da
24 changed files with 243 additions and 241 deletions

View File

@@ -41,15 +41,16 @@ impl MinecraftGameVersion {
redis: &RedisPool,
) -> Result<Vec<MinecraftGameVersion>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
E: sqlx::Acquire<'a, Database = sqlx::Postgres>,
{
let game_version_enum = LoaderFieldEnum::get(Self::FIELD_NAME, exec, redis)
let mut exec = exec.acquire().await?;
let game_version_enum = LoaderFieldEnum::get(Self::FIELD_NAME, &mut *exec, redis)
.await?
.ok_or_else(|| {
DatabaseError::SchemaError("Could not find game version enum.".to_string())
})?;
let game_version_enum_values =
LoaderFieldEnumValue::list(game_version_enum.id, exec, redis).await?;
LoaderFieldEnumValue::list(game_version_enum.id, &mut *exec, redis).await?;
let game_versions = game_version_enum_values
.into_iter()
@@ -71,24 +72,6 @@ impl MinecraftGameVersion {
Ok(game_versions)
}
// TODO: remove this
pub async fn list_transaction(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<Vec<MinecraftGameVersion>, DatabaseError> {
let game_version_enum = LoaderFieldEnum::get(Self::FIELD_NAME, &mut **transaction, redis)
.await?
.ok_or_else(|| {
DatabaseError::SchemaError("Could not find game version enum.".to_string())
})?;
let game_version_enum_values =
LoaderFieldEnumValue::list(game_version_enum.id, &mut **transaction, redis).await?;
Ok(game_version_enum_values
.into_iter()
.map(MinecraftGameVersion::from_enum_value)
.collect())
}
// Tries to create a MinecraftGameVersion from a VersionField
// Clones on success
pub fn try_from_version_field(

View File

@@ -101,7 +101,6 @@ impl LegacyProject {
// Requires any queried versions to be passed in, to get access to certain version fields contained within.
// - This can be any version, because the fields are ones that used to be on the project itself.
// - Its conceivable that certain V3 projects that have many different ones may not have the same fields on all of them.
// TODO: Should this return an error instead for v2 users?
// It's safe to use a db version_item for this as the only info is side types, game versions, and loader fields (for loaders), which used to be public on project anyway.
pub fn from(data: Project, versions_item: Option<version_item::QueryVersion>) -> Self {
let mut client_side = LegacySideType::Unknown;
@@ -289,10 +288,6 @@ pub struct LegacyVersion {
/// A list of loaders this project supports (has a newtype struct)
pub loaders: Vec<Loader>,
// TODO: should we remove this? as this is a v3 field and tests for it should be isolated to v3
// it allows us to keep tests that use this struct in common
pub ordering: Option<i32>,
pub id: VersionId,
pub project_id: ProjectId,
pub author_id: UserId,
@@ -354,7 +349,6 @@ impl From<Version> for LegacyVersion {
files: data.files,
dependencies: data.dependencies,
game_versions,
ordering: data.ordering,
loaders,
}
}

View File

@@ -42,7 +42,7 @@ pub struct LegacyResultSearchProject {
impl LegacyResultSearchProject {
pub fn from(result_search_project: ResultSearchProject) -> Self {
let mut categories = result_search_project.categories;
categories.extend(result_search_project.loaders);
categories.extend(result_search_project.loaders.clone());
if categories.contains(&"mrpack".to_string()) {
if let Some(mrpack_loaders) = result_search_project
.project_loader_fields
@@ -58,6 +58,7 @@ impl LegacyResultSearchProject {
}
}
let mut display_categories = result_search_project.display_categories;
display_categories.extend(result_search_project.loaders);
if display_categories.contains(&"mrpack".to_string()) {
if let Some(mrpack_loaders) = result_search_project
.project_loader_fields

View File

@@ -12,7 +12,6 @@ use crate::routes::v3::projects::ProjectIds;
use crate::routes::{v2_reroute, v3, ApiError};
use crate::search::{search_for_project, SearchConfig, SearchError};
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::collections::HashMap;
@@ -53,35 +52,16 @@ pub async fn project_search(
web::Query(info): web::Query<SearchRequest>,
config: web::Data<SearchConfig>,
) -> Result<HttpResponse, SearchError> {
// TODO: make this nicer
// Search now uses loader_fields instead of explicit 'client_side' and 'server_side' fields
// While the backend for this has changed, it doesnt affect much
// in the API calls except that 'versions:x' is now 'game_versions:x'
let facets: Option<Vec<Vec<Vec<String>>>> = if let Some(facets) = info.facets {
let facets = serde_json::from_str::<Vec<Vec<serde_json::Value>>>(&facets)?;
// Search can now *optionally* have a third inner array: So Vec(AND)<Vec(OR)<Vec(AND)< _ >>>
// For every inner facet, we will check if it can be deserialized into a Vec<&str>, and do so.
// If not, we will assume it is a single facet and wrap it in a Vec.
let facets: Vec<Vec<Vec<String>>> = facets
.into_iter()
.map(|facets| {
facets
.into_iter()
.map(|facet| {
if facet.is_array() {
serde_json::from_value::<Vec<String>>(facet).unwrap_or_default()
} else {
vec![serde_json::from_value::<String>(facet).unwrap_or_default()]
}
})
.collect_vec()
})
.collect_vec();
let facets: Option<Vec<Vec<String>>> = if let Some(facets) = info.facets {
let facets = serde_json::from_str::<Vec<Vec<String>>>(&facets)?;
// These loaders specifically used to be combined with 'mod' to be a plugin, but now
// they are their own loader type. We will convert 'mod' to 'mod' OR 'plugin'
// as it essentially was before.
let facets = v2_reroute::convert_plugin_loaders_v3(facets);
let facets = v2_reroute::convert_plugin_loader_facets_v3(facets);
Some(
facets
@@ -89,27 +69,22 @@ pub async fn project_search(
.map(|facet| {
facet
.into_iter()
.map(|facets| {
facets
.into_iter()
.map(|facet| {
if let Some((key, operator, val)) = parse_facet(&facet) {
format!(
"{}{}{}",
match key.as_str() {
"versions" => "game_versions",
"project_type" => "project_types",
"title" => "name",
x => x,
},
operator,
val
)
} else {
facet.to_string()
}
})
.collect::<Vec<_>>()
.map(|facet| {
if let Some((key, operator, val)) = parse_facet(&facet) {
format!(
"{}{}{}",
match key.as_str() {
"versions" => "game_versions",
"project_type" => "project_types",
"title" => "name",
x => x,
},
operator,
val
)
} else {
facet.to_string()
}
})
.collect::<Vec<_>>()
})

View File

@@ -215,7 +215,6 @@ pub struct EditVersion {
pub downloads: Option<u32>,
pub status: Option<VersionStatus>,
pub file_types: Option<Vec<EditVersionFileType>>,
pub ordering: Option<Option<i32>>, //TODO: How do you actually pass this in json?
}
#[derive(Serialize, Deserialize)]
@@ -291,7 +290,7 @@ pub async fn version_edit(
})
.collect::<Vec<_>>()
}),
ordering: new_version.ordering,
ordering: None,
fields,
};

View File

@@ -178,18 +178,18 @@ pub fn convert_side_types_v3(
fields
}
// Converts plugin loaders from v2 to v3
// Converts plugin loaders from v2 to v3, for search facets
// Within every 1st and 2nd level (the ones allowed in v2), we convert every instance of:
// "project_type:mod" to "project_type:plugin" OR "project_type:mod"
pub fn convert_plugin_loaders_v3(facets: Vec<Vec<Vec<String>>>) -> Vec<Vec<Vec<String>>> {
pub fn convert_plugin_loader_facets_v3(facets: Vec<Vec<String>>) -> Vec<Vec<String>> {
facets
.into_iter()
.map(|inner_facets| {
if inner_facets == [["project_type:mod"]] {
if inner_facets == ["project_type:mod"] {
vec![
vec!["project_type:plugin".to_string()],
vec!["project_type:datapack".to_string()],
vec!["project_type:mod".to_string()],
"project_type:plugin".to_string(),
"project_type:datapack".to_string(),
"project_type:mod".to_string(),
]
} else {
inner_facets

View File

@@ -583,8 +583,6 @@ async fn upload_file_to_version_inner(
};
let all_loaders = models::loader_fields::Loader::list(&mut **transaction, &redis).await?;
// TODO: this coded is reused a lot, it should be refactored into a function
let selected_loaders = version
.loaders
.iter()

View File

@@ -132,7 +132,6 @@ pub async fn get_update_from_hash(
.map(|x| x.1)
.ok();
let hash = info.into_inner().0.to_lowercase();
if let Some(file) = database::models::Version::get_file_from_hash(
hash_query
.algorithm
@@ -185,7 +184,6 @@ pub async fn get_update_from_hash(
}
}
}
Err(ApiError::NotFound)
}

View File

@@ -202,8 +202,12 @@ pub struct EditVersion {
pub downloads: Option<u32>,
pub status: Option<VersionStatus>,
pub file_types: Option<Vec<EditVersionFileType>>,
pub ordering: Option<Option<i32>>, //TODO: How do you actually pass this in json?
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "::serde_with::rust::double_option"
)]
pub ordering: Option<Option<i32>>,
// Flattened loader fields
// All other fields are loader-specific VersionFields
@@ -220,8 +224,6 @@ pub struct EditVersionFileType {
pub file_type: Option<FileType>,
}
// TODO: Avoid this 'helper' pattern here and similar fnunctoins- a macro might be the best bet here to ensure it's callable from both v2 and v3
// (web::Path can't be recreated naturally)
pub async fn version_edit(
req: HttpRequest,
info: web::Path<(VersionId,)>,

View File

@@ -131,8 +131,8 @@ pub struct UploadSearchProject {
pub loaders: Vec<String>, // Search uses loaders as categories- this is purely for the Project model.
pub links: Vec<LinkUrl>,
pub gallery_items: Vec<GalleryItem>, // Gallery *only* urls are stored in gallery, but the gallery items are stored here- required for the Project model.
pub games: Vec<String>, // Todo: in future, could be a searchable field.
pub organization_id: Option<String>, // Todo: in future, could be a searchable field.
pub games: Vec<String>,
pub organization_id: Option<String>,
pub project_loader_fields: HashMap<String, Vec<serde_json::Value>>, // Aggregation of loader_fields from all versions of the project, allowing for reconstruction of the Project model.
#[serde(flatten)]
@@ -183,8 +183,8 @@ pub struct ResultSearchProject {
pub loaders: Vec<String>, // Search uses loaders as categories- this is purely for the Project model.
pub links: Vec<LinkUrl>,
pub gallery_items: Vec<GalleryItem>, // Gallery *only* urls are stored in gallery, but the gallery items are stored here- required for the Project model.
pub games: Vec<String>, // Todo: in future, could be a searchable field.
pub organization_id: Option<String>, // Todo: in future, could be a searchable field.
pub games: Vec<String>,
pub organization_id: Option<String>,
pub project_loader_fields: HashMap<String, Vec<serde_json::Value>>, // Aggregation of loader_fields from all versions of the project, allowing for reconstruction of the Project model.
#[serde(flatten)]

View File

@@ -132,7 +132,7 @@ pub async fn validate_file(
.find_map(|v| MinecraftGameVersion::try_from_version_field(&v).ok())
.unwrap_or_default();
let all_game_versions =
MinecraftGameVersion::list_transaction(&mut *transaction, redis).await?;
MinecraftGameVersion::list(None, None, &mut *transaction, redis).await?;
validate_minecraft_file(
data,
file_extension,