fedimovies/src/activitypub/handlers/follow.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2022-12-09 21:16:53 +00:00
use serde_json::Value;
use tokio_postgres::GenericClient;
use crate::activitypub::{
activity::Activity,
builders::accept_follow::prepare_accept_follow,
2022-10-23 22:18:01 +00:00
fetcher::helpers::get_or_import_profile_by_actor_id,
identifiers::parse_local_actor_id,
receiver::find_object_id,
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;
use crate::models::relationships::queries::follow;
use crate::models::users::queries::get_user_by_name;
2022-10-23 22:18:01 +00:00
use super::{HandlerError, HandlerResult};
pub async fn handle_follow(
config: &Config,
db_client: &mut impl GenericClient,
2022-12-09 21:16:53 +00:00
activity: Value,
) -> HandlerResult {
2022-12-09 21:16:53 +00:00
let activity: Activity = serde_json::from_value(activity)
.map_err(|_| ValidationError("unexpected activity structure"))?;
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)?;
let target_actor_id = find_object_id(&activity.object)?;
let target_username = parse_local_actor_id(
&config.instance_url(),
&target_actor_id,
)?;
let target_user = get_user_by_name(db_client, &target_username).await?;
match follow(db_client, &source_profile.id, &target_user.profile.id).await {
Ok(_) => (),
// Proceed even if relationship already exists
Err(DatabaseError::AlreadyExists(_)) => (),
Err(other_error) => return Err(other_error.into()),
};
// Send activity
prepare_accept_follow(
&config.instance(),
&target_user,
&source_actor,
&activity.id,
).spawn_deliver();
Ok(Some(PERSON))
}