2 activity_lists::AnnouncableActivities,
3 collections::CommunityContext,
5 objects::post::ApubPost,
8 community::announce::AnnounceActivity,
9 create_or_update::page::CreateOrUpdatePage,
12 collections::group_outbox::GroupOutbox,
15 use activitypub_federation::{
17 traits::{ActivityHandler, ApubObject},
18 utils::verify_domains_match,
20 use activitystreams_kinds::collection::OrderedCollectionType;
21 use chrono::NaiveDateTime;
22 use futures::future::join_all;
23 use lemmy_db_schema::{
24 source::{person::Person, post::Post},
27 use lemmy_utils::error::LemmyError;
30 #[derive(Clone, Debug)]
31 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
33 #[async_trait::async_trait(?Send)]
34 impl ApubObject for ApubCommunityOutbox {
35 type DataType = CommunityContext;
36 type ApubType = GroupOutbox;
37 type Error = LemmyError;
39 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
43 #[tracing::instrument(skip_all)]
44 async fn read_from_apub_id(
46 data: &Self::DataType,
47 ) -> Result<Option<Self>, LemmyError> {
48 // Only read from database if its a local community, otherwise fetch over http
50 let community_id = data.0.id;
51 let post_list: Vec<ApubPost> = Post::list_for_community(data.1.pool(), community_id)
56 Ok(Some(ApubCommunityOutbox(post_list)))
62 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
63 // do nothing (it gets deleted automatically with the community)
67 #[tracing::instrument(skip_all)]
68 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
69 let mut ordered_items = vec![];
71 let person = Person::read(data.1.pool(), post.creator_id).await?.into();
73 CreateOrUpdatePage::new(post, &person, &data.0, CreateOrUpdateType::Create, &data.1)
75 let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
76 let announce = AnnounceActivity::new(announcable.try_into()?, &data.0, &data.1)?;
77 ordered_items.push(announce);
81 r#type: OrderedCollectionType::OrderedCollection,
82 id: generate_outbox_url(&data.0.actor_id)?.into(),
83 total_items: ordered_items.len() as i32,
88 #[tracing::instrument(skip_all)]
90 group_outbox: &GroupOutbox,
91 expected_domain: &Url,
92 _context: &CommunityContext,
93 _request_counter: &mut i32,
94 ) -> Result<(), LemmyError> {
95 verify_domains_match(expected_domain, &group_outbox.id)?;
99 #[tracing::instrument(skip_all)]
101 apub: Self::ApubType,
102 data: &Self::DataType,
103 _request_counter: &mut i32,
104 ) -> Result<Self, LemmyError> {
105 let mut outbox_activities = apub.ordered_items;
106 if outbox_activities.len() > 20 {
107 outbox_activities = outbox_activities[0..20].to_vec();
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()))