2 activity_lists::AnnouncableActivities,
3 collections::CommunityContext,
5 objects::post::ApubPost,
7 activities::community::announce::AnnounceActivity,
8 collections::group_outbox::GroupOutbox,
11 use activitystreams_kinds::collection::OrderedCollectionType;
12 use chrono::NaiveDateTime;
13 use futures::future::join_all;
14 use lemmy_api_common::utils::blocking;
17 traits::{ActivityHandler, ApubObject},
18 verify::verify_domains_match,
20 use lemmy_db_schema::source::post::Post;
21 use lemmy_utils::LemmyError;
24 #[derive(Clone, Debug)]
25 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
27 #[async_trait::async_trait(?Send)]
28 impl ApubObject for ApubCommunityOutbox {
29 type DataType = CommunityContext;
30 type TombstoneType = ();
31 type ApubType = GroupOutbox;
33 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
37 #[tracing::instrument(skip_all)]
38 async fn read_from_apub_id(
40 data: &Self::DataType,
41 ) -> Result<Option<Self>, LemmyError> {
42 // Only read from database if its a local community, otherwise fetch over http
44 let community_id = data.0.id;
45 let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
46 Post::list_for_community(conn, community_id)
52 Ok(Some(ApubCommunityOutbox(post_list)))
58 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
59 // do nothing (it gets deleted automatically with the community)
63 #[tracing::instrument(skip_all)]
64 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
65 let mut ordered_items = vec![];
67 let page = post.into_apub(&data.1).await?;
68 let announcable = AnnouncableActivities::Page(page);
69 let announce = AnnounceActivity::new(announcable, &data.0, &data.1)?;
70 ordered_items.push(announce);
74 r#type: OrderedCollectionType::OrderedCollection,
75 id: generate_outbox_url(&data.0.actor_id)?.into(),
76 total_items: ordered_items.len() as i32,
81 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
82 // no tombstone for this, there is only a tombstone for the community
86 #[tracing::instrument(skip_all)]
88 group_outbox: &GroupOutbox,
89 expected_domain: &Url,
90 _context: &CommunityContext,
91 _request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 verify_domains_match(expected_domain, &group_outbox.id)?;
97 #[tracing::instrument(skip_all)]
100 data: &Self::DataType,
101 _request_counter: &mut i32,
102 ) -> Result<Self, LemmyError> {
103 let mut outbox_activities = apub.ordered_items;
104 if outbox_activities.len() > 20 {
105 outbox_activities = outbox_activities[0..20].to_vec();
108 // We intentionally ignore errors here. This is because the outbox might contain posts from old
109 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
110 // item and only parse the ones that work.
111 let data = Data::new(data.1.clone());
112 // process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
113 join_all(outbox_activities.into_iter().map(|activity| {
115 // use separate request counter for each item, otherwise there will be problems with
116 // parallel processing
117 let request_counter = &mut 0;
118 let verify = activity.verify(&data, request_counter).await;
120 activity.receive(&data, request_counter).await.ok();
126 // This return value is unused, so just set an empty vec
127 Ok(ApubCommunityOutbox(Vec::new()))