From ce0a37cdf1e6cceb029b033caf1369863e2075ae Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 7 May 2020 19:07:33 +0200 Subject: [PATCH] get it working (mostly) --- docker/federation/run-federation-test.bash | 6 +++ server/src/apub/post.rs | 46 ++++++++++++++++------ server/src/apub/shared_inbox.rs | 37 ++++++++--------- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/docker/federation/run-federation-test.bash b/docker/federation/run-federation-test.bash index 8486648b3..206a32402 100755 --- a/docker/federation/run-federation-test.bash +++ b/docker/federation/run-federation-test.bash @@ -1,6 +1,11 @@ #!/bin/bash set -e +# already start rust build in the background +pushd ../../server/ || exit +cargo build & +popd || exit + if [ "$1" = "-yarn" ]; then pushd ../../ui/ || exit yarn @@ -8,6 +13,7 @@ if [ "$1" = "-yarn" ]; then popd || exit fi +# wait for rust build to finish pushd ../../server/ || exit cargo build popd || exit diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 5401b67f6..084e0fb25 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -25,17 +25,14 @@ use crate::{ routes::DbPoolParam, Settings, }; -use activitystreams::{ - activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, - context, - object::{kind::PageType, properties::ObjectProperties, AnyImage, Image, Page, Tombstone}, - BaseBox, -}; +use activitystreams::{activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, context, object::{kind::PageType, properties::ObjectProperties, AnyImage, Image, Page, Tombstone}, BaseBox, Activity, Base}; use activitystreams_ext::Ext1; use actix_web::{body::Body, web::Path, HttpResponse, Result}; use diesel::PgConnection; use failure::Error; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; +use crate::apub::shared_inbox::do_announce; +use failure::_core::fmt::Debug; #[derive(Deserialize)] pub struct PostQuery { @@ -241,7 +238,7 @@ impl ApubObjectType for Post { insert_activity(&conn, creator.id, &create, true)?; - send_activity(&create, creator, vec!(community.get_shared_inbox_url()))?; + Post::send_post(creator, conn, &community, create)?; Ok(()) } @@ -264,7 +261,7 @@ impl ApubObjectType for Post { insert_activity(&conn, creator.id, &update, true)?; - send_activity(&update, creator, vec!(community.get_shared_inbox_url()))?; + Post::send_post(creator, conn, &community, update)?; Ok(()) } @@ -288,7 +285,8 @@ impl ApubObjectType for Post { insert_activity(&conn, self.creator_id, &delete, true)?; let community = Community::read(conn, self.community_id)?; - send_activity(&delete, creator, vec!(community.get_shared_inbox_url()))?; + + Post::send_post(creator, conn, &community, delete)?; Ok(()) } @@ -328,7 +326,7 @@ impl ApubObjectType for Post { insert_activity(&conn, self.creator_id, &undo, true)?; let community = Community::read(conn, self.community_id)?; - send_activity(&undo, creator, vec!(community.get_shared_inbox_url()))?; + Post::send_post(creator, conn, &community, undo)?; Ok(()) } @@ -352,7 +350,8 @@ impl ApubObjectType for Post { insert_activity(&conn, mod_.id, &remove, true)?; let community = Community::read(conn, self.community_id)?; - send_activity(&remove, mod_, vec!(community.get_shared_inbox_url()))?; + + Post::send_post(mod_, conn, &community, remove)?; Ok(()) } fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> { @@ -390,11 +389,12 @@ impl ApubObjectType for Post { insert_activity(&conn, mod_.id, &undo, true)?; let community = Community::read(conn, self.community_id)?; - send_activity(&undo, mod_, vec!(community.get_shared_inbox_url()))?; + Post::send_post(mod_, conn, &community, undo)?; Ok(()) } } + impl ApubLikeableType for Post { fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> { let page = self.to_apub(conn)?; @@ -478,3 +478,23 @@ impl ApubLikeableType for Post { Ok(()) } } + +impl Post { + fn send_post(creator: &User_, conn: &PgConnection, community: &Community, activity: A) -> Result<(), Error> + where + A: Activity + Base + Serialize + Debug { + insert_activity(&conn, creator.id, &activity, true)?; + + // if this is a local community, we need to do an announce from the community instead + if community.local { + do_announce(activity, &community.actor_id, &creator.actor_id, conn)?; + } else { + send_activity( + &activity, + creator, + vec![community.get_shared_inbox_url()], + )?; + } + Ok(()) + } +} diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index a19b698dc..9e8e893f9 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -37,7 +37,6 @@ use failure::{Error, _core::fmt::Debug}; use log::debug; use serde::{Deserialize, Serialize}; use crate::apub::fetcher::get_or_fetch_and_upsert_remote_community; -use activitystreams_new::primitives::XsdAnyUri; use crate::apub::activities::{populate_object_props, send_activity}; use crate::apub::ActorType; use activitystreams::activity::Announce; @@ -68,7 +67,7 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Announce(a) => a.announce_props.get_object_base_box(), } } - fn sender(&self) -> XsdAnyUri { + fn sender(&self) -> String { let uri = match self { SharedAcceptedObjects::Create(c) => c.create_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Update(u) => u.update_props.get_actor_xsd_any_uri(), @@ -79,9 +78,9 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Remove(r) => r.remove_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Announce(a) => a.announce_props.get_actor_xsd_any_uri(), }; - uri.unwrap().clone() + uri.unwrap().clone().to_string() } - fn cc(&self) -> XsdAnyUri { + fn cc(&self) -> String { // TODO: there is probably an easier way to do this let oprops = match self { SharedAcceptedObjects::Create(c) => &c.object_props, @@ -93,7 +92,7 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Remove(r) => &r.object_props, SharedAcceptedObjects::Announce(a) => &a.object_props, }; - oprops.get_cc_xsd_any_uri().unwrap().to_owned() + oprops.get_cc_xsd_any_uri().unwrap().to_owned().to_string() } } @@ -111,10 +110,9 @@ pub async fn shared_inbox( debug!("Shared inbox received activity: {}", json); let object = activity.object().cloned().unwrap(); - let sender = activity.sender(); - let cc = activity.cc(); + let sender = &activity.sender(); + let cc = &activity.cc(); - // TODO: should make an enum Actor that contains user and community, and has methods like get_public_key() match get_or_fetch_and_upsert_remote_user(&sender.to_string(), &conn) { Ok(u) => verify(&request, &u), Err(_) => { @@ -127,7 +125,7 @@ pub async fn shared_inbox( (SharedAcceptedObjects::Create(c), Some("Page")) => { // TODO: first check that it is addressed to a local community receive_create_post(&c, &conn, chat_server)?; - do_announce(*c, &cc, &sender, conn) + do_announce(*c, cc, sender, conn) } (SharedAcceptedObjects::Update(u), Some("Page")) => { receive_update_post(&u, &conn, chat_server)?; @@ -1499,22 +1497,22 @@ fn receive_undo_like_post( Ok(HttpResponse::Ok().finish()) } -fn do_announce( +// TODO: move to community.rs +pub fn do_announce( activity: A, - community_uri: &XsdAnyUri, - sender: &XsdAnyUri, + community_uri: &str, + sender: &str, conn: &PgConnection, ) -> Result where A: Activity + Base + Serialize, { - // Note: signature check is done by receive_create_post() etc - - let community = Community::read_from_actor_id(conn, &community_uri.to_string())?; + dbg!(&community_uri); + // TODO: this fails for some reason + let community = Community::read_from_actor_id(conn, &community_uri)?; insert_activity(&conn, -1, &activity, false)?; - // TODO: move the sending to community.rs let mut announce = Announce::default(); populate_object_props( &mut announce.object_props, @@ -1526,15 +1524,18 @@ where .set_actor_xsd_any_uri(community.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(activity)?)?; - insert_activity(&conn, -1, &announce, true)?; + insert_activity(&conn, community.id, &announce, true)?; // dont send to the instance where the activity originally came from, because that would result // in a database error (same data inserted twice) let mut to = community.get_follower_inboxes(&conn)?; - let sending_user = get_or_fetch_and_upsert_remote_user(&sender.to_string(), conn)?; + let sending_user = get_or_fetch_and_upsert_remote_user(&sender, conn)?; // this seems to be the "easiest" stable alternative for remove_item() to.retain(|x| *x != sending_user.get_shared_inbox_url()); + dbg!(&announce); + dbg!(&to); + send_activity( &announce, &community,