From 7cfcf0acec38c2746557e2ae027b47e2bbe347fb Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 12 Oct 2020 18:02:28 +0200 Subject: [PATCH] Change the way that `to` is set in apub --- lemmy_apub/src/activities/receive/announce.rs | 29 +++++------ lemmy_apub/src/activities/receive/create.rs | 13 +++-- lemmy_apub/src/activities/receive/delete.rs | 13 +++-- lemmy_apub/src/activities/receive/dislike.rs | 11 +++-- lemmy_apub/src/activities/receive/like.rs | 11 +++-- lemmy_apub/src/activities/receive/mod.rs | 20 +++++++- lemmy_apub/src/activities/receive/remove.rs | 28 +++++++---- lemmy_apub/src/activities/receive/undo.rs | 6 +-- .../src/activities/receive/undo_comment.rs | 11 ++--- .../src/activities/receive/undo_post.rs | 11 ++--- lemmy_apub/src/activities/receive/update.rs | 13 +++-- lemmy_apub/src/activities/send/comment.rs | 34 +++++++------ lemmy_apub/src/activities/send/post.rs | 24 +++++----- .../src/activities/send/private_message.rs | 23 ++++----- lemmy_apub/src/activity_queue.rs | 30 +++++++----- lemmy_apub/src/inbox/shared_inbox.rs | 48 +++---------------- 16 files changed, 169 insertions(+), 156 deletions(-) diff --git a/lemmy_apub/src/activities/receive/announce.rs b/lemmy_apub/src/activities/receive/announce.rs index a553c190d..50c2ed9d6 100644 --- a/lemmy_apub/src/activities/receive/announce.rs +++ b/lemmy_apub/src/activities/receive/announce.rs @@ -1,15 +1,12 @@ -use crate::{ - activities::receive::{ - create::receive_create, - delete::receive_delete, - dislike::receive_dislike, - like::receive_like, - receive_unhandled_activity, - remove::receive_remove, - undo::receive_undo, - update::receive_update, - }, - inbox::shared_inbox::get_community_id_from_activity, +use crate::activities::receive::{ + create::receive_create, + delete::receive_delete, + dislike::receive_dislike, + like::receive_like, + receive_unhandled_activity, + remove::receive_remove, + undo::receive_undo, + update::receive_update, }; use activitystreams::{ activity::*, @@ -28,8 +25,12 @@ pub async fn receive_announce( let announce = Announce::from_any_base(activity)?.context(location_info!())?; // ensure that announce and community come from the same instance - let community = get_community_id_from_activity(&announce)?; - announce.id(community.domain().context(location_info!())?)?; + let community_id = announce + .actor()? + .to_owned() + .single_xsd_any_uri() + .context(location_info!())?; + announce.id(community_id.domain().context(location_info!())?)?; let kind = announce.object().as_single_kind_str(); let object = announce.object(); diff --git a/lemmy_apub/src/activities/receive/create.rs b/lemmy_apub/src/activities/receive/create.rs index 4911088d0..201d620ba 100644 --- a/lemmy_apub/src/activities/receive/create.rs +++ b/lemmy_apub/src/activities/receive/create.rs @@ -1,6 +1,9 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, - inbox::shared_inbox::get_user_from_activity, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, ActorType, FromApub, PageExt, @@ -29,7 +32,7 @@ pub async fn receive_create( let create = Create::from_any_base(activity)?.context(location_info!())?; // ensure that create and actor come from the same instance - let user = get_user_from_activity(&create, context).await?; + let user = get_actor_as_user(&create, context).await?; create.id(user.actor_id()?.domain().context(location_info!())?)?; match create.object().as_single_kind_str() { @@ -43,7 +46,7 @@ async fn receive_create_post( create: Create, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&create, context).await?; + let user = get_actor_as_user(&create, context).await?; let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -76,7 +79,7 @@ async fn receive_create_comment( create: Create, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&create, context).await?; + let user = get_actor_as_user(&create, context).await?; let note = Note::from_any_base(create.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; diff --git a/lemmy_apub/src/activities/receive/delete.rs b/lemmy_apub/src/activities/receive/delete.rs index 1490ce918..a1985881e 100644 --- a/lemmy_apub/src/activities/receive/delete.rs +++ b/lemmy_apub/src/activities/receive/delete.rs @@ -1,7 +1,10 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::get_user_from_activity, ActorType, FromApub, GroupExt, @@ -50,7 +53,7 @@ async fn receive_delete_post( delete: Delete, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&delete, context).await?; + let user = get_actor_as_user(&delete, context).await?; let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -109,7 +112,7 @@ async fn receive_delete_comment( delete: Delete, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&delete, context).await?; + let user = get_actor_as_user(&delete, context).await?; let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -169,7 +172,7 @@ async fn receive_delete_community( ) -> Result { let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; - let user = get_user_from_activity(&delete, context).await?; + let user = get_actor_as_user(&delete, context).await?; let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?)) .await? diff --git a/lemmy_apub/src/activities/receive/dislike.rs b/lemmy_apub/src/activities/receive/dislike.rs index e79414804..1007e6154 100644 --- a/lemmy_apub/src/activities/receive/dislike.rs +++ b/lemmy_apub/src/activities/receive/dislike.rs @@ -1,7 +1,10 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::get_user_from_activity, FromApub, PageExt, }; @@ -49,7 +52,7 @@ async fn receive_dislike_post( dislike: Dislike, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&dislike, context).await?; + let user = get_actor_as_user(&dislike, context).await?; let page = PageExt::from_any_base( dislike .object() @@ -107,7 +110,7 @@ async fn receive_dislike_comment( .context(location_info!())?, )? .context(location_info!())?; - let user = get_user_from_activity(&dislike, context).await?; + let user = get_actor_as_user(&dislike, context).await?; let comment = CommentForm::from_apub(¬e, context, None).await?; diff --git a/lemmy_apub/src/activities/receive/like.rs b/lemmy_apub/src/activities/receive/like.rs index 3000e512d..ef53e7918 100644 --- a/lemmy_apub/src/activities/receive/like.rs +++ b/lemmy_apub/src/activities/receive/like.rs @@ -1,7 +1,10 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::get_user_from_activity, FromApub, PageExt, }; @@ -36,7 +39,7 @@ pub async fn receive_like( } async fn receive_like_post(like: Like, context: &LemmyContext) -> Result { - let user = get_user_from_activity(&like, context).await?; + let user = get_actor_as_user(&like, context).await?; let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -82,7 +85,7 @@ async fn receive_like_comment( ) -> Result { let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; - let user = get_user_from_activity(&like, context).await?; + let user = get_actor_as_user(&like, context).await?; let comment = CommentForm::from_apub(¬e, context, None).await?; diff --git a/lemmy_apub/src/activities/receive/mod.rs b/lemmy_apub/src/activities/receive/mod.rs index b765b03e9..06c5f291a 100644 --- a/lemmy_apub/src/activities/receive/mod.rs +++ b/lemmy_apub/src/activities/receive/mod.rs @@ -1,6 +1,10 @@ -use crate::{fetcher::get_or_fetch_and_upsert_community, ActorType}; +use crate::{ + fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user}, + ActorType, +}; use activitystreams::{ - base::{Extends, ExtendsExt}, + activity::{ActorAndObjectRef, ActorAndObjectRefExt}, + base::{AsBase, Extends, ExtendsExt}, object::{AsObject, ObjectExt}, }; use actix_web::HttpResponse; @@ -63,3 +67,15 @@ where } Ok(()) } + +pub(in crate) async fn get_actor_as_user( + activity: &T, + context: &LemmyContext, +) -> Result +where + T: AsBase + ActorAndObjectRef, +{ + let actor = activity.actor()?; + let user_uri = actor.as_single_xsd_any_uri().context(location_info!())?; + get_or_fetch_and_upsert_user(&user_uri, context).await +} diff --git a/lemmy_apub/src/activities/receive/remove.rs b/lemmy_apub/src/activities/receive/remove.rs index 09cfd0813..dfd166e6c 100644 --- a/lemmy_apub/src/activities/receive/remove.rs +++ b/lemmy_apub/src/activities/receive/remove.rs @@ -1,7 +1,10 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::{get_community_id_from_activity, get_user_from_activity}, ActorType, FromApub, GroupExt, @@ -38,9 +41,18 @@ pub async fn receive_remove( context: &LemmyContext, ) -> Result { let remove = Remove::from_any_base(activity)?.context(location_info!())?; - let actor = get_user_from_activity(&remove, context).await?; - let community = get_community_id_from_activity(&remove)?; - if actor.actor_id()?.domain() != community.domain() { + let actor = get_actor_as_user(&remove, context).await?; + let cc = remove + .cc() + .map(|c| c.as_many()) + .flatten() + .context(location_info!())?; + let community_id = cc + .first() + .map(|c| c.as_xsd_any_uri()) + .flatten() + .context(location_info!())?; + if actor.actor_id()?.domain() != community_id.domain() { return Err(anyhow!("Remove receive are only allowed on local objects").into()); } @@ -56,7 +68,7 @@ async fn receive_remove_post( remove: Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(&remove, context).await?; + let mod_ = get_actor_as_user(&remove, context).await?; let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -115,7 +127,7 @@ async fn receive_remove_comment( remove: Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(&remove, context).await?; + let mod_ = get_actor_as_user(&remove, context).await?; let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -173,7 +185,7 @@ async fn receive_remove_community( remove: Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(&remove, context).await?; + let mod_ = get_actor_as_user(&remove, context).await?; let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; diff --git a/lemmy_apub/src/activities/receive/undo.rs b/lemmy_apub/src/activities/receive/undo.rs index 0880c3f68..95a739505 100644 --- a/lemmy_apub/src/activities/receive/undo.rs +++ b/lemmy_apub/src/activities/receive/undo.rs @@ -1,11 +1,11 @@ use crate::{ activities::receive::{ announce_if_community_is_local, + get_actor_as_user, receive_unhandled_activity, undo_comment::*, undo_post::*, }, - inbox::shared_inbox::get_user_from_activity, ActorType, FromApub, GroupExt, @@ -141,7 +141,7 @@ async fn receive_undo_delete_community( delete: &Delete, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(delete, context).await?; + let user = get_actor_as_user(delete, context).await?; let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -207,7 +207,7 @@ async fn receive_undo_remove_community( remove: &Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(remove, context).await?; + let mod_ = get_actor_as_user(remove, context).await?; let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; diff --git a/lemmy_apub/src/activities/receive/undo_comment.rs b/lemmy_apub/src/activities/receive/undo_comment.rs index 8ff805e93..d7566827d 100644 --- a/lemmy_apub/src/activities/receive/undo_comment.rs +++ b/lemmy_apub/src/activities/receive/undo_comment.rs @@ -1,7 +1,6 @@ use crate::{ - activities::receive::announce_if_community_is_local, + activities::receive::{announce_if_community_is_local, get_actor_as_user}, fetcher::get_or_fetch_and_insert_comment, - inbox::shared_inbox::get_user_from_activity, ActorType, FromApub, }; @@ -24,7 +23,7 @@ pub(crate) async fn receive_undo_like_comment( like: &Like, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(like, context).await?; + let user = get_actor_as_user(like, context).await?; let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -69,7 +68,7 @@ pub(crate) async fn receive_undo_dislike_comment( dislike: &Dislike, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(dislike, context).await?; + let user = get_actor_as_user(dislike, context).await?; let note = Note::from_any_base( dislike .object() @@ -120,7 +119,7 @@ pub(crate) async fn receive_undo_delete_comment( delete: &Delete, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(delete, context).await?; + let user = get_actor_as_user(delete, context).await?; let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -179,7 +178,7 @@ pub(crate) async fn receive_undo_remove_comment( remove: &Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(remove, context).await?; + let mod_ = get_actor_as_user(remove, context).await?; let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; diff --git a/lemmy_apub/src/activities/receive/undo_post.rs b/lemmy_apub/src/activities/receive/undo_post.rs index 00527a102..3eebb08ec 100644 --- a/lemmy_apub/src/activities/receive/undo_post.rs +++ b/lemmy_apub/src/activities/receive/undo_post.rs @@ -1,7 +1,6 @@ use crate::{ - activities::receive::announce_if_community_is_local, + activities::receive::{announce_if_community_is_local, get_actor_as_user}, fetcher::get_or_fetch_and_insert_post, - inbox::shared_inbox::get_user_from_activity, ActorType, FromApub, PageExt, @@ -25,7 +24,7 @@ pub(crate) async fn receive_undo_like_post( like: &Like, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(like, context).await?; + let user = get_actor_as_user(like, context).await?; let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -64,7 +63,7 @@ pub(crate) async fn receive_undo_dislike_post( dislike: &Dislike, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(dislike, context).await?; + let user = get_actor_as_user(dislike, context).await?; let page = PageExt::from_any_base( dislike .object() @@ -109,7 +108,7 @@ pub(crate) async fn receive_undo_delete_post( delete: &Delete, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(delete, context).await?; + let user = get_actor_as_user(delete, context).await?; let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -169,7 +168,7 @@ pub(crate) async fn receive_undo_remove_post( remove: &Remove, context: &LemmyContext, ) -> Result { - let mod_ = get_user_from_activity(remove, context).await?; + let mod_ = get_actor_as_user(remove, context).await?; let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; diff --git a/lemmy_apub/src/activities/receive/update.rs b/lemmy_apub/src/activities/receive/update.rs index a18c6fe92..0dd21ef5a 100644 --- a/lemmy_apub/src/activities/receive/update.rs +++ b/lemmy_apub/src/activities/receive/update.rs @@ -1,7 +1,10 @@ use crate::{ - activities::receive::{announce_if_community_is_local, receive_unhandled_activity}, + activities::receive::{ + announce_if_community_is_local, + get_actor_as_user, + receive_unhandled_activity, + }, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, - inbox::shared_inbox::get_user_from_activity, ActorType, FromApub, PageExt, @@ -31,7 +34,7 @@ pub async fn receive_update( let update = Update::from_any_base(activity)?.context(location_info!())?; // ensure that update and actor come from the same instance - let user = get_user_from_activity(&update, context).await?; + let user = get_actor_as_user(&update, context).await?; update.id(user.actor_id()?.domain().context(location_info!())?)?; match update.object().as_single_kind_str() { @@ -45,7 +48,7 @@ async fn receive_update_post( update: Update, context: &LemmyContext, ) -> Result { - let user = get_user_from_activity(&update, context).await?; + let user = get_actor_as_user(&update, context).await?; let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; @@ -84,7 +87,7 @@ async fn receive_update_comment( ) -> Result { let note = Note::from_any_base(update.object().to_owned().one().context(location_info!())?)? .context(location_info!())?; - let user = get_user_from_activity(&update, context).await?; + let user = get_actor_as_user(&update, context).await?; let comment = CommentForm::from_apub(¬e, context, Some(user.actor_id()?)).await?; diff --git a/lemmy_apub/src/activities/send/comment.rs b/lemmy_apub/src/activities/send/comment.rs index af31dc433..092d7ef3d 100644 --- a/lemmy_apub/src/activities/send/comment.rs +++ b/lemmy_apub/src/activities/send/comment.rs @@ -54,14 +54,17 @@ impl ApubObjectType for Comment { }) .await??; - let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?; + let mut maa = + collect_non_local_mentions_and_addresses(&self.content, &community, context).await?; + let mut ccs = vec![community.actor_id()?]; + ccs.append(&mut maa.addressed_ccs); let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?); create .set_context(activitystreams::context()) .set_id(generate_activity_id(CreateType::Create)?) .set_to(public()) - .set_many_ccs(maa.addressed_ccs.to_owned()) + .set_many_ccs(ccs) // Set the mention tags .set_many_tags(maa.get_tags()?); @@ -83,14 +86,17 @@ impl ApubObjectType for Comment { }) .await??; - let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?; + let mut maa = + collect_non_local_mentions_and_addresses(&self.content, &community, context).await?; + let mut ccs = vec![community.actor_id()?]; + ccs.append(&mut maa.addressed_ccs); let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?); update .set_context(activitystreams::context()) .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) - .set_many_ccs(maa.addressed_ccs.to_owned()) + .set_many_ccs(ccs) // Set the mention tags .set_many_tags(maa.get_tags()?); @@ -116,7 +122,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, delete, context).await?; Ok(()) @@ -144,7 +150,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); @@ -152,7 +158,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, undo, context).await?; Ok(()) @@ -175,7 +181,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&mod_, &community, remove, context).await?; Ok(()) @@ -199,7 +205,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); @@ -207,7 +213,7 @@ impl ApubObjectType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&mod_, &community, undo, context).await?; Ok(()) @@ -233,7 +239,7 @@ impl ApubLikeableType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, like, context).await?; Ok(()) @@ -256,7 +262,7 @@ impl ApubLikeableType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, dislike, context).await?; Ok(()) @@ -283,7 +289,7 @@ impl ApubLikeableType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?); @@ -291,7 +297,7 @@ impl ApubLikeableType for Comment { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, undo, context).await?; Ok(()) diff --git a/lemmy_apub/src/activities/send/post.rs b/lemmy_apub/src/activities/send/post.rs index f7c27964c..81d1a954c 100644 --- a/lemmy_apub/src/activities/send/post.rs +++ b/lemmy_apub/src/activities/send/post.rs @@ -42,7 +42,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(CreateType::Create)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(creator, &community, create, context).await?; Ok(()) @@ -63,7 +63,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(creator, &community, update, context).await?; Ok(()) @@ -83,7 +83,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(creator, &community, delete, context).await?; Ok(()) @@ -107,7 +107,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); @@ -115,7 +115,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(creator, &community, undo, context).await?; Ok(()) @@ -135,7 +135,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(mod_, &community, remove, context).await?; Ok(()) @@ -155,7 +155,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); @@ -163,7 +163,7 @@ impl ApubObjectType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(mod_, &community, undo, context).await?; Ok(()) @@ -186,7 +186,7 @@ impl ApubLikeableType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, like, context).await?; Ok(()) @@ -206,7 +206,7 @@ impl ApubLikeableType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, dislike, context).await?; Ok(()) @@ -230,7 +230,7 @@ impl ApubLikeableType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?); @@ -238,7 +238,7 @@ impl ApubLikeableType for Post { .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) - .set_many_ccs(vec![community.get_followers_url()?]); + .set_many_ccs(vec![community.actor_id()?]); send_to_community(&creator, &community, undo, context).await?; Ok(()) diff --git a/lemmy_apub/src/activities/send/private_message.rs b/lemmy_apub/src/activities/send/private_message.rs index 8c3f5aa9c..fc3e52252 100644 --- a/lemmy_apub/src/activities/send/private_message.rs +++ b/lemmy_apub/src/activities/send/private_message.rs @@ -30,13 +30,13 @@ impl ApubObjectType for PrivateMessage { let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??; let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?); - let to = recipient.get_inbox_url()?; + create .set_context(activitystreams::context()) .set_id(generate_activity_id(CreateType::Create)?) - .set_to(to.clone()); + .set_to(recipient.actor_id()?); - send_activity_single_dest(create, creator, to, context).await?; + send_activity_single_dest(create, creator, recipient.get_inbox_url()?, context).await?; Ok(()) } @@ -48,13 +48,12 @@ impl ApubObjectType for PrivateMessage { let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??; let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?); - let to = recipient.get_inbox_url()?; update .set_context(activitystreams::context()) .set_id(generate_activity_id(UpdateType::Update)?) - .set_to(to.clone()); + .set_to(recipient.actor_id()?); - send_activity_single_dest(update, creator, to, context).await?; + send_activity_single_dest(update, creator, recipient.get_inbox_url()?, context).await?; Ok(()) } @@ -65,13 +64,12 @@ impl ApubObjectType for PrivateMessage { let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??; let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); - let to = recipient.get_inbox_url()?; delete .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) - .set_to(to.clone()); + .set_to(recipient.actor_id()?); - send_activity_single_dest(delete, creator, to, context).await?; + send_activity_single_dest(delete, creator, recipient.get_inbox_url()?, context).await?; Ok(()) } @@ -86,20 +84,19 @@ impl ApubObjectType for PrivateMessage { let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??; let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); - let to = recipient.get_inbox_url()?; delete .set_context(activitystreams::context()) .set_id(generate_activity_id(DeleteType::Delete)?) - .set_to(to.clone()); + .set_to(recipient.actor_id()?); // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo .set_context(activitystreams::context()) .set_id(generate_activity_id(UndoType::Undo)?) - .set_to(to.clone()); + .set_to(recipient.actor_id()?); - send_activity_single_dest(undo, creator, to, context).await?; + send_activity_single_dest(undo, creator, recipient.get_inbox_url()?, context).await?; Ok(()) } diff --git a/lemmy_apub/src/activity_queue.rs b/lemmy_apub/src/activity_queue.rs index 379618f29..715c53044 100644 --- a/lemmy_apub/src/activity_queue.rs +++ b/lemmy_apub/src/activity_queue.rs @@ -31,7 +31,7 @@ use url::Url; pub async fn send_activity_single_dest( activity: T, creator: &dyn ActorType, - to: Url, + inbox: Url, context: &LemmyContext, ) -> Result<(), LemmyError> where @@ -39,13 +39,17 @@ where Kind: Serialize, >::Error: From + Send + Sync + 'static, { - if check_is_apub_id_valid(&to).is_ok() { - debug!("Sending activity {:?} to {}", &activity.id_unchecked(), &to); + if check_is_apub_id_valid(&inbox).is_ok() { + debug!( + "Sending activity {:?} to {}", + &activity.id_unchecked(), + &inbox + ); send_activity_internal( context.activity_queue(), activity, creator, - vec![to], + vec![inbox], context.pool(), true, ) @@ -69,7 +73,7 @@ where // dont send to the local instance, nor to the instance where the activity originally came from, // because that would result in a database error (same data inserted twice) let community_shared_inbox = community.get_shared_inbox_url()?; - let to: Vec = community + let follower_inboxes: Vec = community .get_follower_inboxes(context.pool()) .await? .iter() @@ -89,7 +93,7 @@ where context.activity_queue(), activity, community, - to, + follower_inboxes, context.pool(), true, ) @@ -177,7 +181,7 @@ async fn send_activity_internal( activity_sender: &QueueHandle, activity: T, actor: &dyn ActorType, - to: Vec, + inboxes: Vec, pool: &DbPool, insert_into_db: bool, ) -> Result<(), LemmyError> @@ -186,7 +190,7 @@ where Kind: Serialize, >::Error: From + Send + Sync + 'static, { - if !Settings::get().federation.enabled || to.is_empty() { + if !Settings::get().federation.enabled || inboxes.is_empty() { return Ok(()); } @@ -199,10 +203,10 @@ where insert_activity(actor.user_id(), activity.clone(), true, pool).await?; } - for t in to { + for i in inboxes { let message = SendActivityTask { activity: serialised_activity.to_owned(), - to: t, + inbox: i, actor_id: actor.actor_id()?, private_key: actor.private_key().context(location_info!())?, }; @@ -215,7 +219,7 @@ where #[derive(Clone, Debug, Deserialize, Serialize)] struct SendActivityTask { activity: String, - to: Url, + inbox: Url, actor_id: Url, private_key: String, } @@ -235,7 +239,7 @@ impl ActixJob for SendActivityTask { let result = sign_and_send( &state.client, headers, - &self.to, + &self.inbox, self.activity.clone(), &self.actor_id, self.private_key.to_owned(), @@ -247,7 +251,7 @@ impl ActixJob for SendActivityTask { return Err(anyhow!( "Failed to send activity {} to {}", &self.activity, - self.to + self.inbox )); } Ok(()) diff --git a/lemmy_apub/src/inbox/shared_inbox.rs b/lemmy_apub/src/inbox/shared_inbox.rs index c8ce9cdfe..5b87e0f89 100644 --- a/lemmy_apub/src/inbox/shared_inbox.rs +++ b/lemmy_apub/src/inbox/shared_inbox.rs @@ -11,24 +11,17 @@ use crate::{ }, check_is_apub_id_valid, extensions::signatures::verify, - fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_user}, + fetcher::get_or_fetch_and_upsert_actor, insert_activity, }; -use activitystreams::{ - activity::{ActorAndObject, ActorAndObjectRef}, - base::AsBase, - object::AsObject, - prelude::*, -}; +use activitystreams::{activity::ActorAndObject, prelude::*}; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::Context; -use lemmy_db::user::User_; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; use log::debug; use serde::{Deserialize, Serialize}; use std::fmt::Debug; -use url::Url; #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] @@ -55,22 +48,20 @@ pub async fn shared_inbox( ) -> Result { let activity = input.into_inner(); - let sender = &activity + let actor = activity .actor()? .to_owned() .single_xsd_any_uri() .context(location_info!())?; - let community = get_community_id_from_activity(&activity)?; debug!( "Shared inbox received activity {:?} from {}", &activity.id_unchecked(), - &sender + &actor ); - check_is_apub_id_valid(sender)?; - check_is_apub_id_valid(&community)?; + check_is_apub_id_valid(&actor)?; - let actor = get_or_fetch_and_upsert_actor(sender, &context).await?; + let actor = get_or_fetch_and_upsert_actor(&actor, &context).await?; verify(&request, actor.as_ref())?; let any_base = activity.clone().into_any_base()?; @@ -89,30 +80,3 @@ pub async fn shared_inbox( insert_activity(actor.user_id(), activity.clone(), false, context.pool()).await?; res } - -pub(in crate) async fn get_user_from_activity( - activity: &T, - context: &LemmyContext, -) -> Result -where - T: AsBase + ActorAndObjectRef, -{ - let actor = activity.actor()?; - let user_uri = actor.as_single_xsd_any_uri().context(location_info!())?; - get_or_fetch_and_upsert_user(&user_uri, context).await -} - -pub(in crate) fn get_community_id_from_activity(activity: &T) -> Result -where - T: AsBase + ActorAndObjectRef + AsObject, -{ - let cc = activity.cc().context(location_info!())?; - let cc = cc.as_many().context(location_info!())?; - Ok( - cc.first() - .context(location_info!())? - .as_xsd_any_uri() - .context(location_info!())? - .to_owned(), - ) -}