relay/src/jobs/apub/undo.rs

62 lines
1.7 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,
2020-03-30 17:10:04 +00:00
config::UrlKind,
2021-02-10 04:05:06 +00:00
db::Actor,
2021-09-18 17:55:39 +00:00
error::Error,
future::BoxFuture,
2020-03-30 17:10:04 +00:00
jobs::{apub::generate_undo_follow, Deliver, JobState},
};
2022-11-13 19:57:52 +00:00
use activitystreams::prelude::BaseExt;
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 Undo {
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 Undo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Undo")
.field("input", &self.input.id_unchecked())
.field("actor", &self.actor.id)
.finish()
}
}
2020-03-30 17:10:04 +00:00
impl Undo {
2021-02-10 04:17:20 +00:00
pub(crate) fn new(input: AcceptedActivities, actor: Actor) -> Self {
2020-03-30 17:10:04 +00:00
Undo { input, actor }
}
2022-11-01 20:57:33 +00:00
#[tracing::instrument(name = "Undo", skip(state))]
2021-09-18 17:55:39 +00:00
async fn perform(self, state: JobState) -> Result<(), Error> {
2021-02-10 04:05:06 +00:00
let was_following = state.state.db.is_connected(self.actor.id.clone()).await?;
2020-03-30 17:10:04 +00:00
2021-02-10 06:44:48 +00:00
state.actors.remove_connection(&self.actor).await?;
2020-03-30 17:10:04 +00:00
if was_following {
let my_id = state.config.generate_url(UrlKind::Actor);
2020-03-30 17:10:04 +00:00
let undo = generate_undo_follow(&state.config, &self.actor.id, &my_id)?;
state
.job_server
2022-11-01 20:57:33 +00:00
.queue(Deliver::new(self.actor.inbox, undo)?)
.await?;
2020-03-30 17:10:04 +00:00
}
Ok(())
}
}
impl Job for Undo {
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::Undo";
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
}
}