You've already forked AstralRinth
forked from didirus/AstralRinth
Rustic cleanups, dedups and making the code less hard to read in general (#251)
* typos :help_me: * (part 1/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 2/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 3/?) cut down some pretty major heap allocations here - more Bytes and BytesMuts, less Vec<u8>s also I don't really understand why you need to `to_vec` when you don't really use it again afterwards * (part 4/?) deduplicate error handling in backblaze logic * (part 5/?) fixes, cleanups, refactors, and reformatting * (part 6/?) cleanups and refactors * remove loads of `as_str` in types that already are `Display` * Revert "remove loads of `as_str` in types that already are `Display`" This reverts commit 4f974310cfb167ceba03001d81388db4f0fbb509. * reformat and move routes util to the util module * use streams * Run prepare + formatting issues Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -3,7 +3,6 @@ use log::info;
|
||||
|
||||
use super::IndexingError;
|
||||
use crate::database::models::ProjectId;
|
||||
use crate::models::projects::ProjectStatus;
|
||||
use crate::search::UploadSearchProject;
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
@@ -12,6 +11,8 @@ pub async fn index_local(pool: PgPool) -> Result<Vec<UploadSearchProject>, Index
|
||||
info!("Indexing local projects!");
|
||||
Ok(
|
||||
sqlx::query!(
|
||||
//FIXME: there must be a way to reduce the duplicate lines between this query and the one in `query_one` here...
|
||||
//region query
|
||||
"
|
||||
SELECT m.id id, m.project_type project_type, m.title title, m.description description, m.downloads downloads, m.follows follows,
|
||||
m.icon_url icon_url, m.published published,
|
||||
@@ -39,19 +40,22 @@ pub async fn index_local(pool: PgPool) -> Result<Vec<UploadSearchProject>, Index
|
||||
WHERE s.status = $1
|
||||
GROUP BY m.id, s.id, cs.id, ss.id, l.id, pt.id, u.id;
|
||||
",
|
||||
ProjectStatus::Approved.as_str(),
|
||||
//endregion query
|
||||
crate::models::projects::ProjectStatus::Approved.as_str(),
|
||||
crate::models::teams::OWNER_ROLE,
|
||||
)
|
||||
.fetch_many(&pool)
|
||||
.try_filter_map(|e| async {
|
||||
Ok(e.right().map(|m| {
|
||||
let mut categories = m.categories.map(|x| x.split(',').map(|x| x.to_string()).collect::<Vec<String>>()).unwrap_or_default();
|
||||
categories.append(&mut m.loaders.map(|x| x.split(',').map(|x| x.to_string()).collect::<Vec<String>>()).unwrap_or_default());
|
||||
let mut categories = split_to_strings(m.categories);
|
||||
categories.append(&mut split_to_strings(m.loaders));
|
||||
let versions = split_to_strings(m.versions);
|
||||
|
||||
let versions : Vec<String> = m.versions.map(|x| x.split(',').map(|x| x.to_string()).collect()).unwrap_or_default();
|
||||
|
||||
let project_id : crate::models::projects::ProjectId = ProjectId(m.id).into();
|
||||
let project_id: crate::models::projects::ProjectId = ProjectId(m.id).into();
|
||||
|
||||
// TODO: Cleanup - This method has a lot of code in common with the method below.
|
||||
// But, since the macro returns an (de facto) unnamed struct,
|
||||
// We cannot reuse the code easily. Ugh.
|
||||
UploadSearchProject {
|
||||
project_id: format!("{}", project_id),
|
||||
title: m.title,
|
||||
@@ -76,64 +80,53 @@ pub async fn index_local(pool: PgPool) -> Result<Vec<UploadSearchProject>, Index
|
||||
}
|
||||
}))
|
||||
})
|
||||
.try_collect::<Vec<UploadSearchProject>>()
|
||||
.try_collect::<Vec<_>>()
|
||||
.await?
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn query_one(
|
||||
id: ProjectId,
|
||||
exec: &mut sqlx::PgConnection,
|
||||
) -> Result<UploadSearchProject, IndexingError> {
|
||||
let m = sqlx::query!(
|
||||
"
|
||||
SELECT m.id id, m.project_type project_type, m.title title, m.description description, m.downloads downloads, m.follows follows,
|
||||
m.icon_url icon_url, m.published published,
|
||||
m.updated updated,
|
||||
m.team_id team_id, m.license license, m.slug slug,
|
||||
s.status status_name, cs.name client_side_type, ss.name server_side_type, l.short short, pt.name project_type_name, u.username username,
|
||||
STRING_AGG(DISTINCT c.category, ',') categories, STRING_AGG(DISTINCT lo.loader, ',') loaders, STRING_AGG(DISTINCT gv.version, ',') versions,
|
||||
STRING_AGG(DISTINCT mg.image_url, ',') gallery
|
||||
FROM mods m
|
||||
LEFT OUTER JOIN mods_categories mc ON joining_mod_id = m.id
|
||||
LEFT OUTER JOIN categories c ON mc.joining_category_id = c.id
|
||||
LEFT OUTER JOIN versions v ON v.mod_id = m.id
|
||||
LEFT OUTER JOIN game_versions_versions gvv ON gvv.joining_version_id = v.id
|
||||
LEFT OUTER JOIN game_versions gv ON gvv.game_version_id = gv.id
|
||||
LEFT OUTER JOIN loaders_versions lv ON lv.version_id = v.id
|
||||
LEFT OUTER JOIN loaders lo ON lo.id = lv.loader_id
|
||||
LEFT OUTER JOIN mods_gallery mg ON mg.mod_id = m.id
|
||||
INNER JOIN statuses s ON s.id = m.status
|
||||
INNER JOIN project_types pt ON pt.id = m.project_type
|
||||
INNER JOIN side_types cs ON m.client_side = cs.id
|
||||
INNER JOIN side_types ss ON m.server_side = ss.id
|
||||
INNER JOIN licenses l ON m.license = l.id
|
||||
INNER JOIN team_members tm ON tm.team_id = m.team_id AND tm.role = $2
|
||||
INNER JOIN users u ON tm.user_id = u.id
|
||||
WHERE m.id = $1
|
||||
GROUP BY m.id, s.id, cs.id, ss.id, l.id, pt.id, u.id;
|
||||
",
|
||||
id as ProjectId,
|
||||
crate::models::teams::OWNER_ROLE,
|
||||
)
|
||||
//region query
|
||||
"
|
||||
SELECT m.id id, m.project_type project_type, m.title title, m.description description, m.downloads downloads, m.follows follows,
|
||||
m.icon_url icon_url, m.published published,
|
||||
m.updated updated,
|
||||
m.team_id team_id, m.license license, m.slug slug,
|
||||
s.status status_name, cs.name client_side_type, ss.name server_side_type, l.short short, pt.name project_type_name, u.username username,
|
||||
STRING_AGG(DISTINCT c.category, ',') categories, STRING_AGG(DISTINCT lo.loader, ',') loaders, STRING_AGG(DISTINCT gv.version, ',') versions,
|
||||
STRING_AGG(DISTINCT mg.image_url, ',') gallery
|
||||
FROM mods m
|
||||
LEFT OUTER JOIN mods_categories mc ON joining_mod_id = m.id
|
||||
LEFT OUTER JOIN categories c ON mc.joining_category_id = c.id
|
||||
LEFT OUTER JOIN versions v ON v.mod_id = m.id
|
||||
LEFT OUTER JOIN game_versions_versions gvv ON gvv.joining_version_id = v.id
|
||||
LEFT OUTER JOIN game_versions gv ON gvv.game_version_id = gv.id
|
||||
LEFT OUTER JOIN loaders_versions lv ON lv.version_id = v.id
|
||||
LEFT OUTER JOIN loaders lo ON lo.id = lv.loader_id
|
||||
LEFT OUTER JOIN mods_gallery mg ON mg.mod_id = m.id
|
||||
INNER JOIN statuses s ON s.id = m.status
|
||||
INNER JOIN project_types pt ON pt.id = m.project_type
|
||||
INNER JOIN side_types cs ON m.client_side = cs.id
|
||||
INNER JOIN side_types ss ON m.server_side = ss.id
|
||||
INNER JOIN licenses l ON m.license = l.id
|
||||
INNER JOIN team_members tm ON tm.team_id = m.team_id AND tm.role = $2
|
||||
INNER JOIN users u ON tm.user_id = u.id
|
||||
WHERE m.id = $1
|
||||
GROUP BY m.id, s.id, cs.id, ss.id, l.id, pt.id, u.id;
|
||||
",
|
||||
//endregion query
|
||||
id as ProjectId,
|
||||
crate::models::teams::OWNER_ROLE
|
||||
)
|
||||
.fetch_one(exec)
|
||||
.await?;
|
||||
|
||||
let mut categories = m
|
||||
.categories
|
||||
.map(|x| x.split(',').map(|x| x.to_string()).collect::<Vec<String>>())
|
||||
.unwrap_or_default();
|
||||
categories.append(
|
||||
&mut m
|
||||
.loaders
|
||||
.map(|x| x.split(',').map(|x| x.to_string()).collect::<Vec<String>>())
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
|
||||
let versions: Vec<String> = m
|
||||
.versions
|
||||
.map(|x| x.split(',').map(|x| x.to_string()).collect())
|
||||
.unwrap_or_default();
|
||||
let mut categories = split_to_strings(m.categories);
|
||||
categories.append(&mut split_to_strings(m.loaders));
|
||||
let versions = split_to_strings(m.versions);
|
||||
|
||||
let project_id: crate::models::projects::ProjectId = ProjectId(m.id).into();
|
||||
|
||||
@@ -160,9 +153,11 @@ pub async fn query_one(
|
||||
server_side: m.server_side_type,
|
||||
slug: m.slug,
|
||||
project_type: m.project_type_name,
|
||||
gallery: m
|
||||
.gallery
|
||||
.map(|x| x.split(',').map(|x| x.to_string()).collect())
|
||||
.unwrap_or_default(),
|
||||
gallery: split_to_strings(m.gallery),
|
||||
})
|
||||
}
|
||||
|
||||
fn split_to_strings(s: Option<String>) -> Vec<String> {
|
||||
s.map(|x| x.split(',').map(ToString::to_string).collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user