1 #![recursion_limit = "512"]
3 pub extern crate strum_macros;
5 pub extern crate lazy_static;
7 pub extern crate failure;
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;
28 use chrono::{DateTime, NaiveDateTime, Utc};
37 rate_limit_message: i32,
38 rate_limit_message_per_second: i32,
40 rate_limit_post_per_second: i32,
41 rate_limit_register: i32,
42 rate_limit_register_per_second: i32,
49 db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
50 hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
51 jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
52 rate_limit_message: env::var("RATE_LIMIT_MESSAGE")
53 .unwrap_or("30".to_string())
56 rate_limit_message_per_second: env::var("RATE_LIMIT_MESSAGE_PER_SECOND")
57 .unwrap_or("60".to_string())
60 rate_limit_post: env::var("RATE_LIMIT_POST")
61 .unwrap_or("3".to_string())
64 rate_limit_post_per_second: env::var("RATE_LIMIT_POST_PER_SECOND")
65 .unwrap_or("600".to_string())
68 rate_limit_register: env::var("RATE_LIMIT_REGISTER")
69 .unwrap_or("1".to_string())
72 rate_limit_register_per_second: env::var("RATE_LIMIT_REGISTER_PER_SECOND")
73 .unwrap_or("3600".to_string())
78 fn api_endpoint(&self) -> String {
79 format!("{}/api/v1", self.hostname)
83 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
84 DateTime::<Utc>::from_utc(ndt, Utc)
87 pub fn naive_now() -> NaiveDateTime {
88 chrono::prelude::Utc::now().naive_utc()
91 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
92 NaiveDateTime::from_timestamp(time, 0)
95 pub fn is_email_regex(test: &str) -> bool {
96 EMAIL_REGEX.is_match(test)
99 pub fn remove_slurs(test: &str) -> String {
100 SLUR_REGEX.replace_all(test, "*removed*").to_string()
103 pub fn has_slurs(test: &str) -> bool {
104 SLUR_REGEX.is_match(test)
109 use crate::{has_slurs, is_email_regex, remove_slurs, Settings};
112 assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
117 assert!(is_email_regex("gush@gmail.com"));
118 assert!(!is_email_regex("nada_neutho"));
122 fn test_slur_filter() {
124 "coons test dindu ladyboy tranny retardeds. This is a bunch of other safe text.".to_string();
125 let slur_free = "No slurs here";
128 "*removed* test *removed* *removed* *removed* *removed*. This is a bunch of other safe text."
131 assert!(has_slurs(&test));
132 assert!(!has_slurs(slur_free));
137 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
138 static ref SLUR_REGEX: Regex = Regex::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|nig(\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();