1 use activitystreams::collection::kind::OrderedCollectionType;
2 use chrono::NaiveDateTime;
5 use lemmy_api_common::blocking;
8 traits::{ActivityHandler, ApubObject},
9 verify::verify_domains_match,
11 use lemmy_db_schema::{
12 source::{person::Person, post::Post},
15 use lemmy_utils::LemmyError;
18 collections::CommunityContext,
20 objects::{person::ApubPerson, post::ApubPost},
22 activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
23 collections::group_outbox::GroupOutbox,
27 #[derive(Clone, Debug)]
28 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
30 #[async_trait::async_trait(?Send)]
31 impl ApubObject for ApubCommunityOutbox {
32 type DataType = CommunityContext;
33 type TombstoneType = ();
34 type ApubType = GroupOutbox;
36 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
40 async fn read_from_apub_id(
42 data: &Self::DataType,
43 ) -> Result<Option<Self>, LemmyError> {
44 // Only read from database if its a local community, otherwise fetch over http
46 let community_id = data.0.id;
47 let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
48 Post::list_for_community(conn, community_id)
54 Ok(Some(ApubCommunityOutbox(post_list)))
60 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
61 // do nothing (it gets deleted automatically with the community)
65 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
66 let mut ordered_items = vec![];
68 let actor = post.creator_id;
69 let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
73 CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
74 ordered_items.push(a);
78 r#type: OrderedCollectionType::OrderedCollection,
79 id: generate_outbox_url(&data.0.actor_id)?.into(),
80 total_items: ordered_items.len() as i32,
85 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
86 // no tombstone for this, there is only a tombstone for the community
91 apub: &Self::ApubType,
92 data: &Self::DataType,
93 expected_domain: &Url,
94 request_counter: &mut i32,
95 ) -> Result<Self, LemmyError> {
96 verify_domains_match(expected_domain, &apub.id)?;
97 let mut outbox_activities = apub.ordered_items.clone();
98 if outbox_activities.len() > 20 {
99 outbox_activities = outbox_activities[0..20].to_vec();
102 // We intentionally ignore errors here. This is because the outbox might contain posts from old
103 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
104 // item and only parse the ones that work.
105 for activity in outbox_activities {
107 .receive(&Data::new(data.1.clone()), request_counter)
112 // This return value is unused, so just set an empty vec
113 Ok(ApubCommunityOutbox { 0: vec![] })