From e207dda2fed450807bb838d7b394b99ca59e9719 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 3 Mar 2023 02:19:48 +0100 Subject: [PATCH] actor: normalize unicode away for TagRelay --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/actor.rs | 10 ++++++++++ src/main.rs | 4 ++-- src/relay.rs | 4 ++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fa0414e..8946a27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,6 +237,7 @@ dependencies = [ "axum-extra", "axum-macros", "chrono", + "deunicode", "eventsource-stream", "futures", "http", @@ -417,6 +418,12 @@ dependencies = [ "syn", ] +[[package]] +name = "deunicode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c1bba4f227a4a53d12b653f50ca7bf10c9119ae2aba56aff9e0338b5c98f36a" + [[package]] name = "digest" version = "0.10.6" diff --git a/Cargo.toml b/Cargo.toml index 98893a2..e4527e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,4 @@ systemd = "0.10" metrics = "0.20" metrics-util = "0.14" metrics-exporter-prometheus = "0.11" +deunicode = "1.3" diff --git a/src/actor.rs b/src/actor.rs index b2e6c63..9a759f2 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1,4 +1,5 @@ use std::sync::Arc; +use deunicode::deunicode; use sigh::{PublicKey, Key}; use crate::activitypub; @@ -9,6 +10,15 @@ pub enum ActorKind { InstanceRelay(String), } +impl ActorKind { + pub fn from_tag(tag: &str) -> Self { + let tag = deunicode(tag) + .to_lowercase() + .replace(char::is_whitespace, ""); + ActorKind::TagRelay(tag) + } +} + #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Actor { pub host: Arc, diff --git a/src/main.rs b/src/main.rs index 43e035e..59b7856 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,7 @@ async fn get_tag_actor( track_request("GET", "actor", "tag"); let target = actor::Actor { host: state.hostname.clone(), - kind: actor::ActorKind::TagRelay(tag.to_lowercase()), + kind: actor::ActorKind::from_tag(&tag), }; target.as_activitypub(&state.pub_key) .into_response() @@ -123,7 +123,7 @@ async fn post_tag_relay( ) -> Response { let target = actor::Actor { host: state.hostname.clone(), - kind: actor::ActorKind::TagRelay(tag.to_lowercase()), + kind: actor::ActorKind::from_tag(&tag), }; post_relay(state, endpoint, target).await } diff --git a/src/relay.rs b/src/relay.rs index d402059..351b209 100644 --- a/src/relay.rs +++ b/src/relay.rs @@ -30,7 +30,7 @@ impl Post<'_> { vec![], Some(tags) => tags.iter() - .map(|tag| tag.name.to_lowercase()) + .map(|tag| tag.name.to_string()) .collect() } } @@ -42,7 +42,7 @@ impl Post<'_> { .chain( self.tags() .into_iter() - .map(actor::ActorKind::TagRelay) + .map(|ref s| actor::ActorKind::from_tag(s)) ) }