Create types for deserializing Follow(), Accept() and Reject() activities

This commit is contained in:
silverpill 2022-12-06 21:36:51 +00:00
parent 1efbf5a3fb
commit 72eabce15b
3 changed files with 36 additions and 16 deletions

View file

@ -1,10 +1,10 @@
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_object_id, identifiers::parse_local_object_id,
receiver::find_object_id, receiver::deserialize_into_object_id,
vocabulary::FOLLOW, vocabulary::FOLLOW,
}; };
use crate::config::Config; use crate::config::Config;
@ -17,22 +17,28 @@ use crate::models::relationships::queries::{
use crate::models::relationships::types::FollowRequestStatus; use crate::models::relationships::types::FollowRequestStatus;
use super::HandlerResult; use super::HandlerResult;
#[derive(Deserialize)]
struct Accept {
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
pub async fn handle_accept( pub async fn handle_accept(
config: &Config, config: &Config,
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,
activity: Value, activity: Value,
) -> HandlerResult { ) -> HandlerResult {
// Accept(Follow) // Accept(Follow)
let activity: Activity = serde_json::from_value(activity) let activity: Accept = 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 object_id = find_object_id(&activity.object)?;
let follow_request_id = parse_local_object_id( let follow_request_id = parse_local_object_id(
&config.instance_url(), &config.instance_url(),
&object_id, &activity.object,
)?; )?;
let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?; let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?;
if follow_request.target_id != actor_profile.id { if follow_request.target_id != actor_profile.id {

View file

@ -1,12 +1,12 @@
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,
builders::accept_follow::prepare_accept_follow, builders::accept_follow::prepare_accept_follow,
fetcher::helpers::get_or_import_profile_by_actor_id, fetcher::helpers::get_or_import_profile_by_actor_id,
identifiers::parse_local_actor_id, identifiers::parse_local_actor_id,
receiver::find_object_id, receiver::deserialize_into_object_id,
vocabulary::PERSON, vocabulary::PERSON,
}; };
use crate::config::Config; use crate::config::Config;
@ -16,12 +16,21 @@ use crate::models::relationships::queries::follow;
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 Follow {
id: String,
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
pub async fn handle_follow( pub async fn handle_follow(
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) // Follow(Person)
let activity: Follow = serde_json::from_value(activity)
.map_err(|_| ValidationError("unexpected activity structure"))?; .map_err(|_| ValidationError("unexpected activity structure"))?;
let source_profile = get_or_import_profile_by_actor_id( let source_profile = get_or_import_profile_by_actor_id(
db_client, db_client,
@ -31,10 +40,9 @@ pub async fn handle_follow(
).await?; ).await?;
let source_actor = source_profile.actor_json let source_actor = source_profile.actor_json
.ok_or(HandlerError::LocalObject)?; .ok_or(HandlerError::LocalObject)?;
let target_actor_id = find_object_id(&activity.object)?;
let target_username = parse_local_actor_id( let target_username = parse_local_actor_id(
&config.instance_url(), &config.instance_url(),
&target_actor_id, &activity.object,
)?; )?;
let target_user = get_user_by_name(db_client, &target_username).await?; let target_user = get_user_by_name(db_client, &target_username).await?;
match follow(db_client, &source_profile.id, &target_user.profile.id).await { match follow(db_client, &source_profile.id, &target_user.profile.id).await {
@ -44,7 +52,7 @@ pub async fn handle_follow(
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
// Send activity // Send Accept(Follow)
prepare_accept_follow( prepare_accept_follow(
&config.instance(), &config.instance(),
&target_user, &target_user,

View file

@ -1,10 +1,10 @@
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_object_id, identifiers::parse_local_object_id,
receiver::find_object_id, receiver::deserialize_into_object_id,
vocabulary::FOLLOW, vocabulary::FOLLOW,
}; };
use crate::config::Config; use crate::config::Config;
@ -17,22 +17,28 @@ use crate::models::relationships::queries::{
use crate::models::relationships::types::FollowRequestStatus; use crate::models::relationships::types::FollowRequestStatus;
use super::HandlerResult; use super::HandlerResult;
#[derive(Deserialize)]
struct Reject {
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
pub async fn handle_reject( pub async fn handle_reject(
config: &Config, config: &Config,
db_client: &impl GenericClient, db_client: &impl GenericClient,
activity: Value, activity: Value,
) -> HandlerResult { ) -> HandlerResult {
// Reject(Follow) // Reject(Follow)
let activity: Activity = serde_json::from_value(activity) let activity: Reject = 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 object_id = find_object_id(&activity.object)?;
let follow_request_id = parse_local_object_id( let follow_request_id = parse_local_object_id(
&config.instance_url(), &config.instance_url(),
&object_id, &activity.object,
)?; )?;
let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?; let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?;
if follow_request.target_id != actor_profile.id { if follow_request.target_id != actor_profile.id {