refactor: extract handler business logic into application use cases
Some checks failed
test / unit (push) Has been cancelled
lint / lint (push) Has been cancelled

11 handlers were calling repos/ports directly, bypassing the
application layer. Extracted into proper use cases:

feed: get_public_feed, get_user_feed, get_tag_feed, get_popular_tags
profile: get_user_profile (with follow check), list_users,
         count_local_users, list_local_followers, list_local_following
federation_management: set_also_known_as

Also registers 9 previously undocumented handlers in OpenAPI modules.
This commit is contained in:
2026-05-29 04:22:43 +02:00
parent 5b4b747dd7
commit bcd86fbfe7
8 changed files with 139 additions and 48 deletions

View File

@@ -10,7 +10,7 @@ use domain::{
ports::{
EventPublisher, FederationActionPort, FederationFollowPort, FederationFollowRequestPort,
FederationSchedulerPort, FeedOptions, FeedQuery, FeedRepository, FeedRequest,
FollowRepository, RemoteActorConnectionRepository, UserReader,
FollowRepository, RemoteActorConnectionRepository, UserReader, UserWriter,
},
value_objects::UserId,
};
@@ -187,5 +187,13 @@ pub async fn get_actor_connections_page(
Ok((items, has_more))
}
pub async fn set_also_known_as(
users: &dyn UserWriter,
user_id: &UserId,
value: Option<String>,
) -> Result<(), DomainError> {
users.set_also_known_as(user_id, value).await
}
#[cfg(test)]
mod tests;

View File

@@ -1,7 +1,7 @@
use domain::{
errors::DomainError,
models::feed::{FeedEntry, PageParams, Paginated},
ports::{FeedOptions, FeedQuery, FeedRepository, FeedRequest, FollowRepository},
ports::{FeedOptions, FeedQuery, FeedRepository, FeedRequest, FollowRepository, TagRepository},
value_objects::UserId,
};
@@ -20,3 +20,51 @@ pub async fn get_home_feed(
})
.await
}
pub async fn get_public_feed(
feed: &dyn FeedRepository,
viewer: Option<UserId>,
page: PageParams,
opts: FeedOptions,
) -> Result<Paginated<FeedEntry>, DomainError> {
feed.query(&FeedRequest {
query: FeedQuery::public(page, viewer),
options: opts,
})
.await
}
pub async fn get_user_feed(
feed: &dyn FeedRepository,
user_id: UserId,
page: PageParams,
opts: FeedOptions,
viewer: Option<UserId>,
) -> Result<Paginated<FeedEntry>, DomainError> {
feed.query(&FeedRequest {
query: FeedQuery::user(user_id, page, viewer),
options: opts,
})
.await
}
pub async fn get_tag_feed(
feed: &dyn FeedRepository,
tag: &str,
page: PageParams,
opts: FeedOptions,
viewer: Option<UserId>,
) -> Result<Paginated<FeedEntry>, DomainError> {
feed.query(&FeedRequest {
query: FeedQuery::tag(tag, page, viewer),
options: opts,
})
.await
}
pub async fn get_popular_tags(
tags: &dyn TagRepository,
limit: usize,
) -> Result<Vec<(String, i64)>, DomainError> {
tags.popular_tags(limit).await
}

View File

@@ -5,11 +5,13 @@ use domain::{
errors::DomainError,
events::DomainEvent,
models::{
feed::{PageParams, Paginated, UserSummary},
top_friend::TopFriend,
user::{UpdateProfileInput, User},
},
ports::{
EventPublisher, MediaStore, TopFriendRepository, UserReader, UserRepository, UserWriter,
EventPublisher, FollowRepository, MediaStore, TopFriendRepository, UserReader,
UserRepository, UserWriter,
},
value_objects::{UserId, Username},
};
@@ -230,5 +232,46 @@ pub async fn upload_banner(
.await
}
pub async fn get_user_profile(
users: &dyn UserReader,
follows: &dyn FollowRepository,
id_or_username: &str,
viewer_id: Option<&UserId>,
) -> Result<(User, bool), DomainError> {
let user = get_user_by_id_or_username(users, id_or_username).await?;
let is_followed = match viewer_id {
Some(vid) if vid != &user.id => follows.find(vid, &user.id).await?.is_some(),
_ => false,
};
Ok((user, is_followed))
}
pub async fn list_users(
users: &dyn UserReader,
page: PageParams,
) -> Result<Paginated<UserSummary>, DomainError> {
users.list_paginated(page).await
}
pub async fn count_local_users(users: &dyn UserReader) -> Result<i64, DomainError> {
users.count().await
}
pub async fn list_local_followers(
follows: &dyn FollowRepository,
user_id: &UserId,
page: PageParams,
) -> Result<Paginated<User>, DomainError> {
follows.list_followers(user_id, &page).await
}
pub async fn list_local_following(
follows: &dyn FollowRepository,
user_id: &UserId,
page: PageParams,
) -> Result<Paginated<User>, DomainError> {
follows.list_following(user_id, &page).await
}
#[cfg(test)]
mod tests;