Move Delete() activity handler to inbox::delete module

This commit is contained in:
silverpill 2022-05-30 20:45:53 +00:00
parent 30990c8af6
commit 8bce89e6d1
3 changed files with 45 additions and 23 deletions

View file

@ -0,0 +1,41 @@
use tokio_postgres::GenericClient;
use crate::activitypub::{
activity::Activity,
receiver::get_object_id,
vocabulary::NOTE,
};
use crate::config::Config;
use crate::errors::{DatabaseError, ValidationError};
use crate::models::posts::queries::{delete_post, get_post_by_object_id};
use crate::models::profiles::queries::get_profile_by_actor_id;
use super::HandlerResult;
pub async fn handle_delete(
config: &Config,
db_client: &mut impl GenericClient,
activity: Activity,
) -> HandlerResult {
let object_id = get_object_id(&activity.object)?;
if object_id == activity.actor {
log::info!("received deletion request for {}", object_id);
// Ignore Delete(Person)
return Ok(None);
};
let post = match get_post_by_object_id(db_client, &object_id).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()),
};
let actor_profile = get_profile_by_actor_id(db_client, &activity.actor).await?;
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();
actix_rt::spawn(async move {
deletion_queue.process(&config).await;
});
Ok(Some(NOTE))
}

View file

@ -4,5 +4,6 @@ use super::fetcher::helpers::ImportError;
pub type HandlerResult = Result<Option<&'static str>, ImportError>;
pub mod create_note;
pub mod delete;
pub mod update_note;
pub mod update_person;

View file

@ -41,6 +41,7 @@ use super::fetcher::helpers::{
get_or_import_profile_by_actor_id,
import_post,
};
use super::handlers::delete::handle_delete;
use super::handlers::update_note::handle_update_note;
use super::handlers::update_person::handle_update_person;
use super::vocabulary::*;
@ -130,7 +131,7 @@ pub fn parse_property_value<T: DeserializeOwned>(value: &Value) -> Result<Vec<T>
}
/// Parses object json value and returns its ID as string
fn get_object_id(object: &Value) -> Result<String, ValidationError> {
pub fn get_object_id(object: &Value) -> Result<String, ValidationError> {
let object_id = match object.as_str() {
Some(object_id) => object_id.to_owned(),
None => {
@ -268,28 +269,7 @@ pub async fn receive_activity(
// Ignore forwarded Delete() activities
return Ok(());
};
let object_id = get_object_id(&activity.object)?;
if object_id == activity.actor {
log::info!("received deletion request for {}", object_id);
// Ignore Delete(Person)
return Ok(());
};
let post = match get_post_by_object_id(db_client, &object_id).await {
Ok(post) => post,
// Ignore Delete(Note) if post is not found
Err(DatabaseError::NotFound(_)) => return Ok(()),
Err(other_error) => return Err(other_error.into()),
};
let actor_profile = get_profile_by_actor_id(db_client, &activity.actor).await?;
if post.author.id != actor_profile.id {
return Err(HttpError::ValidationError("actor is not an author".into()));
};
let deletion_queue = delete_post(db_client, &post.id).await?;
let config = config.clone();
actix_rt::spawn(async move {
deletion_queue.process(&config).await;
});
Some(NOTE)
handle_delete(config, db_client, activity).await?
},
(EMOJI_REACT | LIKE, _) => {
require_actor_signature(&activity.actor, &signer_id)?;