diff --git a/src/models/comments.rs b/src/models/comments.rs index 3bdf838b..dbb95217 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -1,6 +1,11 @@ use chrono; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; +use serde_json; +use activity_pub::{ap_url, PUBLIC_VISIBILTY}; +use activity_pub::actor::Actor; +use activity_pub::object::Object; +use models::posts::Post; use models::users::User; use schema::comments; @@ -62,4 +67,36 @@ impl Comment { pub fn get_author(&self, conn: &PgConnection) -> User { User::get(conn, self.author_id).unwrap() } + + pub fn get_post(&self, conn: &PgConnection) -> Post { + Post::get(conn, self.post_id).unwrap() + } +} + +impl Object for Comment { + fn serialize(&self, conn: &PgConnection) -> serde_json::Value { + let mut to = self.get_author(conn).get_followers(conn).into_iter().map(|f| f.ap_url).collect::>(); + to.append(&mut self.get_post(conn).get_receivers_urls(conn)); + to.push(PUBLIC_VISIBILTY.to_string()); + + json!({ + "id": self.compute_id(conn), + "type": "Note", + "summary": self.spoiler_text, + "content": self.content, + "inReplyTo": self.in_response_to_id.map_or_else(|| self.get_post(conn).ap_url, |id| { + let comm = Comment::get(conn, id).unwrap(); + comm.ap_url.clone().unwrap_or(comm.compute_id(conn)) + }), + "published": self.creation_date, + "attributedTo": self.get_author(conn).compute_id(conn), + "to": to, + "cc": [], + "sensitive": self.sensitive, + }) + } + + fn compute_id(&self, conn: &PgConnection) -> String { + ap_url(format!("{}#comment-{}", self.get_post(conn).compute_id(conn), self.id)) + } } diff --git a/src/models/posts.rs b/src/models/posts.rs index 7551d94a..992f6631 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -92,6 +92,17 @@ impl Post { .get_result::(conn).expect("Couldn't update AP URL"); } } + + pub fn get_receivers_urls(&self, conn: &PgConnection) -> Vec { + let followers = self.get_authors(conn).into_iter().map(|a| a.get_followers(conn)).collect::>>(); + let to = followers.into_iter().fold(vec![], |mut acc, f| { + for x in f { + acc.push(x.ap_url); + } + acc + }); + to + } } impl Object for Post { @@ -100,13 +111,7 @@ impl Object for Post { } fn serialize(&self, conn: &PgConnection) -> serde_json::Value { - let followers = self.get_authors(conn).into_iter().map(|a| a.get_followers(conn)).collect::>>(); - let mut to = followers.into_iter().fold(vec![], |mut acc, f| { - for x in f { - acc.push(x.ap_url); - } - acc - }); + let mut to = self.get_receivers_urls(conn); to.push(PUBLIC_VISIBILTY.to_string()); json!({ diff --git a/src/routes/comments.rs b/src/routes/comments.rs index b51fb55f..abb42566 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -2,6 +2,8 @@ use rocket::request::Form; use rocket::response::Redirect; use rocket_contrib::Template; +use activity_pub::activity::Create; +use activity_pub::outbox::broadcast; use db_conn::DbConn; use models::comments::*; use models::posts::Post; @@ -38,5 +40,8 @@ fn create(blog: String, slug: String, query: CommentQuery, data: Form