feat: implement remote unfollow — wire FederationActionPort through delete_follow handler

This commit is contained in:
2026-05-15 02:46:24 +02:00
parent eebdbeaaf2
commit 031a22514d
5 changed files with 100 additions and 7 deletions

View File

@@ -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,

View File

@@ -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();

View File

@@ -223,6 +223,11 @@ pub trait RemoteActorConnectionRepository: Send + Sync {
pub trait FederationActionPort: Send + Sync {
async fn lookup_actor(&self, handle: &str) -> Result<RemoteActor, DomainError>;
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<String, DomainError>;
async fn followers_collection_json(
&self,

View File

@@ -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<String, DomainError> {
Err(DomainError::NotFound)
}

View File

@@ -79,13 +79,15 @@ pub async fn delete_follow(
AuthUser(uid): AuthUser,
Path(username): Path<String>,
) -> Result<StatusCode, ApiError> {
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" = [])))]