fedimovies/src/activitypub/handlers/delete.rs

71 lines
2.4 KiB
Rust
Raw Permalink Normal View History

use serde::Deserialize;
2022-12-09 21:16:53 +00:00
use serde_json::Value;
2023-04-25 13:49:35 +00:00
use fedimovies_config::Config;
use fedimovies_models::{
database::{DatabaseClient, DatabaseError},
2023-04-24 15:35:32 +00:00
posts::queries::{delete_post, get_post_by_remote_object_id},
profiles::queries::{delete_profile, get_profile_by_remote_actor_id},
2022-05-30 23:58:11 +00:00
};
use crate::activitypub::{
receiver::deserialize_into_object_id,
vocabulary::{NOTE, PERSON},
};
use crate::errors::ValidationError;
use crate::media::remove_media;
use super::HandlerResult;
#[derive(Deserialize)]
struct Delete {
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
pub async fn handle_delete(
config: &Config,
db_client: &mut impl DatabaseClient,
2022-12-09 21:16:53 +00:00
activity: Value,
) -> HandlerResult {
2023-04-26 10:55:42 +00:00
let activity: Delete = serde_json::from_value(activity.clone()).map_err(|_| {
ValidationError(format!(
"unexpected Delete activity structure: {}",
activity
))
})?;
if activity.object == activity.actor {
2022-05-30 23:58:11 +00:00
// Self-delete
2023-04-24 15:35:32 +00:00
let profile = match get_profile_by_remote_actor_id(db_client, &activity.object).await {
2022-05-30 23:58:11 +00:00
Ok(profile) => profile,
// Ignore Delete(Person) if profile is not found
Err(DatabaseError::NotFound(_)) => return Ok(None),
Err(other_error) => return Err(other_error.into()),
};
let deletion_queue = delete_profile(db_client, &profile.id).await?;
let config = config.clone();
tokio::spawn(async move {
remove_media(&config, deletion_queue).await;
2022-05-30 23:58:11 +00:00
});
log::info!("deleted profile {}", profile.acct);
2022-05-30 23:58:11 +00:00
return Ok(Some(PERSON));
};
2023-04-24 15:35:32 +00:00
let post = match get_post_by_remote_object_id(db_client, &activity.object).await {
Ok(post) => post,
// Ignore Delete(Note) if post is not found
Err(DatabaseError::NotFound(_)) => return Ok(None),
Err(other_error) => return Err(other_error.into()),
};
2023-04-24 15:35:32 +00:00
let actor_profile = get_profile_by_remote_actor_id(db_client, &activity.actor).await?;
if post.author.id != actor_profile.id {
2023-04-26 10:55:42 +00:00
return Err(ValidationError("actor is not an author".to_string()).into());
};
let deletion_queue = delete_post(db_client, &post.id).await?;
let config = config.clone();
tokio::spawn(async move {
remove_media(&config, deletion_queue).await;
});
Ok(Some(NOTE))
}