2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-04-22 15:35:37 +02:00
|
|
|
use std::iter::Iterator;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
use ap_url;
|
2018-07-27 22:16:17 +02:00
|
|
|
use plume_common::utils::md_to_html;
|
2018-09-14 15:14:24 +02:00
|
|
|
use safe_string::SafeString;
|
2018-04-24 11:21:39 +02:00
|
|
|
use schema::{instances, users};
|
2018-11-24 12:44:17 +01:00
|
|
|
use users::User;
|
|
|
|
use Connection;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
#[derive(Clone, Identifiable, Queryable, Serialize)]
|
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,
|
2018-12-06 18:54:16 +01:00
|
|
|
pub default_license : String,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance {
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_local(conn: &Connection) -> Option<Instance> {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.filter(instances::local.eq(true))
|
2018-04-22 15:35:37 +02:00
|
|
|
.limit(1)
|
|
|
|
.load::<Instance>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::get_local: loading error")
|
2018-11-24 12:44:17 +01:00
|
|
|
.into_iter()
|
|
|
|
.nth(0)
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_remotes(conn: &Connection) -> 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-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::get_remotes: loading error")
|
2018-05-01 17:51:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Vec<Instance> {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.order(instances::public_domain.asc())
|
2018-09-08 20:54:09 +02:00
|
|
|
.offset(min.into())
|
|
|
|
.limit((max - min).into())
|
|
|
|
.load::<Instance>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::page: loading error")
|
2018-09-08 20:54:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn local_id(conn: &Connection) -> i32 {
|
2018-11-24 12:44:17 +01:00
|
|
|
Instance::get_local(conn)
|
|
|
|
.expect("Instance::local_id: local instance not found error")
|
|
|
|
.id
|
2018-05-01 13:48:19 +02:00
|
|
|
}
|
|
|
|
|
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-09-26 17:22:42 +02:00
|
|
|
pub fn toggle_block(&self, conn: &Connection) {
|
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-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::toggle_block: update error");
|
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-11-26 10:21:52 +01:00
|
|
|
pub fn is_blocked(conn: &Connection, id: &str) -> bool {
|
2018-11-24 12:44:17 +01:00
|
|
|
for block in instances::table
|
|
|
|
.filter(instances::blocked.eq(true))
|
2018-09-08 23:05:48 +02:00
|
|
|
.get_results::<Instance>(conn)
|
2018-11-24 12:44:17 +01:00
|
|
|
.expect("Instance::is_blocked: loading error")
|
|
|
|
{
|
2018-11-26 10:21:52 +01:00
|
|
|
if id.starts_with(&format!("https://{}/", block.public_domain)) {
|
2018-09-08 23:05:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn has_admin(&self, conn: &Connection) -> bool {
|
2018-11-26 10:21:52 +01:00
|
|
|
!users::table
|
2018-11-24 12:44:17 +01:00
|
|
|
.filter(users::instance_id.eq(self.id))
|
2018-04-22 20:17:40 +02:00
|
|
|
.filter(users::is_admin.eq(true))
|
|
|
|
.load::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::has_admin: loading error")
|
2018-11-26 10:21:52 +01:00
|
|
|
.is_empty()
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
2018-06-21 19:42:17 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn main_admin(&self, conn: &Connection) -> User {
|
2018-11-24 12:44:17 +01:00
|
|
|
users::table
|
|
|
|
.filter(users::instance_id.eq(self.id))
|
2018-09-01 18:39:40 +02:00
|
|
|
.filter(users::is_admin.eq(true))
|
|
|
|
.limit(1)
|
|
|
|
.get_result::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::main_admin: loading error")
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn compute_box(
|
|
|
|
&self,
|
2018-11-26 10:21:52 +01:00
|
|
|
prefix: &str,
|
|
|
|
name: &str,
|
|
|
|
box_name: &str,
|
2018-11-24 12:44:17 +01:00
|
|
|
) -> 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,
|
|
|
|
) {
|
2018-12-23 11:12:15 +01:00
|
|
|
let (sd, _, _) = md_to_html(short_description.as_ref(), &self.public_domain);
|
|
|
|
let (ld, _, _) = md_to_html(long_description.as_ref(), &self.public_domain);
|
2018-07-27 19:05:36 +02:00
|
|
|
diesel::update(self)
|
|
|
|
.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),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::update: update error");
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
2018-09-01 18:39:40 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn count(conn: &Connection) -> i64 {
|
2018-11-24 12:44:17 +01:00
|
|
|
instances::table
|
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Instance::count: counting error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
|
|
|
use diesel::Connection;
|
|
|
|
use tests::db;
|
|
|
|
use Connection as Conn;
|
|
|
|
|
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> Vec<(NewInstance, Instance)> {
|
|
|
|
vec![
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
].into_iter()
|
|
|
|
.map(|inst| {
|
|
|
|
(
|
|
|
|
inst.clone(),
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::find_by_domain(conn, &inst.public_domain)
|
2018-11-24 12:44:17 +01:00
|
|
|
.unwrap_or_else(|| Instance::insert(conn, inst)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
let res = Instance::get_local(conn).unwrap();
|
|
|
|
|
|
|
|
part_eq!(
|
|
|
|
res,
|
|
|
|
inserted,
|
|
|
|
[
|
|
|
|
default_license,
|
|
|
|
local,
|
|
|
|
long_description,
|
|
|
|
short_description,
|
|
|
|
name,
|
|
|
|
open_registrations,
|
|
|
|
public_domain
|
|
|
|
]
|
|
|
|
);
|
2018-12-06 18:54:16 +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
|
|
|
assert_eq!(Instance::local_id(conn), res.id);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remote_instance() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let inserted = fill_database(conn);
|
|
|
|
assert_eq!(Instance::count(conn), inserted.len() as i64);
|
|
|
|
|
|
|
|
let res = Instance::get_remotes(conn);
|
|
|
|
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
|
|
|
);
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
let page = Instance::page(conn, (0, 2));
|
|
|
|
assert_eq!(page.len(), 2);
|
|
|
|
let page1 = &page[0];
|
|
|
|
let page2 = &page[1];
|
|
|
|
assert!(page1.public_domain <= page2.public_domain);
|
|
|
|
|
|
|
|
let mut last_domaine: String = Instance::page(conn, (0, 1))[0].public_domain.clone();
|
|
|
|
for i in 1..inserted.len() as i32 {
|
|
|
|
let page = Instance::page(conn, (i, i + 1));
|
|
|
|
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;
|
|
|
|
inst.toggle_block(conn);
|
|
|
|
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!(
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)),
|
2018-11-24 12:44:17 +01:00
|
|
|
inst.blocked
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)),
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
|
|
|
inst.toggle_block(conn);
|
|
|
|
let inst = Instance::get(conn, inst.id).unwrap();
|
|
|
|
assert_eq!(inst.blocked, blocked);
|
|
|
|
assert_eq!(
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)),
|
2018-11-24 12:44:17 +01:00
|
|
|
inst.blocked
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-11-26 10:21:52 +01:00
|
|
|
Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)),
|
|
|
|
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)"),
|
|
|
|
);
|
|
|
|
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,
|
2018-12-06 18:54:16 +01:00
|
|
|
SafeString::new("<p><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,
|
2018-12-06 18:54:16 +01:00
|
|
|
SafeString::new("<p><a href=\"#link\">short</a></p>\n")
|
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
|
|
|
}
|