2 collections::CommunityContext,
4 objects::{person::ApubPerson, post::ApubPost},
6 activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
7 collections::group_outbox::GroupOutbox,
10 use activitystreams::collection::kind::OrderedCollectionType;
11 use chrono::NaiveDateTime;
12 use lemmy_api_common::blocking;
15 traits::{ActivityHandler, ApubObject},
16 verify::verify_domains_match,
18 use lemmy_db_schema::{
19 source::{person::Person, post::Post},
22 use lemmy_utils::LemmyError;
25 #[derive(Clone, Debug)]
26 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
28 #[async_trait::async_trait(?Send)]
29 impl ApubObject for ApubCommunityOutbox {
30 type DataType = CommunityContext;
31 type TombstoneType = ();
32 type ApubType = GroupOutbox;
34 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
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 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
64 let mut ordered_items = vec![];
66 let actor = post.creator_id;
67 let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
71 CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
72 ordered_items.push(a);
76 r#type: OrderedCollectionType::OrderedCollection,
77 id: generate_outbox_url(&data.0.actor_id)?.into(),
78 total_items: ordered_items.len() as i32,
83 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
84 // no tombstone for this, there is only a tombstone for the community
89 apub: &Self::ApubType,
90 data: &Self::DataType,
91 expected_domain: &Url,
92 request_counter: &mut i32,
93 ) -> Result<Self, LemmyError> {
94 verify_domains_match(expected_domain, &apub.id)?;
95 let mut outbox_activities = apub.ordered_items.clone();
96 if outbox_activities.len() > 20 {
97 outbox_activities = outbox_activities[0..20].to_vec();
100 // We intentionally ignore errors here. This is because the outbox might contain posts from old
101 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
102 // item and only parse the ones that work.
103 for activity in outbox_activities {
105 .receive(&Data::new(data.1.clone()), request_counter)
110 // This return value is unused, so just set an empty vec
111 Ok(ApubCommunityOutbox { 0: vec![] })