]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/community/collection_add.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / apub / src / activities / community / collection_add.rs
index b45fa278ee933185265d1f45848a3458702cbb75..d08b0cb486d8c67e069a57423fba92bce0340a70 100644 (file)
@@ -7,30 +7,25 @@ use crate::{
     verify_person_in_community,
   },
   activity_lists::AnnouncableActivities,
-  local_instance,
+  insert_activity,
   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
   protocol::{
-    activities::{
-      community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
-      create_or_update::page::CreateOrUpdatePage,
-      CreateOrUpdateType,
-    },
+    activities::community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
     InCommunity,
   },
-  ActorType,
   SendActivity,
 };
 use activitypub_federation::{
-  core::object_id::ObjectId,
-  data::Data,
+  config::Data,
+  fetch::object_id::ObjectId,
+  kinds::{activity::AddType, public},
   traits::{ActivityHandler, Actor},
 };
-use activitystreams_kinds::{activity::AddType, public};
 use lemmy_api_common::{
   community::{AddModToCommunity, AddModToCommunityResponse},
   context::LemmyContext,
   post::{FeaturePost, PostResponse},
-  utils::{generate_featured_url, generate_moderators_url, get_local_user_view_from_jwt},
+  utils::{generate_featured_url, generate_moderators_url, local_user_view_from_jwt},
 };
 use lemmy_db_schema::{
   impls::community::CollectionType,
@@ -51,21 +46,21 @@ impl CollectionAdd {
     community: &ApubCommunity,
     added_mod: &ApubPerson,
     actor: &ApubPerson,
-    context: &LemmyContext,
+    context: &Data<LemmyContext>,
   ) -> Result<(), LemmyError> {
     let id = generate_activity_id(
       AddType::Add,
       &context.settings().get_protocol_and_hostname(),
     )?;
     let add = CollectionAdd {
-      actor: ObjectId::new(actor.actor_id()),
+      actor: actor.id().into(),
       to: vec![public()],
-      object: added_mod.actor_id(),
+      object: added_mod.id(),
       target: generate_moderators_url(&community.actor_id)?.into(),
-      cc: vec![community.actor_id()],
+      cc: vec![community.id()],
       kind: AddType::Add,
       id: id.clone(),
-      audience: Some(ObjectId::new(community.actor_id())),
+      audience: Some(community.id().into()),
     };
 
     let activity = AnnouncableActivities::CollectionAdd(add);
@@ -77,28 +72,28 @@ impl CollectionAdd {
     community: &ApubCommunity,
     featured_post: &ApubPost,
     actor: &ApubPerson,
-    context: &LemmyContext,
+    context: &Data<LemmyContext>,
   ) -> Result<(), LemmyError> {
     let id = generate_activity_id(
       AddType::Add,
       &context.settings().get_protocol_and_hostname(),
     )?;
     let add = CollectionAdd {
-      actor: ObjectId::new(actor.actor_id()),
+      actor: actor.id().into(),
       to: vec![public()],
       object: featured_post.ap_id.clone().into(),
       target: generate_featured_url(&community.actor_id)?.into(),
-      cc: vec![community.actor_id()],
+      cc: vec![community.id()],
       kind: AddType::Add,
       id: id.clone(),
-      audience: Some(ObjectId::new(community.actor_id())),
+      audience: Some(community.id().into()),
     };
     let activity = AnnouncableActivities::CollectionAdd(add);
     send_activity_in_community(activity, actor, community, vec![], true, context).await
   }
 }
 
-#[async_trait::async_trait(?Send)]
+#[async_trait::async_trait]
 impl ActivityHandler for CollectionAdd {
   type DataType = LemmyContext;
   type Error = LemmyError;
@@ -112,95 +107,78 @@ impl ActivityHandler for CollectionAdd {
   }
 
   #[tracing::instrument(skip_all)]
-  async fn verify(
-    &self,
-    context: &Data<LemmyContext>,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
+  async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
     verify_is_public(&self.to, &self.cc)?;
-    let community = self.community(context, request_counter).await?;
-    verify_person_in_community(&self.actor, &community, context, request_counter).await?;
-    verify_mod_action(
-      &self.actor,
-      &self.object,
-      community.id,
-      context,
-      request_counter,
-    )
-    .await?;
+    let community = self.community(context).await?;
+    verify_person_in_community(&self.actor, &community, context).await?;
+    verify_mod_action(&self.actor, &self.object, community.id, context).await?;
     Ok(())
   }
 
   #[tracing::instrument(skip_all)]
-  async fn receive(
-    self,
-    context: &Data<LemmyContext>,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
+  async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
+    insert_activity(&self.id, &self, false, false, context).await?;
     let (community, collection_type) =
-      Community::get_by_collection_url(context.pool(), &self.target.into()).await?;
+      Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?;
     match collection_type {
       CollectionType::Moderators => {
-        let new_mod = ObjectId::<ApubPerson>::new(self.object)
-          .dereference(context, local_instance(context).await, request_counter)
+        let new_mod = ObjectId::<ApubPerson>::from(self.object)
+          .dereference(context)
           .await?;
 
         // If we had to refetch the community while parsing the activity, then the new mod has already
         // been added. Skip it here as it would result in a duplicate key error.
         let new_mod_id = new_mod.id;
         let moderated_communities =
-          CommunityModerator::get_person_moderated_communities(context.pool(), new_mod_id).await?;
+          CommunityModerator::get_person_moderated_communities(&mut context.pool(), new_mod_id)
+            .await?;
         if !moderated_communities.contains(&community.id) {
           let form = CommunityModeratorForm {
             community_id: community.id,
             person_id: new_mod.id,
           };
-          CommunityModerator::join(context.pool(), &form).await?;
+          CommunityModerator::join(&mut context.pool(), &form).await?;
 
           // write mod log
-          let actor = self
-            .actor
-            .dereference(context, local_instance(context).await, request_counter)
-            .await?;
+          let actor = self.actor.dereference(context).await?;
           let form = ModAddCommunityForm {
             mod_person_id: actor.id,
             other_person_id: new_mod.id,
             community_id: community.id,
             removed: Some(false),
           };
-          ModAddCommunity::create(context.pool(), &form).await?;
+          ModAddCommunity::create(&mut context.pool(), &form).await?;
         }
         // TODO: send websocket notification about added mod
       }
       CollectionType::Featured => {
-        let post = ObjectId::<ApubPost>::new(self.object)
-          .dereference(context, local_instance(context).await, request_counter)
+        let post = ObjectId::<ApubPost>::from(self.object)
+          .dereference(context)
           .await?;
         let form = PostUpdateForm::builder()
           .featured_community(Some(true))
           .build();
-        Post::update(context.pool(), post.id, &form).await?;
+        Post::update(&mut context.pool(), post.id, &form).await?;
       }
     }
     Ok(())
   }
 }
 
-#[async_trait::async_trait(?Send)]
+#[async_trait::async_trait]
 impl SendActivity for AddModToCommunity {
   type Response = AddModToCommunityResponse;
 
   async fn send_activity(
     request: &Self,
     _response: &Self::Response,
-    context: &LemmyContext,
+    context: &Data<LemmyContext>,
   ) -> Result<(), LemmyError> {
-    let local_user_view =
-      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
-    let community: ApubCommunity = Community::read(context.pool(), request.community_id)
+    let local_user_view = local_user_view_from_jwt(&request.auth, context).await?;
+    let community: ApubCommunity = Community::read(&mut context.pool(), request.community_id)
       .await?
       .into();
-    let updated_mod: ApubPerson = Person::read(context.pool(), request.person_id)
+    let updated_mod: ApubPerson = Person::read(&mut context.pool(), request.person_id)
       .await?
       .into();
     if request.added {
@@ -223,26 +201,17 @@ impl SendActivity for AddModToCommunity {
   }
 }
 
-#[async_trait::async_trait(?Send)]
+#[async_trait::async_trait]
 impl SendActivity for FeaturePost {
   type Response = PostResponse;
 
   async fn send_activity(
     request: &Self,
     response: &Self::Response,
-    context: &LemmyContext,
+    context: &Data<LemmyContext>,
   ) -> Result<(), LemmyError> {
-    let local_user_view =
-      get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
-    // Deprecated, for backwards compatibility with 0.17
-    CreateOrUpdatePage::send(
-      &response.post_view.post,
-      local_user_view.person.id,
-      CreateOrUpdateType::Update,
-      context,
-    )
-    .await?;
-    let community = Community::read(context.pool(), response.post_view.community.id)
+    let local_user_view = local_user_view_from_jwt(&request.auth, context).await?;
+    let community = Community::read(&mut context.pool(), response.post_view.community.id)
       .await?
       .into();
     let post = response.post_view.post.clone().into();