From 1bcc34a199e7d93e9a2010a2cf404b5ed73cb241 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 19 Sep 2024 14:06:09 +0200 Subject: [PATCH 1/3] Ignore zero values when setting rate limits (fixes #4280) Havent bothered to add an error message for such an uncommon case. --- crates/api_crud/src/site/create.rs | 14 ++++++++------ crates/api_crud/src/site/mod.rs | 7 +++++++ crates/api_crud/src/site/update.rs | 14 ++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 30321d8dda..2cca5f5e4f 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -41,6 +41,8 @@ use lemmy_utils::{ }; use url::Url; +use super::not_zero; + #[tracing::instrument(skip(context))] pub async fn create_site( data: Json, @@ -117,17 +119,17 @@ pub async fn create_site( let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm { 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_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_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_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_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_per_second: data.rate_limit_search_per_second, + search_per_second: not_zero(data.rate_limit_search_per_second), ..Default::default() }; diff --git a/crates/api_crud/src/site/mod.rs b/crates/api_crud/src/site/mod.rs index 0bf7cc2797..fd6f573e44 100644 --- a/crates/api_crud/src/site/mod.rs +++ b/crates/api_crud/src/site/mod.rs @@ -94,3 +94,10 @@ mod tests { ); } } + +fn not_zero(val: Option) -> Option { + match val { + Some(0) => None, + v => v + } +} \ No newline at end of file diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index a6046b4235..b06b6babd8 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -46,6 +46,8 @@ use lemmy_utils::{ }, }; +use super::not_zero; + #[tracing::instrument(skip(context))] pub async fn update_site( data: Json, @@ -130,17 +132,17 @@ pub async fn update_site( let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm { 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_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_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_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_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_per_second: data.rate_limit_search_per_second, + search_per_second: not_zero(data.rate_limit_search_per_second), ..Default::default() }; From 6c6f7c6dff43067d86f488d7f553d08fdaaa5733 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 19 Sep 2024 14:11:20 +0200 Subject: [PATCH 2/3] fmt --- crates/api_crud/src/site/create.rs | 3 +-- crates/api_crud/src/site/mod.rs | 4 ++-- crates/api_crud/src/site/update.rs | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 2cca5f5e4f..57d989f8d3 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -1,3 +1,4 @@ +use super::not_zero; use crate::site::{application_question_check, site_default_post_listing_type_check}; use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair}; use actix_web::web::Json; @@ -41,8 +42,6 @@ use lemmy_utils::{ }; use url::Url; -use super::not_zero; - #[tracing::instrument(skip(context))] pub async fn create_site( data: Json, diff --git a/crates/api_crud/src/site/mod.rs b/crates/api_crud/src/site/mod.rs index fd6f573e44..87fb1dc1a8 100644 --- a/crates/api_crud/src/site/mod.rs +++ b/crates/api_crud/src/site/mod.rs @@ -98,6 +98,6 @@ mod tests { fn not_zero(val: Option) -> Option { match val { Some(0) => None, - v => v + v => v, } -} \ No newline at end of file +} diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index b06b6babd8..930839a713 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -1,3 +1,4 @@ +use super::not_zero; use crate::site::{application_question_check, site_default_post_listing_type_check}; use activitypub_federation::config::Data; use actix_web::web::Json; @@ -46,8 +47,6 @@ use lemmy_utils::{ }, }; -use super::not_zero; - #[tracing::instrument(skip(context))] pub async fn update_site( data: Json, From f43996880ef97ee07cf908bd1fff054c3f347fd5 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 19 Sep 2024 14:46:31 +0200 Subject: [PATCH 3/3] reorder, add test --- crates/api_crud/src/site/mod.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/api_crud/src/site/mod.rs b/crates/api_crud/src/site/mod.rs index 87fb1dc1a8..462d2f072c 100644 --- a/crates/api_crud/src/site/mod.rs +++ b/crates/api_crud/src/site/mod.rs @@ -40,12 +40,19 @@ pub fn application_question_check( } } +fn not_zero(val: Option) -> Option { + 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, 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}; #[test] @@ -93,11 +100,11 @@ mod tests { RegistrationMode::RequireApplication ); } -} -fn not_zero(val: Option) -> Option { - match val { - Some(0) => None, - v => v, + #[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))); } }