Skip to content

Commit

Permalink
Count chars, not bytes for max title length (fixes #4366) (#4367)
Browse files Browse the repository at this point in the history
* Count chars, not bytes for max title length (fixes #4366)

* fix api test
  • Loading branch information
Nutomic committed Jan 15, 2024
1 parent 9240a65 commit 1be7dbd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion api_tests/src/post.spec.ts
Expand Up @@ -76,7 +76,12 @@ test("Create a post", async () => {
throw "Missing beta community";
}

let postRes = await createPost(alpha, betaCommunity.community.id);
let postRes = await createPost(
alpha,
betaCommunity.community.id,
"https://example.com/",
"แƒแƒจแƒจ แƒ˜แƒ—แƒฎแƒแƒ•แƒก แƒ˜แƒ แƒแƒœแƒก แƒ“แƒแƒฃแƒงแƒแƒ•แƒœแƒ”แƒ‘แƒšแƒ˜แƒ• แƒ’แƒแƒแƒœแƒ—แƒแƒ•แƒ˜แƒกแƒฃแƒคแƒšแƒแƒก แƒ“แƒแƒ™แƒแƒ•แƒ”แƒ‘แƒฃแƒšแƒ˜ แƒœแƒแƒ•แƒ—แƒแƒ‘แƒ˜แƒก แƒขแƒแƒœแƒ™แƒ”แƒ แƒ˜",
);
expect(postRes.post_view.post).toBeDefined();
expect(postRes.post_view.community.local).toBe(false);
expect(postRes.post_view.creator.local).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions api_tests/src/shared.ts
Expand Up @@ -202,10 +202,10 @@ export async function setupLogins() {
export async function createPost(
api: LemmyHttp,
community_id: number,
// use example.com for consistent title and embed description
url: string = "https://example.com/",
// use example.com for consistent title and embed description
name: string = randomString(5),
): Promise<PostResponse> {
let name = randomString(5);
let body = randomString(10);
let form: CreatePost = {
name,
Expand Down
6 changes: 5 additions & 1 deletion crates/utils/src/utils/validation.rs
Expand Up @@ -147,7 +147,7 @@ pub fn is_valid_matrix_id(matrix_id: &str) -> LemmyResult<()> {
}

pub fn is_valid_post_title(title: &str) -> LemmyResult<()> {
let length = title.trim().len();
let length = title.trim().chars().count();
let check = (3..=200).contains(&length) && !has_newline(title);
if !check {
Err(LemmyErrorType::InvalidPostTitle.into())
Expand Down Expand Up @@ -380,6 +380,10 @@ mod tests {
#[test]
fn test_valid_post_title() {
assert!(is_valid_post_title("Post Title").is_ok());
assert!(is_valid_post_title(
"แƒแƒจแƒจ แƒ˜แƒ—แƒฎแƒแƒ•แƒก แƒ˜แƒ แƒแƒœแƒก แƒ“แƒแƒฃแƒงแƒแƒ•แƒœแƒ”แƒ‘แƒšแƒ˜แƒ• แƒ’แƒแƒแƒœแƒ—แƒแƒ•แƒ˜แƒกแƒฃแƒคแƒšแƒแƒก แƒ“แƒแƒ™แƒแƒ•แƒ”แƒ‘แƒฃแƒšแƒ˜ แƒœแƒแƒ•แƒ—แƒแƒ‘แƒ˜แƒก แƒขแƒแƒœแƒ™แƒ”แƒ แƒ˜"
)
.is_ok());
assert!(is_valid_post_title(" POST TITLE ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ").is_ok());
assert!(is_valid_post_title("\n \n \n \n ").is_err()); // tabs/spaces/newlines
}
Expand Down

0 comments on commit 1be7dbd

Please sign in to comment.