]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/community/announce.rs
Rewrite fetcher (#1792)
[lemmy.git] / crates / apub / src / activities / community / announce.rs
index 301ccc64840574e0bcce1a9e839b96677cfa7700..96797c8ffbbbee4439b6682ad9409b101c9cb5ff 100644 (file)
@@ -1,69 +1,96 @@
 use crate::{
   activities::{
-    comment::{create::CreateComment, update::UpdateComment},
+    comment::create_or_update::CreateOrUpdateComment,
     community::{
       add_mod::AddMod,
       block_user::BlockUserFromCommunity,
+      list_community_follower_inboxes,
+      remove_mod::RemoveMod,
       undo_block_user::UndoBlockUserFromCommunity,
+      update::UpdateCommunity,
     },
-    deletion::{
-      delete::DeletePostCommentOrCommunity,
-      undo_delete::UndoDeletePostCommentOrCommunity,
-    },
-    post::{create::CreatePost, update::UpdatePost},
-    removal::{
-      remove::RemovePostCommentCommunityOrMod,
-      undo_remove::UndoRemovePostCommentOrCommunity,
-    },
+    deletion::{delete::Delete, undo_delete::UndoDelete},
+    generate_activity_id,
+    post::create_or_update::CreateOrUpdatePost,
+    undo_remove::UndoRemovePostCommentOrCommunity,
     verify_activity,
     verify_community,
-    voting::{
-      dislike::DislikePostOrComment,
-      like::LikePostOrComment,
-      undo_dislike::UndoDislikePostOrComment,
-      undo_like::UndoLikePostOrComment,
-    },
+    voting::{undo_vote::UndoVote, vote::Vote},
   },
+  activity_queue::send_activity_new,
+  extensions::context::lemmy_context,
+  fetcher::object_id::ObjectId,
   http::is_activity_already_known,
   insert_activity,
+  ActorType,
+  CommunityType,
 };
-use activitystreams::activity::kind::AnnounceType;
-use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
+use activitystreams::{
+  activity::kind::AnnounceType,
+  base::AnyBase,
+  primitives::OneOrMany,
+  unparsed::Unparsed,
+};
+use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
+use lemmy_db_schema::source::community::Community;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use serde::{Deserialize, Serialize};
 use url::Url;
 
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
+#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
 #[serde(untagged)]
 pub enum AnnouncableActivities {
-  CreateComment(CreateComment),
-  UpdateComment(UpdateComment),
-  CreatePost(CreatePost),
-  UpdatePost(UpdatePost),
-  LikePostOrComment(LikePostOrComment),
-  DislikePostOrComment(DislikePostOrComment),
-  UndoLikePostOrComment(UndoLikePostOrComment),
-  UndoDislikePostOrComment(UndoDislikePostOrComment),
-  DeletePostCommentOrCommunity(DeletePostCommentOrCommunity),
-  UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity),
-  RemovePostCommentCommunityOrMod(RemovePostCommentCommunityOrMod),
+  CreateOrUpdateComment(CreateOrUpdateComment),
+  CreateOrUpdatePost(Box<CreateOrUpdatePost>),
+  Vote(Vote),
+  UndoVote(UndoVote),
+  Delete(Delete),
+  UndoDelete(UndoDelete),
   UndoRemovePostCommentOrCommunity(UndoRemovePostCommentOrCommunity),
+  UpdateCommunity(Box<UpdateCommunity>),
   BlockUserFromCommunity(BlockUserFromCommunity),
   UndoBlockUserFromCommunity(UndoBlockUserFromCommunity),
   AddMod(AddMod),
+  RemoveMod(RemoveMod),
 }
 
-#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
+#[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
 #[serde(rename_all = "camelCase")]
 pub struct AnnounceActivity {
-  to: PublicUrl,
+  actor: ObjectId<Community>,
+  to: [PublicUrl; 1],
   object: AnnouncableActivities,
   cc: Vec<Url>,
   #[serde(rename = "type")]
   kind: AnnounceType,
+  id: Url,
+  #[serde(rename = "@context")]
+  context: OneOrMany<AnyBase>,
   #[serde(flatten)]
-  common: ActivityCommonFields,
+  unparsed: Unparsed,
+}
+
+impl AnnounceActivity {
+  pub async fn send(
+    object: AnnouncableActivities,
+    community: &Community,
+    additional_inboxes: Vec<Url>,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let announce = AnnounceActivity {
+      actor: ObjectId::new(community.actor_id()),
+      to: [PublicUrl::Public],
+      object,
+      cc: vec![community.followers_url()],
+      kind: AnnounceType::Announce,
+      id: generate_activity_id(&AnnounceType::Announce)?,
+      context: lemmy_context(),
+      unparsed: Default::default(),
+    };
+    let inboxes = list_community_follower_inboxes(community, additional_inboxes, context).await?;
+    send_activity_new(context, &announce, &announce.id, community, inboxes, false).await
+  }
 }
 
 #[async_trait::async_trait(?Send)]
@@ -73,22 +100,22 @@ impl ActivityHandler for AnnounceActivity {
     context: &LemmyContext,
     request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
-    verify_activity(self.common())?;
-    verify_community(&self.common.actor, context, request_counter).await?;
+    verify_activity(self)?;
+    verify_community(&self.actor, context, request_counter).await?;
     self.object.verify(context, request_counter).await?;
     Ok(())
   }
 
   async fn receive(
-    &self,
+    self,
     context: &LemmyContext,
     request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
-    if is_activity_already_known(context.pool(), self.object.common().id_unchecked()).await? {
+    if is_activity_already_known(context.pool(), self.object.id_unchecked()).await? {
       return Ok(());
     }
     insert_activity(
-      self.object.common().id_unchecked(),
+      self.object.id_unchecked(),
       self.object.clone(),
       false,
       true,
@@ -97,8 +124,4 @@ impl ActivityHandler for AnnounceActivity {
     .await?;
     self.object.receive(context, request_counter).await
   }
-
-  fn common(&self) -> &ActivityCommonFields {
-    &self.common
-  }
 }