2 pub extern crate diesel;
3 pub extern crate dotenv;
4 pub extern crate chrono;
5 pub extern crate serde;
6 pub extern crate serde_json;
7 pub extern crate actix;
8 pub extern crate actix_web;
10 pub extern crate strum;
11 pub extern crate jsonwebtoken;
12 pub extern crate bcrypt;
13 pub extern crate regex;
14 #[macro_use] pub extern crate strum_macros;
15 #[macro_use] pub extern crate lazy_static;
16 #[macro_use] extern crate failure;
21 pub mod websocket_server;
24 use diesel::pg::PgConnection;
25 use diesel::result::Error;
29 use serde::{Deserialize, Serialize};
30 use chrono::{DateTime, NaiveDateTime, Utc};
33 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
34 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;
35 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;
36 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
39 pub trait Followable<T> {
40 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
41 fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
44 pub trait Joinable<T> {
45 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
46 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
49 pub trait Likeable<T> {
50 fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
51 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
52 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
55 pub trait Bannable<T> {
56 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
57 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
60 pub trait Saveable<T> {
61 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
62 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
65 pub trait Readable<T> {
66 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
67 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
70 pub fn establish_connection() -> PgConnection {
71 let db_url = Settings::get().db_url;
72 PgConnection::establish(&db_url)
73 .expect(&format!("Error connecting to {}", db_url))
86 db_url: env::var("DATABASE_URL")
87 .expect("DATABASE_URL must be set"),
88 hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
89 jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
92 fn api_endpoint(&self) -> String {
93 format!("{}/api/v1", self.hostname)
97 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
99 Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
102 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
103 pub enum SearchType {
104 Both, Comments, Posts
107 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
108 DateTime::<Utc>::from_utc(ndt, Utc)
111 pub fn naive_now() -> NaiveDateTime {
112 chrono::prelude::Utc::now().naive_utc()
115 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
116 NaiveDateTime::from_timestamp(time, 0)
119 pub fn is_email_regex(test: &str) -> bool {
120 EMAIL_REGEX.is_match(test)
123 pub fn remove_slurs(test: &str) -> String {
124 SLUR_REGEX.replace_all(test, "*removed*").to_string()
127 pub fn has_slurs(test: &str) -> bool {
128 SLUR_REGEX.is_match(test)
131 pub fn fuzzy_search(q: &str) -> String {
132 let replaced = q.replace(" ", "%");
133 format!("%{}%", replaced)
136 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
137 let page = page.unwrap_or(1);
138 let limit = limit.unwrap_or(10);
139 let offset = limit * (page - 1);
145 use {Settings, is_email_regex, remove_slurs, has_slurs, fuzzy_search};
148 assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
151 #[test] fn test_email() {
152 assert!(is_email_regex("gush@gmail.com"));
153 assert!(!is_email_regex("nada_neutho"));
156 #[test] fn test_slur_filter() {
157 let test = "coons test dindu ladyboy tranny. This is a bunch of other safe text.".to_string();
158 let slur_free = "No slurs here";
159 assert_eq!(remove_slurs(&test), "*removed* test *removed* *removed* *removed*. This is a bunch of other safe text.".to_string());
160 assert!(has_slurs(&test));
161 assert!(!has_slurs(slur_free));
164 #[test] fn test_fuzzy_search() {
165 let test = "This is a fuzzy search";
166 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
173 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
174 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();