]> Untitled Git - lemmy.git/blob - lemmy_apub/src/activity_queue.rs
Merge branch 'drone-io-dess' into move_views_to_diesel_drone
[lemmy.git] / lemmy_apub / src / activity_queue.rs
1 use crate::{
2   check_is_apub_id_valid,
3   extensions::signatures::sign_and_send,
4   insert_activity,
5   ActorType,
6 };
7 use activitystreams::{
8   base::{BaseExt, Extends, ExtendsExt},
9   object::AsObject,
10 };
11 use anyhow::{anyhow, Context, Error};
12 use background_jobs::{
13   create_server,
14   memory_storage::Storage,
15   ActixJob,
16   Backoff,
17   MaxRetries,
18   QueueHandle,
19   WorkerConfig,
20 };
21 use itertools::Itertools;
22 use lemmy_db::{
23   source::{community::Community, user::User_},
24   DbPool,
25 };
26 use lemmy_utils::{location_info, settings::Settings, LemmyError};
27 use lemmy_websocket::LemmyContext;
28 use log::{debug, warn};
29 use reqwest::Client;
30 use serde::{export::fmt::Debug, Deserialize, Serialize};
31 use std::{collections::BTreeMap, env, future::Future, pin::Pin};
32 use url::Url;
33
34 /// Sends a local activity to a single, remote actor.
35 ///
36 /// * `activity` the apub activity to be sent
37 /// * `creator` the local actor which created the activity
38 /// * `inbox` the inbox url where the activity should be delivered to
39 pub(crate) async fn send_activity_single_dest<T, Kind>(
40   activity: T,
41   creator: &dyn ActorType,
42   inbox: Url,
43   context: &LemmyContext,
44 ) -> Result<(), LemmyError>
45 where
46   T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
47   Kind: Serialize,
48   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
49 {
50   if check_is_apub_id_valid(&inbox).is_ok() {
51     debug!(
52       "Sending activity {:?} to {}",
53       &activity.id_unchecked(),
54       &inbox
55     );
56     send_activity_internal(
57       context.activity_queue(),
58       activity,
59       creator,
60       vec![inbox],
61       context.pool(),
62       true,
63       true,
64     )
65     .await?;
66   }
67
68   Ok(())
69 }
70
71 /// From a local community, send activity to all remote followers.
72 ///
73 /// * `activity` the apub activity to send
74 /// * `community` the sending community
75 /// * `sender_shared_inbox` in case of an announce, this should be the shared inbox of the inner
76 ///                         activities creator, as receiving a known activity will cause an error
77 pub(crate) async fn send_to_community_followers<T, Kind>(
78   activity: T,
79   community: &Community,
80   context: &LemmyContext,
81 ) -> Result<(), LemmyError>
82 where
83   T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
84   Kind: Serialize,
85   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
86 {
87   let follower_inboxes: Vec<Url> = community
88     .get_follower_inboxes(context.pool())
89     .await?
90     .iter()
91     .unique()
92     .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname))
93     .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
94     .map(|inbox| inbox.to_owned())
95     .collect();
96   debug!(
97     "Sending activity {:?} to followers of {}",
98     &activity.id_unchecked(),
99     &community.actor_id
100   );
101
102   send_activity_internal(
103     context.activity_queue(),
104     activity,
105     community,
106     follower_inboxes,
107     context.pool(),
108     true,
109     false,
110   )
111   .await?;
112
113   Ok(())
114 }
115
116 /// Sends an activity from a local user to a remote community.
117 ///
118 /// * `activity` the activity to send
119 /// * `creator` the creator of the activity
120 /// * `community` the destination community
121 ///
122 pub(crate) async fn send_to_community<T, Kind>(
123   activity: T,
124   creator: &User_,
125   community: &Community,
126   context: &LemmyContext,
127 ) -> Result<(), LemmyError>
128 where
129   T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
130   Kind: Serialize,
131   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
132 {
133   // if this is a local community, we need to do an announce from the community instead
134   if community.local {
135     community
136       .send_announce(activity.into_any_base()?, context)
137       .await?;
138   } else {
139     let inbox = community.get_shared_inbox_url()?;
140     check_is_apub_id_valid(&inbox)?;
141     debug!(
142       "Sending activity {:?} to community {}",
143       &activity.id_unchecked(),
144       &community.actor_id
145     );
146     send_activity_internal(
147       context.activity_queue(),
148       activity,
149       creator,
150       vec![inbox],
151       context.pool(),
152       true,
153       false,
154     )
155     .await?;
156   }
157
158   Ok(())
159 }
160
161 /// Sends notification to any users mentioned in a comment
162 ///
163 /// * `creator` user who created the comment
164 /// * `mentions` list of inboxes of users which are mentioned in the comment
165 /// * `activity` either a `Create/Note` or `Update/Note`
166 pub(crate) async fn send_comment_mentions<T, Kind>(
167   creator: &User_,
168   mentions: Vec<Url>,
169   activity: T,
170   context: &LemmyContext,
171 ) -> Result<(), LemmyError>
172 where
173   T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
174   Kind: Serialize,
175   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
176 {
177   debug!(
178     "Sending mentions activity {:?} to {:?}",
179     &activity.id_unchecked(),
180     &mentions
181   );
182   let mentions = mentions
183     .iter()
184     .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
185     .map(|i| i.to_owned())
186     .collect();
187   send_activity_internal(
188     context.activity_queue(),
189     activity,
190     creator,
191     mentions,
192     context.pool(),
193     false, // Don't create a new DB row
194     false,
195   )
196   .await?;
197   Ok(())
198 }
199
200 /// Create new `SendActivityTasks`, which will deliver the given activity to inboxes, as well as
201 /// handling signing and retrying failed deliveres.
202 ///
203 /// The caller of this function needs to remove any blocked domains from `to`,
204 /// using `check_is_apub_id_valid()`.
205 async fn send_activity_internal<T, Kind>(
206   activity_sender: &QueueHandle,
207   activity: T,
208   actor: &dyn ActorType,
209   inboxes: Vec<Url>,
210   pool: &DbPool,
211   insert_into_db: bool,
212   sensitive: bool,
213 ) -> Result<(), LemmyError>
214 where
215   T: AsObject<Kind> + Extends<Kind> + Debug,
216   Kind: Serialize,
217   <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
218 {
219   if !Settings::get().federation.enabled || inboxes.is_empty() {
220     return Ok(());
221   }
222
223   let activity = activity.into_any_base()?;
224   let serialised_activity = serde_json::to_string(&activity)?;
225
226   // This is necessary because send_comment and send_comment_mentions
227   // might send the same ap_id
228   if insert_into_db {
229     let id = activity.id().context(location_info!())?;
230     insert_activity(id, activity.clone(), true, sensitive, pool).await?;
231   }
232
233   for i in inboxes {
234     let message = SendActivityTask {
235       activity: serialised_activity.to_owned(),
236       inbox: i,
237       actor_id: actor.actor_id()?,
238       private_key: actor.private_key().context(location_info!())?,
239     };
240     if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
241       do_send(message, &Client::default()).await?;
242     } else {
243       activity_sender.queue::<SendActivityTask>(message)?;
244     }
245   }
246
247   Ok(())
248 }
249
250 #[derive(Clone, Debug, Deserialize, Serialize)]
251 struct SendActivityTask {
252   activity: String,
253   inbox: Url,
254   actor_id: Url,
255   private_key: String,
256 }
257
258 /// Signs the activity with the sending actor's key, and delivers to the given inbox. Also retries
259 /// if the delivery failed.
260 impl ActixJob for SendActivityTask {
261   type State = MyState;
262   type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
263   const NAME: &'static str = "SendActivityTask";
264
265   const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
266   const BACKOFF: Backoff = Backoff::Exponential(2);
267
268   fn run(self, state: Self::State) -> Self::Future {
269     Box::pin(async move { do_send(self, &state.client).await })
270   }
271 }
272
273 async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
274   let mut headers = BTreeMap::<String, String>::new();
275   headers.insert("Content-Type".into(), "application/json".into());
276   let result = sign_and_send(
277     client,
278     headers,
279     &task.inbox,
280     task.activity.clone(),
281     &task.actor_id,
282     task.private_key.to_owned(),
283   )
284   .await;
285
286   if let Err(e) = result {
287     warn!("{}", e);
288     return Err(anyhow!(
289       "Failed to send activity {} to {}",
290       &task.activity,
291       task.inbox
292     ));
293   }
294   Ok(())
295 }
296
297 pub fn create_activity_queue() -> QueueHandle {
298   // Start the application server. This guards access to to the jobs store
299   let queue_handle = create_server(Storage::new());
300
301   // Configure and start our workers
302   WorkerConfig::new(|| MyState {
303     client: Client::default(),
304   })
305   .register::<SendActivityTask>()
306   .start(queue_handle.clone());
307
308   queue_handle
309 }
310
311 #[derive(Clone)]
312 struct MyState {
313   pub client: Client,
314 }