From 28ca5da78f61d90c6ad50a5ff875b95b61110f78 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 27 Sep 2021 18:57:19 +0200 Subject: [PATCH] Remove dependency of apub_lib on LemmyContext --- Cargo.lock | 1 - .../activities/comment/create_or_update.rs | 14 +++++-- .../apub/src/activities/community/add_mod.rs | 8 ++-- .../apub/src/activities/community/announce.rs | 8 ++-- .../src/activities/community/block_user.rs | 7 ++-- .../src/activities/community/remove_mod.rs | 7 ++-- .../activities/community/undo_block_user.rs | 7 ++-- .../apub/src/activities/community/update.rs | 7 ++-- crates/apub/src/activities/deletion/delete.rs | 7 ++-- .../src/activities/deletion/undo_delete.rs | 7 ++-- .../apub/src/activities/following/accept.rs | 7 ++-- .../apub/src/activities/following/follow.rs | 7 ++-- crates/apub/src/activities/following/undo.rs | 7 ++-- .../src/activities/post/create_or_update.rs | 6 ++- .../private_message/create_or_update.rs | 7 ++-- .../src/activities/private_message/delete.rs | 7 ++-- .../activities/private_message/undo_delete.rs | 13 ++++-- crates/apub/src/activities/undo_remove.rs | 7 ++-- .../apub/src/activities/voting/undo_vote.rs | 7 ++-- crates/apub/src/activities/voting/vote.rs | 7 ++-- crates/apub/src/fetcher/community.rs | 6 ++- crates/apub/src/http/community.rs | 1 + crates/apub/src/http/mod.rs | 13 ++++-- crates/apub/src/http/person.rs | 1 + crates/apub_lib/Cargo.toml | 1 - crates/apub_lib/src/lib.rs | 41 +++++++++++++++++-- crates/apub_lib_derive/src/lib.rs | 31 +++++++++++--- 27 files changed, 170 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a2d33dd79..0e87d3cf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1790,7 +1790,6 @@ dependencies = [ "async-trait", "lemmy_apub_lib_derive", "lemmy_utils", - "lemmy_websocket", "log", "reqwest", "serde", diff --git a/crates/apub/src/activities/comment/create_or_update.rs b/crates/apub/src/activities/comment/create_or_update.rs index 130623ff7..e469e74b3 100644 --- a/crates/apub/src/activities/comment/create_or_update.rs +++ b/crates/apub/src/activities/comment/create_or_update.rs @@ -16,7 +16,13 @@ use crate::{ }; use activitystreams::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed}; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, verify_domains_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{ + values::PublicUrl, + verify_domains_match, + ActivityFields, + ActivityHandler, + Data, +}; use lemmy_db_queries::Crud; use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post}; use lemmy_utils::LemmyError; @@ -82,9 +88,11 @@ impl CreateOrUpdateComment { #[async_trait::async_trait(?Send)] impl ActivityHandler for CreateOrUpdateComment { + type DataType = LemmyContext; + async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let community = extract_community(&self.cc, context, request_counter).await?; @@ -101,7 +109,7 @@ impl ActivityHandler for CreateOrUpdateComment { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let comment = diff --git a/crates/apub/src/activities/community/add_mod.rs b/crates/apub/src/activities/community/add_mod.rs index a71414dd2..93af4efba 100644 --- a/crates/apub/src/activities/community/add_mod.rs +++ b/crates/apub/src/activities/community/add_mod.rs @@ -20,7 +20,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{source::community::CommunityModerator_, Joinable}; use lemmy_db_schema::source::{ community::{Community, CommunityModerator, CommunityModeratorForm}, @@ -79,9 +79,11 @@ impl AddMod { #[async_trait::async_trait(?Send)] impl ActivityHandler for AddMod { + type DataType = LemmyContext; + async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -93,7 +95,7 @@ impl ActivityHandler for AddMod { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let community = self.cc[0].dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index dae37b60e..032797c81 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -31,7 +31,7 @@ use activitystreams::{ primitives::OneOrMany, unparsed::Unparsed, }; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_schema::source::community::Community; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; @@ -40,6 +40,7 @@ use url::Url; #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)] #[serde(untagged)] +#[activity_handler(LemmyContext)] pub enum AnnouncableActivities { CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdatePost(Box), @@ -98,9 +99,10 @@ impl AnnounceActivity { #[async_trait::async_trait(?Send)] impl ActivityHandler for AnnounceActivity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -111,7 +113,7 @@ impl ActivityHandler for AnnounceActivity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { if is_activity_already_known(context.pool(), self.object.id_unchecked()).await? { diff --git a/crates/apub/src/activities/community/block_user.rs b/crates/apub/src/activities/community/block_user.rs index ad9999884..976dbc8a3 100644 --- a/crates/apub/src/activities/community/block_user.rs +++ b/crates/apub/src/activities/community/block_user.rs @@ -18,7 +18,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{Bannable, Followable}; use lemmy_db_schema::source::{ community::{ @@ -90,9 +90,10 @@ impl BlockUserFromCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for BlockUserFromCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -103,7 +104,7 @@ impl ActivityHandler for BlockUserFromCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let community = self.cc[0].dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/community/remove_mod.rs b/crates/apub/src/activities/community/remove_mod.rs index 2da9bbdbb..3de9ccca1 100644 --- a/crates/apub/src/activities/community/remove_mod.rs +++ b/crates/apub/src/activities/community/remove_mod.rs @@ -21,7 +21,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Joinable; use lemmy_db_schema::source::{ community::{Community, CommunityModerator, CommunityModeratorForm}, @@ -81,9 +81,10 @@ impl RemoveMod { #[async_trait::async_trait(?Send)] impl ActivityHandler for RemoveMod { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -107,7 +108,7 @@ impl ActivityHandler for RemoveMod { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { if self.target.is_some() { diff --git a/crates/apub/src/activities/community/undo_block_user.rs b/crates/apub/src/activities/community/undo_block_user.rs index 29aa83f5b..cf70fc32c 100644 --- a/crates/apub/src/activities/community/undo_block_user.rs +++ b/crates/apub/src/activities/community/undo_block_user.rs @@ -18,7 +18,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Bannable; use lemmy_db_schema::source::{ community::{Community, CommunityPersonBan, CommunityPersonBanForm}, @@ -77,9 +77,10 @@ impl UndoBlockUserFromCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoBlockUserFromCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -91,7 +92,7 @@ impl ActivityHandler for UndoBlockUserFromCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let community = self.cc[0].dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index d217e2561..ec8a2bea9 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -19,7 +19,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{ApubObject, Crud}; use lemmy_db_schema::source::{ community::{Community, CommunityForm}, @@ -77,9 +77,10 @@ impl UpdateCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for UpdateCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -90,7 +91,7 @@ impl ActivityHandler for UpdateCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, _request_counter: &mut i32, ) -> Result<(), LemmyError> { let cc = self.cc[0].clone().into(); diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index 9e957a8ee..c9821cb65 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -23,7 +23,7 @@ use activitystreams::{ }; use anyhow::anyhow; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{ source::{comment::Comment_, community::Community_, post::Post_}, Crud, @@ -82,9 +82,10 @@ pub struct Delete { #[async_trait::async_trait(?Send)] impl ActivityHandler for Delete { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -102,7 +103,7 @@ impl ActivityHandler for Delete { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { if let Some(reason) = self.summary { diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index f74c34c90..f7f07297f 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -24,7 +24,7 @@ use activitystreams::{ }; use anyhow::anyhow; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_}; use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post}; use lemmy_utils::LemmyError; @@ -54,9 +54,10 @@ pub struct UndoDelete { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoDelete { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -75,7 +76,7 @@ impl ActivityHandler for UndoDelete { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { if self.object.summary.is_some() { diff --git a/crates/apub/src/activities/following/accept.rs b/crates/apub/src/activities/following/accept.rs index 86b0d2093..ab28b9454 100644 --- a/crates/apub/src/activities/following/accept.rs +++ b/crates/apub/src/activities/following/accept.rs @@ -17,7 +17,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{ApubObject, Followable}; use lemmy_db_schema::source::{ community::{Community, CommunityFollower}, @@ -75,9 +75,10 @@ impl AcceptFollowCommunity { /// Handle accepted follows #[async_trait::async_trait(?Send)] impl ActivityHandler for AcceptFollowCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -90,7 +91,7 @@ impl ActivityHandler for AcceptFollowCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index 21446eb12..3171d72f7 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -17,7 +17,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Followable; use lemmy_db_schema::source::{ community::{Community, CommunityFollower, CommunityFollowerForm}, @@ -86,9 +86,10 @@ impl FollowCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for FollowCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -99,7 +100,7 @@ impl ActivityHandler for FollowCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/following/undo.rs b/crates/apub/src/activities/following/undo.rs index cc103075d..ae6cc9bbc 100644 --- a/crates/apub/src/activities/following/undo.rs +++ b/crates/apub/src/activities/following/undo.rs @@ -17,7 +17,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{verify_urls_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Followable; use lemmy_db_schema::source::{ community::{Community, CommunityFollower, CommunityFollowerForm}, @@ -69,9 +69,10 @@ impl UndoFollowCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoFollowCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -84,7 +85,7 @@ impl ActivityHandler for UndoFollowCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/post/create_or_update.rs b/crates/apub/src/activities/post/create_or_update.rs index ae93f0988..a46c9b066 100644 --- a/crates/apub/src/activities/post/create_or_update.rs +++ b/crates/apub/src/activities/post/create_or_update.rs @@ -22,6 +22,7 @@ use lemmy_apub_lib::{ verify_urls_match, ActivityFields, ActivityHandler, + Data, }; use lemmy_db_queries::Crud; use lemmy_db_schema::source::{community::Community, person::Person, post::Post}; @@ -81,9 +82,10 @@ impl CreateOrUpdatePost { #[async_trait::async_trait(?Send)] impl ActivityHandler for CreateOrUpdatePost { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -119,7 +121,7 @@ impl ActivityHandler for CreateOrUpdatePost { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/private_message/create_or_update.rs b/crates/apub/src/activities/private_message/create_or_update.rs index f3b1c91fb..e2519dc28 100644 --- a/crates/apub/src/activities/private_message/create_or_update.rs +++ b/crates/apub/src/activities/private_message/create_or_update.rs @@ -8,7 +8,7 @@ use crate::{ }; use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed}; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Crud; use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; use lemmy_utils::LemmyError; @@ -61,9 +61,10 @@ impl CreateOrUpdatePrivateMessage { } #[async_trait::async_trait(?Send)] impl ActivityHandler for CreateOrUpdatePrivateMessage { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -75,7 +76,7 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let private_message = diff --git a/crates/apub/src/activities/private_message/delete.rs b/crates/apub/src/activities/private_message/delete.rs index bf12f9d1e..107a50891 100644 --- a/crates/apub/src/activities/private_message/delete.rs +++ b/crates/apub/src/activities/private_message/delete.rs @@ -12,7 +12,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{verify_domains_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject, Crud}; use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; use lemmy_utils::LemmyError; @@ -72,9 +72,10 @@ impl DeletePrivateMessage { #[async_trait::async_trait(?Send)] impl ActivityHandler for DeletePrivateMessage { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -85,7 +86,7 @@ impl ActivityHandler for DeletePrivateMessage { async fn receive( self, - context: &LemmyContext, + context: &Data, _request_counter: &mut i32, ) -> Result<(), LemmyError> { let ap_id = self.object.clone(); diff --git a/crates/apub/src/activities/private_message/undo_delete.rs b/crates/apub/src/activities/private_message/undo_delete.rs index 5aa6dbed4..aff4a59a2 100644 --- a/crates/apub/src/activities/private_message/undo_delete.rs +++ b/crates/apub/src/activities/private_message/undo_delete.rs @@ -17,7 +17,13 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{verify_domains_match, verify_urls_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{ + verify_domains_match, + verify_urls_match, + ActivityFields, + ActivityHandler, + Data, +}; use lemmy_db_queries::{source::private_message::PrivateMessage_, ApubObject, Crud}; use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage}; use lemmy_utils::LemmyError; @@ -71,9 +77,10 @@ impl UndoDeletePrivateMessage { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoDeletePrivateMessage { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -86,7 +93,7 @@ impl ActivityHandler for UndoDeletePrivateMessage { async fn receive( self, - context: &LemmyContext, + context: &Data, _request_counter: &mut i32, ) -> Result<(), LemmyError> { let ap_id = self.object.object.clone(); diff --git a/crates/apub/src/activities/undo_remove.rs b/crates/apub/src/activities/undo_remove.rs index 422d0deea..f92938f5c 100644 --- a/crates/apub/src/activities/undo_remove.rs +++ b/crates/apub/src/activities/undo_remove.rs @@ -12,7 +12,7 @@ use activitystreams::{ primitives::OneOrMany, unparsed::Unparsed, }; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_schema::source::{community::Community, person::Person}; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; @@ -38,9 +38,10 @@ pub struct UndoRemovePostCommentOrCommunity { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoRemovePostCommentOrCommunity { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -60,7 +61,7 @@ impl ActivityHandler for UndoRemovePostCommentOrCommunity { async fn receive( self, - context: &LemmyContext, + context: &Data, _request_counter: &mut i32, ) -> Result<(), LemmyError> { UndoDelete::receive_undo_remove_action(self.object.object.inner(), context).await diff --git a/crates/apub/src/activities/voting/undo_vote.rs b/crates/apub/src/activities/voting/undo_vote.rs index 963292fa5..06e73ae22 100644 --- a/crates/apub/src/activities/voting/undo_vote.rs +++ b/crates/apub/src/activities/voting/undo_vote.rs @@ -23,7 +23,7 @@ use activitystreams::{ unparsed::Unparsed, }; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Crud; use lemmy_db_schema::{ source::{community::Community, person::Person}, @@ -86,9 +86,10 @@ impl UndoVote { #[async_trait::async_trait(?Send)] impl ActivityHandler for UndoVote { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -100,7 +101,7 @@ impl ActivityHandler for UndoVote { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/activities/voting/vote.rs b/crates/apub/src/activities/voting/vote.rs index e74dea108..23b728797 100644 --- a/crates/apub/src/activities/voting/vote.rs +++ b/crates/apub/src/activities/voting/vote.rs @@ -15,7 +15,7 @@ use crate::{ use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed}; use anyhow::anyhow; use lemmy_api_common::blocking; -use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::Crud; use lemmy_db_schema::{ source::{community::Community, person::Person}, @@ -112,9 +112,10 @@ impl Vote { #[async_trait::async_trait(?Send)] impl ActivityHandler for Vote { + type DataType = LemmyContext; async fn verify( &self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_activity(self, &context.settings())?; @@ -124,7 +125,7 @@ impl ActivityHandler for Vote { async fn receive( self, - context: &LemmyContext, + context: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError> { let actor = self.actor.dereference(context, request_counter).await?; diff --git a/crates/apub/src/fetcher/community.rs b/crates/apub/src/fetcher/community.rs index 174741128..329d4ad54 100644 --- a/crates/apub/src/fetcher/community.rs +++ b/crates/apub/src/fetcher/community.rs @@ -6,7 +6,7 @@ use crate::{ use activitystreams::collection::{CollectionExt, OrderedCollection}; use anyhow::Context; use lemmy_api_common::blocking; -use lemmy_apub_lib::ActivityHandler; +use lemmy_apub_lib::{ActivityHandler, Data}; use lemmy_db_queries::Joinable; use lemmy_db_schema::source::{ community::{Community, CommunityModerator, CommunityModeratorForm}, @@ -91,7 +91,9 @@ pub(crate) async fn fetch_community_outbox( // AnnounceActivity as inner type, but that gives me stackoverflow let ser = serde_json::to_string(&announce)?; let announce: AnnounceActivity = serde_json::from_str(&ser)?; - announce.receive(context, recursion_counter).await?; + announce + .receive(&Data::new(context.clone()), recursion_counter) + .await?; } Ok(()) diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index cd75031dc..3eed569c3 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -60,6 +60,7 @@ pub(crate) async fn get_apub_community_http( #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)] #[serde(untagged)] +#[activity_handler(LemmyContext)] pub enum GroupInboxActivities { FollowCommunity(FollowCommunity), UndoFollowCommunity(UndoFollowCommunity), diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index a70ed6f00..1471f02d7 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -20,7 +20,7 @@ use anyhow::{anyhow, Context}; use futures::StreamExt; use http::StatusCode; use lemmy_api_common::blocking; -use lemmy_apub_lib::{ActivityFields, ActivityHandler}; +use lemmy_apub_lib::{ActivityFields, ActivityHandler, Data}; use lemmy_db_queries::{source::activity::Activity_, DbPool}; use lemmy_db_schema::source::activity::Activity; use lemmy_utils::{location_info, LemmyError}; @@ -38,6 +38,7 @@ pub mod routes; #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)] #[serde(untagged)] +#[activity_handler(LemmyContext)] pub enum SharedInboxActivities { GroupInboxActivities(GroupInboxActivities), // Note, pm activities need to be at the end, otherwise comments will end up here. We can probably @@ -80,7 +81,7 @@ async fn receive_activity<'a, T>( context: &LemmyContext, ) -> Result where - T: ActivityHandler + T: ActivityHandler + ActivityFields + Clone + Deserialize<'a> @@ -100,7 +101,9 @@ where } check_is_apub_id_valid(activity.actor(), false, &context.settings())?; info!("Verifying activity {}", activity.id_unchecked().to_string()); - activity.verify(context, request_counter).await?; + activity + .verify(&Data::new(context.clone()), request_counter) + .await?; assert_activity_not_local(&activity, &context.settings().hostname)?; // Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen @@ -115,7 +118,9 @@ where .await?; info!("Receiving activity {}", activity.id_unchecked().to_string()); - activity.receive(context, request_counter).await?; + activity + .receive(&Data::new(context.clone()), request_counter) + .await?; Ok(HttpResponse::Ok().finish()) } diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index dcab60017..d00e5aaa6 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -61,6 +61,7 @@ pub(crate) async fn get_apub_person_http( #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)] #[serde(untagged)] +#[activity_handler(LemmyContext)] pub enum PersonInboxActivities { AcceptFollowCommunity(AcceptFollowCommunity), /// Some activities can also be sent from user to user, eg a comment with mentions diff --git a/crates/apub_lib/Cargo.toml b/crates/apub_lib/Cargo.toml index cdcacd7b4..2a94eb375 100644 --- a/crates/apub_lib/Cargo.toml +++ b/crates/apub_lib/Cargo.toml @@ -7,7 +7,6 @@ license = "AGPL-3.0" [dependencies] lemmy_utils = { version = "=0.13.0", path = "../utils" } -lemmy_websocket = { version = "=0.13.0", path = "../websocket" } lemmy_apub_lib_derive = { version = "=0.13.0", path = "../apub_lib_derive" } activitystreams = "0.7.0-alpha.11" serde = { version = "1.0.130", features = ["derive"] } diff --git a/crates/apub_lib/src/lib.rs b/crates/apub_lib/src/lib.rs index 846666ed2..192108e29 100644 --- a/crates/apub_lib/src/lib.rs +++ b/crates/apub_lib/src/lib.rs @@ -3,7 +3,7 @@ pub mod values; use activitystreams::error::DomainError; pub use lemmy_apub_lib_derive::*; use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; +use std::{ops::Deref, sync::Arc}; use url::Url; pub mod webfinger; @@ -16,15 +16,16 @@ pub trait ActivityFields { #[async_trait::async_trait(?Send)] pub trait ActivityHandler { + type DataType; async fn verify( &self, - context: &LemmyContext, + data: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError>; async fn receive( self, - context: &LemmyContext, + data: &Data, request_counter: &mut i32, ) -> Result<(), LemmyError>; } @@ -42,3 +43,37 @@ pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), LemmyError> { } Ok(()) } + +#[derive(Debug)] +pub struct Data(Arc); + +impl Data { + /// Create new `Data` instance. + pub fn new(state: T) -> Data { + Data(Arc::new(state)) + } + + /// Get reference to inner app data. + pub fn get_ref(&self) -> &T { + self.0.as_ref() + } + + /// Convert to the internal Arc + pub fn into_inner(self) -> Arc { + self.0 + } +} + +impl Deref for Data { + type Target = Arc; + + fn deref(&self) -> &Arc { + &self.0 + } +} + +impl Clone for Data { + fn clone(&self) -> Data { + Data(self.0.clone()) + } +} diff --git a/crates/apub_lib_derive/src/lib.rs b/crates/apub_lib_derive/src/lib.rs index c04f9747c..a00644c65 100644 --- a/crates/apub_lib_derive/src/lib.rs +++ b/crates/apub_lib_derive/src/lib.rs @@ -1,6 +1,6 @@ -use proc_macro2::TokenStream; +use proc_macro2::{TokenStream, TokenTree}; use quote::quote; -use syn::{parse_macro_input, Data, DeriveInput, Fields::Unnamed, Ident, Variant}; +use syn::{parse_macro_input, Attribute, Data, DeriveInput, Fields::Unnamed, Ident, Variant}; /// Generates implementation ActivityHandler for an enum, which looks like the following (handling /// all enum variants). @@ -46,9 +46,29 @@ use syn::{parse_macro_input, Data, DeriveInput, Fields::Unnamed, Ident, Variant} /// } /// /// ``` -#[proc_macro_derive(ActivityHandler)] +#[proc_macro_derive(ActivityHandler, attributes(activity_handler))] pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input = parse_macro_input!(input as DeriveInput); + let attrs: Vec<&Attribute> = input + .attrs + .iter() + .filter(|attr| attr.path.is_ident("activity_handler")) + .collect(); + let attrs: &Vec = &attrs + .first() + .unwrap() + .tokens + .clone() + .into_iter() + .map(|t| { + if let TokenTree::Group(g) = t { + g.stream() + } else { + panic!() + } + }) + .collect(); + let attrs = attrs.first(); let enum_name = input.ident; @@ -72,9 +92,10 @@ pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::To let expanded = quote! { #[async_trait::async_trait(?Send)] impl #impl_generics lemmy_apub_lib::ActivityHandler for #enum_name #ty_generics #where_clause { + type DataType = #attrs; async fn verify( &self, - context: &lemmy_websocket::LemmyContext, + context: &lemmy_apub_lib::Data, request_counter: &mut i32, ) -> Result<(), lemmy_utils::LemmyError> { match self { @@ -83,7 +104,7 @@ pub fn derive_activity_handler(input: proc_macro::TokenStream) -> proc_macro::To } async fn receive( self, - context: &lemmy_websocket::LemmyContext, + context: &lemmy_apub_lib::Data, request_counter: &mut i32, ) -> Result<(), lemmy_utils::LemmyError> { match self {