feat: implement remote unfollow — wire FederationActionPort through delete_follow handler
This commit is contained in:
@@ -1418,6 +1418,21 @@ impl domain::ports::FederationActionPort for ActivityPubService {
|
|||||||
.map_err(|e| domain::errors::DomainError::ExternalService(e.to_string()))
|
.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(
|
async fn actor_json(
|
||||||
&self,
|
&self,
|
||||||
user_id: &domain::value_objects::UserId,
|
user_id: &domain::value_objects::UserId,
|
||||||
|
|||||||
@@ -137,6 +137,27 @@ pub async fn follow_user(
|
|||||||
Ok(())
|
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(
|
pub async fn unfollow_user(
|
||||||
follows: &dyn FollowRepository,
|
follows: &dyn FollowRepository,
|
||||||
events: &dyn EventPublisher,
|
events: &dyn EventPublisher,
|
||||||
@@ -370,6 +391,48 @@ mod tests {
|
|||||||
assert!(store.follows.lock().unwrap().is_empty());
|
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]
|
#[tokio::test]
|
||||||
async fn boost_and_unboost() {
|
async fn boost_and_unboost() {
|
||||||
let store = TestStore::default();
|
let store = TestStore::default();
|
||||||
|
|||||||
@@ -223,6 +223,11 @@ pub trait RemoteActorConnectionRepository: Send + Sync {
|
|||||||
pub trait FederationActionPort: Send + Sync {
|
pub trait FederationActionPort: Send + Sync {
|
||||||
async fn lookup_actor(&self, handle: &str) -> Result<RemoteActor, DomainError>;
|
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 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 actor_json(&self, user_id: &UserId) -> Result<String, DomainError>;
|
||||||
async fn followers_collection_json(
|
async fn followers_collection_json(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
@@ -548,6 +548,14 @@ impl FederationActionPort for TestStore {
|
|||||||
Ok(())
|
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> {
|
async fn actor_json(&self, _user_id: &UserId) -> Result<String, DomainError> {
|
||||||
Err(DomainError::NotFound)
|
Err(DomainError::NotFound)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,13 +79,15 @@ pub async fn delete_follow(
|
|||||||
AuthUser(uid): AuthUser,
|
AuthUser(uid): AuthUser,
|
||||||
Path(username): Path<String>,
|
Path(username): Path<String>,
|
||||||
) -> Result<StatusCode, ApiError> {
|
) -> Result<StatusCode, ApiError> {
|
||||||
if username.contains('@') {
|
unfollow_actor(
|
||||||
return Err(ApiError::BadRequest(
|
&*s.follows,
|
||||||
"remote unfollow not yet supported".into(),
|
&*s.users,
|
||||||
));
|
&*s.federation,
|
||||||
}
|
&*s.events,
|
||||||
let target = get_user_by_username(&*s.users, &username).await?;
|
&uid,
|
||||||
unfollow_user(&*s.follows, &*s.events, &uid, &target.id).await?;
|
&username,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
Ok(StatusCode::NO_CONTENT)
|
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" = [])))]
|
#[utoipa::path(post, path = "/users/{username}/block", params(("username" = String, Path, description = "Username")), responses((status = 204, description = "Blocked")), security(("bearer_auth" = [])))]
|
||||||
|
|||||||
Reference in New Issue
Block a user