X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapi_crud%2Fsrc%2Fsite%2Fmod.rs;h=652b9e6564629d0dddb5b5dea2fb4c36730881bb;hb=92568956353f21649ed9aff68b42699c9d036f30;hp=845da04914fd7169562142ddce0348fbc8f52b8b;hpb=4c8f2e976effe381d4ea1914462c43242a8c64fd;p=lemmy.git diff --git a/crates/api_crud/src/site/mod.rs b/crates/api_crud/src/site/mod.rs index 845da049..652b9e65 100644 --- a/crates/api_crud/src/site/mod.rs +++ b/crates/api_crud/src/site/mod.rs @@ -1,3 +1,96 @@ +use lemmy_db_schema::{ListingType, RegistrationMode}; +use lemmy_utils::error::{LemmyErrorType, LemmyResult}; + mod create; mod read; mod update; + +/// Checks whether the default post listing type is valid for a site. +pub fn site_default_post_listing_type_check( + default_post_listing_type: &Option, +) -> LemmyResult<()> { + if let Some(listing_type) = default_post_listing_type { + // Only allow all or local as default listing types... + if listing_type != &ListingType::All && listing_type != &ListingType::Local { + Err(LemmyErrorType::InvalidDefaultPostListingType)? + } else { + Ok(()) + } + } else { + Ok(()) + } +} + +/// Checks whether the application question and registration mode align. +pub fn application_question_check( + current_application_question: &Option, + new_application_question: &Option, + registration_mode: RegistrationMode, +) -> LemmyResult<()> { + let has_no_question: bool = + current_application_question.is_none() && new_application_question.is_none(); + let is_nullifying_question: bool = new_application_question == &Some(String::new()); + + if registration_mode == RegistrationMode::RequireApplication + && (has_no_question || is_nullifying_question) + { + Err(LemmyErrorType::ApplicationQuestionRequired)? + } else { + Ok(()) + } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + #![allow(clippy::indexing_slicing)] + + use crate::site::{application_question_check, site_default_post_listing_type_check}; + use lemmy_db_schema::{ListingType, RegistrationMode}; + + #[test] + fn test_site_default_post_listing_type_check() { + assert!(site_default_post_listing_type_check(&None::).is_ok()); + assert!(site_default_post_listing_type_check(&Some(ListingType::All)).is_ok()); + assert!(site_default_post_listing_type_check(&Some(ListingType::Local)).is_ok()); + assert!(site_default_post_listing_type_check(&Some(ListingType::Subscribed)).is_err()); + } + + #[test] + fn test_application_question_check() { + assert!( + application_question_check(&Some(String::from("q")), &Some(String::new()), RegistrationMode::RequireApplication).is_err(), + "Expected application to be invalid because an application is required, current question: {:?}, new question: {:?}", + "q", + String::new(), + ); + assert!( + application_question_check(&None, &None, RegistrationMode::RequireApplication).is_err(), + "Expected application to be invalid because an application is required, current question: {:?}, new question: {:?}", + None::, + None:: + ); + + assert!( + application_question_check(&None, &None, RegistrationMode::Open).is_ok(), + "Expected application to be valid because no application required, current question: {:?}, new question: {:?}, mode: {:?}", + None::, + None::, + RegistrationMode::Open + ); + assert!( + application_question_check(&None, &Some(String::from("q")), RegistrationMode::RequireApplication).is_ok(), + "Expected application to be valid because new application provided, current question: {:?}, new question: {:?}, mode: {:?}", + None::, + Some(String::from("q")), + RegistrationMode::RequireApplication + ); + assert!( + application_question_check(&Some(String::from("q")), &None, RegistrationMode::RequireApplication).is_ok(), + "Expected application to be valid because application existed, current question: {:?}, new question: {:?}, mode: {:?}", + Some(String::from("q")), + None::, + RegistrationMode::RequireApplication + ); + } +}