2018-04-24 11:21:39 +02:00
|
|
|
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
|
2018-04-22 15:35:37 +02:00
|
|
|
use std::iter::Iterator;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-04-23 17:19:28 +02:00
|
|
|
use models::users::User;
|
2018-04-24 11:21:39 +02:00
|
|
|
use schema::{instances, users};
|
2018-04-22 15:35:37 +02:00
|
|
|
|
|
|
|
#[derive(Identifiable, Queryable)]
|
|
|
|
pub struct Instance {
|
|
|
|
pub id: i32,
|
|
|
|
pub local_domain: String,
|
|
|
|
pub public_domain: String,
|
|
|
|
pub name: String,
|
|
|
|
pub local: bool,
|
|
|
|
pub blocked: bool
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "instances"]
|
|
|
|
pub struct NewInstance {
|
|
|
|
pub local_domain: String,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert<'a>(conn: &PgConnection, loc_dom: String, pub_dom: String, name: String, local: bool) -> Instance {
|
|
|
|
diesel::insert_into(instances::table)
|
|
|
|
.values(NewInstance {
|
|
|
|
local_domain: loc_dom,
|
|
|
|
public_domain: pub_dom,
|
|
|
|
name: name,
|
|
|
|
local: local
|
|
|
|
})
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Error saving new instance")
|
|
|
|
}
|
|
|
|
|
2018-04-22 17:11:58 +02:00
|
|
|
pub fn get(conn: &PgConnection, id: i32) -> Option<Instance> {
|
|
|
|
instances::table.filter(instances::id.eq(id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Instance>(conn)
|
|
|
|
.expect("Error loading local instance infos")
|
|
|
|
.into_iter().nth(0)
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block(&self) {}
|
|
|
|
|
|
|
|
pub fn has_admin(&self, conn: &PgConnection) -> 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)
|
|
|
|
.expect("Couldn't load admins")
|
|
|
|
.len() > 0
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
}
|