]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Clippy fixes.
[lemmy.git] / crates / apub / src / collections / community_outbox.rs
1 use crate::{
2   activity_lists::AnnouncableActivities,
3   collections::CommunityContext,
4   generate_outbox_url,
5   objects::post::ApubPost,
6   protocol::{
7     activities::community::announce::AnnounceActivity,
8     collections::group_outbox::GroupOutbox,
9   },
10 };
11 use activitystreams_kinds::collection::OrderedCollectionType;
12 use chrono::NaiveDateTime;
13 use lemmy_api_common::blocking;
14 use lemmy_apub_lib::{
15   data::Data,
16   traits::{ActivityHandler, ApubObject},
17   verify::verify_domains_match,
18 };
19 use lemmy_db_schema::source::post::Post;
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   #[tracing::instrument(skip_all)]
37   async fn read_from_apub_id(
38     _object_id: Url,
39     data: &Self::DataType,
40   ) -> Result<Option<Self>, LemmyError> {
41     // Only read from database if its a local community, otherwise fetch over http
42     if data.0.local {
43       let community_id = data.0.id;
44       let post_list: Vec<ApubPost> = blocking(data.1.pool(), move |conn| {
45         Post::list_for_community(conn, community_id)
46       })
47       .await??
48       .into_iter()
49       .map(Into::into)
50       .collect();
51       Ok(Some(ApubCommunityOutbox(post_list)))
52     } else {
53       Ok(None)
54     }
55   }
56
57   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
58     // do nothing (it gets deleted automatically with the community)
59     Ok(())
60   }
61
62   #[tracing::instrument(skip_all)]
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 page = post.into_apub(&data.1).await?;
67       let announcable = AnnouncableActivities::Page(page);
68       let announce = AnnounceActivity::new(announcable, &data.0, &data.1)?;
69       ordered_items.push(announce);
70     }
71
72     Ok(GroupOutbox {
73       r#type: OrderedCollectionType::OrderedCollection,
74       id: generate_outbox_url(&data.0.actor_id)?.into(),
75       total_items: ordered_items.len() as i32,
76       ordered_items,
77     })
78   }
79
80   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
81     // no tombstone for this, there is only a tombstone for the community
82     unimplemented!()
83   }
84
85   #[tracing::instrument(skip_all)]
86   async fn verify(
87     group_outbox: &GroupOutbox,
88     expected_domain: &Url,
89     _context: &CommunityContext,
90     _request_counter: &mut i32,
91   ) -> Result<(), LemmyError> {
92     verify_domains_match(expected_domain, &group_outbox.id)?;
93     Ok(())
94   }
95
96   #[tracing::instrument(skip_all)]
97   async fn from_apub(
98     apub: Self::ApubType,
99     data: &Self::DataType,
100     request_counter: &mut i32,
101   ) -> Result<Self, LemmyError> {
102     let mut outbox_activities = apub.ordered_items;
103     if outbox_activities.len() > 20 {
104       outbox_activities = outbox_activities[0..20].to_vec();
105     }
106
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     let data = Data::new(data.1.clone());
111     for activity in outbox_activities {
112       let verify = activity.verify(&data, request_counter).await;
113       if verify.is_ok() {
114         activity.receive(&data, request_counter).await.ok();
115       }
116     }
117
118     // This return value is unused, so just set an empty vec
119     Ok(ApubCommunityOutbox(Vec::new()))
120   }
121
122   type DbType = ();
123 }