2 check_is_apub_id_valid,
3 extensions::signatures::sign_and_send,
8 base::{BaseExt, Extends, ExtendsExt},
11 use anyhow::{anyhow, Context, Error};
12 use background_jobs::{
14 memory_storage::Storage,
21 use itertools::Itertools;
23 source::{community::Community, user::User_},
26 use lemmy_utils::{location_info, settings::Settings, LemmyError};
27 use lemmy_websocket::LemmyContext;
28 use log::{debug, warn};
30 use serde::{export::fmt::Debug, Deserialize, Serialize};
31 use std::{collections::BTreeMap, env, future::Future, pin::Pin};
34 /// Sends a local activity to a single, remote actor.
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>(
41 creator: &dyn ActorType,
43 context: &LemmyContext,
44 ) -> Result<(), LemmyError>
46 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
48 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
50 if check_is_apub_id_valid(&inbox).is_ok() {
52 "Sending activity {:?} to {}",
53 &activity.id_unchecked(),
56 send_activity_internal(
57 context.activity_queue(),
71 /// From a local community, send activity to all remote followers.
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>(
79 community: &Community,
80 context: &LemmyContext,
81 ) -> Result<(), LemmyError>
83 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
85 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
87 let follower_inboxes: Vec<Url> = community
88 .get_follower_inboxes(context.pool())
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())
97 "Sending activity {:?} to followers of {}",
98 &activity.id_unchecked(),
102 send_activity_internal(
103 context.activity_queue(),
116 /// Sends an activity from a local user to a remote community.
118 /// * `activity` the activity to send
119 /// * `creator` the creator of the activity
120 /// * `community` the destination community
122 pub(crate) async fn send_to_community<T, Kind>(
125 community: &Community,
126 context: &LemmyContext,
127 ) -> Result<(), LemmyError>
129 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
131 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
133 // if this is a local community, we need to do an announce from the community instead
136 .send_announce(activity.into_any_base()?, context)
139 let inbox = community.get_shared_inbox_url()?;
140 check_is_apub_id_valid(&inbox)?;
142 "Sending activity {:?} to community {}",
143 &activity.id_unchecked(),
146 send_activity_internal(
147 context.activity_queue(),
161 /// Sends notification to any users mentioned in a comment
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>(
170 context: &LemmyContext,
171 ) -> Result<(), LemmyError>
173 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
175 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
178 "Sending mentions activity {:?} to {:?}",
179 &activity.id_unchecked(),
182 let mentions = mentions
184 .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
185 .map(|i| i.to_owned())
187 send_activity_internal(
188 context.activity_queue(),
193 false, // Don't create a new DB row
200 /// Create new `SendActivityTasks`, which will deliver the given activity to inboxes, as well as
201 /// handling signing and retrying failed deliveres.
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,
208 actor: &dyn ActorType,
211 insert_into_db: bool,
213 ) -> Result<(), LemmyError>
215 T: AsObject<Kind> + Extends<Kind> + Debug,
217 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
219 if !Settings::get().federation.enabled || inboxes.is_empty() {
223 let activity = activity.into_any_base()?;
224 let serialised_activity = serde_json::to_string(&activity)?;
226 // This is necessary because send_comment and send_comment_mentions
227 // might send the same ap_id
229 let id = activity.id().context(location_info!())?;
230 insert_activity(id, activity.clone(), true, sensitive, pool).await?;
234 let message = SendActivityTask {
235 activity: serialised_activity.to_owned(),
237 actor_id: actor.actor_id()?,
238 private_key: actor.private_key().context(location_info!())?,
240 if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
241 do_send(message, &Client::default()).await?;
243 activity_sender.queue::<SendActivityTask>(message)?;
250 #[derive(Clone, Debug, Deserialize, Serialize)]
251 struct SendActivityTask {
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";
265 const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
266 const BACKOFF: Backoff = Backoff::Exponential(2);
268 fn run(self, state: Self::State) -> Self::Future {
269 Box::pin(async move { do_send(self, &state.client).await })
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(
280 task.activity.clone(),
282 task.private_key.to_owned(),
286 if let Err(e) = result {
289 "Failed to send activity {} to {}",
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());
301 // Configure and start our workers
302 WorkerConfig::new(|| MyState {
303 client: Client::default(),
305 .register::<SendActivityTask>()
306 .start(queue_handle.clone());