From 629ac7196bf550a8b78d3b38554bc808226c876a Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 30 May 2022 22:23:06 +0000 Subject: [PATCH] Move Announce() activity handler to inbox::announce module --- src/activitypub/handlers/announce.rs | 48 ++++++++++++++++++++++++++++ src/activitypub/handlers/mod.rs | 1 + src/activitypub/receiver.rs | 40 ++++------------------- 3 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 src/activitypub/handlers/announce.rs diff --git a/src/activitypub/handlers/announce.rs b/src/activitypub/handlers/announce.rs new file mode 100644 index 0000000..e1488cb --- /dev/null +++ b/src/activitypub/handlers/announce.rs @@ -0,0 +1,48 @@ +use tokio_postgres::GenericClient; + +use crate::activitypub::{ + activity::Activity, + fetcher::helpers::{get_or_import_profile_by_actor_id, import_post}, + receiver::{get_object_id, parse_object_id}, + vocabulary::NOTE, +}; +use crate::config::Config; +use crate::errors::DatabaseError; +use crate::models::posts::queries::{create_post, get_post_by_object_id}; +use crate::models::posts::types::PostCreateData; +use super::HandlerResult; + +pub async fn handle_announce( + config: &Config, + db_client: &mut impl GenericClient, + activity: Activity, +) -> HandlerResult { + let repost_object_id = activity.id; + match get_post_by_object_id(db_client, &repost_object_id).await { + Ok(_) => return Ok(None), // Ignore if repost already exists + Err(DatabaseError::NotFound(_)) => (), + Err(other_error) => return Err(other_error.into()), + }; + let author = get_or_import_profile_by_actor_id( + db_client, + &config.instance(), + &config.media_dir(), + &activity.actor, + ).await?; + let object_id = get_object_id(&activity.object)?; + let post_id = match parse_object_id(&config.instance_url(), &object_id) { + Ok(post_id) => post_id, + Err(_) => { + // Try to get remote post + let post = import_post(config, db_client, object_id, None).await?; + post.id + }, + }; + let repost_data = PostCreateData { + repost_of_id: Some(post_id), + object_id: Some(repost_object_id), + ..Default::default() + }; + create_post(db_client, &author.id, repost_data).await?; + Ok(Some(NOTE)) +} diff --git a/src/activitypub/handlers/mod.rs b/src/activitypub/handlers/mod.rs index 28d6354..26df5ca 100644 --- a/src/activitypub/handlers/mod.rs +++ b/src/activitypub/handlers/mod.rs @@ -3,6 +3,7 @@ use super::fetcher::helpers::ImportError; // or None if it has been ignored pub type HandlerResult = Result, ImportError>; +pub mod announce; pub mod create_note; pub mod delete; pub mod update_note; diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index 4e27d23..0fd1416 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -9,11 +9,9 @@ use crate::config::Config; use crate::errors::{ConversionError, DatabaseError, HttpError, ValidationError}; use crate::http_signatures::verify::verify_http_signature; use crate::models::posts::queries::{ - create_post, get_post_by_object_id, delete_post, }; -use crate::models::posts::types::PostCreateData; use crate::models::profiles::queries::{ get_profile_by_actor_id, get_profile_by_acct, @@ -41,9 +39,12 @@ 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::handlers::{ + announce::handle_announce, + delete::handle_delete, + update_note::handle_update_note, + update_person::handle_update_person, +}; use super::vocabulary::*; pub fn parse_actor_id( @@ -235,34 +236,7 @@ pub async fn receive_activity( }, (ANNOUNCE, _) => { require_actor_signature(&activity.actor, &signer_id)?; - let repost_object_id = activity.id; - match get_post_by_object_id(db_client, &repost_object_id).await { - Ok(_) => return Ok(()), // Ignore if repost already exists - Err(DatabaseError::NotFound(_)) => (), - Err(other_error) => return Err(other_error.into()), - }; - let author = get_or_import_profile_by_actor_id( - db_client, - &config.instance(), - &config.media_dir(), - &activity.actor, - ).await?; - let object_id = get_object_id(&activity.object)?; - let post_id = match parse_object_id(&config.instance_url(), &object_id) { - Ok(post_id) => post_id, - Err(_) => { - // Try to get remote post - let post = import_post(config, db_client, object_id, None).await?; - post.id - }, - }; - let repost_data = PostCreateData { - repost_of_id: Some(post_id), - object_id: Some(repost_object_id), - ..Default::default() - }; - create_post(db_client, &author.id, repost_data).await?; - Some(NOTE) + handle_announce(config, db_client, activity).await? }, (DELETE, _) => { if signer_id != activity.actor {