]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/announce.rs
Extract Activitypub logic into separate library (#2288)
[lemmy.git] / crates / apub / src / activities / community / announce.rs
1 use crate::{
2   activities::{generate_activity_id, send_lemmy_activity, verify_is_public},
3   activity_lists::AnnouncableActivities,
4   insert_activity,
5   objects::community::ApubCommunity,
6   protocol::{
7     activities::{community::announce::AnnounceActivity, CreateOrUpdateType},
8     IdOrNestedObject,
9   },
10   ActorType,
11 };
12 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
13 use activitystreams_kinds::{activity::AnnounceType, public};
14 use lemmy_utils::error::LemmyError;
15 use lemmy_websocket::LemmyContext;
16 use tracing::debug;
17 use url::Url;
18
19 #[async_trait::async_trait(?Send)]
20 pub(crate) trait GetCommunity {
21   async fn get_community(
22     &self,
23     context: &LemmyContext,
24     request_counter: &mut i32,
25   ) -> Result<ApubCommunity, LemmyError>;
26 }
27
28 impl AnnounceActivity {
29   pub(crate) fn new(
30     object: AnnouncableActivities,
31     community: &ApubCommunity,
32     context: &LemmyContext,
33   ) -> Result<AnnounceActivity, LemmyError> {
34     Ok(AnnounceActivity {
35       actor: ObjectId::new(community.actor_id()),
36       to: vec![public()],
37       object: IdOrNestedObject::NestedObject(object),
38       cc: vec![community.followers_url.clone().into()],
39       kind: AnnounceType::Announce,
40       id: generate_activity_id(
41         &AnnounceType::Announce,
42         &context.settings().get_protocol_and_hostname(),
43       )?,
44       unparsed: Default::default(),
45     })
46   }
47
48   #[tracing::instrument(skip_all)]
49   pub async fn send(
50     object: AnnouncableActivities,
51     community: &ApubCommunity,
52     context: &LemmyContext,
53   ) -> Result<(), LemmyError> {
54     let announce = AnnounceActivity::new(object.clone(), community, context)?;
55     let inboxes = community.get_follower_inboxes(context).await?;
56     send_lemmy_activity(
57       context,
58       &announce,
59       &announce.id,
60       community,
61       inboxes.clone(),
62       false,
63     )
64     .await?;
65
66     // Pleroma and Mastodon can't handle activities like Announce/Create/Page. So for
67     // compatibility, we also send Announce/Page so that they can follow Lemmy communities.
68     use AnnouncableActivities::*;
69     let object = match object {
70       CreateOrUpdatePost(c) if c.kind == CreateOrUpdateType::Create => Page(c.object),
71       _ => return Ok(()),
72     };
73     let announce_compat = AnnounceActivity::new(object, community, context)?;
74     send_lemmy_activity(
75       context,
76       &announce_compat,
77       &announce_compat.id,
78       community,
79       inboxes,
80       false,
81     )
82     .await?;
83     Ok(())
84   }
85 }
86
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for AnnounceActivity {
89   type DataType = LemmyContext;
90   type Error = LemmyError;
91
92   fn id(&self) -> &Url {
93     &self.id
94   }
95
96   fn actor(&self) -> &Url {
97     self.actor.inner()
98   }
99
100   #[tracing::instrument(skip_all)]
101   async fn verify(
102     &self,
103     _context: &Data<LemmyContext>,
104     _request_counter: &mut i32,
105   ) -> Result<(), LemmyError> {
106     verify_is_public(&self.to, &self.cc)?;
107     Ok(())
108   }
109
110   #[tracing::instrument(skip_all)]
111   async fn receive(
112     self,
113     context: &Data<LemmyContext>,
114     request_counter: &mut i32,
115   ) -> Result<(), LemmyError> {
116     let object = self.object.object(context, request_counter).await?;
117     // we have to verify this here in order to avoid fetching the object twice over http
118     object.verify(context, request_counter).await?;
119
120     // TODO: this can probably be implemented in a cleaner way
121     match object {
122       // Dont insert these into activities table, as they are not activities.
123       AnnouncableActivities::Page(_) => {}
124       _ => {
125         let object_value = serde_json::to_value(&object)?;
126         let insert =
127           insert_activity(object.id(), object_value, false, true, context.pool()).await?;
128         if !insert {
129           debug!(
130             "Received duplicate activity in announce {}",
131             object.id().to_string()
132           );
133           return Ok(());
134         }
135       }
136     }
137     object.receive(context, request_counter).await
138   }
139 }