2018-12-29 08:36:07 +00:00
|
|
|
#![feature(try_trait)]
|
2019-03-19 13:37:56 +00:00
|
|
|
#![feature(never_type)]
|
2019-03-21 10:51:41 +00:00
|
|
|
#![feature(custom_attribute)]
|
2019-04-29 14:30:20 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2018-09-07 23:11:27 +00:00
|
|
|
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate activitypub;
|
|
|
|
extern crate ammonia;
|
2018-12-06 17:54:16 +00:00
|
|
|
extern crate askama_escape;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate bcrypt;
|
|
|
|
extern crate chrono;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2018-10-31 09:40:20 +00:00
|
|
|
extern crate guid_create;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate heck;
|
2018-12-02 16:37:51 +00:00
|
|
|
extern crate itertools;
|
2018-06-23 16:36:11 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2019-04-29 14:30:20 +00:00
|
|
|
extern crate migrations_internals;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate openssl;
|
2018-09-19 14:49:34 +00:00
|
|
|
extern crate plume_api;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate plume_common;
|
2019-04-29 14:30:20 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate plume_macro;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate reqwest;
|
|
|
|
extern crate rocket;
|
2019-04-17 17:31:47 +00:00
|
|
|
extern crate rocket_i18n;
|
2018-12-24 15:42:40 +00:00
|
|
|
extern crate scheduled_thread_pool;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
2019-03-21 10:51:41 +00:00
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate serde_json;
|
2018-12-02 16:37:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tantivy;
|
2018-06-23 16:36:11 +00:00
|
|
|
extern crate url;
|
|
|
|
extern crate webfinger;
|
2018-12-02 16:37:51 +00:00
|
|
|
extern crate whatlang;
|
2018-06-23 16:36:11 +00:00
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
use plume_common::activity_pub::inbox::InboxError;
|
|
|
|
|
2018-11-26 09:21:52 +00: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 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;
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
/// All the possible errors that can be encoutered in this crate
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Db(diesel::result::Error),
|
2019-04-17 17:31:47 +00:00
|
|
|
Inbox(Box<InboxError<Error>>),
|
2018-12-29 08:36:07 +00:00
|
|
|
InvalidValue,
|
|
|
|
Io(std::io::Error),
|
|
|
|
MissingApProperty,
|
|
|
|
NotFound,
|
|
|
|
Request,
|
|
|
|
SerDe,
|
|
|
|
Search(search::SearcherError),
|
|
|
|
Signature,
|
|
|
|
Unauthorized,
|
|
|
|
Url,
|
|
|
|
Webfinger,
|
2019-06-04 18:55:17 +00:00
|
|
|
Expired,
|
2018-12-29 08:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bcrypt::BcryptError> for Error {
|
|
|
|
fn from(_: bcrypt::BcryptError) -> Self {
|
|
|
|
Error::Signature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<openssl::error::ErrorStack> for Error {
|
|
|
|
fn from(_: openssl::error::ErrorStack) -> Self {
|
|
|
|
Error::Signature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<diesel::result::Error> for Error {
|
|
|
|
fn from(err: diesel::result::Error) -> Self {
|
|
|
|
Error::Db(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::option::NoneError> for Error {
|
|
|
|
fn from(_: std::option::NoneError) -> Self {
|
|
|
|
Error::NotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<url::ParseError> for Error {
|
|
|
|
fn from(_: url::ParseError) -> Self {
|
|
|
|
Error::Url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for Error {
|
|
|
|
fn from(_: serde_json::Error) -> Self {
|
|
|
|
Error::SerDe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
fn from(_: reqwest::Error) -> Self {
|
|
|
|
Error::Request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::header::InvalidHeaderValue> for Error {
|
|
|
|
fn from(_: reqwest::header::InvalidHeaderValue) -> Self {
|
|
|
|
Error::Request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<activitypub::Error> for Error {
|
|
|
|
fn from(err: activitypub::Error) -> Self {
|
|
|
|
match err {
|
|
|
|
activitypub::Error::NotFound => Error::MissingApProperty,
|
|
|
|
_ => Error::SerDe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<webfinger::WebfingerError> for Error {
|
|
|
|
fn from(_: webfinger::WebfingerError) -> Self {
|
|
|
|
Error::Webfinger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<search::SearcherError> for Error {
|
|
|
|
fn from(err: search::SearcherError) -> Self {
|
|
|
|
Error::Search(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
|
|
|
Error::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
impl From<InboxError<Error>> for Error {
|
|
|
|
fn from(err: InboxError<Error>) -> Error {
|
|
|
|
match err {
|
|
|
|
InboxError::InvalidActor(Some(e)) | InboxError::InvalidObject(Some(e)) => e,
|
|
|
|
e => Error::Inbox(Box::new(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2018-09-29 10:05:05 +00: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 13:37:49 +00:00
|
|
|
macro_rules! find_by {
|
2018-11-26 09:21:52 +00:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ty),+) => {
|
2018-06-18 13:37:49 +00:00
|
|
|
/// Try to find a $table with a given $col
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Result<Self> {
|
2018-06-18 15:13:09 +00:00
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
2018-06-18 13:37:49 +00:00
|
|
|
.limit(1)
|
2018-12-29 08:36:07 +00:00
|
|
|
.load::<Self>(conn)?
|
|
|
|
.into_iter()
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::NotFound)
|
2018-06-18 13:37:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 10:05:05 +00: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 18:23:54 +00:00
|
|
|
macro_rules! list_by {
|
2018-11-26 09:21:52 +00:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ty),+) => {
|
2018-06-20 18:23:54 +00:00
|
|
|
/// Try to find a $table with a given $col
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Result<Vec<Self>> {
|
2018-06-20 18:23:54 +00:00
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
|
|
|
.load::<Self>(conn)
|
2018-12-29 08:36:07 +00:00
|
|
|
.map_err(Error::from)
|
2018-06-20 18:23:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 10:05:05 +00: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 13:44:23 +00:00
|
|
|
macro_rules! get {
|
|
|
|
($table:ident) => {
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn get(conn: &crate::Connection, id: i32) -> Result<Self> {
|
2018-11-24 11:44:17 +00:00
|
|
|
$table::table
|
|
|
|
.filter($table::id.eq(id))
|
2018-06-18 13:44:23 +00:00
|
|
|
.limit(1)
|
2018-12-29 08:36:07 +00:00
|
|
|
.load::<Self>(conn)?
|
2018-11-24 11:44:17 +00:00
|
|
|
.into_iter()
|
2018-12-29 08:36:07 +00:00
|
|
|
.next()
|
|
|
|
.ok_or(Error::NotFound)
|
2018-06-18 13:44:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 10:05:05 +00: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 13:57:38 +00:00
|
|
|
macro_rules! insert {
|
|
|
|
($table:ident, $from:ident) => {
|
2019-03-22 18:51:36 +00:00
|
|
|
insert!($table, $from, |x, _conn| Ok(x));
|
2019-03-04 20:35:03 +00:00
|
|
|
};
|
2019-03-22 18:51:36 +00:00
|
|
|
($table:ident, $from:ident, |$val:ident, $conn:ident | $( $after:tt )+) => {
|
2018-09-27 21:06:40 +00:00
|
|
|
last!($table);
|
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn insert(conn: &crate::Connection, new: $from) -> Result<Self> {
|
2018-06-18 13:57:38 +00:00
|
|
|
diesel::insert_into($table::table)
|
|
|
|
.values(new)
|
2018-12-29 08:36:07 +00:00
|
|
|
.execute(conn)?;
|
2019-03-04 20:35:03 +00:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut $val = Self::last(conn)?;
|
|
|
|
let $conn = conn;
|
2019-03-22 18:51:36 +00:00
|
|
|
$( $after )+
|
2018-06-18 13:57:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 10:05:05 +00: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 21:06:40 +00:00
|
|
|
macro_rules! last {
|
|
|
|
($table:ident) => {
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn last(conn: &crate::Connection) -> Result<Self> {
|
2018-11-24 11:44:17 +00:00
|
|
|
$table::table
|
|
|
|
.order_by($table::id.desc())
|
2018-09-27 21:06:40 +00:00
|
|
|
.limit(1)
|
2018-12-29 08:36:07 +00:00
|
|
|
.load::<Self>(conn)?
|
|
|
|
.into_iter()
|
2018-11-24 11:44:17 +00:00
|
|
|
.next()
|
2018-12-29 08:36:07 +00:00
|
|
|
.ok_or(Error::NotFound)
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-21 09:30:33 +00:00
|
|
|
mod config;
|
|
|
|
pub use config::CONFIG;
|
2018-10-06 18:17:36 +00:00
|
|
|
|
2018-11-26 09:21:52 +00:00
|
|
|
pub fn ap_url(url: &str) -> String {
|
2019-03-21 09:30:33 +00:00
|
|
|
format!("https://{}", url)
|
2018-06-26 14:21:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
mod tests {
|
2019-04-17 17:31:47 +00:00
|
|
|
use db_conn;
|
|
|
|
use diesel::r2d2::ConnectionManager;
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
use diesel::{dsl::sql_query, RunQueryDsl};
|
2019-04-29 14:30:20 +00:00
|
|
|
use migrations::IMPORTED_MIGRATIONS;
|
|
|
|
use plume_common::utils::random_hex;
|
2019-04-17 17:31:47 +00:00
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use search;
|
2019-04-29 14:30:20 +00:00
|
|
|
use std::env::temp_dir;
|
2019-04-17 17:31:47 +00:00
|
|
|
use std::sync::Arc;
|
2018-11-24 11:44:17 +00:00
|
|
|
use Connection as Conn;
|
2019-03-21 09:30:33 +00:00
|
|
|
use CONFIG;
|
2018-11-24 11:44:17 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! part_eq {
|
|
|
|
( $x:expr, $y:expr, [$( $var:ident ),*] ) => {
|
|
|
|
{
|
|
|
|
$(
|
|
|
|
assert_eq!($x.$var, $y.$var);
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
pub fn db<'a>() -> db_conn::DbConn {
|
|
|
|
db_conn::DbConn((*DB_POOL).get().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref DB_POOL: db_conn::DbPool = {
|
|
|
|
let pool = db_conn::DbPool::builder()
|
|
|
|
.connection_customizer(Box::new(db_conn::PragmaForeignKey))
|
|
|
|
.build(ConnectionManager::<Conn>::new(CONFIG.database_url.as_str()))
|
|
|
|
.unwrap();
|
2019-04-29 14:30:20 +00:00
|
|
|
let dir = temp_dir().join(format!("plume-test-{}", random_hex()));
|
|
|
|
IMPORTED_MIGRATIONS
|
|
|
|
.run_pending_migrations(&pool.get().unwrap(), &dir)
|
|
|
|
.expect("Migrations error");
|
2019-04-17 17:31:47 +00:00
|
|
|
pool
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rockets() -> super::PlumeRocket {
|
|
|
|
super::PlumeRocket {
|
|
|
|
conn: db_conn::DbConn((*DB_POOL).get().unwrap()),
|
|
|
|
searcher: Arc::new(search::tests::get_searcher()),
|
|
|
|
worker: Arc::new(ScheduledThreadPool::new(2)),
|
|
|
|
user: None,
|
|
|
|
}
|
2018-11-24 11:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 17:05:36 +00:00
|
|
|
pub mod admin;
|
2018-10-22 13:30:04 +00:00
|
|
|
pub mod api_tokens;
|
2018-10-21 13:19:07 +00:00
|
|
|
pub mod apps;
|
2018-04-23 11:27:27 +00:00
|
|
|
pub mod blog_authors;
|
2018-04-23 10:29:27 +00:00
|
|
|
pub mod blogs;
|
2018-12-24 10:23:04 +00:00
|
|
|
pub mod comment_seers;
|
2019-03-20 16:56:17 +00:00
|
|
|
pub mod comments;
|
2018-06-23 16:36:11 +00:00
|
|
|
pub mod db_conn;
|
2018-05-01 13:06:31 +00:00
|
|
|
pub mod follows;
|
2018-10-03 18:48:25 +00:00
|
|
|
pub mod headers;
|
2019-04-17 17:31:47 +00:00
|
|
|
pub mod inbox;
|
2018-04-22 13:35:37 +00:00
|
|
|
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;
|
2019-04-29 14:30:20 +00:00
|
|
|
pub mod migrations;
|
2018-05-13 12:44:18 +00:00
|
|
|
pub mod notifications;
|
2019-06-04 18:55:17 +00:00
|
|
|
pub mod password_reset_requests;
|
2019-04-17 17:31:47 +00:00
|
|
|
pub mod plume_rocket;
|
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;
|
2018-06-23 16:36:11 +00:00
|
|
|
pub mod safe_string;
|
|
|
|
pub mod schema;
|
2019-03-20 16:56:17 +00:00
|
|
|
pub mod search;
|
2018-09-05 18:05:53 +00:00
|
|
|
pub mod tags;
|
2018-04-23 15:19:28 +00:00
|
|
|
pub mod users;
|
2019-04-17 17:31:47 +00:00
|
|
|
pub use plume_rocket::PlumeRocket;
|