2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
|
|
|
ap_url,
|
|
|
|
medias::Media,
|
|
|
|
safe_string::SafeString,
|
|
|
|
schema::{instances, users},
|
2021-11-22 15:11:32 +01:00
|
|
|
users::{NewUser, Role, User},
|
2020-01-21 07:02:03 +01:00
|
|
|
Connection, Error, Result,
|
|
|
|
};
|
2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2021-11-22 15:11:32 +01:00
|
|
|
use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2021-11-24 14:04:35 +01:00
|
|
|
use once_cell::sync::OnceCell;
|
2018-07-27 22:16:17 +02:00
|
|
|
use plume_common::utils::md_to_html;
|
2020-01-21 07:02:03 +01:00
|
|
|
use std::sync::RwLock;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Clone, Identifiable, Queryable)]
|
2018-04-22 15:35:37 +02:00
|
|
|
pub struct Instance {
|
|
|
|
pub id: i32,
|
|
|
|
pub public_domain: String,
|
|
|
|
pub name: String,
|
|
|
|
pub local: bool,
|
2018-04-30 19:46:27 +02:00
|
|
|
pub blocked: bool,
|
2018-09-27 23:06:40 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-07-27 19:05:36 +02:00
|
|
|
pub open_registrations: bool,
|
2018-09-14 15:14:24 +02:00
|
|
|
pub short_description: SafeString,
|
|
|
|
pub long_description: SafeString,
|
2019-03-20 17:56:17 +01:00
|
|
|
pub default_license: String,
|
2018-12-06 18:54:16 +01:00
|
|
|
pub long_description_html: SafeString,
|
|
|
|
pub short_description_html: SafeString,
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
#[derive(Clone, Insertable)]
|
2018-04-22 15:35:37 +02:00
|
|
|
#[table_name = "instances"]
|
|
|
|
pub struct NewInstance {
|
|
|
|
pub public_domain: String,
|
|
|
|
pub name: String,
|
2018-07-27 19:05:36 +02:00
|
|
|
pub local: bool,
|
|
|
|
pub open_registrations: bool,
|
2018-09-14 15:14:24 +02:00
|
|
|
pub short_description: SafeString,
|
|
|
|
pub long_description: SafeString,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub default_license: String,
|
2018-07-27 22:16:17 +02:00
|
|
|
pub long_description_html: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub short_description_html: String,
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
lazy_static! {
|
|
|
|
static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None);
|
|
|
|
}
|
|
|
|
|
2021-11-24 13:49:09 +01:00
|
|
|
const LOCAL_INSTANCE_USERNAME: &str = "__instance__";
|
2021-11-24 14:04:35 +01:00
|
|
|
static LOCAL_INSTANCE_USER: OnceCell<User> = OnceCell::new();
|
2021-11-22 15:11:32 +01:00
|
|
|
|
2018-04-22 15:35:37 +02:00
|
|
|
impl Instance {
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn set_local(self) {
|
|
|
|
LOCAL_INSTANCE.write().unwrap().replace(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_local() -> Result<Instance> {
|
|
|
|
LOCAL_INSTANCE
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.clone()
|
|
|
|
.ok_or(Error::NotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_local_uncached(conn: &Connection) -> Result<Instance> {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.filter(instances::local.eq(true))
|
2019-10-07 19:08:20 +02:00
|
|
|
.first(conn)
|
|
|
|
.map_err(Error::from)
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn cache_local(conn: &Connection) {
|
|
|
|
*LOCAL_INSTANCE.write().unwrap() = Instance::get_local_uncached(conn).ok();
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_remotes(conn: &Connection) -> Result<Vec<Instance>> {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.filter(instances::local.eq(false))
|
2018-05-01 17:51:49 +02:00
|
|
|
.load::<Instance>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-05-01 17:51:49 +02:00
|
|
|
}
|
|
|
|
|
2021-11-24 14:11:56 +01:00
|
|
|
pub fn create_local_instance_user(conn: &Connection) -> Result<User> {
|
2021-11-26 01:51:25 +01:00
|
|
|
let instance = Instance::get_local()?;
|
2021-11-24 14:11:56 +01:00
|
|
|
let email = format!("{}@{}", LOCAL_INSTANCE_USERNAME, &instance.public_domain);
|
|
|
|
NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
LOCAL_INSTANCE_USERNAME.into(),
|
|
|
|
instance.public_domain,
|
|
|
|
Role::Instance,
|
|
|
|
"Local instance",
|
|
|
|
email,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:04:35 +01:00
|
|
|
pub fn get_local_instance_user() -> Option<&'static User> {
|
|
|
|
LOCAL_INSTANCE_USER.get()
|
2018-05-01 13:48:19 +02:00
|
|
|
}
|
|
|
|
|
2021-11-24 14:04:35 +01:00
|
|
|
pub fn get_local_instance_user_uncached(conn: &Connection) -> Result<User> {
|
2021-11-22 15:11:32 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::role.eq(3))
|
|
|
|
.first(conn)
|
|
|
|
.or_else(|err| match err {
|
2021-11-24 14:11:56 +01:00
|
|
|
NotFound => Self::create_local_instance_user(conn),
|
2021-11-22 15:11:32 +01:00
|
|
|
_ => Err(Error::Db(err)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:04:35 +01:00
|
|
|
pub fn cache_local_instance_user(conn: &Connection) {
|
2021-12-05 12:57:10 +01:00
|
|
|
let _ = LOCAL_INSTANCE_USER.get_or_init(|| {
|
|
|
|
Self::get_local_instance_user_uncached(conn)
|
|
|
|
.or_else(|_| Self::create_local_instance_user(conn))
|
|
|
|
.expect("Failed to cache local instance user")
|
|
|
|
});
|
2021-11-24 14:04:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Instance>> {
|
|
|
|
instances::table
|
|
|
|
.order(instances::public_domain.asc())
|
|
|
|
.offset(min.into())
|
|
|
|
.limit((max - min).into())
|
|
|
|
.load::<Instance>(conn)
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(instances, NewInstance);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(instances);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(instances, find_by_domain, public_domain as &str);
|
2018-05-01 13:48:19 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn toggle_block(&self, conn: &Connection) -> Result<()> {
|
2018-09-08 21:07:55 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.set(instances::blocked.eq(!self.blocked))
|
2018-09-27 23:06:40 +02:00
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::from)
|
2018-05-13 13:53:58 +02:00
|
|
|
}
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-09-08 23:05:48 +02:00
|
|
|
/// id: AP object id
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn is_blocked(conn: &Connection, id: &str) -> Result<bool> {
|
2018-11-24 12:44:17 +01:00
|
|
|
for block in instances::table
|
|
|
|
.filter(instances::blocked.eq(true))
|
2018-12-29 09:36:07 +01:00
|
|
|
.get_results::<Instance>(conn)?
|
2018-11-24 12:44:17 +01:00
|
|
|
{
|
2018-11-26 10:21:52 +01:00
|
|
|
if id.starts_with(&format!("https://{}/", block.public_domain)) {
|
2018-12-29 09:36:07 +01:00
|
|
|
return Ok(true);
|
2018-09-08 23:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(false)
|
2018-09-08 23:05:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn has_admin(&self, conn: &Connection) -> Result<bool> {
|
|
|
|
users::table
|
2018-11-24 12:44:17 +01:00
|
|
|
.filter(users::instance_id.eq(self.id))
|
2019-09-13 12:28:36 +02:00
|
|
|
.filter(users::role.eq(Role::Admin as i32))
|
2018-04-22 20:17:40 +02:00
|
|
|
.load::<User>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
|
|
|
.map(|r| !r.is_empty())
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
2018-06-21 19:42:17 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn main_admin(&self, conn: &Connection) -> Result<User> {
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::instance_id.eq(self.id))
|
2019-09-13 12:28:36 +02:00
|
|
|
.filter(users::role.eq(Role::Admin as i32))
|
2019-10-07 19:08:20 +02:00
|
|
|
.first(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn compute_box(&self, prefix: &str, name: &str, box_name: &str) -> String {
|
2018-11-26 10:21:52 +01:00
|
|
|
ap_url(&format!(
|
2018-06-21 19:42:17 +02:00
|
|
|
"{instance}/{prefix}/{name}/{box_name}",
|
|
|
|
instance = self.public_domain,
|
|
|
|
prefix = prefix,
|
|
|
|
name = name,
|
|
|
|
box_name = box_name
|
|
|
|
))
|
|
|
|
}
|
2018-07-27 19:05:36 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn update(
|
|
|
|
&self,
|
|
|
|
conn: &Connection,
|
|
|
|
name: String,
|
|
|
|
open_registrations: bool,
|
|
|
|
short_description: SafeString,
|
|
|
|
long_description: SafeString,
|
2019-08-27 21:47:45 +02:00
|
|
|
default_license: String,
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<()> {
|
2019-04-06 19:20:33 +02:00
|
|
|
let (sd, _, _) = md_to_html(
|
|
|
|
short_description.as_ref(),
|
2019-05-04 17:33:50 +02:00
|
|
|
Some(&self.public_domain),
|
2019-04-06 19:20:33 +02:00
|
|
|
true,
|
|
|
|
Some(Media::get_media_processor(conn, vec![])),
|
|
|
|
);
|
|
|
|
let (ld, _, _) = md_to_html(
|
|
|
|
long_description.as_ref(),
|
2019-05-04 17:33:50 +02:00
|
|
|
Some(&self.public_domain),
|
2019-04-06 19:20:33 +02:00
|
|
|
false,
|
|
|
|
Some(Media::get_media_processor(conn, vec![])),
|
|
|
|
);
|
2019-05-10 22:59:34 +02:00
|
|
|
let res = diesel::update(self)
|
2018-07-27 19:05:36 +02:00
|
|
|
.set((
|
|
|
|
instances::name.eq(name),
|
|
|
|
instances::open_registrations.eq(open_registrations),
|
|
|
|
instances::short_description.eq(short_description),
|
|
|
|
instances::long_description.eq(long_description),
|
2018-07-27 22:16:17 +02:00
|
|
|
instances::short_description_html.eq(sd),
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::long_description_html.eq(ld),
|
2019-08-27 21:47:45 +02:00
|
|
|
instances::default_license.eq(default_license),
|
2018-11-24 12:44:17 +01:00
|
|
|
))
|
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
2019-05-10 22:59:34 +02:00
|
|
|
.map_err(Error::from);
|
|
|
|
if self.local {
|
|
|
|
Instance::cache_local(conn);
|
|
|
|
}
|
|
|
|
res
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
2018-09-01 18:39:40 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn count(conn: &Connection) -> Result<i64> {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2019-08-21 00:42:04 +02:00
|
|
|
|
|
|
|
/// Returns a list of the local instance themes (all files matching `static/css/NAME/theme.css`)
|
|
|
|
///
|
|
|
|
/// The list only contains the name of the themes, without their extension or full path.
|
|
|
|
pub fn list_themes() -> Result<Vec<String>> {
|
|
|
|
// List all the files in static/css
|
|
|
|
std::path::Path::new("static")
|
|
|
|
.join("css")
|
|
|
|
.read_dir()
|
|
|
|
.map(|files| {
|
|
|
|
files
|
|
|
|
.filter_map(std::result::Result::ok)
|
|
|
|
// Only keep actual directories (each theme has its own dir)
|
|
|
|
.filter(|f| f.file_type().map(|t| t.is_dir()).unwrap_or(false))
|
|
|
|
// Only keep the directory name (= theme name)
|
|
|
|
.filter_map(|f| {
|
|
|
|
f.path()
|
|
|
|
.file_name()
|
|
|
|
.and_then(std::ffi::OsStr::to_str)
|
|
|
|
.map(std::borrow::ToOwned::to_owned)
|
|
|
|
})
|
|
|
|
// Ignore the one starting with "blog-": these are the blog themes
|
|
|
|
.filter(|f| !f.starts_with("blog-"))
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a list of the local blog themes (all files matching `static/css/blog-NAME/theme.css`)
|
|
|
|
///
|
|
|
|
/// The list only contains the name of the themes, without their extension or full path.
|
|
|
|
pub fn list_blog_themes() -> Result<Vec<String>> {
|
|
|
|
// List all the files in static/css
|
|
|
|
std::path::Path::new("static")
|
|
|
|
.join("css")
|
|
|
|
.read_dir()
|
|
|
|
.map(|files| {
|
|
|
|
files
|
|
|
|
.filter_map(std::result::Result::ok)
|
|
|
|
// Only keep actual directories (each theme has its own dir)
|
|
|
|
.filter(|f| f.file_type().map(|t| t.is_dir()).unwrap_or(false))
|
|
|
|
// Only keep the directory name (= theme name)
|
|
|
|
.filter_map(|f| {
|
|
|
|
f.path()
|
|
|
|
.file_name()
|
|
|
|
.and_then(std::ffi::OsStr::to_str)
|
|
|
|
.map(std::borrow::ToOwned::to_owned)
|
|
|
|
})
|
|
|
|
// Only keep the one starting with "blog-": these are the blog themes
|
|
|
|
.filter(|f| f.starts_with("blog-"))
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::{tests::db, Connection as Conn};
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::Connection;
|
|
|
|
|
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> Vec<(NewInstance, Instance)> {
|
2021-01-24 15:28:40 +01:00
|
|
|
diesel::delete(instances::table).execute(conn).unwrap();
|
2019-05-10 22:59:34 +02:00
|
|
|
let res = vec![
|
2018-11-24 12:44:17 +01:00
|
|
|
NewInstance {
|
|
|
|
default_license: "WTFPL".to_string(),
|
|
|
|
local: true,
|
|
|
|
long_description: SafeString::new("This is my instance."),
|
|
|
|
long_description_html: "<p>This is my instance</p>".to_string(),
|
|
|
|
short_description: SafeString::new("My instance."),
|
|
|
|
short_description_html: "<p>My instance</p>".to_string(),
|
|
|
|
name: "My instance".to_string(),
|
|
|
|
open_registrations: true,
|
|
|
|
public_domain: "plu.me".to_string(),
|
|
|
|
},
|
|
|
|
NewInstance {
|
|
|
|
default_license: "WTFPL".to_string(),
|
|
|
|
local: false,
|
|
|
|
long_description: SafeString::new("This is an instance."),
|
|
|
|
long_description_html: "<p>This is an instance</p>".to_string(),
|
|
|
|
short_description: SafeString::new("An instance."),
|
|
|
|
short_description_html: "<p>An instance</p>".to_string(),
|
|
|
|
name: "An instance".to_string(),
|
|
|
|
open_registrations: true,
|
|
|
|
public_domain: "1plu.me".to_string(),
|
|
|
|
},
|
|
|
|
NewInstance {
|
|
|
|
default_license: "CC-0".to_string(),
|
|
|
|
local: false,
|
|
|
|
long_description: SafeString::new("This is the instance of someone."),
|
|
|
|
long_description_html: "<p>This is the instance of someone</p>".to_string(),
|
|
|
|
short_description: SafeString::new("Someone instance."),
|
|
|
|
short_description_html: "<p>Someone instance</p>".to_string(),
|
|
|
|
name: "Someone instance".to_string(),
|
|
|
|
open_registrations: false,
|
|
|
|
public_domain: "2plu.me".to_string(),
|
|
|
|
},
|
|
|
|
NewInstance {
|
|
|
|
default_license: "CC-0-BY-SA".to_string(),
|
|
|
|
local: false,
|
|
|
|
long_description: SafeString::new("Good morning"),
|
|
|
|
long_description_html: "<p>Good morning</p>".to_string(),
|
|
|
|
short_description: SafeString::new("Hello"),
|
|
|
|
short_description_html: "<p>Hello</p>".to_string(),
|
|
|
|
name: "Nice day".to_string(),
|
|
|
|
open_registrations: true,
|
|
|
|
public_domain: "3plu.me".to_string(),
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.map(|inst| {
|
|
|
|
(
|
|
|
|
inst.clone(),
|
|
|
|
Instance::find_by_domain(conn, &inst.public_domain)
|
|
|
|
.unwrap_or_else(|_| Instance::insert(conn, inst).unwrap()),
|
|
|
|
)
|
|
|
|
})
|
2019-05-10 22:59:34 +02:00
|
|
|
.collect();
|
|
|
|
Instance::cache_local(conn);
|
2021-11-26 01:51:51 +01:00
|
|
|
Instance::cache_local_instance_user(conn);
|
2019-05-10 22:59:34 +02:00
|
|
|
res
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn local_instance() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(inserted, _)| inserted)
|
|
|
|
.find(|inst| inst.local)
|
|
|
|
.unwrap();
|
2019-05-10 22:59:34 +02:00
|
|
|
let res = Instance::get_local().unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
part_eq!(
|
|
|
|
res,
|
|
|
|
inserted,
|
|
|
|
[
|
|
|
|
default_license,
|
|
|
|
local,
|
|
|
|
long_description,
|
|
|
|
short_description,
|
|
|
|
name,
|
|
|
|
open_registrations,
|
|
|
|
public_domain
|
|
|
|
]
|
|
|
|
);
|
2019-03-20 17:56:17 +01:00
|
|
|
assert_eq!(
|
|
|
|
res.long_description_html.get(),
|
|
|
|
&inserted.long_description_html
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
res.short_description_html.get(),
|
|
|
|
&inserted.short_description_html
|
|
|
|
);
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remote_instance() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn);
|
2018-12-29 09:36:07 +01:00
|
|
|
assert_eq!(Instance::count(conn).unwrap(), inserted.len() as i64);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let res = Instance::get_remotes(conn).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
assert_eq!(
|
|
|
|
res.len(),
|
|
|
|
inserted.iter().filter(|(inst, _)| !inst.local).count()
|
|
|
|
);
|
|
|
|
|
|
|
|
inserted
|
|
|
|
.iter()
|
|
|
|
.filter(|(newinst, _)| !newinst.local)
|
|
|
|
.map(|(newinst, inst)| (newinst, res.iter().find(|res| res.id == inst.id).unwrap()))
|
|
|
|
.for_each(|(newinst, inst)| {
|
|
|
|
part_eq!(
|
|
|
|
newinst,
|
|
|
|
inst,
|
|
|
|
[
|
|
|
|
default_license,
|
|
|
|
local,
|
|
|
|
long_description,
|
|
|
|
short_description,
|
|
|
|
name,
|
|
|
|
open_registrations,
|
|
|
|
public_domain
|
|
|
|
]
|
2018-12-06 18:54:16 +01:00
|
|
|
);
|
2019-03-20 17:56:17 +01:00
|
|
|
assert_eq!(
|
|
|
|
&newinst.long_description_html,
|
|
|
|
inst.long_description_html.get()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
&newinst.short_description_html,
|
|
|
|
inst.short_description_html.get()
|
|
|
|
);
|
2018-11-24 12:44:17 +01:00
|
|
|
});
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let page = Instance::page(conn, (0, 2)).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
assert_eq!(page.len(), 2);
|
|
|
|
let page1 = &page[0];
|
|
|
|
let page2 = &page[1];
|
|
|
|
assert!(page1.public_domain <= page2.public_domain);
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
let mut last_domaine: String = Instance::page(conn, (0, 1)).unwrap()[0]
|
|
|
|
.public_domain
|
|
|
|
.clone();
|
2018-11-24 12:44:17 +01:00
|
|
|
for i in 1..inserted.len() as i32 {
|
2018-12-29 09:36:07 +01:00
|
|
|
let page = Instance::page(conn, (i, i + 1)).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
assert_eq!(page.len(), 1);
|
|
|
|
assert!(last_domaine <= page[0].public_domain);
|
|
|
|
last_domaine = page[0].public_domain.clone();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn blocked() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inst_list = fill_database(conn);
|
|
|
|
let inst = &inst_list[0].1;
|
|
|
|
let inst_list = &inst_list[1..];
|
|
|
|
|
|
|
|
let blocked = inst.blocked;
|
2018-12-29 09:36:07 +01:00
|
|
|
inst.toggle_block(conn).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
let inst = Instance::get(conn, inst.id).unwrap();
|
|
|
|
assert_eq!(inst.blocked, !blocked);
|
|
|
|
assert_eq!(
|
|
|
|
inst_list
|
|
|
|
.iter()
|
|
|
|
.filter(
|
|
|
|
|(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked
|
|
|
|
)
|
|
|
|
.count(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-03-20 17:56:17 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain))
|
|
|
|
.unwrap(),
|
2018-11-24 12:44:17 +01:00
|
|
|
inst.blocked
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-03-20 17:56:17 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain))
|
|
|
|
.unwrap(),
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::find_by_domain(conn, &format!("{}a", inst.public_domain))
|
2018-11-24 12:44:17 +01:00
|
|
|
.map(|inst| inst.blocked)
|
|
|
|
.unwrap_or(false)
|
|
|
|
);
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
inst.toggle_block(conn).unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
let inst = Instance::get(conn, inst.id).unwrap();
|
|
|
|
assert_eq!(inst.blocked, blocked);
|
|
|
|
assert_eq!(
|
2019-03-20 17:56:17 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain))
|
|
|
|
.unwrap(),
|
2018-11-24 12:44:17 +01:00
|
|
|
inst.blocked
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-03-20 17:56:17 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain))
|
|
|
|
.unwrap(),
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::find_by_domain(conn, &format!("{}a", inst.public_domain))
|
2018-11-24 12:44:17 +01:00
|
|
|
.map(|inst| inst.blocked)
|
|
|
|
.unwrap_or(false)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
inst_list
|
|
|
|
.iter()
|
|
|
|
.filter(
|
|
|
|
|(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked
|
|
|
|
)
|
|
|
|
.count(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inst = &fill_database(conn)[0].1;
|
|
|
|
|
|
|
|
inst.update(
|
|
|
|
conn,
|
|
|
|
"NewName".to_owned(),
|
|
|
|
false,
|
|
|
|
SafeString::new("[short](#link)"),
|
|
|
|
SafeString::new("[long_description](/with_link)"),
|
2019-08-27 21:47:45 +02:00
|
|
|
"CC-BY-SAO".to_owned(),
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
let inst = Instance::get(conn, inst.id).unwrap();
|
|
|
|
assert_eq!(inst.name, "NewName".to_owned());
|
|
|
|
assert_eq!(inst.open_registrations, false);
|
|
|
|
assert_eq!(
|
|
|
|
inst.long_description.get(),
|
|
|
|
"[long_description](/with_link)"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
inst.long_description_html,
|
2021-01-02 01:12:45 +01:00
|
|
|
SafeString::new(
|
|
|
|
"<p dir=\"auto\"><a href=\"/with_link\">long_description</a></p>\n"
|
|
|
|
)
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
|
|
|
assert_eq!(inst.short_description.get(), "[short](#link)");
|
|
|
|
assert_eq!(
|
|
|
|
inst.short_description_html,
|
2021-01-02 01:00:07 +01:00
|
|
|
SafeString::new("<p dir=\"auto\"><a href=\"#link\">short</a></p>\n")
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
2019-08-27 21:47:45 +02:00
|
|
|
assert_eq!(inst.default_license, "CC-BY-SAO".to_owned());
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
|
|
|
});
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|