2020-09-30 16:19:14 +00:00
|
|
|
use crate::{
|
|
|
|
check_is_apub_id_valid,
|
|
|
|
community::do_announce,
|
2020-10-01 20:57:47 +00:00
|
|
|
extensions::signatures::sign_and_send,
|
2020-09-30 16:19:14 +00:00
|
|
|
insert_activity,
|
|
|
|
ActorType,
|
|
|
|
};
|
2020-08-31 13:48:02 +00:00
|
|
|
use activitystreams::{
|
2020-10-06 16:28:31 +00:00
|
|
|
base::{BaseExt, Extends, ExtendsExt},
|
2020-08-31 13:48:02 +00:00
|
|
|
object::AsObject,
|
|
|
|
};
|
|
|
|
use anyhow::{anyhow, Context, Error};
|
|
|
|
use background_jobs::{
|
|
|
|
create_server,
|
|
|
|
memory_storage::Storage,
|
|
|
|
ActixJob,
|
|
|
|
Backoff,
|
|
|
|
MaxRetries,
|
|
|
|
QueueHandle,
|
|
|
|
WorkerConfig,
|
|
|
|
};
|
2020-09-30 16:19:14 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use lemmy_db::{community::Community, user::User_, DbPool};
|
2020-09-01 14:25:34 +00:00
|
|
|
use lemmy_utils::{location_info, settings::Settings, LemmyError};
|
2020-09-30 16:19:14 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2020-10-06 16:28:31 +00:00
|
|
|
use log::{debug, warn};
|
2020-09-29 13:10:55 +00:00
|
|
|
use reqwest::Client;
|
2020-10-06 16:28:31 +00:00
|
|
|
use serde::{export::fmt::Debug, Deserialize, Serialize};
|
2020-09-29 13:10:55 +00:00
|
|
|
use std::{collections::BTreeMap, future::Future, pin::Pin};
|
2020-08-31 13:48:02 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
pub async fn send_activity_single_dest<T, Kind>(
|
|
|
|
activity: T,
|
|
|
|
creator: &dyn ActorType,
|
|
|
|
to: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
if check_is_apub_id_valid(&to).is_ok() {
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!("Sending activity {:?} to {}", &activity.id_unchecked(), &to);
|
2020-09-30 16:19:14 +00:00
|
|
|
send_activity_internal(
|
|
|
|
context.activity_queue(),
|
|
|
|
activity,
|
|
|
|
creator,
|
|
|
|
vec![to],
|
|
|
|
context.pool(),
|
2020-10-06 15:19:01 +00:00
|
|
|
true,
|
2020-09-30 16:19:14 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_to_community_followers<T, Kind>(
|
|
|
|
activity: T,
|
|
|
|
community: &Community,
|
|
|
|
context: &LemmyContext,
|
|
|
|
sender_shared_inbox: Option<Url>,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
// dont send to the local instance, nor to the instance where the activity originally came from,
|
|
|
|
// because that would result in a database error (same data inserted twice)
|
|
|
|
let community_shared_inbox = community.get_shared_inbox_url()?;
|
|
|
|
let to: Vec<Url> = community
|
|
|
|
.get_follower_inboxes(context.pool())
|
|
|
|
.await?
|
|
|
|
.iter()
|
|
|
|
.filter(|inbox| Some(inbox) != sender_shared_inbox.as_ref().as_ref())
|
|
|
|
.filter(|inbox| inbox != &&community_shared_inbox)
|
|
|
|
.filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
|
|
|
|
.unique()
|
|
|
|
.map(|inbox| inbox.to_owned())
|
|
|
|
.collect();
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!(
|
|
|
|
"Sending activity {:?} to followers of {}",
|
|
|
|
&activity.id_unchecked(),
|
|
|
|
&community.actor_id
|
|
|
|
);
|
2020-09-30 16:19:14 +00:00
|
|
|
|
|
|
|
send_activity_internal(
|
|
|
|
context.activity_queue(),
|
|
|
|
activity,
|
|
|
|
community,
|
|
|
|
to,
|
|
|
|
context.pool(),
|
2020-10-06 15:19:01 +00:00
|
|
|
true,
|
2020-09-30 16:19:14 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_to_community<T, Kind>(
|
|
|
|
creator: &User_,
|
|
|
|
community: &Community,
|
|
|
|
activity: T,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
// if this is a local community, we need to do an announce from the community instead
|
|
|
|
if community.local {
|
|
|
|
do_announce(activity.into_any_base()?, &community, creator, context).await?;
|
|
|
|
} else {
|
|
|
|
let inbox = community.get_shared_inbox_url()?;
|
|
|
|
check_is_apub_id_valid(&inbox)?;
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!(
|
|
|
|
"Sending activity {:?} to community {}",
|
|
|
|
&activity.id_unchecked(),
|
|
|
|
&community.actor_id
|
|
|
|
);
|
2020-09-30 16:19:14 +00:00
|
|
|
send_activity_internal(
|
|
|
|
context.activity_queue(),
|
|
|
|
activity,
|
|
|
|
creator,
|
|
|
|
vec![inbox],
|
|
|
|
context.pool(),
|
2020-10-06 15:19:01 +00:00
|
|
|
true,
|
2020-09-30 16:19:14 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_comment_mentions<T, Kind>(
|
|
|
|
creator: &User_,
|
|
|
|
mentions: Vec<Url>,
|
|
|
|
activity: T,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2020-10-06 16:28:31 +00:00
|
|
|
dbg!(&mentions, &activity.id_unchecked());
|
|
|
|
debug!(
|
|
|
|
"Sending mentions activity {:?} to {:?}",
|
|
|
|
&activity.id_unchecked(),
|
|
|
|
&mentions
|
|
|
|
);
|
2020-09-30 16:19:14 +00:00
|
|
|
let mentions = mentions
|
|
|
|
.iter()
|
|
|
|
.filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
|
|
|
|
.map(|i| i.to_owned())
|
|
|
|
.collect();
|
|
|
|
send_activity_internal(
|
|
|
|
context.activity_queue(),
|
|
|
|
activity,
|
|
|
|
creator,
|
|
|
|
mentions,
|
|
|
|
context.pool(),
|
2020-10-06 15:19:01 +00:00
|
|
|
false, // Don't create a new DB row
|
2020-09-30 16:19:14 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously sends the given `activity` from `actor` to every inbox URL in `to`.
|
|
|
|
///
|
|
|
|
/// The caller of this function needs to remove any blocked domains from `to`,
|
|
|
|
/// using `check_is_apub_id_valid()`.
|
|
|
|
async fn send_activity_internal<T, Kind>(
|
2020-08-31 13:48:02 +00:00
|
|
|
activity_sender: &QueueHandle,
|
|
|
|
activity: T,
|
|
|
|
actor: &dyn ActorType,
|
|
|
|
to: Vec<Url>,
|
2020-09-30 16:19:14 +00:00
|
|
|
pool: &DbPool,
|
2020-10-06 15:19:01 +00:00
|
|
|
insert_into_db: bool,
|
2020-08-31 13:48:02 +00:00
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug,
|
2020-08-31 13:48:02 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2020-10-06 12:58:37 +00:00
|
|
|
if !Settings::get().federation.enabled || to.is_empty() {
|
2020-08-31 13:48:02 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
for to_url in &to {
|
2020-09-30 16:19:14 +00:00
|
|
|
assert!(check_is_apub_id_valid(&to_url).is_ok());
|
2020-08-31 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
let activity = activity.into_any_base()?;
|
|
|
|
let serialised_activity = serde_json::to_string(&activity)?;
|
2020-10-06 15:19:01 +00:00
|
|
|
|
|
|
|
// This is necessary because send_comment and send_comment_mentions
|
|
|
|
// might send the same ap_id
|
|
|
|
if insert_into_db {
|
|
|
|
insert_activity(actor.user_id(), activity.clone(), true, pool).await?;
|
|
|
|
}
|
2020-09-30 16:19:14 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
// TODO: it would make sense to create a separate task for each destination server
|
|
|
|
let message = SendActivityTask {
|
|
|
|
activity: serialised_activity,
|
|
|
|
to,
|
|
|
|
actor_id: actor.actor_id()?,
|
|
|
|
private_key: actor.private_key().context(location_info!())?,
|
|
|
|
};
|
2020-09-29 13:10:55 +00:00
|
|
|
|
2020-08-31 13:48:02 +00:00
|
|
|
activity_sender.queue::<SendActivityTask>(message)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
struct SendActivityTask {
|
|
|
|
activity: String,
|
|
|
|
to: Vec<Url>,
|
|
|
|
actor_id: Url,
|
|
|
|
private_key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActixJob for SendActivityTask {
|
|
|
|
type State = MyState;
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
|
|
|
const NAME: &'static str = "SendActivityTask";
|
|
|
|
|
|
|
|
const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
|
|
|
|
const BACKOFF: Backoff = Backoff::Exponential(2);
|
|
|
|
|
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
|
|
|
Box::pin(async move {
|
|
|
|
for to_url in &self.to {
|
2020-09-29 13:10:55 +00:00
|
|
|
let mut headers = BTreeMap::<String, String>::new();
|
|
|
|
headers.insert("Content-Type".into(), "application/json".into());
|
2020-10-01 17:54:20 +00:00
|
|
|
let result = sign_and_send(
|
2020-09-29 13:10:55 +00:00
|
|
|
&state.client,
|
|
|
|
headers,
|
|
|
|
to_url,
|
2020-08-31 13:48:02 +00:00
|
|
|
self.activity.clone(),
|
|
|
|
&self.actor_id,
|
|
|
|
self.private_key.to_owned(),
|
|
|
|
)
|
|
|
|
.await;
|
2020-09-29 13:10:55 +00:00
|
|
|
|
2020-09-30 00:56:41 +00:00
|
|
|
if let Err(e) = result {
|
2020-08-31 13:48:02 +00:00
|
|
|
warn!("{}", e);
|
|
|
|
return Err(anyhow!(
|
|
|
|
"Failed to send activity {} to {}",
|
|
|
|
&self.activity,
|
|
|
|
to_url
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity_queue() -> QueueHandle {
|
|
|
|
// Start the application server. This guards access to to the jobs store
|
|
|
|
let queue_handle = create_server(Storage::new());
|
|
|
|
|
|
|
|
// Configure and start our workers
|
|
|
|
WorkerConfig::new(|| MyState {
|
|
|
|
client: Client::default(),
|
|
|
|
})
|
|
|
|
.register::<SendActivityTask>()
|
|
|
|
.start(queue_handle.clone());
|
|
|
|
|
|
|
|
queue_handle
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MyState {
|
|
|
|
pub client: Client,
|
|
|
|
}
|