lemmy/crates/apub_lib/src/activity_queue.rs

117 lines
2.9 KiB
Rust
Raw Normal View History

use crate::{signatures::sign_and_send, traits::ActorType};
use anyhow::{anyhow, Context, Error};
use background_jobs::{
memory_storage::Storage,
ActixJob,
Backoff,
2021-11-23 12:20:01 +00:00
Manager,
MaxRetries,
QueueHandle,
WorkerConfig,
};
use lemmy_utils::{location_info, LemmyError};
use reqwest_middleware::ClientWithMiddleware;
2021-02-01 20:56:37 +00:00
use serde::{Deserialize, Serialize};
use std::{env, fmt::Debug, future::Future, pin::Pin};
2021-11-23 12:16:47 +00:00
use tracing::{info, warn};
use url::Url;
pub async fn send_activity(
2021-11-16 17:03:09 +00:00
activity_id: &Url,
actor: &dyn ActorType,
inboxes: Vec<&Url>,
2021-11-16 17:03:09 +00:00
activity: String,
client: &ClientWithMiddleware,
activity_queue: &QueueHandle,
) -> Result<(), LemmyError> {
for i in inboxes {
let message = SendActivityTask {
2021-11-16 17:03:09 +00:00
activity_id: activity_id.clone(),
inbox: i.to_owned(),
actor_id: actor.actor_id(),
2021-11-16 17:03:09 +00:00
activity: activity.clone(),
private_key: actor.private_key().context(location_info!())?,
};
if env::var("APUB_TESTING_SEND_SYNC").is_ok() {
do_send(message, client).await?;
2020-12-14 16:44:27 +00:00
} else {
2021-11-23 12:20:01 +00:00
activity_queue.queue::<SendActivityTask>(message).await?;
2020-12-14 16:44:27 +00:00
}
}
Ok(())
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct SendActivityTask {
2021-11-16 17:03:09 +00:00
activity_id: Url,
inbox: Url,
actor_id: Url,
2021-11-16 17:03:09 +00:00
activity: String,
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.
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 { do_send(self, &state.client).await })
2020-12-14 16:44:27 +00:00
}
}
2020-09-29 13:10:55 +00:00
async fn do_send(task: SendActivityTask, client: &ClientWithMiddleware) -> Result<(), Error> {
2021-11-16 17:03:09 +00:00
info!("Sending {} to {}", task.activity_id, task.inbox);
2020-12-14 16:44:27 +00:00
let result = sign_and_send(
client,
&task.inbox,
task.activity.clone(),
&task.actor_id,
task.private_key.to_owned(),
)
.await;
2020-12-14 16:44:27 +00:00
2021-11-16 17:03:09 +00:00
match result {
Ok(o) => {
if !o.status().is_success() {
let status = o.status();
let text = o.text().await?;
2021-11-16 17:03:09 +00:00
warn!(
"Send {} to {} failed with status {}: {}",
task.activity_id, task.inbox, status, text,
2021-11-16 17:03:09 +00:00
);
}
}
Err(e) => {
return Err(anyhow!(
"Failed to send activity {} to {}: {}",
&task.activity_id,
task.inbox,
e
));
}
}
2020-12-14 16:44:27 +00:00
Ok(())
}
pub fn create_activity_queue(client: ClientWithMiddleware) -> Manager {
// Configure and start our workers
WorkerConfig::new_managed(Storage::new(), move |_| MyState {
client: client.clone(),
})
.register::<SendActivityTask>()
2021-11-23 12:20:01 +00:00
.start()
}
#[derive(Clone)]
struct MyState {
pub client: ClientWithMiddleware,
}