Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding check to description and body length fields. #2805

Merged
merged 4 commits into from
Apr 15, 2023
Merged

Conversation

dessalines
Copy link
Member

- Also making the checks return `LemmyError`
- Fixes #1747
@dessalines dessalines requested a review from Nutomic as a code owner April 13, 2023 18:52

fn has_newline(name: &str) -> bool {
name.contains('\n')
}

pub fn is_valid_actor_name(name: &str, actor_name_max_length: usize) -> bool {
name.chars().count() <= actor_name_max_length
pub fn is_valid_actor_name(name: &str, actor_name_max_length: usize) -> Result<(), LemmyError> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all called from the API, and needed to return lemmyerror's on failure anyway.

}
}

pub fn is_valid_bio_field(bio: &str) -> Result<(), LemmyError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use LemmyResult which I added recently to simplify this a bit.

}

pub fn is_valid_post_title(title: &str) -> bool {
VALID_POST_TITLE_REGEX.is_match(title) && !has_newline(title)
pub fn is_valid_body_field(body: &str) -> Result<(), LemmyError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would call this is_valid_post_body() for clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind it seems to be used both for posts and comments. Would then add a comment mentioning that.

@@ -46,6 +50,9 @@ impl Perform for BanFromCommunity {

// Verify that only mods or admins can ban
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
if let Some(reason) = &data.reason {
is_valid_body_field(reason)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As most of these are optional, I think it would be cleaner if the functions would also take Option<String> as param.

Copy link
Member Author

@dessalines dessalines Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but there's a lot of places where the bodies are possibly Option<Option<String>>, and some where its just String, so it seemed best to leave it outside, unless I want to write several overloaded wrapper functions rather than just one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not seeing Option<Option<String>> anywhere. And there are only a few cases without option, there it would work to pass the parameter as Some(body).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it required a few clones in some places, but it works now.

@Nutomic
Copy link
Member

Nutomic commented Apr 15, 2023

CI is failing, feel free to merge when it passes.

@dessalines
Copy link
Member Author

Tough one, cause I just tested this locally, and its working.

@dessalines dessalines enabled auto-merge (squash) April 15, 2023 14:00
@dessalines dessalines merged commit 38d4429 into main Apr 15, 2023
dessalines added a commit that referenced this pull request May 4, 2023
* Adding check to description and body length fields.

- Also making the checks return `LemmyError`
- Fixes #1747

* Address PR comments.

* PR comments 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add server-side character limit checks on description and body fields.
2 participants