Extricating min and max length checks (#4018)

This commit is contained in:
Dessalines 2023-10-05 06:21:37 -04:00 committed by GitHub
parent d49e80ded3
commit 25c7733eb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 23 deletions

View file

@ -145,11 +145,10 @@ pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> {
/// Checks the site name length, the limit as defined in the DB.
pub fn site_name_length_check(name: &str) -> LemmyResult<()> {
min_max_length_check(
min_length_check(name, SITE_NAME_MIN_LENGTH, LemmyErrorType::SiteNameRequired)?;
max_length_check(
name,
SITE_NAME_MIN_LENGTH,
SITE_NAME_MAX_LENGTH,
LemmyErrorType::SiteNameRequired,
LemmyErrorType::SiteNameLengthOverflow,
)
}
@ -163,33 +162,24 @@ pub fn site_description_length_check(description: &str) -> LemmyResult<()> {
)
}
fn max_length_check(item: &str, max_length: usize, error_type: LemmyErrorType) -> LemmyResult<()> {
min_max_length_check(
item,
0,
max_length,
LemmyErrorType::Unknown(String::new()),
error_type,
)
}
/// Check minumum and maximum length of input string. If the string is too short or too long, the
/// corresponding error is returned.
///
/// HTML frontends specify maximum input length using `maxlength` attribute.
/// For consistency we use the same counting method (UTF-16 code units).
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/maxlength
fn min_max_length_check(
item: &str,
min_length: usize,
max_length: usize,
min_msg: LemmyErrorType,
max_msg: LemmyErrorType,
) -> LemmyResult<()> {
fn max_length_check(item: &str, max_length: usize, max_msg: LemmyErrorType) -> LemmyResult<()> {
let len = item.encode_utf16().count();
if len > max_length {
Err(max_msg.into())
} else if len < min_length {
} else {
Ok(())
}
}
fn min_length_check(item: &str, min_length: usize, min_msg: LemmyErrorType) -> LemmyResult<()> {
let len = item.encode_utf16().count();
if len < min_length {
Err(min_msg.into())
} else {
Ok(())

View file

@ -239,7 +239,7 @@ async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection)
let mut previous_batch_result = Some(process_start_time);
while let Some(previous_batch_last_published) = previous_batch_result {
let result = sql_query(
r#"WITH batch AS (SELECT pa.id
r"WITH batch AS (SELECT pa.id
FROM post_aggregates pa
WHERE pa.published > $1
AND (pa.hot_rank != 0 OR pa.hot_rank_active != 0)
@ -252,7 +252,7 @@ async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection)
scaled_rank = scaled_rank(pa.score, pa.published, ca.users_active_month)
FROM batch, community_aggregates ca
WHERE pa.id = batch.id and pa.community_id = ca.community_id RETURNING pa.published;
"#,
",
)
.bind::<Timestamptz, _>(previous_batch_last_published)
.bind::<Integer, _>(update_batch_size)