]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/collections/community_moderators.rs
Implement instance actor (#1798)
[lemmy.git] / crates / apub / src / collections / community_moderators.rs
index 3579ed38ff84e18680b4dbfd7509348680c65d8f..72d23d4acd5287d23a0d7e65ec5e0fbb87512987 100644 (file)
@@ -4,7 +4,8 @@ use crate::{
   objects::person::ApubPerson,
   protocol::collections::group_moderators::GroupModerators,
 };
-use activitystreams::{chrono::NaiveDateTime, collection::kind::OrderedCollectionType};
+use activitystreams_kinds::collection::OrderedCollectionType;
+use chrono::NaiveDateTime;
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
 use lemmy_db_schema::{
@@ -28,6 +29,7 @@ impl ApubObject for ApubCommunityModerators {
     None
   }
 
+  #[tracing::instrument(skip_all)]
   async fn read_from_apub_id(
     _object_id: Url,
     data: &Self::DataType,
@@ -45,15 +47,17 @@ impl ApubObject for ApubCommunityModerators {
     }
   }
 
+  #[tracing::instrument(skip_all)]
   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
     unimplemented!()
   }
 
-  async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
+  #[tracing::instrument(skip_all)]
+  async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
     let ordered_items = self
       .0
-      .iter()
-      .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id.clone()))
+      .into_iter()
+      .map(|m| ObjectId::<ApubPerson>::new(m.moderator.actor_id))
       .collect();
     Ok(GroupModerators {
       r#type: OrderedCollectionType::OrderedCollection,
@@ -66,13 +70,23 @@ impl ApubObject for ApubCommunityModerators {
     unimplemented!()
   }
 
+  #[tracing::instrument(skip_all)]
+  async fn verify(
+    group_moderators: &GroupModerators,
+    expected_domain: &Url,
+    _context: &CommunityContext,
+    _request_counter: &mut i32,
+  ) -> Result<(), LemmyError> {
+    verify_domains_match(&group_moderators.id, expected_domain)?;
+    Ok(())
+  }
+
+  #[tracing::instrument(skip_all)]
   async fn from_apub(
-    apub: &Self::ApubType,
+    apub: Self::ApubType,
     data: &Self::DataType,
-    expected_domain: &Url,
     request_counter: &mut i32,
   ) -> Result<Self, LemmyError> {
-    verify_domains_match(expected_domain, &apub.id)?;
     let community_id = data.0.id;
     let current_moderators = blocking(data.1.pool(), move |conn| {
       CommunityModeratorView::for_community(conn, community_id)
@@ -94,12 +108,13 @@ impl ApubObject for ApubCommunityModerators {
     }
 
     // Add new mods to database which have been added to moderators collection
-    for mod_id in &apub.ordered_items {
-      let mod_id = ObjectId::new(mod_id.clone());
-      let mod_user: ApubPerson = mod_id.dereference(&data.1, request_counter).await?;
+    for mod_id in apub.ordered_items {
+      let mod_id = ObjectId::new(mod_id);
+      let mod_user: ApubPerson = mod_id
+        .dereference(&data.1, data.1.client(), request_counter)
+        .await?;
 
       if !current_moderators
-        .clone()
         .iter()
         .map(|c| c.moderator.actor_id.clone())
         .any(|x| x == mod_user.actor_id)
@@ -128,10 +143,12 @@ mod tests {
     person::tests::parse_lemmy_person,
     tests::{file_to_json_object, init_context},
   };
+  use lemmy_apub_lib::activity_queue::create_activity_queue;
   use lemmy_db_schema::{
     source::{
       community::Community,
       person::{Person, PersonForm},
+      site::Site,
     },
     traits::Crud,
   };
@@ -140,7 +157,10 @@ mod tests {
   #[actix_rt::test]
   #[serial]
   async fn test_parse_lemmy_community_moderators() {
-    let context = init_context();
+    let client = reqwest::Client::new().into();
+    let manager = create_activity_queue(client);
+    let context = init_context(manager.queue_handle().clone());
+    let (new_mod, site) = parse_lemmy_person(&context).await;
     let community = parse_lemmy_community(&context).await;
     let community_id = community.id;
 
@@ -156,17 +176,20 @@ mod tests {
 
     CommunityModerator::join(&context.pool().get().unwrap(), &community_moderator_form).unwrap();
 
-    let new_mod = parse_lemmy_person(&context).await;
+    assert_eq!(site.actor_id.to_string(), "https://enterprise.lemmy.ml/");
 
     let json: GroupModerators =
-      file_to_json_object("assets/lemmy/collections/group_moderators.json");
+      file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
     let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
     let mut request_counter = 0;
     let community_context = CommunityContext {
       0: community,
       1: context,
     };
-    ApubCommunityModerators::from_apub(&json, &community_context, &url, &mut request_counter)
+    ApubCommunityModerators::verify(&json, &url, &community_context, &mut request_counter)
+      .await
+      .unwrap();
+    ApubCommunityModerators::from_apub(json, &community_context, &mut request_counter)
       .await
       .unwrap();
     assert_eq!(request_counter, 0);
@@ -188,5 +211,6 @@ mod tests {
       community_context.0.id,
     )
     .unwrap();
+    Site::delete(&*community_context.1.pool().get().unwrap(), site.id).unwrap();
   }
 }