Add dependencies to search (#578)

* Add dependencies to search

* add attrs for faceting

* run prepare

* Add user data route from token

* update to 24hrs

* Fix report bugs
This commit is contained in:
Geometrically
2023-04-20 16:38:30 -07:00
committed by GitHub
parent 5c559af936
commit 59f24df294
65 changed files with 1518 additions and 2218 deletions

View File

@@ -59,8 +59,7 @@ where
{
let github_user = get_github_user_from_token(access_token).await?;
let res =
models::User::get_from_github_id(github_user.id, executor).await?;
let res = models::User::get_from_github_id(github_user.id, executor).await?;
match res {
Some(result) => Ok(User {
@@ -190,8 +189,7 @@ pub async fn filter_authorized_projects(
.try_for_each(|e| {
if let Some(row) = e.right() {
check_projects.retain(|x| {
let bool = x.inner.id.0 == row.id
&& x.inner.team_id.0 == row.team_id;
let bool = x.inner.id.0 == row.id && x.inner.team_id.0 == row.team_id;
if bool {
return_projects.push(x.clone().into());
@@ -274,25 +272,29 @@ pub async fn filter_authorized_versions(
INNER JOIN team_members tm ON tm.team_id = m.team_id AND user_id = $2
WHERE m.id = ANY($1)
",
&check_versions.iter().map(|x| x.inner.project_id.0).collect::<Vec<_>>(),
&check_versions
.iter()
.map(|x| x.inner.project_id.0)
.collect::<Vec<_>>(),
user_id as database::models::ids::UserId,
)
.fetch_many(&***pool)
.try_for_each(|e| {
if let Some(row) = e.right() {
check_versions.retain(|x| {
let bool = x.inner.project_id.0 == row.id;
.fetch_many(&***pool)
.try_for_each(|e| {
if let Some(row) = e.right() {
check_versions.retain(|x| {
let bool = x.inner.project_id.0 == row.id;
if bool {
return_versions.push(x.clone().into());
}
if bool {
return_versions.push(x.clone().into());
}
!bool
});
}
!bool
});
}
futures::future::ready(Ok(()))
}).await?;
futures::future::ready(Ok(()))
})
.await?;
}
}

View File

@@ -2,9 +2,8 @@ use actix_web::guard::GuardContext;
pub const ADMIN_KEY_HEADER: &str = "Modrinth-Admin";
pub fn admin_key_guard(ctx: &GuardContext) -> bool {
let admin_key = std::env::var("LABRINTH_ADMIN_KEY").expect(
"No admin key provided, this should have been caught by check_env_vars",
);
let admin_key = std::env::var("LABRINTH_ADMIN_KEY")
.expect("No admin key provided, this should have been caught by check_env_vars");
ctx.head()
.headers()

View File

@@ -6,15 +6,10 @@ pub fn get_color_from_img(data: &[u8]) -> Result<Option<u32>, ImageError> {
let image = image::load_from_memory(data)?
.resize(256, 256, FilterType::Nearest)
.crop_imm(128, 128, 64, 64);
let color = color_thief::get_palette(
image.to_rgb8().as_bytes(),
ColorFormat::Rgb,
10,
2,
)
.ok()
.and_then(|x| x.get(0).copied())
.map(|x| (x.r as u32) << 16 | (x.g as u32) << 8 | (x.b as u32));
let color = color_thief::get_palette(image.to_rgb8().as_bytes(), ColorFormat::Rgb, 10, 2)
.ok()
.and_then(|x| x.get(0).copied())
.map(|x| (x.r as u32) << 16 | (x.g as u32) << 8 | (x.b as u32));
Ok(color)
}

View File

@@ -18,9 +18,7 @@ pub async fn read_from_payload(
return Err(ApiError::InvalidInput(String::from(err_msg)));
} else {
bytes.extend_from_slice(&item.map_err(|_| {
ApiError::InvalidInput(
"Unable to parse bytes in payload sent!".to_string(),
)
ApiError::InvalidInput("Unable to parse bytes in payload sent!".to_string())
})?);
}
}
@@ -43,9 +41,7 @@ pub async fn read_from_field(
Ok(bytes)
}
pub(crate) fn ok_or_not_found<T, U>(
version_data: Option<T>,
) -> Result<HttpResponse, ApiError>
pub(crate) fn ok_or_not_found<T, U>(version_data: Option<T>) -> Result<HttpResponse, ApiError>
where
U: From<T> + Serialize,
{

View File

@@ -4,15 +4,11 @@ use regex::Regex;
use validator::{ValidationErrors, ValidationErrorsKind};
lazy_static! {
pub static ref RE_URL_SAFE: Regex =
Regex::new(r#"^[a-zA-Z0-9!@$()`.+,_"-]*$"#).unwrap();
pub static ref RE_URL_SAFE: Regex = Regex::new(r#"^[a-zA-Z0-9!@$()`.+,_"-]*$"#).unwrap();
}
//TODO: In order to ensure readability, only the first error is printed, this may need to be expanded on in the future!
pub fn validation_errors_to_string(
errors: ValidationErrors,
adder: Option<String>,
) -> String {
pub fn validation_errors_to_string(errors: ValidationErrors, adder: Option<String>) -> String {
let mut output = String::new();
let map = errors.into_errors();
@@ -23,10 +19,7 @@ pub fn validation_errors_to_string(
if let Some(error) = map.get(field) {
return match error {
ValidationErrorsKind::Struct(errors) => {
validation_errors_to_string(
*errors.clone(),
Some(format!("of item {field}")),
)
validation_errors_to_string(*errors.clone(), Some(format!("of item {field}")))
}
ValidationErrorsKind::List(list) => {
if let Some((index, errors)) = list.iter().next() {

View File

@@ -180,8 +180,7 @@ pub async fn send_discord_webhook(
}
if !versions.is_empty() {
let formatted_game_versions: String =
get_gv_range(versions, all_game_versions);
let formatted_game_versions: String = get_gv_range(versions, all_game_versions);
fields.push(DiscordEmbedField {
name: "Versions",
@@ -229,9 +228,7 @@ pub async fn send_discord_webhook(
thumbnail: DiscordEmbedThumbnail {
url: project.icon_url,
},
image: if let Some(first) =
project.featured_gallery.unwrap_or_default().first()
{
image: if let Some(first) = project.featured_gallery.unwrap_or_default().first() {
Some(first.clone())
} else {
project.gallery.unwrap_or_default().first().cloned()
@@ -242,9 +239,7 @@ pub async fn send_discord_webhook(
"{}{display_project_type} on Modrinth",
display_project_type.remove(0).to_uppercase()
),
icon_url: Some(
"https://cdn-raw.modrinth.com/modrinth-new.png".to_string(),
),
icon_url: Some("https://cdn-raw.modrinth.com/modrinth-new.png".to_string()),
}),
};
@@ -253,10 +248,7 @@ pub async fn send_discord_webhook(
client
.post(&webhook_url)
.json(&DiscordWebhook {
avatar_url: Some(
"https://cdn.modrinth.com/Modrinth_Dark_Logo.png"
.to_string(),
),
avatar_url: Some("https://cdn.modrinth.com/Modrinth_Dark_Logo.png".to_string()),
username: Some("Modrinth Release".to_string()),
embeds: vec![embed],
content: message,
@@ -264,9 +256,7 @@ pub async fn send_discord_webhook(
.send()
.await
.map_err(|_| {
ApiError::DiscordError(
"Error while sending projects webhook".to_string(),
)
ApiError::DiscordError("Error while sending projects webhook".to_string())
})?;
}
@@ -310,21 +300,15 @@ fn get_gv_range(
} else {
let interval_base = &intervals[current_interval];
if ((index as i32)
- (interval_base[interval_base.len() - 1][1] as i32)
== 1
|| (release_index as i32)
- (interval_base[interval_base.len() - 1][2] as i32)
== 1)
if ((index as i32) - (interval_base[interval_base.len() - 1][1] as i32) == 1
|| (release_index as i32) - (interval_base[interval_base.len() - 1][2] as i32) == 1)
&& (all_game_versions[interval_base[0][1]].type_ == "release"
|| all_game_versions[index].type_ != "release")
{
if intervals[current_interval].get(1).is_some() {
intervals[current_interval][1] =
vec![i, index, release_index];
intervals[current_interval][1] = vec![i, index, release_index];
} else {
intervals[current_interval]
.insert(1, vec![i, index, release_index]);
intervals[current_interval].insert(1, vec![i, index, release_index]);
}
} else {
current_interval += 1;
@@ -336,10 +320,7 @@ fn get_gv_range(
let mut new_intervals = Vec::new();
for interval in intervals {
if interval.len() == 2
&& interval[0][2] != MAX_VALUE
&& interval[1][2] == MAX_VALUE
{
if interval.len() == 2 && interval[0][2] != MAX_VALUE && interval[1][2] == MAX_VALUE {
let mut last_snapshot: Option<usize> = None;
for j in ((interval[0][1] + 1)..=interval[1][1]).rev() {
@@ -349,16 +330,12 @@ fn get_gv_range(
vec![
game_versions
.iter()
.position(|x| {
x.version == all_game_versions[j].version
})
.position(|x| x.version == all_game_versions[j].version)
.unwrap_or(MAX_VALUE),
j,
all_releases
.iter()
.position(|x| {
x.version == all_game_versions[j].version
})
.position(|x| x.version == all_game_versions[j].version)
.unwrap_or(MAX_VALUE),
],
]);
@@ -370,10 +347,7 @@ fn get_gv_range(
game_versions
.iter()
.position(|x| {
x.version
== all_game_versions
[last_snapshot]
.version
x.version == all_game_versions[last_snapshot].version
})
.unwrap_or(MAX_VALUE),
last_snapshot,
@@ -402,8 +376,7 @@ fn get_gv_range(
if interval.len() == 2 {
output.push(format!(
"{}{}",
&game_versions[interval[0][0]].version,
&game_versions[interval[1][0]].version
&game_versions[interval[0][0]].version, &game_versions[interval[1][0]].version
))
} else {
output.push(game_versions[interval[0][0]].version.clone())