Actually implement blocks

This commit is contained in:
Bat 2018-09-08 22:05:48 +01:00
parent 0a70ff2594
commit f66ddf6ef2
3 changed files with 28 additions and 1 deletions

View file

@ -75,6 +75,19 @@ impl Instance {
.expect("Couldn't block/unblock instance");
}
/// id: AP object id
pub fn is_blocked(conn: &PgConnection, id: String) -> bool {
for block in instances::table.filter(instances::blocked.eq(true))
.get_results::<Instance>(conn)
.expect("Error listing blocked instances") {
if id.starts_with(format!("https://{}", block.public_domain).as_str()) {
return true;
}
}
false
}
pub fn has_admin(&self, conn: &PgConnection) -> bool {
users::table.filter(users::instance_id.eq(self.id))
.filter(users::is_admin.eq(true))

View file

@ -155,7 +155,7 @@ fn admin_instances_paginated(admin: Admin, conn: DbConn, page: Page) -> Template
}
#[get("/admin/instances/<id>/block")]
fn toggle_block(admin: Admin, conn: DbConn, id: i32) -> Redirect {
fn toggle_block(_admin: Admin, conn: DbConn, id: i32) -> Redirect {
if let Some(inst) = Instance::get(&*conn, id) {
inst.toggle_block(&*conn);
}
@ -166,6 +166,13 @@ fn toggle_block(admin: Admin, conn: DbConn, id: i32) -> Redirect {
#[post("/inbox", data = "<data>")]
fn shared_inbox(conn: DbConn, data: String) -> String {
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
let activity = act.clone();
let actor_id = activity["actor"].as_str()
.unwrap_or_else(|| activity["actor"]["id"].as_str().expect("No actor ID for incoming activity, blocks by panicking"));
if Instance::is_blocked(&*conn, actor_id.to_string()) {
return String::new();
}
let instance = Instance::get_local(&*conn).unwrap();
match instance.received(&*conn, act) {
Ok(_) => String::new(),

View file

@ -282,6 +282,13 @@ fn outbox(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
fn inbox(name: String, conn: DbConn, data: String) -> String {
let user = User::find_local(&*conn, name).unwrap();
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
let activity = act.clone();
let actor_id = activity["actor"].as_str()
.unwrap_or_else(|| activity["actor"]["id"].as_str().expect("User: No actor ID for incoming activity, blocks by panicking"));
if Instance::is_blocked(&*conn, actor_id.to_string()) {
return String::new();
}
match user.received(&*conn, act) {
Ok(_) => String::new(),
Err(e) => {