]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activity_lists.rs
Implement federated user following (fixes #752) (#2577)
[lemmy.git] / crates / apub / src / activity_lists.rs
index 1197af85fc51873ee9e14cdf73a2a0bd6d5bc3df..242f26a1f5088218b1ec1c4ed94cbe5bc371cc0b 100644 (file)
 use crate::{
   activities::community::announce::GetCommunity,
   objects::community::ApubCommunity,
-  protocol::activities::{
-    community::{
-      add_mod::AddMod,
-      announce::AnnounceActivity,
-      block_user::BlockUserFromCommunity,
-      remove_mod::RemoveMod,
-      report::Report,
-      undo_block_user::UndoBlockUserFromCommunity,
-      update::UpdateCommunity,
+  protocol::{
+    activities::{
+      block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
+      community::{
+        add_mod::AddMod,
+        announce::{AnnounceActivity, RawAnnouncableActivities},
+        remove_mod::RemoveMod,
+        report::Report,
+        update::UpdateCommunity,
+      },
+      create_or_update::{
+        comment::CreateOrUpdateComment,
+        post::CreateOrUpdatePost,
+        private_message::CreateOrUpdatePrivateMessage,
+      },
+      deletion::{delete::Delete, delete_user::DeleteUser, undo_delete::UndoDelete},
+      following::{accept::AcceptFollow, follow::Follow, undo_follow::UndoFollow},
+      voting::{undo_vote::UndoVote, vote::Vote},
     },
-    create_or_update::{comment::CreateOrUpdateComment, post::CreateOrUpdatePost},
-    deletion::{delete::Delete, undo_delete::UndoDelete},
-    following::{
-      accept::AcceptFollowCommunity,
-      follow::FollowCommunity,
-      undo_follow::UndoFollowCommunity,
-    },
-    private_message::{
-      create_or_update::CreateOrUpdatePrivateMessage,
-      delete::DeletePrivateMessage,
-      undo_delete::UndoDeletePrivateMessage,
-    },
-    voting::{undo_vote::UndoVote, vote::Vote},
+    objects::page::Page,
   },
 };
-use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler};
-use lemmy_utils::LemmyError;
+use activitypub_federation::{data::Data, deser::context::WithContext, traits::ActivityHandler};
+use lemmy_utils::error::LemmyError;
 use lemmy_websocket::LemmyContext;
 use serde::{Deserialize, Serialize};
+use url::Url;
 
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
+#[derive(Debug, Deserialize, Serialize)]
 #[serde(untagged)]
-#[activity_handler(LemmyContext)]
+#[enum_delegate::implement(ActivityHandler)]
 pub enum SharedInboxActivities {
-  GroupInboxActivities(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(PersonInboxActivities),
+  PersonInboxActivities(Box<WithContext<PersonInboxActivities>>),
+  GroupInboxActivities(Box<WithContext<GroupInboxActivities>>),
 }
 
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
+#[derive(Debug, Deserialize, Serialize)]
 #[serde(untagged)]
-#[activity_handler(LemmyContext)]
+#[enum_delegate::implement(ActivityHandler)]
 pub enum GroupInboxActivities {
-  FollowCommunity(FollowCommunity),
-  UndoFollowCommunity(UndoFollowCommunity),
-  AnnouncableActivities(AnnouncableActivities),
+  Follow(Follow),
+  UndoFollow(UndoFollow),
   Report(Report),
+  // This is a catch-all and needs to be last
+  AnnouncableActivities(RawAnnouncableActivities),
 }
 
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
+#[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(untagged)]
-#[activity_handler(LemmyContext)]
+#[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),
-  DeletePrivateMessage(DeletePrivateMessage),
-  UndoDeletePrivateMessage(UndoDeletePrivateMessage),
-  AnnounceActivity(Box<AnnounceActivity>),
+  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)]
+#[enum_delegate::implement(ActivityHandler)]
+pub enum PersonInboxActivitiesWithAnnouncable {
+  PersonInboxActivities(Box<PersonInboxActivities>),
+  AnnouncableActivities(Box<AnnouncableActivities>),
 }
 
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
+#[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(untagged)]
-#[activity_handler(LemmyContext)]
+#[enum_delegate::implement(ActivityHandler)]
 pub enum AnnouncableActivities {
   CreateOrUpdateComment(CreateOrUpdateComment),
-  CreateOrUpdatePost(Box<CreateOrUpdatePost>),
+  CreateOrUpdatePost(CreateOrUpdatePost),
   Vote(Vote),
   UndoVote(UndoVote),
   Delete(Delete),
   UndoDelete(UndoDelete),
-  UpdateCommunity(Box<UpdateCommunity>),
-  BlockUserFromCommunity(BlockUserFromCommunity),
-  UndoBlockUserFromCommunity(UndoBlockUserFromCommunity),
+  UpdateCommunity(UpdateCommunity),
+  BlockUser(BlockUser),
+  UndoBlockUser(UndoBlockUser),
   AddMod(AddMod),
   RemoveMod(RemoveMod),
+  // For compatibility with Pleroma/Mastodon (send only)
+  Page(Page),
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(untagged)]
+#[enum_delegate::implement(ActivityHandler)]
+#[allow(clippy::enum_variant_names)]
+pub enum SiteInboxActivities {
+  BlockUser(BlockUser),
+  UndoBlockUser(UndoBlockUser),
+  DeleteUser(DeleteUser),
 }
 
 #[async_trait::async_trait(?Send)]
 impl GetCommunity for AnnouncableActivities {
+  #[tracing::instrument(skip(self, context))]
   async fn get_community(
     &self,
     context: &LemmyContext,
@@ -97,11 +118,57 @@ impl GetCommunity for AnnouncableActivities {
       Delete(a) => a.get_community(context, request_counter).await?,
       UndoDelete(a) => a.get_community(context, request_counter).await?,
       UpdateCommunity(a) => a.get_community(context, request_counter).await?,
-      BlockUserFromCommunity(a) => a.get_community(context, request_counter).await?,
-      UndoBlockUserFromCommunity(a) => a.get_community(context, request_counter).await?,
+      BlockUser(a) => a.get_community(context, request_counter).await?,
+      UndoBlockUser(a) => a.get_community(context, request_counter).await?,
       AddMod(a) => a.get_community(context, request_counter).await?,
       RemoveMod(a) => a.get_community(context, request_counter).await?,
+      Page(_) => unimplemented!(),
     };
     Ok(community)
   }
 }
+
+#[cfg(test)]
+mod tests {
+  use crate::{
+    activity_lists::{
+      GroupInboxActivities,
+      PersonInboxActivities,
+      PersonInboxActivitiesWithAnnouncable,
+      SiteInboxActivities,
+    },
+    protocol::tests::test_parse_lemmy_item,
+  };
+
+  #[test]
+  fn test_group_inbox() {
+    test_parse_lemmy_item::<GroupInboxActivities>("assets/lemmy/activities/following/follow.json")
+      .unwrap();
+    test_parse_lemmy_item::<GroupInboxActivities>(
+      "assets/lemmy/activities/create_or_update/create_note.json",
+    )
+    .unwrap();
+  }
+
+  #[test]
+  fn test_person_inbox() {
+    test_parse_lemmy_item::<PersonInboxActivities>("assets/lemmy/activities/following/accept.json")
+      .unwrap();
+    test_parse_lemmy_item::<PersonInboxActivitiesWithAnnouncable>(
+      "assets/lemmy/activities/create_or_update/create_note.json",
+    )
+    .unwrap();
+    test_parse_lemmy_item::<PersonInboxActivitiesWithAnnouncable>(
+      "assets/lemmy/activities/create_or_update/create_private_message.json",
+    )
+    .unwrap();
+  }
+
+  #[test]
+  fn test_site_inbox() {
+    test_parse_lemmy_item::<SiteInboxActivities>(
+      "assets/lemmy/activities/deletion/delete_user.json",
+    )
+    .unwrap();
+  }
+}