2022-12-10 00:54:59 +00:00
|
|
|
use serde::Deserialize;
|
2022-12-09 21:16:53 +00:00
|
|
|
use serde_json::Value;
|
2022-05-30 20:45:53 +00:00
|
|
|
|
2023-02-18 23:52:48 +00:00
|
|
|
use mitra_config::Config;
|
|
|
|
|
2022-05-30 20:45:53 +00:00
|
|
|
use crate::activitypub::{
|
2022-12-10 00:54:59 +00:00
|
|
|
receiver::deserialize_into_object_id,
|
2022-05-30 23:58:11 +00:00
|
|
|
vocabulary::{NOTE, PERSON},
|
2022-05-30 20:45:53 +00:00
|
|
|
};
|
2023-01-17 23:14:18 +00:00
|
|
|
use crate::database::{DatabaseClient, DatabaseError};
|
2022-12-03 22:09:42 +00:00
|
|
|
use crate::errors::ValidationError;
|
2023-03-17 22:48:18 +00:00
|
|
|
use crate::media::remove_media;
|
2023-01-19 20:34:46 +00:00
|
|
|
use crate::models::{
|
|
|
|
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
|
|
|
};
|
2022-05-30 20:45:53 +00:00
|
|
|
use super::HandlerResult;
|
|
|
|
|
2022-12-10 00:54:59 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Delete {
|
|
|
|
actor: String,
|
|
|
|
#[serde(deserialize_with = "deserialize_into_object_id")]
|
|
|
|
object: String,
|
|
|
|
}
|
|
|
|
|
2022-05-30 20:45:53 +00:00
|
|
|
pub async fn handle_delete(
|
|
|
|
config: &Config,
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &mut impl DatabaseClient,
|
2022-12-09 21:16:53 +00:00
|
|
|
activity: Value,
|
2022-05-30 20:45:53 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-10 00:54:59 +00:00
|
|
|
let activity: Delete = serde_json::from_value(activity)
|
2022-12-09 21:16:53 +00:00
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2022-12-10 00:54:59 +00:00
|
|
|
if activity.object == activity.actor {
|
2022-05-30 23:58:11 +00:00
|
|
|
// Self-delete
|
2022-10-15 12:32:27 +00:00
|
|
|
let profile = match get_profile_by_remote_actor_id(
|
|
|
|
db_client,
|
2022-12-10 00:54:59 +00:00
|
|
|
&activity.object,
|
2022-10-15 12:32:27 +00:00
|
|
|
).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();
|
2022-07-17 00:00:35 +00:00
|
|
|
tokio::spawn(async move {
|
2023-03-17 22:48:18 +00:00
|
|
|
remove_media(&config, deletion_queue).await;
|
2022-05-30 23:58:11 +00:00
|
|
|
});
|
2022-07-12 19:03:44 +00:00
|
|
|
log::info!("deleted profile {}", profile.acct);
|
2022-05-30 23:58:11 +00:00
|
|
|
return Ok(Some(PERSON));
|
2022-05-30 20:45:53 +00:00
|
|
|
};
|
2022-10-15 12:32:27 +00:00
|
|
|
let post = match get_post_by_remote_object_id(
|
|
|
|
db_client,
|
2022-12-10 00:54:59 +00:00
|
|
|
&activity.object,
|
2022-10-15 12:32:27 +00:00
|
|
|
).await {
|
2022-05-30 20:45:53 +00:00
|
|
|
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()),
|
|
|
|
};
|
2022-10-15 12:32:27 +00:00
|
|
|
let actor_profile = get_profile_by_remote_actor_id(
|
|
|
|
db_client,
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-05-30 20:45:53 +00:00
|
|
|
if post.author.id != actor_profile.id {
|
|
|
|
return Err(ValidationError("actor is not an author").into());
|
|
|
|
};
|
|
|
|
let deletion_queue = delete_post(db_client, &post.id).await?;
|
|
|
|
let config = config.clone();
|
2022-07-17 00:00:35 +00:00
|
|
|
tokio::spawn(async move {
|
2023-03-17 22:48:18 +00:00
|
|
|
remove_media(&config, deletion_queue).await;
|
2022-05-30 20:45:53 +00:00
|
|
|
});
|
|
|
|
Ok(Some(NOTE))
|
|
|
|
}
|