Skip to content

Some small Labrinth refactors and fixes #3698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
26 changes: 9 additions & 17 deletions apps/labrinth/src/models/v3/projects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::hash::RandomState;
use std::mem;

use crate::database::models::loader_fields::VersionField;
use crate::database::models::project_item::{LinkUrl, ProjectQueryResult};
Expand All @@ -8,6 +10,7 @@ use crate::models::ids::{
};
use ariadne::ids::UserId;
use chrono::{DateTime, Utc};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use validator::Validate;

Expand Down Expand Up @@ -95,19 +98,6 @@ pub struct Project {
pub fields: HashMap<String, Vec<serde_json::Value>>,
}

fn remove_duplicates(values: Vec<serde_json::Value>) -> Vec<serde_json::Value> {
let mut seen = HashSet::new();
values
.into_iter()
.filter(|value| {
// Convert the JSON value to a string for comparison
let as_string = value.to_string();
// Check if the string is already in the set
seen.insert(as_string)
})
.collect()
}

// This is a helper function to convert a list of VersionFields into a HashMap of field name to vecs of values
// This allows for removal of duplicates
pub fn from_duplicate_version_fields(
Expand All @@ -132,9 +122,11 @@ pub fn from_duplicate_version_fields(
}
}

// Remove duplicates by converting to string and back
// Remove duplicates
for (_, v) in fields.iter_mut() {
*v = remove_duplicates(v.clone());
*v = HashSet::<_, RandomState>::from_iter(mem::take(v).into_iter())
.into_iter()
.collect_vec();
}
fields
}
Expand Down Expand Up @@ -624,7 +616,7 @@ pub struct Version {
pub downloads: u32,
/// The type of the release - `Alpha`, `Beta`, or `Release`.
pub version_type: VersionType,
/// The status of tne version
/// The status of the version
pub status: VersionStatus,
/// The requested status of the version (used for scheduling)
pub requested_status: Option<VersionStatus>,
Expand Down Expand Up @@ -880,7 +872,7 @@ impl std::fmt::Display for DependencyType {
}

impl DependencyType {
// These are constant, so this can remove unneccessary allocations (`to_string`)
// These are constant, so this can remove unnecessary allocations (`to_string`)
pub fn as_str(&self) -> &'static str {
match self {
DependencyType::Required => "required",
Expand Down
17 changes: 12 additions & 5 deletions apps/labrinth/src/routes/v2_reroute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,18 @@ pub fn convert_side_types_v2_bools(
}

pub fn capitalize_first(input: &str) -> String {
let mut result = input.to_owned();
if let Some(first_char) = result.get_mut(0..1) {
first_char.make_ascii_uppercase();
}
result
let mut first_char = true;
input
.chars()
.map(|char| {
if first_char {
first_char = false;
char.to_ascii_uppercase()
} else {
char
}
})
.collect()
}

#[cfg(test)]
Expand Down
28 changes: 12 additions & 16 deletions apps/labrinth/src/routes/v3/version_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ pub async fn get_version_from_hash(
.map(|x| x.1)
.ok();
let hash = info.into_inner().0.to_lowercase();
let algorithm = hash_query
.algorithm
.clone()
.unwrap_or_else(|| default_algorithm_from_hashes(&[hash.clone()]));
let algorithm = hash_query.algorithm.clone().unwrap_or_else(|| {
default_algorithm_from_hashes(std::slice::from_ref(&hash))
});
let file = database::models::DBVersion::get_file_from_hash(
algorithm,
hash,
Expand Down Expand Up @@ -140,10 +139,9 @@ pub async fn get_update_from_hash(
.ok();
let hash = info.into_inner().0.to_lowercase();
if let Some(file) = database::models::DBVersion::get_file_from_hash(
hash_query
.algorithm
.clone()
.unwrap_or_else(|| default_algorithm_from_hashes(&[hash.clone()])),
hash_query.algorithm.clone().unwrap_or_else(|| {
default_algorithm_from_hashes(std::slice::from_ref(&hash))
}),
hash,
hash_query.version_id.map(|x| x.into()),
&**pool,
Expand Down Expand Up @@ -577,10 +575,9 @@ pub async fn delete_file(
.1;

let hash = info.into_inner().0.to_lowercase();
let algorithm = hash_query
.algorithm
.clone()
.unwrap_or_else(|| default_algorithm_from_hashes(&[hash.clone()]));
let algorithm = hash_query.algorithm.clone().unwrap_or_else(|| {
default_algorithm_from_hashes(std::slice::from_ref(&hash))
});
let file = database::models::DBVersion::get_file_from_hash(
algorithm.clone(),
hash,
Expand Down Expand Up @@ -709,10 +706,9 @@ pub async fn download_version(
.ok();

let hash = info.into_inner().0.to_lowercase();
let algorithm = hash_query
.algorithm
.clone()
.unwrap_or_else(|| default_algorithm_from_hashes(&[hash.clone()]));
let algorithm = hash_query.algorithm.clone().unwrap_or_else(|| {
default_algorithm_from_hashes(std::slice::from_ref(&hash))
});
let file = database::models::DBVersion::get_file_from_hash(
algorithm.clone(),
hash,
Expand Down
Loading