fedimovies/src/activitypub/handlers/reject.rs

51 lines
1.7 KiB
Rust
Raw Permalink Normal View History

use serde::Deserialize;
2022-12-09 21:16:53 +00:00
use serde_json::Value;
2023-04-25 13:49:35 +00:00
use fedimovies_config::Config;
use fedimovies_models::{
database::DatabaseClient,
2023-01-19 20:34:46 +00:00
profiles::queries::get_profile_by_remote_actor_id,
2023-04-24 15:35:32 +00:00
relationships::queries::{follow_request_rejected, get_follow_request_by_id},
2023-01-19 20:34:46 +00:00
relationships::types::FollowRequestStatus,
};
use crate::activitypub::{
2023-04-24 15:35:32 +00:00
identifiers::parse_local_object_id, receiver::deserialize_into_object_id, vocabulary::FOLLOW,
};
use crate::errors::ValidationError;
use super::HandlerResult;
#[derive(Deserialize)]
struct Reject {
actor: String,
#[serde(deserialize_with = "deserialize_into_object_id")]
object: String,
}
2022-12-07 20:26:51 +00:00
pub async fn handle_reject(
config: &Config,
db_client: &impl DatabaseClient,
2022-12-09 21:16:53 +00:00
activity: Value,
) -> HandlerResult {
2022-12-07 20:26:51 +00:00
// Reject(Follow)
2023-04-26 10:55:42 +00:00
let activity: Reject = serde_json::from_value(activity.clone()).map_err(|_| {
ValidationError(format!(
"unexpected Reject activity structure: {}",
activity
))
})?;
2023-04-24 15:35:32 +00:00
let actor_profile = get_profile_by_remote_actor_id(db_client, &activity.actor).await?;
let follow_request_id = parse_local_object_id(&config.instance_url(), &activity.object)?;
let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?;
if follow_request.target_id != actor_profile.id {
2023-04-26 10:55:42 +00:00
return Err(ValidationError("actor is not a target".to_string()).into());
};
if matches!(follow_request.request_status, FollowRequestStatus::Rejected) {
// Ignore Reject if follow request already rejected
return Ok(None);
};
follow_request_rejected(db_client, &follow_request_id).await?;
Ok(Some(FOLLOW))
}