2 activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
3 collections::CommunityContext,
5 objects::{person::ApubPerson, post::ApubPost},
6 protocol::collections::group_outbox::GroupOutbox,
8 use activitystreams::collection::kind::OrderedCollectionType;
9 use chrono::NaiveDateTime;
10 use lemmy_api_common::blocking;
13 traits::{ActivityHandler, ApubObject},
14 verify::verify_domains_match,
16 use lemmy_db_schema::{
17 source::{person::Person, 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 to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62 let mut ordered_items = vec![];
64 let actor = post.creator_id;
65 let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
69 CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
70 ordered_items.push(a);
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
87 apub: &Self::ApubType,
88 data: &Self::DataType,
89 expected_domain: &Url,
90 request_counter: &mut i32,
91 ) -> Result<Self, LemmyError> {
92 verify_domains_match(expected_domain, &apub.id)?;
93 let mut outbox_activities = apub.ordered_items.clone();
94 if outbox_activities.len() > 20 {
95 outbox_activities = outbox_activities[0..20].to_vec();
98 // We intentionally ignore errors here. This is because the outbox might contain posts from old
99 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
100 // item and only parse the ones that work.
101 for activity in outbox_activities {
103 .receive(&Data::new(data.1.clone()), request_counter)
108 // This return value is unused, so just set an empty vec
109 Ok(ApubCommunityOutbox { 0: vec![] })