Create types for deserializing Add() and Remove() activities
This commit is contained in:
parent
f50609e4e5
commit
73e0b10a10
2 changed files with 28 additions and 16 deletions
|
@ -1,10 +1,9 @@
|
|||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use tokio_postgres::GenericClient;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::Activity,
|
||||
identifiers::parse_local_actor_id,
|
||||
receiver::find_object_id,
|
||||
vocabulary::PERSON,
|
||||
};
|
||||
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 super::{HandlerError, HandlerResult};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Add {
|
||||
actor: String,
|
||||
object: String,
|
||||
target: String,
|
||||
}
|
||||
|
||||
pub async fn handle_add(
|
||||
config: &Config,
|
||||
db_client: &mut impl GenericClient,
|
||||
activity: Value,
|
||||
) -> HandlerResult {
|
||||
let activity: Activity = serde_json::from_value(activity)
|
||||
let activity: Add = serde_json::from_value(activity)
|
||||
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||
let actor_profile = get_profile_by_remote_actor_id(
|
||||
db_client,
|
||||
&activity.actor,
|
||||
).await?;
|
||||
let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
||||
let target_value = activity.target.ok_or(ValidationError("target is missing"))?;
|
||||
let target_id = find_object_id(&target_value)?;
|
||||
if Some(target_id) == actor.subscribers {
|
||||
if Some(activity.target) == actor.subscribers {
|
||||
// Adding to subscribers
|
||||
let object_id = find_object_id(&activity.object)?;
|
||||
let username = parse_local_actor_id(&config.instance_url(), &object_id)?;
|
||||
let username = parse_local_actor_id(
|
||||
&config.instance_url(),
|
||||
&activity.object,
|
||||
)?;
|
||||
let user = get_user_by_name(db_client, &username).await?;
|
||||
subscribe_opt(db_client, &user.id, &actor_profile.id).await?;
|
||||
return Ok(Some(PERSON));
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use tokio_postgres::GenericClient;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::Activity,
|
||||
identifiers::parse_local_actor_id,
|
||||
receiver::find_object_id,
|
||||
vocabulary::PERSON,
|
||||
};
|
||||
use crate::config::Config;
|
||||
|
@ -18,24 +17,31 @@ use crate::models::relationships::queries::unsubscribe;
|
|||
use crate::models::users::queries::get_user_by_name;
|
||||
use super::{HandlerError, HandlerResult};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Remove {
|
||||
actor: String,
|
||||
object: String,
|
||||
target: String,
|
||||
}
|
||||
|
||||
pub async fn handle_remove(
|
||||
config: &Config,
|
||||
db_client: &mut impl GenericClient,
|
||||
activity: Value,
|
||||
) -> HandlerResult {
|
||||
let activity: Activity = serde_json::from_value(activity)
|
||||
let activity: Remove = serde_json::from_value(activity)
|
||||
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||
let actor_profile = get_profile_by_remote_actor_id(
|
||||
db_client,
|
||||
&activity.actor,
|
||||
).await?;
|
||||
let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
||||
let target_value = activity.target.ok_or(ValidationError("target is missing"))?;
|
||||
let target_id = find_object_id(&target_value)?;
|
||||
if Some(target_id) == actor.subscribers {
|
||||
if Some(activity.target) == actor.subscribers {
|
||||
// Removing from subscribers
|
||||
let object_id = find_object_id(&activity.object)?;
|
||||
let username = parse_local_actor_id(&config.instance_url(), &object_id)?;
|
||||
let username = parse_local_actor_id(
|
||||
&config.instance_url(),
|
||||
&activity.object,
|
||||
)?;
|
||||
let user = get_user_by_name(db_client, &username).await?;
|
||||
// actor is recipient, user is sender
|
||||
match unsubscribe(db_client, &user.id, &actor_profile.id).await {
|
||||
|
|
Loading…
Reference in a new issue