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