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
This commit is contained in:
Nutomic 2024-09-20 14:43:04 +02:00 committed by Felix Ableitner
parent 9a77b327f7
commit a67cfd2348
3 changed files with 29 additions and 13 deletions

View file

@ -1,3 +1,4 @@
use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check}; use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair}; use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair};
use actix_web::web::Json; use actix_web::web::Json;
@ -117,17 +118,17 @@ pub async fn create_site(
let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm { let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm {
message: data.rate_limit_message, message: data.rate_limit_message,
message_per_second: data.rate_limit_message_per_second, message_per_second: not_zero(data.rate_limit_message_per_second),
post: data.rate_limit_post, post: data.rate_limit_post,
post_per_second: data.rate_limit_post_per_second, post_per_second: not_zero(data.rate_limit_post_per_second),
register: data.rate_limit_register, register: data.rate_limit_register,
register_per_second: data.rate_limit_register_per_second, register_per_second: not_zero(data.rate_limit_register_per_second),
image: data.rate_limit_image, image: data.rate_limit_image,
image_per_second: data.rate_limit_image_per_second, image_per_second: not_zero(data.rate_limit_image_per_second),
comment: data.rate_limit_comment, comment: data.rate_limit_comment,
comment_per_second: data.rate_limit_comment_per_second, comment_per_second: not_zero(data.rate_limit_comment_per_second),
search: data.rate_limit_search, search: data.rate_limit_search,
search_per_second: data.rate_limit_search_per_second, search_per_second: not_zero(data.rate_limit_search_per_second),
..Default::default() ..Default::default()
}; };

View file

@ -40,12 +40,19 @@ pub fn application_question_check(
} }
} }
fn not_zero(val: Option<i32>) -> Option<i32> {
match val {
Some(0) => None,
v => v,
}
}
#[cfg(test)] #[cfg(test)]
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)] #[allow(clippy::indexing_slicing)]
mod tests { mod tests {
use crate::site::{application_question_check, site_default_post_listing_type_check}; use crate::site::{application_question_check, not_zero, site_default_post_listing_type_check};
use lemmy_db_schema::{ListingType, RegistrationMode}; use lemmy_db_schema::{ListingType, RegistrationMode};
#[test] #[test]
@ -93,4 +100,11 @@ mod tests {
RegistrationMode::RequireApplication 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)));
}
} }

View file

@ -1,3 +1,4 @@
use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check}; use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::Json; use actix_web::web::Json;
@ -131,17 +132,17 @@ pub async fn update_site(
let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm { let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm {
message: data.rate_limit_message, message: data.rate_limit_message,
message_per_second: data.rate_limit_message_per_second, message_per_second: not_zero(data.rate_limit_message_per_second),
post: data.rate_limit_post, post: data.rate_limit_post,
post_per_second: data.rate_limit_post_per_second, post_per_second: not_zero(data.rate_limit_post_per_second),
register: data.rate_limit_register, register: data.rate_limit_register,
register_per_second: data.rate_limit_register_per_second, register_per_second: not_zero(data.rate_limit_register_per_second),
image: data.rate_limit_image, image: data.rate_limit_image,
image_per_second: data.rate_limit_image_per_second, image_per_second: not_zero(data.rate_limit_image_per_second),
comment: data.rate_limit_comment, comment: data.rate_limit_comment,
comment_per_second: data.rate_limit_comment_per_second, comment_per_second: not_zero(data.rate_limit_comment_per_second),
search: data.rate_limit_search, search: data.rate_limit_search,
search_per_second: data.rate_limit_search_per_second, search_per_second: not_zero(data.rate_limit_search_per_second),
..Default::default() ..Default::default()
}; };