]> Untitled Git - lemmy.git/blob - server/src/lib.rs
Merge branch 'reorg' into dev
[lemmy.git] / server / src / lib.rs
1 #[macro_use] pub extern crate strum_macros;
2 #[macro_use] pub extern crate lazy_static;
3 #[macro_use] pub extern crate failure;
4 #[macro_use] pub extern crate diesel;
5 pub extern crate dotenv;
6 pub extern crate chrono;
7 pub extern crate serde;
8 pub extern crate serde_json;
9 pub extern crate actix;
10 pub extern crate actix_web;
11 pub extern crate rand;
12 pub extern crate strum;
13 pub extern crate jsonwebtoken;
14 pub extern crate bcrypt;
15 pub extern crate regex;
16
17 pub mod schema;
18 pub mod api;
19 pub mod apub;
20 pub mod db;
21 pub mod websocket;
22
23 use dotenv::dotenv;
24 use std::env;
25 use regex::Regex;
26 use chrono::{DateTime, NaiveDateTime, Utc};
27
28 pub struct Settings {
29   db_url: String,
30   hostname: String,
31   jwt_secret: String,
32 }
33
34 impl Settings {
35   fn get() -> Self {
36     dotenv().ok();
37     Settings {
38       db_url: env::var("DATABASE_URL")
39         .expect("DATABASE_URL must be set"),
40         hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
41         jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
42     }
43   }
44   fn api_endpoint(&self) -> String {
45     format!("{}/api/v1", self.hostname)
46   }
47 }
48
49 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
50   DateTime::<Utc>::from_utc(ndt, Utc)
51 }
52
53 pub fn naive_now() -> NaiveDateTime {
54   chrono::prelude::Utc::now().naive_utc()
55 }
56
57 pub fn naive_from_unix(time: i64)  ->  NaiveDateTime {
58   NaiveDateTime::from_timestamp(time, 0)
59 }
60
61 pub fn is_email_regex(test: &str) -> bool {
62   EMAIL_REGEX.is_match(test)
63 }
64
65 pub fn remove_slurs(test: &str) -> String {
66   SLUR_REGEX.replace_all(test, "*removed*").to_string()
67 }
68
69 pub fn has_slurs(test: &str) -> bool {
70   SLUR_REGEX.is_match(test)
71 }
72
73 #[cfg(test)]
74 mod tests {
75   use {Settings, is_email_regex, remove_slurs, has_slurs};
76   #[test]
77   fn test_api() {
78     assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
79   }
80
81   #[test] fn test_email() {
82     assert!(is_email_regex("gush@gmail.com"));
83     assert!(!is_email_regex("nada_neutho"));
84   } 
85
86   #[test] fn test_slur_filter() {
87     let test = "coons test dindu ladyboy tranny. This is a bunch of other safe text.".to_string();
88     let slur_free = "No slurs here";
89     assert_eq!(remove_slurs(&test), "*removed* test *removed* *removed* *removed*. This is a bunch of other safe text.".to_string());
90     assert!(has_slurs(&test));
91     assert!(!has_slurs(slur_free));
92   } 
93
94 }
95
96 lazy_static! {
97   static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
98   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?))").unwrap();
99 }