Create type for deserializing Update(Person) activities
This commit is contained in:
parent
72eabce15b
commit
f50609e4e5
1 changed files with 18 additions and 13 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue