+ Ok(Json(SiteResponse {
+ site_view,
+ taglines,
+ }))
+}
+
+fn validate_update_payload(local_site: &LocalSite, edit_site: &EditSite) -> LemmyResult<()> {
+ // Check that the slur regex compiles, and return 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(
+ &edit_site
+ .slur_filter_regex
+ .as_deref()
+ .or(local_site.slur_filter_regex.as_deref()),
+ )?;
+
+ if let Some(name) = &edit_site.name {
+ // The name doesn't need to be updated, but if provided it cannot be blanked out...
+ site_name_length_check(name)?;
+ check_slurs_opt(&edit_site.name, &slur_regex)?;
+ }
+
+ if let Some(desc) = &edit_site.description {
+ site_description_length_check(desc)?;
+ check_slurs_opt(&edit_site.description, &slur_regex)?;
+ }
+
+ site_default_post_listing_type_check(&edit_site.default_post_listing_type)?;
+
+ check_site_visibility_valid(
+ local_site.private_instance,
+ local_site.federation_enabled,
+ &edit_site.private_instance,
+ &edit_site.federation_enabled,
+ )?;
+
+ // Ensure that the sidebar has fewer than the max num characters...
+ is_valid_body_field(&edit_site.sidebar, false)?;
+
+ application_question_check(
+ &local_site.application_question,
+ &edit_site.application_question,
+ edit_site
+ .registration_mode
+ .unwrap_or(local_site.registration_mode),
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
+ use crate::site::update::validate_update_payload;
+ use lemmy_api_common::site::EditSite;
+ use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode};
+ use lemmy_utils::error::LemmyErrorType;
+
+ #[test]
+ fn test_validate_invalid_update_payload() {
+ let invalid_payloads = [
+ (
+ "EditSite name matches LocalSite slur filter",
+ LemmyErrorType::Slurs,
+ &generate_local_site(
+ Some(String::from("(foo|bar)")),
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(String::from("foo site_name")),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "EditSite name matches new slur filter",
+ LemmyErrorType::Slurs,
+ &generate_local_site(
+ Some(String::from("(foo|bar)")),
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(String::from("zeta site_name")),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ Some(String::from("(zeta|alpha)")),
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "EditSite listing type is Subscribed, which is invalid",
+ LemmyErrorType::InvalidDefaultPostListingType,
+ &generate_local_site(
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(String::from("site_name")),
+ None::<String>,
+ None::<String>,
+ Some(ListingType::Subscribed),
+ None::<String>,
+ None::<bool>,
+ None::<bool>,
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "EditSite is both private and federated",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
+ &generate_local_site(
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(String::from("site_name")),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ Some(true),
+ Some(true),
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "LocalSite is private, but EditSite also makes it federated",
+ LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
+ &generate_local_site(
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(String::from("site_name")),
+ None::<String>,
+ None::<String>,
+ None::<ListingType>,
+ None::<String>,
+ None::<bool>,
+ Some(true),
+ None::<String>,
+ None::<RegistrationMode>,
+ ),
+ ),
+ (
+ "EditSite requires application, but neither it nor LocalSite has an application question",
+ LemmyErrorType::ApplicationQuestionRequired,
+ &generate_local_site(
+ None::<String>,
+ true,
+ false,
+ None::<String>,
+ RegistrationMode::Open,
+ ),
+ &generate_edit_site(
+ Some(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, edit_site),
+ )| {
+ match validate_update_payload(local_site, edit_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
+ )
+ }
+ }
+ },
+ );
+ }