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 async fn read_from_apub_id(
38 data: &Self::DataType,
39 ) -> Result<Option<Self>, LemmyError> {
40 // Only read from database if its a local community, otherwise fetch over http
42 let community_id = data.0.id;
43 let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
44 Post::list_for_community(conn, community_id)
50 Ok(Some(ApubCommunityOutbox(post_list)))
56 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
57 // do nothing (it gets deleted automatically with the community)
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 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
79 // no tombstone for this, there is only a tombstone for the community
84 group_outbox: &GroupOutbox,
85 expected_domain: &Url,
86 _context: &CommunityContext,
87 _request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 verify_domains_match(expected_domain, &group_outbox.id)?;
95 data: &Self::DataType,
96 request_counter: &mut i32,
97 ) -> Result<Self, LemmyError> {
98 let mut outbox_activities = apub.ordered_items;
99 if outbox_activities.len() > 20 {
100 outbox_activities = outbox_activities[0..20].to_vec();
103 // We intentionally ignore errors here. This is because the outbox might contain posts from old
104 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
105 // item and only parse the ones that work.
106 let data = Data::new(data.1.clone());
107 for activity in outbox_activities {
108 let verify = activity.verify(&data, request_counter).await;
110 activity.receive(&data, request_counter).await.ok();
114 // This return value is unused, so just set an empty vec
115 Ok(ApubCommunityOutbox { 0: vec![] })