2018-06-23 18:36:11 +02:00
|
|
|
extern crate activitypub;
|
|
|
|
extern crate ammonia;
|
|
|
|
extern crate bcrypt;
|
|
|
|
extern crate chrono;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
extern crate heck;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
extern crate openssl;
|
|
|
|
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;
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
use diesel::{PgConnection, RunQueryDsl, select};
|
2018-06-23 18:36:11 +02:00
|
|
|
use std::env;
|
2018-06-21 12:28:42 +02:00
|
|
|
|
2018-06-18 15:37:49 +02:00
|
|
|
macro_rules! find_by {
|
2018-06-18 17:13:09 +02:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
|
2018-06-18 15:37:49 +02:00
|
|
|
/// Try to find a $table with a given $col
|
2018-06-18 17:13:09 +02:00
|
|
|
pub fn $fn(conn: &PgConnection, $($col: $type),+) -> Option<Self> {
|
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
2018-06-18 15:37:49 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.expect("Error loading $table by $col")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:23:54 +02: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: &PgConnection, $($col: $type),+) -> Vec<Self> {
|
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.expect("Error loading $table by $col")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-18 15:44:23 +02:00
|
|
|
macro_rules! get {
|
|
|
|
($table:ident) => {
|
|
|
|
pub fn get(conn: &PgConnection, 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)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-18 15:57:38 +02:00
|
|
|
macro_rules! insert {
|
|
|
|
($table:ident, $from:ident) => {
|
|
|
|
pub fn insert(conn: &PgConnection, new: $from) -> Self {
|
|
|
|
diesel::insert_into($table::table)
|
|
|
|
.values(new)
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Error saving new $table")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
sql_function!(nextval, nextval_t, (seq: ::diesel::sql_types::Text) -> ::diesel::sql_types::BigInt);
|
|
|
|
sql_function!(setval, setval_t, (seq: ::diesel::sql_types::Text, val: ::diesel::sql_types::BigInt) -> ::diesel::sql_types::BigInt);
|
|
|
|
|
|
|
|
fn get_next_id(conn: &PgConnection, seq: &str) -> i32 {
|
|
|
|
// We cant' use currval because it may fail if nextval have never been called before
|
|
|
|
let next = select(nextval(seq)).get_result::<i64>(conn).expect("Next ID fail");
|
2018-06-27 00:19:18 +02:00
|
|
|
if next > 1 {
|
|
|
|
select(setval(seq, next - 1)).get_result::<i64>(conn).expect("Reset ID fail");
|
|
|
|
}
|
2018-06-21 12:28:42 +02:00
|
|
|
next as i32
|
|
|
|
}
|
|
|
|
|
2018-06-23 18:36:11 +02: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"))));
|
|
|
|
|
|
|
|
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"))));
|
2018-06-26 16:16:59 +02:00
|
|
|
|
|
|
|
pub static ref USE_HTTPS: bool = env::var("USE_HTTPS").map(|val| val == "1").unwrap_or(true);
|
2018-06-23 18:36:11 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 17:32:35 +02:00
|
|
|
pub fn ap_url(url: String) -> String {
|
2018-06-26 16:21:58 +02:00
|
|
|
let scheme = if *USE_HTTPS {
|
|
|
|
"https"
|
|
|
|
} else {
|
|
|
|
"http"
|
|
|
|
};
|
|
|
|
format!("{}://{}", scheme, url)
|
|
|
|
}
|
|
|
|
|
2018-07-27 19:05:36 +02:00
|
|
|
pub mod admin;
|
2018-04-23 13:27:27 +02:00
|
|
|
pub mod blog_authors;
|
2018-04-23 12:29:27 +02:00
|
|
|
pub mod blogs;
|
2018-05-09 22:35:02 +02:00
|
|
|
pub mod comments;
|
2018-06-23 18:36:11 +02:00
|
|
|
pub mod db_conn;
|
2018-05-01 15:06:31 +02:00
|
|
|
pub mod follows;
|
2018-04-22 15:35:37 +02:00
|
|
|
pub mod instance;
|
2018-05-10 17:54:35 +02:00
|
|
|
pub mod likes;
|
2018-06-20 20:22:34 +02:00
|
|
|
pub mod mentions;
|
2018-05-13 14:44:18 +02:00
|
|
|
pub mod notifications;
|
2018-04-23 16:37:49 +02:00
|
|
|
pub mod post_authors;
|
2018-04-23 17:19:28 +02:00
|
|
|
pub mod posts;
|
2018-05-19 11:23:02 +02:00
|
|
|
pub mod reshares;
|
2018-06-23 18:36:11 +02:00
|
|
|
pub mod safe_string;
|
|
|
|
pub mod schema;
|
2018-04-23 17:19:28 +02:00
|
|
|
pub mod users;
|