relay/src/jobs/apub/announce.rs

64 lines
1.7 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,
2020-03-30 17:10:04 +00:00
error::MyError,
jobs::{
apub::{get_inboxes, prepare_activity},
DeliverMany, JobState,
},
};
2020-09-07 21:51:02 +00:00
use activitystreams::{activity::Announce as AsAnnounce, url::Url};
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};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Announce {
2020-06-27 22:29:23 +00:00
object_id: Url,
2020-03-30 17:10:04 +00:00
actor: Actor,
}
impl Announce {
2020-06-20 04:11:02 +00:00
pub fn new(object_id: Url, actor: Actor) -> Self {
2020-06-27 22:29:23 +00:00
Announce { object_id, actor }
2020-03-30 17:10:04 +00:00
}
async fn perform(self, state: JobState) -> Result<(), anyhow::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
.queue(DeliverMany::new(inboxes, announce)?)?;
2020-06-27 22:29:23 +00:00
state.state.cache(self.object_id, activity_id).await;
2020-03-30 17:10:04 +00:00
Ok(())
}
}
// Generate a type that says "Look at this object"
fn generate_announce(
config: &Config,
2020-06-20 04:11:02 +00:00
activity_id: &Url,
object_id: &Url,
2020-05-21 21:24:56 +00:00
) -> Result<AsAnnounce, MyError> {
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";
2020-04-21 00:56:50 +00:00
2020-03-30 17:10:04 +00:00
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
}
}