2022-11-15 01:18:09 +00:00
|
|
|
use crate::{
|
|
|
|
error::Error,
|
|
|
|
jobs::{debug_object, JobState},
|
|
|
|
};
|
2022-01-17 22:54:45 +00:00
|
|
|
use activitystreams::iri_string::types::IriString;
|
2020-04-21 00:56:50 +00:00
|
|
|
use background_jobs::{ActixJob, Backoff};
|
2020-03-21 20:24:05 +00:00
|
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
|
2021-09-21 19:32:25 +00:00
|
|
|
#[derive(Clone, serde::Deserialize, serde::Serialize)]
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) struct Deliver {
|
2022-01-17 22:54:45 +00:00
|
|
|
to: IriString,
|
2020-03-21 20:24:05 +00:00
|
|
|
data: serde_json::Value,
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:32:25 +00:00
|
|
|
impl std::fmt::Debug for Deliver {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("Deliver")
|
|
|
|
.field("to", &self.to.to_string())
|
2022-11-13 19:57:52 +00:00
|
|
|
.field("activity", &self.data["type"])
|
2022-11-15 01:18:09 +00:00
|
|
|
.field("object", debug_object(&self.data))
|
2021-09-21 19:32:25 +00:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:24:05 +00:00
|
|
|
impl Deliver {
|
2022-01-17 22:54:45 +00:00
|
|
|
pub(crate) fn new<T>(to: IriString, data: T) -> Result<Self, Error>
|
2020-03-21 20:24:05 +00:00
|
|
|
where
|
|
|
|
T: serde::ser::Serialize,
|
|
|
|
{
|
|
|
|
Ok(Deliver {
|
|
|
|
to,
|
|
|
|
data: serde_json::to_value(data)?,
|
|
|
|
})
|
|
|
|
}
|
2021-09-18 17:55:39 +00:00
|
|
|
|
2022-11-01 20:57:33 +00:00
|
|
|
#[tracing::instrument(name = "Deliver", skip(state))]
|
2021-09-18 17:55:39 +00:00
|
|
|
async fn permform(self, state: JobState) -> Result<(), Error> {
|
|
|
|
state.requests.deliver(self.to, &self.data).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-21 20:24:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:45:44 +00:00
|
|
|
impl ActixJob for Deliver {
|
2020-03-21 20:24:05 +00:00
|
|
|
type State = JobState;
|
2021-09-18 17:55:39 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
|
2020-03-21 20:24:05 +00:00
|
|
|
|
2020-04-21 01:03:46 +00:00
|
|
|
const NAME: &'static str = "relay::jobs::Deliver";
|
2020-04-21 00:56:50 +00:00
|
|
|
const BACKOFF: Backoff = Backoff::Exponential(8);
|
|
|
|
|
2020-03-21 20:24:05 +00:00
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
2021-09-18 17:55:39 +00:00
|
|
|
Box::pin(async move { self.permform(state).await.map_err(Into::into) })
|
2020-03-21 20:24:05 +00:00
|
|
|
}
|
|
|
|
}
|