]> Untitled Git - lemmy.git/blob - crates/utils/src/utils.rs
Rewrite fetcher (#1792)
[lemmy.git] / crates / utils / src / utils.rs
1 use crate::{settings::structs::Settings, ApiError, IpAddr};
2 use actix_web::dev::ConnectionInfo;
3 use chrono::{DateTime, FixedOffset, NaiveDateTime};
4 use itertools::Itertools;
5 use rand::{distributions::Alphanumeric, thread_rng, Rng};
6 use regex::{Regex, RegexBuilder};
7 use url::Url;
8
9 lazy_static! {
10   static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").expect("compile regex");
11   static ref SLUR_REGEX: Regex = {
12     let mut slurs = r"(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)".to_string();
13     if let Some(additional_slurs) = Settings::get().additional_slurs {
14         slurs.push('|');
15         slurs.push_str(&additional_slurs);
16     };
17     RegexBuilder::new(&slurs).case_insensitive(true).build().expect("compile regex")
18   };
19
20
21   static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").expect("compile regex");
22   // TODO keep this old one, it didn't work with port well tho
23   // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex");
24   static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex");
25   static ref VALID_ACTOR_NAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex");
26   static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex");
27   static ref VALID_MATRIX_ID_REGEX: Regex = Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex");
28   // taken from https://en.wikipedia.org/wiki/UTM_parameters
29   static ref CLEAN_URL_PARAMS_REGEX: Regex = Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$").expect("compile regex");
30 }
31
32 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
33   NaiveDateTime::from_timestamp(time, 0)
34 }
35
36 pub fn convert_datetime(datetime: NaiveDateTime) -> DateTime<FixedOffset> {
37   DateTime::<FixedOffset>::from_utc(datetime, FixedOffset::east(0))
38 }
39
40 pub fn remove_slurs(test: &str) -> String {
41   SLUR_REGEX.replace_all(test, "*removed*").to_string()
42 }
43
44 pub(crate) fn slur_check(test: &str) -> Result<(), Vec<&str>> {
45   let mut matches: Vec<&str> = SLUR_REGEX.find_iter(test).map(|mat| mat.as_str()).collect();
46
47   // Unique
48   matches.sort_unstable();
49   matches.dedup();
50
51   if matches.is_empty() {
52     Ok(())
53   } else {
54     Err(matches)
55   }
56 }
57
58 pub fn check_slurs(text: &str) -> Result<(), ApiError> {
59   if let Err(slurs) = slur_check(text) {
60     Err(ApiError::err(&slurs_vec_to_str(slurs)))
61   } else {
62     Ok(())
63   }
64 }
65
66 pub fn check_slurs_opt(text: &Option<String>) -> Result<(), ApiError> {
67   match text {
68     Some(t) => check_slurs(t),
69     None => Ok(()),
70   }
71 }
72
73 pub(crate) fn slurs_vec_to_str(slurs: Vec<&str>) -> String {
74   let start = "No slurs - ";
75   let combined = &slurs.join(", ");
76   [start, combined].concat()
77 }
78
79 pub fn generate_random_string() -> String {
80   thread_rng()
81     .sample_iter(&Alphanumeric)
82     .map(char::from)
83     .take(30)
84     .collect()
85 }
86
87 pub fn markdown_to_html(text: &str) -> String {
88   comrak::markdown_to_html(text, &comrak::ComrakOptions::default())
89 }
90
91 // TODO nothing is done with community / group webfingers yet, so just ignore those for now
92 #[derive(Clone, PartialEq, Eq, Hash)]
93 pub struct MentionData {
94   pub name: String,
95   pub domain: String,
96 }
97
98 impl MentionData {
99   pub fn is_local(&self) -> bool {
100     Settings::get().hostname.eq(&self.domain)
101   }
102   pub fn full_name(&self) -> String {
103     format!("@{}@{}", &self.name, &self.domain)
104   }
105 }
106
107 pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
108   let mut out: Vec<MentionData> = Vec::new();
109   for caps in MENTIONS_REGEX.captures_iter(text) {
110     out.push(MentionData {
111       name: caps["name"].to_string(),
112       domain: caps["domain"].to_string(),
113     });
114   }
115   out.into_iter().unique().collect()
116 }
117
118 pub fn is_valid_actor_name(name: &str) -> bool {
119   name.chars().count() <= Settings::get().actor_name_max_length
120     && VALID_ACTOR_NAME_REGEX.is_match(name)
121 }
122
123 // Can't do a regex here, reverse lookarounds not supported
124 pub fn is_valid_display_name(name: &str) -> bool {
125   !name.starts_with('@')
126     && !name.starts_with('\u{200b}')
127     && name.chars().count() >= 3
128     && name.chars().count() <= Settings::get().actor_name_max_length
129 }
130
131 pub fn is_valid_matrix_id(matrix_id: &str) -> bool {
132   VALID_MATRIX_ID_REGEX.is_match(matrix_id)
133 }
134
135 pub fn is_valid_post_title(title: &str) -> bool {
136   VALID_POST_TITLE_REGEX.is_match(title)
137 }
138
139 pub fn get_ip(conn_info: &ConnectionInfo) -> IpAddr {
140   IpAddr(
141     conn_info
142       .realip_remote_addr()
143       .unwrap_or("127.0.0.1:12345")
144       .split(':')
145       .next()
146       .unwrap_or("127.0.0.1")
147       .to_string(),
148   )
149 }
150
151 pub fn clean_url_params(mut url: Url) -> Url {
152   if url.query().is_some() {
153     let new_query = url
154       .query_pairs()
155       .filter(|q| !CLEAN_URL_PARAMS_REGEX.is_match(&q.0))
156       .map(|q| format!("{}={}", q.0, q.1))
157       .join("&");
158     url.set_query(Some(&new_query));
159   }
160   url
161 }
162
163 #[cfg(test)]
164 mod tests {
165   use crate::utils::clean_url_params;
166   use url::Url;
167
168   #[test]
169   fn test_clean_url_params() {
170     let url = Url::parse("https://example.com/path/123?utm_content=buffercf3b2&utm_medium=social&username=randomuser&id=123").unwrap();
171     let cleaned = clean_url_params(url);
172     let expected = Url::parse("https://example.com/path/123?username=randomuser&id=123").unwrap();
173     assert_eq!(expected.to_string(), cleaned.to_string());
174
175     let url = Url::parse("https://example.com/path/123").unwrap();
176     let cleaned = clean_url_params(url.clone());
177     assert_eq!(url.to_string(), cleaned.to_string());
178   }
179 }