From a9f95c91e2ed00421741a5f98454c5743e1577c0 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 26 Jul 2018 17:51:41 +0200 Subject: [PATCH] Sent activities in other threads --- src/routes/comments.rs | 8 ++++++-- src/routes/likes.rs | 12 ++++++++---- src/routes/reshares.rs | 12 ++++++++---- src/routes/user.rs | 9 ++++++--- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/routes/comments.rs b/src/routes/comments.rs index f25489f..a10c3cd 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -1,10 +1,12 @@ use rocket::{ + State, request::LenientForm, response::Redirect }; use rocket_contrib::Template; use serde_json; use validator::Validate; +use workerpool::{Pool, thunk::*}; use plume_common::activity_pub::broadcast; use plume_models::{ @@ -25,7 +27,7 @@ struct NewCommentForm { } #[post("/~///comment", data = "")] -fn create(blog_name: String, slug: String, data: LenientForm, user: User, conn: DbConn) -> Result { +fn create(blog_name: String, slug: String, data: LenientForm, user: User, conn: DbConn, worker: State>>) -> Result { let blog = Blog::find_by_fqn(&*conn, blog_name.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone(), blog.id).unwrap(); let form = data.get(); @@ -41,7 +43,9 @@ fn create(blog_name: String, slug: String, data: LenientForm, us 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)"); - broadcast(&user, new_comment, user.get_followers(&*conn)); + let followers = user.get_followers(&*conn); + let user_clone = user.clone(); + worker.execute(Thunk::of(move || broadcast(&user_clone, new_comment, followers))); Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id)) }) diff --git a/src/routes/likes.rs b/src/routes/likes.rs index 8126871..a3a2dd3 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -1,4 +1,5 @@ -use rocket::response::{Redirect, Flash}; +use rocket::{State, response::{Redirect, Flash}}; +use workerpool::{Pool, thunk::*}; use plume_common::activity_pub::{broadcast, inbox::Notify}; use plume_common::utils; @@ -11,7 +12,7 @@ use plume_models::{ }; #[post("/~///like")] -fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { +fn create(blog: String, slug: String, user: User, conn: DbConn, worker: State>>) -> Redirect { let b = Blog::find_by_fqn(&*conn, blog.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone(), b.id).unwrap(); @@ -24,11 +25,14 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { like.update_ap_url(&*conn); like.notify(&*conn); - broadcast(&user, like.into_activity(&*conn), user.get_followers(&*conn)); + let followers = user.get_followers(&*conn); + let act = like.into_activity(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, act, followers))); } else { let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); let delete_act = like.delete(&*conn); - broadcast(&user, delete_act, user.get_followers(&*conn)); + let followers = user.get_followers(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, delete_act, followers))); } Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index 67cf87a..1ba673a 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -1,4 +1,5 @@ -use rocket::response::{Redirect, Flash}; +use rocket::{State, response::{Redirect, Flash}}; +use workerpool::{Pool, thunk::*}; use plume_common::activity_pub::{broadcast, inbox::Notify}; use plume_common::utils; @@ -11,7 +12,7 @@ use plume_models::{ }; #[post("/~///reshare")] -fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { +fn create(blog: String, slug: String, user: User, conn: DbConn, worker: State>>) -> Redirect { let b = Blog::find_by_fqn(&*conn, blog.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone(), b.id).unwrap(); @@ -24,11 +25,14 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { reshare.update_ap_url(&*conn); reshare.notify(&*conn); - broadcast(&user, reshare.into_activity(&*conn), user.get_followers(&*conn)); + let followers = user.get_followers(&*conn); + let act = reshare.into_activity(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, act, followers))); } else { let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap(); let delete_act = reshare.delete(&*conn); - broadcast(&user, delete_act, user.get_followers(&*conn)); + let followers = user.get_followers(&*conn); + worker.execute(Thunk::of(move || broadcast(&user, delete_act, followers))); } Redirect::to(uri!(super::posts::details: blog = blog, slug = slug)) diff --git a/src/routes/user.rs b/src/routes/user.rs index 67fb021..cab8e6a 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -2,12 +2,15 @@ use activitypub::{ activity::Follow, collection::OrderedCollection }; -use rocket::{request::LenientForm, +use rocket::{ + State, + request::LenientForm, response::{Redirect, Flash} }; use rocket_contrib::Template; use serde_json; use validator::{Validate, ValidationError}; +use workerpool::{Pool, thunk::*}; use plume_common::activity_pub::{ ActivityStream, broadcast, Id, IntoId, ApRequest, @@ -71,7 +74,7 @@ fn dashboard_auth() -> Flash { } #[get("/@//follow")] -fn follow(name: String, conn: DbConn, user: User) -> Redirect { +fn follow(name: String, conn: DbConn, user: User, worker: State>>) -> Redirect { let target = User::find_by_fqn(&*conn, name.clone()).unwrap(); let f = follows::Follow::insert(&*conn, follows::NewFollow { follower_id: user.id, @@ -86,7 +89,7 @@ fn follow(name: String, conn: DbConn, user: User) -> Redirect { act.object_props.set_to_link(target.clone().into_id()).expect("New Follow error while setting 'to'"); act.object_props.set_cc_link_vec::(vec![]).expect("New Follow error while setting 'cc'"); - broadcast(&user, act, vec![target]); + worker.execute(Thunk::of(move || broadcast(&user, act, vec![target]))); Redirect::to(uri!(details: name = name)) }