2020-09-30 16:19:14 +00:00
|
|
|
use crate::{
|
2021-07-27 22:18:50 +00:00
|
|
|
activities::community::announce::{AnnouncableActivities, AnnounceActivity},
|
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-12-16 15:30:44 +00:00
|
|
|
APUB_JSON_CONTENT_TYPE,
|
2020-09-30 16:19:14 +00:00
|
|
|
};
|
2020-08-31 13:48:02 +00:00
|
|
|
use anyhow::{anyhow, Context, Error};
|
|
|
|
use background_jobs::{
|
|
|
|
create_server,
|
|
|
|
memory_storage::Storage,
|
|
|
|
ActixJob,
|
|
|
|
Backoff,
|
|
|
|
MaxRetries,
|
|
|
|
QueueHandle,
|
|
|
|
WorkerConfig,
|
|
|
|
};
|
2021-08-19 21:24:33 +00:00
|
|
|
use lemmy_db_schema::source::community::Community;
|
2021-03-01 17:24:11 +00:00
|
|
|
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
|
2020-09-30 16:19:14 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-08-19 21:24:33 +00:00
|
|
|
use log::{info, warn};
|
2020-09-29 13:10:55 +00:00
|
|
|
use reqwest::Client;
|
2021-02-01 20:56:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::{collections::BTreeMap, env, fmt::Debug, future::Future, pin::Pin};
|
2020-08-31 13:48:02 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2021-07-27 22:18:50 +00:00
|
|
|
pub(crate) async fn send_to_community_new(
|
|
|
|
activity: AnnouncableActivities,
|
|
|
|
activity_id: &Url,
|
|
|
|
actor: &dyn ActorType,
|
|
|
|
community: &Community,
|
|
|
|
additional_inboxes: Vec<Url>,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
// if this is a local community, we need to do an announce from the community instead
|
|
|
|
if community.local {
|
|
|
|
insert_activity(activity_id, activity.clone(), true, false, context.pool()).await?;
|
|
|
|
AnnounceActivity::send(activity, community, additional_inboxes, context).await?;
|
|
|
|
} else {
|
|
|
|
let mut inboxes = additional_inboxes;
|
|
|
|
inboxes.push(community.get_shared_inbox_or_inbox_url());
|
|
|
|
send_activity_new(context, &activity, activity_id, actor, inboxes, false).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn send_activity_new<T>(
|
|
|
|
context: &LemmyContext,
|
|
|
|
activity: &T,
|
|
|
|
activity_id: &Url,
|
|
|
|
actor: &dyn ActorType,
|
|
|
|
inboxes: Vec<Url>,
|
|
|
|
sensitive: bool,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
2021-08-04 21:13:51 +00:00
|
|
|
if !Settings::get().federation.enabled || inboxes.is_empty() {
|
2021-07-27 22:18:50 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Sending activity {}", activity_id.to_string());
|
|
|
|
|
|
|
|
// Don't send anything to ourselves
|
|
|
|
// TODO: this should be a debug assert
|
|
|
|
let hostname = Settings::get().get_hostname_without_port()?;
|
|
|
|
let inboxes: Vec<&Url> = inboxes
|
|
|
|
.iter()
|
|
|
|
.filter(|i| i.domain().expect("valid inbox url") != hostname)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let serialised_activity = serde_json::to_string(&activity)?;
|
|
|
|
|
|
|
|
insert_activity(
|
|
|
|
activity_id,
|
|
|
|
serialised_activity.clone(),
|
|
|
|
true,
|
|
|
|
sensitive,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2020-10-12 16:02:28 +00:00
|
|
|
for i in inboxes {
|
2020-10-06 17:19:53 +00:00
|
|
|
let message = SendActivityTask {
|
|
|
|
activity: serialised_activity.to_owned(),
|
2021-01-15 01:18:18 +00:00
|
|
|
inbox: i.to_owned(),
|
2021-01-27 16:42:23 +00:00
|
|
|
actor_id: actor.actor_id(),
|
2020-10-06 17:19:53 +00:00
|
|
|
private_key: actor.private_key().context(location_info!())?,
|
|
|
|
};
|
2020-12-14 16:44:27 +00:00
|
|
|
if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
|
|
|
|
do_send(message, &Client::default()).await?;
|
|
|
|
} else {
|
2021-04-09 15:01:26 +00:00
|
|
|
context.activity_queue.queue::<SendActivityTask>(message)?;
|
2020-12-14 16:44:27 +00:00
|
|
|
}
|
2020-10-06 17:19:53 +00:00
|
|
|
}
|
2020-08-31 13:48:02 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
struct SendActivityTask {
|
|
|
|
activity: String,
|
2020-10-12 16:02:28 +00:00
|
|
|
inbox: Url,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: Url,
|
|
|
|
private_key: String,
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Signs the activity with the sending actor's key, and delivers to the given inbox. Also retries
|
|
|
|
/// if the delivery failed.
|
2020-08-31 13:48:02 +00:00
|
|
|
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 {
|
2020-12-14 17:01:12 +00:00
|
|
|
Box::pin(async move { do_send(self, &state.client).await })
|
2020-12-14 16:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 13:10:55 +00:00
|
|
|
|
2020-12-14 17:01:12 +00:00
|
|
|
async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
|
2020-12-14 16:44:27 +00:00
|
|
|
let mut headers = BTreeMap::<String, String>::new();
|
2021-01-06 04:55:02 +00:00
|
|
|
headers.insert("Content-Type".into(), APUB_JSON_CONTENT_TYPE.to_string());
|
2020-12-14 16:44:27 +00:00
|
|
|
let result = sign_and_send(
|
|
|
|
client,
|
|
|
|
headers,
|
|
|
|
&task.inbox,
|
|
|
|
task.activity.clone(),
|
|
|
|
&task.actor_id,
|
|
|
|
task.private_key.to_owned(),
|
|
|
|
)
|
2020-12-14 17:01:12 +00:00
|
|
|
.await;
|
2020-12-14 16:44:27 +00:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
|
|
|
warn!("{}", e);
|
|
|
|
return Err(anyhow!(
|
2020-12-14 17:01:12 +00:00
|
|
|
"Failed to send activity {} to {}",
|
|
|
|
&task.activity,
|
|
|
|
task.inbox
|
|
|
|
));
|
2020-08-31 13:48:02 +00:00
|
|
|
}
|
2020-12-14 16:44:27 +00:00
|
|
|
Ok(())
|
2020-08-31 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|