]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/following/undo_follow.rs
Split activity table into sent and received parts (fixes #3103) (#3583)
[lemmy.git] / crates / apub / src / activities / following / undo_follow.rs
index 436d8a02e84711f7f5f162406bb8dde7e03a27d6..c36b36df8cb4c118464387c6690d687114d7ccdc 100644 (file)
@@ -1,18 +1,16 @@
 use crate::{
   activities::{generate_activity_id, send_lemmy_activity, verify_person},
   fetcher::user_or_community::UserOrCommunity,
-  local_instance,
+  insert_received_activity,
   objects::{community::ApubCommunity, person::ApubPerson},
   protocol::activities::following::{follow::Follow, undo_follow::UndoFollow},
-  ActorType,
 };
 use activitypub_federation::{
-  core::object_id::ObjectId,
-  data::Data,
+  config::Data,
+  kinds::activity::UndoType,
+  protocol::verification::verify_urls_match,
   traits::{ActivityHandler, Actor},
-  utils::verify_urls_match,
 };
-use activitystreams_kinds::activity::UndoType;
 use lemmy_api_common::context::LemmyContext;
 use lemmy_db_schema::{
   source::{
@@ -29,11 +27,12 @@ impl UndoFollow {
   pub async fn send(
     actor: &ApubPerson,
     community: &ApubCommunity,
-    context: &LemmyContext,
+    context: &Data<LemmyContext>,
   ) -> Result<(), LemmyError> {
     let object = Follow::new(actor, community, context)?;
     let undo = UndoFollow {
-      actor: ObjectId::new(actor.actor_id()),
+      actor: actor.id().into(),
+      to: Some([community.id().into()]),
       object,
       kind: UndoType::Undo,
       id: generate_activity_id(
@@ -46,7 +45,7 @@ impl UndoFollow {
   }
 }
 
-#[async_trait::async_trait(?Send)]
+#[async_trait::async_trait]
 impl ActivityHandler for UndoFollow {
   type DataType = LemmyContext;
   type Error = LemmyError;
@@ -60,32 +59,21 @@ impl ActivityHandler for UndoFollow {
   }
 
   #[tracing::instrument(skip_all)]
-  async fn verify(
-    &self,
-    context: &Data<LemmyContext>,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
+  async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
+    insert_received_activity(&self.id, context).await?;
     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
-    verify_person(&self.actor, context, request_counter).await?;
-    self.object.verify(context, request_counter).await?;
+    verify_person(&self.actor, context).await?;
+    self.object.verify(context).await?;
+    if let Some(to) = &self.to {
+      verify_urls_match(to[0].inner(), self.object.object.inner())?;
+    }
     Ok(())
   }
 
   #[tracing::instrument(skip_all)]
-  async fn receive(
-    self,
-    context: &Data<LemmyContext>,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
-    let person = self
-      .actor
-      .dereference(context, local_instance(context).await, request_counter)
-      .await?;
-    let object = self
-      .object
-      .object
-      .dereference(context, local_instance(context).await, request_counter)
-      .await?;
+  async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
+    let person = self.actor.dereference(context).await?;
+    let object = self.object.object.dereference(context).await?;
 
     match object {
       UserOrCommunity::User(u) => {
@@ -94,7 +82,7 @@ impl ActivityHandler for UndoFollow {
           follower_id: person.id,
           pending: false,
         };
-        PersonFollower::unfollow(context.pool(), &form).await?;
+        PersonFollower::unfollow(&mut context.pool(), &form).await?;
       }
       UserOrCommunity::Community(c) => {
         let form = CommunityFollowerForm {
@@ -102,7 +90,7 @@ impl ActivityHandler for UndoFollow {
           person_id: person.id,
           pending: false,
         };
-        CommunityFollower::unfollow(context.pool(), &form).await?;
+        CommunityFollower::unfollow(&mut context.pool(), &form).await?;
       }
     }