Federate comments written in Plume

pull/12/head
Bat 6 years ago
parent b506e93bd8
commit b81b9f90ec

@ -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::<Vec<String>>();
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))
}
}

@ -92,6 +92,17 @@ impl Post {
.get_result::<Post>(conn).expect("Couldn't update AP URL");
}
}
pub fn get_receivers_urls(&self, conn: &PgConnection) -> Vec<String> {
let followers = self.get_authors(conn).into_iter().map(|a| a.get_followers(conn)).collect::<Vec<Vec<User>>>();
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::<Vec<Vec<User>>>();
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!({

@ -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<NewComment
sensitive: false,
spoiler_text: "".to_string()
});
let act = Create::new(&user, &comment, &*conn);
broadcast(&*conn, &user, act, user.get_followers(&*conn));
Redirect::to(format!("/~/{}/{}/#comment-{}", blog, slug, comment.id).as_ref())
}

Loading…
Cancel
Save