Create type for deserializing Like() activities

This commit is contained in:
silverpill 2022-12-09 22:56:51 +00:00
parent 515d158448
commit 5307a28111

View file

@ -1,11 +1,11 @@
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,
fetcher::helpers::get_or_import_profile_by_actor_id, fetcher::helpers::get_or_import_profile_by_actor_id,
identifiers::parse_local_object_id, identifiers::parse_local_object_id,
receiver::find_object_id, receiver::deserialize_into_object_id,
vocabulary::NOTE, vocabulary::NOTE,
}; };
use crate::config::Config; use crate::config::Config;
@ -15,12 +15,20 @@ use crate::models::reactions::queries::create_reaction;
use crate::models::posts::queries::get_post_by_remote_object_id; use crate::models::posts::queries::get_post_by_remote_object_id;
use super::HandlerResult; use super::HandlerResult;
#[derive(Deserialize)]
struct Like {
id: String,
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
pub async fn handle_like( pub async fn handle_like(
config: &Config, config: &Config,
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 activity: Like = serde_json::from_value(activity)
.map_err(|_| ValidationError("unexpected activity structure"))?; .map_err(|_| ValidationError("unexpected activity structure"))?;
let author = get_or_import_profile_by_actor_id( let author = get_or_import_profile_by_actor_id(
db_client, db_client,
@ -28,16 +36,15 @@ pub async fn handle_like(
&config.media_dir(), &config.media_dir(),
&activity.actor, &activity.actor,
).await?; ).await?;
let object_id = find_object_id(&activity.object)?;
let post_id = match parse_local_object_id( let post_id = match parse_local_object_id(
&config.instance_url(), &config.instance_url(),
&object_id, &activity.object,
) { ) {
Ok(post_id) => post_id, Ok(post_id) => post_id,
Err(_) => { Err(_) => {
let post = match get_post_by_remote_object_id( let post = match get_post_by_remote_object_id(
db_client, db_client,
&object_id, &activity.object,
).await { ).await {
Ok(post) => post, Ok(post) => post,
// Ignore like if post is not found locally // Ignore like if post is not found locally