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