]> Untitled Git - lemmy.git/blob - server/src/lib.rs
Adding to slur filter.
[lemmy.git] / server / src / lib.rs
1 #![recursion_limit = "512"]
2 #[macro_use]
3 pub extern crate strum_macros;
4 #[macro_use]
5 pub extern crate lazy_static;
6 #[macro_use]
7 pub extern crate failure;
8 #[macro_use]
9 pub extern crate diesel;
10 pub extern crate actix;
11 pub extern crate actix_web;
12 pub extern crate bcrypt;
13 pub extern crate chrono;
14 pub extern crate dotenv;
15 pub extern crate jsonwebtoken;
16 pub extern crate rand;
17 pub extern crate regex;
18 pub extern crate serde;
19 pub extern crate serde_json;
20 pub extern crate strum;
21
22 pub mod api;
23 pub mod apub;
24 pub mod db;
25 pub mod schema;
26 pub mod websocket;
27
28 use chrono::{DateTime, NaiveDateTime, Utc};
29 use dotenv::dotenv;
30 use regex::Regex;
31 use std::env;
32
33 pub struct Settings {
34   db_url: String,
35   hostname: String,
36   jwt_secret: String,
37 }
38
39 impl Settings {
40   fn get() -> Self {
41     dotenv().ok();
42     Settings {
43       db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
44       hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
45       jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
46     }
47   }
48   fn api_endpoint(&self) -> String {
49     format!("{}/api/v1", self.hostname)
50   }
51 }
52
53 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
54   DateTime::<Utc>::from_utc(ndt, Utc)
55 }
56
57 pub fn naive_now() -> NaiveDateTime {
58   chrono::prelude::Utc::now().naive_utc()
59 }
60
61 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
62   NaiveDateTime::from_timestamp(time, 0)
63 }
64
65 pub fn is_email_regex(test: &str) -> bool {
66   EMAIL_REGEX.is_match(test)
67 }
68
69 pub fn remove_slurs(test: &str) -> String {
70   SLUR_REGEX.replace_all(test, "*removed*").to_string()
71 }
72
73 pub fn has_slurs(test: &str) -> bool {
74   SLUR_REGEX.is_match(test)
75 }
76
77 #[cfg(test)]
78 mod tests {
79   use crate::{has_slurs, is_email_regex, remove_slurs, Settings};
80   #[test]
81   fn test_api() {
82     assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
83   }
84
85   #[test]
86   fn test_email() {
87     assert!(is_email_regex("gush@gmail.com"));
88     assert!(!is_email_regex("nada_neutho"));
89   }
90
91   #[test]
92   fn test_slur_filter() {
93     let test = "coons test dindu ladyboy tranny retardeds. This is a bunch of other safe text.".to_string();
94     let slur_free = "No slurs here";
95     assert_eq!(
96       remove_slurs(&test),
97       "*removed* test *removed* *removed* *removed* *removed*. This is a bunch of other safe text."
98         .to_string()
99     );
100     assert!(has_slurs(&test));
101     assert!(!has_slurs(slur_free));
102   }
103
104 }
105
106 lazy_static! {
107   static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
108   static ref SLUR_REGEX: Regex = Regex::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|\bnig(\b|g?(a|er)?s?)\b|dindu(s?)|mudslime?s?|kikes?|mongoloids?|towel\s*heads?|\bspi(c|k)s?\b|\bchinks?|niglets?|beaners?|\bnips?\b|\bcoons?\b|jungle\s*bunn(y|ies?)|jigg?aboo?s?|\bpakis?\b|rag\s*heads?|gooks?|cunts?|bitch(es|ing|y)?|puss(y|ies?)|twats?|feminazis?|whor(es?|ing)|\bslut(s|t?y)?|\btrann?(y|ies?)|ladyboy(s?)|\b(b|re|r)tard(ed)?s?)").unwrap();
109 }