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

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

View File

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