From 3fe2625e86b2a1152bc9a4ffe4029947ebc02887 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 21 Jun 2018 17:00:37 +0100 Subject: [PATCH] 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. --- src/activity_pub/inbox.rs | 4 +--- src/models/instance.rs | 9 +-------- src/models/users.rs | 10 +--------- src/routes/comments.rs | 3 ++- src/routes/instance.rs | 9 +++++++-- src/routes/user.rs | 9 +++++++-- 6 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index f1a3e2b9..012108e8 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -50,9 +50,7 @@ pub trait Deletable { } pub trait Inbox { - fn received(&self, conn: &PgConnection, act: serde_json::Value); - - fn save(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> { + fn received(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> { let actor_id = Id::new(act["actor"].as_str().unwrap()); match act["type"].as_str() { Some(t) => { diff --git a/src/models/instance.rs b/src/models/instance.rs index b7128da6..b627718b 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -1,6 +1,5 @@ use chrono::NaiveDateTime; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; -use serde_json; use std::iter::Iterator; use activity_pub::inbox::Inbox; @@ -61,10 +60,4 @@ impl 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 - } -} +impl Inbox for Instance {} diff --git a/src/models/users.rs b/src/models/users.rs index 5190c614..300c5fa9 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -441,15 +441,7 @@ impl WithInbox 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 Inbox for User {} impl Signer for User { fn get_key_id(&self) -> String { diff --git a/src/routes/comments.rs b/src/routes/comments.rs index 2bb48d5e..aa9997fc 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -44,7 +44,8 @@ fn create_response(blog_name: String, slug: String, query: Option, .create(&*conn); 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)); Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id)) diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 9463511f..4c7816f0 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -35,8 +35,13 @@ fn index(conn: DbConn, user: Option) -> Template { fn shared_inbox(conn: DbConn, data: String) -> String { let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap(); let instance = Instance::get_local(&*conn).unwrap(); - instance.received(&*conn, act); - String::from("") + match instance.received(&*conn, act) { + Ok(_) => String::new(), + Err(e) => { + println!("Shared inbox error: {}\n{}", e.cause(), e.backtrace()); + format!("Error: {}", e.cause()) + } + } } #[get("/nodeinfo")] diff --git a/src/routes/user.rs b/src/routes/user.rs index cc125d34..a6d00527 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -199,8 +199,13 @@ fn outbox(name: String, conn: DbConn) -> ActivityStream { 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(); - user.received(&*conn, act); - String::from("") + match user.received(&*conn, act) { + Ok(_) => String::new(), + Err(e) => { + println!("User inbox error: {}\n{}", e.cause(), e.backtrace()); + format!("Error: {}", e.cause()) + } + } } #[get("/@//followers", format = "application/activity+json")]