fix: check status code when delivering

This commit is contained in:
əlemi 2024-03-26 02:50:58 +01:00
parent d5c73e85e8
commit 83b0b35043
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -85,16 +85,7 @@ impl Dispatcher {
let signature = base64::prelude::BASE64_URL_SAFE.encode(signer.sign_to_vec().unwrap()); let signature = base64::prelude::BASE64_URL_SAFE.encode(signer.sign_to_vec().unwrap());
let signature_header = format!("keyId=\"{}\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date\",signature=\"{signature}\"", delivery.actor); let signature_header = format!("keyId=\"{}\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date\",signature=\"{signature}\"", delivery.actor);
if let Err(e) = reqwest::Client::new() if let Err(e) = deliver(&delivery.target, &payload, host, date, signature_header, &domain).await {
.post(&delivery.target)
.json(&payload)
.header("Host", host)
.header("Date", date)
.header("Signature", signature_header)
.header(USER_AGENT, format!("upub+{VERSION} ({domain})")) // TODO put instance admin email
.send()
.await
{
tracing::warn!("failed delivery of {} to {} : {e}", delivery.activity, delivery.target); tracing::warn!("failed delivery of {} to {} : {e}", delivery.activity, delivery.target);
let new_delivery = model::delivery::ActiveModel { let new_delivery = model::delivery::ActiveModel {
id: sea_orm::ActiveValue::NotSet, id: sea_orm::ActiveValue::NotSet,
@ -105,12 +96,24 @@ impl Dispatcher {
created: sea_orm::ActiveValue::Set(delivery.created), created: sea_orm::ActiveValue::Set(delivery.created),
attempt: sea_orm::ActiveValue::Set(delivery.attempt + 1), attempt: sea_orm::ActiveValue::Set(delivery.attempt + 1),
}; };
model::delivery::Entity::insert(new_delivery) model::delivery::Entity::insert(new_delivery).exec(&db).await?;
.exec(&db)
.await?;
} }
} }
}) })
} }
} }
async fn deliver(target: &str, payload: &serde_json::Value, host: String, date: String, signature_header: String, domain: &str) -> Result<(), reqwest::Error> {
reqwest::Client::new()
.post(target)
.json(payload)
.header("Host", host)
.header("Date", date)
.header("Signature", signature_header)
.header(USER_AGENT, format!("upub+{VERSION} ({domain})")) // TODO put instance admin email
.send()
.await?
.error_for_status()?;
Ok(())
}