]> Untitled Git - lemmy.git/blob - crates/apub/src/collections/community_outbox.rs
Merge pull request #1918 from LemmyNet/fix-smithereen-webfinger
[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::collection::kind::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   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 into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
62     let mut ordered_items = vec![];
63     for post in self.0 {
64       let page = post.into_apub(&data.1).await?;
65       let announcable = AnnouncableActivities::Page(page);
66       let announce = AnnounceActivity::new(announcable, &data.0, &data.1)?;
67       ordered_items.push(announce);
68     }
69
70     Ok(GroupOutbox {
71       r#type: OrderedCollectionType::OrderedCollection,
72       id: generate_outbox_url(&data.0.actor_id)?.into(),
73       total_items: ordered_items.len() as i32,
74       ordered_items,
75     })
76   }
77
78   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
79     // no tombstone for this, there is only a tombstone for the community
80     unimplemented!()
81   }
82
83   async fn verify(
84     group_outbox: &GroupOutbox,
85     expected_domain: &Url,
86     _context: &CommunityContext,
87     _request_counter: &mut i32,
88   ) -> Result<(), LemmyError> {
89     verify_domains_match(expected_domain, &group_outbox.id)?;
90     Ok(())
91   }
92
93   async fn from_apub(
94     apub: Self::ApubType,
95     data: &Self::DataType,
96     request_counter: &mut i32,
97   ) -> Result<Self, LemmyError> {
98     let mut outbox_activities = apub.ordered_items;
99     if outbox_activities.len() > 20 {
100       outbox_activities = outbox_activities[0..20].to_vec();
101     }
102
103     // We intentionally ignore errors here. This is because the outbox might contain posts from old
104     // Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
105     // item and only parse the ones that work.
106     let data = Data::new(data.1.clone());
107     for activity in outbox_activities {
108       let verify = activity.verify(&data, request_counter).await;
109       if verify.is_ok() {
110         activity.receive(&data, request_counter).await.ok();
111       }
112     }
113
114     // This return value is unused, so just set an empty vec
115     Ok(ApubCommunityOutbox { 0: vec![] })
116   }
117 }