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

Commit

Permalink
Merge branch 'master' into links
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain authored Dec 1, 2023
2 parents 056becb + 756c14d commit 3fc59ab
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
5 changes: 5 additions & 0 deletions migrations/20231130153100_loader_fields_loaders.sql
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 4 additions & 7 deletions src/database/redis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::models::DatabaseError;
use deadpool_redis::{Config, Runtime};
use itertools::Itertools;
use redis::{cmd, Cmd};
use redis::{cmd, Cmd, FromRedisValue};
use std::fmt::Display;

const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
Expand Down Expand Up @@ -126,21 +126,18 @@ impl RedisConnection {
ids: impl IntoIterator<Item = impl Display>,
) -> Result<Vec<Option<R>>, DatabaseError>
where
R: for<'a> serde::Deserialize<'a>,
R: FromRedisValue,
{
let mut cmd = cmd("MGET");

let ids = ids.into_iter().map(|x| x.to_string()).collect_vec();
redis_args(
&mut cmd,
&ids.into_iter()
.map(|x| format!("{}_{}:{}", self.meta_namespace, namespace, x))
.collect_vec(),
);
let res: Vec<Option<String>> = redis_execute(&mut cmd, &mut self.connection).await?;
Ok(res
.into_iter()
.map(|x| x.and_then(|x| serde_json::from_str(&x).ok()))
.collect())
Ok(redis_execute(&mut cmd, &mut self.connection).await?)
}

pub async fn delete<T1>(&mut self, namespace: &str, id: T1) -> Result<(), DatabaseError>
Expand Down
22 changes: 18 additions & 4 deletions src/models/v2/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,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 {
Expand Down
27 changes: 22 additions & 5 deletions src/models/v2/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/files/dummy_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion tests/v2/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 3fc59ab

Please sign in to comment.