2 activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
3 collections::CommunityContext,
5 objects::{person::ApubPerson, post::ApubPost},
7 use activitystreams::collection::kind::OrderedCollectionType;
8 use chrono::NaiveDateTime;
9 use lemmy_api_common::blocking;
12 traits::{ActivityHandler, ApubObject},
13 verify::verify_domains_match,
15 use lemmy_db_schema::{
16 source::{person::Person, post::Post},
19 use lemmy_utils::LemmyError;
20 use serde::{Deserialize, Serialize};
21 use serde_with::skip_serializing_none;
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct GroupOutbox {
28 r#type: OrderedCollectionType,
30 ordered_items: Vec<CreateOrUpdatePost>,
33 #[derive(Clone, Debug)]
34 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
36 #[async_trait::async_trait(?Send)]
37 impl ApubObject for ApubCommunityOutbox {
38 type DataType = CommunityContext;
39 type TombstoneType = ();
40 type ApubType = GroupOutbox;
42 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
46 async fn read_from_apub_id(
48 data: &Self::DataType,
49 ) -> Result<Option<Self>, LemmyError> {
50 // Only read from database if its a local community, otherwise fetch over http
52 let community_id = data.0.id;
53 let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
54 Post::list_for_community(conn, community_id)
60 Ok(Some(ApubCommunityOutbox(post_list)))
66 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
67 // do nothing (it gets deleted automatically with the community)
71 async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
72 let mut ordered_items = vec![];
74 let actor = post.creator_id;
75 let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
79 CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
80 ordered_items.push(a);
84 r#type: OrderedCollectionType::OrderedCollection,
85 id: generate_outbox_url(&data.0.actor_id)?.into(),
90 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
91 // no tombstone for this, there is only a tombstone for the community
96 apub: &Self::ApubType,
97 data: &Self::DataType,
98 expected_domain: &Url,
99 request_counter: &mut i32,
100 ) -> Result<Self, LemmyError> {
101 verify_domains_match(expected_domain, &apub.id)?;
102 let mut outbox_activities = apub.ordered_items.clone();
103 if outbox_activities.len() > 20 {
104 outbox_activities = outbox_activities[0..20].to_vec();
107 // We intentionally ignore errors here. This is because the outbox might contain posts from old
108 // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
109 // item and only parse the ones that work.
110 for activity in outbox_activities {
112 .receive(&Data::new(data.1.clone()), request_counter)
117 // This return value is unused, so just set an empty vec
118 Ok(ApubCommunityOutbox { 0: vec![] })