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;
22 use lemmy_db::{community::Community, user::User_, DbPool};
23 use lemmy_utils::{location_info, settings::Settings, LemmyError};
24 use lemmy_websocket::LemmyContext;
25 use log::{debug, warn};
27 use serde::{export::fmt::Debug, Deserialize, Serialize};
28 use std::{collections::BTreeMap, future::Future, pin::Pin};
31 /// Sends a local activity to a single, remote actor.
33 /// * `activity` the apub activity to be sent
34 /// * `creator` the local actor which created the activity
35 /// * `inbox` the inbox url where the activity should be delivered to
36 pub async fn send_activity_single_dest<T, Kind>(
38 creator: &dyn ActorType,
40 context: &LemmyContext,
41 ) -> Result<(), LemmyError>
43 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
45 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
47 if check_is_apub_id_valid(&inbox).is_ok() {
49 "Sending activity {:?} to {}",
50 &activity.id_unchecked(),
53 send_activity_internal(
54 context.activity_queue(),
67 /// From a local community, send activity to all remote followers.
69 /// * `activity` the apub activity to send
70 /// * `community` the sending community
71 /// * `sender_shared_inbox` in case of an announce, this should be the shared inbox of the inner
72 /// activities creator, as receiving a known activity will cause an error
73 pub async fn send_to_community_followers<T, Kind>(
75 community: &Community,
76 context: &LemmyContext,
77 ) -> Result<(), LemmyError>
79 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
81 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
83 let follower_inboxes: Vec<Url> = community
84 .get_follower_inboxes(context.pool())
88 .filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname))
89 .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
90 .map(|inbox| inbox.to_owned())
93 "Sending activity {:?} to followers of {}",
94 &activity.id_unchecked(),
98 send_activity_internal(
99 context.activity_queue(),
111 /// Sends an activity from a local user to a remote community.
113 /// * `activity` the activity to send
114 /// * `creator` the creator of the activity
115 /// * `community` the destination community
117 pub async fn send_to_community<T, Kind>(
120 community: &Community,
121 context: &LemmyContext,
122 ) -> Result<(), LemmyError>
124 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
126 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
128 // if this is a local community, we need to do an announce from the community instead
131 .send_announce(activity.into_any_base()?, context)
134 let inbox = community.get_shared_inbox_url()?;
135 check_is_apub_id_valid(&inbox)?;
137 "Sending activity {:?} to community {}",
138 &activity.id_unchecked(),
141 send_activity_internal(
142 context.activity_queue(),
155 /// Sends notification to any users mentioned in a comment
157 /// * `creator` user who created the comment
158 /// * `mentions` list of inboxes of users which are mentioned in the comment
159 /// * `activity` either a `Create/Note` or `Update/Note`
160 pub async fn send_comment_mentions<T, Kind>(
164 context: &LemmyContext,
165 ) -> Result<(), LemmyError>
167 T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
169 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
172 "Sending mentions activity {:?} to {:?}",
173 &activity.id_unchecked(),
176 let mentions = mentions
178 .filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
179 .map(|i| i.to_owned())
181 send_activity_internal(
182 context.activity_queue(),
187 false, // Don't create a new DB row
193 /// Create new `SendActivityTasks`, which will deliver the given activity to inboxes, as well as
194 /// handling signing and retrying failed deliveres.
196 /// The caller of this function needs to remove any blocked domains from `to`,
197 /// using `check_is_apub_id_valid()`.
198 async fn send_activity_internal<T, Kind>(
199 activity_sender: &QueueHandle,
201 actor: &dyn ActorType,
204 insert_into_db: bool,
205 ) -> Result<(), LemmyError>
207 T: AsObject<Kind> + Extends<Kind> + Debug,
209 <T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
211 if !Settings::get().federation.enabled || inboxes.is_empty() {
215 let activity = activity.into_any_base()?;
216 let serialised_activity = serde_json::to_string(&activity)?;
218 // This is necessary because send_comment and send_comment_mentions
219 // might send the same ap_id
221 let id = activity.id().context(location_info!())?;
222 insert_activity(id, actor.user_id(), activity.clone(), true, pool).await?;
226 let message = SendActivityTask {
227 activity: serialised_activity.to_owned(),
229 actor_id: actor.actor_id()?,
230 private_key: actor.private_key().context(location_info!())?,
232 activity_sender.queue::<SendActivityTask>(message)?;
238 #[derive(Clone, Debug, Deserialize, Serialize)]
239 struct SendActivityTask {
246 /// Signs the activity with the sending actor's key, and delivers to the given inbox. Also retries
247 /// if the delivery failed.
248 impl ActixJob for SendActivityTask {
249 type State = MyState;
250 type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
251 const NAME: &'static str = "SendActivityTask";
253 const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
254 const BACKOFF: Backoff = Backoff::Exponential(2);
256 fn run(self, state: Self::State) -> Self::Future {
257 Box::pin(async move {
258 let mut headers = BTreeMap::<String, String>::new();
259 headers.insert("Content-Type".into(), "application/json".into());
260 let result = sign_and_send(
264 self.activity.clone(),
266 self.private_key.to_owned(),
270 if let Err(e) = result {
273 "Failed to send activity {} to {}",
283 pub fn create_activity_queue() -> QueueHandle {
284 // Start the application server. This guards access to to the jobs store
285 let queue_handle = create_server(Storage::new());
287 // Configure and start our workers
288 WorkerConfig::new(|| MyState {
289 client: Client::default(),
291 .register::<SendActivityTask>()
292 .start(queue_handle.clone());