Create type for deserializing Update(Person) activities

This commit is contained in:
silverpill 2022-12-09 22:31:54 +00:00
parent 72eabce15b
commit f50609e4e5

View file

@ -1,9 +1,10 @@
use chrono::Utc; use chrono::Utc;
use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use crate::activitypub::{ use crate::activitypub::{
activity::{Activity, Object}, activity::Object,
actors::{ actors::{
helpers::update_remote_profile, helpers::update_remote_profile,
types::Actor, types::Actor,
@ -26,8 +27,10 @@ use super::HandlerResult;
async fn handle_update_note( async fn handle_update_note(
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,
object: Object, activity: Value,
) -> HandlerResult { ) -> 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( let post_id = match get_post_by_remote_object_id(
db_client, db_client,
&object.id, &object.id,
@ -44,26 +47,32 @@ async fn handle_update_note(
Ok(Some(NOTE)) Ok(Some(NOTE))
} }
#[derive(Deserialize)]
struct UpdatePerson {
actor: String,
object: Actor,
}
async fn handle_update_person( async fn handle_update_person(
config: &Config, config: &Config,
db_client: &impl GenericClient, db_client: &impl GenericClient,
activity: Activity, activity: Value,
) -> HandlerResult { ) -> HandlerResult {
let actor: Actor = serde_json::from_value(activity.object) let activity: UpdatePerson = serde_json::from_value(activity)
.map_err(|_| ValidationError("invalid actor data"))?; .map_err(|_| ValidationError("invalid actor data"))?;
if actor.id != activity.actor { if activity.object.id != activity.actor {
return Err(ValidationError("actor ID mismatch").into()); return Err(ValidationError("actor ID mismatch").into());
}; };
let profile = get_profile_by_remote_actor_id( let profile = get_profile_by_remote_actor_id(
db_client, db_client,
&actor.id, &activity.object.id,
).await?; ).await?;
update_remote_profile( update_remote_profile(
db_client, db_client,
&config.instance(), &config.instance(),
&config.media_dir(), &config.media_dir(),
profile, profile,
actor, activity.object,
).await?; ).await?;
Ok(Some(PERSON)) Ok(Some(PERSON))
} }
@ -73,15 +82,11 @@ pub async fn handle_update(
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,
activity: Value, activity: Value,
) -> HandlerResult { ) -> HandlerResult {
let activity: Activity = serde_json::from_value(activity) let object_type = activity["object"]["type"].as_str()
.map_err(|_| ValidationError("unexpected activity structure"))?;
let object_type = activity.object["type"].as_str()
.ok_or(ValidationError("unknown object type"))?; .ok_or(ValidationError("unknown object type"))?;
match object_type { match object_type {
NOTE => { NOTE => {
let object: Object = serde_json::from_value(activity.object) handle_update_note(db_client, activity).await
.map_err(|_| ValidationError("invalid object"))?;
handle_update_note(db_client, object).await
}, },
PERSON => { PERSON => {
handle_update_person(config, db_client, activity).await handle_update_person(config, db_client, activity).await