refactor(application): move local/remote follow routing out of presentation handler
This commit is contained in:
@@ -3,8 +3,11 @@ use domain::{
|
|||||||
errors::DomainError,
|
errors::DomainError,
|
||||||
events::DomainEvent,
|
events::DomainEvent,
|
||||||
models::social::{Block, Boost, Follow, FollowState, Like},
|
models::social::{Block, Boost, Follow, FollowState, Like},
|
||||||
ports::{BlockRepository, BoostRepository, EventPublisher, FollowRepository, LikeRepository},
|
ports::{
|
||||||
value_objects::{BoostId, LikeId, ThoughtId, UserId},
|
BlockRepository, BoostRepository, EventPublisher, FederationActionPort, FollowRepository,
|
||||||
|
LikeRepository, UserRepository,
|
||||||
|
},
|
||||||
|
value_objects::{BoostId, LikeId, ThoughtId, UserId, Username},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn like_thought(
|
pub async fn like_thought(
|
||||||
@@ -87,6 +90,27 @@ pub async fn unboost_thought(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn follow_actor(
|
||||||
|
follows: &dyn FollowRepository,
|
||||||
|
users: &dyn UserRepository,
|
||||||
|
federation: &dyn FederationActionPort,
|
||||||
|
events: &dyn EventPublisher,
|
||||||
|
follower_id: &UserId,
|
||||||
|
username: &str,
|
||||||
|
) -> Result<(), DomainError> {
|
||||||
|
if username.contains('@') {
|
||||||
|
federation.follow_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)?;
|
||||||
|
follow_user(follows, events, follower_id, &target.id).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn follow_user(
|
pub async fn follow_user(
|
||||||
follows: &dyn FollowRepository,
|
follows: &dyn FollowRepository,
|
||||||
events: &dyn EventPublisher,
|
events: &dyn EventPublisher,
|
||||||
@@ -315,6 +339,37 @@ mod tests {
|
|||||||
assert!(matches!(err, DomainError::InvalidInput(_)));
|
assert!(matches!(err, DomainError::InvalidInput(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn follow_actor_local_routes_to_follow_user() {
|
||||||
|
let store = TestStore::default();
|
||||||
|
let alice = user("alice");
|
||||||
|
let bob = user("bob");
|
||||||
|
store.users.lock().unwrap().push(bob.clone());
|
||||||
|
follow_actor(&store, &store, &store, &store, &alice.id, "bob")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(store.follows.lock().unwrap().len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn follow_actor_remote_routes_to_federation() {
|
||||||
|
let store = TestStore::default();
|
||||||
|
let alice = user("alice");
|
||||||
|
follow_actor(
|
||||||
|
&store,
|
||||||
|
&store,
|
||||||
|
&store,
|
||||||
|
&store,
|
||||||
|
&alice.id,
|
||||||
|
"@bob@example.com",
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
// TestStore.follow_remote is a no-op that returns Ok(())
|
||||||
|
// no local follow should be recorded
|
||||||
|
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();
|
||||||
|
|||||||
@@ -57,12 +57,15 @@ pub async fn post_follow(
|
|||||||
AuthUser(uid): AuthUser,
|
AuthUser(uid): AuthUser,
|
||||||
Path(username): Path<String>,
|
Path(username): Path<String>,
|
||||||
) -> Result<StatusCode, ApiError> {
|
) -> Result<StatusCode, ApiError> {
|
||||||
if username.contains('@') {
|
follow_actor(
|
||||||
s.federation.follow_remote(&uid, &username).await?;
|
&*s.follows,
|
||||||
} else {
|
&*s.users,
|
||||||
let target = get_user_by_username(&*s.users, &username).await?;
|
&*s.federation,
|
||||||
follow_user(&*s.follows, &*s.events, &uid, &target.id).await?;
|
&*s.events,
|
||||||
}
|
&uid,
|
||||||
|
&username,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
|
|||||||
Reference in New Issue
Block a user