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
|
|
|
|
2023-04-25 13:49:35 +00:00
|
|
|
use fedimovies_config::Config;
|
|
|
|
use fedimovies_models::{
|
2023-03-30 20:27:17 +00:00
|
|
|
database::{DatabaseClient, DatabaseError},
|
2023-04-24 15:35:32 +00:00
|
|
|
relationships::queries::{create_remote_follow_request_opt, follow_request_accepted},
|
2023-03-30 20:27:17 +00:00
|
|
|
users::queries::get_user_by_name,
|
|
|
|
};
|
2023-02-18 23:52:48 +00:00
|
|
|
|
2023-04-24 15:35:32 +00:00
|
|
|
use super::{HandlerError, HandlerResult};
|
2022-05-30 22:50:57 +00:00
|
|
|
use crate::activitypub::{
|
2022-07-14 00:33:41 +00:00
|
|
|
builders::accept_follow::prepare_accept_follow,
|
2023-04-24 15:35:32 +00:00
|
|
|
fetcher::helpers::get_or_import_profile_by_actor_id, identifiers::parse_local_actor_id,
|
|
|
|
receiver::deserialize_into_object_id, vocabulary::PERSON,
|
2022-05-30 22:50:57 +00:00
|
|
|
};
|
2022-12-09 21:16:53 +00:00
|
|
|
use crate::errors::ValidationError;
|
2023-03-25 21:50:10 +00:00
|
|
|
use crate::media::MediaStorage;
|
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,
|
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 22:50:57 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-06 21:36:51 +00:00
|
|
|
// Follow(Person)
|
2023-04-26 10:55:42 +00:00
|
|
|
let activity: Follow = serde_json::from_value(activity.clone()).map_err(|_| {
|
|
|
|
ValidationError(format!(
|
|
|
|
"unexpected Follow activity structure: {}",
|
|
|
|
activity
|
|
|
|
))
|
|
|
|
})?;
|
2022-05-30 22:50:57 +00:00
|
|
|
let source_profile = get_or_import_profile_by_actor_id(
|
|
|
|
db_client,
|
|
|
|
&config.instance(),
|
2023-03-25 21:50:10 +00:00
|
|
|
&MediaStorage::from(config),
|
2022-05-30 22:50:57 +00:00
|
|
|
&activity.actor,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let source_actor = source_profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
|
|
|
let target_username = parse_local_actor_id(&config.instance_url(), &activity.object)?;
|
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,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2022-12-12 23:27:01 +00:00
|
|
|
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,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.enqueue(db_client)
|
|
|
|
.await?;
|
2022-07-14 00:33:41 +00:00
|
|
|
|
2022-05-30 22:50:57 +00:00
|
|
|
Ok(Some(PERSON))
|
|
|
|
}
|