refactor: group use cases into DDD bounded contexts
Flat use_cases/ (44 files) + monolithic commands.rs/queries.rs split into diary/, movies/, watchlist/, import/, auth/, users/, integrations/, search/, person/, federation/ — each with own commands.rs, queries.rs, and use case modules. Inline tests extracted to sibling tests/ dirs.
This commit is contained in:
17
crates/application/src/users/commands.rs
Normal file
17
crates/application/src/users/commands.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct UpdateProfileCommand {
|
||||
pub user_id: Uuid,
|
||||
pub display_name: Option<String>,
|
||||
pub bio: Option<String>,
|
||||
pub avatar_bytes: Option<Vec<u8>>,
|
||||
pub avatar_content_type: Option<String>,
|
||||
pub banner_bytes: Option<Vec<u8>>,
|
||||
pub banner_content_type: Option<String>,
|
||||
pub also_known_as: Option<String>,
|
||||
}
|
||||
|
||||
pub struct UpdateProfileFieldsCommand {
|
||||
pub user_id: Uuid,
|
||||
pub fields: Vec<domain::models::ProfileField>,
|
||||
}
|
||||
32
crates/application/src/users/get_current_profile.rs
Normal file
32
crates/application/src/users/get_current_profile.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use domain::errors::DomainError;
|
||||
|
||||
use crate::{context::AppContext, users::queries::GetCurrentProfileQuery};
|
||||
|
||||
pub struct CurrentProfileData {
|
||||
pub username: String,
|
||||
pub bio: Option<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
query: GetCurrentProfileQuery,
|
||||
) -> Result<CurrentProfileData, DomainError> {
|
||||
let user_id = domain::value_objects::UserId::from_uuid(query.user_id);
|
||||
let user = ctx
|
||||
.repos
|
||||
.user
|
||||
.find_by_id(&user_id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("User not found".into()))?;
|
||||
|
||||
let avatar_url = user
|
||||
.avatar_path()
|
||||
.map(|path| format!("{}/images/{}", ctx.config.base_url, path));
|
||||
|
||||
Ok(CurrentProfileData {
|
||||
username: user.username().value().to_string(),
|
||||
bio: user.bio().map(|s| s.to_string()),
|
||||
avatar_url,
|
||||
})
|
||||
}
|
||||
192
crates/application/src/users/get_profile.rs
Normal file
192
crates/application/src/users/get_profile.rs
Normal file
@@ -0,0 +1,192 @@
|
||||
use crate::{
|
||||
context::AppContext,
|
||||
users::queries::{GetUserProfileQuery, ProfileView},
|
||||
};
|
||||
use chrono::Datelike;
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
DiaryEntry, DiaryFilter, MonthActivity, SortDirection, UserStats, UserTrends,
|
||||
collections::{PageParams, Paginated},
|
||||
},
|
||||
ports::FeedSortBy,
|
||||
value_objects::UserId,
|
||||
};
|
||||
|
||||
pub struct PendingFollowerView {
|
||||
pub url: String,
|
||||
pub handle: String,
|
||||
pub display_name: Option<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
}
|
||||
|
||||
pub struct UserProfileData {
|
||||
pub stats: UserStats,
|
||||
pub entries: Option<Paginated<DiaryEntry>>,
|
||||
pub history: Option<Vec<MonthActivity>>,
|
||||
pub trends: Option<UserTrends>,
|
||||
pub following_count: usize,
|
||||
pub followers_count: usize,
|
||||
pub pending_followers: Vec<PendingFollowerView>,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
query: GetUserProfileQuery,
|
||||
) -> Result<UserProfileData, DomainError> {
|
||||
let user_id = UserId::from_uuid(query.user_id);
|
||||
let stats = ctx.repos.stats.get_user_stats(&user_id).await?;
|
||||
|
||||
let (following_count, followers_count, pending_followers) =
|
||||
load_social_counts(ctx, query.user_id, query.is_own_profile).await;
|
||||
|
||||
let base = |entries, history, trends| UserProfileData {
|
||||
stats,
|
||||
entries,
|
||||
history,
|
||||
trends,
|
||||
following_count,
|
||||
followers_count,
|
||||
pending_followers,
|
||||
};
|
||||
|
||||
match query.view {
|
||||
ProfileView::History => {
|
||||
let all_entries = ctx.repos.diary.get_user_history(&user_id).await?;
|
||||
let history = group_by_month(all_entries);
|
||||
Ok(base(None, Some(history), None))
|
||||
}
|
||||
ProfileView::Trends => {
|
||||
let trends = ctx.repos.stats.get_user_trends(&user_id).await?;
|
||||
Ok(base(None, None, Some(trends)))
|
||||
}
|
||||
ProfileView::Ratings | ProfileView::Recent => {
|
||||
let sort_direction = feed_sort_to_direction(query.sort_by);
|
||||
let filter = paged_user_filter(
|
||||
user_id,
|
||||
sort_direction,
|
||||
query.limit,
|
||||
query.offset,
|
||||
query.search.clone(),
|
||||
)?;
|
||||
let entries = ctx.repos.diary.query_diary(&filter).await?;
|
||||
Ok(base(Some(entries), None, None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_social_counts(
|
||||
_ctx: &AppContext,
|
||||
_user_id: uuid::Uuid,
|
||||
_is_own_profile: bool,
|
||||
) -> (usize, usize, Vec<PendingFollowerView>) {
|
||||
#[cfg(not(feature = "federation"))]
|
||||
{
|
||||
(0, 0, vec![])
|
||||
}
|
||||
#[cfg(feature = "federation")]
|
||||
{
|
||||
if !_is_own_profile {
|
||||
return (0, 0, vec![]);
|
||||
}
|
||||
let following = _ctx
|
||||
.repos
|
||||
.social_query
|
||||
.count_following(_user_id)
|
||||
.await
|
||||
.unwrap_or(0);
|
||||
let followers = _ctx
|
||||
.repos
|
||||
.social_query
|
||||
.count_accepted_followers(_user_id)
|
||||
.await
|
||||
.unwrap_or(0);
|
||||
let pending = _ctx
|
||||
.repos
|
||||
.social_query
|
||||
.get_pending_followers(_user_id)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|p| PendingFollowerView {
|
||||
url: p.url,
|
||||
handle: p.handle,
|
||||
display_name: p.display_name,
|
||||
avatar_url: p.avatar_url,
|
||||
})
|
||||
.collect();
|
||||
(following, followers, pending)
|
||||
}
|
||||
}
|
||||
|
||||
fn feed_sort_to_direction(sort_by: FeedSortBy) -> SortDirection {
|
||||
match sort_by {
|
||||
FeedSortBy::Date => SortDirection::Descending,
|
||||
FeedSortBy::DateAsc => SortDirection::Ascending,
|
||||
FeedSortBy::Rating => SortDirection::ByRatingDesc,
|
||||
FeedSortBy::RatingAsc => SortDirection::ByRatingAsc,
|
||||
}
|
||||
}
|
||||
|
||||
fn paged_user_filter(
|
||||
user_id: UserId,
|
||||
sort_by: SortDirection,
|
||||
limit: Option<u32>,
|
||||
offset: Option<u32>,
|
||||
search: Option<String>,
|
||||
) -> Result<DiaryFilter, DomainError> {
|
||||
let page = PageParams::new(limit, offset)?;
|
||||
Ok(DiaryFilter {
|
||||
sort_by,
|
||||
page,
|
||||
movie_id: None,
|
||||
user_id: Some(user_id),
|
||||
search,
|
||||
})
|
||||
}
|
||||
|
||||
fn group_by_month(entries: Vec<DiaryEntry>) -> Vec<MonthActivity> {
|
||||
use std::collections::BTreeMap;
|
||||
let mut map: BTreeMap<(i32, u32), Vec<DiaryEntry>> = BTreeMap::new();
|
||||
for entry in entries {
|
||||
let watched_at = entry.review().watched_at();
|
||||
let year = watched_at.year();
|
||||
let month = watched_at.month();
|
||||
map.entry((year, month)).or_default().push(entry);
|
||||
}
|
||||
map.into_iter()
|
||||
.rev()
|
||||
.map(|((year, month), entries)| {
|
||||
let year_month = format!("{:04}-{:02}", year, month);
|
||||
MonthActivity {
|
||||
month_label: format_year_month_long(&year_month),
|
||||
count: entries.len() as i64,
|
||||
entries,
|
||||
year_month,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn format_year_month_long(ym: &str) -> String {
|
||||
let parts: Vec<&str> = ym.splitn(2, '-').collect();
|
||||
if parts.len() != 2 {
|
||||
return ym.to_string();
|
||||
}
|
||||
let month = match parts[1] {
|
||||
"01" => "January",
|
||||
"02" => "February",
|
||||
"03" => "March",
|
||||
"04" => "April",
|
||||
"05" => "May",
|
||||
"06" => "June",
|
||||
"07" => "July",
|
||||
"08" => "August",
|
||||
"09" => "September",
|
||||
"10" => "October",
|
||||
"11" => "November",
|
||||
"12" => "December",
|
||||
_ => parts[1],
|
||||
};
|
||||
format!("{} {}", month, parts[0])
|
||||
}
|
||||
28
crates/application/src/users/get_users.rs
Normal file
28
crates/application/src/users/get_users.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::{context::AppContext, users::queries::GetUsersQuery};
|
||||
use domain::{errors::DomainError, models::UserSummary, ports::RemoteActorInfo};
|
||||
|
||||
pub struct UsersListData {
|
||||
pub users: Vec<UserSummary>,
|
||||
pub remote_actors: Vec<RemoteActorInfo>,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
ctx: &AppContext,
|
||||
_query: GetUsersQuery,
|
||||
) -> Result<UsersListData, DomainError> {
|
||||
#[cfg(feature = "federation")]
|
||||
let (users_result, actors_result) = tokio::join!(
|
||||
ctx.repos.user.list_with_stats(),
|
||||
ctx.repos.social_query.list_all_followed_remote_actors()
|
||||
);
|
||||
#[cfg(not(feature = "federation"))]
|
||||
let (users_result, actors_result) = (
|
||||
ctx.repos.user.list_with_stats().await,
|
||||
Ok::<Vec<RemoteActorInfo>, DomainError>(vec![]),
|
||||
);
|
||||
|
||||
Ok(UsersListData {
|
||||
users: users_result?,
|
||||
remote_actors: actors_result?,
|
||||
})
|
||||
}
|
||||
7
crates/application/src/users/mod.rs
Normal file
7
crates/application/src/users/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod commands;
|
||||
pub mod get_current_profile;
|
||||
pub mod get_profile;
|
||||
pub mod get_users;
|
||||
pub mod queries;
|
||||
pub mod update_profile;
|
||||
pub mod update_profile_fields;
|
||||
50
crates/application/src/users/queries.rs
Normal file
50
crates/application/src/users/queries.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct GetUsersQuery;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub enum ProfileView {
|
||||
History,
|
||||
Trends,
|
||||
Ratings,
|
||||
#[default]
|
||||
Recent,
|
||||
}
|
||||
|
||||
impl ProfileView {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::History => "history",
|
||||
Self::Trends => "trends",
|
||||
Self::Ratings => "ratings",
|
||||
Self::Recent => "recent",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for ProfileView {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"history" => Ok(Self::History),
|
||||
"trends" => Ok(Self::Trends),
|
||||
"ratings" => Ok(Self::Ratings),
|
||||
"recent" => Ok(Self::Recent),
|
||||
other => Err(format!("unknown profile view: {other}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GetUserProfileQuery {
|
||||
pub user_id: Uuid,
|
||||
pub view: ProfileView,
|
||||
pub limit: Option<u32>,
|
||||
pub offset: Option<u32>,
|
||||
pub sort_by: domain::ports::FeedSortBy,
|
||||
pub search: Option<String>,
|
||||
pub is_own_profile: bool,
|
||||
}
|
||||
|
||||
pub struct GetCurrentProfileQuery {
|
||||
pub user_id: Uuid,
|
||||
}
|
||||
92
crates/application/src/users/update_profile.rs
Normal file
92
crates/application/src/users/update_profile.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
use domain::{errors::DomainError, events::DomainEvent, value_objects::UserId};
|
||||
|
||||
use crate::{context::AppContext, users::commands::UpdateProfileCommand};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: UpdateProfileCommand) -> Result<(), DomainError> {
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
|
||||
let user = ctx
|
||||
.repos
|
||||
.user
|
||||
.find_by_id(&user_id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("User not found".into()))?;
|
||||
|
||||
// Handle avatar
|
||||
let new_avatar_path = if let Some(bytes) = cmd.avatar_bytes {
|
||||
let content_type = cmd.avatar_content_type.as_deref().unwrap_or("");
|
||||
if !["image/jpeg", "image/png", "image/webp"].contains(&content_type) {
|
||||
return Err(DomainError::ValidationError(
|
||||
"Avatar must be jpeg, png, or webp".into(),
|
||||
));
|
||||
}
|
||||
if let Some(old_path) = user.avatar_path() {
|
||||
let _ = ctx.services.image_storage.delete(old_path).await;
|
||||
}
|
||||
let key = format!("avatars/{}", user_id.value());
|
||||
let stored = ctx.services.image_storage.store(&key, &bytes).await?;
|
||||
if let Err(e) = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::ImageStored {
|
||||
key: stored.clone(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
tracing::warn!("failed to emit ImageStored for avatar {stored}: {e}");
|
||||
}
|
||||
Some(stored)
|
||||
} else {
|
||||
user.avatar_path().map(|s| s.to_string())
|
||||
};
|
||||
|
||||
// Handle banner
|
||||
let new_banner_path = if let Some(bytes) = cmd.banner_bytes {
|
||||
let content_type = cmd.banner_content_type.as_deref().unwrap_or("");
|
||||
if !["image/jpeg", "image/png", "image/webp"].contains(&content_type) {
|
||||
return Err(DomainError::ValidationError(
|
||||
"Banner must be jpeg, png, or webp".into(),
|
||||
));
|
||||
}
|
||||
if let Some(old_path) = user.banner_path() {
|
||||
let _ = ctx.services.image_storage.delete(old_path).await;
|
||||
}
|
||||
let key = format!("banners/{}", user_id.value());
|
||||
let stored = ctx.services.image_storage.store(&key, &bytes).await?;
|
||||
if let Err(e) = ctx
|
||||
.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::ImageStored {
|
||||
key: stored.clone(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
tracing::warn!("failed to emit ImageStored for banner {stored}: {e}");
|
||||
}
|
||||
Some(stored)
|
||||
} else {
|
||||
user.banner_path().map(|s| s.to_string())
|
||||
};
|
||||
|
||||
ctx.repos
|
||||
.user
|
||||
.update_profile(
|
||||
&user_id,
|
||||
&domain::models::UserProfile {
|
||||
display_name: cmd.display_name,
|
||||
bio: cmd.bio,
|
||||
avatar_path: new_avatar_path,
|
||||
banner_path: new_banner_path,
|
||||
also_known_as: cmd.also_known_as,
|
||||
profile_fields: vec![],
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
ctx.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::UserUpdated { user_id })
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
21
crates/application/src/users/update_profile_fields.rs
Normal file
21
crates/application/src/users/update_profile_fields.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use domain::{errors::DomainError, events::DomainEvent, value_objects::UserId};
|
||||
|
||||
use crate::{context::AppContext, users::commands::UpdateProfileFieldsCommand};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: UpdateProfileFieldsCommand) -> Result<(), DomainError> {
|
||||
if cmd.fields.len() > 4 {
|
||||
return Err(DomainError::ValidationError(
|
||||
"Maximum 4 profile fields allowed".into(),
|
||||
));
|
||||
}
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
ctx.repos
|
||||
.profile_fields
|
||||
.set_fields(&user_id, cmd.fields)
|
||||
.await?;
|
||||
ctx.services
|
||||
.event_publisher
|
||||
.publish(&DomainEvent::UserUpdated { user_id })
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user