1 use itertools::Itertools;
2 use once_cell::sync::Lazy;
5 static MENTIONS_REGEX: Lazy<Regex> = Lazy::new(|| {
6 Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex")
8 // TODO nothing is done with community / group webfingers yet, so just ignore those for now
9 #[derive(Clone, PartialEq, Eq, Hash)]
10 pub struct MentionData {
16 pub fn is_local(&self, hostname: &str) -> bool {
17 hostname.eq(&self.domain)
19 pub fn full_name(&self) -> String {
20 format!("@{}@{}", &self.name, &self.domain)
24 pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
25 let mut out: Vec<MentionData> = Vec::new();
26 for caps in MENTIONS_REGEX.captures_iter(text) {
27 out.push(MentionData {
28 name: caps["name"].to_string(),
29 domain: caps["domain"].to_string(),
32 out.into_iter().unique().collect()
37 use crate::utils::mention::scrape_text_for_mentions;
40 fn test_mentions_regex() {
41 let text = "Just read a great blog post by [@tedu@honk.teduangst.com](/u/test). And another by !test_community@fish.teduangst.com . Another [@lemmy@lemmy-alpha:8540](/u/fish)";
42 let mentions = scrape_text_for_mentions(text);
44 assert_eq!(mentions[0].name, "tedu".to_string());
45 assert_eq!(mentions[0].domain, "honk.teduangst.com".to_string());
46 assert_eq!(mentions[1].domain, "lemmy-alpha:8540".to_string());