2 activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
3 collections::CommunityContext,
4 context::lemmy_context,
6 objects::{person::ApubPerson, post::ApubPost},
9 base::AnyBase, chrono::NaiveDateTime, collection::kind::OrderedCollectionType,
10 primitives::OneOrMany, url::Url,
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;
23 use serde::{Deserialize, Serialize};
24 use serde_with::skip_serializing_none;
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct GroupOutbox {
30 #[serde(rename = "@context")]
31 context: OneOrMany<AnyBase>,
32 r#type: OrderedCollectionType,
34 ordered_items: Vec<CreateOrUpdatePost>,
37 #[derive(Clone, Debug)]
38 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
40 #[async_trait::async_trait(?Send)]
41 impl ApubObject for ApubCommunityOutbox {
42 type DataType = CommunityContext;
43 type TombstoneType = ();
44 type ApubType = GroupOutbox;
46 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
50 async fn read_from_apub_id(
52 data: &Self::DataType,
53 ) -> Result<Option<Self>, LemmyError> {
54 // Only read from database if its a local community, otherwise fetch over http
56 let community_id = data.0.id;
57 let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
58 Post::list_for_community(conn, community_id)
64 Ok(Some(ApubCommunityOutbox(post_list)))
70 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
71 // do nothing (it gets deleted automatically with the community)
75 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
76 let mut ordered_items = vec![];
78 let actor = post.creator_id;
79 let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
83 CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
84 ordered_items.push(a);
88 context: lemmy_context(),
89 r#type: OrderedCollectionType::OrderedCollection,
90 id: generate_outbox_url(&data.0.actor_id)?.into(),
95 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
96 // no tombstone for this, there is only a tombstone for the community
101 apub: &Self::ApubType,
102 data: &Self::DataType,
103 expected_domain: &Url,
104 request_counter: &mut i32,
105 ) -> Result<Self, LemmyError> {
106 verify_domains_match(expected_domain, &apub.id)?;
107 let mut outbox_activities = apub.ordered_items.clone();
108 if outbox_activities.len() > 20 {
109 outbox_activities = outbox_activities[0..20].to_vec();
112 // We intentionally ignore errors here. This is because the outbox might contain posts from old
113 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
114 // item and only parse the ones that work.
115 for activity in outbox_activities {
117 .receive(&Data::new(data.1.clone()), request_counter)
122 // This return value is unused, so just set an empty vec
123 Ok(ApubCommunityOutbox { 0: vec![] })