]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/deletion/mod.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / apub / src / activities / deletion / mod.rs
index 123e87fbd551565462cfce13b905d469022f5129..c17dcbfc2c41be13a5253e2287339cfafa473089 100644 (file)
@@ -1,6 +1,6 @@
 use crate::{
   activities::{
-    community::{announce::GetCommunity, send_activity_in_community},
+    community::send_activity_in_community,
     send_lemmy_activity,
     verify_is_public,
     verify_mod_action,
@@ -8,6 +8,7 @@ use crate::{
     verify_person_in_community,
   },
   activity_lists::AnnouncableActivities,
+  local_instance,
   objects::{
     comment::ApubComment,
     community::ApubCommunity,
@@ -15,36 +16,47 @@ use crate::{
     post::ApubPost,
     private_message::ApubPrivateMessage,
   },
-  protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
+  protocol::{
+    activities::deletion::{delete::Delete, undo_delete::UndoDelete},
+    InCommunity,
+  },
+  ActorType,
+  SendActivity,
+};
+use activitypub_federation::{
+  core::object_id::ObjectId,
+  traits::{Actor, ApubObject},
+  utils::verify_domains_match,
 };
 use activitystreams_kinds::public;
-use lemmy_api_common::utils::blocking;
-use lemmy_apub_lib::{
-  object_id::ObjectId,
-  traits::{ActorType, ApubObject},
-  verify::verify_domains_match,
+use lemmy_api_common::{
+  comment::{CommentResponse, DeleteComment, RemoveComment},
+  community::{CommunityResponse, DeleteCommunity, RemoveCommunity},
+  context::LemmyContext,
+  post::{DeletePost, PostResponse, RemovePost},
+  private_message::{DeletePrivateMessage, PrivateMessageResponse},
+  utils::get_local_user_view_from_jwt,
+  websocket::{
+    send::{
+      send_comment_ws_message_simple,
+      send_community_ws_message,
+      send_pm_ws_message,
+      send_post_ws_message,
+    },
+    UserOperationCrud,
+  },
 };
 use lemmy_db_schema::{
   source::{
-    comment::Comment,
-    community::Community,
+    comment::{Comment, CommentUpdateForm},
+    community::{Community, CommunityUpdateForm},
     person::Person,
-    post::Post,
-    private_message::PrivateMessage,
+    post::{Post, PostUpdateForm},
+    private_message::{PrivateMessage, PrivateMessageUpdateForm},
   },
   traits::Crud,
 };
-use lemmy_utils::LemmyError;
-use lemmy_websocket::{
-  send::{
-    send_comment_ws_message_simple,
-    send_community_ws_message,
-    send_pm_ws_message,
-    send_post_ws_message,
-  },
-  LemmyContext,
-  UserOperationCrud,
-};
+use lemmy_utils::error::LemmyError;
 use std::ops::Deref;
 use url::Url;
 
@@ -52,10 +64,175 @@ pub mod delete;
 pub mod delete_user;
 pub mod undo_delete;
 
+#[async_trait::async_trait(?Send)]
+impl SendActivity for DeletePost {
+  type Response = PostResponse;
+
+  async fn send_activity(
+    request: &Self,
+    response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    let community = Community::read(context.pool(), response.post_view.community.id).await?;
+    let deletable = DeletableObjects::Post(response.post_view.post.clone().into());
+    send_apub_delete_in_community(
+      local_user_view.person,
+      community,
+      deletable,
+      None,
+      request.deleted,
+      context,
+    )
+    .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for RemovePost {
+  type Response = PostResponse;
+
+  async fn send_activity(
+    request: &Self,
+    response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    let community = Community::read(context.pool(), response.post_view.community.id).await?;
+    let deletable = DeletableObjects::Post(response.post_view.post.clone().into());
+    send_apub_delete_in_community(
+      local_user_view.person,
+      community,
+      deletable,
+      request.reason.clone().or_else(|| Some(String::new())),
+      request.removed,
+      context,
+    )
+    .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for DeleteComment {
+  type Response = CommentResponse;
+
+  async fn send_activity(
+    request: &Self,
+    response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let community_id = response.comment_view.community.id;
+    let community = Community::read(context.pool(), community_id).await?;
+    let person = Person::read(context.pool(), response.comment_view.creator.id).await?;
+    let deletable = DeletableObjects::Comment(response.comment_view.comment.clone().into());
+    send_apub_delete_in_community(person, community, deletable, None, request.deleted, context)
+      .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for RemoveComment {
+  type Response = CommentResponse;
+
+  async fn send_activity(
+    request: &Self,
+    response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    let comment = Comment::read(context.pool(), request.comment_id).await?;
+    let community = Community::read(context.pool(), response.comment_view.community.id).await?;
+    let deletable = DeletableObjects::Comment(comment.into());
+    send_apub_delete_in_community(
+      local_user_view.person,
+      community,
+      deletable,
+      request.reason.clone().or_else(|| Some(String::new())),
+      request.removed,
+      context,
+    )
+    .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for DeletePrivateMessage {
+  type Response = PrivateMessageResponse;
+
+  async fn send_activity(
+    request: &Self,
+    response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    send_apub_delete_private_message(
+      &local_user_view.person.into(),
+      response.private_message_view.private_message.clone(),
+      request.deleted,
+      context,
+    )
+    .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for DeleteCommunity {
+  type Response = CommunityResponse;
+
+  async fn send_activity(
+    request: &Self,
+    _response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    let community = Community::read(context.pool(), request.community_id).await?;
+    let deletable = DeletableObjects::Community(community.clone().into());
+    send_apub_delete_in_community(
+      local_user_view.person,
+      community,
+      deletable,
+      None,
+      request.deleted,
+      context,
+    )
+    .await
+  }
+}
+
+#[async_trait::async_trait(?Send)]
+impl SendActivity for RemoveCommunity {
+  type Response = CommunityResponse;
+
+  async fn send_activity(
+    request: &Self,
+    _response: &Self::Response,
+    context: &LemmyContext,
+  ) -> Result<(), LemmyError> {
+    let local_user_view =
+      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
+    let community = Community::read(context.pool(), request.community_id).await?;
+    let deletable = DeletableObjects::Community(community.clone().into());
+    send_apub_delete_in_community(
+      local_user_view.person,
+      community,
+      deletable,
+      request.reason.clone().or_else(|| Some(String::new())),
+      request.removed,
+      context,
+    )
+    .await
+  }
+}
+
 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
 /// action was done by a normal user.
 #[tracing::instrument(skip_all)]
-pub async fn send_apub_delete_in_community(
+async fn send_apub_delete_in_community(
   actor: Person,
   community: Community,
   object: DeletableObjects,
@@ -63,56 +240,53 @@ pub async fn send_apub_delete_in_community(
   deleted: bool,
   context: &LemmyContext,
 ) -> Result<(), LemmyError> {
-  let (id, activity) = if deleted {
+  let actor = ApubPerson::from(actor);
+  let is_mod_action = reason.is_some();
+  let activity = if deleted {
     let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
-    (delete.id.clone(), AnnouncableActivities::Delete(delete))
+    AnnouncableActivities::Delete(delete)
   } else {
     let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
-    (undo.id.clone(), AnnouncableActivities::UndoDelete(undo))
+    AnnouncableActivities::UndoDelete(undo)
   };
   send_activity_in_community(
     activity,
-    &id,
-    &ApubPerson::from(actor),
+    &actor,
     &community.into(),
     vec![],
+    is_mod_action,
     context,
   )
   .await
 }
 
 #[tracing::instrument(skip_all)]
-pub async fn send_apub_delete_private_message(
+async fn send_apub_delete_private_message(
   actor: &ApubPerson,
   pm: PrivateMessage,
   deleted: bool,
   context: &LemmyContext,
 ) -> Result<(), LemmyError> {
   let recipient_id = pm.recipient_id;
-  let recipient: ApubPerson =
-    blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
-      .await??
-      .into();
+  let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into();
 
-  let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
-  let inbox = vec![recipient.shared_inbox_or_inbox_url()];
+  let deletable = DeletableObjects::PrivateMessage(pm.into());
+  let inbox = vec![recipient.shared_inbox_or_inbox()];
   if deleted {
     let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
-    let id = delete.id.clone();
-    send_lemmy_activity(context, &delete, &id, actor, inbox, true).await?;
+    send_lemmy_activity(context, delete, actor, inbox, true).await?;
   } else {
     let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
-    let id = undo.id.clone();
-    send_lemmy_activity(context, &undo, &id, actor, inbox, true).await?;
+    send_lemmy_activity(context, undo, actor, inbox, true).await?;
   };
   Ok(())
 }
 
 pub enum DeletableObjects {
-  Community(Box<ApubCommunity>),
-  Comment(Box<ApubComment>),
-  Post(Box<ApubPost>),
-  PrivateMessage(Box<ApubPrivateMessage>),
+  Community(ApubCommunity),
+  Comment(ApubComment),
+  Post(ApubPost),
+  PrivateMessage(ApubPrivateMessage),
 }
 
 impl DeletableObjects {
@@ -122,16 +296,16 @@ impl DeletableObjects {
     context: &LemmyContext,
   ) -> Result<DeletableObjects, LemmyError> {
     if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
-      return Ok(DeletableObjects::Community(Box::new(c)));
+      return Ok(DeletableObjects::Community(c));
     }
     if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
-      return Ok(DeletableObjects::Post(Box::new(p)));
+      return Ok(DeletableObjects::Post(p));
     }
     if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
-      return Ok(DeletableObjects::Comment(Box::new(c)));
+      return Ok(DeletableObjects::Comment(c));
     }
     if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
-      return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
+      return Ok(DeletableObjects::PrivateMessage(p));
     }
     Err(diesel::NotFound.into())
   }
@@ -166,7 +340,7 @@ pub(in crate::activities) async fn verify_delete_activity(
       verify_mod_action(
         &activity.actor,
         activity.object.id(),
-        &community,
+        community.id,
         context,
         request_counter,
       )
@@ -177,7 +351,7 @@ pub(in crate::activities) async fn verify_delete_activity(
       verify_delete_post_or_comment(
         &activity.actor,
         &p.ap_id.clone().into(),
-        &activity.get_community(context, request_counter).await?,
+        &activity.community(context, request_counter).await?,
         is_mod_action,
         context,
         request_counter,
@@ -189,7 +363,7 @@ pub(in crate::activities) async fn verify_delete_activity(
       verify_delete_post_or_comment(
         &activity.actor,
         &c.ap_id.clone().into(),
-        &activity.get_community(context, request_counter).await?,
+        &activity.community(context, request_counter).await?,
         is_mod_action,
         context,
         request_counter,
@@ -215,7 +389,7 @@ async fn verify_delete_post_or_comment(
 ) -> Result<(), LemmyError> {
   verify_person_in_community(actor, community, context, request_counter).await?;
   if is_mod_action {
-    verify_mod_action(actor, object_id, community, context, request_counter).await?;
+    verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
   } else {
     // domain of post ap_id and post.creator ap_id are identical, so we just check the former
     verify_domains_match(actor.inner(), object_id)?;
@@ -236,7 +410,7 @@ async fn receive_delete_action(
     DeletableObjects::Community(community) => {
       if community.local {
         let mod_: Person = actor
-          .dereference(context, context.client(), request_counter)
+          .dereference(context, local_instance(context).await, request_counter)
           .await?
           .deref()
           .clone();
@@ -245,10 +419,14 @@ async fn receive_delete_action(
         send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
       }
 
-      let community = blocking(context.pool(), move |conn| {
-        Community::update_deleted(conn, community.id, deleted)
-      })
-      .await??;
+      let community = Community::update(
+        context.pool(),
+        community.id,
+        &CommunityUpdateForm::builder()
+          .deleted(Some(deleted))
+          .build(),
+      )
+      .await?;
       send_community_ws_message(
         community.id,
         UserOperationCrud::DeleteCommunity,
@@ -260,10 +438,12 @@ async fn receive_delete_action(
     }
     DeletableObjects::Post(post) => {
       if deleted != post.deleted {
-        let deleted_post = blocking(context.pool(), move |conn| {
-          Post::update_deleted(conn, post.id, deleted)
-        })
-        .await??;
+        let deleted_post = Post::update(
+          context.pool(),
+          post.id,
+          &PostUpdateForm::builder().deleted(Some(deleted)).build(),
+        )
+        .await?;
         send_post_ws_message(
           deleted_post.id,
           UserOperationCrud::DeletePost,
@@ -276,10 +456,12 @@ async fn receive_delete_action(
     }
     DeletableObjects::Comment(comment) => {
       if deleted != comment.deleted {
-        let deleted_comment = blocking(context.pool(), move |conn| {
-          Comment::update_deleted(conn, comment.id, deleted)
-        })
-        .await??;
+        let deleted_comment = Comment::update(
+          context.pool(),
+          comment.id,
+          &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
+        )
+        .await?;
         send_comment_ws_message_simple(
           deleted_comment.id,
           UserOperationCrud::DeleteComment,
@@ -289,10 +471,14 @@ async fn receive_delete_action(
       }
     }
     DeletableObjects::PrivateMessage(pm) => {
-      let deleted_private_message = blocking(context.pool(), move |conn| {
-        PrivateMessage::update_deleted(conn, pm.id, deleted)
-      })
-      .await??;
+      let deleted_private_message = PrivateMessage::update(
+        context.pool(),
+        pm.id,
+        &PrivateMessageUpdateForm::builder()
+          .deleted(Some(deleted))
+          .build(),
+      )
+      .await?;
 
       send_pm_ws_message(
         deleted_private_message.id,