diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index b26da66..9f717af 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -35,7 +35,7 @@ use crate::models::relationships::queries::{ follow, unfollow, }; -use crate::models::users::queries::get_user_by_id; +use crate::models::users::queries::get_user_by_name; use super::activity::{Object, Activity, create_activity_accept_follow}; use super::actor::Actor; use super::deliverer::deliver_activity; @@ -476,19 +476,21 @@ pub async fn receive_activity( .ok_or(HttpError::InternalError)?; let target_actor_id = get_object_id(activity.object)?; let target_username = parse_actor_id(&config.instance_url(), &target_actor_id)?; - let target_profile = get_profile_by_acct(db_client, &target_username).await?; - // Create and send 'Accept' activity - let target_user = get_user_by_id(db_client, &target_profile.id).await?; + 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 let new_activity = create_activity_accept_follow( &config.instance_url(), - &target_profile, + &target_user.profile, &activity.id, &source_actor.id, ); - // Save relationship - follow(db_client, &source_profile.id, &target_profile.id).await?; - - // Send activity let recipients = vec![source_actor]; deliver_activity(config, &target_user, new_activity, recipients); PERSON