2018-09-08 01:11:27 +02:00
|
|
|
#![allow(proc_macro_derive_resolution_fallback)] // This can be removed after diesel-1.4
|
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate activitypub;
|
|
|
|
extern crate ammonia;
|
2018-12-06 18:54:16 +01:00
|
|
|
extern crate askama_escape;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate bcrypt;
|
2018-09-19 16:49:34 +02:00
|
|
|
extern crate canapi;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate chrono;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2018-10-31 10:40:20 +01:00
|
|
|
extern crate guid_create;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate heck;
|
2018-12-02 17:37:51 +01:00
|
|
|
extern crate itertools;
|
2018-06-23 18:36:11 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
extern crate openssl;
|
2018-09-19 16:49:34 +02:00
|
|
|
extern crate plume_api;
|
2018-06-23 18:36:11 +02:00
|
|
|
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;
|
2018-12-02 17:37:51 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tantivy;
|
2018-06-23 18:36:11 +02:00
|
|
|
extern crate url;
|
|
|
|
extern crate webfinger;
|
2018-12-02 17:37:51 +01:00
|
|
|
extern crate whatlang;
|
2018-06-23 18:36:11 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use std::env;
|
2018-06-21 12:28:42 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
#[cfg(not(any(feature = "sqlite", feature = "postgres")))]
|
|
|
|
compile_error!("Either feature \"sqlite\" or \"postgres\" must be enabled for this crate.");
|
|
|
|
#[cfg(all(feature = "sqlite", feature = "postgres"))]
|
|
|
|
compile_error!("Either feature \"sqlite\" or \"postgres\" must be enabled for this crate.");
|
|
|
|
|
2018-09-27 23:06:40 +02: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;
|
|
|
|
|
2018-09-29 12:05:05 +02:00
|
|
|
/// Adds a function to a model, that returns the first
|
|
|
|
/// matching row for a given list of fields.
|
|
|
|
///
|
|
|
|
/// Usage:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl Model {
|
|
|
|
/// find_by!(model_table, name_of_the_function, field1 as String, field2 as i32);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Get the Model with field1 == "", and field2 == 0
|
|
|
|
/// Model::name_of_the_function(connection, String::new(), 0);
|
|
|
|
/// ```
|
2018-06-18 15:37:49 +02:00
|
|
|
macro_rules! find_by {
|
2018-11-26 10:21:52 +01:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ty),+) => {
|
2018-06-18 15:37:49 +02:00
|
|
|
/// Try to find a $table with a given $col
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Option<Self> {
|
2018-06-18 17:13:09 +02:00
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
2018-06-18 15:37:49 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("macro::find_by: Error loading $table by $col")
|
2018-06-18 15:37:49 +02:00
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:05:05 +02:00
|
|
|
/// List all rows of a model, with field-based filtering.
|
|
|
|
///
|
|
|
|
/// Usage:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl Model {
|
|
|
|
/// list_by!(model_table, name_of_the_function, field1 as String);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // To get all Models with field1 == ""
|
|
|
|
/// Model::name_of_the_function(connection, String::new());
|
|
|
|
/// ```
|
2018-06-20 20:23:54 +02:00
|
|
|
macro_rules! list_by {
|
2018-11-26 10:21:52 +01:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ty),+) => {
|
2018-06-20 20:23:54 +02:00
|
|
|
/// Try to find a $table with a given $col
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Vec<Self> {
|
2018-06-20 20:23:54 +02:00
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
|
|
|
.load::<Self>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("macro::list_by: Error loading $table by $col")
|
2018-06-20 20:23:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:05:05 +02:00
|
|
|
/// Adds a function to a model to retrieve a row by ID
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl Model {
|
|
|
|
/// get!(model_table);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Get the Model with ID 1
|
|
|
|
/// Model::get(connection, 1);
|
|
|
|
/// ```
|
2018-06-18 15:44:23 +02:00
|
|
|
macro_rules! get {
|
|
|
|
($table:ident) => {
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get(conn: &crate::Connection, id: i32) -> Option<Self> {
|
2018-11-24 12:44:17 +01:00
|
|
|
$table::table
|
|
|
|
.filter($table::id.eq(id))
|
2018-06-18 15:44:23 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("macro::get: Error loading $table by id")
|
2018-11-24 12:44:17 +01:00
|
|
|
.into_iter()
|
|
|
|
.nth(0)
|
2018-06-18 15:44:23 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:05:05 +02:00
|
|
|
/// Adds a function to a model to insert a new row
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl Model {
|
|
|
|
/// insert!(model_table, NewModelType);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Insert a new row
|
|
|
|
/// Model::insert(connection, NewModelType::new());
|
|
|
|
/// ```
|
2018-06-18 15:57:38 +02:00
|
|
|
macro_rules! insert {
|
|
|
|
($table:ident, $from:ident) => {
|
2018-09-27 23:06:40 +02:00
|
|
|
last!($table);
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn insert(conn: &crate::Connection, new: $from) -> Self {
|
2018-06-18 15:57:38 +02:00
|
|
|
diesel::insert_into($table::table)
|
|
|
|
.values(new)
|
2018-09-27 23:06:40 +02:00
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("macro::insert: Error saving new $table");
|
2018-09-27 23:06:40 +02:00
|
|
|
Self::last(conn)
|
2018-06-18 15:57:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:05:05 +02:00
|
|
|
/// Returns the last row of a table.
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl Model {
|
|
|
|
/// last!(model_table);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Get the last Model
|
|
|
|
/// Model::last(connection)
|
|
|
|
/// ```
|
2018-09-27 23:06:40 +02:00
|
|
|
macro_rules! last {
|
|
|
|
($table:ident) => {
|
|
|
|
pub fn last(conn: &crate::Connection) -> Self {
|
2018-11-24 12:44:17 +01:00
|
|
|
$table::table
|
|
|
|
.order_by($table::id.desc())
|
2018-09-27 23:06:40 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect(concat!(
|
|
|
|
"macro::last: Error getting last ",
|
|
|
|
stringify!($table)
|
|
|
|
))
|
|
|
|
.iter()
|
|
|
|
.next()
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect(concat!("macro::last: No last ", stringify!($table)))
|
2018-09-27 23:06:40 +02:00
|
|
|
.clone()
|
2018-09-06 23:39:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
lazy_static! {
|
2018-11-26 10:21:52 +01:00
|
|
|
pub static ref BASE_URL: String = env::var("BASE_URL").unwrap_or_else(|_| format!(
|
2018-11-24 12:44:17 +01:00
|
|
|
"127.0.0.1:{}",
|
2018-11-26 10:21:52 +01:00
|
|
|
env::var("ROCKET_PORT").unwrap_or_else(|_| String::from("8000"))
|
2018-11-24 12:44:17 +01:00
|
|
|
));
|
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-11-24 12:44:17 +01:00
|
|
|
#[cfg(not(test))]
|
|
|
|
static DB_NAME: &str = "plume";
|
|
|
|
#[cfg(test)]
|
|
|
|
static DB_NAME: &str = "plume_tests";
|
|
|
|
|
2018-10-06 20:17:36 +02:00
|
|
|
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
|
|
|
|
lazy_static! {
|
2018-11-24 12:44:17 +01:00
|
|
|
pub static ref DATABASE_URL: String =
|
2018-11-26 10:21:52 +01:00
|
|
|
env::var("DATABASE_URL").unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME));
|
2018-10-06 20:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
|
|
|
|
lazy_static! {
|
2018-11-24 12:44:17 +01:00
|
|
|
pub static ref DATABASE_URL: String =
|
2018-11-26 10:21:52 +01:00
|
|
|
env::var("DATABASE_URL").unwrap_or_else(|_| format!("{}.sqlite", DB_NAME));
|
2018-10-06 20:17:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
pub fn ap_url(url: &str) -> String {
|
2018-11-24 12:44:17 +01:00
|
|
|
let scheme = if *USE_HTTPS { "https" } else { "http" };
|
2018-06-26 16:21:58 +02:00
|
|
|
format!("{}://{}", scheme, url)
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod tests {
|
2018-12-14 23:16:18 +01:00
|
|
|
use diesel::{dsl::sql_query, Connection, RunQueryDsl};
|
2018-11-24 12:44:17 +01:00
|
|
|
use Connection as Conn;
|
|
|
|
use DATABASE_URL;
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
embed_migrations!("../migrations/sqlite");
|
|
|
|
|
|
|
|
#[cfg(feature = "postgres")]
|
|
|
|
embed_migrations!("../migrations/postgres");
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! part_eq {
|
|
|
|
( $x:expr, $y:expr, [$( $var:ident ),*] ) => {
|
|
|
|
{
|
|
|
|
$(
|
|
|
|
assert_eq!($x.$var, $y.$var);
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn db() -> Conn {
|
|
|
|
let conn =
|
|
|
|
Conn::establish(&*DATABASE_URL.as_str()).expect("Couldn't connect to the database");
|
|
|
|
embedded_migrations::run(&conn).expect("Couldn't run migrations");
|
2018-12-14 23:16:18 +01:00
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
sql_query("PRAGMA foreign_keys = on;").execute(&conn).expect("PRAGMA foreign_keys fail");
|
2018-11-24 12:44:17 +01:00
|
|
|
conn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 19:05:36 +02:00
|
|
|
pub mod admin;
|
2018-10-22 15:30:04 +02:00
|
|
|
pub mod api_tokens;
|
2018-10-21 15:19:07 +02:00
|
|
|
pub mod apps;
|
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-12-24 11:23:04 +01:00
|
|
|
pub mod comment_seers;
|
2018-06-23 18:36:11 +02:00
|
|
|
pub mod db_conn;
|
2018-05-01 15:06:31 +02:00
|
|
|
pub mod follows;
|
2018-10-03 20:48:25 +02:00
|
|
|
pub mod headers;
|
2018-04-22 15:35:37 +02:00
|
|
|
pub mod instance;
|
2018-05-10 17:54:35 +02:00
|
|
|
pub mod likes;
|
2018-09-02 13:34:48 +02:00
|
|
|
pub mod medias;
|
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;
|
2018-12-02 17:37:51 +01:00
|
|
|
pub mod search;
|
2018-06-23 18:36:11 +02:00
|
|
|
pub mod schema;
|
2018-09-05 20:05:53 +02:00
|
|
|
pub mod tags;
|
2018-04-23 17:19:28 +02:00
|
|
|
pub mod users;
|