]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Move activity structs to protocol folder
[lemmy.git] / crates / apub / src / collections / community_outbox.rs
1 use activitystreams::collection::kind::OrderedCollectionType;
2 use chrono::NaiveDateTime;
3 use url::Url;
4
5 use lemmy_api_common::blocking;
6 use lemmy_apub_lib::{
7   data::Data,
8   traits::{ActivityHandler, ApubObject},
9   verify::verify_domains_match,
10 };
11 use lemmy_db_schema::{
12   source::{person::Person, post::Post},
13   traits::Crud,
14 };
15 use lemmy_utils::LemmyError;
16
17 use crate::{
18   collections::CommunityContext,
19   generate_outbox_url,
20   objects::{person::ApubPerson, post::ApubPost},
21   protocol::{
22     activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
23     collections::group_outbox::GroupOutbox,
24   },
25 };
26
27 #[derive(Clone, Debug)]
28 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
29
30 #[async_trait::async_trait(?Send)]
31 impl ApubObject for ApubCommunityOutbox {
32   type DataType = CommunityContext;
33   type TombstoneType = ();
34   type ApubType = GroupOutbox;
35
36   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
37     None
38   }
39
40   async fn read_from_apub_id(
41     _object_id: Url,
42     data: &Self::DataType,
43   ) -> Result<Option<Self>, LemmyError> {
44     // Only read from database if its a local community, otherwise fetch over http
45     if data.0.local {
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)
49       })
50       .await??
51       .into_iter()
52       .map(Into::into)
53       .collect();
54       Ok(Some(ApubCommunityOutbox(post_list)))
55     } else {
56       Ok(None)
57     }
58   }
59
60   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
61     // do nothing (it gets deleted automatically with the community)
62     Ok(())
63   }
64
65   async fn to_apub(&self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
66     let mut ordered_items = vec![];
67     for post in &self.0 {
68       let actor = post.creator_id;
69       let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
70         .await??
71         .into();
72       let a =
73         CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
74       ordered_items.push(a);
75     }
76
77     Ok(GroupOutbox {
78       r#type: OrderedCollectionType::OrderedCollection,
79       id: generate_outbox_url(&data.0.actor_id)?.into(),
80       total_items: ordered_items.len() as i32,
81       ordered_items,
82     })
83   }
84
85   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
86     // no tombstone for this, there is only a tombstone for the community
87     unimplemented!()
88   }
89
90   async fn from_apub(
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();
100     }
101
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 {
106       activity
107         .receive(&Data::new(data.1.clone()), request_counter)
108         .await
109         .ok();
110     }
111
112     // This return value is unused, so just set an empty vec
113     Ok(ApubCommunityOutbox { 0: vec![] })
114   }
115 }