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 lemmy_api_common::blocking;
16 traits::{ActivityHandler, ApubObject},
17 verify::verify_domains_match,
19 use lemmy_db_schema::source::post::Post;
20 use lemmy_utils::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 TombstoneType = ();
30 type ApubType = GroupOutbox;
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> = blocking(data.1.pool(), move |conn| {
45 Post::list_for_community(conn, community_id)
51 Ok(Some(ApubCommunityOutbox(post_list)))
57 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
58 // do nothing (it gets deleted automatically with the community)
62 #[tracing::instrument(skip_all)]
63 async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
64 let mut ordered_items = vec![];
66 let page = post.into_apub(&data.1).await?;
67 let announcable = AnnouncableActivities::Page(page);
68 let announce = AnnounceActivity::new(announcable, &data.0, &data.1)?;
69 ordered_items.push(announce);
73 r#type: OrderedCollectionType::OrderedCollection,
74 id: generate_outbox_url(&data.0.actor_id)?.into(),
75 total_items: ordered_items.len() as i32,
80 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
81 // no tombstone for this, there is only a tombstone for the community
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() > 20 {
104 outbox_activities = outbox_activities[0..20].to_vec();
107 // We intentionally ignore errors here. This is because the outbox might contain posts from old
108 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
109 // item and only parse the ones that work.
110 let data = Data::new(data.1.clone());
111 for activity in outbox_activities {
112 let verify = activity.verify(&data, request_counter).await;
114 activity.receive(&data, request_counter).await.ok();
118 // This return value is unused, so just set an empty vec
119 Ok(ApubCommunityOutbox(Vec::new()))