mirror of
https://github.com/LemmyNet/activitypub-federation-rust.git
synced 2024-06-02 13:29:36 +00:00
dedupupup
This commit is contained in:
parent
499f037b68
commit
3ea4bb6e53
|
@ -3,7 +3,7 @@
|
||||||
#![doc = include_str!("../docs/09_sending_activities.md")]
|
#![doc = include_str!("../docs/09_sending_activities.md")]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
activity_sending::{build_tasks, generate_request_headers, send, SendActivityTask},
|
activity_sending::{build_tasks, generate_request_headers, SendActivityTask},
|
||||||
config::Data,
|
config::Data,
|
||||||
error::Error,
|
error::Error,
|
||||||
http_signatures::sign_request,
|
http_signatures::sign_request,
|
||||||
|
@ -91,30 +91,8 @@ async fn sign_and_send(
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
retry_strategy: RetryStrategy,
|
retry_strategy: RetryStrategy,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
debug!("Sending {} to {}", task.activity_id, task.inbox,);
|
|
||||||
let request_builder = client
|
|
||||||
.post(task.inbox.to_string())
|
|
||||||
.timeout(timeout)
|
|
||||||
.headers(generate_request_headers(&task.inbox));
|
|
||||||
let request = sign_request(
|
|
||||||
request_builder,
|
|
||||||
&task.actor_id,
|
|
||||||
task.activity.clone(),
|
|
||||||
task.private_key.clone(),
|
|
||||||
task.http_signature_compat,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
retry(
|
retry(
|
||||||
|| {
|
|| task.sign_and_send_internal(client, timeout),
|
||||||
send(
|
|
||||||
task,
|
|
||||||
client,
|
|
||||||
request
|
|
||||||
.try_clone()
|
|
||||||
.expect("The body of the request is not cloneable"),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
retry_strategy,
|
retry_strategy,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -24,7 +24,7 @@ use serde::Serialize;
|
||||||
use std::{
|
use std::{
|
||||||
self,
|
self,
|
||||||
fmt::{Debug, Display},
|
fmt::{Debug, Display},
|
||||||
time::SystemTime,
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -70,10 +70,19 @@ impl SendActivityTask {
|
||||||
|
|
||||||
/// convert a sendactivitydata to a request, signing and sending it
|
/// convert a sendactivitydata to a request, signing and sending it
|
||||||
pub async fn sign_and_send<Datatype: Clone>(&self, data: &Data<Datatype>) -> Result<(), Error> {
|
pub async fn sign_and_send<Datatype: Clone>(&self, data: &Data<Datatype>) -> Result<(), Error> {
|
||||||
let client = &data.config.client;
|
self.sign_and_send_internal(&data.config.client, data.config.request_timeout)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn sign_and_send_internal(
|
||||||
|
&self,
|
||||||
|
client: &ClientWithMiddleware,
|
||||||
|
timeout: Duration,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
debug!("Sending {} to {}", self.activity_id, self.inbox,);
|
||||||
let request_builder = client
|
let request_builder = client
|
||||||
.post(self.inbox.to_string())
|
.post(self.inbox.to_string())
|
||||||
.timeout(data.config.request_timeout)
|
.timeout(timeout)
|
||||||
.headers(generate_request_headers(&self.inbox));
|
.headers(generate_request_headers(&self.inbox));
|
||||||
let request = sign_request(
|
let request = sign_request(
|
||||||
request_builder,
|
request_builder,
|
||||||
|
@ -83,35 +92,26 @@ impl SendActivityTask {
|
||||||
self.http_signature_compat,
|
self.http_signature_compat,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
let response = client.execute(request).await?;
|
||||||
|
|
||||||
send(&self, client, request).await
|
match response {
|
||||||
}
|
o if o.status().is_success() => {
|
||||||
}
|
debug!("Activity {self} delivered successfully");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
o if o.status().is_client_error() => {
|
||||||
|
let text = o.text_limited().await?;
|
||||||
|
debug!("Activity {self} was rejected, aborting: {text}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
o => {
|
||||||
|
let status = o.status();
|
||||||
|
let text = o.text_limited().await?;
|
||||||
|
|
||||||
pub(crate) async fn send<T: Display>(
|
Err(Error::Other(format!(
|
||||||
activity: &T,
|
"Activity {self} failure with status {status}: {text}",
|
||||||
client: &ClientWithMiddleware,
|
)))
|
||||||
request: Request,
|
}
|
||||||
) -> Result<(), Error> {
|
|
||||||
let response = client.execute(request).await?;
|
|
||||||
|
|
||||||
match response {
|
|
||||||
o if o.status().is_success() => {
|
|
||||||
debug!("Activity {activity} delivered successfully");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
o if o.status().is_client_error() => {
|
|
||||||
let text = o.text_limited().await?;
|
|
||||||
debug!("Activity {activity} was rejected, aborting: {text}");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
o => {
|
|
||||||
let status = o.status();
|
|
||||||
let text = o.text_limited().await?;
|
|
||||||
|
|
||||||
Err(Error::Other(format!(
|
|
||||||
"Activity {activity} failure with status {status}: {text}",
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue