Create unified handler for Update() activities

This commit is contained in:
silverpill 2022-12-06 23:48:24 +00:00
parent a3c3f97759
commit 648a217971
3 changed files with 39 additions and 11 deletions

View file

@ -15,5 +15,6 @@ pub mod reject_follow;
pub mod remove;
pub mod undo;
pub mod undo_follow;
pub mod update_note;
pub mod update;
mod update_note;
pub mod update_person;

View file

@ -0,0 +1,34 @@
use tokio_postgres::GenericClient;
use crate::activitypub::{
activity::{Activity, Object},
vocabulary::{NOTE, PERSON},
};
use crate::config::Config;
use crate::errors::ValidationError;
use super::HandlerResult;
use super::update_note::handle_update_note;
use super::update_person::handle_update_person;
pub async fn handle_update(
config: &Config,
db_client: &mut impl GenericClient,
activity: Activity,
) -> HandlerResult {
let object_type = activity.object["type"].as_str()
.ok_or(ValidationError("unknown object type"))?;
match object_type {
NOTE => {
let object: Object = serde_json::from_value(activity.object)
.map_err(|_| ValidationError("invalid object"))?;
handle_update_note(db_client, object).await
},
PERSON => {
handle_update_person(config, db_client, activity).await
},
_ => {
log::warn!("unexpected object type {}", object_type);
Ok(None)
},
}
}

View file

@ -32,8 +32,7 @@ use super::handlers::{
remove::handle_remove,
undo::handle_undo,
undo_follow::handle_undo_follow,
update_note::handle_update_note,
update_person::handle_update_person,
update::handle_update,
};
use super::vocabulary::*;
@ -264,15 +263,9 @@ pub async fn receive_activity(
require_actor_signature(&activity.actor, &signer_id)?;
handle_undo(db_client, activity).await?
},
(UPDATE, NOTE) => {
(UPDATE, _) => {
require_actor_signature(&activity.actor, &signer_id)?;
let object: Object = serde_json::from_value(activity.object)
.map_err(|_| ValidationError("invalid object"))?;
handle_update_note(db_client, object).await?
},
(UPDATE, PERSON) => {
require_actor_signature(&activity.actor, &signer_id)?;
handle_update_person(config, db_client, activity).await?
handle_update(config, db_client, activity).await?
},
(MOVE, _) => {
require_actor_signature(&activity.actor, &signer_id)?;