From 8f9bd1fef7fe9abee04ef54672faad588823ddab Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 28 May 2020 15:20:12 +0200 Subject: [PATCH] get some more things working --- server/src/apub/comment.rs | 66 +++++++++++++++------------- server/src/apub/community_inbox.rs | 43 +++++++------------ server/src/apub/post.rs | 69 +++++++++++++----------------- server/src/apub/shared_inbox.rs | 52 ++++++++++++---------- server/src/routes/federation.rs | 2 +- 5 files changed, 112 insertions(+), 120 deletions(-) diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 90e9f07b6..7b0cfed9e 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -6,6 +6,7 @@ use crate::{ create_tombstone, fetch_webfinger_url, fetcher::get_or_fetch_and_upsert_remote_user, + shared_inbox::do_announce, ActorType, ApubLikeableType, ApubObjectType, @@ -30,13 +31,15 @@ use activitystreams::{ context, link::Mention, object::{kind::NoteType, properties::ObjectProperties, Note, Tombstone}, + Activity, + Base, }; use actix_web::{body::Body, web::Path, HttpResponse, Result}; use diesel::PgConnection; -use failure::Error; +use failure::{Error, _core::fmt::Debug}; use itertools::Itertools; use log::debug; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; #[derive(Deserialize)] pub struct CommentQuery { @@ -128,6 +131,7 @@ impl FromApub for CommentForm { // TODO this failed because a mention on a post that wasn't on this server yet. Has to do with // fetching replytos + dbg!(&post_ap_id); let post = Post::read_from_apub_id(&conn, &post_ap_id)?; Ok(CommentForm { @@ -175,9 +179,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, creator.id, &create, true)?; - - send_activity(&create, creator, maa.inboxes)?; + Comment::send_comment_activity(&creator, &conn, &community, create)?; Ok(()) } @@ -202,9 +204,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, creator.id, &update, true)?; - - send_activity(&update, creator, maa.inboxes)?; + Comment::send_comment_activity(&creator, &conn, &community, update)?; Ok(()) } @@ -226,9 +226,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, creator.id, &delete, true)?; - - send_activity(&delete, creator, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&creator, &conn, &community, delete)?; Ok(()) } @@ -268,9 +266,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - insert_activity(&conn, creator.id, &undo, true)?; - - send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&creator, &conn, &community, undo)?; Ok(()) } @@ -292,9 +288,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, mod_.id, &remove, true)?; - - send_activity(&remove, mod_, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&mod_, &conn, &community, remove)?; Ok(()) } @@ -333,9 +327,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(remove)?; - insert_activity(&conn, mod_.id, &undo, true)?; - - send_activity(&undo, mod_, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&mod_, &conn, &community, undo)?; Ok(()) } } @@ -358,9 +350,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, creator.id, &like, true)?; - - send_activity(&like, creator, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&creator, &conn, &community, like)?; Ok(()) } @@ -381,9 +371,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - insert_activity(&conn, creator.id, &dislike, true)?; - - send_activity(&dislike, creator, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&creator, &conn, &community, dislike)?; Ok(()) } @@ -420,9 +408,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(like)?; - insert_activity(&conn, creator.id, &undo, true)?; - - send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; + Comment::send_comment_activity(&creator, &conn, &community, undo)?; Ok(()) } } @@ -480,3 +466,25 @@ fn collect_non_local_mentions_and_addresses( tags, }) } + +impl Comment { + fn send_comment_activity( + 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/community_inbox.rs b/server/src/apub/community_inbox.rs index db66d3821..9b6f5ed0e 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -1,5 +1,6 @@ use crate::{ apub::{ + activities::{populate_object_props, send_activity}, extensions::signatures::verify, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, ActorType, @@ -12,16 +13,16 @@ use crate::{ }, routes::{ChatServerParam, DbPoolParam}, }; -use activitystreams::activity::{Follow, Undo, Update, Create, Delete, Remove}; +use activitystreams::{ + activity::{Activity, Announce, Create, Delete, Follow, Remove, Undo, Update}, + Base, + BaseBox, +}; use actix_web::{web, HttpRequest, HttpResponse, Result}; use diesel::PgConnection; use failure::{Error, _core::fmt::Debug}; use log::debug; use serde::{Deserialize, Serialize}; -use activitystreams::activity::{Activity, Announce}; -use activitystreams::Base; -use crate::apub::activities::{populate_object_props, send_activity}; -use activitystreams::BaseBox; #[serde(untagged)] #[derive(Deserialize, Debug)] @@ -46,7 +47,7 @@ impl CommunityAcceptedObjects { .to_owned() .into_concrete::()?, ), - _ => todo!() + _ => todo!(), } } } @@ -92,27 +93,17 @@ pub async fn community_inbox( verify(&request, &user)?; match input { - CommunityAcceptedObjects::Follow(f) => { - handle_follow(&f, &user, &community, &conn) - } + CommunityAcceptedObjects::Follow(f) => handle_follow(&f, &user, &community, &conn), CommunityAcceptedObjects::Undo(u) => { // TODO: if this is an undo or undo, we need to announce it instead - handle_undo_follow(&u, &user, &community, &conn) + handle_undo_follow(&u, &user, &community, &conn) } // TODO: we should be able to handle all this with a single wildcard match, but i dont see how // to get the value from that - CommunityAcceptedObjects::Create(c) => { - do_announce(c, &request, &community, &conn, chat_server) - } - CommunityAcceptedObjects::Update(u) => { - do_announce(u, &request, &community, &conn, chat_server) - } - CommunityAcceptedObjects::Delete(d) => { - do_announce(d, &request, &community, &conn, chat_server) - } - CommunityAcceptedObjects::Remove(r) => { - do_announce(r, &request, &community, &conn, chat_server) - } + CommunityAcceptedObjects::Create(c) => do_announce(c, &request, &community, &conn, chat_server), + CommunityAcceptedObjects::Update(u) => do_announce(u, &request, &community, &conn, chat_server), + CommunityAcceptedObjects::Delete(d) => do_announce(d, &request, &community, &conn, chat_server), + CommunityAcceptedObjects::Remove(r) => do_announce(r, &request, &community, &conn, chat_server), } } @@ -185,7 +176,7 @@ where let mut announce = Announce::default(); populate_object_props( &mut announce.object_props, - vec!(community.get_followers_url()), + vec![community.get_followers_url()], &format!("{}/announce/{}", community.actor_id, uuid::Uuid::new_v4()), )?; announce @@ -195,11 +186,7 @@ where insert_activity(&conn, -1, &announce, true)?; - send_activity( - &announce, - community, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&announce, community, community.get_follower_inboxes(&conn)?)?; Ok(HttpResponse::Ok().finish()) } diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 084e0fb25..3aac524bf 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -7,6 +7,7 @@ use crate::{ extensions::page_extension::PageExtension, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, get_apub_protocol_string, + shared_inbox::do_announce, ActorType, ApubLikeableType, ApubObjectType, @@ -25,14 +26,19 @@ 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, Activity, Base}; +use activitystreams::{ + activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, + context, + object::{kind::PageType, properties::ObjectProperties, AnyImage, Image, Page, Tombstone}, + Activity, + Base, + BaseBox, +}; use activitystreams_ext::Ext1; use actix_web::{body::Body, web::Path, HttpResponse, Result}; use diesel::PgConnection; -use failure::Error; +use failure::{Error, _core::fmt::Debug}; use serde::{Deserialize, Serialize}; -use crate::apub::shared_inbox::do_announce; -use failure::_core::fmt::Debug; #[derive(Deserialize)] pub struct PostQuery { @@ -236,9 +242,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, creator.id, &create, true)?; - - Post::send_post(creator, conn, &community, create)?; + Post::send_post_activity(creator, conn, &community, create)?; Ok(()) } @@ -259,9 +263,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, creator.id, &update, true)?; - - Post::send_post(creator, conn, &community, update)?; + Post::send_post_activity(creator, conn, &community, update)?; Ok(()) } @@ -282,11 +284,9 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, self.creator_id, &delete, true)?; - let community = Community::read(conn, self.community_id)?; - Post::send_post(creator, conn, &community, delete)?; + Post::send_post_activity(creator, conn, &community, delete)?; Ok(()) } @@ -323,10 +323,8 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - insert_activity(&conn, self.creator_id, &undo, true)?; - let community = Community::read(conn, self.community_id)?; - Post::send_post(creator, conn, &community, undo)?; + Post::send_post_activity(creator, conn, &community, undo)?; Ok(()) } @@ -347,11 +345,9 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, mod_.id, &remove, true)?; - let community = Community::read(conn, self.community_id)?; - Post::send_post(mod_, conn, &community, remove)?; + Post::send_post_activity(mod_, conn, &community, remove)?; Ok(()) } fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> { @@ -386,15 +382,12 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(remove)?; - insert_activity(&conn, mod_.id, &undo, true)?; - let community = Community::read(conn, self.community_id)?; - Post::send_post(mod_, conn, &community, undo)?; + Post::send_post_activity(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)?; @@ -412,9 +405,7 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, creator.id, &like, true)?; - - send_activity(&like, creator, vec!(community.get_inbox_url()))?; + Post::send_post_activity(&creator, &conn, &community, like)?; Ok(()) } @@ -434,9 +425,7 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(BaseBox::from_concrete(page)?)?; - insert_activity(&conn, creator.id, &dislike, true)?; - - send_activity(&dislike, creator, vec!(community.get_inbox_url()))?; + Post::send_post_activity(&creator, &conn, &community, dislike)?; Ok(()) } @@ -472,28 +461,28 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(like)?; - insert_activity(&conn, creator.id, &undo, true)?; - - send_activity(&undo, creator, vec!(community.get_inbox_url()))?; + Post::send_post_activity(&creator, &conn, &community, undo)?; Ok(()) } } impl Post { - fn send_post(creator: &User_, conn: &PgConnection, community: &Community, activity: A) -> Result<(), Error> - where - A: Activity + Base + Serialize + Debug { + fn send_post_activity( + 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()], - )?; + 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 9e8e893f9..6322e7fc6 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -5,8 +5,10 @@ use crate::{ post::PostResponse, }, apub::{ + activities::{populate_object_props, send_activity}, extensions::signatures::verify, - fetcher::get_or_fetch_and_upsert_remote_user, + fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, + ActorType, FromApub, GroupExt, PageExt, @@ -30,16 +32,18 @@ use crate::{ UserOperation, }, }; -use activitystreams::{activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, object::Note, BaseBox, Base, Activity}; +use activitystreams::{ + activity::{Announce, Create, Delete, Dislike, Like, Remove, Undo, Update}, + object::Note, + Activity, + Base, + BaseBox, +}; use actix_web::{web, HttpRequest, HttpResponse, Result}; use diesel::PgConnection; 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 crate::apub::activities::{populate_object_props, send_activity}; -use crate::apub::ActorType; -use activitystreams::activity::Announce; #[serde(untagged)] #[derive(Serialize, Deserialize, Debug)] @@ -92,7 +96,12 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Remove(r) => &r.object_props, SharedAcceptedObjects::Announce(a) => &a.object_props, }; - oprops.get_cc_xsd_any_uri().unwrap().to_owned().to_string() + oprops + .get_many_cc_xsd_any_uris() + .unwrap() + .next() + .unwrap() + .to_string() } } @@ -112,40 +121,42 @@ pub async fn shared_inbox( let object = activity.object().cloned().unwrap(); let sender = &activity.sender(); let cc = &activity.cc(); + // TODO: this is hacky, we should probably send the community id directly somehow + let to = cc.replace("/followers", ""); match get_or_fetch_and_upsert_remote_user(&sender.to_string(), &conn) { Ok(u) => verify(&request, &u), Err(_) => { let c = get_or_fetch_and_upsert_remote_community(&sender.to_string(), &conn)?; verify(&request, &c) - }, + } }?; match (activity, object.kind()) { (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, &to, sender, conn) } (SharedAcceptedObjects::Update(u), Some("Page")) => { receive_update_post(&u, &conn, chat_server)?; - do_announce(*u, &cc, &sender, conn) + do_announce(*u, &to, &sender, conn) } (SharedAcceptedObjects::Like(l), Some("Page")) => { receive_like_post(&l, &conn, chat_server)?; - do_announce(*l, &cc, &sender, conn) + do_announce(*l, &to, &sender, conn) } (SharedAcceptedObjects::Dislike(d), Some("Page")) => { receive_dislike_post(&d, &conn, chat_server)?; - do_announce(*d, &cc, &sender, conn) + do_announce(*d, &to, &sender, conn) } (SharedAcceptedObjects::Delete(d), Some("Page")) => { receive_delete_post(&d, &conn, chat_server)?; - do_announce(*d, &cc, &sender, conn) + do_announce(*d, &to, &sender, conn) } (SharedAcceptedObjects::Remove(r), Some("Page")) => { receive_remove_post(&r, &conn, chat_server)?; - do_announce(*r, &cc, &sender, conn) + do_announce(*r, &to, &sender, conn) } (SharedAcceptedObjects::Create(c), Some("Note")) => { receive_create_comment(&c, &conn, chat_server) @@ -1505,18 +1516,19 @@ pub fn do_announce( conn: &PgConnection, ) -> Result where - A: Activity + Base + Serialize, + A: Activity + Base + Serialize + Debug, { 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: need to add boolean param is_local_activity + //insert_activity(&conn, -1, &activity, false)?; let mut announce = Announce::default(); populate_object_props( &mut announce.object_props, - vec!(community.get_followers_url()), + vec![community.get_followers_url()], &format!("{}/announce/{}", community.actor_id, uuid::Uuid::new_v4()), )?; announce @@ -1536,11 +1548,7 @@ where dbg!(&announce); dbg!(&to); - send_activity( - &announce, - &community, - to, - )?; + send_activity(&announce, &community, to)?; Ok(HttpResponse::Ok().finish()) } diff --git a/server/src/routes/federation.rs b/server/src/routes/federation.rs index cb0a05b0e..fe6e33657 100644 --- a/server/src/routes/federation.rs +++ b/server/src/routes/federation.rs @@ -2,6 +2,7 @@ use crate::{ apub::{ comment::get_apub_comment, community::*, + community_inbox::community_inbox, post::get_apub_post, shared_inbox::shared_inbox, user::*, @@ -11,7 +12,6 @@ use crate::{ settings::Settings, }; use actix_web::*; -use crate::apub::community_inbox::community_inbox; pub fn config(cfg: &mut web::ServiceConfig) { if Settings::get().federation.enabled {