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