lemmy/crates/api_crud/src/site/mod.rs
Nutomic 8cdfc148d7
Ignore zero values when setting rate limits (fixes #4280) (#5029)
* Ignore zero values when setting rate limits (fixes #4280)

Havent bothered to add an error message for such an uncommon case.

* fmt

* reorder, add test
2024-09-20 14:43:04 +02:00

110 lines
3.8 KiB
Rust

use lemmy_db_schema::{ListingType, RegistrationMode};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
pub mod create;
pub mod read;
pub 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<ListingType>,
) -> 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<String>,
new_application_question: &Option<String>,
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(())
}
}
fn not_zero(val: Option<i32>) -> Option<i32> {
match val {
Some(0) => None,
v => v,
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
mod tests {
use crate::site::{application_question_check, not_zero, 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::<ListingType>).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::<String>,
None::<String>
);
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::<String>,
None::<String>,
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::<String>,
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::<String>,
RegistrationMode::RequireApplication
);
}
#[test]
fn test_not_zero() {
assert_eq!(None, not_zero(None));
assert_eq!(None, not_zero(Some(0)));
assert_eq!(Some(5), not_zero(Some(5)));
}
}