]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Merge pull request #1877 from LemmyNet/refactor-apub-2
[lemmy.git] / crates / apub / src / collections / community_outbox.rs
1 use crate::{
2   collections::CommunityContext,
3   generate_outbox_url,
4   objects::{person::ApubPerson, post::ApubPost},
5   protocol::{
6     activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
7     collections::group_outbox::GroupOutbox,
8   },
9 };
10 use activitystreams::collection::kind::OrderedCollectionType;
11 use chrono::NaiveDateTime;
12 use lemmy_api_common::blocking;
13 use lemmy_apub_lib::{
14   data::Data,
15   traits::{ActivityHandler, ApubObject},
16   verify::verify_domains_match,
17 };
18 use lemmy_db_schema::{
19   source::{person::Person, post::Post},
20   traits::Crud,
21 };
22 use lemmy_utils::LemmyError;
23 use url::Url;
24
25 #[derive(Clone, Debug)]
26 pub(crate) struct ApubCommunityOutbox(Vec<ApubPost>);
27
28 #[async_trait::async_trait(?Send)]
29 impl ApubObject for ApubCommunityOutbox {
30   type DataType = CommunityContext;
31   type TombstoneType = ();
32   type ApubType = GroupOutbox;
33
34   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
35     None
36   }
37
38   async fn read_from_apub_id(
39     _object_id: Url,
40     data: &Self::DataType,
41   ) -> Result<Option<Self>, LemmyError> {
42     // Only read from database if its a local community, otherwise fetch over http
43     if data.0.local {
44       let community_id = data.0.id;
45       let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
46         Post::list_for_community(conn, community_id)
47       })
48       .await??
49       .into_iter()
50       .map(Into::into)
51       .collect();
52       Ok(Some(ApubCommunityOutbox(post_list)))
53     } else {
54       Ok(None)
55     }
56   }
57
58   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
59     // do nothing (it gets deleted automatically with the community)
60     Ok(())
61   }
62
63   async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
64     let mut ordered_items = vec![];
65     for post in self.0 {
66       let actor = post.creator_id;
67       let actor: ApubPerson = blocking(data.1.pool(), move |conn| Person::read(conn, actor))
68         .await??
69         .into();
70       let a =
71         CreateOrUpdatePost::new(post, &actor, &data.0, CreateOrUpdateType::Create, &data.1).await?;
72       ordered_items.push(a);
73     }
74
75     Ok(GroupOutbox {
76       r#type: OrderedCollectionType::OrderedCollection,
77       id: generate_outbox_url(&data.0.actor_id)?.into(),
78       total_items: ordered_items.len() as i32,
79       ordered_items,
80     })
81   }
82
83   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
84     // no tombstone for this, there is only a tombstone for the community
85     unimplemented!()
86   }
87
88   async fn verify(
89     group_outbox: &GroupOutbox,
90     expected_domain: &Url,
91     _context: &CommunityContext,
92     _request_counter: &mut i32,
93   ) -> Result<(), LemmyError> {
94     verify_domains_match(expected_domain, &group_outbox.id)?;
95     Ok(())
96   }
97
98   async fn from_apub(
99     apub: Self::ApubType,
100     data: &Self::DataType,
101     request_counter: &mut i32,
102   ) -> Result<Self, LemmyError> {
103     let mut outbox_activities = apub.ordered_items;
104     if outbox_activities.len() > 20 {
105       outbox_activities = outbox_activities[0..20].to_vec();
106     }
107
108     // We intentionally ignore errors here. This is because the outbox might contain posts from old
109     // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
110     // item and only parse the ones that work.
111     for activity in outbox_activities {
112       activity
113         .receive(&Data::new(data.1.clone()), request_counter)
114         .await
115         .ok();
116     }
117
118     // This return value is unused, so just set an empty vec
119     Ok(ApubCommunityOutbox { 0: vec![] })
120   }
121 }