mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-09 09:52:10 +00:00
Extricating min and max length checks
This commit is contained in:
parent
d49e80ded3
commit
a79db1d514
|
@ -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.
|
/// Checks the site name length, the limit as defined in the DB.
|
||||||
pub fn site_name_length_check(name: &str) -> LemmyResult<()> {
|
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,
|
name,
|
||||||
SITE_NAME_MIN_LENGTH,
|
|
||||||
SITE_NAME_MAX_LENGTH,
|
SITE_NAME_MAX_LENGTH,
|
||||||
LemmyErrorType::SiteNameRequired,
|
|
||||||
LemmyErrorType::SiteNameLengthOverflow,
|
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
|
/// Check minumum and maximum length of input string. If the string is too short or too long, the
|
||||||
/// corresponding error is returned.
|
/// corresponding error is returned.
|
||||||
///
|
///
|
||||||
/// HTML frontends specify maximum input length using `maxlength` attribute.
|
/// HTML frontends specify maximum input length using `maxlength` attribute.
|
||||||
/// For consistency we use the same counting method (UTF-16 code units).
|
/// For consistency we use the same counting method (UTF-16 code units).
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/maxlength
|
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/maxlength
|
||||||
fn min_max_length_check(
|
fn max_length_check(item: &str, max_length: usize, max_msg: LemmyErrorType) -> LemmyResult<()> {
|
||||||
item: &str,
|
|
||||||
min_length: usize,
|
|
||||||
max_length: usize,
|
|
||||||
min_msg: LemmyErrorType,
|
|
||||||
max_msg: LemmyErrorType,
|
|
||||||
) -> LemmyResult<()> {
|
|
||||||
let len = item.encode_utf16().count();
|
let len = item.encode_utf16().count();
|
||||||
if len > max_length {
|
if len > max_length {
|
||||||
Err(max_msg.into())
|
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())
|
Err(min_msg.into())
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -239,7 +239,7 @@ async fn process_post_aggregates_ranks_in_batches(conn: &mut AsyncPgConnection)
|
||||||
let mut previous_batch_result = Some(process_start_time);
|
let mut previous_batch_result = Some(process_start_time);
|
||||||
while let Some(previous_batch_last_published) = previous_batch_result {
|
while let Some(previous_batch_last_published) = previous_batch_result {
|
||||||
let result = sql_query(
|
let result = sql_query(
|
||||||
r#"WITH batch AS (SELECT pa.id
|
r"WITH batch AS (SELECT pa.id
|
||||||
FROM post_aggregates pa
|
FROM post_aggregates pa
|
||||||
WHERE pa.published > $1
|
WHERE pa.published > $1
|
||||||
AND (pa.hot_rank != 0 OR pa.hot_rank_active != 0)
|
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)
|
scaled_rank = scaled_rank(pa.score, pa.published, ca.users_active_month)
|
||||||
FROM batch, community_aggregates ca
|
FROM batch, community_aggregates ca
|
||||||
WHERE pa.id = batch.id and pa.community_id = ca.community_id RETURNING pa.published;
|
WHERE pa.id = batch.id and pa.community_id = ca.community_id RETURNING pa.published;
|
||||||
"#,
|
",
|
||||||
)
|
)
|
||||||
.bind::<Timestamptz, _>(previous_batch_last_published)
|
.bind::<Timestamptz, _>(previous_batch_last_published)
|
||||||
.bind::<Integer, _>(update_batch_size)
|
.bind::<Integer, _>(update_batch_size)
|
||||||
|
|
Loading…
Reference in a new issue