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};
|
2018-05-13 19:39:18 +02:00
|
|
|
use serde_json;
|
2018-04-22 15:35:37 +02:00
|
|
|
use std::iter::Iterator;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-05-13 19:39:18 +02:00
|
|
|
use activity_pub::inbox::Inbox;
|
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
|
|
|
|
2018-05-09 21:09:52 +02:00
|
|
|
#[derive(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,
|
|
|
|
pub creation_date: NaiveDateTime
|
2018-04-22 15:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:53:42 +02:00
|
|
|
pub fn insert<'a>(conn: &PgConnection, pub_dom: String, name: String, local: bool) -> Instance {
|
2018-04-22 15:35:37 +02:00
|
|
|
diesel::insert_into(instances::table)
|
|
|
|
.values(NewInstance {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-13 13:53:58 +02:00
|
|
|
pub fn find_by_domain(conn: &PgConnection, domain: String) -> Option<Instance> {
|
2018-05-01 13:48:19 +02:00
|
|
|
instances::table.filter(instances::public_domain.eq(domain))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Instance>(conn)
|
|
|
|
.expect("Couldn't load instance by domain")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:53:58 +02:00
|
|
|
pub fn block(&self) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-04-22 15:35:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-05-13 19:39:18 +02:00
|
|
|
|
|
|
|
impl Inbox for Instance {
|
|
|
|
fn received(&self, conn: &PgConnection, act: serde_json::Value) {
|
2018-05-16 20:20:44 +02:00
|
|
|
self.save(conn, act.clone()).unwrap();
|
2018-05-13 19:39:18 +02:00
|
|
|
|
|
|
|
// TODO: add to stream, or whatever needs to be done
|
|
|
|
}
|
|
|
|
}
|