relay/src/jobs/apub/forward.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2020-03-30 17:10:04 +00:00
use crate::{
2020-05-21 21:24:56 +00:00
apub::AcceptedActivities,
2021-02-10 04:05:06 +00:00
db::Actor,
2021-09-18 17:55:39 +00:00
error::{Error, ErrorKind},
future::BoxFuture,
2020-03-30 17:10:04 +00:00
jobs::{apub::get_inboxes, DeliverMany, JobState},
};
2020-09-07 21:51:02 +00:00
use activitystreams::prelude::*;
use background_jobs::Job;
2020-03-30 17:10:04 +00:00
2022-11-13 19:57:52 +00:00
#[derive(Clone, serde::Deserialize, serde::Serialize)]
2021-02-10 04:17:20 +00:00
pub(crate) struct Forward {
2020-05-21 21:24:56 +00:00
input: AcceptedActivities,
2020-03-30 17:10:04 +00:00
actor: Actor,
}
2022-11-13 19:57:52 +00:00
impl std::fmt::Debug for Forward {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Forward")
.field("input", &self.input.id_unchecked())
.field("actor", &self.actor.id)
.finish()
}
}
2020-03-30 17:10:04 +00:00
impl Forward {
2020-05-21 21:24:56 +00:00
pub fn new(input: AcceptedActivities, actor: Actor) -> Self {
2020-03-30 17:10:04 +00:00
Forward { input, actor }
}
2022-11-01 20:57:33 +00:00
#[tracing::instrument(name = "Forward", skip(state))]
2021-09-18 17:55:39 +00:00
async fn perform(self, state: JobState) -> Result<(), Error> {
2020-05-21 21:24:56 +00:00
let object_id = self
.input
2022-01-17 22:54:45 +00:00
.object_unchecked()
2020-05-21 21:24:56 +00:00
.as_single_id()
2021-09-18 17:55:39 +00:00
.ok_or(ErrorKind::MissingId)?;
2020-03-30 17:10:04 +00:00
let inboxes = get_inboxes(&state.state, &self.actor, object_id).await?;
state
.job_server
2022-01-17 22:54:45 +00:00
.queue(DeliverMany::new(inboxes, self.input)?)
.await?;
2020-03-30 17:10:04 +00:00
Ok(())
}
}
impl Job for Forward {
2020-03-30 17:10:04 +00:00
type State = JobState;
type Future = BoxFuture<'static, anyhow::Result<()>>;
2020-03-30 17:10:04 +00:00
2020-04-21 01:03:46 +00:00
const NAME: &'static str = "relay::jobs::apub::Forward";
2022-11-20 03:32:45 +00:00
const QUEUE: &'static str = "apub";
2020-04-21 00:56:50 +00:00
2020-03-30 17:10:04 +00:00
fn run(self, state: Self::State) -> Self::Future {
2021-09-18 17:55:39 +00:00
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
2020-03-30 17:10:04 +00:00
}
}