Remove dependency of apub_lib on LemmyContext

This commit is contained in:
Felix Ableitner 2021-09-27 18:57:19 +02:00
parent fe7c1b300b
commit 28ca5da78f
27 changed files with 170 additions and 72 deletions

1
Cargo.lock generated
View file

@ -1790,7 +1790,6 @@ dependencies = [
"async-trait",
"lemmy_apub_lib_derive",
"lemmy_utils",
"lemmy_websocket",
"log",
"reqwest",
"serde",

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let comment =

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community = self.cc[0].dereference(context, request_counter).await?;

View file

@ -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<CreateOrUpdatePost>),
@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
if is_activity_already_known(context.pool(), self.object.id_unchecked()).await? {

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community = self.cc[0].dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
if self.target.is_some() {

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community = self.cc[0].dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
let cc = self.cc[0].clone().into();

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
if let Some(reason) = self.summary {

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
if self.object.summary.is_some() {

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let private_message =

View file

@ -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<LemmyContext>,
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<LemmyContext>,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
let ap_id = self.object.clone();

View file

@ -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<LemmyContext>,
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<LemmyContext>,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
let ap_id = self.object.object.clone();

View file

@ -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<LemmyContext>,
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<LemmyContext>,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
UndoDelete::receive_undo_remove_action(self.object.object.inner(), context).await

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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<LemmyContext>,
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<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?;

View file

@ -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(())

View file

@ -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),

View file

@ -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<HttpResponse, LemmyError>
where
T: ActivityHandler
T: ActivityHandler<DataType = LemmyContext>
+ 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())
}

View file

@ -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

View file

@ -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"] }

View file

@ -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<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), LemmyError>;
async fn receive(
self,
context: &LemmyContext,
data: &Data<Self::DataType>,
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<T: ?Sized>(Arc<T>);
impl<T> Data<T> {
/// Create new `Data` instance.
pub fn new(state: T) -> Data<T> {
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<T>
pub fn into_inner(self) -> Arc<T> {
self.0
}
}
impl<T: ?Sized> Deref for Data<T> {
type Target = Arc<T>;
fn deref(&self) -> &Arc<T> {
&self.0
}
}
impl<T: ?Sized> Clone for Data<T> {
fn clone(&self) -> Data<T> {
Data(self.0.clone())
}
}

View file

@ -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<TokenStream> = &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<Self::DataType>,
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<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), lemmy_utils::LemmyError> {
match self {