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