Plume/plume-models/src/lib.rs

151 lines
4.1 KiB
Rust
Raw Normal View History

#![allow(proc_macro_derive_resolution_fallback)] // This can be removed after diesel-1.4
#![feature(crate_in_paths)]
extern crate activitypub;
extern crate ammonia;
extern crate bcrypt;
2018-09-19 14:49:34 +00:00
extern crate canapi;
extern crate chrono;
#[macro_use]
extern crate diesel;
extern crate heck;
#[macro_use]
extern crate lazy_static;
extern crate openssl;
2018-09-19 14:49:34 +00:00
extern crate plume_api;
extern crate plume_common;
extern crate reqwest;
extern crate rocket;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate url;
extern crate webfinger;
use std::env;
2018-09-27 21:06:40 +00:00
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type Connection = diesel::SqliteConnection;
#[cfg(all(not(feature = "sqlite"), feature = "postgres"))]
pub type Connection = diesel::PgConnection;
macro_rules! find_by {
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
/// Try to find a $table with a given $col
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Option<Self> {
$table::table
$(.filter($table::$col.eq($col)))+
.limit(1)
.load::<Self>(conn)
.expect("Error loading $table by $col")
.into_iter().nth(0)
}
};
}
2018-06-20 18:23:54 +00:00
macro_rules! list_by {
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
/// Try to find a $table with a given $col
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Vec<Self> {
2018-06-20 18:23:54 +00:00
$table::table
$(.filter($table::$col.eq($col)))+
.load::<Self>(conn)
.expect("Error loading $table by $col")
}
};
}
macro_rules! get {
($table:ident) => {
pub fn get(conn: &crate::Connection, id: i32) -> Option<Self> {
$table::table.filter($table::id.eq(id))
.limit(1)
.load::<Self>(conn)
.expect("Error loading $table by id")
.into_iter().nth(0)
}
};
}
macro_rules! insert {
($table:ident, $from:ident) => {
2018-09-27 21:06:40 +00:00
last!($table);
pub fn insert(conn: &crate::Connection, new: $from) -> Self {
diesel::insert_into($table::table)
.values(new)
2018-09-27 21:06:40 +00:00
.execute(conn)
.expect("Error saving new $table");
Self::last(conn)
}
};
}
2018-09-06 21:39:22 +00:00
macro_rules! update {
($table:ident) => {
pub fn update(&self, conn: &crate::Connection) -> Self {
2018-09-06 21:39:22 +00:00
diesel::update(self)
.set(self)
2018-09-27 21:06:40 +00:00
.execute(conn)
.expect(concat!("Error updating ", stringify!($table)));
Self::get(conn, self.id)
.expect(concat!(stringify!($table), " we just updated doesn't exist anymore???"))
}
};
}
macro_rules! last {
($table:ident) => {
pub fn last(conn: &crate::Connection) -> Self {
$table::table.order_by($table::id.desc())
.limit(1)
.load::<Self>(conn)
.expect(concat!("Error getting last ", stringify!($table)))
.iter().next()
.expect(concat!("No last ", stringify!($table)))
.clone()
2018-09-06 21:39:22 +00:00
}
};
}
lazy_static! {
pub static ref BASE_URL: String = env::var("BASE_URL")
.unwrap_or(format!("127.0.0.1:{}", env::var("ROCKET_PORT").unwrap_or(String::from("8000"))));
2018-09-02 11:34:48 +00:00
pub static ref DB_URL: String = env::var("DB_URL")
.unwrap_or(format!("postgres://plume:plume@localhost/{}", env::var("DB_NAME").unwrap_or(String::from("plume"))));
pub static ref USE_HTTPS: bool = env::var("USE_HTTPS").map(|val| val == "1").unwrap_or(true);
}
pub fn ap_url(url: String) -> String {
let scheme = if *USE_HTTPS {
"https"
} else {
"http"
};
format!("{}://{}", scheme, url)
}
pub mod admin;
2018-04-23 11:27:27 +00:00
pub mod blog_authors;
2018-04-23 10:29:27 +00:00
pub mod blogs;
2018-05-09 20:35:02 +00:00
pub mod comments;
pub mod db_conn;
2018-05-01 13:06:31 +00:00
pub mod follows;
pub mod instance;
2018-05-10 15:54:35 +00:00
pub mod likes;
2018-09-02 11:34:48 +00:00
pub mod medias;
2018-06-20 18:22:34 +00:00
pub mod mentions;
2018-05-13 12:44:18 +00:00
pub mod notifications;
2018-04-23 14:37:49 +00:00
pub mod post_authors;
2018-04-23 15:19:28 +00:00
pub mod posts;
2018-05-19 09:23:02 +00:00
pub mod reshares;
pub mod safe_string;
pub mod schema;
2018-09-05 18:05:53 +00:00
pub mod tags;
2018-04-23 15:19:28 +00:00
pub mod users;