]> Untitled Git - lemmy.git/blob - crates/utils/src/utils.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / utils / src / utils.rs
1 use crate::{error::LemmyError, location_info, IpAddr};
2 use actix_web::dev::ConnectionInfo;
3 use anyhow::Context;
4 use chrono::{DateTime, FixedOffset, NaiveDateTime};
5 use itertools::Itertools;
6 use once_cell::sync::Lazy;
7 use rand::{distributions::Alphanumeric, thread_rng, Rng};
8 use regex::{Regex, RegexBuilder};
9 use url::Url;
10
11 static MENTIONS_REGEX: Lazy<Regex> = Lazy::new(|| {
12   Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex")
13 });
14 static VALID_ACTOR_NAME_REGEX: Lazy<Regex> =
15   Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"));
16 static VALID_POST_TITLE_REGEX: Lazy<Regex> =
17   Lazy::new(|| Regex::new(r".*\S{3,}.*").expect("compile regex"));
18 static VALID_MATRIX_ID_REGEX: Lazy<Regex> = Lazy::new(|| {
19   Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex")
20 });
21 // taken from https://en.wikipedia.org/wiki/UTM_parameters
22 static CLEAN_URL_PARAMS_REGEX: Lazy<Regex> = Lazy::new(|| {
23   Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$")
24     .expect("compile regex")
25 });
26
27 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
28   NaiveDateTime::from_timestamp(time, 0)
29 }
30
31 pub fn convert_datetime(datetime: NaiveDateTime) -> DateTime<FixedOffset> {
32   DateTime::<FixedOffset>::from_utc(datetime, FixedOffset::east(0))
33 }
34
35 pub fn remove_slurs(test: &str, slur_regex: &Option<Regex>) -> String {
36   if let Some(slur_regex) = slur_regex {
37     slur_regex.replace_all(test, "*removed*").to_string()
38   } else {
39     test.to_string()
40   }
41 }
42
43 pub(crate) fn slur_check<'a>(
44   test: &'a str,
45   slur_regex: &'a Option<Regex>,
46 ) -> Result<(), Vec<&'a str>> {
47   if let Some(slur_regex) = slur_regex {
48     let mut matches: Vec<&str> = slur_regex.find_iter(test).map(|mat| mat.as_str()).collect();
49
50     // Unique
51     matches.sort_unstable();
52     matches.dedup();
53
54     if matches.is_empty() {
55       Ok(())
56     } else {
57       Err(matches)
58     }
59   } else {
60     Ok(())
61   }
62 }
63
64 pub fn build_slur_regex(regex_str: Option<&str>) -> Option<Regex> {
65   regex_str.map(|slurs| {
66     RegexBuilder::new(slurs)
67       .case_insensitive(true)
68       .build()
69       .expect("compile regex")
70   })
71 }
72
73 pub fn check_slurs(text: &str, slur_regex: &Option<Regex>) -> Result<(), LemmyError> {
74   if let Err(slurs) = slur_check(text, slur_regex) {
75     Err(LemmyError::from_error_message(
76       anyhow::anyhow!("{}", slurs_vec_to_str(slurs)),
77       "slurs",
78     ))
79   } else {
80     Ok(())
81   }
82 }
83
84 pub fn check_slurs_opt(
85   text: &Option<String>,
86   slur_regex: &Option<Regex>,
87 ) -> Result<(), LemmyError> {
88   match text {
89     Some(t) => check_slurs(t, slur_regex),
90     None => Ok(()),
91   }
92 }
93
94 pub(crate) fn slurs_vec_to_str(slurs: Vec<&str>) -> String {
95   let start = "No slurs - ";
96   let combined = &slurs.join(", ");
97   [start, combined].concat()
98 }
99
100 /// Make sure if applications are required, that there is an application questionnaire
101 pub fn check_application_question(
102   application_question: &Option<Option<String>>,
103   require_application: &Option<bool>,
104 ) -> Result<(), LemmyError> {
105   if require_application.unwrap_or(false)
106     && application_question.as_ref().unwrap_or(&None).is_none()
107   {
108     Err(LemmyError::from_message("application_question_required"))
109   } else {
110     Ok(())
111   }
112 }
113
114 pub fn generate_random_string() -> String {
115   thread_rng()
116     .sample_iter(&Alphanumeric)
117     .map(char::from)
118     .take(30)
119     .collect()
120 }
121
122 pub fn markdown_to_html(text: &str) -> String {
123   comrak::markdown_to_html(text, &comrak::ComrakOptions::default())
124 }
125
126 // TODO nothing is done with community / group webfingers yet, so just ignore those for now
127 #[derive(Clone, PartialEq, Eq, Hash)]
128 pub struct MentionData {
129   pub name: String,
130   pub domain: String,
131 }
132
133 impl MentionData {
134   pub fn is_local(&self, hostname: &str) -> bool {
135     hostname.eq(&self.domain)
136   }
137   pub fn full_name(&self) -> String {
138     format!("@{}@{}", &self.name, &self.domain)
139   }
140 }
141
142 pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
143   let mut out: Vec<MentionData> = Vec::new();
144   for caps in MENTIONS_REGEX.captures_iter(text) {
145     out.push(MentionData {
146       name: caps["name"].to_string(),
147       domain: caps["domain"].to_string(),
148     });
149   }
150   out.into_iter().unique().collect()
151 }
152
153 fn has_newline(name: &str) -> bool {
154   name.contains('\n')
155 }
156
157 pub fn is_valid_actor_name(name: &str, actor_name_max_length: usize) -> bool {
158   name.chars().count() <= actor_name_max_length
159     && VALID_ACTOR_NAME_REGEX.is_match(name)
160     && !has_newline(name)
161 }
162
163 // Can't do a regex here, reverse lookarounds not supported
164 pub fn is_valid_display_name(name: &str, actor_name_max_length: usize) -> bool {
165   !name.starts_with('@')
166     && !name.starts_with('\u{200b}')
167     && name.chars().count() >= 3
168     && name.chars().count() <= actor_name_max_length
169     && !has_newline(name)
170 }
171
172 pub fn is_valid_matrix_id(matrix_id: &str) -> bool {
173   VALID_MATRIX_ID_REGEX.is_match(matrix_id) && !has_newline(matrix_id)
174 }
175
176 pub fn is_valid_post_title(title: &str) -> bool {
177   VALID_POST_TITLE_REGEX.is_match(title) && !has_newline(title)
178 }
179
180 pub fn get_ip(conn_info: &ConnectionInfo) -> IpAddr {
181   IpAddr(
182     conn_info
183       .realip_remote_addr()
184       .unwrap_or("127.0.0.1:12345")
185       .split(':')
186       .next()
187       .unwrap_or("127.0.0.1")
188       .to_string(),
189   )
190 }
191
192 pub fn clean_url_params(url: &Url) -> Url {
193   let mut url_out = url.to_owned();
194   if url.query().is_some() {
195     let new_query = url
196       .query_pairs()
197       .filter(|q| !CLEAN_URL_PARAMS_REGEX.is_match(&q.0))
198       .map(|q| format!("{}={}", q.0, q.1))
199       .join("&");
200     url_out.set_query(Some(&new_query));
201   }
202   url_out
203 }
204
205 pub fn generate_domain_url(actor_id: &Url) -> Result<String, LemmyError> {
206   Ok(actor_id.host_str().context(location_info!())?.to_string())
207 }
208
209 #[cfg(test)]
210 mod tests {
211   use crate::utils::{clean_url_params, is_valid_post_title};
212   use url::Url;
213
214   #[test]
215   fn test_clean_url_params() {
216     let url = Url::parse("https://example.com/path/123?utm_content=buffercf3b2&utm_medium=social&username=randomuser&id=123").unwrap();
217     let cleaned = clean_url_params(&url);
218     let expected = Url::parse("https://example.com/path/123?username=randomuser&id=123").unwrap();
219     assert_eq!(expected.to_string(), cleaned.to_string());
220
221     let url = Url::parse("https://example.com/path/123").unwrap();
222     let cleaned = clean_url_params(&url);
223     assert_eq!(url.to_string(), cleaned.to_string());
224   }
225
226   #[test]
227   fn regex_checks() {
228     assert!(!is_valid_post_title("hi"));
229     assert!(is_valid_post_title("him"));
230     assert!(!is_valid_post_title("n\n\n\n\nanother"));
231     assert!(!is_valid_post_title("hello there!\n this is a test."));
232     assert!(is_valid_post_title("hello there! this is a test."));
233   }
234 }