2 collections::CommunityContext,
3 objects::post::ApubPost,
4 protocol::collections::group_featured::GroupFeatured,
6 use activitypub_federation::{
8 traits::{ActivityHandler, ApubObject},
9 utils::verify_domains_match,
11 use activitystreams_kinds::collection::OrderedCollectionType;
12 use futures::future::{join_all, try_join_all};
13 use lemmy_api_common::utils::generate_featured_url;
14 use lemmy_db_schema::{source::post::Post, utils::FETCH_LIMIT_MAX};
15 use lemmy_utils::error::LemmyError;
18 #[derive(Clone, Debug)]
19 pub(crate) struct ApubCommunityFeatured(Vec<ApubPost>);
21 #[async_trait::async_trait(?Send)]
22 impl ApubObject for ApubCommunityFeatured {
23 type DataType = CommunityContext;
24 type ApubType = GroupFeatured;
26 type Error = LemmyError;
28 async fn read_from_apub_id(
30 data: &Self::DataType,
31 ) -> Result<Option<Self>, Self::Error>
35 // Only read from database if its a local community, otherwise fetch over http
37 let community_id = data.0.id;
38 let post_list: Vec<ApubPost> = Post::list_featured_for_community(data.1.pool(), community_id)
43 Ok(Some(ApubCommunityFeatured(post_list)))
49 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, Self::Error> {
50 let ordered_items = try_join_all(self.0.into_iter().map(|p| p.into_apub(&data.1))).await?;
52 r#type: OrderedCollectionType::OrderedCollection,
53 id: generate_featured_url(&data.0.actor_id)?.into(),
54 total_items: ordered_items.len() as i32,
60 apub: &Self::ApubType,
61 expected_domain: &Url,
62 _data: &Self::DataType,
63 _request_counter: &mut i32,
64 ) -> Result<(), Self::Error> {
65 verify_domains_match(expected_domain, &apub.id)?;
71 data: &Self::DataType,
72 _request_counter: &mut i32,
73 ) -> Result<Self, Self::Error>
77 let mut posts = apub.ordered_items;
78 if posts.len() as i64 > FETCH_LIMIT_MAX {
79 posts = posts[0..(FETCH_LIMIT_MAX as usize)].to_vec();
82 // We intentionally ignore errors here. This is because the outbox might contain posts from old
83 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
84 // item and only parse the ones that work.
85 let data = Data::new(data.1.clone());
86 // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
87 join_all(posts.into_iter().map(|post| {
89 // use separate request counter for each item, otherwise there will be problems with
90 // parallel processing
91 let request_counter = &mut 0;
92 let verify = post.verify(&data, request_counter).await;
94 post.receive(&data, request_counter).await.ok();
100 // This return value is unused, so just set an empty vec
101 Ok(ApubCommunityFeatured(Vec::new()))