You've already forked AstralRinth
forked from didirus/AstralRinth
Links (#763)
This commit is contained in:
@@ -29,10 +29,10 @@ pub struct ReportType {
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DonationPlatform {
|
||||
pub id: DonationPlatformId,
|
||||
pub short: String,
|
||||
pub struct LinkPlatform {
|
||||
pub id: LinkPlatformId,
|
||||
pub name: String,
|
||||
pub donation: bool,
|
||||
}
|
||||
|
||||
impl Category {
|
||||
@@ -129,38 +129,32 @@ impl Category {
|
||||
}
|
||||
}
|
||||
|
||||
impl DonationPlatform {
|
||||
pub async fn get_id<'a, E>(
|
||||
id: &str,
|
||||
exec: E,
|
||||
) -> Result<Option<DonationPlatformId>, DatabaseError>
|
||||
impl LinkPlatform {
|
||||
pub async fn get_id<'a, E>(id: &str, exec: E) -> Result<Option<LinkPlatformId>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
SELECT id FROM donation_platforms
|
||||
WHERE short = $1
|
||||
SELECT id FROM link_platforms
|
||||
WHERE name = $1
|
||||
",
|
||||
id
|
||||
)
|
||||
.fetch_optional(exec)
|
||||
.await?;
|
||||
|
||||
Ok(result.map(|r| DonationPlatformId(r.id)))
|
||||
Ok(result.map(|r| LinkPlatformId(r.id)))
|
||||
}
|
||||
|
||||
pub async fn list<'a, E>(
|
||||
exec: E,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<DonationPlatform>, DatabaseError>
|
||||
pub async fn list<'a, E>(exec: E, redis: &RedisPool) -> Result<Vec<LinkPlatform>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
let mut redis = redis.connect().await?;
|
||||
|
||||
let res: Option<Vec<DonationPlatform>> = redis
|
||||
.get_deserialized_from_json(TAGS_NAMESPACE, "donation_platform")
|
||||
let res: Option<Vec<LinkPlatform>> = redis
|
||||
.get_deserialized_from_json(TAGS_NAMESPACE, "link_platform")
|
||||
.await?;
|
||||
|
||||
if let Some(res) = res {
|
||||
@@ -169,22 +163,22 @@ impl DonationPlatform {
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
SELECT id, short, name FROM donation_platforms
|
||||
SELECT id, name, donation FROM link_platforms
|
||||
"
|
||||
)
|
||||
.fetch_many(exec)
|
||||
.try_filter_map(|e| async {
|
||||
Ok(e.right().map(|c| DonationPlatform {
|
||||
id: DonationPlatformId(c.id),
|
||||
short: c.short,
|
||||
Ok(e.right().map(|c| LinkPlatform {
|
||||
id: LinkPlatformId(c.id),
|
||||
name: c.name,
|
||||
donation: c.donation,
|
||||
}))
|
||||
})
|
||||
.try_collect::<Vec<DonationPlatform>>()
|
||||
.try_collect::<Vec<LinkPlatform>>()
|
||||
.await?;
|
||||
|
||||
redis
|
||||
.set_serialized_to_json(TAGS_NAMESPACE, "donation_platform", &result, None)
|
||||
.set_serialized_to_json(TAGS_NAMESPACE, "link_platform", &result, None)
|
||||
.await?;
|
||||
|
||||
Ok(result)
|
||||
|
||||
@@ -223,9 +223,9 @@ pub struct SideTypeId(pub i32);
|
||||
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
|
||||
#[sqlx(transparent)]
|
||||
pub struct GameId(pub i32);
|
||||
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
|
||||
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
#[sqlx(transparent)]
|
||||
pub struct DonationPlatformId(pub i32);
|
||||
pub struct LinkPlatformId(pub i32);
|
||||
|
||||
#[derive(
|
||||
Copy, Clone, Debug, Type, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord,
|
||||
|
||||
@@ -15,26 +15,26 @@ pub const PROJECTS_SLUGS_NAMESPACE: &str = "projects_slugs";
|
||||
const PROJECTS_DEPENDENCIES_NAMESPACE: &str = "projects_dependencies";
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct DonationUrl {
|
||||
pub platform_id: DonationPlatformId,
|
||||
pub platform_short: String,
|
||||
pub struct LinkUrl {
|
||||
pub platform_id: LinkPlatformId,
|
||||
pub platform_name: String,
|
||||
pub url: String,
|
||||
pub donation: bool, // Is this a donation link
|
||||
}
|
||||
|
||||
impl DonationUrl {
|
||||
impl LinkUrl {
|
||||
pub async fn insert_many_projects(
|
||||
donation_urls: Vec<Self>,
|
||||
links: Vec<Self>,
|
||||
project_id: ProjectId,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), sqlx::error::Error> {
|
||||
let (project_ids, platform_ids, urls): (Vec<_>, Vec<_>, Vec<_>) = donation_urls
|
||||
let (project_ids, platform_ids, urls): (Vec<_>, Vec<_>, Vec<_>) = links
|
||||
.into_iter()
|
||||
.map(|url| (project_id.0, url.platform_id.0, url.url))
|
||||
.multiunzip();
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO mods_donations (
|
||||
INSERT INTO mods_links (
|
||||
joining_mod_id, joining_platform_id, url
|
||||
)
|
||||
SELECT * FROM UNNEST($1::bigint[], $2::int[], $3::varchar[])
|
||||
@@ -148,11 +148,7 @@ pub struct ProjectBuilder {
|
||||
pub description: String,
|
||||
pub body: String,
|
||||
pub icon_url: Option<String>,
|
||||
pub issues_url: Option<String>,
|
||||
pub source_url: Option<String>,
|
||||
pub wiki_url: Option<String>,
|
||||
pub license_url: Option<String>,
|
||||
pub discord_url: Option<String>,
|
||||
pub categories: Vec<CategoryId>,
|
||||
pub additional_categories: Vec<CategoryId>,
|
||||
pub initial_versions: Vec<super::version_item::VersionBuilder>,
|
||||
@@ -160,7 +156,7 @@ pub struct ProjectBuilder {
|
||||
pub requested_status: Option<ProjectStatus>,
|
||||
pub license: String,
|
||||
pub slug: Option<String>,
|
||||
pub donation_urls: Vec<DonationUrl>,
|
||||
pub link_urls: Vec<LinkUrl>,
|
||||
pub gallery_items: Vec<GalleryItem>,
|
||||
pub color: Option<u32>,
|
||||
pub monetization_status: MonetizationStatus,
|
||||
@@ -192,11 +188,7 @@ impl ProjectBuilder {
|
||||
downloads: 0,
|
||||
follows: 0,
|
||||
icon_url: self.icon_url,
|
||||
issues_url: self.issues_url,
|
||||
source_url: self.source_url,
|
||||
wiki_url: self.wiki_url,
|
||||
license_url: self.license_url,
|
||||
discord_url: self.discord_url,
|
||||
license: self.license,
|
||||
slug: self.slug,
|
||||
moderation_message: None,
|
||||
@@ -209,7 +201,7 @@ impl ProjectBuilder {
|
||||
project_struct.insert(&mut *transaction).await?;
|
||||
|
||||
let ProjectBuilder {
|
||||
donation_urls,
|
||||
link_urls,
|
||||
gallery_items,
|
||||
categories,
|
||||
additional_categories,
|
||||
@@ -221,8 +213,7 @@ impl ProjectBuilder {
|
||||
version.insert(&mut *transaction).await?;
|
||||
}
|
||||
|
||||
DonationUrl::insert_many_projects(donation_urls, self.project_id, &mut *transaction)
|
||||
.await?;
|
||||
LinkUrl::insert_many_projects(link_urls, self.project_id, &mut *transaction).await?;
|
||||
|
||||
GalleryItem::insert_many(gallery_items, self.project_id, &mut *transaction).await?;
|
||||
|
||||
@@ -259,11 +250,7 @@ pub struct Project {
|
||||
pub downloads: i32,
|
||||
pub follows: i32,
|
||||
pub icon_url: Option<String>,
|
||||
pub issues_url: Option<String>,
|
||||
pub source_url: Option<String>,
|
||||
pub wiki_url: Option<String>,
|
||||
pub license_url: Option<String>,
|
||||
pub discord_url: Option<String>,
|
||||
pub license: String,
|
||||
pub slug: Option<String>,
|
||||
pub moderation_message: Option<String>,
|
||||
@@ -283,17 +270,15 @@ impl Project {
|
||||
"
|
||||
INSERT INTO mods (
|
||||
id, team_id, title, description, body,
|
||||
published, downloads, icon_url, issues_url,
|
||||
source_url, wiki_url, status, requested_status, discord_url,
|
||||
published, downloads, icon_url, status, requested_status,
|
||||
license_url, license,
|
||||
slug, color, monetization_status
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, $9,
|
||||
$10, $11, $12, $13, $14,
|
||||
$15, $16,
|
||||
LOWER($17), $18, $19
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10,
|
||||
$11, $12,
|
||||
LOWER($13), $14, $15
|
||||
)
|
||||
",
|
||||
self.id as ProjectId,
|
||||
@@ -304,12 +289,8 @@ impl Project {
|
||||
self.published,
|
||||
self.downloads,
|
||||
self.icon_url.as_ref(),
|
||||
self.issues_url.as_ref(),
|
||||
self.source_url.as_ref(),
|
||||
self.wiki_url.as_ref(),
|
||||
self.status.as_str(),
|
||||
self.requested_status.map(|x| x.as_str()),
|
||||
self.discord_url.as_ref(),
|
||||
self.license_url.as_ref(),
|
||||
&self.license,
|
||||
self.slug.as_ref(),
|
||||
@@ -384,7 +365,7 @@ impl Project {
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
DELETE FROM mods_donations
|
||||
DELETE FROM mods_links
|
||||
WHERE joining_mod_id = $1
|
||||
",
|
||||
id as ProjectId,
|
||||
@@ -650,23 +631,23 @@ impl Project {
|
||||
WHERE m.id = ANY($1) OR m.slug = ANY($2)
|
||||
GROUP BY mod_id
|
||||
),
|
||||
donations_json AS (
|
||||
links_json AS (
|
||||
SELECT DISTINCT joining_mod_id as mod_id,
|
||||
JSONB_AGG(
|
||||
DISTINCT jsonb_build_object(
|
||||
'platform_id', md.joining_platform_id, 'platform_short', dp.short, 'platform_name', dp.name,'url', md.url
|
||||
'platform_id', ml.joining_platform_id, 'platform_name', lp.name,'url', ml.url, 'donation', lp.donation
|
||||
)
|
||||
) filter (where md.joining_platform_id is not null) donations_json
|
||||
FROM mods_donations md
|
||||
INNER JOIN mods m ON md.joining_mod_id = m.id AND m.id = ANY($1) OR m.slug = ANY($2)
|
||||
INNER JOIN donation_platforms dp ON md.joining_platform_id = dp.id
|
||||
) filter (where ml.joining_platform_id is not null) links_json
|
||||
FROM mods_links ml
|
||||
INNER JOIN mods m ON ml.joining_mod_id = m.id AND m.id = ANY($1) OR m.slug = ANY($2)
|
||||
INNER JOIN link_platforms lp ON ml.joining_platform_id = lp.id
|
||||
GROUP BY mod_id
|
||||
)
|
||||
|
||||
SELECT m.id id, m.title title, m.description description, m.downloads downloads, m.follows follows,
|
||||
m.icon_url icon_url, m.body body, m.published published,
|
||||
m.updated updated, m.approved approved, m.queued, m.status status, m.requested_status requested_status,
|
||||
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.license_url license_url,
|
||||
m.team_id team_id, m.organization_id organization_id, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
|
||||
m.webhook_sent, m.color,
|
||||
t.id thread_id, m.monetization_status monetization_status,
|
||||
@@ -677,14 +658,14 @@ impl Project {
|
||||
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is true) additional_categories,
|
||||
v.versions_json versions,
|
||||
mg.mods_gallery_json gallery,
|
||||
md.donations_json donations,
|
||||
ml.links_json links,
|
||||
vf.version_fields_json version_fields,
|
||||
lf.loader_fields_json loader_fields,
|
||||
lfev.loader_field_enum_values_json loader_field_enum_values
|
||||
FROM mods m
|
||||
INNER JOIN threads t ON t.mod_id = m.id
|
||||
LEFT JOIN mods_gallery_json mg ON mg.mod_id = m.id
|
||||
LEFT JOIN donations_json md ON md.mod_id = m.id
|
||||
LEFT JOIN links_json ml ON ml.mod_id = m.id
|
||||
LEFT JOIN mods_categories mc ON mc.joining_mod_id = m.id
|
||||
LEFT JOIN categories c ON mc.joining_category_id = c.id
|
||||
LEFT JOIN versions_json v ON v.mod_id = m.id
|
||||
@@ -697,7 +678,7 @@ impl Project {
|
||||
LEFT OUTER JOIN loader_fields_json lf ON m.id = lf.mod_id
|
||||
LEFT OUTER JOIN loader_field_enum_values_json lfev ON m.id = lfev.mod_id
|
||||
WHERE m.id = ANY($1) OR m.slug = ANY($2)
|
||||
GROUP BY t.id, m.id, version_fields_json, loader_fields_json, loader_field_enum_values_json, versions_json, mods_gallery_json, donations_json;
|
||||
GROUP BY t.id, m.id, version_fields_json, loader_fields_json, loader_field_enum_values_json, versions_json, mods_gallery_json, links_json;
|
||||
",
|
||||
&project_ids_parsed,
|
||||
&remaining_strings.into_iter().map(|x| x.to_string().to_lowercase()).collect::<Vec<_>>(),
|
||||
@@ -719,11 +700,7 @@ impl Project {
|
||||
icon_url: m.icon_url.clone(),
|
||||
published: m.published,
|
||||
updated: m.updated,
|
||||
issues_url: m.issues_url.clone(),
|
||||
source_url: m.source_url.clone(),
|
||||
wiki_url: m.wiki_url.clone(),
|
||||
license_url: m.license_url.clone(),
|
||||
discord_url: m.discord_url.clone(),
|
||||
status: ProjectStatus::from_string(
|
||||
&m.status,
|
||||
),
|
||||
@@ -774,9 +751,9 @@ impl Project {
|
||||
|
||||
gallery
|
||||
},
|
||||
donation_urls: serde_json::from_value(
|
||||
m.donations.unwrap_or_default(),
|
||||
).ok().unwrap_or_default(),
|
||||
urls: serde_json::from_value(
|
||||
m.links.unwrap_or_default(),
|
||||
).unwrap_or_default(),
|
||||
aggregate_version_fields: VersionField::from_query_json(m.loader_fields, m.version_fields, m.loader_field_enum_values, true),
|
||||
thread_id: ThreadId(m.thread_id),
|
||||
}}))
|
||||
@@ -894,7 +871,7 @@ pub struct QueryProject {
|
||||
pub versions: Vec<VersionId>,
|
||||
pub project_types: Vec<String>,
|
||||
pub games: Vec<String>,
|
||||
pub donation_urls: Vec<DonationUrl>,
|
||||
pub urls: Vec<LinkUrl>,
|
||||
pub gallery_items: Vec<GalleryItem>,
|
||||
pub thread_id: ThreadId,
|
||||
pub aggregate_version_fields: Vec<VersionField>,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::super::ids::OrganizationId;
|
||||
@@ -8,13 +10,14 @@ use crate::database::models::{version_item, DatabaseError};
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::ids::{ProjectId, VersionId};
|
||||
use crate::models::projects::{
|
||||
Dependency, DonationLink, GalleryItem, License, Loader, ModeratorMessage, MonetizationStatus,
|
||||
Project, ProjectStatus, Version, VersionFile, VersionStatus, VersionType,
|
||||
Dependency, GalleryItem, License, Link, Loader, ModeratorMessage, MonetizationStatus, Project,
|
||||
ProjectStatus, Version, VersionFile, VersionStatus, VersionType,
|
||||
};
|
||||
use crate::models::threads::ThreadId;
|
||||
use crate::routes::v2_reroute;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
/// A project returned from the API
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
@@ -132,6 +135,18 @@ impl LegacyProject {
|
||||
}
|
||||
}
|
||||
|
||||
let issues_url = data.link_urls.get("issues").map(|l| l.url.clone());
|
||||
let source_url = data.link_urls.get("source").map(|l| l.url.clone());
|
||||
let wiki_url = data.link_urls.get("wiki").map(|l| l.url.clone());
|
||||
let discord_url = data.link_urls.get("discord").map(|l| l.url.clone());
|
||||
|
||||
let donation_urls = data
|
||||
.link_urls
|
||||
.iter()
|
||||
.filter(|(_, l)| l.donation)
|
||||
.map(|(_, l)| DonationLink::try_from(l.clone()).ok())
|
||||
.collect::<Option<Vec<_>>>();
|
||||
|
||||
Self {
|
||||
id: data.id,
|
||||
slug: data.slug,
|
||||
@@ -157,11 +172,11 @@ impl LegacyProject {
|
||||
loaders,
|
||||
versions: data.versions,
|
||||
icon_url: data.icon_url,
|
||||
issues_url: data.issues_url,
|
||||
source_url: data.source_url,
|
||||
wiki_url: data.wiki_url,
|
||||
discord_url: data.discord_url,
|
||||
donation_urls: data.donation_urls,
|
||||
issues_url,
|
||||
source_url,
|
||||
wiki_url,
|
||||
discord_url,
|
||||
donation_urls,
|
||||
gallery: data.gallery,
|
||||
color: data.color,
|
||||
thread_id: data.thread_id,
|
||||
@@ -316,3 +331,36 @@ impl From<Version> for LegacyVersion {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Validate, Clone, Eq, PartialEq)]
|
||||
pub struct DonationLink {
|
||||
pub id: String,
|
||||
pub platform: String,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl TryFrom<Link> for DonationLink {
|
||||
type Error = String;
|
||||
fn try_from(link: Link) -> Result<Self, String> {
|
||||
if !link.donation {
|
||||
return Err("Not a donation".to_string());
|
||||
}
|
||||
Ok(Self {
|
||||
platform: capitalize_first(&link.platform),
|
||||
url: link.url,
|
||||
id: link.platform,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn capitalize_first(input: &str) -> String {
|
||||
let mut result = input.to_owned();
|
||||
if let Some(first_char) = result.get_mut(0..1) {
|
||||
first_char.make_ascii_uppercase();
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
|
||||
use super::ids::{Base62Id, OrganizationId};
|
||||
use super::teams::TeamId;
|
||||
use super::users::UserId;
|
||||
use crate::database::models::project_item::QueryProject;
|
||||
use crate::database::models::project_item::{LinkUrl, QueryProject};
|
||||
use crate::database::models::version_item::QueryVersion;
|
||||
use crate::models::threads::ThreadId;
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -87,16 +87,9 @@ pub struct Project {
|
||||
pub versions: Vec<VersionId>,
|
||||
/// The URL of the icon of the project
|
||||
pub icon_url: Option<String>,
|
||||
/// An optional link to where to submit bugs or issues with the project.
|
||||
pub issues_url: Option<String>,
|
||||
/// An optional link to the source code for the project.
|
||||
pub source_url: Option<String>,
|
||||
/// An optional link to the project's wiki page or other relevant information.
|
||||
pub wiki_url: Option<String>,
|
||||
/// An optional link to the project's discord
|
||||
pub discord_url: Option<String>,
|
||||
/// An optional list of all donation links the project has
|
||||
pub donation_urls: Option<Vec<DonationLink>>,
|
||||
|
||||
/// A collection of links to the project's various pages.
|
||||
pub link_urls: HashMap<String, Link>,
|
||||
|
||||
/// A string of URLs to visual content featuring the project
|
||||
pub gallery: Vec<GalleryItem>,
|
||||
@@ -206,20 +199,11 @@ impl From<QueryProject> for Project {
|
||||
loaders: m.loaders,
|
||||
versions: data.versions.into_iter().map(|v| v.into()).collect(),
|
||||
icon_url: m.icon_url,
|
||||
issues_url: m.issues_url,
|
||||
source_url: m.source_url,
|
||||
wiki_url: m.wiki_url,
|
||||
discord_url: m.discord_url,
|
||||
donation_urls: Some(
|
||||
data.donation_urls
|
||||
.into_iter()
|
||||
.map(|d| DonationLink {
|
||||
id: d.platform_short,
|
||||
platform: d.platform_name,
|
||||
url: d.url,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
link_urls: data
|
||||
.urls
|
||||
.into_iter()
|
||||
.map(|d| (d.platform_name.clone(), Link::from(d)))
|
||||
.collect(),
|
||||
gallery: data
|
||||
.gallery_items
|
||||
.into_iter()
|
||||
@@ -266,15 +250,24 @@ pub struct License {
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Validate, Clone, Eq, PartialEq)]
|
||||
pub struct DonationLink {
|
||||
pub id: String,
|
||||
pub struct Link {
|
||||
pub platform: String,
|
||||
pub donation: bool,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub url: String,
|
||||
}
|
||||
impl From<LinkUrl> for Link {
|
||||
fn from(data: LinkUrl) -> Self {
|
||||
Self {
|
||||
platform: data.platform_name,
|
||||
donation: data.donation,
|
||||
url: data.url,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A status decides the visibility of a project in search, URLs, and the whole site itself.
|
||||
/// Approved - Project is displayed on search, and accessible by URL
|
||||
|
||||
@@ -3,8 +3,8 @@ use crate::database::redis::RedisPool;
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models;
|
||||
use crate::models::ids::ImageId;
|
||||
use crate::models::projects::{DonationLink, Loader, Project, ProjectStatus};
|
||||
use crate::models::v2::projects::{LegacyProject, LegacySideType};
|
||||
use crate::models::projects::{Loader, Project, ProjectStatus};
|
||||
use crate::models::v2::projects::{DonationLink, LegacyProject, LegacySideType};
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::v3::project_creation::default_project_type;
|
||||
use crate::routes::v3::project_creation::{CreateError, NewGalleryItem};
|
||||
@@ -193,6 +193,25 @@ pub async fn project_create(
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut link_urls = HashMap::new();
|
||||
if let Some(issue_url) = legacy_create.issues_url {
|
||||
link_urls.insert("issues".to_string(), issue_url);
|
||||
}
|
||||
if let Some(source_url) = legacy_create.source_url {
|
||||
link_urls.insert("source".to_string(), source_url);
|
||||
}
|
||||
if let Some(wiki_url) = legacy_create.wiki_url {
|
||||
link_urls.insert("wiki".to_string(), wiki_url);
|
||||
}
|
||||
if let Some(discord_url) = legacy_create.discord_url {
|
||||
link_urls.insert("discord".to_string(), discord_url);
|
||||
}
|
||||
if let Some(donation_urls) = legacy_create.donation_urls {
|
||||
for donation_url in donation_urls {
|
||||
link_urls.insert(donation_url.platform, donation_url.url);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(v3::project_creation::ProjectCreateData {
|
||||
title: legacy_create.title,
|
||||
slug: legacy_create.slug,
|
||||
@@ -201,12 +220,8 @@ pub async fn project_create(
|
||||
initial_versions,
|
||||
categories: legacy_create.categories,
|
||||
additional_categories: legacy_create.additional_categories,
|
||||
issues_url: legacy_create.issues_url,
|
||||
source_url: legacy_create.source_url,
|
||||
wiki_url: legacy_create.wiki_url,
|
||||
license_url: legacy_create.license_url,
|
||||
discord_url: legacy_create.discord_url,
|
||||
donation_urls: legacy_create.donation_urls,
|
||||
link_urls,
|
||||
is_draft: legacy_create.is_draft,
|
||||
license_id: legacy_create.license_id,
|
||||
gallery_items: legacy_create.gallery_items,
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
use crate::database::models::categories::LinkPlatform;
|
||||
use crate::database::models::{project_item, version_item};
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models;
|
||||
use crate::models::projects::{
|
||||
DonationLink, MonetizationStatus, Project, ProjectStatus, SearchRequest, Version,
|
||||
Link, MonetizationStatus, Project, ProjectStatus, SearchRequest, Version,
|
||||
};
|
||||
use crate::models::v2::projects::{LegacyProject, LegacySideType};
|
||||
use crate::models::v2::projects::{DonationLink, LegacyProject, LegacySideType};
|
||||
use crate::models::v2::search::LegacySearchResults;
|
||||
use crate::queue::session::AuthQueue;
|
||||
use crate::routes::v3::projects::ProjectIds;
|
||||
use crate::routes::{v2_reroute, v3, ApiError};
|
||||
use crate::search::{search_for_project, SearchConfig, SearchError};
|
||||
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
|
||||
|
||||
use itertools::Itertools;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use validator::Validate;
|
||||
|
||||
@@ -360,18 +361,78 @@ pub async fn project_edit(
|
||||
// - change the loaders to mrpack only
|
||||
// - add categories to the project for the corresponding loaders
|
||||
|
||||
let mut new_links = HashMap::new();
|
||||
if let Some(issues_url) = v2_new_project.issues_url {
|
||||
if let Some(issues_url) = issues_url {
|
||||
new_links.insert("issues".to_string(), Some(issues_url));
|
||||
} else {
|
||||
new_links.insert("issues".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(source_url) = v2_new_project.source_url {
|
||||
if let Some(source_url) = source_url {
|
||||
new_links.insert("source".to_string(), Some(source_url));
|
||||
} else {
|
||||
new_links.insert("source".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(wiki_url) = v2_new_project.wiki_url {
|
||||
if let Some(wiki_url) = wiki_url {
|
||||
new_links.insert("wiki".to_string(), Some(wiki_url));
|
||||
} else {
|
||||
new_links.insert("wiki".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(discord_url) = v2_new_project.discord_url {
|
||||
if let Some(discord_url) = discord_url {
|
||||
new_links.insert("discord".to_string(), Some(discord_url));
|
||||
} else {
|
||||
new_links.insert("discord".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
// In v2, setting donation links resets all other donation links
|
||||
// (resetting to the new ones)
|
||||
if let Some(donation_urls) = v2_new_project.donation_urls {
|
||||
// Fetch current donation links from project so we know what to delete
|
||||
let fetched_example_project = project_item::Project::get(&info.0, &**pool, &redis).await?;
|
||||
let donation_links = fetched_example_project
|
||||
.map(|x| {
|
||||
x.urls
|
||||
.into_iter()
|
||||
.filter_map(|l| {
|
||||
if l.donation {
|
||||
Some(Link::from(l)) // TODO: tests
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
// Set existing donation links to None
|
||||
for old_link in donation_links {
|
||||
new_links.insert(old_link.platform, None);
|
||||
}
|
||||
|
||||
// Add new donation links
|
||||
for donation_url in donation_urls {
|
||||
new_links.insert(donation_url.id, Some(donation_url.url));
|
||||
}
|
||||
}
|
||||
|
||||
let new_project = v3::projects::EditProject {
|
||||
title: v2_new_project.title,
|
||||
description: v2_new_project.description,
|
||||
body: v2_new_project.body,
|
||||
categories: v2_new_project.categories,
|
||||
additional_categories: v2_new_project.additional_categories,
|
||||
issues_url: v2_new_project.issues_url,
|
||||
source_url: v2_new_project.source_url,
|
||||
wiki_url: v2_new_project.wiki_url,
|
||||
license_url: v2_new_project.license_url,
|
||||
discord_url: v2_new_project.discord_url,
|
||||
donation_urls: v2_new_project.donation_urls,
|
||||
link_urls: Some(new_links),
|
||||
license_id: v2_new_project.license_id,
|
||||
slug: v2_new_project.slug,
|
||||
status: v2_new_project.status,
|
||||
@@ -500,6 +561,70 @@ pub async fn projects_edit(
|
||||
session_queue: web::Data<AuthQueue>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let bulk_edit_project = bulk_edit_project.into_inner();
|
||||
|
||||
let mut link_urls = HashMap::new();
|
||||
|
||||
// If we are *setting* donation links, we will set every possible donation link to None, as
|
||||
// setting will delete all of them then 're-add' the ones we want to keep
|
||||
if let Some(donation_url) = bulk_edit_project.donation_urls {
|
||||
let link_platforms = LinkPlatform::list(&**pool, &redis).await?;
|
||||
for link in link_platforms {
|
||||
if link.donation {
|
||||
link_urls.insert(link.name, None);
|
||||
}
|
||||
}
|
||||
// add
|
||||
for donation_url in donation_url {
|
||||
link_urls.insert(donation_url.id, Some(donation_url.url));
|
||||
}
|
||||
}
|
||||
|
||||
// For every delete, we will set the link to None
|
||||
if let Some(donation_url) = bulk_edit_project.remove_donation_urls {
|
||||
for donation_url in donation_url {
|
||||
link_urls.insert(donation_url.id, None);
|
||||
}
|
||||
}
|
||||
|
||||
// For every add, we will set the link to the new url
|
||||
if let Some(donation_url) = bulk_edit_project.add_donation_urls {
|
||||
for donation_url in donation_url {
|
||||
link_urls.insert(donation_url.id, Some(donation_url.url));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(issue_url) = bulk_edit_project.issues_url {
|
||||
if let Some(issue_url) = issue_url {
|
||||
link_urls.insert("issues".to_string(), Some(issue_url));
|
||||
} else {
|
||||
link_urls.insert("issues".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(source_url) = bulk_edit_project.source_url {
|
||||
if let Some(source_url) = source_url {
|
||||
link_urls.insert("source".to_string(), Some(source_url));
|
||||
} else {
|
||||
link_urls.insert("source".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(wiki_url) = bulk_edit_project.wiki_url {
|
||||
if let Some(wiki_url) = wiki_url {
|
||||
link_urls.insert("wiki".to_string(), Some(wiki_url));
|
||||
} else {
|
||||
link_urls.insert("wiki".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(discord_url) = bulk_edit_project.discord_url {
|
||||
if let Some(discord_url) = discord_url {
|
||||
link_urls.insert("discord".to_string(), Some(discord_url));
|
||||
} else {
|
||||
link_urls.insert("discord".to_string(), None);
|
||||
}
|
||||
}
|
||||
|
||||
v3::projects::projects_edit(
|
||||
req,
|
||||
web::Query(ids),
|
||||
@@ -511,13 +636,7 @@ pub async fn projects_edit(
|
||||
additional_categories: bulk_edit_project.additional_categories,
|
||||
add_additional_categories: bulk_edit_project.add_additional_categories,
|
||||
remove_additional_categories: bulk_edit_project.remove_additional_categories,
|
||||
donation_urls: bulk_edit_project.donation_urls,
|
||||
add_donation_urls: bulk_edit_project.add_donation_urls,
|
||||
remove_donation_urls: bulk_edit_project.remove_donation_urls,
|
||||
issues_url: bulk_edit_project.issues_url,
|
||||
source_url: bulk_edit_project.source_url,
|
||||
wiki_url: bulk_edit_project.wiki_url,
|
||||
discord_url: bulk_edit_project.discord_url,
|
||||
link_urls: Some(link_urls),
|
||||
}),
|
||||
redis,
|
||||
session_queue,
|
||||
|
||||
@@ -4,7 +4,9 @@ use super::ApiError;
|
||||
use crate::database::models::loader_fields::LoaderFieldEnumValue;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::v2::projects::LegacySideType;
|
||||
use crate::routes::v3::tags::{LoaderData as LoaderDataV3, LoaderFieldsEnumQuery};
|
||||
use crate::routes::v3::tags::{
|
||||
LinkPlatformQueryData, LoaderData as LoaderDataV3, LoaderFieldsEnumQuery,
|
||||
};
|
||||
use crate::routes::{v2_reroute, v3};
|
||||
use actix_web::{get, web, HttpResponse};
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -164,8 +166,10 @@ pub async fn license_text(params: web::Path<(String,)>) -> Result<HttpResponse,
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct DonationPlatformQueryData {
|
||||
short: String,
|
||||
name: String,
|
||||
// The difference between name and short is removed in v3.
|
||||
// Now, the 'id' becomes the name, and the 'name' is removed (the frontend uses the id as the name)
|
||||
// pub short: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[get("donation_platform")]
|
||||
@@ -173,7 +177,21 @@ pub async fn donation_platform_list(
|
||||
pool: web::Data<PgPool>,
|
||||
redis: web::Data<RedisPool>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
v3::tags::donation_platform_list(pool, redis).await
|
||||
let response = v3::tags::link_platform_list(pool, redis).await?;
|
||||
|
||||
// Convert to V2 format
|
||||
Ok(
|
||||
match v2_reroute::extract_ok_json::<Vec<LinkPlatformQueryData>>(response).await {
|
||||
Ok(platforms) => {
|
||||
let platforms = platforms
|
||||
.into_iter()
|
||||
.map(|p| DonationPlatformQueryData { name: p.name })
|
||||
.collect::<Vec<_>>();
|
||||
HttpResponse::Ok().json(platforms)
|
||||
}
|
||||
Err(response) => response,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[get("report_type")]
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::models::ids::{ImageId, OrganizationId};
|
||||
use crate::models::images::{Image, ImageContext};
|
||||
use crate::models::pats::Scopes;
|
||||
use crate::models::projects::{
|
||||
DonationLink, License, MonetizationStatus, ProjectId, ProjectStatus, VersionId, VersionStatus,
|
||||
License, Link, MonetizationStatus, ProjectId, ProjectStatus, VersionId, VersionStatus,
|
||||
};
|
||||
use crate::models::teams::ProjectPermissions;
|
||||
use crate::models::threads::ThreadType;
|
||||
@@ -187,39 +187,12 @@ pub struct ProjectCreateData {
|
||||
/// A list of the categories that the project is in.
|
||||
pub additional_categories: Vec<String>,
|
||||
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
/// An optional link to where to submit bugs or issues with the project.
|
||||
pub issues_url: Option<String>,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
/// An optional link to the source code for the project.
|
||||
pub source_url: Option<String>,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
/// An optional link to the project's wiki page or other relevant information.
|
||||
pub wiki_url: Option<String>,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
/// An optional link to the project's license page
|
||||
pub license_url: Option<String>,
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
/// An optional link to the project's discord.
|
||||
pub discord_url: Option<String>,
|
||||
/// An optional list of all donation links the project has\
|
||||
#[validate]
|
||||
pub donation_urls: Option<Vec<DonationLink>>,
|
||||
/// An optional list of all donation links the project has
|
||||
#[validate(custom(function = "crate::util::validate::validate_url_hashmap_values"))]
|
||||
#[serde(default)]
|
||||
pub link_urls: HashMap<String, String>,
|
||||
|
||||
/// An optional boolean. If true, the project will be created as a draft.
|
||||
pub is_draft: Option<bool>,
|
||||
@@ -671,27 +644,35 @@ async fn project_create_inner(
|
||||
CreateError::InvalidInput(format!("Invalid SPDX license identifier: {err}"))
|
||||
})?;
|
||||
|
||||
let mut donation_urls = vec![];
|
||||
let mut link_urls = vec![];
|
||||
|
||||
if let Some(urls) = &project_create_data.donation_urls {
|
||||
for url in urls {
|
||||
let platform_id =
|
||||
models::categories::DonationPlatform::get_id(&url.id, &mut **transaction)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
CreateError::InvalidInput(format!(
|
||||
"Donation platform {} does not exist.",
|
||||
url.id.clone()
|
||||
))
|
||||
})?;
|
||||
|
||||
donation_urls.push(models::project_item::DonationUrl {
|
||||
platform_id,
|
||||
platform_short: "".to_string(),
|
||||
platform_name: "".to_string(),
|
||||
url: url.url.clone(),
|
||||
})
|
||||
}
|
||||
let link_platforms =
|
||||
models::categories::LinkPlatform::list(&mut **transaction, redis).await?;
|
||||
for (platform, url) in &project_create_data.link_urls {
|
||||
let platform_id =
|
||||
models::categories::LinkPlatform::get_id(platform, &mut **transaction)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
CreateError::InvalidInput(format!(
|
||||
"Link platform {} does not exist.",
|
||||
platform.clone()
|
||||
))
|
||||
})?;
|
||||
let link_platform = link_platforms
|
||||
.iter()
|
||||
.find(|x| x.id == platform_id)
|
||||
.ok_or_else(|| {
|
||||
CreateError::InvalidInput(format!(
|
||||
"Link platform {} does not exist.",
|
||||
platform.clone()
|
||||
))
|
||||
})?;
|
||||
link_urls.push(models::project_item::LinkUrl {
|
||||
platform_id,
|
||||
platform_name: link_platform.name.clone(),
|
||||
url: url.clone(),
|
||||
donation: link_platform.donation,
|
||||
})
|
||||
}
|
||||
|
||||
let project_builder_actual = models::project_item::ProjectBuilder {
|
||||
@@ -702,12 +683,8 @@ async fn project_create_inner(
|
||||
description: project_create_data.description,
|
||||
body: project_create_data.body,
|
||||
icon_url: icon_data.clone().map(|x| x.0),
|
||||
issues_url: project_create_data.issues_url,
|
||||
source_url: project_create_data.source_url,
|
||||
wiki_url: project_create_data.wiki_url,
|
||||
|
||||
license_url: project_create_data.license_url,
|
||||
discord_url: project_create_data.discord_url,
|
||||
categories,
|
||||
additional_categories,
|
||||
initial_versions: versions,
|
||||
@@ -715,7 +692,7 @@ async fn project_create_inner(
|
||||
requested_status: Some(project_create_data.requested_status),
|
||||
license: license_id.to_string(),
|
||||
slug: Some(project_create_data.slug),
|
||||
donation_urls,
|
||||
link_urls,
|
||||
gallery_items: gallery_urls
|
||||
.iter()
|
||||
.map(|x| models::project_item::GalleryItem {
|
||||
@@ -835,11 +812,12 @@ async fn project_create_inner(
|
||||
.map(|v| v.version_id.into())
|
||||
.collect::<Vec<_>>(),
|
||||
icon_url: project_builder.icon_url.clone(),
|
||||
issues_url: project_builder.issues_url.clone(),
|
||||
source_url: project_builder.source_url.clone(),
|
||||
wiki_url: project_builder.wiki_url.clone(),
|
||||
discord_url: project_builder.discord_url.clone(),
|
||||
donation_urls: project_create_data.donation_urls.clone(),
|
||||
link_urls: project_builder
|
||||
.link_urls
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|x| (x.platform_name.clone(), Link::from(x)))
|
||||
.collect(),
|
||||
gallery: gallery_urls,
|
||||
color: project_builder.color,
|
||||
thread_id: thread_id.into(),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth::{filter_authorized_projects, get_user_from_headers, is_authorized};
|
||||
@@ -14,7 +15,7 @@ use crate::models::images::ImageContext;
|
||||
use crate::models::notifications::NotificationBody;
|
||||
use crate::models::pats::Scopes;
|
||||
use crate::models::projects::{
|
||||
DonationLink, MonetizationStatus, Project, ProjectId, ProjectStatus, SearchRequest,
|
||||
MonetizationStatus, Project, ProjectId, ProjectStatus, SearchRequest,
|
||||
};
|
||||
use crate::models::teams::ProjectPermissions;
|
||||
use crate::models::threads::MessageBody;
|
||||
@@ -192,49 +193,10 @@ pub struct EditProject {
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub issues_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub source_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub wiki_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub license_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub discord_url: Option<Option<String>>,
|
||||
#[validate]
|
||||
pub donation_urls: Option<Vec<DonationLink>>,
|
||||
#[validate(custom(function = "crate::util::validate::validate_url_hashmap_optional_values"))]
|
||||
// <name, url> (leave url empty to delete)
|
||||
pub link_urls: Option<HashMap<String, Option<String>>>,
|
||||
pub license_id: Option<String>,
|
||||
#[validate(
|
||||
length(min = 3, max = 64),
|
||||
@@ -592,69 +554,6 @@ pub async fn project_edit(
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(issues_url) = &new_project.issues_url {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have the permissions to edit the issues URL of this project!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET issues_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
issues_url.as_deref(),
|
||||
id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(source_url) = &new_project.source_url {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have the permissions to edit the source URL of this project!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET source_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
source_url.as_deref(),
|
||||
id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(wiki_url) = &new_project.wiki_url {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have the permissions to edit the wiki URL of this project!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET wiki_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
wiki_url.as_deref(),
|
||||
id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(license_url) = &new_project.license_url {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
@@ -676,27 +575,6 @@ pub async fn project_edit(
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(discord_url) = &new_project.discord_url {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have the permissions to edit the discord URL of this project!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET discord_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
discord_url.as_deref(),
|
||||
id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(slug) = &new_project.slug {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
@@ -785,51 +663,59 @@ pub async fn project_edit(
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
if let Some(donations) = &new_project.donation_urls {
|
||||
if let Some(links) = &new_project.link_urls {
|
||||
if !perms.contains(ProjectPermissions::EDIT_DETAILS) {
|
||||
return Err(ApiError::CustomAuthentication(
|
||||
"You do not have the permissions to edit the donation links of this project!"
|
||||
"You do not have the permissions to edit the links of this project!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let ids_to_delete = links
|
||||
.iter()
|
||||
.map(|(name, _)| name.clone())
|
||||
.collect::<Vec<String>>();
|
||||
// Deletes all links from hashmap- either will be deleted or be replaced
|
||||
sqlx::query!(
|
||||
"
|
||||
DELETE FROM mods_donations
|
||||
WHERE joining_mod_id = $1
|
||||
DELETE FROM mods_links
|
||||
WHERE joining_mod_id = $1 AND joining_platform_id IN (
|
||||
SELECT id FROM link_platforms WHERE name = ANY($2)
|
||||
)
|
||||
",
|
||||
id as db_ids::ProjectId,
|
||||
&ids_to_delete
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
for donation in donations {
|
||||
let platform_id = db_models::categories::DonationPlatform::get_id(
|
||||
&donation.id,
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInput(format!(
|
||||
"Platform {} does not exist.",
|
||||
donation.id.clone()
|
||||
))
|
||||
})?;
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO mods_donations (joining_mod_id, joining_platform_id, url)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
id as db_ids::ProjectId,
|
||||
platform_id as db_ids::DonationPlatformId,
|
||||
donation.url
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
for (platform, url) in links {
|
||||
if let Some(url) = url {
|
||||
let platform_id = db_models::categories::LinkPlatform::get_id(
|
||||
platform,
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInput(format!(
|
||||
"Platform {} does not exist.",
|
||||
platform.clone()
|
||||
))
|
||||
})?;
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
id as db_ids::ProjectId,
|
||||
platform_id as db_ids::LinkPlatformId,
|
||||
url
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(moderation_message) = &new_project.moderation_message {
|
||||
if !user.role.is_mod()
|
||||
&& (!project_item.inner.status.is_approved() || moderation_message.is_some())
|
||||
@@ -1139,53 +1025,8 @@ pub struct BulkEditProject {
|
||||
pub add_additional_categories: Option<Vec<String>>,
|
||||
pub remove_additional_categories: Option<Vec<String>>,
|
||||
|
||||
#[validate]
|
||||
pub donation_urls: Option<Vec<DonationLink>>,
|
||||
#[validate]
|
||||
pub add_donation_urls: Option<Vec<DonationLink>>,
|
||||
#[validate]
|
||||
pub remove_donation_urls: Option<Vec<DonationLink>>,
|
||||
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub issues_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub source_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub wiki_url: Option<Option<String>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
with = "::serde_with::rust::double_option"
|
||||
)]
|
||||
#[validate(
|
||||
custom(function = "crate::util::validate::validate_url"),
|
||||
length(max = 2048)
|
||||
)]
|
||||
pub discord_url: Option<Option<String>>,
|
||||
#[validate(custom(function = " crate::util::validate::validate_url_hashmap_optional_values"))]
|
||||
pub link_urls: Option<HashMap<String, Option<String>>>,
|
||||
}
|
||||
|
||||
pub async fn projects_edit(
|
||||
@@ -1250,7 +1091,7 @@ pub async fn projects_edit(
|
||||
.await?;
|
||||
|
||||
let categories = db_models::categories::Category::list(&**pool, &redis).await?;
|
||||
let donation_platforms = db_models::categories::DonationPlatform::list(&**pool, &redis).await?;
|
||||
let link_platforms = db_models::categories::LinkPlatform::list(&**pool, &redis).await?;
|
||||
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
@@ -1330,130 +1171,52 @@ pub async fn projects_edit(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let project_donations: Vec<DonationLink> = project
|
||||
.donation_urls
|
||||
.into_iter()
|
||||
.map(|d| DonationLink {
|
||||
id: d.platform_short,
|
||||
platform: d.platform_name,
|
||||
url: d.url,
|
||||
})
|
||||
.collect();
|
||||
let mut set_donation_links =
|
||||
if let Some(donation_links) = bulk_edit_project.donation_urls.clone() {
|
||||
donation_links
|
||||
} else {
|
||||
project_donations.clone()
|
||||
};
|
||||
if let Some(links) = &bulk_edit_project.link_urls {
|
||||
let ids_to_delete = links
|
||||
.iter()
|
||||
.map(|(name, _)| name.clone())
|
||||
.collect::<Vec<String>>();
|
||||
// Deletes all links from hashmap- either will be deleted or be replaced
|
||||
sqlx::query!(
|
||||
"
|
||||
DELETE FROM mods_links
|
||||
WHERE joining_mod_id = $1 AND joining_platform_id IN (
|
||||
SELECT id FROM link_platforms WHERE name = ANY($2)
|
||||
)
|
||||
",
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
&ids_to_delete
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
if let Some(delete_donations) = &bulk_edit_project.remove_donation_urls {
|
||||
for donation in delete_donations {
|
||||
if let Some(pos) = set_donation_links
|
||||
.iter()
|
||||
.position(|x| donation.url == x.url && donation.id == x.id)
|
||||
{
|
||||
set_donation_links.remove(pos);
|
||||
for (platform, url) in links {
|
||||
if let Some(url) = url {
|
||||
let platform_id = link_platforms
|
||||
.iter()
|
||||
.find(|x| &x.name == platform)
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInput(format!(
|
||||
"Platform {} does not exist.",
|
||||
platform.clone()
|
||||
))
|
||||
})?
|
||||
.id;
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
platform_id as db_ids::LinkPlatformId,
|
||||
url
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(add_donations) = &bulk_edit_project.add_donation_urls {
|
||||
set_donation_links.append(&mut add_donations.clone());
|
||||
}
|
||||
|
||||
if set_donation_links != project_donations {
|
||||
sqlx::query!(
|
||||
"
|
||||
DELETE FROM mods_donations
|
||||
WHERE joining_mod_id = $1
|
||||
",
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
for donation in set_donation_links {
|
||||
let platform_id = donation_platforms
|
||||
.iter()
|
||||
.find(|x| x.short == donation.id)
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInput(format!(
|
||||
"Platform {} does not exist.",
|
||||
donation.id.clone()
|
||||
))
|
||||
})?
|
||||
.id;
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO mods_donations (joining_mod_id, joining_platform_id, url)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
platform_id as db_ids::DonationPlatformId,
|
||||
donation.url
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(issues_url) = &bulk_edit_project.issues_url {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET issues_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
issues_url.as_deref(),
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(source_url) = &bulk_edit_project.source_url {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET source_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
source_url.as_deref(),
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(wiki_url) = &bulk_edit_project.wiki_url {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET wiki_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
wiki_url.as_deref(),
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(discord_url) = &bulk_edit_project.discord_url {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE mods
|
||||
SET discord_url = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
discord_url.as_deref(),
|
||||
project.inner.id as db_ids::ProjectId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
db_models::Project::clear_cache(project.inner.id, project.inner.slug, None, &redis).await?;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::ApiError;
|
||||
use crate::database::models::categories::{Category, DonationPlatform, ProjectType, ReportType};
|
||||
use crate::database::models::categories::{Category, LinkPlatform, ProjectType, ReportType};
|
||||
use crate::database::models::loader_fields::{
|
||||
Game, Loader, LoaderField, LoaderFieldEnumValue, LoaderFieldType,
|
||||
};
|
||||
@@ -22,7 +22,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||
.route("loader_field", web::get().to(loader_fields_list))
|
||||
.route("license", web::get().to(license_list))
|
||||
.route("license/{id}", web::get().to(license_text))
|
||||
.route("donation_platform", web::get().to(donation_platform_list))
|
||||
.route("link_platform", web::get().to(link_platform_list))
|
||||
.route("report_type", web::get().to(report_type_list))
|
||||
.route("project_type", web::get().to(project_type_list));
|
||||
}
|
||||
@@ -214,23 +214,19 @@ pub async fn license_text(params: web::Path<(String,)>) -> Result<HttpResponse,
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct DonationPlatformQueryData {
|
||||
short: String,
|
||||
name: String,
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct LinkPlatformQueryData {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub async fn donation_platform_list(
|
||||
pub async fn link_platform_list(
|
||||
pool: web::Data<PgPool>,
|
||||
redis: web::Data<RedisPool>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let results: Vec<DonationPlatformQueryData> = DonationPlatform::list(&**pool, &redis)
|
||||
let results: Vec<LinkPlatformQueryData> = LinkPlatform::list(&**pool, &redis)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|x| DonationPlatformQueryData {
|
||||
short: x.short,
|
||||
name: x.name,
|
||||
})
|
||||
.map(|x| LinkPlatformQueryData { name: x.name })
|
||||
.collect();
|
||||
Ok(HttpResponse::Ok().json(results))
|
||||
}
|
||||
|
||||
@@ -93,6 +93,26 @@ pub fn validate_url(value: &str) -> Result<(), validator::ValidationError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_url_hashmap_optional_values(
|
||||
values: &std::collections::HashMap<String, Option<String>>,
|
||||
) -> Result<(), validator::ValidationError> {
|
||||
for value in values.values().flatten() {
|
||||
validate_url(value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_url_hashmap_values(
|
||||
values: &std::collections::HashMap<String, String>,
|
||||
) -> Result<(), validator::ValidationError> {
|
||||
for value in values.values() {
|
||||
validate_url(value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_no_restricted_scopes(value: &Scopes) -> Result<(), validator::ValidationError> {
|
||||
if value.is_restricted() {
|
||||
return Err(validator::ValidationError::new(
|
||||
|
||||
Reference in New Issue
Block a user