]> Untitled Git - lemmy.git/blob - server/src/lib.rs
Merge branch 'dev' into moderation
[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 use serde::{Deserialize, Serialize};
28 use chrono::{DateTime, NaiveDateTime, Utc};
29
30 pub trait Crud<T> {
31   fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
32   fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;  
33   fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;  
34   fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
35 }
36
37 pub trait Followable<T> {
38   fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
39   fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
40 }
41
42 pub trait Joinable<T> {
43   fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
44   fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
45 }
46
47 pub trait Likeable<T> {
48   fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
49   fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
50   fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
51 }
52
53 pub fn establish_connection() -> PgConnection {
54   let db_url = Settings::get().db_url;
55   PgConnection::establish(&db_url)
56     .expect(&format!("Error connecting to {}", db_url))
57 }
58
59 pub struct Settings {
60   db_url: String,
61   hostname: String
62 }
63
64 impl Settings {
65   fn get() -> Self {
66     dotenv().ok();
67     Settings {
68       db_url: env::var("DATABASE_URL")
69         .expect("DATABASE_URL must be set"),
70         hostname: env::var("HOSTNAME").unwrap_or("http://0.0.0.0".to_string())
71     }
72   }
73   fn api_endpoint(&self) -> String {
74     format!("{}/api/v1", self.hostname)
75   }
76 }
77
78 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
79 pub enum SortType {
80   Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
81 }
82
83 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
84   DateTime::<Utc>::from_utc(ndt, Utc)
85 }
86
87 pub fn naive_now() -> NaiveDateTime {
88   chrono::prelude::Utc::now().naive_utc()
89 }
90
91 pub fn is_email_regex(test: &str) -> bool {
92   let re = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
93   re.is_match(test)
94 }
95
96 #[cfg(test)]
97 mod tests {
98   use {Settings, is_email_regex};
99   #[test]
100   fn test_api() {
101     assert_eq!(Settings::get().api_endpoint(), "http://0.0.0.0/api/v1");
102   }
103
104   #[test]
105   fn test_email() {
106     assert!(is_email_regex("gush@gmail.com"));
107     assert!(!is_email_regex("nada_neutho"));
108   } 
109 }