2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-09-26 17:22:42 +02:00
|
|
|
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods};
|
2018-04-22 15:35:37 +02:00
|
|
|
use std::iter::Iterator;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-07-27 22:16:17 +02:00
|
|
|
use plume_common::utils::md_to_html;
|
2018-09-27 23:06:40 +02:00
|
|
|
use Connection;
|
2018-09-14 15:14:24 +02:00
|
|
|
use safe_string::SafeString;
|
2018-06-26 16:21:58 +02:00
|
|
|
use ap_url;
|
2018-06-23 18:36:11 +02:00
|
|
|
use users::User;
|
2018-04-24 11:21:39 +02:00
|
|
|
use schema::{instances, users};
|
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-07-27 22:16:17 +02:00
|
|
|
pub default_license : String,
|
|
|
|
pub long_description_html: String,
|
|
|
|
pub short_description_html: String
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[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-07-27 22:16:17 +02:00
|
|
|
pub default_license : String,
|
|
|
|
pub long_description_html: String,
|
|
|
|
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-04-22 15:35:37 +02:00
|
|
|
instances::table.filter(instances::local.eq(true))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Instance>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::get_local: loading error")
|
2018-04-22 15:35:37 +02:00
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_remotes(conn: &Connection) -> Vec<Instance> {
|
2018-05-01 17:51:49 +02:00
|
|
|
instances::table.filter(instances::local.eq(false))
|
|
|
|
.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-09-08 20:54:09 +02:00
|
|
|
instances::table.order(instances::public_domain.asc())
|
|
|
|
.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-10-20 08:44:33 +02: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-06-18 15:37:49 +02:00
|
|
|
find_by!(instances, find_by_domain, public_domain as String);
|
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-09-26 17:22:42 +02:00
|
|
|
pub fn is_blocked(conn: &Connection, id: String) -> bool {
|
2018-09-08 23:05:48 +02:00
|
|
|
for block in instances::table.filter(instances::blocked.eq(true))
|
|
|
|
.get_results::<Instance>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::is_blocked: loading error") {
|
2018-09-08 23:05:48 +02:00
|
|
|
if id.starts_with(format!("https://{}", block.public_domain).as_str()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn has_admin(&self, conn: &Connection) -> bool {
|
2018-04-22 20:17:40 +02:00
|
|
|
users::table.filter(users::instance_id.eq(self.id))
|
|
|
|
.filter(users::is_admin.eq(true))
|
|
|
|
.load::<User>(conn)
|
2018-10-20 08:44:33 +02:00
|
|
|
.expect("Instance::has_admin: loading error")
|
2018-04-22 20:17:40 +02:00
|
|
|
.len() > 0
|
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-09-01 18:39:40 +02:00
|
|
|
users::table.filter(users::instance_id.eq(self.id))
|
|
|
|
.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-06-21 19:42:17 +02:00
|
|
|
pub fn compute_box(&self, prefix: &'static str, name: String, box_name: &'static str) -> String {
|
|
|
|
ap_url(format!(
|
|
|
|
"{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-09-27 23:06:40 +02:00
|
|
|
pub fn update(&self, conn: &Connection, name: String, open_registrations: bool, short_description: SafeString, long_description: SafeString) {
|
2018-10-20 16:38:16 +02:00
|
|
|
let (sd, _, _) = md_to_html(short_description.as_ref());
|
|
|
|
let (ld, _, _) = md_to_html(long_description.as_ref());
|
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),
|
|
|
|
instances::long_description_html.eq(ld)
|
2018-09-27 23:06:40 +02:00
|
|
|
)).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-10-20 08:44:33 +02:00
|
|
|
instances::table.count().get_result(conn).expect("Instance::count: counting error")
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|