Simplify the Inbox trait

If we want to add, for instance, streams in the future, we could introduce
a new trait for that, similar to FromActivity or Notify

We also display inbox errors to the "client" if something fails,
which could be useful for debugging.
This commit is contained in:
Bat 2018-06-21 17:00:37 +01:00
parent 5193ad6f65
commit 3fe2625e86
6 changed files with 19 additions and 25 deletions

View File

@ -50,9 +50,7 @@ pub trait Deletable {
} }
pub trait Inbox { pub trait Inbox {
fn received(&self, conn: &PgConnection, act: serde_json::Value); fn received(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> {
fn save(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> {
let actor_id = Id::new(act["actor"].as_str().unwrap()); let actor_id = Id::new(act["actor"].as_str().unwrap());
match act["type"].as_str() { match act["type"].as_str() {
Some(t) => { Some(t) => {

View File

@ -1,6 +1,5 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use serde_json;
use std::iter::Iterator; use std::iter::Iterator;
use activity_pub::inbox::Inbox; use activity_pub::inbox::Inbox;
@ -61,10 +60,4 @@ impl Instance {
} }
} }
impl Inbox for Instance { impl Inbox for Instance {}
fn received(&self, conn: &PgConnection, act: serde_json::Value) {
self.save(conn, act.clone()).expect("Shared Inbox: Couldn't save activity");
// TODO: add to stream, or whatever needs to be done
}
}

View File

@ -441,15 +441,7 @@ impl WithInbox for User {
} }
} }
impl Inbox for User { impl Inbox for User {}
fn received(&self, conn: &PgConnection, act: serde_json::Value) {
if let Err(err) = self.save(conn, act.clone()) {
println!("Inbox error:\n{}\n{}\n\nActivity was: {}", err.cause(), err.backtrace(), act.to_string());
}
// TODO: add to stream, or whatever needs to be done
}
}
impl Signer for User { impl Signer for User {
fn get_key_id(&self) -> String { fn get_key_id(&self) -> String {

View File

@ -44,7 +44,8 @@ fn create_response(blog_name: String, slug: String, query: Option<CommentQuery>,
.create(&*conn); .create(&*conn);
let instance = Instance::get_local(&*conn).unwrap(); let instance = Instance::get_local(&*conn).unwrap();
instance.received(&*conn, serde_json::to_value(new_comment.clone()).expect("JSON serialization error")); instance.received(&*conn, serde_json::to_value(new_comment.clone()).expect("JSON serialization error"))
.expect("We are not compatible with ourselve: local broadcast failed (new comment)");
broadcast(&user, new_comment, user.get_followers(&*conn)); broadcast(&user, new_comment, user.get_followers(&*conn));
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id)) Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id))

View File

@ -35,8 +35,13 @@ fn index(conn: DbConn, user: Option<User>) -> Template {
fn shared_inbox(conn: DbConn, data: String) -> String { fn shared_inbox(conn: DbConn, data: String) -> String {
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
let instance = Instance::get_local(&*conn).unwrap(); let instance = Instance::get_local(&*conn).unwrap();
instance.received(&*conn, act); match instance.received(&*conn, act) {
String::from("") Ok(_) => String::new(),
Err(e) => {
println!("Shared inbox error: {}\n{}", e.cause(), e.backtrace());
format!("Error: {}", e.cause())
}
}
} }
#[get("/nodeinfo")] #[get("/nodeinfo")]

View File

@ -199,8 +199,13 @@ fn outbox(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
fn inbox(name: String, conn: DbConn, data: String) -> String { fn inbox(name: String, conn: DbConn, data: String) -> String {
let user = User::find_local(&*conn, name).unwrap(); let user = User::find_local(&*conn, name).unwrap();
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
user.received(&*conn, act); match user.received(&*conn, act) {
String::from("") Ok(_) => String::new(),
Err(e) => {
println!("User inbox error: {}\n{}", e.cause(), e.backtrace());
format!("Error: {}", e.cause())
}
}
} }
#[get("/@/<name>/followers", format = "application/activity+json")] #[get("/@/<name>/followers", format = "application/activity+json")]