2 check_is_apub_id_valid,
3 extensions::signatures::sign_and_send,
7 APUB_JSON_CONTENT_TYPE,
10 base::{BaseExt, Extends, ExtendsExt},
13 use anyhow::{anyhow, Context, Error};
14 use background_jobs::{
16 memory_storage::Storage,
23 use itertools::Itertools;
24 use lemmy_db_queries::DbPool;
25 use lemmy_db_schema::source::{community::Community, user::User_};
26 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
27 use lemmy_websocket::LemmyContext;
28 use log::{debug, warn};
30 use serde::{Deserialize, Serialize};
31 use std::{collections::BTreeMap, env, fmt::Debug, 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().map(|i| i.to_string()),
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_or_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 // Don't send anything to ourselves
224 let hostname = Settings::get().get_hostname_without_port()?;
225 let inboxes: Vec<&Url> = inboxes
227 .filter(|i| i.domain().expect("valid inbox url") != hostname)
230 let activity = activity.into_any_base()?;
231 let serialised_activity = serde_json::to_string(&activity)?;
233 // This is necessary because send_comment and send_comment_mentions
234 // might send the same ap_id
236 let id = activity.id().context(location_info!())?;
237 insert_activity(id, activity.clone(), true, sensitive, pool).await?;
241 let message = SendActivityTask {
242 activity: serialised_activity.to_owned(),
244 actor_id: actor.actor_id(),
245 private_key: actor.private_key().context(location_info!())?,
247 if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
248 do_send(message, &Client::default()).await?;
250 activity_sender.queue::<SendActivityTask>(message)?;
257 #[derive(Clone, Debug, Deserialize, Serialize)]
258 struct SendActivityTask {
265 /// Signs the activity with the sending actor's key, and delivers to the given inbox. Also retries
266 /// if the delivery failed.
267 impl ActixJob for SendActivityTask {
268 type State = MyState;
269 type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
270 const NAME: &'static str = "SendActivityTask";
272 const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
273 const BACKOFF: Backoff = Backoff::Exponential(2);
275 fn run(self, state: Self::State) -> Self::Future {
276 Box::pin(async move { do_send(self, &state.client).await })
280 async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
281 let mut headers = BTreeMap::<String, String>::new();
282 headers.insert("Content-Type".into(), APUB_JSON_CONTENT_TYPE.to_string());
283 let result = sign_and_send(
287 task.activity.clone(),
289 task.private_key.to_owned(),
293 if let Err(e) = result {
296 "Failed to send activity {} to {}",
304 pub fn create_activity_queue() -> QueueHandle {
305 // Start the application server. This guards access to to the jobs store
306 let queue_handle = create_server(Storage::new());
308 // Configure and start our workers
309 WorkerConfig::new(|| MyState {
310 client: Client::default(),
312 .register::<SendActivityTask>()
313 .start(queue_handle.clone());