From f15b2fefcdf7ad86feb6003297e0baa57205dd4e Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Tue, 28 Jul 2026 03:17:16 +0300 Subject: [PATCH] Refactor movie matching logic to improve clarity and accuracy Updated the MovieMatcher class to streamline the status determination for movie matches. Introduced a new method, StatusOf, to encapsulate the logic for assessing whether a movie is already in the library or if a confident match exists. This refactor enhances code readability and maintains the integrity of the movie import process. Adjusted related logic to ensure consistent handling of movie matching statuses. --- .../AutoAttachMovieCommandHandler.cs | 2 +- .../Media/MovieImport/MovieMatcher.cs | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/src/TeleWave.Application/Media/MovieImport/AutoAttachMovieCommandHandler.cs b/backend/src/TeleWave.Application/Media/MovieImport/AutoAttachMovieCommandHandler.cs index 99cedca..7512466 100644 --- a/backend/src/TeleWave.Application/Media/MovieImport/AutoAttachMovieCommandHandler.cs +++ b/backend/src/TeleWave.Application/Media/MovieImport/AutoAttachMovieCommandHandler.cs @@ -20,7 +20,7 @@ public sealed class AutoAttachMovieCommandHandler( ) { // Источник берём первый настроенный: выбирать его в фоне некому, а настроен обычно один. - if (resolver.AvailableKeys.FirstOrDefault() is not { } provider) + if (resolver.AvailableKeys is not [var provider, ..]) return Result.Success(false); var asset = await dbContext.MediaAssets.FirstOrDefaultAsync( diff --git a/backend/src/TeleWave.Application/Media/MovieImport/MovieMatcher.cs b/backend/src/TeleWave.Application/Media/MovieImport/MovieMatcher.cs index 0651c57..17d7b89 100644 --- a/backend/src/TeleWave.Application/Media/MovieImport/MovieMatcher.cs +++ b/backend/src/TeleWave.Application/Media/MovieImport/MovieMatcher.cs @@ -176,16 +176,11 @@ public sealed class MovieMatcher(IAppDbContext dbContext, IMetadataProviderResol var picked = Pick(candidates, parsed); var existing = picked is null ? null : taken.GetValueOrDefault(picked.ExternalId); - var status = - existing is not null ? MovieMatchStatus.AlreadyInLibrary - : picked is not null ? MovieMatchStatus.Confident - : MovieMatchStatus.Uncertain; - return new MovieMatchDto( name, parsed.Title, parsed.Year, - status, + StatusOf(existing, picked), candidates, picked?.ExternalId, existing?.Id, @@ -196,6 +191,18 @@ public sealed class MovieMatcher(IAppDbContext dbContext, IMetadataProviderResol private static MovieMatchDto NothingFound(string name, string title, int? year) => new(name, title, year, MovieMatchStatus.NotFound, [], null, null, null); + /// + /// Что делать со строкой. Занятый кандидат важнее уверенности: второй файл того же фильма — + /// это не второй фильм, и заводить его нельзя, каким бы точным ни было совпадение. + /// + private static MovieMatchStatus StatusOf(TakenShow? existing, MetadataCandidate? picked) + { + if (existing is not null) + return MovieMatchStatus.AlreadyInLibrary; + + return picked is not null ? MovieMatchStatus.Confident : MovieMatchStatus.Uncertain; + } + /// /// Единственный кандидат, у которого название совпало дословно, а год сошёлся. Несколько таких — /// значит выбирать не из чего: два «Мстителя» одного года различит только человек.