refactor: fix HIGH+MEDIUM architectural violations from code review
HIGH: fix watch_medium data-loss bug, standardize error handling on ApiError, fix dep direction (rss/template-askama no longer dep on application), extract ImageFetcher port (remove reqwest from app layer), move event construction from save_review to use case, extract infra-wiring crate (DbPool/EventBusBackend dedup), deduplicate presentation helpers (encode_error, export streaming, multipart parsing) MEDIUM: split LocalApContentQuery god-trait 10→3 methods, dedup movie resolution orchestration, add RemoteActorDto/PersonDto mappers, move AppConfig to infra-wiring, fix SocialQueryPort Uuid→UserId, replace stringly-typed api-types with domain enums, move count_reviews_in_year to StatsRepository, dedup event publisher cfg blocks, extract should_enrich, move group_by_month to application, dedup count_local_posts, add FederationFlags Default, TUI input helper + ShowError rename + typed auth errors, api-types cleanup (UserSettingsDto/UserProfileBase/PreviewRowData) 102 files changed, -681 lines net
This commit is contained in:
@@ -19,6 +19,16 @@ pub struct FederationFlags {
|
||||
pub watchlist: bool,
|
||||
}
|
||||
|
||||
impl Default for FederationFlags {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
goals: true,
|
||||
reviews: true,
|
||||
watchlist: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FederatedProfile {
|
||||
pub actor_url: String,
|
||||
|
||||
@@ -57,7 +57,8 @@ pub use search::{
|
||||
|
||||
use crate::errors::DomainError;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum GoalType {
|
||||
Movies,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::value_objects::{Email, PasswordHash, UserId, Username};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum UserRole {
|
||||
#[default]
|
||||
Standard,
|
||||
|
||||
@@ -2,7 +2,6 @@ use async_trait::async_trait;
|
||||
|
||||
use crate::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::{
|
||||
DiaryEntry, DiaryFilter, ExportFormat, FeedEntry, FeedSortBy, FollowingFilter, MovieStats,
|
||||
Review, ReviewHistory, UserStats, UserTrends,
|
||||
@@ -43,7 +42,7 @@ pub trait DiaryRepository: Send + Sync {
|
||||
|
||||
#[async_trait]
|
||||
pub trait ReviewRepository: Send + Sync {
|
||||
async fn save_review(&self, review: &Review) -> Result<DomainEvent, DomainError>;
|
||||
async fn save_review(&self, review: &Review) -> Result<(), DomainError>;
|
||||
async fn get_review_by_id(&self, review_id: &ReviewId) -> Result<Option<Review>, DomainError>;
|
||||
async fn update_review(&self, review: &Review) -> Result<(), DomainError>;
|
||||
async fn delete_review(&self, review_id: &ReviewId) -> Result<(), DomainError>;
|
||||
@@ -54,6 +53,7 @@ pub trait ReviewRepository: Send + Sync {
|
||||
pub trait StatsRepository: Send + Sync {
|
||||
async fn get_user_stats(&self, user_id: &UserId) -> Result<UserStats, DomainError>;
|
||||
async fn get_user_trends(&self, user_id: &UserId) -> Result<UserTrends, DomainError>;
|
||||
async fn count_reviews_in_year(&self, user_id: &UserId, year: u16) -> Result<u32, DomainError>;
|
||||
}
|
||||
|
||||
pub trait DiaryExporter: Send + Sync {
|
||||
|
||||
@@ -17,5 +17,4 @@ pub trait GoalRepository: Send + Sync {
|
||||
year: u16,
|
||||
) -> Result<Option<Goal>, DomainError>;
|
||||
async fn list_for_user(&self, user_id: &UserId) -> Result<Vec<Goal>, DomainError>;
|
||||
async fn count_reviews_in_year(&self, user_id: &UserId, year: u16) -> Result<u32, DomainError>;
|
||||
}
|
||||
|
||||
8
crates/domain/src/ports/image_fetcher.rs
Normal file
8
crates/domain/src/ports/image_fetcher.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::errors::DomainError;
|
||||
|
||||
#[async_trait]
|
||||
pub trait ImageFetcher: Send + Sync {
|
||||
async fn fetch_image(&self, url: &str) -> Result<Vec<u8>, DomainError>;
|
||||
}
|
||||
@@ -3,12 +3,14 @@ pub mod diary;
|
||||
pub mod events;
|
||||
pub mod federated_profile;
|
||||
pub mod goals;
|
||||
pub mod image_fetcher;
|
||||
pub mod images;
|
||||
pub mod import;
|
||||
pub mod jobs;
|
||||
pub mod media_server;
|
||||
pub mod movie;
|
||||
pub mod person;
|
||||
pub mod rss;
|
||||
pub mod search;
|
||||
pub mod social;
|
||||
pub mod watchlist;
|
||||
@@ -19,12 +21,14 @@ pub use diary::*;
|
||||
pub use events::*;
|
||||
pub use federated_profile::*;
|
||||
pub use goals::*;
|
||||
pub use image_fetcher::*;
|
||||
pub use images::*;
|
||||
pub use import::*;
|
||||
pub use jobs::*;
|
||||
pub use media_server::*;
|
||||
pub use movie::*;
|
||||
pub use person::*;
|
||||
pub use rss::*;
|
||||
pub use search::*;
|
||||
pub use social::*;
|
||||
pub use watchlist::*;
|
||||
|
||||
5
crates/domain/src/ports/rss.rs
Normal file
5
crates/domain/src/ports/rss.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use crate::models::DiaryEntry;
|
||||
|
||||
pub trait RssFeedRenderer: Send + Sync {
|
||||
fn render_feed(&self, entries: &[DiaryEntry], title: &str) -> Result<String, String>;
|
||||
}
|
||||
@@ -4,24 +4,24 @@ use chrono::NaiveDateTime;
|
||||
use crate::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
DiaryEntry, FederationFlags, Goal, Movie, PendingFollowerInfo, RemoteActorInfo,
|
||||
RemoteGoalEntry, RemoteWatchlistEntry, Review, WatchlistWithMovie,
|
||||
DiaryEntry, FederationFlags, PendingFollowerInfo, RemoteActorInfo, RemoteGoalEntry,
|
||||
RemoteWatchlistEntry, WatchlistWithMovie,
|
||||
},
|
||||
value_objects::{MovieId, ReviewId, UserId},
|
||||
value_objects::{MovieId, UserId},
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
pub trait SocialQueryPort: Send + Sync {
|
||||
async fn get_accepted_following_urls(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
user_id: &UserId,
|
||||
) -> Result<Vec<String>, DomainError>;
|
||||
async fn list_all_followed_remote_actors(&self) -> Result<Vec<RemoteActorInfo>, DomainError>;
|
||||
async fn count_following(&self, user_id: uuid::Uuid) -> Result<usize, DomainError>;
|
||||
async fn count_accepted_followers(&self, user_id: uuid::Uuid) -> Result<usize, DomainError>;
|
||||
async fn count_following(&self, user_id: &UserId) -> Result<usize, DomainError>;
|
||||
async fn count_accepted_followers(&self, user_id: &UserId) -> Result<usize, DomainError>;
|
||||
async fn get_pending_followers(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
user_id: &UserId,
|
||||
) -> Result<Vec<PendingFollowerInfo>, DomainError>;
|
||||
}
|
||||
|
||||
@@ -60,25 +60,16 @@ pub trait RemoteGoalRepository: Send + Sync {
|
||||
async fn get_by_actor_url(&self, actor_url: &str) -> Result<Vec<RemoteGoalEntry>, DomainError>;
|
||||
}
|
||||
|
||||
/// Read-only query port used exclusively by the ActivityPub adapter.
|
||||
/// Consolidates all reads the AP adapter needs so it never touches write repositories.
|
||||
/// Federation-specific read-only queries that have no equivalent on the
|
||||
/// standard domain ports (e.g. unpaginated watchlist, local-only review
|
||||
/// listings). Generic lookups (get_movie_by_id, get_review_by_id, etc.)
|
||||
/// live on MovieRepository, ReviewRepository, and the other domain ports.
|
||||
#[async_trait]
|
||||
pub trait LocalApContentQuery: Send + Sync {
|
||||
async fn get_local_reviews_for_user(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> Result<Vec<DiaryEntry>, DomainError>;
|
||||
async fn get_local_watchlist_for_user(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> Result<Vec<WatchlistWithMovie>, DomainError>;
|
||||
async fn get_review_by_id(&self, review_id: &ReviewId) -> Result<Option<Review>, DomainError>;
|
||||
async fn get_movie_by_id(&self, movie_id: &MovieId) -> Result<Option<Movie>, DomainError>;
|
||||
async fn get_movie_by_external_metadata_id(
|
||||
&self,
|
||||
external_id: &str,
|
||||
) -> Result<Option<Movie>, DomainError>;
|
||||
async fn count_local_posts(&self) -> Result<u64, DomainError>;
|
||||
async fn get_local_reviews_for_movie(
|
||||
&self,
|
||||
movie_id: &MovieId,
|
||||
@@ -89,10 +80,4 @@ pub trait LocalApContentQuery: Send + Sync {
|
||||
before: Option<NaiveDateTime>,
|
||||
limit: usize,
|
||||
) -> Result<Vec<DiaryEntry>, DomainError>;
|
||||
async fn get_goal_with_progress(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
year: u16,
|
||||
) -> Result<Option<(Goal, u32)>, DomainError>;
|
||||
async fn list_goals_for_user(&self, user_id: &UserId) -> Result<Vec<Goal>, DomainError>;
|
||||
}
|
||||
|
||||
@@ -191,7 +191,24 @@ impl DiaryRepository for FakeDiaryRepository {
|
||||
|
||||
// ── FakeStatsRepository ─────────────────────────────────────────────────────
|
||||
|
||||
pub struct FakeStatsRepository;
|
||||
pub struct FakeStatsRepository {
|
||||
review_counts: Mutex<HashMap<(Uuid, u16), u32>>,
|
||||
}
|
||||
|
||||
impl FakeStatsRepository {
|
||||
pub fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
review_counts: Mutex::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_review_count(&self, user_id: Uuid, year: u16, count: u32) {
|
||||
self.review_counts
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((user_id, year), count);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl StatsRepository for FakeStatsRepository {
|
||||
@@ -211,6 +228,11 @@ impl StatsRepository for FakeStatsRepository {
|
||||
max_director_count: 0,
|
||||
})
|
||||
}
|
||||
|
||||
async fn count_reviews_in_year(&self, user_id: &UserId, year: u16) -> Result<u32, DomainError> {
|
||||
let counts = self.review_counts.lock().unwrap();
|
||||
Ok(counts.get(&(user_id.value(), year)).copied().unwrap_or(0))
|
||||
}
|
||||
}
|
||||
|
||||
// ── FakePersonQuery ─────────────────────────────────────────────────────────
|
||||
|
||||
@@ -10,7 +10,6 @@ use chrono::Utc;
|
||||
|
||||
use crate::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::{
|
||||
FederationFlags, Goal, ImportProfile, ImportSession, Movie, MovieFilter, MovieProfile,
|
||||
MovieSummary, ProfileField, RefreshSession, Review, User, UserSettings, UserSummary,
|
||||
@@ -171,18 +170,12 @@ impl InMemoryReviewRepository {
|
||||
|
||||
#[async_trait]
|
||||
impl ReviewRepository for InMemoryReviewRepository {
|
||||
async fn save_review(&self, review: &Review) -> Result<DomainEvent, DomainError> {
|
||||
async fn save_review(&self, review: &Review) -> Result<(), DomainError> {
|
||||
self.store
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(review.id().value(), review.clone());
|
||||
Ok(DomainEvent::ReviewLogged {
|
||||
review_id: review.id().clone(),
|
||||
movie_id: review.movie_id().clone(),
|
||||
user_id: review.user_id().clone(),
|
||||
rating: review.rating().clone(),
|
||||
watched_at: *review.watched_at(),
|
||||
})
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_review_by_id(&self, review_id: &ReviewId) -> Result<Option<Review>, DomainError> {
|
||||
@@ -345,27 +338,18 @@ impl WatchlistRepository for InMemoryWatchlistRepository {
|
||||
|
||||
pub struct InMemoryGoalRepository {
|
||||
store: Mutex<HashMap<Uuid, Goal>>,
|
||||
review_counts: Mutex<HashMap<(Uuid, u16), u32>>,
|
||||
}
|
||||
|
||||
impl InMemoryGoalRepository {
|
||||
pub fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
store: Mutex::new(HashMap::new()),
|
||||
review_counts: Mutex::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
self.store.lock().unwrap().len()
|
||||
}
|
||||
|
||||
pub fn set_review_count(&self, user_id: Uuid, year: u16, count: u32) {
|
||||
self.review_counts
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((user_id, year), count);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -416,11 +400,6 @@ impl GoalRepository for InMemoryGoalRepository {
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn count_reviews_in_year(&self, user_id: &UserId, year: u16) -> Result<u32, DomainError> {
|
||||
let counts = self.review_counts.lock().unwrap();
|
||||
Ok(counts.get(&(user_id.value(), year)).copied().unwrap_or(0))
|
||||
}
|
||||
}
|
||||
|
||||
// ── InMemoryUserSettingsRepository ──────────────────────────────────────────
|
||||
|
||||
@@ -97,7 +97,10 @@ pub struct NoopSocialQueryPort;
|
||||
|
||||
#[async_trait]
|
||||
impl crate::ports::SocialQueryPort for NoopSocialQueryPort {
|
||||
async fn get_accepted_following_urls(&self, _: uuid::Uuid) -> Result<Vec<String>, DomainError> {
|
||||
async fn get_accepted_following_urls(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<Vec<String>, DomainError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
async fn list_all_followed_remote_actors(
|
||||
@@ -105,15 +108,21 @@ impl crate::ports::SocialQueryPort for NoopSocialQueryPort {
|
||||
) -> Result<Vec<crate::models::RemoteActorInfo>, DomainError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
async fn count_following(&self, _: uuid::Uuid) -> Result<usize, DomainError> {
|
||||
async fn count_following(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<usize, DomainError> {
|
||||
Ok(0)
|
||||
}
|
||||
async fn count_accepted_followers(&self, _: uuid::Uuid) -> Result<usize, DomainError> {
|
||||
async fn count_accepted_followers(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<usize, DomainError> {
|
||||
Ok(0)
|
||||
}
|
||||
async fn get_pending_followers(
|
||||
&self,
|
||||
_: uuid::Uuid,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<Vec<crate::models::PendingFollowerInfo>, DomainError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
@@ -148,9 +157,6 @@ impl crate::ports::GoalRepository for NoopGoalRepository {
|
||||
async fn list_for_user(&self, _: &UserId) -> Result<Vec<crate::models::Goal>, DomainError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
async fn count_reviews_in_year(&self, _: &UserId, _: u16) -> Result<u32, DomainError> {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
// ── NoopUserSettingsRepository ────────────────────────────────────────────────
|
||||
|
||||
@@ -80,6 +80,9 @@ impl StatsRepository for PanicStatsRepository {
|
||||
async fn get_user_trends(&self, _: &UserId) -> Result<UserTrends, DomainError> {
|
||||
panic!("PanicStatsRepository called")
|
||||
}
|
||||
async fn count_reviews_in_year(&self, _: &UserId, _: u16) -> Result<u32, DomainError> {
|
||||
panic!("PanicStatsRepository called")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PanicImportSessionRepository;
|
||||
@@ -327,21 +330,30 @@ pub struct PanicSocialQueryPort;
|
||||
|
||||
#[async_trait]
|
||||
impl crate::ports::SocialQueryPort for PanicSocialQueryPort {
|
||||
async fn get_accepted_following_urls(&self, _: uuid::Uuid) -> Result<Vec<String>, DomainError> {
|
||||
async fn get_accepted_following_urls(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<Vec<String>, DomainError> {
|
||||
panic!("PanicSocialQueryPort called")
|
||||
}
|
||||
async fn list_all_followed_remote_actors(&self) -> Result<Vec<RemoteActorInfo>, DomainError> {
|
||||
panic!("PanicSocialQueryPort called")
|
||||
}
|
||||
async fn count_following(&self, _: uuid::Uuid) -> Result<usize, DomainError> {
|
||||
async fn count_following(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<usize, DomainError> {
|
||||
panic!("PanicSocialQueryPort called")
|
||||
}
|
||||
async fn count_accepted_followers(&self, _: uuid::Uuid) -> Result<usize, DomainError> {
|
||||
async fn count_accepted_followers(
|
||||
&self,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<usize, DomainError> {
|
||||
panic!("PanicSocialQueryPort called")
|
||||
}
|
||||
async fn get_pending_followers(
|
||||
&self,
|
||||
_: uuid::Uuid,
|
||||
_: &crate::value_objects::UserId,
|
||||
) -> Result<Vec<PendingFollowerInfo>, DomainError> {
|
||||
panic!("PanicSocialQueryPort called")
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@ use std::str::FromStr;
|
||||
|
||||
use crate::errors::DomainError;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum WatchMedium {
|
||||
Cinema,
|
||||
Streaming,
|
||||
#[serde(rename = "tv")]
|
||||
TV,
|
||||
PhysicalMedia,
|
||||
Download,
|
||||
|
||||
Reference in New Issue
Block a user