2022-12-06 21:36:51 +00:00
|
|
|
use serde::Deserialize;
|
2022-12-09 21:16:53 +00:00
|
|
|
use serde_json::Value;
|
2022-05-30 23:08:49 +00:00
|
|
|
|
2023-02-18 23:52:48 +00:00
|
|
|
use mitra_config::Config;
|
2023-03-30 20:27:17 +00:00
|
|
|
use mitra_models::{
|
|
|
|
database::DatabaseClient,
|
2023-01-19 20:34:46 +00:00
|
|
|
profiles::queries::get_profile_by_remote_actor_id,
|
|
|
|
relationships::queries::{
|
|
|
|
follow_request_accepted,
|
|
|
|
get_follow_request_by_id,
|
|
|
|
},
|
|
|
|
relationships::types::FollowRequestStatus,
|
2022-05-30 23:08:49 +00:00
|
|
|
};
|
2023-03-30 20:27:17 +00:00
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
identifiers::parse_local_object_id,
|
|
|
|
receiver::deserialize_into_object_id,
|
|
|
|
vocabulary::FOLLOW,
|
|
|
|
};
|
|
|
|
use crate::errors::ValidationError;
|
|
|
|
|
2022-05-30 23:08:49 +00:00
|
|
|
use super::HandlerResult;
|
|
|
|
|
2022-12-06 21:36:51 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Accept {
|
|
|
|
actor: String,
|
|
|
|
#[serde(deserialize_with = "deserialize_into_object_id")]
|
|
|
|
object: String,
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:26:51 +00:00
|
|
|
pub async fn handle_accept(
|
2022-05-30 23:08:49 +00:00
|
|
|
config: &Config,
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &mut impl DatabaseClient,
|
2022-12-09 21:16:53 +00:00
|
|
|
activity: Value,
|
2022-05-30 23:08:49 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-07 20:26:51 +00:00
|
|
|
// Accept(Follow)
|
2022-12-06 21:36:51 +00:00
|
|
|
let activity: Accept = serde_json::from_value(activity)
|
2022-12-09 21:16:53 +00:00
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2022-10-15 12:32:27 +00:00
|
|
|
let actor_profile = get_profile_by_remote_actor_id(
|
|
|
|
db_client,
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-07-16 01:49:27 +00:00
|
|
|
let follow_request_id = parse_local_object_id(
|
|
|
|
&config.instance_url(),
|
2022-12-06 21:36:51 +00:00
|
|
|
&activity.object,
|
2022-07-16 01:49:27 +00:00
|
|
|
)?;
|
2022-05-30 23:08:49 +00:00
|
|
|
let follow_request = get_follow_request_by_id(db_client, &follow_request_id).await?;
|
|
|
|
if follow_request.target_id != actor_profile.id {
|
|
|
|
return Err(ValidationError("actor is not a target").into());
|
|
|
|
};
|
2022-09-12 23:36:35 +00:00
|
|
|
if matches!(follow_request.request_status, FollowRequestStatus::Accepted) {
|
|
|
|
// Ignore Accept if follow request already accepted
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2022-05-30 23:08:49 +00:00
|
|
|
follow_request_accepted(db_client, &follow_request_id).await?;
|
|
|
|
Ok(Some(FOLLOW))
|
|
|
|
}
|