use crate::{
- activities::{community::announce::GetCommunity, verify_person_in_community},
+ activities::community::announce::GetCommunity,
objects::community::ApubCommunity,
protocol::{
activities::{
block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
community::{
add_mod::AddMod,
- announce::AnnounceActivity,
+ announce::{AnnounceActivity, RawAnnouncableActivities},
remove_mod::RemoveMod,
report::Report,
update::UpdateCommunity,
private_message::CreateOrUpdatePrivateMessage,
},
deletion::{delete::Delete, delete_user::DeleteUser, undo_delete::UndoDelete},
- following::{
- accept::AcceptFollowCommunity,
- follow::FollowCommunity,
- undo_follow::UndoFollowCommunity,
- },
+ following::{accept::AcceptFollow, follow::Follow, undo_follow::UndoFollow},
voting::{undo_vote::UndoVote, vote::Vote},
},
objects::page::Page,
- Id,
},
};
-use activitypub_federation::{
- core::object_id::ObjectId,
- data::Data,
- deser::context::WithContext,
- traits::{activity_handler, ActivityHandler},
-};
+use activitypub_federation::{data::Data, deser::context::WithContext, traits::ActivityHandler};
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
-#[activity_handler(LemmyContext, LemmyError)]
+#[enum_delegate::implement(ActivityHandler)]
pub enum SharedInboxActivities {
- GroupInboxActivities(Box<WithContext<GroupInboxActivities>>),
- // Note, pm activities need to be at the end, otherwise comments will end up here. We can probably
- // avoid this problem by replacing createpm.object with our own struct, instead of NoteExt.
PersonInboxActivities(Box<WithContext<PersonInboxActivities>>),
+ GroupInboxActivities(Box<WithContext<GroupInboxActivities>>),
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
+#[enum_delegate::implement(ActivityHandler)]
pub enum GroupInboxActivities {
- FollowCommunity(FollowCommunity),
- UndoFollowCommunity(UndoFollowCommunity),
- AnnouncableActivities(Box<AnnouncableActivities>),
+ Follow(Follow),
+ UndoFollow(UndoFollow),
Report(Report),
+ // This is a catch-all and needs to be last
+ AnnouncableActivities(RawAnnouncableActivities),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
-#[activity_handler(LemmyContext, LemmyError)]
+#[enum_delegate::implement(ActivityHandler)]
pub enum PersonInboxActivities {
- AcceptFollowCommunity(AcceptFollowCommunity),
- /// Some activities can also be sent from user to user, eg a comment with mentions
- AnnouncableActivities(AnnouncableActivities),
+ AcceptFollow(AcceptFollow),
+ UndoFollow(UndoFollow),
+ FollowCommunity(Follow),
CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
Delete(Delete),
UndoDelete(UndoDelete),
AnnounceActivity(AnnounceActivity),
}
+/// This is necessary for user inbox, which can also receive some "announcable" activities,
+/// eg a comment mention. This needs to be a separate enum so that announcables received in shared
+/// inbox can fall through to be parsed as GroupInboxActivities::AnnouncableActivities.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
-#[activity_handler(LemmyContext, LemmyError)]
+#[enum_delegate::implement(ActivityHandler)]
+pub enum PersonInboxActivitiesWithAnnouncable {
+ PersonInboxActivities(Box<PersonInboxActivities>),
+ AnnouncableActivities(Box<AnnouncableActivities>),
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(untagged)]
+#[enum_delegate::implement(ActivityHandler)]
pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment),
- CreateOrUpdatePost(Box<CreateOrUpdatePost>),
+ CreateOrUpdatePost(CreateOrUpdatePost),
Vote(Vote),
UndoVote(UndoVote),
Delete(Delete),
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
-#[activity_handler(LemmyContext, LemmyError)]
+#[enum_delegate::implement(ActivityHandler)]
#[allow(clippy::enum_variant_names)]
pub enum SiteInboxActivities {
BlockUser(BlockUser),
}
}
-impl Id for AnnouncableActivities {
- fn object_id(&self) -> &Url {
- ActivityHandler::id(self)
- }
-}
-
-// Need to implement this manually to announce matching activities
-#[async_trait::async_trait(?Send)]
-impl ActivityHandler for GroupInboxActivities {
- type DataType = LemmyContext;
- type Error = LemmyError;
-
- fn id(&self) -> &Url {
- match self {
- GroupInboxActivities::FollowCommunity(a) => a.id(),
- GroupInboxActivities::UndoFollowCommunity(a) => a.id(),
- GroupInboxActivities::AnnouncableActivities(a) => a.object_id(),
- GroupInboxActivities::Report(a) => a.id(),
- }
- }
-
- fn actor(&self) -> &Url {
- match self {
- GroupInboxActivities::FollowCommunity(a) => a.actor(),
- GroupInboxActivities::UndoFollowCommunity(a) => a.actor(),
- GroupInboxActivities::AnnouncableActivities(a) => a.actor(),
- GroupInboxActivities::Report(a) => a.actor(),
- }
- }
-
- async fn verify(
- &self,
- data: &Data<Self::DataType>,
- request_counter: &mut i32,
- ) -> Result<(), LemmyError> {
- match self {
- GroupInboxActivities::FollowCommunity(a) => a.verify(data, request_counter).await,
- GroupInboxActivities::UndoFollowCommunity(a) => a.verify(data, request_counter).await,
- GroupInboxActivities::AnnouncableActivities(a) => a.verify(data, request_counter).await,
- GroupInboxActivities::Report(a) => a.verify(data, request_counter).await,
- }
- }
-
- async fn receive(
- self,
- data: &Data<Self::DataType>,
- request_counter: &mut i32,
- ) -> Result<(), LemmyError> {
- match self {
- GroupInboxActivities::FollowCommunity(a) => a.receive(data, request_counter).await,
- GroupInboxActivities::UndoFollowCommunity(a) => a.receive(data, request_counter).await,
- GroupInboxActivities::AnnouncableActivities(activity) => {
- activity.clone().receive(data, request_counter).await?;
-
- // Ignore failures in get_community(). those happen because Delete/PrivateMessage is not in a
- // community, but looks identical to Delete/Post or Delete/Comment which are in a community.
- let community = activity.get_community(data, &mut 0).await;
- if let Ok(community) = community {
- if community.local {
- let actor_id = ObjectId::new(activity.actor().clone());
- verify_person_in_community(&actor_id, &community, data, &mut 0).await?;
- AnnounceActivity::send(*activity, &community, data).await?;
- }
- }
- Ok(())
- }
- GroupInboxActivities::Report(a) => a.receive(data, request_counter).await,
- }
- }
-}
-
#[cfg(test)]
mod tests {
use crate::{
- activity_lists::{GroupInboxActivities, PersonInboxActivities, SiteInboxActivities},
+ activity_lists::{
+ GroupInboxActivities,
+ PersonInboxActivities,
+ PersonInboxActivitiesWithAnnouncable,
+ SiteInboxActivities,
+ },
protocol::tests::test_parse_lemmy_item,
};
fn test_person_inbox() {
test_parse_lemmy_item::<PersonInboxActivities>("assets/lemmy/activities/following/accept.json")
.unwrap();
- test_parse_lemmy_item::<PersonInboxActivities>(
+ test_parse_lemmy_item::<PersonInboxActivitiesWithAnnouncable>(
"assets/lemmy/activities/create_or_update/create_note.json",
)
.unwrap();
- test_parse_lemmy_item::<PersonInboxActivities>(
+ test_parse_lemmy_item::<PersonInboxActivitiesWithAnnouncable>(
"assets/lemmy/activities/create_or_update/create_private_message.json",
)
.unwrap();