]> Untitled Git - lemmy.git/blob - 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
1 use crate::{
2   activity_lists::AnnouncableActivities,
3   collections::CommunityContext,
4   objects::post::ApubPost,
5   protocol::{
6     activities::{
7       community::announce::AnnounceActivity,
8       create_or_update::page::CreateOrUpdatePage,
9       CreateOrUpdateType,
10     },
11     collections::group_outbox::GroupOutbox,
12   },
13 };
14 use activitypub_federation::{
15   data::Data,
16   traits::{ActivityHandler, ApubObject},
17   utils::verify_domains_match,
18 };
19 use activitystreams_kinds::collection::OrderedCollectionType;
20 use chrono::NaiveDateTime;
21 use futures::future::join_all;
22 use lemmy_api_common::utils::generate_outbox_url;
23 use lemmy_db_schema::{
24   source::{person::Person, post::Post},
25   traits::Crud,
26   utils::FETCH_LIMIT_MAX,
27 };
28 use lemmy_utils::error::LemmyError;
29 use url::Url;
30
31 #[derive(Clone, Debug)]
32 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
33
34 #[async_trait::async_trait(?Send)]
35 impl ApubObject for ApubCommunityOutbox {
36   type DataType = CommunityContext;
37   type ApubType = GroupOutbox;
38   type Error = LemmyError;
39   type DbType = ();
40
41   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
42     None
43   }
44
45   #[tracing::instrument(skip_all)]
46   async fn read_from_apub_id(
47     _object_id: Url,
48     data: &Self::DataType,
49   ) -> Result<Option<Self>, LemmyError> {
50     // Only read from database if its a local community, otherwise fetch over http
51     if data.0.local {
52       let community_id = data.0.id;
53       let post_list: Vec<ApubPost> = Post::list_for_community(data.1.pool(), community_id)
54         .await?
55         .into_iter()
56         .map(Into::into)
57         .collect();
58       Ok(Some(ApubCommunityOutbox(post_list)))
59     } else {
60       Ok(None)
61     }
62   }
63
64   #[tracing::instrument(skip_all)]
65   async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
66     let mut ordered_items = vec![];
67     for post in self.0 {
68       let person = Person::read(data.1.pool(), post.creator_id).await?.into();
69       let create =
70         CreateOrUpdatePage::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1)
71           .await?;
72       let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
73       let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?;
74       ordered_items.push(announce);
75     }
76
77     Ok(GroupOutbox {
78       r#type: OrderedCollectionType::OrderedCollection,
79       id: generate_outbox_url(&data.0.actor_id)?.into(),
80       total_items: ordered_items.len() as i32,
81       ordered_items,
82     })
83   }
84
85   #[tracing::instrument(skip_all)]
86   async fn verify(
87     group_outbox: &GroupOutbox,
88     expected_domain: &Url,
89     _context: &CommunityContext,
90     _request_counter: &mut i32,
91   ) -> Result<(), LemmyError> {
92     verify_domains_match(expected_domain, &group_outbox.id)?;
93     Ok(())
94   }
95
96   #[tracing::instrument(skip_all)]
97   async fn from_apub(
98     apub: Self::ApubType,
99     data: &Self::DataType,
100     _request_counter: &mut i32,
101   ) -> Result<Self, LemmyError> {
102     let mut outbox_activities = apub.ordered_items;
103     if outbox_activities.len() as i64 > FETCH_LIMIT_MAX {
104       outbox_activities = outbox_activities[0..(FETCH_LIMIT_MAX as usize)].to_vec();
105     }
106
107     // We intentionally ignore errors here. This is because the outbox might contain posts from old
108     // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
109     // item and only parse the ones that work.
110     let data = Data::new(data.1.clone());
111     // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
112     join_all(outbox_activities.into_iter().map(|activity| {
113       async {
114         // use separate request counter for each item, otherwise there will be problems with
115         // parallel processing
116         let request_counter = &mut 0;
117         let verify = activity.verify(&data, request_counter).await;
118         if verify.is_ok() {
119           activity.receive(&data, request_counter).await.ok();
120         }
121       }
122     }))
123     .await;
124
125     // This return value is unused, so just set an empty vec
126     Ok(ApubCommunityOutbox(Vec::new()))
127   }
128 }