Skip to content

Commit

Permalink
Ignore zero values when setting rate limits (fixes #4280)
Browse files Browse the repository at this point in the history
Havent bothered to add an error message for such an uncommon case.
  • Loading branch information
Nutomic committed Sep 19, 2024
1 parent 026e23c commit 1bcc34a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
14 changes: 8 additions & 6 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateSite>,
Expand Down Expand Up @@ -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()
};

Expand Down
7 changes: 7 additions & 0 deletions crates/api_crud/src/site/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ mod tests {
);
}
}

fn not_zero(val: Option<i32>) -> Option<i32> {
match val {
Some(0) => None,
v => v
}
}
14 changes: 8 additions & 6 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ use lemmy_utils::{
},
};

use super::not_zero;

#[tracing::instrument(skip(context))]
pub async fn update_site(
data: Json<EditSite>,
Expand Down Expand Up @@ -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()
};

Expand Down

0 comments on commit 1bcc34a

Please sign in to comment.