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 22:50:57 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
2022-07-14 00:33:41 +00:00
|
|
|
builders::accept_follow::prepare_accept_follow,
|
2022-10-23 22:18:01 +00:00
|
|
|
fetcher::helpers::get_or_import_profile_by_actor_id,
|
2022-07-16 01:49:27 +00:00
|
|
|
identifiers::parse_local_actor_id,
|
2022-12-06 21:36:51 +00:00
|
|
|
receiver::deserialize_into_object_id,
|
2022-05-30 22:50:57 +00:00
|
|
|
vocabulary::PERSON,
|
|
|
|
};
|
|
|
|
use crate::config::Config;
|
2022-12-03 22:09:42 +00:00
|
|
|
use crate::database::DatabaseError;
|
2022-12-09 21:16:53 +00:00
|
|
|
use crate::errors::ValidationError;
|
2022-12-12 23:27:01 +00:00
|
|
|
use crate::models::{
|
|
|
|
relationships::queries::{
|
|
|
|
create_remote_follow_request_opt,
|
|
|
|
follow_request_accepted,
|
|
|
|
},
|
|
|
|
users::queries::get_user_by_name,
|
|
|
|
};
|
2022-10-23 22:18:01 +00:00
|
|
|
use super::{HandlerError, HandlerResult};
|
2022-05-30 22:50:57 +00:00
|
|
|
|
2022-12-06 21:36:51 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Follow {
|
|
|
|
id: String,
|
|
|
|
actor: String,
|
|
|
|
#[serde(deserialize_with = "deserialize_into_object_id")]
|
|
|
|
object: String,
|
|
|
|
}
|
|
|
|
|
2022-05-30 22:50:57 +00:00
|
|
|
pub async fn handle_follow(
|
|
|
|
config: &Config,
|
|
|
|
db_client: &mut impl GenericClient,
|
2022-12-09 21:16:53 +00:00
|
|
|
activity: Value,
|
2022-05-30 22:50:57 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-06 21:36:51 +00:00
|
|
|
// Follow(Person)
|
|
|
|
let activity: Follow = serde_json::from_value(activity)
|
2022-12-09 21:16:53 +00:00
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2022-05-30 22:50:57 +00:00
|
|
|
let source_profile = get_or_import_profile_by_actor_id(
|
|
|
|
db_client,
|
|
|
|
&config.instance(),
|
|
|
|
&config.media_dir(),
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
|
|
|
let source_actor = source_profile.actor_json
|
2022-10-23 22:18:01 +00:00
|
|
|
.ok_or(HandlerError::LocalObject)?;
|
2022-07-16 01:49:27 +00:00
|
|
|
let target_username = parse_local_actor_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 22:50:57 +00:00
|
|
|
let target_user = get_user_by_name(db_client, &target_username).await?;
|
2022-12-12 23:27:01 +00:00
|
|
|
let follow_request = create_remote_follow_request_opt(
|
|
|
|
db_client,
|
|
|
|
&source_profile.id,
|
|
|
|
&target_user.id,
|
|
|
|
&activity.id,
|
|
|
|
).await?;
|
|
|
|
match follow_request_accepted(db_client, &follow_request.id).await {
|
2022-05-30 22:50:57 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
// Proceed even if relationship already exists
|
|
|
|
Err(DatabaseError::AlreadyExists(_)) => (),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
|
2022-12-06 21:36:51 +00:00
|
|
|
// Send Accept(Follow)
|
2022-07-14 00:33:41 +00:00
|
|
|
prepare_accept_follow(
|
2022-10-22 00:15:28 +00:00
|
|
|
&config.instance(),
|
2022-07-14 00:33:41 +00:00
|
|
|
&target_user,
|
|
|
|
&source_actor,
|
2022-05-30 22:50:57 +00:00
|
|
|
&activity.id,
|
2022-07-14 00:33:41 +00:00
|
|
|
).spawn_deliver();
|
|
|
|
|
2022-05-30 22:50:57 +00:00
|
|
|
Ok(Some(PERSON))
|
|
|
|
}
|