]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/objects/community.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / apub / src / objects / community.rs
index 6526d2d26eff08f05b9dad3bf3930733bf593714..75eb941b1b3af0589cf29782277e4ef6e388c4c9 100644 (file)
@@ -1,6 +1,6 @@
 use crate::{
-  check_apub_id_valid_with_strictness,
-  fetch_local_site_data,
+  check_apub_id_valid,
+  local_site_data_cached,
   objects::instance::fetch_instance_actor_for_object,
   protocol::{
     objects::{group::Group, Endpoints, LanguageTag},
@@ -14,7 +14,6 @@ use activitypub_federation::{
   traits::{Actor, Object},
 };
 use chrono::NaiveDateTime;
-use itertools::Itertools;
 use lemmy_api_common::{
   context::LemmyContext,
   utils::{generate_featured_url, generate_moderators_url, generate_outbox_url},
@@ -67,7 +66,7 @@ impl Object for ApubCommunity {
     context: &Data<Self::DataType>,
   ) -> Result<Option<Self>, LemmyError> {
     Ok(
-      Community::read_from_apub_id(context.pool(), &object_id.into())
+      Community::read_from_apub_id(&mut context.pool(), &object_id.into())
         .await?
         .map(Into::into),
     )
@@ -76,15 +75,15 @@ impl Object for ApubCommunity {
   #[tracing::instrument(skip_all)]
   async fn delete(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
     let form = CommunityUpdateForm::builder().deleted(Some(true)).build();
-    Community::update(context.pool(), self.id, &form).await?;
+    Community::update(&mut context.pool(), self.id, &form).await?;
     Ok(())
   }
 
   #[tracing::instrument(skip_all)]
   async fn into_json(self, data: &Data<Self::DataType>) -> Result<Group, LemmyError> {
     let community_id = self.id;
-    let langs = CommunityLanguage::read(data.pool(), community_id).await?;
-    let language = LanguageTag::new_multiple(langs, data.pool()).await?;
+    let langs = CommunityLanguage::read(&mut data.pool(), community_id).await?;
+    let language = LanguageTag::new_multiple(langs, &mut data.pool()).await?;
 
     let group = Group {
       kind: GroupType::Group,
@@ -131,28 +130,26 @@ impl Object for ApubCommunity {
     let instance_id = fetch_instance_actor_for_object(&group.id, context).await?;
 
     let form = Group::into_insert_form(group.clone(), instance_id);
-    let languages = LanguageTag::to_language_id_multiple(group.language, context.pool()).await?;
+    let languages =
+      LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?;
 
-    let community = Community::create(context.pool(), &form).await?;
-    CommunityLanguage::update(context.pool(), languages, community.id).await?;
+    let community = Community::create(&mut context.pool(), &form).await?;
+    CommunityLanguage::update(&mut context.pool(), languages, community.id).await?;
 
     let community: ApubCommunity = community.into();
 
     // 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.
-    group
-      .outbox
-      .dereference(&community, context)
-      .await
-      .map_err(|e| debug!("{}", e))
-      .ok();
+    let fetch_outbox = group.outbox.dereference(&community, context);
 
     if let Some(moderators) = group.attributed_to {
-      moderators
-        .dereference(&community, context)
-        .await
-        .map_err(|e| debug!("{}", e))
-        .ok();
+      let fetch_moderators = moderators.dereference(&community, context);
+      // Fetch mods and outbox in parallel
+      let res = tokio::join!(fetch_outbox, fetch_moderators);
+      res.0.map_err(|e| debug!("{}", e)).ok();
+      res.1.map_err(|e| debug!("{}", e)).ok();
+    } else {
+      fetch_outbox.await.map_err(|e| debug!("{}", e)).ok();
     }
 
     Ok(community)
@@ -190,24 +187,15 @@ impl ApubCommunity {
   ) -> Result<Vec<Url>, LemmyError> {
     let id = self.id;
 
-    let local_site_data = fetch_local_site_data(context.pool()).await?;
-    let follows = CommunityFollowerView::for_community(context.pool(), id).await?;
+    let local_site_data = local_site_data_cached(&mut context.pool()).await?;
+    let follows =
+      CommunityFollowerView::get_community_follower_inboxes(&mut context.pool(), id).await?;
     let inboxes: Vec<Url> = follows
       .into_iter()
-      .filter(|f| !f.follower.local)
-      .map(|f| {
-        f.follower
-          .shared_inbox_url
-          .unwrap_or(f.follower.inbox_url)
-          .into()
-      })
-      .unique()
+      .map(Into::into)
       .filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname))
       // Don't send to blocked instances
-      .filter(|inbox| {
-        check_apub_id_valid_with_strictness(inbox, false, &local_site_data, context.settings())
-          .is_ok()
-      })
+      .filter(|inbox| check_apub_id_valid(inbox, &local_site_data).is_ok())
       .collect();
 
     Ok(inboxes)
@@ -216,6 +204,9 @@ impl ApubCommunity {
 
 #[cfg(test)]
 pub(crate) mod tests {
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
   use super::*;
   use crate::{
     objects::{instance::tests::parse_lemmy_instance, tests::init_context},
@@ -242,7 +233,7 @@ pub(crate) mod tests {
     community
   }
 
-  #[actix_rt::test]
+  #[tokio::test]
   #[serial]
   async fn test_parse_lemmy_community() {
     let context = init_context().await;
@@ -253,9 +244,9 @@ pub(crate) mod tests {
     assert!(!community.local);
     assert_eq!(community.description.as_ref().unwrap().len(), 132);
 
-    Community::delete(context.pool(), community.id)
+    Community::delete(&mut context.pool(), community.id)
       .await
       .unwrap();
-    Site::delete(context.pool(), site.id).await.unwrap();
+    Site::delete(&mut context.pool(), site.id).await.unwrap();
   }
 }