+ // Check that the slur regex compiles, and returns the regex if valid...
+ // Prioritize using new slur regex from the request; if not provided, use the existing regex.
+ let slur_regex = build_and_check_regex(
+ &create_site
+ .slur_filter_regex
+ .as_deref()
+ .or(local_site.slur_filter_regex.as_deref()),
+ )?;
+
+ site_name_length_check(&create_site.name)?;
+ check_slurs(&create_site.name, &slur_regex)?;
+
+ if let Some(desc) = &create_site.description {
+ site_description_length_check(desc)?;
+ check_slurs_opt(&create_site.description, &slur_regex)?;
+ }
+
+ site_default_post_listing_type_check(&create_site.default_post_listing_type)?;
+
+ check_site_visibility_valid(
+ local_site.private_instance,
+ local_site.federation_enabled,
+ &create_site.private_instance,
+ &create_site.federation_enabled,
+ )?;
+
+ // Ensure that the sidebar has fewer than the max num characters...
+ is_valid_body_field(&create_site.sidebar, false)?;
+
+ application_question_check(
+ &local_site.application_question,
+ &create_site.application_question,
+ create_site
+ .registration_mode
+ .unwrap_or(local_site.registration_mode),
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
+ use crate::site::create::validate_create_payload;
+ use lemmy_api_common::site::CreateSite;
+ use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode};
+ use lemmy_utils::error::LemmyErrorType;
+
+ #[test]
+ fn test_validate_invalid_create_payload() {
+ let invalid_payloads = [
+ (
+ "CreateSite attempted on set up LocalSite",
+ LemmyErrorType::SiteAlreadyExists,
+ &generate_local_site(
+ true,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite name matches LocalSite slur filter",
+ LemmyErrorType::Slurs,
+ &generate_local_site(
+ false,
+ Some(String::from("(foo|bar)")),
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("foo site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite name matches new slur filter",
+ LemmyErrorType::Slurs,
+ &generate_local_site(
+ false,
+ Some(String::from("(foo|bar)")),
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("zeta site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ Some(String::from("(zeta|alpha)")),
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite listing type is Subscribed, which is invalid",
+ LemmyErrorType::InvalidDefaultPostListingType,
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ Some(ListingType::Subscribed),
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite is both private and federated",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ Some(true),
+ Some(true),
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "LocalSite is private, but CreateSite also makes it federated",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ Some(true),
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite requires application, but neither it nor LocalSite has an application question",
+ LemmyErrorType::ApplicationQuestionRequired,
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ Some(RegistrationMode::RequireApplication),
+ ),
+ ),
+ ];
+
+ invalid_payloads.iter().enumerate().for_each(
+ |(
+ idx,
+ &(reason, ref expected_err, local_site, create_site),
+ )| {
+ match validate_create_payload(
+ local_site,
+ create_site,
+ ) {
+ Ok(_) => {
+ panic!(
+ "Got Ok, but validation should have failed with error: {} for reason: {}. invalid_payloads.nth({})",
+ expected_err, reason, idx
+ )
+ }
+ Err(error) => {
+ assert!(
+ error.error_type.eq(&expected_err.clone()),
+ "Got Err {:?}, but should have failed with message: {} for reason: {}. invalid_payloads.nth({})",
+ error.error_type,
+ expected_err,
+ reason,
+ idx
+ )
+ }
+ }
+ },
+ );
+ }
+
+ #[test]
+ fn test_validate_valid_create_payload() {
+ let valid_payloads = [
+ (
+ "No changes between LocalSite and CreateSite",
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "CreateSite allows clearing and changing values",
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ Some(String::new()),
+ Some(String::new()),
+ Some(ListingType::All),
+ Some(String::new()),
+ Some(false),
+ Some(true),
+ Some(String::new()),
+ Some(RegistrationMode::Open),
+ ),
+ ),
+ (
+ "CreateSite clears existing slur filter regex",
+ &generate_local_site(
+ false,
+ Some(String::from("(foo|bar)")),
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("foo site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ Some(String::new()),
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "LocalSite has application question and CreateSite now requires applications,",
+ &generate_local_site(
+ false,
+ None::<String>,
+ true,
+ false,
+ Some(String::from("question")),
+ RegistrationMode::Open,
+ ),
+ &generate_create_site(
+ String::from("site_name"),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ Some(RegistrationMode::RequireApplication),
+ ),
+ ),
+ ];
+
+ valid_payloads
+ .iter()
+ .enumerate()
+ .for_each(|(idx, &(reason, local_site, edit_site))| {
+ assert!(
+ validate_create_payload(local_site, edit_site).is_ok(),
+ "Got Err, but should have got Ok for reason: {}. valid_payloads.nth({})",
+ reason,
+ idx
+ );
+ })
+ }
+
+ fn generate_local_site(
+ site_setup: bool,
+ site_slur_filter_regex: Option<String>,
+ site_is_private: bool,
+ site_is_federated: bool,
+ site_application_question: Option<String>,
+ site_registration_mode: RegistrationMode,
+ ) -> LocalSite {
+ LocalSite {
+ id: Default::default(),
+ site_id: Default::default(),
+ site_setup,
+ enable_downvotes: false,
+ enable_nsfw: false,
+ community_creation_admin_only: false,
+ require_email_verification: false,
+ application_question: site_application_question,
+ private_instance: site_is_private,
+ default_theme: String::new(),
+ default_post_listing_type: ListingType::All,
+ legal_information: None,
+ hide_modlog_mod_names: false,
+ application_email_admins: false,
+ slur_filter_regex: site_slur_filter_regex,
+ actor_name_max_length: 0,
+ federation_enabled: site_is_federated,
+ captcha_enabled: false,
+ captcha_difficulty: String::new(),
+ published: Default::default(),
+ updated: None,
+ registration_mode: site_registration_mode,
+ reports_email_admins: false,