relay: fix processing of unicode tags

This commit is contained in:
Astro 2023-06-23 18:49:12 +02:00
parent ed8d2c9e06
commit eda7568ffe

View file

@ -54,10 +54,19 @@ impl Post<'_> {
// Distribute hashtags that end in a date to // Distribute hashtags that end in a date to
// followers of the hashtag with the date // followers of the hashtag with the date
// stripped. Example: #dd1302 -> #dd // stripped. Example: #dd1302 -> #dd
let first_trailing_digit = s.rfind(|c| ! char::is_digit(c, 10)) let mut first_trailing_digit = 0;
.map(|first_trailing_digit| first_trailing_digit + 1) let mut scanning_digits = false;
.unwrap_or(s.len()); for (pos, c) in s.char_indices() {
if first_trailing_digit > 0 && first_trailing_digit < s.len() { if char::is_digit(c, 10) {
if ! scanning_digits {
first_trailing_digit = pos;
scanning_digits = true;
}
} else {
scanning_digits = false;
}
}
if scanning_digits && first_trailing_digit > 0 {
let tag = &s[..first_trailing_digit]; let tag = &s[..first_trailing_digit];
let actor2 = actor::ActorKind::from_tag(tag); let actor2 = actor::ActorKind::from_tag(tag);
vec![actor1, actor2] vec![actor1, actor2]
@ -291,4 +300,19 @@ mod test {
assert_eq!(kinds.next(), Some(ActorKind::TagRelay("dd".to_string()))); assert_eq!(kinds.next(), Some(ActorKind::TagRelay("dd".to_string())));
assert_eq!(kinds.next(), None); assert_eq!(kinds.next(), None);
} }
#[test]
fn post_relay_kind_jp() {
let post = Post {
url: Some("http://example.com/post/1"),
uri: "http://example.com/post/1",
tags: Some(vec![Tag {
name: "スコティッシュ・フォールド・ロングヘアー",
}]),
};
let mut kinds = post.relay_target_kinds();
assert_eq!(kinds.next(), Some(ActorKind::InstanceRelay("example.com".to_string())));
assert_eq!(kinds.next(), Some(ActorKind::TagRelay("sukoteitusiyuhuorudoronguhea".to_string())));
assert_eq!(kinds.next(), None);
}
} }