diff --git a/src/config/mod.rs b/src/config/mod.rs index a5935242..0f1db32b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -47,6 +47,11 @@ pub type Logging = v2::logging::Logging; pub type Threshold = v2::logging::Threshold; pub type Website = v2::website::Website; +pub type Demo = v2::website::Demo; +pub type Terms = v2::website::Terms; +pub type TermsPage = v2::website::TermsPage; +pub type TermsUpload = v2::website::TermsUpload; +pub type Markdown = v2::website::Markdown; /// Configuration version const VERSION_2: &str = "2.0.0"; @@ -193,6 +198,14 @@ impl Info { config_toml_path, }) } + + #[must_use] + pub fn from_toml(config_toml: &str) -> Self { + Self { + config_toml: Some(config_toml.to_owned()), + config_toml_path: String::new(), + } + } } /// Errors that can occur when loading the configuration. @@ -385,88 +398,44 @@ mod tests { #[cfg(test)] fn default_config_toml() -> String { - let config = r#"[metadata] - app = "torrust-index" - purpose = "configuration" - schema_version = "2.0.0" - - [logging] - threshold = "info" - - [website] - name = "Torrust" - - [tracker] - api_url = "http://localhost:1212/" - listed = false - private = false - token = "MyAccessToken" - token_valid_seconds = 7257600 - url = "udp://localhost:6969" - - [net] - bind_address = "0.0.0.0:3001" - - [auth] - user_claim_token_pepper = "MaxVerstappenWC2021" - - [auth.password_constraints] - max_password_length = 64 - min_password_length = 6 - - [database] - connect_url = "sqlite://data.db?mode=rwc" - - [mail] - from = "example@email.com" - reply_to = "noreply@email.com" - - [mail.smtp] - port = 25 - server = "" - - [mail.smtp.credentials] - password = "" - username = "" - - [image_cache] - capacity = 128000000 - entry_size_limit = 4000000 - max_request_timeout_ms = 1000 - user_quota_bytes = 64000000 - user_quota_period_seconds = 3600 - - [api] - default_torrent_page_size = 10 - max_torrent_page_size = 30 - - [tracker_statistics_importer] - port = 3002 - torrent_info_update_interval = 3600 - "# - .lines() - .map(str::trim_start) - .collect::>() - .join("\n"); + use std::fs; + use std::path::PathBuf; + + // Get the path to the current Cargo.toml directory + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR environment variable not set"); + + // Construct the path to the default configuration file relative to the Cargo.toml directory + let mut path = PathBuf::from(manifest_dir); + path.push("tests/fixtures/default_configuration.toml"); + + let config = fs::read_to_string(path) + .expect("Could not read default configuration TOML file: tests/fixtures/default_configuration.toml"); + + config.lines().map(str::trim_start).collect::>().join("\n"); + config } - #[tokio::test] - async fn configuration_should_build_settings_with_default_values() { - let configuration = Configuration::default().get_all().await; + /// Build settings from default configuration fixture in TOML. + /// + /// We just want to load that file without overriding with env var or other + /// configuration loading behavior. + #[cfg(test)] + fn default_settings() -> Settings { + use figment::providers::{Format, Toml}; + use figment::Figment; - let toml = toml::to_string(&configuration).expect("Could not encode TOML value for configuration"); + let figment = Figment::from(Toml::string(&default_config_toml())); + let settings: Settings = figment.extract().expect("Invalid configuration"); - assert_eq!(toml, default_config_toml()); + settings } #[tokio::test] - async fn configuration_should_return_all_settings() { - let configuration = Configuration::default().get_all().await; - - let toml = toml::to_string(&configuration).expect("Could not encode TOML value for configuration"); + async fn configuration_should_build_settings_with_default_values() { + let settings = Configuration::default().get_all().await; - assert_eq!(toml, default_config_toml()); + assert_eq!(settings, default_settings()); } #[tokio::test] diff --git a/src/config/v2/website.rs b/src/config/v2/website.rs index 489ce265..a568c29b 100644 --- a/src/config/v2/website.rs +++ b/src/config/v2/website.rs @@ -6,12 +6,22 @@ pub struct Website { /// The name of the website. #[serde(default = "Website::default_name")] pub name: String, + + /// The demo settings when the app runs in `demo` mode. + #[serde(default = "Website::default_demo")] + pub demo: Option, + + /// The legal information. + #[serde(default = "Website::default_terms")] + pub terms: Terms, } impl Default for Website { fn default() -> Self { Self { name: Self::default_name(), + demo: Self::default_demo(), + terms: Self::default_terms(), } } } @@ -20,4 +30,136 @@ impl Website { fn default_name() -> String { "Torrust".to_string() } + + fn default_demo() -> Option { + None + } + + fn default_terms() -> Terms { + Terms::default() + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Demo { + /// The fixed message to show when the index is running in demo mode. + #[serde(default = "Demo::default_warning")] + pub warning: String, +} + +impl Demo { + fn default_warning() -> String { + "⚠️ Please be aware: This demo resets all data weekly. Torrents not complying with our Usage Policies will be removed immediately without notice. We encourage the responsible use of this software in compliance with all legal requirements.".to_string() + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Terms { + /// The terms page info. + #[serde(default = "Terms::default_page")] + pub page: TermsPage, + + /// The upload agreement info. + #[serde(default = "Terms::default_upload")] + pub upload: TermsUpload, +} + +impl Terms { + fn default_page() -> TermsPage { + TermsPage::default() + } + + fn default_upload() -> TermsUpload { + TermsUpload::default() + } +} + +impl Default for Terms { + fn default() -> Self { + Self { + page: Self::default_page(), + upload: Self::default_upload(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct TermsPage { + /// The terms page title. + #[serde(default = "TermsPage::default_title")] + pub title: String, + + /// The terms page content. + #[serde(default = "TermsPage::default_content")] + pub content: Markdown, +} + +impl TermsPage { + fn default_title() -> String { + "Usage Policies and Content Restrictions".to_string() + } + + fn default_content() -> Markdown { + Markdown::new( + r" +# Usage Policies and Content Restrictions + +Our software is designed to support the distribution of legal, authorized content only. Users may only upload or share files that fall under the following categories: + +- **Open-Source Licenses:** Content licensed under recognized open-source licenses, allowing for free distribution and modification. +- **Creative Commons Licenses:** Content released under Creative Commons licenses that permit sharing and distribution. +- **Public Domain:** Content that is free of copyright restrictions and available for public use. + +**Prohibited Content:** Any content that infringes copyright, is subject to copyright protection, or is illegal under applicable laws is strictly prohibited. This includes but is not limited to copyrighted movies, music, software, books, and any other media. + +**Enforcement:** We reserve the right to remove any content that does not comply with these policies without notice. We may also take additional steps, including reporting violations to the relevant authorities, if necessary. + +", + ) + } +} + +impl Default for TermsPage { + fn default() -> Self { + Self { + title: Self::default_title(), + content: Self::default_content(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct TermsUpload { + /// The terms page content. + #[serde(default = "TermsUpload::default_content_upload_agreement")] + pub content_upload_agreement: Markdown, +} + +impl TermsUpload { + fn default_content_upload_agreement() -> Markdown { + Markdown::new("I confirm that the content I am uploading is authorized, and I have read and agree to the terms.") + } +} + +impl Default for TermsUpload { + fn default() -> Self { + Self { + content_upload_agreement: Self::default_content_upload_agreement(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Markdown(String); + +impl Markdown { + #[must_use] + pub fn new(content: &str) -> Self { + Self(content.to_owned()) + } + + #[must_use] + pub fn source(&self) -> String { + self.0.clone() + } } diff --git a/src/services/settings.rs b/src/services/settings.rs index 2b49dff3..198cba5e 100644 --- a/src/services/settings.rs +++ b/src/services/settings.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use url::Url; use super::authorization::{self, ACTION}; -use crate::config::{Configuration, Settings}; +use crate::config::{self, Configuration, Settings}; use crate::errors::ServiceError; use crate::models::user::UserId; @@ -106,6 +106,7 @@ fn extract_public_settings(settings: &Settings) -> ConfigurationPublic { tracker_listed: settings.tracker.listed, tracker_private: settings.tracker.private, email_on_signup, + website: settings.website.clone().into(), } } @@ -118,6 +119,7 @@ pub struct ConfigurationPublic { tracker_listed: bool, tracker_private: bool, email_on_signup: EmailOnSignup, + website: Website, } /// Whether the email is required on signup or not. @@ -164,6 +166,92 @@ impl FromStr for EmailOnSignup { } } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Website { + pub name: String, + pub demo: Option, + pub terms: Terms, +} + +impl From for Website { + fn from(website: config::Website) -> Self { + Self { + name: website.name, + demo: website.demo.map(std::convert::Into::into), + terms: website.terms.into(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Demo { + pub warning: String, +} + +impl From for Demo { + fn from(demo: config::Demo) -> Self { + Self { warning: demo.warning } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Terms { + pub page: TermsPage, + pub upload: TermsUpload, +} + +impl From for Terms { + fn from(terms: config::Terms) -> Self { + Self { + page: terms.page.into(), + upload: terms.upload.into(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct TermsPage { + pub title: String, + pub content: Markdown, +} + +impl From for TermsPage { + fn from(terms_page: config::TermsPage) -> Self { + Self { + title: terms_page.title, + content: terms_page.content.into(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct TermsUpload { + pub content_upload_agreement: Markdown, +} + +impl From for TermsUpload { + fn from(terms_upload: config::TermsUpload) -> Self { + Self { + content_upload_agreement: terms_upload.content_upload_agreement.into(), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Markdown(pub String); + +impl Markdown { + fn new(content: &str) -> Self { + Self(content.to_owned()) + } +} + +impl From for Markdown { + fn from(markdown: config::Markdown) -> Self { + Self::new(&markdown.source()) + } +} + #[cfg(test)] mod tests { use crate::config::Configuration; @@ -191,11 +279,12 @@ mod tests { assert_eq!( extract_public_settings(&all_settings), ConfigurationPublic { - website_name: all_settings.website.name, + website_name: all_settings.website.name.clone(), tracker_url: all_settings.tracker.url, tracker_listed: all_settings.tracker.listed, tracker_private: all_settings.tracker.private, email_on_signup, + website: all_settings.website.into(), } ); } diff --git a/tests/fixtures/default_configuration.toml b/tests/fixtures/default_configuration.toml new file mode 100644 index 00000000..d86ac1c1 --- /dev/null +++ b/tests/fixtures/default_configuration.toml @@ -0,0 +1,81 @@ +[metadata] +app = "torrust-index" +purpose = "configuration" +schema_version = "2.0.0" + +[logging] +threshold = "info" + +[website] +name = "Torrust" + +[website.terms] + +[website.terms.upload] +content_upload_agreement = """I confirm that the content I am uploading is authorized, and I have read and agree to the terms.""" + +[website.terms.page] +content = """ + +# Usage Policies and Content Restrictions + +Our software is designed to support the distribution of legal, authorized content only. Users may only upload or share files that fall under the following categories: + +- **Open-Source Licenses:** Content licensed under recognized open-source licenses, allowing for free distribution and modification. +- **Creative Commons Licenses:** Content released under Creative Commons licenses that permit sharing and distribution. +- **Public Domain:** Content that is free of copyright restrictions and available for public use. + +**Prohibited Content:** Any content that infringes copyright, is subject to copyright protection, or is illegal under applicable laws is strictly prohibited. This includes but is not limited to copyrighted movies, music, software, books, and any other media. + +**Enforcement:** We reserve the right to remove any content that does not comply with these policies without notice. We may also take additional steps, including reporting violations to the relevant authorities, if necessary. + +""" +title = "Usage Policies and Content Restrictions" + +[tracker] +api_url = "http://localhost:1212/" +listed = false +private = false +token = "MyAccessToken" +token_valid_seconds = 7257600 +url = "udp://localhost:6969" + +[net] +bind_address = "0.0.0.0:3001" + +[auth] +user_claim_token_pepper = "MaxVerstappenWC2021" + +[auth.password_constraints] +max_password_length = 64 +min_password_length = 6 + +[database] +connect_url = "sqlite://data.db?mode=rwc" + +[mail] +from = "example@email.com" +reply_to = "noreply@email.com" + +[mail.smtp] +port = 25 +server = "" + +[mail.smtp.credentials] +password = "" +username = "" + +[image_cache] +capacity = 128000000 +entry_size_limit = 4000000 +max_request_timeout_ms = 1000 +user_quota_bytes = 64000000 +user_quota_period_seconds = 3600 + +[api] +default_torrent_page_size = 10 +max_torrent_page_size = 30 + +[tracker_statistics_importer] +port = 3002 +torrent_info_update_interval = 3600