From f31a48c429c7aaca5622ceb6ee6ecef4bcb251fd Mon Sep 17 00:00:00 2001 From: Jason Dove <1695733+jasongdove@users.noreply.github.com> Date: Wed, 13 Aug 2025 15:56:27 -0500 Subject: [PATCH] fix episodes from multiple plex servers (#2314) --- CHANGELOG.md | 1 + .../Repositories/IPlexTelevisionRepository.cs | 2 +- .../Data/Repositories/PlexMovieRepository.cs | 1 + .../Repositories/PlexTelevisionRepository.cs | 21 ++++++++++++------- .../Core/Plex/PlexNetworkScanner.cs | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6b46bd..e9e2cf88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Until these existing items age out, there will be warnings logged after each playout build/extension - Fix playback of anamorphic content from Jellyfin - This fix requires a manual deep scan of any affected Jellyfin library +- Fix bug where multiple Plex servers would mix their episodes ### Changed - Allow multiple watermarks in playback troubleshooting diff --git a/ErsatzTV.Core/Interfaces/Repositories/IPlexTelevisionRepository.cs b/ErsatzTV.Core/Interfaces/Repositories/IPlexTelevisionRepository.cs index bc121845..db96f704 100644 --- a/ErsatzTV.Core/Interfaces/Repositories/IPlexTelevisionRepository.cs +++ b/ErsatzTV.Core/Interfaces/Repositories/IPlexTelevisionRepository.cs @@ -7,7 +7,7 @@ public interface IPlexTelevisionRepository : IMediaServerTelevisionRepository { Task> RemoveAllTags(PlexLibrary library, PlexTag tag, System.Collections.Generic.HashSet keep); - Task AddTag(PlexShow show, PlexTag tag); + Task AddTag(PlexLibrary library, PlexShow show, PlexTag tag); Task UpdateLastNetworksScan(PlexLibrary library); } diff --git a/ErsatzTV.Infrastructure/Data/Repositories/PlexMovieRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/PlexMovieRepository.cs index 61cdfa55..677c5656 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/PlexMovieRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/PlexMovieRepository.cs @@ -152,6 +152,7 @@ public class PlexMovieRepository : IPlexMovieRepository await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); Option maybeExisting = await dbContext.PlexMovies .AsNoTracking() + .Where(pm => pm.LibraryPath.LibraryId == library.Id) .Include(i => i.MovieMetadata) .ThenInclude(mm => mm.Genres) .Include(i => i.MovieMetadata) diff --git a/ErsatzTV.Infrastructure/Data/Repositories/PlexTelevisionRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/PlexTelevisionRepository.cs index 7a4bfff1..445b5306 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/PlexTelevisionRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/PlexTelevisionRepository.cs @@ -210,6 +210,7 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); Option maybeExisting = await dbContext.PlexShows .AsNoTracking() + .Where(ps => ps.LibraryPath.LibraryId == library.Id) .Include(i => i.ShowMetadata) .ThenInclude(sm => sm.Genres) .Include(i => i.ShowMetadata) @@ -242,6 +243,7 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); Option maybeExisting = await dbContext.PlexSeasons .AsNoTracking() + .Where(ps => ps.LibraryPath.LibraryId == library.Id) .Include(i => i.SeasonMetadata) .ThenInclude(sm => sm.Artwork) .Include(i => i.SeasonMetadata) @@ -270,6 +272,7 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); Option maybeExisting = await dbContext.PlexEpisodes .AsNoTracking() + .Where(pe => pe.LibraryPath.LibraryId == library.Id) .Include(i => i.EpisodeMetadata) .ThenInclude(mm => mm.Artwork) .Include(i => i.EpisodeMetadata) @@ -448,7 +451,7 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository return result; } - public async Task AddTag(PlexShow show, PlexTag tag) + public async Task AddTag(PlexLibrary library, PlexShow show, PlexTag tag) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); @@ -456,8 +459,10 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository @"SELECT PS.Id FROM Tag INNER JOIN ShowMetadata SM on SM.Id = Tag.ShowMetadataId INNER JOIN PlexShow PS on PS.Id = SM.ShowId + INNER JOIN MediaItem MI on PS.Id = MI.Id + INNER JOIN LibraryPath LP on MI.LibraryPathId = LP.Id AND LP.LibraryId = @LibraryId WHERE PS.Key = @Key AND Tag.Name = @Tag AND Tag.ExternalTypeId = @TagType", - new { show.Key, tag.Tag, tag.TagType }); + new { show.Key, tag.Tag, tag.TagType, LibraryId = library.Id }); // already exists if (existingShowId > 0) @@ -565,12 +570,12 @@ public class PlexTelevisionRepository : IPlexTelevisionRepository item.LibraryPathId = library.Paths.Head().Id; foreach (EpisodeMetadata metadata in item.EpisodeMetadata) { - metadata.Genres ??= new List(); - metadata.Tags ??= new List(); - metadata.Studios ??= new List(); - metadata.Actors ??= new List(); - metadata.Directors ??= new List(); - metadata.Writers ??= new List(); + metadata.Genres ??= []; + metadata.Tags ??= []; + metadata.Studios ??= []; + metadata.Actors ??= []; + metadata.Directors ??= []; + metadata.Writers ??= []; } await dbContext.PlexEpisodes.AddAsync(item); diff --git a/ErsatzTV.Scanner/Core/Plex/PlexNetworkScanner.cs b/ErsatzTV.Scanner/Core/Plex/PlexNetworkScanner.cs index 846b0506..8c03843d 100644 --- a/ErsatzTV.Scanner/Core/Plex/PlexNetworkScanner.cs +++ b/ErsatzTV.Scanner/Core/Plex/PlexNetworkScanner.cs @@ -58,7 +58,7 @@ public class PlexNetworkScanner( var keepIds = new System.Collections.Generic.HashSet(); await foreach ((PlexShow item, int _) in items) { - PlexShowAddTagResult result = await plexTelevisionRepository.AddTag(item, tag); + PlexShowAddTagResult result = await plexTelevisionRepository.AddTag(library, item, tag); foreach (int existing in result.Existing) {