Create types for deserializing Add() and Remove() activities

This commit is contained in:
silverpill 2022-12-09 22:49:20 +00:00
parent f50609e4e5
commit 73e0b10a10
2 changed files with 28 additions and 16 deletions

View file

@ -1,10 +1,9 @@
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,
identifiers::parse_local_actor_id, identifiers::parse_local_actor_id,
receiver::find_object_id,
vocabulary::PERSON, vocabulary::PERSON,
}; };
use crate::config::Config; use crate::config::Config;
@ -14,24 +13,31 @@ use crate::models::relationships::queries::subscribe_opt;
use crate::models::users::queries::get_user_by_name; use crate::models::users::queries::get_user_by_name;
use super::{HandlerError, HandlerResult}; use super::{HandlerError, HandlerResult};
#[derive(Deserialize)]
struct Add {
actor: String,
object: String,
target: String,
}
pub async fn handle_add( pub async fn handle_add(
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: Add = serde_json::from_value(activity)
.map_err(|_| ValidationError("unexpected activity structure"))?; .map_err(|_| ValidationError("unexpected activity structure"))?;
let actor_profile = get_profile_by_remote_actor_id( let actor_profile = get_profile_by_remote_actor_id(
db_client, db_client,
&activity.actor, &activity.actor,
).await?; ).await?;
let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?; let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?;
let target_value = activity.target.ok_or(ValidationError("target is missing"))?; if Some(activity.target) == actor.subscribers {
let target_id = find_object_id(&target_value)?;
if Some(target_id) == actor.subscribers {
// Adding to subscribers // Adding to subscribers
let object_id = find_object_id(&activity.object)?; let username = parse_local_actor_id(
let username = parse_local_actor_id(&config.instance_url(), &object_id)?; &config.instance_url(),
&activity.object,
)?;
let user = get_user_by_name(db_client, &username).await?; let user = get_user_by_name(db_client, &username).await?;
subscribe_opt(db_client, &user.id, &actor_profile.id).await?; subscribe_opt(db_client, &user.id, &actor_profile.id).await?;
return Ok(Some(PERSON)); return Ok(Some(PERSON));

View file

@ -1,10 +1,9 @@
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,
identifiers::parse_local_actor_id, identifiers::parse_local_actor_id,
receiver::find_object_id,
vocabulary::PERSON, vocabulary::PERSON,
}; };
use crate::config::Config; use crate::config::Config;
@ -18,24 +17,31 @@ use crate::models::relationships::queries::unsubscribe;
use crate::models::users::queries::get_user_by_name; use crate::models::users::queries::get_user_by_name;
use super::{HandlerError, HandlerResult}; use super::{HandlerError, HandlerResult};
#[derive(Deserialize)]
struct Remove {
actor: String,
object: String,
target: String,
}
pub async fn handle_remove( pub async fn handle_remove(
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: Remove = serde_json::from_value(activity)
.map_err(|_| ValidationError("unexpected activity structure"))?; .map_err(|_| ValidationError("unexpected activity structure"))?;
let actor_profile = get_profile_by_remote_actor_id( let actor_profile = get_profile_by_remote_actor_id(
db_client, db_client,
&activity.actor, &activity.actor,
).await?; ).await?;
let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?; let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?;
let target_value = activity.target.ok_or(ValidationError("target is missing"))?; if Some(activity.target) == actor.subscribers {
let target_id = find_object_id(&target_value)?;
if Some(target_id) == actor.subscribers {
// Removing from subscribers // Removing from subscribers
let object_id = find_object_id(&activity.object)?; let username = parse_local_actor_id(
let username = parse_local_actor_id(&config.instance_url(), &object_id)?; &config.instance_url(),
&activity.object,
)?;
let user = get_user_by_name(db_client, &username).await?; let user = get_user_by_name(db_client, &username).await?;
// actor is recipient, user is sender // actor is recipient, user is sender
match unsubscribe(db_client, &user.id, &actor_profile.id).await { match unsubscribe(db_client, &user.id, &actor_profile.id).await {