]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_featured.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / apub / src / collections / community_featured.rs
1 use crate::{
2   collections::CommunityContext,
3   objects::post::ApubPost,
4   protocol::collections::group_featured::GroupFeatured,
5 };
6 use activitypub_federation::{
7   data::Data,
8   traits::{ActivityHandler, ApubObject},
9   utils::verify_domains_match,
10 };
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;
16 use url::Url;
17
18 #[derive(Clone, Debug)]
19 pub(crate) struct ApubCommunityFeatured(Vec<ApubPost>);
20
21 #[async_trait::async_trait(?Send)]
22 impl ApubObject for ApubCommunityFeatured {
23   type DataType = CommunityContext;
24   type ApubType = GroupFeatured;
25   type DbType = ();
26   type Error = LemmyError;
27
28   async fn read_from_apub_id(
29     _object_id: Url,
30     data: &Self::DataType,
31   ) -> Result<Option<Self>, Self::Error>
32   where
33     Self: Sized,
34   {
35     // Only read from database if its a local community, otherwise fetch over http
36     if data.0.local {
37       let community_id = data.0.id;
38       let post_list: Vec<ApubPost> = Post::list_featured_for_community(data.1.pool(), community_id)
39         .await?
40         .into_iter()
41         .map(Into::into)
42         .collect();
43       Ok(Some(ApubCommunityFeatured(post_list)))
44     } else {
45       Ok(None)
46     }
47   }
48
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?;
51     Ok(GroupFeatured {
52       r#type: OrderedCollectionType::OrderedCollection,
53       id: generate_featured_url(&data.0.actor_id)?.into(),
54       total_items: ordered_items.len() as i32,
55       ordered_items,
56     })
57   }
58
59   async fn verify(
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)?;
66     Ok(())
67   }
68
69   async fn from_apub(
70     apub: Self::ApubType,
71     data: &Self::DataType,
72     _request_counter: &mut i32,
73   ) -> Result<Self, Self::Error>
74   where
75     Self: Sized,
76   {
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();
80     }
81
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| {
88       async {
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;
93         if verify.is_ok() {
94           post.receive(&data, request_counter).await.ok();
95         }
96       }
97     }))
98     .await;
99
100     // This return value is unused, so just set an empty vec
101     Ok(ApubCommunityFeatured(Vec::new()))
102   }
103 }