2 objects::{community::ApubCommunity, post::ApubPost},
3 protocol::collections::group_featured::GroupFeatured,
5 use activitypub_federation::{
7 kinds::collection::OrderedCollectionType,
8 protocol::verification::verify_domains_match,
9 traits::{ActivityHandler, Collection, Object},
11 use futures::future::{join_all, try_join_all};
12 use lemmy_api_common::{context::LemmyContext, utils::generate_featured_url};
13 use lemmy_db_schema::{source::post::Post, utils::FETCH_LIMIT_MAX};
14 use lemmy_utils::error::LemmyError;
17 #[derive(Clone, Debug)]
18 pub(crate) struct ApubCommunityFeatured(Vec<ApubPost>);
20 #[async_trait::async_trait]
21 impl Collection for ApubCommunityFeatured {
22 type Owner = ApubCommunity;
23 type DataType = LemmyContext;
24 type Kind = GroupFeatured;
25 type Error = LemmyError;
29 data: &Data<Self::DataType>,
30 ) -> Result<Self::Kind, Self::Error> {
31 let ordered_items = try_join_all(
32 Post::list_featured_for_community(&mut data.pool(), owner.id)
36 .map(|p| p.into_json(data)),
40 r#type: OrderedCollectionType::OrderedCollection,
41 id: generate_featured_url(&owner.actor_id)?.into(),
42 total_items: ordered_items.len() as i32,
49 expected_domain: &Url,
50 _data: &Data<Self::DataType>,
51 ) -> Result<(), Self::Error> {
52 verify_domains_match(expected_domain, &apub.id)?;
59 data: &Data<Self::DataType>,
60 ) -> Result<Self, Self::Error>
64 let mut posts = apub.ordered_items;
65 if posts.len() as i64 > FETCH_LIMIT_MAX {
67 .get(0..(FETCH_LIMIT_MAX as usize))
72 // We intentionally ignore errors here. This is because the outbox might contain posts from old
73 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
74 // item and only parse the ones that work.
75 // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
76 join_all(posts.into_iter().map(|post| {
78 // use separate request counter for each item, otherwise there will be problems with
79 // parallel processing
80 let verify = post.verify(data).await;
82 post.receive(data).await.ok();
88 // This return value is unused, so just set an empty vec
89 Ok(ApubCommunityFeatured(Vec::new()))