]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/collections/community_outbox.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / apub / src / collections / community_outbox.rs
index dde98859aedb78c9a35f86772d522a16a6868a51..49140aab8d7a29887ff9b9815b9a518ba3517cad 100644 (file)
@@ -1,10 +1,13 @@
 use crate::{
   activity_lists::AnnouncableActivities,
   collections::CommunityContext,
-  generate_outbox_url,
   objects::post::ApubPost,
   protocol::{
-    activities::community::announce::AnnounceActivity,
+    activities::{
+      community::announce::AnnounceActivity,
+      create_or_update::page::CreateOrUpdatePage,
+      CreateOrUpdateType,
+    },
     collections::group_outbox::GroupOutbox,
   },
 };
@@ -16,8 +19,12 @@ use activitypub_federation::{
 use activitystreams_kinds::collection::OrderedCollectionType;
 use chrono::NaiveDateTime;
 use futures::future::join_all;
-use lemmy_api_common::utils::blocking;
-use lemmy_db_schema::source::post::Post;
+use lemmy_api_common::utils::generate_outbox_url;
+use lemmy_db_schema::{
+  source::{person::Person, post::Post},
+  traits::Crud,
+  utils::FETCH_LIMIT_MAX,
+};
 use lemmy_utils::error::LemmyError;
 use url::Url;
 
@@ -29,6 +36,7 @@ impl ApubObject for ApubCommunityOutbox {
   type DataType = CommunityContext;
   type ApubType = GroupOutbox;
   type Error = LemmyError;
+  type DbType = ();
 
   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
     None
@@ -42,31 +50,27 @@ impl ApubObject for ApubCommunityOutbox {
     // Only read from database if its a local community, otherwise fetch over http
     if data.0.local {
       let community_id = data.0.id;
-      let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
-        Post::list_for_community(conn, community_id)
-      })
-      .await??
-      .into_iter()
-      .map(Into::into)
-      .collect();
+      let post_list: Vec<ApubPost> = Post::list_for_community(data.1.pool(), community_id)
+        .await?
+        .into_iter()
+        .map(Into::into)
+        .collect();
       Ok(Some(ApubCommunityOutbox(post_list)))
     } else {
       Ok(None)
     }
   }
 
-  async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
-    // do nothing (it gets deleted automatically with the community)
-    Ok(())
-  }
-
   #[tracing::instrument(skip_all)]
   async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
     let mut ordered_items = vec![];
     for post in self.0 {
-      let page = post.into_apub(&data.1).await?;
-      let announcable = AnnouncableActivities::Page(page);
-      let announce = AnnounceActivity::new(announcable, &data.0, &data.1)?;
+      let person = Person::read(data.1.pool(), post.creator_id).await?.into();
+      let create =
+        CreateOrUpdatePage::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1)
+          .await?;
+      let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
+      let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?;
       ordered_items.push(announce);
     }
 
@@ -96,8 +100,8 @@ impl ApubObject for ApubCommunityOutbox {
     _request_counter: &mut i32,
   ) -> Result<Self, LemmyError> {
     let mut outbox_activities = apub.ordered_items;
-    if outbox_activities.len() > 20 {
-      outbox_activities = outbox_activities[0..20].to_vec();
+    if outbox_activities.len() as i64 > FETCH_LIMIT_MAX {
+      outbox_activities = outbox_activities[0..(FETCH_LIMIT_MAX as usize)].to_vec();
     }
 
     // We intentionally ignore errors here. This is because the outbox might contain posts from old
@@ -121,6 +125,4 @@ impl ApubObject for ApubCommunityOutbox {
     // This return value is unused, so just set an empty vec
     Ok(ApubCommunityOutbox(Vec::new()))
   }
-
-  type DbType = ();
 }