You've already forked AstralRinth
forked from didirus/AstralRinth
More project data (#406)
* More project data * Array_agg fixes + cleanup * fix prepare * Add approval dates to search * Update migrations/20220725204351_more-project-data.sql Co-authored-by: wafflecoffee <emmaffle@modrinth.com> * Add category labels + display categories Co-authored-by: wafflecoffee <emmaffle@modrinth.com>
This commit is contained in:
@@ -6,26 +6,23 @@ use crate::database::models::ProjectId;
|
||||
use crate::search::UploadSearchProject;
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
// TODO: Move this away from STRING_AGG to multiple queries - however this may be more efficient?
|
||||
pub async fn index_local(
|
||||
pool: PgPool,
|
||||
) -> Result<Vec<UploadSearchProject>, IndexingError> {
|
||||
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,
|
||||
m.updated updated,
|
||||
m.icon_url icon_url, m.published published, m.approved approved, 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
|
||||
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null) categories, ARRAY_AGG(DISTINCT cp.category) filter (where cp.category is not null) primary_categories, ARRAY_AGG(DISTINCT lo.loader) filter (where lo.loader is not null) loaders, ARRAY_AGG(DISTINCT gv.version) filter (where gv.version is not null) versions,
|
||||
ARRAY_AGG(DISTINCT mg.image_url) filter (where mg.image_url is not null) 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 categories cp ON mc.joining_category_id = c.id AND mc.is_additional = FALSE
|
||||
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
|
||||
@@ -42,7 +39,6 @@ pub async fn index_local(
|
||||
WHERE s.status = $1 OR s.status = $2
|
||||
GROUP BY m.id, s.id, cs.id, ss.id, l.id, pt.id, u.id;
|
||||
",
|
||||
//endregion query
|
||||
crate::models::projects::ProjectStatus::Approved.as_str(),
|
||||
crate::models::projects::ProjectStatus::Archived.as_str(),
|
||||
crate::models::teams::OWNER_ROLE,
|
||||
@@ -50,15 +46,12 @@ pub async fn index_local(
|
||||
.fetch_many(&pool)
|
||||
.try_filter_map(|e| async {
|
||||
Ok(e.right().map(|m| {
|
||||
let mut categories = split_to_strings(m.categories);
|
||||
categories.append(&mut split_to_strings(m.loaders));
|
||||
let versions = split_to_strings(m.versions);
|
||||
let mut categories = m.categories.unwrap_or_default();
|
||||
categories.append(&mut m.loaders.unwrap_or_default());
|
||||
let versions = m.versions.unwrap_or_default();
|
||||
|
||||
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,
|
||||
@@ -69,9 +62,9 @@ pub async fn index_local(
|
||||
icon_url: m.icon_url.unwrap_or_default(),
|
||||
author: m.username,
|
||||
date_created: m.published,
|
||||
created_timestamp: m.published.unix_timestamp(),
|
||||
created_timestamp: m.approved.unwrap_or(m.published).timestamp(),
|
||||
date_modified: m.updated,
|
||||
modified_timestamp: m.updated.unix_timestamp(),
|
||||
modified_timestamp: m.updated.timestamp(),
|
||||
latest_version: versions.last().cloned().unwrap_or_else(|| "None".to_string()),
|
||||
versions,
|
||||
license: m.short,
|
||||
@@ -79,7 +72,8 @@ pub async fn index_local(
|
||||
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: m.gallery.unwrap_or_default(),
|
||||
display_categories: m.primary_categories.unwrap_or_default()
|
||||
}
|
||||
}))
|
||||
})
|
||||
@@ -87,8 +81,3 @@ pub async fn index_local(
|
||||
.await?
|
||||
)
|
||||
}
|
||||
|
||||
fn split_to_strings(s: Option<String>) -> Vec<String> {
|
||||
s.map(|x| x.split(',').map(ToString::to_string).collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ pub enum IndexingError {
|
||||
Indexing(#[from] meilisearch_sdk::errors::Error),
|
||||
#[error("Error while serializing or deserializing JSON: {0}")]
|
||||
Serde(#[from] serde_json::Error),
|
||||
#[error("Error while parsing a timestamp: {0}")]
|
||||
ParseDate(#[from] time::error::Error),
|
||||
#[error("Database Error: {0}")]
|
||||
Sqlx(#[from] sqlx::error::Error),
|
||||
#[error("Database Error: {0}")]
|
||||
|
||||
@@ -2,13 +2,13 @@ use crate::models::error::ApiError;
|
||||
use crate::models::projects::SearchRequest;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::HttpResponse;
|
||||
use chrono::{DateTime, Utc};
|
||||
use meilisearch_sdk::client::Client;
|
||||
use meilisearch_sdk::document::Document;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::min;
|
||||
use thiserror::Error;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
pub mod indexing;
|
||||
|
||||
@@ -74,6 +74,7 @@ pub struct UploadSearchProject {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub categories: Vec<String>,
|
||||
pub display_categories: Vec<String>,
|
||||
pub versions: Vec<String>,
|
||||
pub follows: i32,
|
||||
pub downloads: i32,
|
||||
@@ -83,16 +84,12 @@ pub struct UploadSearchProject {
|
||||
pub client_side: String,
|
||||
pub server_side: String,
|
||||
pub gallery: Vec<String>,
|
||||
|
||||
#[serde(with = "crate::util::time_ser")]
|
||||
/// RFC 3339 formatted creation date of the project
|
||||
pub date_created: OffsetDateTime,
|
||||
pub date_created: DateTime<Utc>,
|
||||
/// Unix timestamp of the creation date of the project
|
||||
pub created_timestamp: i64,
|
||||
|
||||
#[serde(with = "crate::util::time_ser")]
|
||||
/// RFC 3339 formatted date/time of last major modification (update)
|
||||
pub date_modified: OffsetDateTime,
|
||||
pub date_modified: DateTime<Utc>,
|
||||
/// Unix timestamp of the last major modification
|
||||
pub modified_timestamp: i64,
|
||||
}
|
||||
@@ -114,6 +111,7 @@ pub struct ResultSearchProject {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub categories: Vec<String>,
|
||||
pub display_categories: Vec<String>,
|
||||
// TODO: more efficient format for listing versions, without many repetitions
|
||||
pub versions: Vec<String>,
|
||||
pub downloads: i32,
|
||||
|
||||
Reference in New Issue
Block a user