]> Untitled Git - lemmy.git/blob - server/src/lib.rs
Adding comment voting
[lemmy.git] / server / src / lib.rs
1 #[macro_use]
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;
9 pub extern crate rand;
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
16 pub mod schema;
17 pub mod apub;
18 pub mod actions;
19 pub mod websocket_server;
20
21 use diesel::*;
22 use diesel::pg::PgConnection;
23 use diesel::result::Error;
24 use dotenv::dotenv;
25 use std::env;
26 use regex::Regex;
27
28 pub trait Crud<T> {
29   fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
30   fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;  
31   fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;  
32   fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
33 }
34
35 pub trait Followable<T> {
36   fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
37   fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
38 }
39
40 pub trait Joinable<T> {
41   fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
42   fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
43 }
44
45 pub trait Likeable<T> {
46   fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
47   fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
48   fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
49 }
50
51 pub fn establish_connection() -> PgConnection {
52   let db_url = Settings::get().db_url;
53   PgConnection::establish(&db_url)
54     .expect(&format!("Error connecting to {}", db_url))
55 }
56
57 pub struct Settings {
58   db_url: String,
59   hostname: String
60 }
61
62 impl Settings {
63   fn get() -> Self {
64     dotenv().ok();
65     Settings {
66       db_url: env::var("DATABASE_URL")
67         .expect("DATABASE_URL must be set"),
68         hostname: env::var("HOSTNAME").unwrap_or("http://0.0.0.0".to_string())
69     }
70   }
71   fn api_endpoint(&self) -> String {
72     format!("{}/api/v1", self.hostname)
73   }
74 }
75
76 use chrono::{DateTime, NaiveDateTime, Utc};
77 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
78   DateTime::<Utc>::from_utc(ndt, Utc)
79 }
80
81 pub fn naive_now() -> NaiveDateTime {
82   chrono::prelude::Utc::now().naive_utc()
83 }
84
85 pub fn is_email_regex(test: &str) -> bool {
86   let re = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
87   re.is_match(test)
88 }
89
90 #[cfg(test)]
91 mod tests {
92   use {Settings, is_email_regex};
93   #[test]
94   fn test_api() {
95     assert_eq!(Settings::get().api_endpoint(), "http://0.0.0.0/api/v1");
96   }
97
98   #[test]
99   fn test_email() {
100     assert!(is_email_regex("gush@gmail.com"));
101     assert!(!is_email_regex("nada_neutho"));
102   } 
103 }