From 2f1f6a02955f54d4d0881e5e1bdfa90111c97670 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 1 May 2018 16:51:49 +0100 Subject: [PATCH] Broadcast activities to followers --- src/activity_pub/outbox.rs | 9 +++++++++ src/models/instance.rs | 6 ++++++ src/routes/posts.rs | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/src/activity_pub/outbox.rs b/src/activity_pub/outbox.rs index d8134ef..8255868 100644 --- a/src/activity_pub/outbox.rs +++ b/src/activity_pub/outbox.rs @@ -1,3 +1,4 @@ +use diesel::PgConnection; use rocket::http::Status; use rocket::response::{Response, Responder}; use rocket::request::Request; @@ -5,6 +6,8 @@ use serde_json; use activity_pub::{activity_pub, ActivityPub, context}; use activity_pub::activity::Activity; +use activity_pub::actor::Actor; +use models::users::User; pub struct Outbox { id: String, @@ -36,3 +39,9 @@ impl<'r> Responder<'r> for Outbox { self.serialize().respond_to(request) } } + +pub fn broadcast(conn: &PgConnection, act: Activity, to: Vec) { + for user in to { + user.send_to_inbox(conn, act.clone()); // TODO: run it in Sidekiq or something like that + } +} diff --git a/src/models/instance.rs b/src/models/instance.rs index 80b19cd..6eefb78 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -34,6 +34,12 @@ impl Instance { .into_iter().nth(0) } + pub fn get_remotes(conn: &PgConnection) -> Vec { + instances::table.filter(instances::local.eq(false)) + .load::(conn) + .expect("Error loading remote instances infos") + } + pub fn local_id(conn: &PgConnection) -> i32 { Instance::get_local(conn).unwrap().id } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index c503ea3..6c571c8 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -4,6 +4,8 @@ use rocket::response::Redirect; use rocket_contrib::Template; use std::collections::HashMap; +use activity_pub::activity::Activity; +use activity_pub::outbox::broadcast; use db_conn::DbConn; use models::blogs::*; use models::post_authors::*; @@ -52,5 +54,9 @@ fn create(blog_name: String, data: Form, user: User, conn: DbConn) post_id: post.id, author_id: user.id }); + + let act = Activity::create(&user, post, &*conn); + broadcast(&*conn, act, user.get_followers(&*conn)); + Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str()) }