Use a more classical flow for creating comments

Don't locally federate them anymore

It allows us to have them fetched later too
This commit is contained in:
Bat
2018-09-09 16:08:53 +01:00
parent 642884034d
commit 54f6e7dfc6
4 changed files with 75 additions and 97 deletions
+1
View File
@@ -55,6 +55,7 @@ fn main() {
routes::blogs::atom_feed,
routes::comments::create,
routes::comments::activity_pub,
routes::instance::index,
routes::instance::paginated_local,
+26 -12
View File
@@ -1,3 +1,4 @@
use activitypub::object::Note;
use rocket::{
State,
request::LenientForm,
@@ -8,16 +9,16 @@ use serde_json;
use validator::Validate;
use workerpool::{Pool, thunk::*};
use plume_common::activity_pub::broadcast;
use plume_common::{utils, activity_pub::{broadcast, ApRequest, ActivityStream}};
use plume_models::{
blogs::Blog,
comments::*,
db_conn::DbConn,
instance::Instance,
mentions::Mention,
posts::Post,
safe_string::SafeString,
users::User
};
use inbox::Inbox;
#[derive(FromForm, Debug, Validate)]
struct NewCommentForm {
@@ -33,16 +34,24 @@ fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, us
let form = data.get();
form.validate()
.map(|_| {
let (new_comment, _) = NewComment::build()
.content(form.content.clone())
.in_response_to_id(form.responding_to.clone())
.post(post.clone())
.author(user.clone())
.create(&*conn);
let (html, mentions) = utils::md_to_html(form.content.as_ref());
let comm = Comment::insert(&*conn, NewComment {
content: SafeString::new(html.as_ref()),
in_response_to_id: form.responding_to.clone(),
post_id: post.id,
author_id: user.id,
ap_url: None,
sensitive: false,
spoiler_text: String::new()
}).update_ap_url(&*conn);
let new_comment = comm.create_activity(&*conn);
let instance = Instance::get_local(&*conn).unwrap();
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)");
// save mentions
for ment in mentions {
Mention::from_activity(&*conn, Mention::build_activity(&*conn, ment), post.id, true, true);
}
// federate
let dest = User::one_by_instance(&*conn);
let user_clone = user.clone();
worker.execute(Thunk::of(move || broadcast(&user_clone, new_comment, dest)));
@@ -71,3 +80,8 @@ fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, us
}))
})
}
#[get("/~/<_blog>/<_slug>/comment/<id>")]
fn activity_pub(_blog: String, _slug: String, id: i32, _ap: ApRequest, conn: DbConn) -> Option<ActivityStream<Note>> {
Comment::get(&*conn, id).map(|c| ActivityStream::new(c.into_activity(&*conn)))
}