]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Make functions work with both connection and pool (#3420)
[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(&mut 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(&mut data.pool(), post.creator_id)
52         .await?
53         .into();
54       let create =
55         CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?;
56       let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
57       let announce = AnnounceActivity::new(announcable.try_into()?, owner, data)?;
58       ordered_items.push(announce);
59     }
60
61     Ok(GroupOutbox {
62       r#type: OrderedCollectionType::OrderedCollection,
63       id: generate_outbox_url(&owner.actor_id)?.into(),
64       total_items: ordered_items.len() as i32,
65       ordered_items,
66     })
67   }
68
69   #[tracing::instrument(skip_all)]
70   async fn verify(
71     group_outbox: &GroupOutbox,
72     expected_domain: &Url,
73     _data: &Data<Self::DataType>,
74   ) -> Result<(), LemmyError> {
75     verify_domains_match(expected_domain, &group_outbox.id)?;
76     Ok(())
77   }
78
79   #[tracing::instrument(skip_all)]
80   async fn from_json(
81     apub: Self::Kind,
82     _owner: &Self::Owner,
83     data: &Data<Self::DataType>,
84   ) -> Result<Self, LemmyError> {
85     let mut outbox_activities = apub.ordered_items;
86     if outbox_activities.len() as i64 > FETCH_LIMIT_MAX {
87       outbox_activities = outbox_activities
88         .get(0..(FETCH_LIMIT_MAX as usize))
89         .unwrap_or_default()
90         .to_vec();
91     }
92
93     // We intentionally ignore errors here. This is because the outbox might contain posts from old
94     // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
95     // item and only parse the ones that work.
96     // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
97     join_all(outbox_activities.into_iter().map(|activity| {
98       async {
99         // use separate request counter for each item, otherwise there will be problems with
100         // parallel processing
101         let verify = activity.verify(data).await;
102         if verify.is_ok() {
103           activity.receive(data).await.ok();
104         }
105       }
106     }))
107     .await;
108
109     // This return value is unused, so just set an empty vec
110     Ok(ApubCommunityOutbox(Vec::new()))
111   }
112 }