relay/src/jobs/apub/announce.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

2020-03-30 17:10:04 +00:00
use crate::{
config::{Config, UrlKind},
2021-02-10 04:05:06 +00:00
db::Actor,
2021-09-18 17:55:39 +00:00
error::Error,
2020-03-30 17:10:04 +00:00
jobs::{
apub::{get_inboxes, prepare_activity},
DeliverMany, JobState,
},
};
2022-01-17 22:54:45 +00:00
use activitystreams::{activity::Announce as AsAnnounce, iri_string::types::IriString};
2020-04-21 00:56:50 +00:00
use background_jobs::ActixJob;
2020-03-30 17:10:04 +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 Announce {
2022-01-17 22:54:45 +00:00
object_id: IriString,
2020-03-30 17:10:04 +00:00
actor: Actor,
}
2021-09-21 19:32:25 +00:00
impl std::fmt::Debug for Announce {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Announce")
.field("object_id", &self.object_id.to_string())
2022-11-13 19:57:52 +00:00
.field("actor_id", &self.actor.id)
2021-09-21 19:32:25 +00:00
.finish()
}
}
2020-03-30 17:10:04 +00:00
impl Announce {
2022-01-17 22:54:45 +00:00
pub fn new(object_id: IriString, actor: Actor) -> Self {
2020-06-27 22:29:23 +00:00
Announce { object_id, actor }
2020-03-30 17:10:04 +00:00
}
2022-11-01 20:57:33 +00:00
#[tracing::instrument(name = "Announce", skip(state))]
2021-09-18 17:55:39 +00:00
async fn perform(self, state: JobState) -> Result<(), Error> {
let activity_id = state.config.generate_url(UrlKind::Activity);
2020-03-30 17:10:04 +00:00
let announce = generate_announce(&state.config, &activity_id, &self.object_id)?;
let inboxes = get_inboxes(&state.state, &self.actor, &self.object_id).await?;
state
.job_server
2022-11-01 20:57:33 +00:00
.queue(DeliverMany::new(inboxes, announce)?)
.await?;
2020-03-30 17:10:04 +00:00
2022-12-09 23:47:45 +00:00
state.state.cache(self.object_id, activity_id);
2020-03-30 17:10:04 +00:00
Ok(())
}
}
// Generate a type that says "Look at this object"
fn generate_announce(
config: &Config,
2022-01-17 22:54:45 +00:00
activity_id: &IriString,
object_id: &IriString,
2021-09-18 17:55:39 +00:00
) -> Result<AsAnnounce, Error> {
let announce = AsAnnounce::new(config.generate_url(UrlKind::Actor), object_id.clone());
2020-03-30 17:10:04 +00:00
prepare_activity(
announce,
activity_id.clone(),
config.generate_url(UrlKind::Followers),
)
}
impl ActixJob for Announce {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
2020-04-21 01:03:46 +00:00
const NAME: &'static str = "relay::jobs::apub::Announce";
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
}
}