2 activity_lists::AnnouncableActivities,
3 collections::CommunityContext,
4 objects::post::ApubPost,
7 community::announce::AnnounceActivity,
8 create_or_update::page::CreateOrUpdatePage,
11 collections::group_outbox::GroupOutbox,
14 use activitypub_federation::{
16 traits::{ActivityHandler, ApubObject},
17 utils::verify_domains_match,
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},
26 utils::FETCH_LIMIT_MAX,
28 use lemmy_utils::error::LemmyError;
31 #[derive(Clone, Debug)]
32 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
34 #[async_trait::async_trait(?Send)]
35 impl ApubObject for ApubCommunityOutbox {
36 type DataType = CommunityContext;
37 type ApubType = GroupOutbox;
38 type Error = LemmyError;
41 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
45 #[tracing::instrument(skip_all)]
46 async fn read_from_apub_id(
48 data: &Self::DataType,
49 ) -> Result<Option<Self>, LemmyError> {
50 // Only read from database if its a local community, otherwise fetch over http
52 let community_id = data.0.id;
53 let post_list: Vec<ApubPost> = Post::list_for_community(data.1.pool(), community_id)
58 Ok(Some(ApubCommunityOutbox(post_list)))
64 #[tracing::instrument(skip_all)]
65 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
66 let mut ordered_items = vec![];
68 let person = Person::read(data.1.pool(), post.creator_id).await?.into();
70 CreateOrUpdatePage::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1)
72 let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
73 let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?;
74 ordered_items.push(announce);
78 r#type: OrderedCollectionType::OrderedCollection,
79 id: generate_outbox_url(&data.0.actor_id)?.into(),
80 total_items: ordered_items.len() as i32,
85 #[tracing::instrument(skip_all)]
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)?;
96 #[tracing::instrument(skip_all)]
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
105 .get(0..(FETCH_LIMIT_MAX as usize))
110 // We intentionally ignore errors here. This is because the outbox might contain posts from old
111 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
112 // item and only parse the ones that work.
113 let data = Data::new(data.1.clone());
114 // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
115 join_all(outbox_activities.into_iter().map(|activity| {
117 // use separate request counter for each item, otherwise there will be problems with
118 // parallel processing
119 let request_counter = &mut 0;
120 let verify = activity.verify(&data, request_counter).await;
122 activity.receive(&data, request_counter).await.ok();
128 // This return value is unused, so just set an empty vec
129 Ok(ApubCommunityOutbox(Vec::new()))