Add loaders + game versions param to mods (#528)

This commit is contained in:
Geometrically
2023-01-27 19:24:40 -07:00
committed by GitHub
parent df3aeed291
commit 26533c47e7
14 changed files with 1114 additions and 880 deletions

2
.idea/sqldialects.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/migrations/20230104214503_random-projects.sql" dialect="PostgreSQL" />
<file url="PROJECT" dialect="PostgreSQL" />
</component>
</project>

View File

@@ -0,0 +1,21 @@
-- Add migration script here
ALTER TABLE mods ADD COLUMN loaders varchar(255)[] NOT NULL default array[]::varchar[];
ALTER TABLE mods ADD COLUMN game_versions varchar(255)[] NOT NULL default array[]::varchar[];
UPDATE mods
SET loaders = (
SELECT COALESCE(ARRAY_AGG(DISTINCT l.loader) filter (where l.loader is not null), array[]::varchar[])
FROM versions v
INNER JOIN loaders_versions lv ON lv.version_id = v.id
INNER JOIN loaders l on lv.loader_id = l.id
WHERE v.mod_id = mods.id
);
UPDATE mods
SET game_versions = (
SELECT COALESCE(ARRAY_AGG(DISTINCT gv.version) filter (where gv.version is not null), array[]::varchar[])
FROM versions v
INNER JOIN game_versions_versions gvv ON v.id = gvv.joining_version_id
INNER JOIN game_versions gv on gvv.game_version_id = gv.id
WHERE v.mod_id = mods.id
);

File diff suppressed because it is too large Load Diff

View File

@@ -139,6 +139,8 @@ impl ProjectBuilder {
flame_anvil_user: None,
webhook_sent: false,
color: self.color,
loaders: vec![],
game_versions: vec![],
};
project_struct.insert(&mut *transaction).await?;
@@ -181,6 +183,10 @@ impl ProjectBuilder {
.await?;
}
Project::update_game_versions(self.project_id, &mut *transaction)
.await?;
Project::update_loaders(self.project_id, &mut *transaction).await?;
Ok(self.project_id)
}
}
@@ -216,6 +222,8 @@ pub struct Project {
pub flame_anvil_user: Option<UserId>,
pub webhook_sent: bool,
pub color: Option<u32>,
pub loaders: Vec<String>,
pub game_versions: Vec<String>,
}
impl Project {
@@ -283,7 +291,7 @@ impl Project {
issues_url, source_url, wiki_url, discord_url, license_url,
team_id, client_side, server_side, license, slug,
moderation_message, moderation_message_body, flame_anvil_project,
flame_anvil_user, webhook_sent, color
flame_anvil_user, webhook_sent, color, loaders, game_versions
FROM mods
WHERE id = $1
",
@@ -326,6 +334,8 @@ impl Project {
flame_anvil_user: row.flame_anvil_user.map(UserId),
webhook_sent: row.webhook_sent,
color: row.color.map(|x| x as u32),
loaders: row.loaders,
game_versions: row.game_versions,
}))
} else {
Ok(None)
@@ -351,7 +361,7 @@ impl Project {
issues_url, source_url, wiki_url, discord_url, license_url,
team_id, client_side, server_side, license, slug,
moderation_message, moderation_message_body, flame_anvil_project,
flame_anvil_user, webhook_sent, color
flame_anvil_user, webhook_sent, color, loaders, game_versions
FROM mods
WHERE id = ANY($1)
",
@@ -394,6 +404,8 @@ impl Project {
flame_anvil_user: m.flame_anvil_user.map(UserId),
webhook_sent: m.webhook_sent,
color: m.color.map(|x| x as u32),
loaders: m.loaders,
game_versions: m.game_versions,
}))
})
.try_collect::<Vec<Project>>()
@@ -673,6 +685,7 @@ impl Project {
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
m.team_id team_id, m.client_side client_side, m.server_side server_side, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
cs.name client_side_type, ss.name server_side_type, pt.name project_type_name, m.flame_anvil_project flame_anvil_project, m.flame_anvil_user flame_anvil_user, m.webhook_sent webhook_sent, m.color,
m.loaders loaders, m.game_versions game_versions,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is false) categories,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is true) additional_categories,
JSONB_AGG(DISTINCT jsonb_build_object('id', v.id, 'date_published', v.date_published)) filter (where v.id is not null) versions,
@@ -732,6 +745,8 @@ impl Project {
flame_anvil_user: m.flame_anvil_user.map(UserId),
webhook_sent: m.webhook_sent,
color: m.color.map(|x| x as u32),
loaders: m.loaders,
game_versions: m.game_versions,
},
project_type: m.project_type_name,
categories: m.categories.unwrap_or_default(),
@@ -802,6 +817,7 @@ impl Project {
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
m.team_id team_id, m.client_side client_side, m.server_side server_side, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
cs.name client_side_type, ss.name server_side_type, pt.name project_type_name, m.flame_anvil_project flame_anvil_project, m.flame_anvil_user flame_anvil_user, m.webhook_sent, m.color,
m.loaders loaders, m.game_versions game_versions,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is false) categories,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is true) additional_categories,
JSONB_AGG(DISTINCT jsonb_build_object('id', v.id, 'date_published', v.date_published)) filter (where v.id is not null) versions,
@@ -864,6 +880,8 @@ impl Project {
flame_anvil_user: m.flame_anvil_user.map(UserId),
webhook_sent: m.webhook_sent,
color: m.color.map(|x| x as u32),
loaders: m.loaders,
game_versions: m.game_versions,
},
project_type: m.project_type_name,
categories: m.categories.unwrap_or_default(),
@@ -904,6 +922,56 @@ impl Project {
.try_collect::<Vec<QueryProject>>()
.await
}
pub async fn update_game_versions(
id: ProjectId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query!(
"
UPDATE mods
SET game_versions = (
SELECT COALESCE(ARRAY_AGG(DISTINCT gv.version) filter (where gv.version is not null), array[]::varchar[])
FROM versions v
INNER JOIN game_versions_versions gvv ON v.id = gvv.joining_version_id
INNER JOIN game_versions gv on gvv.game_version_id = gv.id
WHERE v.mod_id = mods.id AND v.status != ANY($2)
)
WHERE id = $1
",
id as ProjectId,
&*crate::models::projects::VersionStatus::iterator().filter(|x| x.is_hidden()).map(|x| x.to_string()).collect::<Vec<String>>()
)
.execute(&mut *transaction)
.await?;
Ok(())
}
pub async fn update_loaders(
id: ProjectId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query!(
"
UPDATE mods
SET loaders = (
SELECT COALESCE(ARRAY_AGG(DISTINCT l.loader) filter (where l.loader is not null), array[]::varchar[])
FROM versions v
INNER JOIN loaders_versions lv ON lv.version_id = v.id
INNER JOIN loaders l on lv.loader_id = l.id
WHERE v.mod_id = mods.id AND v.status != ANY($2)
)
WHERE id = $1
",
id as ProjectId,
&*crate::models::projects::VersionStatus::iterator().filter(|x| x.is_hidden()).map(|x| x.to_string()).collect::<Vec<String>>()
)
.execute(&mut *transaction)
.await?;
Ok(())
}
}
#[derive(Clone, Debug)]

View File

@@ -1,6 +1,6 @@
use super::ids::*;
use super::DatabaseError;
use crate::models::projects::{FileType, VersionStatus};
use crate::models::projects::{FileType, VersionStatus, VersionType};
use chrono::{DateTime, Utc};
use serde::Deserialize;
use std::cmp::Ordering;
@@ -462,6 +462,18 @@ impl Version {
)
.execute(&mut *transaction)
.await?;
crate::database::models::Project::update_game_versions(
ProjectId(project_id.mod_id),
&mut *transaction,
)
.await?;
crate::database::models::Project::update_loaders(
ProjectId(project_id.mod_id),
&mut *transaction,
)
.await?;
Ok(Some(()))
}
@@ -469,6 +481,9 @@ impl Version {
project_id: ProjectId,
game_versions: Option<Vec<String>>,
loaders: Option<Vec<String>>,
version_type: Option<VersionType>,
limit: Option<u32>,
offset: Option<u32>,
exec: E,
) -> Result<Vec<VersionId>, sqlx::Error>
where
@@ -483,12 +498,16 @@ impl Version {
INNER JOIN game_versions gv on gvv.game_version_id = gv.id AND (cardinality($2::varchar[]) = 0 OR gv.version = ANY($2::varchar[]))
INNER JOIN loaders_versions lv ON lv.version_id = v.id
INNER JOIN loaders l on lv.loader_id = l.id AND (cardinality($3::varchar[]) = 0 OR l.loader = ANY($3::varchar[]))
WHERE v.mod_id = $1
WHERE v.mod_id = $1 AND ($4 = NULL OR v.version_type = $4)
ORDER BY v.date_published, v.id ASC
LIMIT $5 OFFSET $6
",
project_id as ProjectId,
&game_versions.unwrap_or_default(),
&loaders.unwrap_or_default(),
version_type.map(|x| x.as_str()),
limit.map(|x| x as i64),
offset.map(|x| x as i64),
)
.fetch_many(exec)
.try_filter_map(|e| async { Ok(e.right().map(|v| VersionId(v.version_id))) })

View File

@@ -21,7 +21,7 @@ pub async fn connect() -> Result<PgPool, sqlx::Error> {
.and_then(|x| x.parse().ok())
.unwrap_or(16),
)
.max_lifetime(Some(Duration::from_secs(60 * 60 * 6)))
.max_lifetime(Some(Duration::from_secs(60 * 60)))
.connect(&database_url)
.await?;

View File

@@ -75,6 +75,11 @@ pub struct Project {
/// A list of the categories that the project is in.
pub additional_categories: Vec<String>,
/// A list of game versions this project supports
pub game_versions: Vec<String>,
/// A list of loaders this project supports
pub loaders: Vec<String>,
/// A list of ids for versions of the project.
pub versions: Vec<VersionId>,
/// The URL of the icon of the project
@@ -154,6 +159,8 @@ impl From<QueryProject> for Project {
followers: m.follows as u32,
categories: data.categories,
additional_categories: data.additional_categories,
game_versions: m.game_versions,
loaders: m.loaders,
versions: data.versions.into_iter().map(|v| v.into()).collect(),
icon_url: m.icon_url,
issues_url: m.issues_url,

View File

@@ -816,6 +816,8 @@ pub async fn project_create_inner(
followers: 0,
categories: project_create_data.categories,
additional_categories: project_create_data.additional_categories,
game_versions: vec![],
loaders: vec![],
versions: project_builder
.initial_versions
.iter()

View File

@@ -38,6 +38,9 @@ pub async fn forge_updates(
project.id,
None,
Some(vec!["forge".to_string()]),
None,
None,
None,
&**pool,
)
.await?;

View File

@@ -107,7 +107,7 @@ pub async fn projects_list(
crate::database::Project::get_many_full(&project_data, &**pool)
.await?
.into_iter()
.filter(|x| can_view_private || x.inner.status.is_approved())
.filter(|x| can_view_private || x.inner.status.is_searchable())
.map(Project::from)
.collect();

View File

@@ -76,6 +76,9 @@ pub async fn version_list(
.loaders
.as_ref()
.map(|x| serde_json::from_str(x).unwrap_or_default()),
filters.version_type,
filters.limit,
filters.offset,
&**pool,
)
.await?;

View File

@@ -422,6 +422,14 @@ async fn version_create_inner(
.insert_many(users, &mut *transaction)
.await?;
models::Project::update_game_versions(
builder.project_id,
&mut *transaction,
)
.await?;
models::Project::update_loaders(builder.project_id, &mut *transaction)
.await?;
let response = Version {
id: builder.version_id.into(),
project_id: builder.project_id.into(),

View File

@@ -301,6 +301,9 @@ pub async fn get_update_from_hash(
.map(|x| x.0)
.collect(),
),
None,
None,
None,
&**pool,
)
.await?;
@@ -493,6 +496,9 @@ pub async fn update_files(
.map(|x| x.0.clone())
.collect(),
),
None,
None,
None,
&**pool,
)
.await?;

View File

@@ -1,7 +1,9 @@
use super::ApiError;
use crate::database;
use crate::models;
use crate::models::projects::{Dependency, FileType, Version, VersionStatus};
use crate::models::projects::{
Dependency, FileType, Version, VersionStatus, VersionType,
};
use crate::models::teams::Permissions;
use crate::util::auth::{
get_user_from_headers, is_authorized, is_authorized_version,
@@ -19,6 +21,9 @@ pub struct VersionListFilters {
pub game_versions: Option<String>,
pub loaders: Option<String>,
pub featured: Option<bool>,
pub version_type: Option<VersionType>,
pub limit: Option<u32>,
pub offset: Option<u32>,
}
#[get("version")]
@@ -54,6 +59,9 @@ pub async fn version_list(
.loaders
.as_ref()
.map(|x| serde_json::from_str(x).unwrap_or_default()),
filters.version_type,
filters.limit,
filters.offset,
&**pool,
)
.await?;
@@ -393,6 +401,12 @@ pub async fn version_edit(
.execute(&mut *transaction)
.await?;
}
database::models::Project::update_game_versions(
version_item.inner.project_id,
&mut transaction,
)
.await?;
}
if let Some(loaders) = &new_version.loaders {
@@ -430,6 +444,12 @@ pub async fn version_edit(
.execute(&mut *transaction)
.await?;
}
database::models::Project::update_loaders(
version_item.inner.project_id,
&mut transaction,
)
.await?;
}
if let Some(featured) = &new_version.featured {