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