lemmy_utils 1.0.0-beta.0

A link aggregator for the fediverse
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use crate::error::{LemmyErrorExt, LemmyErrorType, LemmyResult, MAX_API_PARAM_ELEMENTS};
use invisible_characters::INVISIBLE_CHARS;
use itertools::Itertools;
use regex::{Regex, RegexBuilder, RegexSet};
use std::sync::LazyLock;
use unicode_segmentation::UnicodeSegmentation;
use url::{ParseError, Url};

// From here: https://github.com/vector-im/element-android/blob/develop/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixPatterns.kt#L35
#[expect(clippy::expect_used)]
static VALID_MATRIX_ID_REGEX: LazyLock<Regex> = LazyLock::new(|| {
  Regex::new(r"^@[A-Za-z0-9\x21-\x39\x3B-\x7F]+:[A-Za-z0-9.-]+(:[0-9]{2,5})?$")
    .expect("compile regex")
});
// taken from https://en.wikipedia.org/wiki/UTM_parameters
const ALLOWED_POST_URL_SCHEMES: [&str; 3] = ["http", "https", "magnet"];

const BODY_MAX_LENGTH: usize = 10000;
const POST_BODY_MAX_LENGTH: usize = 50000;
const BIO_MAX_LENGTH: usize = 1000;
const URL_MAX_LENGTH: usize = 2000;
const ALT_TEXT_MAX_LENGTH: usize = 1500;
const SITE_NAME_MAX_LENGTH: usize = 20;
const SITE_NAME_MIN_LENGTH: usize = 1;
const SITE_SUMMARY_MAX_LENGTH: usize = 150;
const MIN_LENGTH_BLOCKING_KEYWORD: usize = 3;
const MAX_LENGTH_BLOCKING_KEYWORD: usize = 50;
const ACTOR_NAME_MAX_LENGTH: usize = 20;
const DISPLAY_NAME_MAX_LENGTH: usize = 50;

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

pub fn is_valid_actor_name(name: &str) -> LemmyResult<()> {
  // Only allow characters from a single alphabet per username. This avoids problems with lookalike
  // characters like `o` which looks identical in Latin and Cyrillic, and can be used to imitate
  // other users. Checks for additional alphabets can be added in the same way.
  #[expect(clippy::expect_used)]
  static VALID_ACTOR_NAME_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^(?:[a-zA-Z0-9_]+|[0-9_\p{Arabic}]+|[0-9_\p{Cyrillic}]+)$").expect("compile regex")
  });

  min_length_check(name, 3, LemmyErrorType::InvalidName)?;
  max_length_check(name, ACTOR_NAME_MAX_LENGTH, LemmyErrorType::InvalidName)?;
  if VALID_ACTOR_NAME_REGEX.is_match(name) {
    Ok(())
  } else {
    Err(LemmyErrorType::InvalidName.into())
  }
}

fn has_3_permitted_display_chars(name: &str) -> bool {
  let mut num_non_fdc: i8 = 0;
  for c in name.chars() {
    if !INVISIBLE_CHARS.contains(&c) {
      num_non_fdc += 1;
      if num_non_fdc >= 3 {
        break;
      }
    }
  }
  if num_non_fdc >= 3 {
    return true;
  }
  false
}

// Can't do a regex here, reverse lookarounds not supported
pub fn is_valid_display_name(name: &str) -> LemmyResult<()> {
  let check = !name.starts_with('@')
    && !name.starts_with(INVISIBLE_CHARS)
    && name.chars().count() <= DISPLAY_NAME_MAX_LENGTH
    && !has_newline(name)
    && has_3_permitted_display_chars(name);
  if !check {
    Err(LemmyErrorType::InvalidDisplayName.into())
  } else {
    Ok(())
  }
}

pub fn is_valid_matrix_id(matrix_id: &str) -> LemmyResult<()> {
  let check = VALID_MATRIX_ID_REGEX.is_match(matrix_id) && !has_newline(matrix_id);
  if !check {
    Err(LemmyErrorType::InvalidMatrixId.into())
  } else {
    Ok(())
  }
}

pub fn is_valid_post_title(title: &str) -> LemmyResult<()> {
  let length = title.trim().chars().count();
  let check =
    (3..=200).contains(&length) && !has_newline(title) && has_3_permitted_display_chars(title);
  if !check {
    Err(LemmyErrorType::InvalidPostTitle.into())
  } else {
    Ok(())
  }
}

/// This could be post bodies, comments, notes, or any description field
pub fn is_valid_body_field(body: &str, post: bool) -> LemmyResult<()> {
  if post {
    max_length_check(body, POST_BODY_MAX_LENGTH, LemmyErrorType::InvalidBodyField)?;
  } else {
    max_length_check(body, BODY_MAX_LENGTH, LemmyErrorType::InvalidBodyField)?;
  };
  Ok(())
}

pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> {
  max_length_check(bio, BIO_MAX_LENGTH, LemmyErrorType::BioLengthOverflow)
}

pub fn is_valid_alt_text_field(alt_text: &str) -> LemmyResult<()> {
  max_length_check(
    alt_text,
    ALT_TEXT_MAX_LENGTH,
    LemmyErrorType::AltTextLengthOverflow,
  )?;

  Ok(())
}

/// Checks the site name length, the limit as defined in the DB.
pub fn site_name_length_check(name: &str) -> LemmyResult<()> {
  min_length_check(name, SITE_NAME_MIN_LENGTH, LemmyErrorType::SiteNameRequired)?;
  max_length_check(
    name,
    SITE_NAME_MAX_LENGTH,
    LemmyErrorType::SiteNameLengthOverflow,
  )
}

/// Checks the site / community description length, the limit as defined in the DB.
pub fn summary_length_check(description: &str) -> LemmyResult<()> {
  max_length_check(
    description,
    SITE_SUMMARY_MAX_LENGTH,
    LemmyErrorType::SiteDescriptionLengthOverflow,
  )
}

/// Check minimum 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 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 {
    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(())
  }
}

/// Attempts to build a regex and check it for common errors before inserting into the DB.
pub fn build_and_check_regex(regex_str_opt: Option<&str>) -> LemmyResult<Regex> {
  // Placeholder regex which doesnt match anything
  // https://stackoverflow.com/a/940840
  let match_nothing = RegexBuilder::new("a^")
    .build()
    .with_lemmy_type(LemmyErrorType::InvalidRegex);
  if let Some(regex) = regex_str_opt {
    if regex.is_empty() {
      match_nothing
    } else {
      let regex = RegexBuilder::new(regex)
        .case_insensitive(true)
        .build()
        .with_lemmy_type(LemmyErrorType::InvalidRegex)?;
      if regex.is_match("1") {
        Err(LemmyErrorType::PermissiveRegex.into())
      } else {
        Ok(regex)
      }
    }
  } else {
    match_nothing
  }
}

pub fn is_valid_url(url: &Url) -> LemmyResult<()> {
  if !ALLOWED_POST_URL_SCHEMES.contains(&url.scheme()) {
    return Err(LemmyErrorType::InvalidUrlScheme.into());
  }

  max_length_check(
    url.as_str(),
    URL_MAX_LENGTH,
    LemmyErrorType::UrlLengthOverflow,
  )?;

  Ok(())
}

pub fn is_url_blocked(url: &Url, blocklist: &RegexSet) -> LemmyResult<()> {
  if blocklist.is_match(url.as_str()) {
    return Err(LemmyErrorType::BlockedUrl.into());
  }

  Ok(())
}

/// Check that urls are valid, and also remove the scheme, and uniques
pub fn check_urls_are_valid(urls: &Vec<String>) -> LemmyResult<Vec<String>> {
  let mut parsed_urls = vec![];
  for url in urls {
    parsed_urls.push(build_url_str_without_scheme(url)?);
  }

  let unique_urls = parsed_urls.into_iter().unique().collect();
  Ok(unique_urls)
}

pub fn check_blocking_keywords_are_valid(blocking_keywords: &Vec<String>) -> LemmyResult<()> {
  for keyword in blocking_keywords {
    min_length_check(
      keyword,
      MIN_LENGTH_BLOCKING_KEYWORD,
      LemmyErrorType::BlockKeywordTooShort,
    )?;
    max_length_check(
      keyword,
      MAX_LENGTH_BLOCKING_KEYWORD,
      LemmyErrorType::BlockKeywordTooLong,
    )?;
  }
  check_api_elements_count(blocking_keywords.len())?;
  Ok(())
}

fn build_url_str_without_scheme(url_str: &str) -> LemmyResult<String> {
  // Parse and check for errors
  let mut url = Url::parse(url_str).or_else(|e| {
    if e == ParseError::RelativeUrlWithoutBase {
      Url::parse(&format!("http://{url_str}"))
    } else {
      Err(e)
    }
  })?;

  // Set the scheme to http, then remove the http:// part
  url
    .set_scheme("http")
    .map_err(|_e| LemmyErrorType::InvalidUrl)?;

  let mut out = url
    .to_string()
    .get(7..)
    .ok_or(LemmyErrorType::InvalidUrl)?
    .to_string();

  // Remove trailing / if necessary
  if out.ends_with('/') {
    out.pop();
  }

  Ok(out)
}

// Shorten a string to n chars, being mindful of unicode grapheme
// boundaries
// To understand the difference between chars and graphemes see:
// https://hsivonen.fi/string-length/
fn truncate_for_db(text: &str, len: usize) -> String {
  if text.chars().count() <= len {
    text.to_string()
  } else {
    // Get the char at the desired `len`
    let char_at_len = text
      .char_indices()
      .nth(len)
      .unwrap_or(text.char_indices().last().unwrap_or_default());
    let graphemes: Vec<(usize, _)> = text.grapheme_indices(true).collect();
    let mut index = 0;

    // Walk the string backwards and find the first char within our length
    for idx in (0..graphemes.len()).rev() {
      if let Some(grapheme) = graphemes.get(idx)
        && grapheme.0 < char_at_len.0
      {
        index = idx;
        break;
      }
    }

    let grapheme_at_index = graphemes.get(index).unwrap_or(&(0, ""));
    // The char count of the grapheme at the very end of the range
    let grapheme_at_index_count = grapheme_at_index.1.chars().count();
    // Count the total chars within the selected grapheme range
    let char_sum = graphemes
      .get(0..index)
      .unwrap_or_default()
      .iter()
      .map(|(_, g)| g.chars().count())
      .sum();

    // Get the actual count of chars we need to take from `text`.
    // `take` isn't inclusive, so if the last grapheme can fit we add its char
    // length
    let char_total = if char_sum + grapheme_at_index_count <= len {
      char_sum + grapheme_at_index_count
    } else {
      char_sum
    };

    text.chars().take(char_total).collect::<String>()
  }
}

pub fn truncate_summary(text: &str) -> String {
  truncate_for_db(text, SITE_SUMMARY_MAX_LENGTH)
}

pub fn check_api_elements_count(len: usize) -> LemmyResult<()> {
  if len >= MAX_API_PARAM_ELEMENTS {
    return Err(LemmyErrorType::TooManyItems.into());
  }
  Ok(())
}
#[cfg(test)]
mod tests {

  use crate::{
    error::{LemmyErrorType, LemmyResult},
    utils::validation::{
      BIO_MAX_LENGTH, SITE_NAME_MAX_LENGTH, SITE_SUMMARY_MAX_LENGTH, URL_MAX_LENGTH,
      build_and_check_regex, check_urls_are_valid, is_url_blocked, is_valid_actor_name,
      is_valid_bio_field, is_valid_display_name, is_valid_matrix_id, is_valid_post_title,
      is_valid_url, site_name_length_check, summary_length_check, truncate_for_db,
    },
  };
  use pretty_assertions::assert_eq;
  use url::Url;

  #[test]
  fn regex_checks() {
    assert!(is_valid_post_title("hi").is_err());
    assert!(is_valid_post_title("him").is_ok());
    assert!(is_valid_post_title("  him  ").is_ok());
    assert!(is_valid_post_title("n\n\n\n\nanother").is_err());
    assert!(is_valid_post_title("hello there!\n this is a test.").is_err());
    assert!(is_valid_post_title("hello there! this is a test.").is_ok());
    assert!(is_valid_post_title(("12345".repeat(40) + "x").as_str()).is_err());
    assert!(is_valid_post_title("12345".repeat(40).as_str()).is_ok());
    assert!(is_valid_post_title((("12345".repeat(40)) + "  ").as_str()).is_ok());
  }

  #[test]
  fn test_valid_actor_name() {
    assert!(is_valid_actor_name("Hello_98",).is_ok());
    assert!(is_valid_actor_name("ten",).is_ok());
    assert!(is_valid_actor_name("ุชุฌุฑูŠุจ",).is_ok());
    assert!(is_valid_actor_name("ุชุฌุฑูŠุจ_123",).is_ok());
    assert!(is_valid_actor_name("ะ’ะปะฐะดะธะผะธั€",).is_ok());

    // mixed scripts
    assert!(is_valid_actor_name("ุชุฌุฑูŠุจ_abc",).is_err());
    assert!(is_valid_actor_name("ะ’ะปะฐะด_abc",).is_err());
    // dash
    assert!(is_valid_actor_name("Hello-98",).is_err());
    // too short
    assert!(is_valid_actor_name("a",).is_err());
    // empty
    assert!(is_valid_actor_name("",).is_err());
    // newline
    assert!(
      is_valid_actor_name(
        r"Line1

Line3",
      )
      .is_err()
    );
    assert!(is_valid_actor_name("Line1\nLine3",).is_err());
  }

  #[test]
  fn test_valid_display_name() {
    assert!(is_valid_display_name("hello @there").is_ok());
    assert!(is_valid_display_name("@hello there").is_err());
    assert!(is_valid_display_name("\u{200d}hello").is_err());
    assert!(is_valid_display_name("\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}Name").is_ok());
    assert!(is_valid_display_name("\u{2003}1\u{ffa0}2\u{200d}").is_err());

    // Make sure zero-space with an @ doesn't work
    assert!(is_valid_display_name(&format!("{}@my name is", '\u{200b}')).is_err());
  }

  #[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
    assert!(is_valid_post_title("\u{206a}").is_err()); // invisible chars
    assert!(is_valid_post_title("\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}").is_ok());
  }

  #[test]
  fn test_valid_matrix_id() {
    assert!(is_valid_matrix_id("@dess:matrix.org").is_ok());
    assert!(is_valid_matrix_id("@dess_:matrix.org").is_ok());
    assert!(is_valid_matrix_id("@dess:matrix.org:443").is_ok());
    assert!(is_valid_matrix_id("dess:matrix.org").is_err());
    assert!(is_valid_matrix_id(" @dess:matrix.org").is_err());
    assert!(is_valid_matrix_id("@dess:matrix.org t").is_err());
    assert!(is_valid_matrix_id("@dess:matrix.org t").is_err());
  }

  #[test]
  fn test_valid_site_name() -> LemmyResult<()> {
    let valid_names = [
      (0..SITE_NAME_MAX_LENGTH).map(|_| 'A').collect::<String>(),
      String::from("A"),
    ];
    let invalid_names = [
      (
        &(0..SITE_NAME_MAX_LENGTH + 1)
          .map(|_| 'A')
          .collect::<String>(),
        LemmyErrorType::SiteNameLengthOverflow,
      ),
      (&String::new(), LemmyErrorType::SiteNameRequired),
    ];

    valid_names.iter().for_each(|valid_name| {
      assert!(
        site_name_length_check(valid_name).is_ok(),
        "Expected {} of length {} to be Ok.",
        valid_name,
        valid_name.len()
      )
    });

    invalid_names
      .iter()
      .for_each(|(invalid_name, expected_err)| {
        let result = site_name_length_check(invalid_name);

        assert!(result.is_err());
        assert!(
          result.is_err_and(|e| e.error_type.eq(&expected_err.clone())),
          "Testing {}, expected error {}",
          invalid_name,
          expected_err
        );
      });
    Ok(())
  }

  #[test]
  fn test_valid_bio() {
    assert!(is_valid_bio_field(&(0..BIO_MAX_LENGTH).map(|_| 'A').collect::<String>()).is_ok());

    let invalid_result =
      is_valid_bio_field(&(0..BIO_MAX_LENGTH + 1).map(|_| 'A').collect::<String>());

    assert!(
      invalid_result.is_err()
        && invalid_result.is_err_and(|e| e.error_type.eq(&LemmyErrorType::BioLengthOverflow))
    );
  }

  #[test]
  fn test_valid_site_description() {
    assert!(
      summary_length_check(
        &(0..SITE_SUMMARY_MAX_LENGTH)
          .map(|_| 'A')
          .collect::<String>()
      )
      .is_ok()
    );

    let invalid_result = summary_length_check(
      &(0..SITE_SUMMARY_MAX_LENGTH + 1)
        .map(|_| 'A')
        .collect::<String>(),
    );

    assert!(
      invalid_result.is_err()
        && invalid_result.is_err_and(|e| e
          .error_type
          .eq(&LemmyErrorType::SiteDescriptionLengthOverflow))
    );
  }

  #[test]
  fn test_valid_slur_regex() -> LemmyResult<()> {
    let valid_regex = Some("(foo|bar)");
    build_and_check_regex(valid_regex)?;

    let missing_regex = None;
    let match_none = build_and_check_regex(missing_regex)?;
    assert!(!match_none.is_match(""));
    assert!(!match_none.is_match("a"));

    let empty = Some("");
    let match_none = build_and_check_regex(empty)?;
    assert!(!match_none.is_match(""));
    assert!(!match_none.is_match("a"));

    Ok(())
  }

  #[test]
  fn test_too_permissive_slur_regex() {
    let match_everything_regexes = [
      (Some("["), LemmyErrorType::InvalidRegex),
      (Some("(foo|bar|)"), LemmyErrorType::PermissiveRegex),
      (Some(".*"), LemmyErrorType::PermissiveRegex),
    ];

    match_everything_regexes
      .into_iter()
      .for_each(|(regex_str, expected_err)| {
        let result = build_and_check_regex(regex_str);

        assert!(result.is_err());
        assert!(
          result.is_err_and(|e| e.error_type.eq(&expected_err.clone())),
          "Testing regex {:?}, expected error {}",
          regex_str,
          expected_err
        );
      });
  }

  #[test]
  fn test_check_url_valid() -> LemmyResult<()> {
    assert!(is_valid_url(&Url::parse("http://example.com")?).is_ok());
    assert!(is_valid_url(&Url::parse("https://example.com")?).is_ok());
    assert!(is_valid_url(&Url::parse("https://example.com")?).is_ok());
    assert!(
      is_valid_url(&Url::parse("ftp://example.com")?)
        .is_err_and(|e| e.error_type.eq(&LemmyErrorType::InvalidUrlScheme))
    );
    assert!(
      is_valid_url(&Url::parse("javascript:void")?)
        .is_err_and(|e| e.error_type.eq(&LemmyErrorType::InvalidUrlScheme))
    );

    let magnet_link = "magnet:?xt=urn:btih:4b390af3891e323778959d5abfff4b726510f14c&dn=Ravel%20Complete%20Piano%20Sheet%20Music%20-%20Public%20Domain&tr=udp%3A%2F%2Fopen.tracker.cl%3A1337%2Fannounce";
    assert!(is_valid_url(&Url::parse(magnet_link)?).is_ok());

    // Also make sure the length overflow hits an error
    let mut long_str = "http://example.com/test=".to_string();
    for _ in 1..URL_MAX_LENGTH {
      long_str.push('X');
    }
    let long_url = Url::parse(&long_str)?;
    assert!(
      is_valid_url(&long_url).is_err_and(|e| e.error_type.eq(&LemmyErrorType::UrlLengthOverflow))
    );

    Ok(())
  }

  #[test]
  fn test_url_block() -> LemmyResult<()> {
    let set = regex::RegexSet::new(vec![
      r"(https://)?example\.org/page/to/article",
      r"(https://)?example\.net/?",
      r"(https://)?example\.com/?",
    ])?;

    assert!(is_url_blocked(&Url::parse("https://example.blog")?, &set).is_ok());

    assert!(is_url_blocked(&Url::parse("https://example.org")?, &set).is_ok());

    assert!(is_url_blocked(&Url::parse("https://example.com")?, &set).is_err());

    Ok(())
  }

  #[test]
  fn test_url_parsed() -> LemmyResult<()> {
    // Make sure the scheme is removed, and uniques also
    assert_eq!(
      &check_urls_are_valid(&vec![
        "example.com".to_string(),
        "http://example.com".to_string(),
        "https://example.com".to_string(),
        "https://example.com/test?q=test2&q2=test3#test4".to_string(),
      ])?,
      &vec![
        "example.com".to_string(),
        "example.com/test?q=test2&q2=test3#test4".to_string()
      ],
    );

    assert!(check_urls_are_valid(&vec!["https://example .com".to_string()]).is_err());
    Ok(())
  }

  #[test]
  fn test_truncate() -> LemmyResult<()> {
    assert_eq!("Hell", truncate_for_db("Hello", 4));
    assert_eq!("word", truncate_for_db("word", 10));
    assert_eq!("Wales: ", truncate_for_db("Wales: ๐Ÿด๓ ง๓ ข๓ ท๓ ฌ๓ ณ๓ ฟ", 10));
    assert_eq!("Wales: ๐Ÿด๓ ง๓ ข๓ ท๓ ฌ๓ ณ๓ ฟ", truncate_for_db("Wales: ๐Ÿด๓ ง๓ ข๓ ท๓ ฌ๓ ณ๓ ฟ", 14));
    assert_eq!("itโ€™s", truncate_for_db("itโ€™s like this", 4));
    assert_eq!("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ150", truncate_for_db("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ150๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ", 11));

    Ok(())
  }
}