diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index e6629119..6b2c624a 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -1,7 +1,7 @@ use activitystreams_traits::Actor; use activitystreams_types::{ actor::Person, - activity::{Accept, Create, Follow, Like, Undo}, + activity::{Accept, Announce, Create, Follow, Like, Undo}, object::{Article, Note} }; use diesel::PgConnection; @@ -19,6 +19,7 @@ use models::{ follows, likes, posts::*, + reshares::*, users::User }; @@ -94,10 +95,22 @@ pub trait Inbox { Ok(()) } + fn announce(&self, conn: &PgConnection, announce: Announce) -> Result<(), Error> { + let user = User::from_url(conn, announce.actor.as_str().unwrap().to_string()); + let post = Post::find_by_ap_url(conn, announce.object.as_str().unwrap().to_string()); + Reshare::insert(conn, NewReshare { + post_id: post.unwrap().id, + user_id: user.unwrap().id, + ap_url: announce.object_props.id_string()? + }); + Ok(()) + } + fn save(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> { match act["type"].as_str() { Some(t) => { match t { + "Announce" => self.announce(conn, serde_json::from_value(act.clone())?), "Create" => { let act: Create = serde_json::from_value(act.clone())?; match act.object["type"].as_str().unwrap() { diff --git a/src/models/users.rs b/src/models/users.rs index 3a38de9c..dcfe2e9f 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -438,7 +438,9 @@ impl WithInbox for User { impl Inbox for User { fn received(&self, conn: &PgConnection, act: serde_json::Value) { - self.save(conn, act.clone()).unwrap(); + if let Err(err) = self.save(conn, act.clone()) { + println!("Inbox error:\n{}\n{}", err.cause(), err.backtrace()); + } // Notifications match act["type"].as_str().unwrap() {