diff --git a/crates/adapters/activitypub-base/src/service.rs b/crates/adapters/activitypub-base/src/service.rs index 2372951..d444b58 100644 --- a/crates/adapters/activitypub-base/src/service.rs +++ b/crates/adapters/activitypub-base/src/service.rs @@ -1418,6 +1418,21 @@ impl domain::ports::FederationActionPort for ActivityPubService { .map_err(|e| domain::errors::DomainError::ExternalService(e.to_string())) } + async fn unfollow_remote( + &self, + local_user_id: &domain::value_objects::UserId, + handle: &str, + ) -> Result<(), domain::errors::DomainError> { + let data = self.federation_config.to_request_data(); + let remote_actor: DbActor = webfinger_resolve_actor(handle, &data).await.map_err(|e| { + domain::errors::DomainError::ExternalService(anyhow::anyhow!("{e}").to_string()) + })?; + let actor_url = remote_actor.ap_id.to_string(); + self.unfollow(local_user_id.as_uuid(), &actor_url) + .await + .map_err(|e| domain::errors::DomainError::ExternalService(e.to_string())) + } + async fn actor_json( &self, user_id: &domain::value_objects::UserId, diff --git a/crates/application/src/use_cases/social.rs b/crates/application/src/use_cases/social.rs index aff30a3..97710ce 100644 --- a/crates/application/src/use_cases/social.rs +++ b/crates/application/src/use_cases/social.rs @@ -137,6 +137,27 @@ pub async fn follow_user( Ok(()) } +pub async fn unfollow_actor( + follows: &dyn FollowRepository, + users: &dyn UserRepository, + federation: &dyn FederationActionPort, + events: &dyn EventPublisher, + follower_id: &UserId, + username: &str, +) -> Result<(), DomainError> { + if username.contains('@') { + federation.unfollow_remote(follower_id, username).await + } else { + let uname = Username::new(username) + .map_err(|_| DomainError::InvalidInput("invalid username".into()))?; + let target = users + .find_by_username(&uname) + .await? + .ok_or(DomainError::NotFound)?; + unfollow_user(follows, events, follower_id, &target.id).await + } +} + pub async fn unfollow_user( follows: &dyn FollowRepository, events: &dyn EventPublisher, @@ -370,6 +391,48 @@ mod tests { assert!(store.follows.lock().unwrap().is_empty()); } + #[tokio::test] + async fn unfollow_actor_local_routes_to_unfollow_user() { + let store = TestStore::default(); + let alice = user("alice"); + let bob = user("bob"); + store.users.lock().unwrap().push(bob.clone()); + // Create an existing follow first + store + .follows + .lock() + .unwrap() + .push(domain::models::social::Follow { + follower_id: alice.id.clone(), + following_id: bob.id.clone(), + state: domain::models::social::FollowState::Accepted, + ap_id: None, + created_at: chrono::Utc::now(), + }); + unfollow_actor(&store, &store, &store, &store, &alice.id, "bob") + .await + .unwrap(); + assert!(store.follows.lock().unwrap().is_empty()); + } + + #[tokio::test] + async fn unfollow_actor_remote_routes_to_federation() { + let store = TestStore::default(); + let alice = user("alice"); + unfollow_actor( + &store, + &store, + &store, + &store, + &alice.id, + "@bob@example.com", + ) + .await + .unwrap(); + // TestStore.unfollow_remote is a no-op — just verify it doesn't error + assert!(store.follows.lock().unwrap().is_empty()); + } + #[tokio::test] async fn boost_and_unboost() { let store = TestStore::default(); diff --git a/crates/domain/src/ports.rs b/crates/domain/src/ports.rs index ef67625..c2db885 100644 --- a/crates/domain/src/ports.rs +++ b/crates/domain/src/ports.rs @@ -223,6 +223,11 @@ pub trait RemoteActorConnectionRepository: Send + Sync { pub trait FederationActionPort: Send + Sync { async fn lookup_actor(&self, handle: &str) -> Result; async fn follow_remote(&self, local_user_id: &UserId, handle: &str) -> Result<(), DomainError>; + async fn unfollow_remote( + &self, + local_user_id: &UserId, + handle: &str, + ) -> Result<(), DomainError>; async fn actor_json(&self, user_id: &UserId) -> Result; async fn followers_collection_json( &self, diff --git a/crates/domain/src/testing.rs b/crates/domain/src/testing.rs index 494f46d..a1031b3 100644 --- a/crates/domain/src/testing.rs +++ b/crates/domain/src/testing.rs @@ -548,6 +548,14 @@ impl FederationActionPort for TestStore { Ok(()) } + async fn unfollow_remote( + &self, + _local_user_id: &UserId, + _handle: &str, + ) -> Result<(), DomainError> { + Ok(()) + } + async fn actor_json(&self, _user_id: &UserId) -> Result { Err(DomainError::NotFound) } diff --git a/crates/presentation/src/handlers/social.rs b/crates/presentation/src/handlers/social.rs index 8648c98..a421f7b 100644 --- a/crates/presentation/src/handlers/social.rs +++ b/crates/presentation/src/handlers/social.rs @@ -79,13 +79,15 @@ pub async fn delete_follow( AuthUser(uid): AuthUser, Path(username): Path, ) -> Result { - if username.contains('@') { - return Err(ApiError::BadRequest( - "remote unfollow not yet supported".into(), - )); - } - let target = get_user_by_username(&*s.users, &username).await?; - unfollow_user(&*s.follows, &*s.events, &uid, &target.id).await?; + unfollow_actor( + &*s.follows, + &*s.users, + &*s.federation, + &*s.events, + &uid, + &username, + ) + .await?; Ok(StatusCode::NO_CONTENT) } #[utoipa::path(post, path = "/users/{username}/block", params(("username" = String, Path, description = "Username")), responses((status = 204, description = "Blocked")), security(("bearer_auth" = [])))]