Fix plugin validator, fix version urls, clippy lints, additional categories (#421)

This commit is contained in:
Geometrically
2022-08-16 17:42:04 -07:00
committed by GitHub
parent c76b527b93
commit ac3a17b178
17 changed files with 650 additions and 646 deletions

View File

@@ -74,10 +74,9 @@ impl Category {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if !name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(name.to_string()));
}
@@ -102,10 +101,9 @@ impl Category {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if !name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(name.to_string()));
}
@@ -203,10 +201,9 @@ impl<'a> CategoryBuilder<'a> {
self,
name: &'a str,
) -> Result<CategoryBuilder<'a>, DatabaseError> {
if name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self {
name: Some(name),
..self
@@ -296,10 +293,9 @@ impl Loader {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if !name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(name.to_string()));
}
@@ -403,10 +399,9 @@ impl<'a> LoaderBuilder<'a> {
self,
name: &'a str,
) -> Result<LoaderBuilder<'a>, DatabaseError> {
if name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self {
name: Some(name),
..self
@@ -501,10 +496,9 @@ impl GameVersion {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !version
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
{
if !version.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(version.to_string()));
}
@@ -676,7 +670,7 @@ impl<'a> GameVersionBuilder<'a> {
) -> Result<GameVersionBuilder<'a>, DatabaseError> {
if version
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
.all(|c| c.is_ascii_alphanumeric() || "-_.+".contains(c))
{
Ok(Self {
version: Some(version),
@@ -693,7 +687,7 @@ impl<'a> GameVersionBuilder<'a> {
) -> Result<GameVersionBuilder<'a>, DatabaseError> {
if version_type
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
.all(|c| c.is_ascii_alphanumeric() || "-_.+".contains(c))
{
Ok(Self {
version_type: Some(version_type),
@@ -851,10 +845,9 @@ impl<'a> LicenseBuilder<'a> {
self,
short: &'a str,
) -> Result<LicenseBuilder<'a>, DatabaseError> {
if short
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
{
if short.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self {
short: Some(short),
..self
@@ -1011,10 +1004,9 @@ impl<'a> DonationPlatformBuilder<'a> {
self,
short: &'a str,
) -> Result<DonationPlatformBuilder<'a>, DatabaseError> {
if short
.chars()
.all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c))
{
if short.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self {
short: Some(short),
..self
@@ -1075,10 +1067,9 @@ impl ReportType {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if !name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(name.to_string()));
}
@@ -1164,10 +1155,9 @@ impl<'a> ReportTypeBuilder<'a> {
self,
name: &'a str,
) -> Result<ReportTypeBuilder<'a>, DatabaseError> {
if name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self { name: Some(name) })
} else {
Err(DatabaseError::InvalidIdentifier(name.to_string()))
@@ -1213,10 +1203,9 @@ impl ProjectType {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if !name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
return Err(DatabaseError::InvalidIdentifier(name.to_string()));
}
@@ -1330,10 +1319,9 @@ impl<'a> ProjectTypeBuilder<'a> {
self,
name: &'a str,
) -> Result<ProjectTypeBuilder<'a>, DatabaseError> {
if name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
if name.chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '+'
}) {
Ok(Self { name: Some(name) })
} else {
Err(DatabaseError::InvalidIdentifier(name.to_string()))

View File

@@ -116,7 +116,7 @@ pub struct TeamId(pub i64);
#[sqlx(transparent)]
pub struct TeamMemberId(pub i64);
#[derive(Copy, Clone, Debug, Type, PartialEq)]
#[derive(Copy, Clone, Debug, Type, PartialEq, Eq)]
#[sqlx(transparent)]
pub struct ProjectId(pub i64);
#[derive(Copy, Clone, Debug, Type)]

View File

@@ -30,7 +30,7 @@ pub enum DatabaseError {
RandomId,
#[error(
"Invalid identifier: Category/version names must contain only ASCII \
alphanumeric characters or '_-'."
alphanumeric characters or '_-+'."
)]
InvalidIdentifier(String),
#[error("Invalid permissions bitflag!")]

View File

@@ -629,8 +629,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,
s.status status_name, cs.name client_side_type, ss.name server_side_type, l.short short, l.name license_name, pt.name project_type_name,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null) categories,
ARRAY_AGG(DISTINCT ca.category) filter (where ca.category is not null) additional_categories,
ARRAY_AGG(DISTINCT c.category || ' |||| ' || mc.is_additional) filter (where c.category is not null) categories,
ARRAY_AGG(DISTINCT v.id || ' |||| ' || v.date_published) filter (where v.id is not null) versions,
ARRAY_AGG(DISTINCT mg.image_url || ' |||| ' || mg.featured || ' |||| ' || mg.created || ' |||| ' || COALESCE(mg.title, ' ') || ' |||| ' || COALESCE(mg.description, ' ')) filter (where mg.image_url is not null) gallery,
ARRAY_AGG(DISTINCT md.joining_platform_id || ' |||| ' || dp.short || ' |||| ' || dp.name || ' |||| ' || md.url) filter (where md.joining_platform_id is not null) donations
@@ -643,8 +642,7 @@ impl Project {
LEFT JOIN mods_donations md ON md.joining_mod_id = m.id
LEFT JOIN donation_platforms dp ON md.joining_platform_id = dp.id
LEFT JOIN mods_categories mc ON mc.joining_mod_id = m.id
LEFT JOIN categories c ON mc.joining_category_id = c.id AND mc.is_additional = FALSE
LEFT JOIN categories ca ON mc.joining_category_id = c.id AND mc.is_additional = TRUE
LEFT JOIN categories c ON mc.joining_category_id = c.id
LEFT JOIN versions v ON v.mod_id = m.id
LEFT JOIN mods_gallery mg ON mg.mod_id = m.id
WHERE m.id = $1
@@ -656,6 +654,23 @@ impl Project {
.await?;
if let Some(m) = result {
let categories_raw = m.categories.unwrap_or_default();
let mut categories = Vec::new();
let mut additional_categories = Vec::new();
for category in categories_raw {
let category: Vec<&str> = category.split(" |||| ").collect();
if category.len() >= 2 {
if category[1].parse::<bool>().ok().unwrap_or_default() {
additional_categories.push(category[0].to_string());
} else {
categories.push(category[0].to_string());
}
}
}
Ok(Some(QueryProject {
inner: Project {
id: ProjectId(m.id),
@@ -685,10 +700,8 @@ impl Project {
approved: m.approved,
},
project_type: m.project_type_name,
categories: m.categories.unwrap_or_default(),
additional_categories: m
.additional_categories
.unwrap_or_default(),
categories,
additional_categories,
versions: {
let versions = m.versions.unwrap_or_default();
@@ -803,8 +816,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,
s.status status_name, cs.name client_side_type, ss.name server_side_type, l.short short, l.name license_name, pt.name project_type_name,
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null) categories,
ARRAY_AGG(DISTINCT ca.category) filter (where ca.category is not null) additional_categories,
ARRAY_AGG(DISTINCT c.category || ' |||| ' || mc.is_additional) filter (where c.category is not null) categories,
ARRAY_AGG(DISTINCT v.id || ' |||| ' || v.date_published) filter (where v.id is not null) versions,
ARRAY_AGG(DISTINCT mg.image_url || ' |||| ' || mg.featured || ' |||| ' || mg.created || ' |||| ' || COALESCE(mg.title, ' ') || ' |||| ' || COALESCE(mg.description, ' ')) filter (where mg.image_url is not null) gallery,
ARRAY_AGG(DISTINCT md.joining_platform_id || ' |||| ' || dp.short || ' |||| ' || dp.name || ' |||| ' || md.url) filter (where md.joining_platform_id is not null) donations
@@ -817,8 +829,7 @@ impl Project {
LEFT JOIN mods_donations md ON md.joining_mod_id = m.id
LEFT JOIN donation_platforms dp ON md.joining_platform_id = dp.id
LEFT JOIN mods_categories mc ON mc.joining_mod_id = m.id
LEFT JOIN categories c ON mc.joining_category_id = c.id AND mc.is_additional = FALSE
LEFT JOIN categories ca ON mc.joining_category_id = c.id AND mc.is_additional = TRUE
LEFT JOIN categories c ON mc.joining_category_id = c.id
LEFT JOIN versions v ON v.mod_id = m.id
LEFT JOIN mods_gallery mg ON mg.mod_id = m.id
WHERE m.id = ANY($1)
@@ -830,6 +841,25 @@ impl Project {
.try_filter_map(|e| async {
Ok(e.right().map(|m| {
let id = m.id;
let categories_raw = m.categories.unwrap_or_default();
let mut categories = Vec::new();
let mut additional_categories = Vec::new();
for category in categories_raw {
let category: Vec<&str> =
category.split(" |||| ").collect();
if category.len() >= 2 {
if category[1].parse::<bool>().ok().unwrap_or_default() {
additional_categories.push(category[0].to_string());
} else {
categories.push(category[0].to_string());
}
}
}
QueryProject {
inner: Project {
id: ProjectId(id),
@@ -859,8 +889,8 @@ impl Project {
approved: m.approved
},
project_type: m.project_type_name,
categories: m.categories.unwrap_or_default(),
additional_categories: m.additional_categories.unwrap_or_default(),
categories,
additional_categories,
versions: {
let versions = m.versions.unwrap_or_default();

View File

@@ -735,14 +735,14 @@ impl Version {
if dependency.len() >= 4 {
Some(QueryDependency {
project_id: match &*dependency[1] {
project_id: match dependency[1] {
"0" => None,
_ => match dependency[1].parse() {
Ok(x) => Some(ProjectId(x)),
Err(_) => None,
},
},
version_id: match &*dependency[0] {
version_id: match dependency[0] {
"0" => None,
_ => match dependency[0].parse() {
Ok(x) => Some(VersionId(x)),
@@ -896,14 +896,14 @@ impl Version {
if dependency.len() >= 4 {
Some(QueryDependency {
project_id: match &*dependency[1] {
project_id: match dependency[1] {
"0" => None,
_ => match dependency[1].parse() {
Ok(x) => Some(ProjectId(x)),
Err(_) => None,
},
},
version_id: match &*dependency[0] {
version_id: match dependency[0] {
"0" => None,
_ => match dependency[0].parse() {
Ok(x) => Some(VersionId(x)),