2022-05-30 23:08:49 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
activity::Activity,
|
2022-07-16 01:49:27 +00:00
|
|
|
identifiers::parse_local_object_id,
|
|
|
|
receiver::find_object_id,
|
2022-05-30 23:08:49 +00:00
|
|
|
vocabulary::FOLLOW,
|
|
|
|
};
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::errors::ValidationError;
|
2022-10-15 12:32:27 +00:00
|
|
|
use crate::models::profiles::queries::get_profile_by_remote_actor_id;
|
2022-05-30 23:08:49 +00:00
|
|
|
use crate::models::relationships::queries::{
|
|
|
|
follow_request_accepted,
|
|
|
|
get_follow_request_by_id,
|
|
|
|
};
|
2022-09-12 23:36:35 +00:00
|
|
|
use crate::models::relationships::types::FollowRequestStatus;
|
2022-05-30 23:08:49 +00:00
|
|
|
use super::HandlerResult;
|
|
|
|
|
2022-12-07 20:26:51 +00:00
|
|
|
pub async fn handle_accept(
|
2022-05-30 23:08:49 +00:00
|
|
|
config: &Config,
|
|
|
|
db_client: &mut impl GenericClient,
|
|
|
|
activity: Activity,
|
|
|
|
) -> HandlerResult {
|
2022-12-07 20:26:51 +00:00
|
|
|
// Accept(Follow)
|
2022-10-15 12:32:27 +00:00
|
|
|
let actor_profile = get_profile_by_remote_actor_id(
|
|
|
|
db_client,
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-07-15 17:31:02 +00:00
|
|
|
let object_id = find_object_id(&activity.object)?;
|
2022-07-16 01:49:27 +00:00
|
|
|
let follow_request_id = parse_local_object_id(
|
|
|
|
&config.instance_url(),
|
|
|
|
&object_id,
|
|
|
|
)?;
|
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))
|
|
|
|
}
|