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

View file

@ -1,12 +1,12 @@
use serde::Deserialize;
use serde_json::Value;
use tokio_postgres::GenericClient;
use crate::activitypub::{
activity::Activity,
builders::accept_follow::prepare_accept_follow,
fetcher::helpers::get_or_import_profile_by_actor_id,
identifiers::parse_local_actor_id,
receiver::find_object_id,
receiver::deserialize_into_object_id,
vocabulary::PERSON,
};
use crate::config::Config;
@ -16,12 +16,21 @@ use crate::models::relationships::queries::follow;
use crate::models::users::queries::get_user_by_name;
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(
config: &Config,
db_client: &mut impl GenericClient,
activity: Value,
) -> 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"))?;
let source_profile = get_or_import_profile_by_actor_id(
db_client,
@ -31,10 +40,9 @@ pub async fn handle_follow(
).await?;
let source_actor = source_profile.actor_json
.ok_or(HandlerError::LocalObject)?;
let target_actor_id = find_object_id(&activity.object)?;
let target_username = parse_local_actor_id(
&config.instance_url(),
&target_actor_id,
&activity.object,
)?;
let target_user = get_user_by_name(db_client, &target_username).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()),
};
// Send activity
// Send Accept(Follow)
prepare_accept_follow(
&config.instance(),
&target_user,

View file

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