From f50609e4e540a8dfe1546e6933eeb1896a3235b5 Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 9 Dec 2022 22:31:54 +0000 Subject: [PATCH] Create type for deserializing Update(Person) activities --- src/activitypub/handlers/update.rs | 31 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/activitypub/handlers/update.rs b/src/activitypub/handlers/update.rs index f2f2b01..62ca88e 100644 --- a/src/activitypub/handlers/update.rs +++ b/src/activitypub/handlers/update.rs @@ -1,9 +1,10 @@ use chrono::Utc; +use serde::Deserialize; use serde_json::Value; use tokio_postgres::GenericClient; use crate::activitypub::{ - activity::{Activity, Object}, + activity::Object, actors::{ helpers::update_remote_profile, types::Actor, @@ -26,8 +27,10 @@ use super::HandlerResult; async fn handle_update_note( db_client: &mut impl GenericClient, - object: Object, + activity: Value, ) -> HandlerResult { + let object: Object = serde_json::from_value(activity["object"].to_owned()) + .map_err(|_| ValidationError("invalid object"))?; let post_id = match get_post_by_remote_object_id( db_client, &object.id, @@ -44,26 +47,32 @@ async fn handle_update_note( Ok(Some(NOTE)) } +#[derive(Deserialize)] +struct UpdatePerson { + actor: String, + object: Actor, +} + async fn handle_update_person( config: &Config, db_client: &impl GenericClient, - activity: Activity, + activity: Value, ) -> HandlerResult { - let actor: Actor = serde_json::from_value(activity.object) + let activity: UpdatePerson = serde_json::from_value(activity) .map_err(|_| ValidationError("invalid actor data"))?; - if actor.id != activity.actor { + if activity.object.id != activity.actor { return Err(ValidationError("actor ID mismatch").into()); }; let profile = get_profile_by_remote_actor_id( db_client, - &actor.id, + &activity.object.id, ).await?; update_remote_profile( db_client, &config.instance(), &config.media_dir(), profile, - actor, + activity.object, ).await?; Ok(Some(PERSON)) } @@ -73,15 +82,11 @@ pub async fn handle_update( db_client: &mut impl GenericClient, activity: Value, ) -> HandlerResult { - let activity: Activity = serde_json::from_value(activity) - .map_err(|_| ValidationError("unexpected activity structure"))?; - let object_type = activity.object["type"].as_str() + 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 + handle_update_note(db_client, activity).await }, PERSON => { handle_update_person(config, db_client, activity).await