Rewrite community followers and user outbox to use our own structs
authorFelix Ableitner <me@nutomic.com>
Thu, 28 Oct 2021 15:52:11 +0000 (17:52 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 29 Oct 2021 10:50:32 +0000 (12:50 +0200)
crates/apub/src/collections/community_followers.rs [new file with mode: 0644]
crates/apub/src/collections/community_moderators.rs
crates/apub/src/collections/community_outbox.rs
crates/apub/src/collections/mod.rs
crates/apub/src/collections/user_outbox.rs [new file with mode: 0644]
crates/apub/src/http/community.rs
crates/apub/src/http/person.rs

diff --git a/crates/apub/src/collections/community_followers.rs b/crates/apub/src/collections/community_followers.rs
new file mode 100644 (file)
index 0000000..41155b1
--- /dev/null
@@ -0,0 +1,38 @@
+use crate::generate_followers_url;
+use activitystreams::collection::kind::CollectionType;
+use lemmy_api_common::blocking;
+use lemmy_db_schema::source::community::Community;
+use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
+use lemmy_utils::LemmyError;
+use lemmy_websocket::LemmyContext;
+use serde::{Deserialize, Serialize};
+use url::Url;
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct CommunityFollowers {
+  id: Url,
+  r#type: CollectionType,
+  total_items: i32,
+  items: Vec<()>,
+}
+
+impl CommunityFollowers {
+  pub(crate) async fn new(
+    community: Community,
+    context: &LemmyContext,
+  ) -> Result<CommunityFollowers, LemmyError> {
+    let community_id = community.id;
+    let community_followers = blocking(context.pool(), move |conn| {
+      CommunityFollowerView::for_community(conn, community_id)
+    })
+    .await??;
+
+    Ok(CommunityFollowers {
+      id: generate_followers_url(&community.actor_id)?.into_inner(),
+      r#type: CollectionType::Collection,
+      total_items: community_followers.len() as i32,
+      items: vec![],
+    })
+  }
+}
index 2d190f4e2506f649936afec56d57ede03aa53ef9..d91b41c458a6af2256b665cad0f96906d6e87aa9 100644 (file)
@@ -14,10 +14,8 @@ use lemmy_db_schema::{
 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
 use lemmy_utils::LemmyError;
 use serde::{Deserialize, Serialize};
-use serde_with::skip_serializing_none;
 use url::Url;
 
-#[skip_serializing_none]
 #[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
 pub struct GroupModerators {
index 9d870f0e836b40e1acf7067c7bc40ccf04edca6b..98fb6644854b2e3bbc13b3013fff75705cf585cc 100644 (file)
@@ -18,15 +18,14 @@ use lemmy_db_schema::{
 };
 use lemmy_utils::LemmyError;
 use serde::{Deserialize, Serialize};
-use serde_with::skip_serializing_none;
 use url::Url;
 
-#[skip_serializing_none]
 #[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
 pub struct GroupOutbox {
   r#type: OrderedCollectionType,
   id: Url,
+  total_items: i32,
   ordered_items: Vec<CreateOrUpdatePost>,
 }
 
@@ -83,6 +82,7 @@ impl ApubObject for ApubCommunityOutbox {
     Ok(GroupOutbox {
       r#type: OrderedCollectionType::OrderedCollection,
       id: generate_outbox_url(&data.0.actor_id)?.into(),
+      total_items: ordered_items.len() as i32,
       ordered_items,
     })
   }
index e2a274a01ee24328d69adf14b6231b54ac09e6a0..378e14c0437e80d97b38277cbad711de9ae8c83c 100644 (file)
@@ -1,8 +1,10 @@
 use crate::objects::community::ApubCommunity;
 use lemmy_websocket::LemmyContext;
 
+pub(crate) mod community_followers;
 pub(crate) mod community_moderators;
 pub(crate) mod community_outbox;
+pub(crate) mod user_outbox;
 
 /// Put community in the data, so we dont have to read it again from the database.
 pub(crate) struct CommunityContext(pub ApubCommunity, pub LemmyContext);
diff --git a/crates/apub/src/collections/user_outbox.rs b/crates/apub/src/collections/user_outbox.rs
new file mode 100644 (file)
index 0000000..59c6975
--- /dev/null
@@ -0,0 +1,26 @@
+use crate::generate_outbox_url;
+use activitystreams::collection::kind::OrderedCollectionType;
+use lemmy_db_schema::source::person::Person;
+use lemmy_utils::LemmyError;
+use serde::{Deserialize, Serialize};
+use url::Url;
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct UserOutbox {
+  r#type: OrderedCollectionType,
+  id: Url,
+  ordered_items: Vec<()>,
+  total_items: i32,
+}
+
+impl UserOutbox {
+  pub(crate) async fn new(user: Person) -> Result<UserOutbox, LemmyError> {
+    Ok(UserOutbox {
+      r#type: OrderedCollectionType::OrderedCollection,
+      id: generate_outbox_url(&user.actor_id)?.into_inner(),
+      ordered_items: vec![],
+      total_items: 0,
+    })
+  }
+}
index 6c9c632e1c1778ce0dee6fa03108ebae4510c969..73f59d035261646d2dcecb87c2667f8ce4f5cde5 100644 (file)
@@ -6,6 +6,7 @@ use crate::{
     verify_person_in_community,
   },
   collections::{
+    community_followers::CommunityFollowers,
     community_moderators::ApubCommunityModerators,
     community_outbox::ApubCommunityOutbox,
     CommunityContext,
@@ -21,10 +22,6 @@ use crate::{
   },
   objects::community::ApubCommunity,
 };
-use activitystreams::{
-  base::BaseExt,
-  collection::{CollectionExt, UnorderedCollection},
-};
 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::{
@@ -32,7 +29,6 @@ use lemmy_apub_lib::{
   verify::verify_domains_match,
 };
 use lemmy_db_schema::source::community::Community;
-use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use log::info;
@@ -118,19 +114,8 @@ pub(crate) async fn get_apub_community_followers(
     Community::read_from_name(conn, &info.community_name)
   })
   .await??;
-
-  let community_id = community.id;
-  let community_followers = blocking(context.pool(), move |conn| {
-    CommunityFollowerView::for_community(conn, community_id)
-  })
-  .await??;
-
-  let mut collection = UnorderedCollection::new();
-  collection
-    .set_id(community.followers_url.into())
-    .set_total_items(community_followers.len() as u64);
-  let collection = WithContext::new(collection);
-  Ok(create_apub_response(&collection))
+  let followers = CommunityFollowers::new(community, &context).await?;
+  Ok(create_apub_response(&followers))
 }
 
 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
index 2cc187681f4f48151af7efb9687b1c2b1e647ecc..a2d4fedd86c4675e17c2f8acf060713a9e80e936 100644 (file)
@@ -8,8 +8,8 @@ use crate::{
       undo_delete::UndoDeletePrivateMessage,
     },
   },
+  collections::user_outbox::UserOutbox,
   context::WithContext,
-  generate_outbox_url,
   http::{
     create_apub_response,
     create_apub_tombstone_response,
@@ -18,10 +18,6 @@ use crate::{
   },
   objects::person::ApubPerson,
 };
-use activitystreams::{
-  base::BaseExt,
-  collection::{CollectionExt, OrderedCollection},
-};
 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
@@ -30,7 +26,6 @@ use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use log::info;
 use serde::{Deserialize, Serialize};
-use url::Url;
 
 #[derive(Deserialize)]
 pub struct PersonQuery {
@@ -100,12 +95,6 @@ pub(crate) async fn get_apub_person_outbox(
     Person::find_by_name(conn, &info.user_name)
   })
   .await??;
-  // TODO: populate the person outbox
-  let mut collection = OrderedCollection::new();
-  collection
-    .set_many_items(Vec::<Url>::new())
-    .set_id(generate_outbox_url(&person.actor_id)?.into())
-    .set_total_items(0_u64);
-  let collection = WithContext::new(collection);
-  Ok(create_apub_response(&collection))
+  let outbox = UserOutbox::new(person).await?;
+  Ok(create_apub_response(&outbox))
 }