This commit is contained in:
Wyatt Verchere
2023-11-30 23:14:52 -08:00
committed by GitHub
parent 756c14d988
commit 4bbc57b0dc
38 changed files with 907 additions and 807 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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")]

View File

@@ -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(),

View File

@@ -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?;
}

View File

@@ -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))
}