Plume/plume-models/src/instance.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2018-04-30 19:46:27 +02:00
use chrono::NaiveDateTime;
2018-04-24 11:21:39 +02:00
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use std::iter::Iterator;
2018-04-24 11:21:39 +02:00
use ap_url;
use users::User;
2018-04-24 11:21:39 +02:00
use schema::{instances, users};
2018-05-09 21:09:52 +02:00
#[derive(Identifiable, Queryable, Serialize)]
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,
pub creation_date: NaiveDateTime
}
#[derive(Insertable)]
#[table_name = "instances"]
pub struct NewInstance {
pub public_domain: String,
pub name: String,
pub local: bool
}
impl Instance {
pub fn get_local(conn: &PgConnection) -> Option<Instance> {
instances::table.filter(instances::local.eq(true))
.limit(1)
.load::<Instance>(conn)
.expect("Error loading local instance infos")
.into_iter().nth(0)
}
2018-05-01 17:51:49 +02:00
pub fn get_remotes(conn: &PgConnection) -> Vec<Instance> {
instances::table.filter(instances::local.eq(false))
.load::<Instance>(conn)
.expect("Error loading remote instances infos")
}
2018-05-01 13:48:19 +02:00
pub fn local_id(conn: &PgConnection) -> i32 {
Instance::get_local(conn).unwrap().id
}
insert!(instances, NewInstance);
get!(instances);
find_by!(instances, find_by_domain, public_domain as String);
2018-05-01 13:48:19 +02:00
pub fn block(&self) {
unimplemented!()
}
pub fn has_admin(&self, conn: &PgConnection) -> bool {
users::table.filter(users::instance_id.eq(self.id))
.filter(users::is_admin.eq(true))
.load::<User>(conn)
.expect("Couldn't load admins")
.len() > 0
}
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
))
}
}