Move Announce() activity handler to inbox::announce module

This commit is contained in:
silverpill 2022-05-30 22:23:06 +00:00
parent 8bce89e6d1
commit 629ac7196b
3 changed files with 56 additions and 33 deletions

View file

@ -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))
}

View file

@ -3,6 +3,7 @@ use super::fetcher::helpers::ImportError;
// or None if it has been ignored
pub type HandlerResult = Result<Option<&'static str>, ImportError>;
pub mod announce;
pub mod create_note;
pub mod delete;
pub mod update_note;

View file

@ -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 {