diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index f3f77d90..ce2e91e3 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -9,6 +9,8 @@ use activity_pub::object::Object; pub trait Activity { fn get_id(&self) -> String; + fn get_type(&self) -> String; + fn serialize(&self) -> serde_json::Value; // fn deserialize(serde_json::Value) -> Self; @@ -25,7 +27,7 @@ pub struct Accept { impl Accept { pub fn new(who: &B, what: &A, conn: &PgConnection) -> Accept { Accept { - id: "TODO".to_string(), + id: format!("{}/accept/{}/{}", who.compute_id(conn), what.get_type().to_lowercase(), what.get_id()), actor: serde_json::Value::String(who.compute_id(conn)), object: serde_json::Value::String(what.get_id()), date: chrono::Utc::now() @@ -38,6 +40,10 @@ impl Activity for Accept { self.id.clone() } + fn get_type(&self) -> String { + "Accept".to_string() + } + fn serialize(&self) -> serde_json::Value { json!({ "type": "Accept", @@ -59,7 +65,7 @@ pub struct Create { impl Create { pub fn new(actor: &A, obj: &B, conn: &PgConnection) -> Create { Create { - id: "TODO".to_string(), + id: format!("{}/activity", obj.compute_id(conn)), actor: serde_json::Value::String(actor.compute_id(conn)), object: obj.serialize(conn), date: chrono::Utc::now() @@ -72,6 +78,10 @@ impl Activity for Create { self.id.clone() } + fn get_type(&self) -> String { + "Create".to_string() + } + fn serialize(&self) -> serde_json::Value { json!({ "type": "Create", @@ -93,7 +103,7 @@ pub struct Follow { impl Follow { pub fn new(follower: &A, following: &B, conn: &PgConnection) -> Follow { Follow { - id: "TODO".to_string(), + id: format!("{}/follow/{}", follower.compute_id(conn), following.compute_id(conn)), actor: serde_json::Value::String(follower.compute_id(conn)), object: serde_json::Value::String(following.compute_id(conn)), date: chrono::Utc::now() @@ -119,6 +129,10 @@ impl Activity for Follow { self.id.clone() } + fn get_type(&self) -> String { + "Follow".to_string() + } + fn serialize(&self) -> serde_json::Value { json!({ "type": "Follow", diff --git a/src/activity_pub/object.rs b/src/activity_pub/object.rs index ff5338b8..c705d236 100644 --- a/src/activity_pub/object.rs +++ b/src/activity_pub/object.rs @@ -5,6 +5,8 @@ use activity_pub::actor::Actor; pub trait Object { fn serialize(&self, conn: &PgConnection) -> serde_json::Value; + + fn compute_id(&self, conn: &PgConnection) -> String; } pub trait Attribuable { diff --git a/src/models/posts.rs b/src/models/posts.rs index 1dfc0eb8..2da6d002 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -3,9 +3,11 @@ use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, Belon use diesel::dsl::any; use serde_json; -use activity_pub::PUBLIC_VISIBILTY; +use BASE_URL; +use activity_pub::{PUBLIC_VISIBILTY, ap_url}; use activity_pub::actor::Actor; use activity_pub::object::Object; +use models::blogs::Blog; use models::users::User; use models::post_authors::PostAuthor; use schema::posts; @@ -63,9 +65,22 @@ impl Post { let author_list = PostAuthor::belonging_to(self).select(post_authors::author_id); users::table.filter(users::id.eq(any(author_list))).load::(conn).unwrap() } + + pub fn get_blog(&self, conn: &PgConnection) -> Blog { + use schema::blogs; + blogs::table.filter(blogs::id.eq(self.blog_id)) + .limit(1) + .load::(conn) + .expect("Couldn't load blog associted to post") + .into_iter().nth(0).unwrap() + } } impl Object for Post { + fn compute_id(&self, conn: &PgConnection) -> String { + ap_url(format!("{}/{}/{}", BASE_URL.as_str(), self.get_blog(conn).actor_id, self.slug)) + } + fn serialize(&self, conn: &PgConnection) -> serde_json::Value { json!({ "type": "Article",