2 activity_lists::AnnouncableActivities,
3 collections::CommunityContext,
5 objects::post::ApubPost,
7 activities::community::announce::AnnounceActivity,
8 collections::group_outbox::GroupOutbox,
11 use activitypub_federation::{
13 traits::{ActivityHandler, ApubObject},
14 utils::verify_domains_match,
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;
23 #[derive(Clone, Debug)]
24 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
26 #[async_trait::async_trait(?Send)]
27 impl ApubObject for ApubCommunityOutbox {
28 type DataType = CommunityContext;
29 type ApubType = GroupOutbox;
30 type Error = LemmyError;
32 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
36 #[tracing::instrument(skip_all)]
37 async fn read_from_apub_id(
39 data: &Self::DataType,
40 ) -> Result<Option<Self>, LemmyError> {
41 // Only read from database if its a local community, otherwise fetch over http
43 let community_id = data.0.id;
44 let post_list: Vec<ApubPost> = Post::list_for_community(data.1.pool(), community_id)
49 Ok(Some(ApubCommunityOutbox(post_list)))
55 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
56 // do nothing (it gets deleted automatically with the community)
60 #[tracing::instrument(skip_all)]
61 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62 let mut ordered_items = vec![];
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);
71 r#type: OrderedCollectionType::OrderedCollection,
72 id: generate_outbox_url(&data.0.actor_id)?.into(),
73 total_items: ordered_items.len() as i32,
78 #[tracing::instrument(skip_all)]
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)?;
89 #[tracing::instrument(skip_all)]
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();
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| {
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;
112 activity.receive(&data, request_counter).await.ok();
118 // This return value is unused, so just set an empty vec
119 Ok(ApubCommunityOutbox(Vec::new()))