]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/objects/community.rs
Making public key required. Fixes #1934
[lemmy.git] / crates / apub / src / objects / community.rs
index 851563ab841eba2752941a38351cc2ee15ba92a0..300ad2f2c093cfdfb7be2222404ca1a6ffe38777 100644 (file)
@@ -4,15 +4,12 @@ use crate::{
   generate_moderators_url,
   generate_outbox_url,
   protocol::{
-    objects::{group::Group, tombstone::Tombstone},
+    objects::{group::Group, tombstone::Tombstone, Endpoints},
     ImageObject,
     Source,
   },
 };
-use activitystreams::{
-  actor::{kind::GroupType, Endpoints},
-  object::kind::ImageType,
-};
+use activitystreams_kinds::actor::GroupType;
 use chrono::NaiveDateTime;
 use itertools::Itertools;
 use lemmy_api_common::blocking;
@@ -79,19 +76,13 @@ impl ApubObject for ApubCommunity {
     Ok(())
   }
 
-  async fn to_apub(&self, _context: &LemmyContext) -> Result<Group, LemmyError> {
+  async fn into_apub(self, _context: &LemmyContext) -> Result<Group, LemmyError> {
     let source = self.description.clone().map(|bio| Source {
       content: bio,
       media_type: MediaTypeMarkdown::Markdown,
     });
-    let icon = self.icon.clone().map(|url| ImageObject {
-      kind: ImageType::Image,
-      url: url.into(),
-    });
-    let image = self.banner.clone().map(|url| ImageObject {
-      kind: ImageType::Image,
-      url: url.into(),
-    });
+    let icon = self.icon.clone().map(ImageObject::new);
+    let image = self.banner.clone().map(ImageObject::new);
 
     let group = Group {
       kind: GroupType::Group,
@@ -111,7 +102,6 @@ impl ApubObject for ApubCommunity {
       followers: self.followers_url.clone().into(),
       endpoints: Endpoints {
         shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
-        ..Default::default()
       },
       public_key: self.get_public_key()?,
       published: Some(convert_datetime(self.published)),
@@ -122,20 +112,25 @@ impl ApubObject for ApubCommunity {
   }
 
   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
-    Ok(Tombstone::new(
-      GroupType::Group,
-      self.updated.unwrap_or(self.published),
-    ))
+    Ok(Tombstone::new(self.actor_id()))
+  }
+
+  async fn verify(
+    group: &Group,
+    expected_domain: &Url,
+    context: &LemmyContext,
+    _request_counter: &mut i32,
+  ) -> Result<(), LemmyError> {
+    group.verify(expected_domain, context).await
   }
 
   /// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
   async fn from_apub(
-    group: &Group,
+    group: Group,
     context: &LemmyContext,
-    expected_domain: &Url,
     request_counter: &mut i32,
   ) -> Result<ApubCommunity, LemmyError> {
-    let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
+    let form = Group::into_form(group.clone());
 
     // Fetching mods and outbox is not necessary for Lemmy to work, so ignore errors. Besides,
     // we need to ignore these errors so that tests can work entirely offline.
@@ -165,16 +160,10 @@ impl ApubObject for ApubCommunity {
 }
 
 impl ActorType for ApubCommunity {
-  fn is_local(&self) -> bool {
-    self.local
-  }
   fn actor_id(&self) -> Url {
     self.actor_id.to_owned().into()
   }
-  fn name(&self) -> String {
-    self.name.clone()
-  }
-  fn public_key(&self) -> Option<String> {
+  fn public_key(&self) -> String {
     self.public_key.to_owned()
   }
   fn private_key(&self) -> Option<String> {
@@ -194,7 +183,6 @@ impl ApubCommunity {
   /// For a given community, returns the inboxes of all followers.
   pub(crate) async fn get_follower_inboxes(
     &self,
-    additional_inboxes: Vec<Url>,
     context: &LemmyContext,
   ) -> Result<Vec<Url>, LemmyError> {
     let id = self.id;
@@ -203,7 +191,7 @@ impl ApubCommunity {
       CommunityFollowerView::for_community(conn, id)
     })
     .await??;
-    let follower_inboxes: Vec<Url> = follows
+    let inboxes: Vec<Url> = follows
       .into_iter()
       .filter(|f| !f.follower.local)
       .map(|f| {
@@ -212,12 +200,8 @@ impl ApubCommunity {
           .unwrap_or(f.follower.inbox_url)
           .into()
       })
-      .collect();
-    let inboxes = vec![follower_inboxes, additional_inboxes]
-      .into_iter()
-      .flatten()
       .unique()
-      .filter(|inbox| inbox.host_str() != Some(&context.settings().hostname))
+      .filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname))
       // Don't send to blocked instances
       .filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok())
       .collect();
@@ -242,7 +226,10 @@ pub(crate) mod tests {
 
     let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
     let mut request_counter = 0;
-    let community = ApubCommunity::from_apub(&json, context, &url, &mut request_counter)
+    ApubCommunity::verify(&json, &url, context, &mut request_counter)
+      .await
+      .unwrap();
+    let community = ApubCommunity::from_apub(json, context, &mut request_counter)
       .await
       .unwrap();
     // this makes two requests to the (intentionally) broken outbox/moderators collections
@@ -257,7 +244,6 @@ pub(crate) mod tests {
     let community = parse_lemmy_community(&context).await;
 
     assert_eq!(community.title, "Ten Forward");
-    assert!(community.public_key.is_some());
     assert!(!community.local);
     assert_eq!(community.description.as_ref().unwrap().len(), 132);