From b3b55210f79000220df7c0401bbd9a47bfc7782d Mon Sep 17 00:00:00 2001 From: Wyatt Verchere Date: Thu, 30 Nov 2023 17:17:21 -0800 Subject: [PATCH] Renamed default project type to unknown. (#774) * small change * plugins and datapacks now correctly return to project * Adds to search --- .../20231130153100_loader_fields_loaders.sql | 5 ++++ src/models/v2/projects.rs | 22 ++++++++++++--- src/models/v2/search.rs | 27 +++++++++++++++---- tests/files/dummy_data.sql | 4 +-- tests/v2/version.rs | 3 ++- 5 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 migrations/20231130153100_loader_fields_loaders.sql diff --git a/migrations/20231130153100_loader_fields_loaders.sql b/migrations/20231130153100_loader_fields_loaders.sql new file mode 100644 index 00000000..2aea21dc --- /dev/null +++ b/migrations/20231130153100_loader_fields_loaders.sql @@ -0,0 +1,5 @@ +-- Adds missing loader_fields_loaders entries for mrpack loader +INSERT INTO loader_fields_loaders +SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf +WHERE l.loader='mrpack' AND lf.field=ANY(ARRAY['game_versions','client_and_server','server_only','client_only','singleplayer']) +ON CONFLICT DO NOTHING; \ No newline at end of file diff --git a/src/models/v2/projects.rs b/src/models/v2/projects.rs index af28b26b..c41be578 100644 --- a/src/models/v2/projects.rs +++ b/src/models/v2/projects.rs @@ -79,12 +79,26 @@ impl LegacyProject { let mut game_versions = Vec::new(); // V2 versions only have one project type- v3 versions can rarely have multiple. - // We'll just use the first one. - let mut project_type = data - .project_types + // We'll prioritize 'modpack' first, then 'mod', and if neither are found, use the first one. + // If there are no project types, default to 'project' + let mut project_types = data.project_types; + if project_types.contains(&"modpack".to_string()) { + project_types = vec!["modpack".to_string()]; + } else if project_types.contains(&"mod".to_string()) { + project_types = vec!["mod".to_string()]; + } + let project_type = project_types .first() .cloned() - .unwrap_or("unknown".to_string()); + .unwrap_or("project".to_string()); // Default to 'project' if none are found + + let mut project_type = if project_type == "datapack" || project_type == "plugin" { + // These are not supported in V2, so we'll just use 'mod' instead + "mod".to_string() + } else { + project_type + }; + let mut loaders = data.loaders; if let Some(versions_item) = versions_item { diff --git a/src/models/v2/search.rs b/src/models/v2/search.rs index 5405245d..3c922f68 100644 --- a/src/models/v2/search.rs +++ b/src/models/v2/search.rs @@ -63,12 +63,29 @@ impl LegacyResultSearchProject { display_categories.sort(); display_categories.dedup(); + // V2 versions only have one project type- v3 versions can rarely have multiple. + // We'll prioritize 'modpack' first, then 'mod', and if neither are found, use the first one. + // If there are no project types, default to 'project' + let mut project_types = result_search_project.project_types; + if project_types.contains(&"modpack".to_string()) { + project_types = vec!["modpack".to_string()]; + } else if project_types.contains(&"mod".to_string()) { + project_types = vec!["mod".to_string()]; + } + let project_type = project_types + .first() + .cloned() + .unwrap_or("project".to_string()); // Default to 'project' if none are found + + let project_type = if project_type == "datapack" || project_type == "plugin" { + // These are not supported in V2, so we'll just use 'mod' instead + "mod".to_string() + } else { + project_type + }; + Self { - project_type: result_search_project - .project_types - .first() - .cloned() - .unwrap_or_default(), + project_type, client_side: result_search_project .loader_fields .get("client_side") diff --git a/tests/files/dummy_data.sql b/tests/files/dummy_data.sql index 52577791..d322eb38 100644 --- a/tests/files/dummy_data.sql +++ b/tests/files/dummy_data.sql @@ -46,7 +46,7 @@ INSERT INTO loader_fields ( true ); INSERT INTO loader_fields_loaders(loader_id, loader_field_id) -SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf WHERE lf.field = 'test_fabric_optional' AND l.loader = 'fabric'; +SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf WHERE lf.field = 'test_fabric_optional' AND l.loader = 'fabric' ON CONFLICT DO NOTHING; -- Sample game versions, loaders, categories -- Game versions is '2' @@ -68,7 +68,7 @@ INSERT INTO loader_field_enum_values(enum_id, value, metadata, ordering) VALUES (2, 'Ordering_Positive100', '{"type":"release","major":false}', 100); INSERT INTO loader_fields_loaders(loader_id, loader_field_id) -SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf WHERE lf.field IN ('game_versions','singleplayer', 'client_and_server', 'client_only', 'server_only'); +SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf WHERE lf.field IN ('game_versions','singleplayer', 'client_and_server', 'client_only', 'server_only') ON CONFLICT DO NOTHING; INSERT INTO categories (id, category, project_type) VALUES (51, 'combat', 1), diff --git a/tests/v2/version.rs b/tests/v2/version.rs index 07e40021..75fc98bc 100644 --- a/tests/v2/version.rs +++ b/tests/v2/version.rs @@ -427,7 +427,8 @@ async fn add_version_project_types_v2() { let test_project = api .get_project_deserialized(&test_project.slug.unwrap(), USER_USER_PAT) .await; - assert_eq!(test_project.project_type, "unknown"); // No project_type set, as no versions are set + assert_eq!(test_project.project_type, "project"); // No project_type set, as no versions are set + // Default to 'project' if none are found // This is a known difference between older v2 ,but is acceptable. // This would be the appropriate test on older v2: // assert_eq!(test_project.project_type, "modpack");