diff --git a/server/entity/Media.ts b/server/entity/Media.ts index 3709e363e1..abbcf4863c 100644 --- a/server/entity/Media.ts +++ b/server/entity/Media.ts @@ -212,6 +212,27 @@ class Media { @Column({ nullable: true, type: 'varchar' }) public ratingKey4k?: string | null; + @Column({ + type: 'text', + nullable: true, + transformer: { + from: (value: string | null): string[] | null => { + if (value == null) { + return null; + } + try { + const parsed = JSON.parse(value); + return Array.isArray(parsed) ? parsed : null; + } catch { + return null; + } + }, + to: (value: string[] | null): string | null => + value == null ? null : JSON.stringify(value), + }, + }) + public plexRatingKeys?: string[] | null; + @Column({ nullable: true, type: 'varchar' }) public jellyfinMediaId?: string | null; @@ -237,6 +258,36 @@ class Media { Object.assign(this, init); } + public addPlexRatingKey(ratingKey?: string | null): boolean { + if (!ratingKey) { + return false; + } + + const keys = this.getPlexRatingKeys(); + if (keys.includes(ratingKey)) { + if (this.plexRatingKeys != null) { + return false; + } + this.plexRatingKeys = keys; + return true; + } + + this.plexRatingKeys = [...keys, ratingKey]; + return true; + } + + public getPlexRatingKeys(): string[] { + if (this.plexRatingKeys) { + return this.plexRatingKeys; + } + + return [ + ...new Set( + [this.ratingKey, this.ratingKey4k].filter((key): key is string => !!key) + ), + ]; + } + public resetServiceData(is4k?: boolean): void { if (is4k === undefined || !is4k) { this.serviceId = null; @@ -252,6 +303,9 @@ class Media { this.ratingKey4k = null; this.jellyfinMediaId4k = null; } + if (is4k === undefined) { + this.plexRatingKeys = null; + } } @AfterLoad() diff --git a/server/lib/availabilitySync.test.ts b/server/lib/availabilitySync.test.ts index 7e861e38c5..46a23f7778 100644 --- a/server/lib/availabilitySync.test.ts +++ b/server/lib/availabilitySync.test.ts @@ -90,11 +90,27 @@ Object.defineProperty(JellyfinAPI.prototype, 'setUserId', { }); // --- Mock PlexAPI --- +function plexNotFoundError(): Error & { response: { status: number } } { + return Object.assign(new Error('Request failed with status code 404'), { + response: { status: 404 }, + isAxiosError: true, + toJSON: () => ({}), + name: 'AxiosError', + }); +} + +function rethrowPlexTestError(error: unknown): never { + if (error instanceof Error && error.message === '404') { + throw plexNotFoundError(); + } + throw error; +} + let getMetadataImpl: ( key: string, options?: { includeChildren?: boolean } ) => Promise = async () => { - throw new Error('404'); + throw plexNotFoundError(); }; let getChildrenMetadataImpl: ( key: string @@ -102,8 +118,13 @@ let getChildrenMetadataImpl: ( Object.defineProperty(PlexAPI.prototype, 'getMetadata', { get() { - return async (key: string, options?: { includeChildren?: boolean }) => - getMetadataImpl(key, options); + return async (key: string, options?: { includeChildren?: boolean }) => { + try { + return await getMetadataImpl(key, options); + } catch (error) { + rethrowPlexTestError(error); + } + }; }, set() {}, configurable: true, @@ -111,7 +132,13 @@ Object.defineProperty(PlexAPI.prototype, 'getMetadata', { Object.defineProperty(PlexAPI.prototype, 'getChildrenMetadata', { get() { - return async (key: string) => getChildrenMetadataImpl(key); + return async (key: string) => { + try { + return await getChildrenMetadataImpl(key); + } catch (error) { + rethrowPlexTestError(error); + } + }; }, set() {}, configurable: true, @@ -394,6 +421,38 @@ function fakePlexEpisodes(count: number): PlexMetadata[] { })); } +function fakePlex4kEpisodes(count: number): PlexMetadata[] { + return Array.from({ length: count }, (_, i) => ({ + ratingKey: `ep4k-${i}`, + guid: `plex://episode/ep4k-${i}`, + type: 'movie' as const, + title: `Episode ${i + 1}`, + Guid: [], + index: i + 1, + leafCount: 0, + viewedLeafCount: 0, + addedAt: 0, + updatedAt: 0, + Media: [ + { + id: i, + duration: 2400, + bitrate: 20000, + width: 3840, + height: 2160, + aspectRatio: 1.78, + audioChannels: 6, + audioCodec: 'eac3', + videoCodec: 'hevc', + videoResolution: '4k', + container: 'mkv', + videoFrameRate: '24p', + videoProfile: 'main 10', + }, + ], + })); +} + function fakePlexShow(ratingKey: string): PlexMetadata { return { ratingKey, @@ -953,6 +1012,95 @@ describe('AvailabilitySync', () => { 'Show should be PARTIALLY_AVAILABLE when some seasons are available and some are unknown' ); }); + + it('should not probe or clear retained Plex rating keys during a Jellyfin sync', async () => { + configureJellyfin(); + configureSonarr([{ syncEnabled: true }]); + + const mediaRepository = getRepository(Media); + const media = new Media(); + media.tmdbId = 1413; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.jellyfinMediaId = 'jellyfin-retain-plex-id'; + media.ratingKey = 'retained-plex-rk'; + media.plexRatingKeys = ['retained-plex-rk']; + media.externalServiceId = 413; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + let plexMetadataCalls = 0; + getMetadataImpl = async () => { + plexMetadataCalls += 1; + throw new Error('404'); + }; + + getItemDataImpl = async (id: string) => { + if (id === 'jellyfin-retain-plex-id') { + return fakeJellyfinShow('jellyfin-retain-plex-id', '1413'); + } + return undefined; + }; + + getSeasonsImpl = async (seriesID: string) => { + if (seriesID === 'jellyfin-retain-plex-id') { + return [fakeJellyfinSeason(1, 'jellyfin-retain-plex-s1-id')]; + } + return []; + }; + + getEpisodesImpl = async (_seriesID: string, seasonID: string) => { + if (seasonID === 'jellyfin-retain-plex-s1-id') { + return fakeJellyfinEpisodes(10); + } + return []; + }; + + getSeriesByIdImpl = async (id: number) => { + if (id === 413) { + return { + tvdbId: 1413, + id: 413, + title: 'Retained Plex Keys', + titleSlug: 'retained-plex-keys', + monitored: true, + statistics: { + episodeFileCount: 10, + totalEpisodeCount: 10, + episodeCount: 10, + percentOfEpisodes: 100, + sizeOnDisk: 0, + seasonCount: 1, + }, + seasons: fakeSonarrSeasons(1, { 1: 10 }), + } as unknown as SonarrSeries; + } + throw new Error('404'); + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 1413 }, + relations: ['seasons'], + }); + + assert.strictEqual( + plexMetadataCalls, + 0, + 'Jellyfin sync must not probe Plex for retained rating keys' + ); + assert.deepStrictEqual(updated.plexRatingKeys, ['retained-plex-rk']); + assert.strictEqual(updated.ratingKey, 'retained-plex-rk'); + assert.strictEqual(updated.status, MediaStatus.AVAILABLE); + assert.strictEqual(updated.seasons[0].status, MediaStatus.AVAILABLE); + }); }); describe('TV season availability - Plex', () => { @@ -1000,7 +1148,7 @@ describe('AvailabilitySync', () => { if (key === 'plex-house-s6-rk') { return fakePlexEpisodes(21); } - return []; + return undefined as unknown as PlexMetadata[]; }; // Sonarr: only season 6 has files @@ -1303,150 +1451,565 @@ describe('AvailabilitySync', () => { 'Show should be PARTIALLY_AVAILABLE after season removal' ); }); - }); - describe('movie availability - Radarr', () => { - it('should mark a deleted movie as DELETED when a second standard Radarr instance has a colliding externalServiceId', async () => { + it('should not mark seasons DELETED when a show is split across a 1080p and a 4K Plex library', async () => { configurePlex(); - configureRadarr([{ syncEnabled: true }, { syncEnabled: true }]); + // No Sonarr configured for this user: only Plex is the source of truth. + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; const mediaRepository = getRepository(Media); const media = new Media(); - media.tmdbId = 5000; - media.mediaType = MediaType.MOVIE; + media.tmdbId = 2010; + media.mediaType = MediaType.TV; media.status = MediaStatus.AVAILABLE; - media.ratingKey = 'gone-from-plex-rk'; - media.externalServiceId = 300; - media.serviceId = 0; + // Season 1 lives in the 1080p Plex library, S2-S4 live in the 4K + // Plex library. Simulate the 4K copy scanning last and taking + // ratingKey; both keys must still be checked. + media.ratingKey = 'plex-split-4k-rk'; + media.ratingKey4k = 'plex-split-4k-rk'; + media.plexRatingKeys = ['plex-split-1080p-rk', 'plex-split-4k-rk']; + media.seasons = []; + + for (let i = 1; i <= 4; i++) { + media.seasons.push( + new Season({ + seasonNumber: i, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }) + ); + } await mediaRepository.save(media); - // Probed once per standard instance with the same id (300): origin 404s - // (deleted); the other instance has a different movie at 300. - let radarrCall = 0; - getMovieImpl = async (id: number) => { - if (id !== 300) { - throw new Error('404'); + getMetadataImpl = async (key: string) => { + if (key === 'plex-split-1080p-rk') { + return fakePlexShow('plex-split-1080p-rk'); } - radarrCall += 1; - if (radarrCall === 1) { - throw new Error('404'); + if (key === 'plex-split-4k-rk') { + return fakePlexShow('plex-split-4k-rk'); } - return { - id: 300, - tmdbId: 999999, - title: 'Unrelated Colliding Movie', - hasFile: true, - } as unknown as RadarrMovie; + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'plex-split-1080p-rk') { + return [fakePlexSeason(1, 'plex-split-1080p-s1-rk')]; + } + if (key === 'plex-split-4k-rk') { + return [ + fakePlexSeason(2, 'plex-split-4k-s2-rk'), + fakePlexSeason(3, 'plex-split-4k-s3-rk'), + fakePlexSeason(4, 'plex-split-4k-s4-rk'), + ]; + } + if (key === 'plex-split-1080p-s1-rk') { + return fakePlexEpisodes(10); + } + if ( + key === 'plex-split-4k-s2-rk' || + key === 'plex-split-4k-s3-rk' || + key === 'plex-split-4k-s4-rk' + ) { + return fakePlex4kEpisodes(10); + } + return []; }; await availabilitySync.run(); const updated = await mediaRepository.findOneOrFail({ - where: { tmdbId: 5000 }, + where: { tmdbId: 2010 }, + relations: ['seasons'], }); + for (const season of updated.seasons) { + assert.notStrictEqual( + season.status, + MediaStatus.DELETED, + `Season ${season.seasonNumber} should not be DELETED` + ); + assert.strictEqual( + season.status, + MediaStatus.AVAILABLE, + `Season ${season.seasonNumber} should remain AVAILABLE` + ); + } + assert.strictEqual( updated.status, - MediaStatus.DELETED, - 'Movie deleted from its origin instance and Plex must not be kept alive by a colliding externalServiceId on another standard instance' + MediaStatus.AVAILABLE, + 'Show should remain AVAILABLE' ); }); - }); - - describe('movie availability - Plex 4K detection', () => { - function fakePlexMovie(ratingKey: string, widths: number[]): PlexMetadata { - return { - ratingKey, - guid: `plex://movie/${ratingKey}`, - type: 'movie', - title: 'Test Movie', - Guid: [], - index: 1, - leafCount: 0, - viewedLeafCount: 0, - addedAt: 0, - updatedAt: 0, - Media: widths.map((width, i) => ({ - id: i, - duration: 7200, - bitrate: 20000, - width, - height: width >= 2000 ? 2160 : 1080, - aspectRatio: 1.78, - audioChannels: 6, - audioCodec: 'eac3', - videoCodec: width >= 2000 ? 'hevc' : 'h264', - videoResolution: width >= 2000 ? '4k' : '1080', - container: 'mkv', - videoFrameRate: '24p', - videoProfile: 'main 10', - })), - }; - } - it('should keep the 4K status when both statuses share a Plex item that has a 4K version (merged versions)', async () => { + it('should still mark seasons DELETED when neither rating key contains them (legacy single-ratingKey)', async () => { configurePlex(); - configureRadarr([ - { syncEnabled: true }, - { is4k: true, syncEnabled: true }, - ]); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; const mediaRepository = getRepository(Media); - // The Plex scanner writes the same rating key to both versions of a - // merged item, and a prior deletion left no externalServiceId4k to - // query, so retention rides on the Plex check alone. const media = new Media(); - media.tmdbId = 687163; - media.mediaType = MediaType.MOVIE; + media.tmdbId = 2011; + media.mediaType = MediaType.TV; media.status = MediaStatus.AVAILABLE; - media.status4k = MediaStatus.AVAILABLE; - media.ratingKey = 'merged-versions-rk'; - media.ratingKey4k = 'merged-versions-rk'; + // Only the primary rating key is set (no ratingKey4k), exercising + // the secondary-lookup branch when the fallback has nothing to find. + media.ratingKey = 'plex-legacy-rk'; + media.seasons = []; + + for (let i = 1; i <= 4; i++) { + media.seasons.push( + new Season({ + seasonNumber: i, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }) + ); + } + await mediaRepository.save(media); getMetadataImpl = async (key: string) => { - if (key === 'merged-versions-rk') { - return fakePlexMovie('merged-versions-rk', [1920, 3840]); + if (key === 'plex-legacy-rk') { + return fakePlexShow('plex-legacy-rk'); } throw new Error('404'); }; + getChildrenMetadataImpl = async (key: string) => { + if (key === 'plex-legacy-rk') { + return [fakePlexSeason(2, 'plex-legacy-s2-rk')]; + } + if (key === 'plex-legacy-s2-rk') { + return fakePlexEpisodes(10); + } + return []; + }; + await availabilitySync.run(); const updated = await mediaRepository.findOneOrFail({ - where: { tmdbId: 687163 }, + where: { tmdbId: 2011 }, + relations: ['seasons'], }); + const s2 = updated.seasons.find((s) => s.seasonNumber === 2); assert.strictEqual( - updated.status4k, + s2?.status, MediaStatus.AVAILABLE, - 'A 4K version present on the media server must prevent 4K removal even when both statuses share one rating key' + 'Season 2 should remain AVAILABLE' ); + + for (const season of updated.seasons) { + if (season.seasonNumber !== 2) { + assert.strictEqual( + season.status, + MediaStatus.DELETED, + `Season ${season.seasonNumber} should be DELETED` + ); + } + } + assert.strictEqual( updated.status, - MediaStatus.AVAILABLE, - 'The non-4K status must be unaffected' + MediaStatus.PARTIALLY_AVAILABLE, + 'Show should be PARTIALLY_AVAILABLE after season removal' ); }); - it('should still remove a stale 4K status when the shared Plex item has no 4K version', async () => { + it('should drop a stored Plex rating key when Plex returns 404 for it', async () => { configurePlex(); - configureRadarr([ - { syncEnabled: true }, - { is4k: true, syncEnabled: true }, - ]); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; const mediaRepository = getRepository(Media); - const media = new Media(); - media.tmdbId = 687164; - media.mediaType = MediaType.MOVIE; + media.tmdbId = 2012; + media.mediaType = MediaType.TV; media.status = MediaStatus.AVAILABLE; - media.status4k = MediaStatus.AVAILABLE; - media.ratingKey = 'stale-4k-rk'; + media.ratingKey = 'plex-dead-rk'; + media.ratingKey4k = 'plex-dead-rk'; + media.plexRatingKeys = ['plex-dead-rk', 'plex-live-rk']; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'plex-live-rk') { + return fakePlexShow('plex-live-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'plex-live-rk') { + return [fakePlexSeason(1, 'plex-live-s1-rk')]; + } + if (key === 'plex-live-s1-rk') { + return fakePlexEpisodes(10); + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 2012 }, + relations: ['seasons'], + }); + + assert.deepStrictEqual(updated.plexRatingKeys?.sort(), ['plex-live-rk']); + assert.strictEqual(updated.seasons[0].status, MediaStatus.AVAILABLE); + assert.strictEqual(updated.ratingKey, 'plex-dead-rk'); + assert.strictEqual(updated.ratingKey4k, 'plex-dead-rk'); + }); + + it('should clear deep-link keys and plexRatingKeys when the last stored key 404s', async () => { + configurePlex(); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; + + const mediaRepository = getRepository(Media); + const media = new Media(); + media.tmdbId = 2014; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.ratingKey = 'plex-only-dead-rk'; + media.plexRatingKeys = ['plex-only-dead-rk']; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async () => { + throw new Error('404'); + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 2014 }, + relations: ['seasons'], + }); + + assert.deepStrictEqual(updated.plexRatingKeys, []); + assert.strictEqual(updated.ratingKey, null); + assert.strictEqual(updated.seasons[0].status, MediaStatus.DELETED); + assert.strictEqual(updated.status, MediaStatus.DELETED); + }); + + it('should keep seasons when one rating key errors even if another key lacks them', async () => { + configurePlex(); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; + + const mediaRepository = getRepository(Media); + const media = new Media(); + media.tmdbId = 2015; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.ratingKey = 'plex-live-partial-rk'; + media.plexRatingKeys = ['plex-error-rk', 'plex-live-partial-rk']; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + new Season({ + seasonNumber: 2, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'plex-error-rk') { + throw new Error('ECONNRESET'); + } + if (key === 'plex-live-partial-rk') { + return fakePlexShow('plex-live-partial-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'plex-live-partial-rk') { + return [fakePlexSeason(1, 'plex-live-partial-s1-rk')]; + } + if (key === 'plex-live-partial-s1-rk') { + return fakePlexEpisodes(10); + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 2015 }, + relations: ['seasons'], + }); + const s1 = updated.seasons.find((s) => s.seasonNumber === 1); + const s2 = updated.seasons.find((s) => s.seasonNumber === 2); + + assert.strictEqual(s1?.status, MediaStatus.AVAILABLE); + assert.strictEqual(s2?.status, MediaStatus.AVAILABLE); + assert.strictEqual(updated.status, MediaStatus.AVAILABLE); + assert.deepStrictEqual( + updated.plexRatingKeys?.slice().sort(), + ['plex-error-rk', 'plex-live-partial-rk'].sort() + ); + }); + + it('should fail open when Plex returns a non-404 error for a stored rating key', async () => { + configurePlex(); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; + + const mediaRepository = getRepository(Media); + const media = new Media(); + media.tmdbId = 2013; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.ratingKey = 'plex-error-rk'; + media.plexRatingKeys = ['plex-error-rk']; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async () => { + throw new Error('ECONNRESET'); + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 2013 }, + relations: ['seasons'], + }); + + assert.strictEqual(updated.seasons[0].status, MediaStatus.AVAILABLE); + assert.strictEqual(updated.status, MediaStatus.AVAILABLE); + assert.deepStrictEqual(updated.plexRatingKeys, ['plex-error-rk']); + }); + + it('should treat an empty successful Plex metadata response as unverified, not missing', async () => { + configurePlex(); + const settings = getSettings(); + settings.sonarr = []; + settings.radarr = []; + + const mediaRepository = getRepository(Media); + const media = new Media(); + media.tmdbId = 2016; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.ratingKey = 'plex-empty-meta-rk'; + media.plexRatingKeys = ['plex-empty-meta-rk']; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.UNKNOWN, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async () => undefined as unknown as PlexMetadata; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 2016 }, + relations: ['seasons'], + }); + + assert.strictEqual( + updated.status, + MediaStatus.AVAILABLE, + 'Empty successful Metadata must not mark the show deleted' + ); + assert.strictEqual( + updated.seasons[0].status, + MediaStatus.AVAILABLE, + 'Empty successful Metadata must not mark seasons deleted' + ); + assert.deepStrictEqual( + updated.plexRatingKeys, + ['plex-empty-meta-rk'], + 'Empty successful Metadata must not drop the rating key' + ); + assert.strictEqual(updated.ratingKey, 'plex-empty-meta-rk'); + }); + }); + + describe('movie availability - Radarr', () => { + it('should mark a deleted movie as DELETED when a second standard Radarr instance has a colliding externalServiceId', async () => { + configurePlex(); + configureRadarr([{ syncEnabled: true }, { syncEnabled: true }]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 5000; + media.mediaType = MediaType.MOVIE; + media.status = MediaStatus.AVAILABLE; + media.ratingKey = 'gone-from-plex-rk'; + media.externalServiceId = 300; + media.serviceId = 0; + + await mediaRepository.save(media); + + // Probed once per standard instance with the same id (300): origin 404s + // (deleted); the other instance has a different movie at 300. + let radarrCall = 0; + getMovieImpl = async (id: number) => { + if (id !== 300) { + throw new Error('404'); + } + radarrCall += 1; + if (radarrCall === 1) { + throw new Error('404'); + } + return { + id: 300, + tmdbId: 999999, + title: 'Unrelated Colliding Movie', + hasFile: true, + } as unknown as RadarrMovie; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 5000 }, + }); + + assert.strictEqual( + updated.status, + MediaStatus.DELETED, + 'Movie deleted from its origin instance and Plex must not be kept alive by a colliding externalServiceId on another standard instance' + ); + }); + }); + + describe('movie availability - Plex 4K detection', () => { + function fakePlexMovie(ratingKey: string, widths: number[]): PlexMetadata { + return { + ratingKey, + guid: `plex://movie/${ratingKey}`, + type: 'movie', + title: 'Test Movie', + Guid: [], + index: 1, + leafCount: 0, + viewedLeafCount: 0, + addedAt: 0, + updatedAt: 0, + Media: widths.map((width, i) => ({ + id: i, + duration: 7200, + bitrate: 20000, + width, + height: width >= 2000 ? 2160 : 1080, + aspectRatio: 1.78, + audioChannels: 6, + audioCodec: 'eac3', + videoCodec: width >= 2000 ? 'hevc' : 'h264', + videoResolution: width >= 2000 ? '4k' : '1080', + container: 'mkv', + videoFrameRate: '24p', + videoProfile: 'main 10', + })), + }; + } + + it('should keep the 4K status when both statuses share a Plex item that has a 4K version (merged versions)', async () => { + configurePlex(); + configureRadarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + // The Plex scanner writes the same rating key to both versions of a + // merged item, and a prior deletion left no externalServiceId4k to + // query, so retention rides on the Plex check alone. + const media = new Media(); + media.tmdbId = 687163; + media.mediaType = MediaType.MOVIE; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'merged-versions-rk'; + media.ratingKey4k = 'merged-versions-rk'; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'merged-versions-rk') { + return fakePlexMovie('merged-versions-rk', [1920, 3840]); + } + throw new Error('404'); + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687163 }, + }); + + assert.strictEqual( + updated.status4k, + MediaStatus.AVAILABLE, + 'A 4K version present on the media server must prevent 4K removal even when both statuses share one rating key' + ); + assert.strictEqual( + updated.status, + MediaStatus.AVAILABLE, + 'The non-4K status must be unaffected' + ); + }); + + it('should still remove a stale 4K status when the shared Plex item has no 4K version', async () => { + configurePlex(); + configureRadarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687164; + media.mediaType = MediaType.MOVIE; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'stale-4k-rk'; media.ratingKey4k = 'stale-4k-rk'; await mediaRepository.save(media); @@ -1546,6 +2109,40 @@ describe('AvailabilitySync', () => { ); }); + it('should keep 4K movie availability in Plex after the last 4K Radarr server is removed', async () => { + configurePlex(); + configureRadarr([{ syncEnabled: true }]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687180; + media.mediaType = MediaType.MOVIE; + media.status = MediaStatus.UNKNOWN; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey4k = 'orphan-4k-rk'; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'orphan-4k-rk') { + return fakePlexMovie('orphan-4k-rk', [3840]); + } + throw new Error('404'); + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687180 }, + }); + + assert.strictEqual( + updated.status4k, + MediaStatus.AVAILABLE, + 'Removing the last 4K Radarr server must not delete a 4K movie that still exists in Plex' + ); + }); + it('should remove the non-4K status when the shared Plex item only has a 4K version left', async () => { configurePlex(); configureRadarr([ @@ -2032,6 +2629,270 @@ describe('AvailabilitySync', () => { assert.strictEqual(updated.status, MediaStatus.AVAILABLE); assert.strictEqual(s1?.status, MediaStatus.AVAILABLE); }); + + it('should keep non-4K show availability when one season is 4K-only and another episode fetch fails', async () => { + configurePlex(); + configureSonarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687181; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'partial-error-show-rk'; + media.ratingKey4k = 'partial-error-show-rk'; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + new Season({ + seasonNumber: 2, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'partial-error-show-rk') { + return fakePlexShow('partial-error-show-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'partial-error-show-rk') { + return [fakePlexSeason(1, 'pe-s1-rk'), fakePlexSeason(2, 'pe-s2-rk')]; + } + if (key === 'pe-s1-rk') { + return [fakeVersionedEpisode('pe-s1-e1', [3840])]; + } + if (key === 'pe-s2-rk') { + throw new Error('ECONNRESET'); + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687181 }, + relations: ['seasons'], + }); + + assert.notStrictEqual( + updated.status, + MediaStatus.DELETED, + 'A failed episode fetch must fail open at show level even if another season only verified 4K' + ); + assert.strictEqual( + updated.status, + MediaStatus.PARTIALLY_AVAILABLE, + 'The verified 4K-only season can still drop non-4K status while the failed season is preserved' + ); + assert.strictEqual(updated.status4k, MediaStatus.AVAILABLE); + const s1 = updated.seasons.find((s) => s.seasonNumber === 1); + const s2 = updated.seasons.find((s) => s.seasonNumber === 2); + assert.strictEqual(s1?.status, MediaStatus.DELETED); + assert.strictEqual(s1?.status4k, MediaStatus.AVAILABLE); + assert.strictEqual(s2?.status, MediaStatus.AVAILABLE); + assert.strictEqual(s2?.status4k, MediaStatus.AVAILABLE); + }); + + it('should not treat unverified empty Media caches as proof a tier is missing', async () => { + configurePlex(); + configureSonarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687182; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'empty-media-show-rk'; + media.ratingKey4k = 'empty-media-show-rk'; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'empty-media-show-rk') { + return fakePlexShow('empty-media-show-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'empty-media-show-rk') { + return [fakePlexSeason(1, 'em-s1-rk')]; + } + if (key === 'em-s1-rk') { + return [fakeVersionedEpisode('em-s1-e1', [])]; + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687182 }, + relations: ['seasons'], + }); + const s1 = updated.seasons.find((s) => s.seasonNumber === 1); + + assert.strictEqual( + updated.status, + MediaStatus.AVAILABLE, + 'Empty Media arrays must not let a later tier pass treat a cached miss as verified' + ); + assert.strictEqual(updated.status4k, MediaStatus.AVAILABLE); + assert.strictEqual(s1?.status, MediaStatus.AVAILABLE); + assert.strictEqual(s1?.status4k, MediaStatus.AVAILABLE); + }); + + it('should keep non-4K show availability when a verified 4K-only season sits beside an unverified season', async () => { + configurePlex(); + configureSonarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687183; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'mixed-verified-show-rk'; + media.ratingKey4k = 'mixed-verified-show-rk'; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + new Season({ + seasonNumber: 2, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'mixed-verified-show-rk') { + return fakePlexShow('mixed-verified-show-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'mixed-verified-show-rk') { + return [fakePlexSeason(1, 'mv-s1-rk'), fakePlexSeason(2, 'mv-s2-rk')]; + } + if (key === 'mv-s1-rk') { + return [fakeVersionedEpisode('mv-s1-e1', [3840])]; + } + if (key === 'mv-s2-rk') { + return [fakeVersionedEpisode('mv-s2-e1', [])]; + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687183 }, + relations: ['seasons'], + }); + + assert.notStrictEqual( + updated.status, + MediaStatus.DELETED, + 'A verified 4K-only season must not prove the non-4K tier is gone while another season is still unverified' + ); + assert.strictEqual(updated.status4k, MediaStatus.AVAILABLE); + }); + + it('should not treat a same-season mix of verified and empty Media episodes as proof a tier is missing', async () => { + configurePlex(); + configureSonarr([ + { syncEnabled: true }, + { is4k: true, syncEnabled: true }, + ]); + + const mediaRepository = getRepository(Media); + + const media = new Media(); + media.tmdbId = 687184; + media.mediaType = MediaType.TV; + media.status = MediaStatus.AVAILABLE; + media.status4k = MediaStatus.AVAILABLE; + media.ratingKey = 'same-season-mixed-rk'; + media.ratingKey4k = 'same-season-mixed-rk'; + media.seasons = [ + new Season({ + seasonNumber: 1, + status: MediaStatus.AVAILABLE, + status4k: MediaStatus.AVAILABLE, + }), + ]; + await mediaRepository.save(media); + + getMetadataImpl = async (key: string) => { + if (key === 'same-season-mixed-rk') { + return fakePlexShow('same-season-mixed-rk'); + } + throw new Error('404'); + }; + + getChildrenMetadataImpl = async (key: string) => { + if (key === 'same-season-mixed-rk') { + return [fakePlexSeason(1, 'ssm-s1-rk')]; + } + if (key === 'ssm-s1-rk') { + return [ + fakeVersionedEpisode('ssm-s1-e1', [3840]), + fakeVersionedEpisode('ssm-s1-e2', []), + ]; + } + return []; + }; + + await availabilitySync.run(); + + const updated = await mediaRepository.findOneOrFail({ + where: { tmdbId: 687184 }, + relations: ['seasons'], + }); + const s1 = updated.seasons.find((s) => s.seasonNumber === 1); + + assert.strictEqual( + updated.status, + MediaStatus.AVAILABLE, + 'A width-3840 episode must not prove the non-4K tier is gone while another episode in the same season has empty Media' + ); + assert.strictEqual(updated.status4k, MediaStatus.AVAILABLE); + assert.strictEqual(s1?.status, MediaStatus.AVAILABLE); + assert.strictEqual(s1?.status4k, MediaStatus.AVAILABLE); + }); }); describe('specials season handling', () => { diff --git a/server/lib/availabilitySync.ts b/server/lib/availabilitySync.ts index 4a700ef355..2af2d70d21 100644 --- a/server/lib/availabilitySync.ts +++ b/server/lib/availabilitySync.ts @@ -26,7 +26,15 @@ class AvailabilitySync { public running = false; private plexClient: PlexAPI; private plexSeasonsCache: Record; - private plexEpisodeExistsCache: Record; + private plexEpisodeExistsCache: Record< + string, + { exists: boolean; verified: boolean } + >; + private plexMetadataCache: Record; + private plexRatingKeyProbeCache: Record< + string, + 'exists' | 'missing' | 'error' + >; private jellyfinClient: JellyfinAPI; private jellyfinSeasonsCache: Record; @@ -46,6 +54,8 @@ class AvailabilitySync { this.running = true; this.plexSeasonsCache = {}; this.plexEpisodeExistsCache = {}; + this.plexMetadataCache = {}; + this.plexRatingKeyProbeCache = {}; this.jellyfinSeasonsCache = {}; this.jellyfinEpisodeExistsCache = {}; this.sonarrSeasonsCache = {}; @@ -154,9 +164,18 @@ class AvailabilitySync { // plex if (mediaServerType === MediaServerType.PLEX) { - const { existsInPlex } = await this.mediaExistsInPlex(media, false); - const { existsInPlex: existsInPlex4k } = - await this.mediaExistsInPlex(media, true); + let existsInPlex = false; + let existsInPlex4k = false; + + if (this.mediaHasAvailableTier(media, false)) { + ({ existsInPlex } = await this.mediaExistsInPlex(media, false)); + } + if (this.mediaHasAvailableTier(media, true)) { + ({ existsInPlex: existsInPlex4k } = await this.mediaExistsInPlex( + media, + true + )); + } if (existsInPlex || existsInRadarr) { movieExists = true; @@ -228,13 +247,23 @@ class AvailabilitySync { let showExists4k = false; //plex + let existsInPlex = false; + let existsInPlex4k = false; + let plexSeasonsMap: Map = new Map(); + let plexSeasonsMap4k: Map = new Map(); - const { existsInPlex, seasonsMap: plexSeasonsMap = new Map() } = - await this.mediaExistsInPlex(media, false); - const { - existsInPlex: existsInPlex4k, - seasonsMap: plexSeasonsMap4k = new Map(), - } = await this.mediaExistsInPlex(media, true); + if (mediaServerType === MediaServerType.PLEX) { + if (this.mediaHasAvailableTier(media, false)) { + ({ existsInPlex, seasonsMap: plexSeasonsMap = new Map() } = + await this.mediaExistsInPlex(media, false)); + } + if (this.mediaHasAvailableTier(media, true)) { + ({ + existsInPlex: existsInPlex4k, + seasonsMap: plexSeasonsMap4k = new Map(), + } = await this.mediaExistsInPlex(media, true)); + } + } //jellyfin const { @@ -868,135 +897,230 @@ class AvailabilitySync { } // Plex - private async mediaExistsInPlex( + private isPlexNotFound(ex: unknown): boolean { + return (ex as { response?: { status?: number } })?.response?.status === 404; + } + + private async dropStoredPlexRatingKey( media: Media, - is4k: boolean - ): Promise<{ existsInPlex: boolean; seasonsMap?: Map }> { - const ratingKey = media.ratingKey; - const ratingKey4k = media.ratingKey4k; - let existsInPlex = false; - let preventSeasonSearch = false; + ratingKey: string + ): Promise { + const keys = media.getPlexRatingKeys(); + if (!keys.includes(ratingKey)) { + return; + } + + media.plexRatingKeys = keys.filter((stored) => stored !== ratingKey); + await getRepository(Media).save(media); + } + + private mediaHasAvailableTier(media: Media, is4k: boolean): boolean { + if (media.mediaType === 'movie') { + return media[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE; + } + + return ( + media[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || + media[is4k ? 'status4k' : 'status'] === MediaStatus.PARTIALLY_AVAILABLE || + media.seasons.some( + (season) => + season[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || + season[is4k ? 'status4k' : 'status'] === + MediaStatus.PARTIALLY_AVAILABLE + ) + ); + } + + private async probePlexRatingKey( + media: Media, + ratingKey: string + ): Promise<'exists' | 'missing' | 'error'> { + if (ratingKey in this.plexRatingKeyProbeCache) { + return this.plexRatingKeyProbeCache[ratingKey]; + } - // Check each plex instance to see if the media still exists - // If found, we will assume the media exists and prevent removal - // We can use the cache we built when we fetched the series with mediaExistsInPlex try { - let plexMedia: PlexMetadata | undefined; + const plexMedia = await this.plexClient?.getMetadata(ratingKey); + // Empty successful Metadata responses are unverified — only HTTP 404 + // confirms the rating key is missing. + if (!plexMedia) { + this.plexRatingKeyProbeCache[ratingKey] = 'error'; + return 'error'; + } - if (ratingKey && !is4k) { - plexMedia = await this.plexClient?.getMetadata(ratingKey); + this.plexMetadataCache[ratingKey] = plexMedia; - if (media.mediaType === 'tv') { - this.plexSeasonsCache[ratingKey] = - await this.plexClient?.getChildrenMetadata(ratingKey); + if (media.mediaType === 'tv') { + this.plexSeasonsCache[ratingKey] = + (await this.plexClient?.getChildrenMetadata(ratingKey)) ?? []; + } + + this.plexRatingKeyProbeCache[ratingKey] = 'exists'; + return 'exists'; + } catch (ex) { + if (this.isPlexNotFound(ex)) { + this.plexRatingKeyProbeCache[ratingKey] = 'missing'; + this.plexSeasonsCache[ratingKey] = []; + return 'missing'; + } + + logger.debug( + `Failure retrieving Plex metadata for rating key ${ratingKey} [TMDB ID ${media.tmdbId}].`, + { + errorMessage: ex instanceof Error ? ex.message : String(ex), + label: 'AvailabilitySync', } + ); + this.plexRatingKeyProbeCache[ratingKey] = 'error'; + return 'error'; + } + } - if ( - plexMedia && - media.mediaType === 'movie' && - this.enable4kMovie && - plexMedia.Media?.length && - !plexMedia.Media.some((mediaItem) => (mediaItem.width ?? 0) < 2000) - ) { - plexMedia = undefined; + private async tvKeyHasTieredEpisodes( + ratingKey: string, + is4k: boolean + ): Promise<{ + hasTier: boolean; + verified: boolean; + }> { + const cachedSeasons = this.plexSeasonsCache[ratingKey]; + if (!cachedSeasons?.length) { + return { hasTier: false, verified: false }; + } + + let hasTier = false; + let verified = true; + + for (const season of cachedSeasons) { + const cacheKey = `${is4k ? '4k' : 'std'}-${season.ratingKey}`; + const otherCacheKey = `${is4k ? 'std' : '4k'}-${season.ratingKey}`; + + if (cacheKey in this.plexEpisodeExistsCache) { + const cached = this.plexEpisodeExistsCache[cacheKey]; + if (!cached.verified) { + verified = false; } + if (cached.exists) { + hasTier = true; + break; + } + continue; } - if (ratingKey4k && is4k) { - plexMedia = await this.plexClient?.getMetadata(ratingKey4k); - - if (media.mediaType === 'tv') { - this.plexSeasonsCache[ratingKey4k] = - await this.plexClient?.getChildrenMetadata(ratingKey4k); + try { + const episodes = + (await this.plexClient?.getChildrenMetadata(season.ratingKey)) ?? []; + const episodeVerified = + episodes.length === 0 || + episodes.every((episode) => episode.Media?.length); + if (!episodeVerified) { + verified = false; + } + const episodeVersions = episodes.flatMap( + (episode) => episode.Media ?? [] + ); + const seasonHasTier = this.seasonHasTieredEpisodes( + episodeVersions, + is4k + ); + this.plexEpisodeExistsCache[cacheKey] = { + exists: seasonHasTier, + verified: episodeVerified, + }; + if (!(otherCacheKey in this.plexEpisodeExistsCache)) { + this.plexEpisodeExistsCache[otherCacheKey] = { + exists: this.seasonHasTieredEpisodes(episodeVersions, !is4k), + verified: episodeVerified, + }; + } + if (seasonHasTier) { + hasTier = true; + break; } + } catch { + verified = false; + } + } - if (plexMedia) { - if ( - plexMedia && - media.mediaType === 'movie' && - plexMedia.Media?.length && - !plexMedia.Media.some((mediaItem) => (mediaItem.width ?? 0) >= 2000) - ) { - plexMedia = undefined; - } + return { hasTier, verified }; + } - if (plexMedia && media.mediaType === 'tv') { - const cachedSeasons = this.plexSeasonsCache[ratingKey4k]; - if (cachedSeasons?.length) { - let has4kInAnySeason = false; - let verifiedAnySeason = false; - for (const season of cachedSeasons) { - try { - const episodes = await this.plexClient?.getChildrenMetadata( - season.ratingKey - ); - if (episodes?.some((episode) => episode.Media?.length)) { - verifiedAnySeason = true; - } - const has4kEpisode = episodes?.some((episode) => - episode.Media?.some( - (mediaItem) => (mediaItem.width ?? 0) >= 2000 - ) - ); - if (has4kEpisode) { - has4kInAnySeason = true; - break; - } - } catch { - // If we can't fetch episodes for a season, continue checking other seasons - } - } - if (verifiedAnySeason && !has4kInAnySeason) { - plexMedia = undefined; - } + private async mediaExistsInPlex( + media: Media, + is4k: boolean + ): Promise<{ existsInPlex: boolean; seasonsMap?: Map }> { + const ratingKeys = media.getPlexRatingKeys(); + let existsInPlex = false; + + for (const ratingKey of ratingKeys) { + const probe = await this.probePlexRatingKey(media, ratingKey); + + if (probe === 'missing') { + await this.dropStoredPlexRatingKey(media, ratingKey); + continue; + } + + if (probe === 'error') { + existsInPlex = true; + continue; + } + + const plexMedia = this.plexMetadataCache[ratingKey]; + + if (media.mediaType === 'movie') { + if (!existsInPlex) { + if (is4k) { + if ( + !plexMedia?.Media?.length || + plexMedia.Media.some( + (mediaItem) => (mediaItem.width ?? 0) >= 2000 + ) + ) { + existsInPlex = true; } + } else if ( + !this.enable4kMovie || + !plexMedia?.Media?.length || + plexMedia.Media.some((mediaItem) => (mediaItem.width ?? 0) < 2000) + ) { + existsInPlex = true; } } + continue; } - if (plexMedia) { + if (!is4k && !this.enable4kShow) { existsInPlex = true; + continue; } - } catch (ex) { - if (!ex.message.includes('404')) { + + if (existsInPlex) { + continue; + } + + const { hasTier, verified } = await this.tvKeyHasTieredEpisodes( + ratingKey, + is4k + ); + if (hasTier || !verified) { existsInPlex = true; - preventSeasonSearch = true; - logger.debug( - `Failure retrieving the ${is4k ? '4K' : 'non-4K'} ${ - media.mediaType === 'tv' ? 'show' : 'movie' - } [TMDB ID ${media.tmdbId}] from Plex.`, - { - errorMessage: ex.message, - label: 'AvailabilitySync', - } - ); } } - // Here we check each season in plex for availability - // If the API returns an error other than a 404, - // we will have to prevent the season check from happening if (media.mediaType === 'tv') { const seasonsMap: Map = new Map(); + const filteredSeasons = media.seasons.filter( + (season) => + season[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || + season[is4k ? 'status4k' : 'status'] === + MediaStatus.PARTIALLY_AVAILABLE + ); - if (!preventSeasonSearch) { - const filteredSeasons = media.seasons.filter( - (season) => - season[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || - season[is4k ? 'status4k' : 'status'] === - MediaStatus.PARTIALLY_AVAILABLE - ); - - for (const season of filteredSeasons) { - const seasonExists = await this.seasonExistsInPlex( - media, - season, - is4k - ); + for (const season of filteredSeasons) { + const seasonExists = await this.seasonExistsInPlex(media, season, is4k); - if (seasonExists) { - seasonsMap.set(season.seasonNumber, true); - } + if (seasonExists) { + seasonsMap.set(season.seasonNumber, true); } } @@ -1006,67 +1130,98 @@ class AvailabilitySync { return { existsInPlex }; } + private seasonHasTieredEpisodes( + episodeVersions: { width?: number }[], + is4k: boolean + ): boolean { + if (is4k) { + return episodeVersions.some( + (mediaItem) => (mediaItem.width ?? 0) >= 2000 + ); + } + if (this.enable4kShow) { + return episodeVersions.some((mediaItem) => (mediaItem.width ?? 0) < 2000); + } + return episodeVersions.length > 0; + } + private async seasonExistsInPlex( media: Media, season: Season, is4k: boolean ): Promise { - const ratingKey = media.ratingKey; - const ratingKey4k = media.ratingKey4k; - let seasonExistsInPlex = false; - - let plexSeasons: PlexMetadata[] | undefined; - - if (ratingKey && !is4k) { - plexSeasons = this.plexSeasonsCache[ratingKey]; - } + const ratingKeys = media.getPlexRatingKeys(); + let sawError = false; + + for (const ratingKey of ratingKeys) { + if (!(ratingKey in this.plexSeasonsCache)) { + const probe = await this.probePlexRatingKey(media, ratingKey); + if (probe === 'missing') { + await this.dropStoredPlexRatingKey(media, ratingKey); + continue; + } + if (probe === 'error') { + sawError = true; + continue; + } + } - if (ratingKey4k && is4k) { - plexSeasons = this.plexSeasonsCache[ratingKey4k]; - } + const seasonMeta = this.plexSeasonsCache[ratingKey]?.find( + (plexSeason) => plexSeason.index === season.seasonNumber + ); - const seasonMeta = plexSeasons?.find( - (plexSeason) => plexSeason.index === season.seasonNumber - ); + if (!seasonMeta) { + continue; + } - if (seasonMeta) { const cacheKey = `${is4k ? '4k' : 'std'}-${seasonMeta.ratingKey}`; + const otherCacheKey = `${is4k ? 'std' : '4k'}-${seasonMeta.ratingKey}`; if (cacheKey in this.plexEpisodeExistsCache) { - seasonExistsInPlex = this.plexEpisodeExistsCache[cacheKey]; - } else { - try { - // Season metadata exists, but we need to verify it has actual - // episode files. Plex can keep empty season entries. - const episodes = await this.plexClient?.getChildrenMetadata( - seasonMeta.ratingKey - ); - - const episodeVersions = - episodes?.flatMap((episode) => episode.Media ?? []) ?? []; - - if (is4k) { - seasonExistsInPlex = episodeVersions.some( - (mediaItem) => (mediaItem.width ?? 0) >= 2000 - ); - } else if (this.enable4kShow) { - seasonExistsInPlex = episodeVersions.some( - (mediaItem) => (mediaItem.width ?? 0) < 2000 - ); - } else { - seasonExistsInPlex = episodeVersions.length > 0; - } - } catch { - // If we can't fetch episodes, assume the season exists - // to avoid false removal - seasonExistsInPlex = true; + const cached = this.plexEpisodeExistsCache[cacheKey]; + if (cached.exists || !cached.verified) { + return true; } + continue; + } - this.plexEpisodeExistsCache[cacheKey] = seasonExistsInPlex; + try { + const episodes = + (await this.plexClient?.getChildrenMetadata(seasonMeta.ratingKey)) ?? + []; + const episodeVersions = episodes.flatMap( + (episode) => episode.Media ?? [] + ); + const episodeVerified = + episodes.length === 0 || + episodes.every((episode) => episode.Media?.length); + const seasonExists = this.seasonHasTieredEpisodes( + episodeVersions, + is4k + ); + this.plexEpisodeExistsCache[cacheKey] = { + exists: seasonExists, + verified: episodeVerified, + }; + if (!(otherCacheKey in this.plexEpisodeExistsCache)) { + this.plexEpisodeExistsCache[otherCacheKey] = { + exists: this.seasonHasTieredEpisodes(episodeVersions, !is4k), + verified: episodeVerified, + }; + } + if (seasonExists || !episodeVerified) { + return true; + } + } catch { + this.plexEpisodeExistsCache[cacheKey] = { + exists: true, + verified: true, + }; + return true; } } - return seasonExistsInPlex; + return sawError; } // Jellyfin diff --git a/server/lib/scanners/baseScanner.ts b/server/lib/scanners/baseScanner.ts index 93f4716650..35e67f0049 100644 --- a/server/lib/scanners/baseScanner.ts +++ b/server/lib/scanners/baseScanner.ts @@ -178,10 +178,17 @@ class BaseScanner { ratingKey && existing[is4k ? 'ratingKey4k' : 'ratingKey'] !== ratingKey ) { + existing.addPlexRatingKey( + existing[is4k ? 'ratingKey4k' : 'ratingKey'] + ); existing[is4k ? 'ratingKey4k' : 'ratingKey'] = ratingKey; changedExisting = true; } + if (existing.addPlexRatingKey(ratingKey)) { + changedExisting = true; + } + if ( jellyfinMediaId && existing[is4k ? 'jellyfinMediaId4k' : 'jellyfinMediaId'] !== @@ -274,6 +281,7 @@ class BaseScanner { newMedia.ratingKey = !is4k ? ratingKey : undefined; newMedia.ratingKey4k = is4k && this.enable4kMovie ? ratingKey : undefined; + newMedia.addPlexRatingKey(ratingKey); } if (jellyfinMediaId) { @@ -346,6 +354,7 @@ class BaseScanner { // We update the rating keys and jellyfinMediaId in the seasons loop because we need episode counts if (media && season.episodes > 0 && media.ratingKey !== ratingKey) { + media.addPlexRatingKey(media.ratingKey); media.ratingKey = ratingKey; } @@ -355,6 +364,7 @@ class BaseScanner { this.enable4kShow && media.ratingKey4k !== ratingKey ) { + media.addPlexRatingKey(media.ratingKey4k); media.ratingKey4k = ratingKey; } @@ -468,6 +478,7 @@ class BaseScanner { } if (media) { + media.addPlexRatingKey(ratingKey); media.seasons = [...media.seasons, ...newSeasons]; const newStandardSeasonsAvailable = ( @@ -651,6 +662,7 @@ class BaseScanner { ? MediaStatus.PROCESSING : MediaStatus.UNKNOWN, }); + newMedia.addPlexRatingKey(ratingKey); await mediaRepository.save(newMedia); this.log(`Saved ${title}`); } diff --git a/server/lib/scanners/plex/plex.test.ts b/server/lib/scanners/plex/plex.test.ts index 49089a6592..70229f3ba9 100644 --- a/server/lib/scanners/plex/plex.test.ts +++ b/server/lib/scanners/plex/plex.test.ts @@ -22,8 +22,9 @@ import MediaRequest from '@server/entity/MediaRequest'; import Season from '@server/entity/Season'; import SeasonRequest from '@server/entity/SeasonRequest'; import { User } from '@server/entity/User'; +import availabilitySync from '@server/lib/availabilitySync'; import { plexFullScanner } from '@server/lib/scanners/plex'; -import type { Library } from '@server/lib/settings'; +import type { Library, SonarrSettings } from '@server/lib/settings'; import { getSettings } from '@server/lib/settings'; import { setupTestDb } from '@server/test/db'; import { runWithMockTimers } from '@server/test/runWithMockTimers'; @@ -36,6 +37,19 @@ Object.defineProperty(animeList, 'sync', { writable: true, }); +function plexNotFoundError(): Error & { response: { status: number } } { + return Object.assign(new Error('Request failed with status code 404'), { + response: { status: 404 }, + }); +} + +function rethrowPlexTestError(error: unknown): never { + if (error instanceof Error && error.message === '404') { + throw plexNotFoundError(); + } + throw error; +} + let getLibrariesImpl: () => Promise = async () => []; let getLibraryContentsImpl: ( id: string @@ -44,8 +58,11 @@ let getLibraryContentsImpl: ( items: [], }); let getMetadataImpl: ( - key: string -) => Promise = async () => undefined; + key: string, + options?: { includeChildren?: boolean } +) => Promise = async () => { + throw plexNotFoundError(); +}; let getChildrenMetadataImpl: ( key: string ) => Promise = async () => []; @@ -68,7 +85,13 @@ Object.defineProperty(PlexAPI.prototype, 'getLibraryContents', { Object.defineProperty(PlexAPI.prototype, 'getMetadata', { get() { - return async (key: string) => getMetadataImpl(key); + return async (key: string, options?: { includeChildren?: boolean }) => { + try { + return await getMetadataImpl(key, options); + } catch (error) { + rethrowPlexTestError(error); + } + }; }, set() {}, configurable: true, @@ -76,7 +99,13 @@ Object.defineProperty(PlexAPI.prototype, 'getMetadata', { Object.defineProperty(PlexAPI.prototype, 'getChildrenMetadata', { get() { - return async (key: string) => getChildrenMetadataImpl(key); + return async (key: string) => { + try { + return await getChildrenMetadataImpl(key); + } catch (error) { + rethrowPlexTestError(error); + } + }; }, set() {}, configurable: true, @@ -84,20 +113,38 @@ Object.defineProperty(PlexAPI.prototype, 'getChildrenMetadata', { let getTvShowImpl: (args: { tvId: number; + language?: string; }) => Promise = async () => fakeTmdbShow(1); +Object.defineProperty(TheMovieDb.prototype, 'getTvShow', { + get() { + return async (args: { tvId: number; language?: string }) => + getTvShowImpl(args); + }, + set() {}, + configurable: true, +}); + +Object.defineProperty(TheMovieDb.prototype, 'getTvShowForScan', { + get() { + return async (args: { tvId: number; language?: string }) => + getTvShowImpl(args); + }, + set() {}, + configurable: true, +}); + for (const method of ['getTvShow', 'getTvShowForScan'] as const) { - Object.defineProperty(TheMovieDb.prototype, method, { - get() { - return async (args: { tvId: number }) => getTvShowImpl(args); - }, - set() {}, + // plexFullScanner / availabilitySync built their own TheMovieDb on import, + // before the stubs above. + Object.defineProperty(plexFullScanner.tmdb, method, { + value: async (args: { tvId: number; language?: string }) => + getTvShowImpl(args), configurable: true, }); - - // plexFullScanner built its own TheMovieDb on import, before the stubs above. - Object.defineProperty(plexFullScanner.tmdb, method, { - value: async (args: { tvId: number }) => getTvShowImpl(args), + Object.defineProperty(availabilitySync.tmdb, method, { + value: async (args: { tvId: number; language?: string }) => + getTvShowImpl(args), configurable: true, }); } @@ -106,18 +153,20 @@ mock.method(MediaRequest, 'sendNotification', async () => undefined); setupTestDb(); +function tmdbSeasons(count: number): TmdbTvSeasonResult[] { + return Array.from({ length: count }, (_, i) => ({ + id: i + 1, + air_date: '2024-01-01', + episode_count: 10, + name: `Season ${i + 1}`, + overview: '', + season_number: i + 1, + })); +} + function fakeTmdbShow( tmdbId: number, - seasons: TmdbTvSeasonResult[] = [ - { - id: 1, - air_date: '2024-01-01', - episode_count: 10, - name: 'Season 1', - overview: '', - season_number: 1, - }, - ] + seasons: TmdbTvSeasonResult[] = tmdbSeasons(1) ): TmdbTvDetails { return { id: tmdbId, @@ -132,7 +181,10 @@ function fakeTmdbShow( last_air_date: '2024-01-01', name: 'Test Show', networks: [], - number_of_episodes: 10, + number_of_episodes: seasons.reduce( + (sum, season) => sum + season.episode_count, + 0 + ), number_of_seasons: seasons.length, origin_country: ['US'], original_language: 'en', @@ -155,65 +207,104 @@ function fakeTmdbShow( }; } -function fakePlexShowItem(ratingKey: string): PlexLibraryItem { +function fakePlexSeason(seasonNumber: number, ratingKey: string): PlexMetadata { return { ratingKey, - title: 'Test Show', - guid: 'plex://show/test-show', - addedAt: 1700000000, - updatedAt: 1700000000, - type: 'show', + guid: `plex://season/${ratingKey}`, + type: 'season', + title: `Season ${seasonNumber}`, + Guid: [], + index: seasonNumber, + leafCount: 0, + viewedLeafCount: 0, + addedAt: 1, + updatedAt: 1, Media: [], }; } -function fakePlexShowMetadata( +function fakePlexEpisodes( + prefix: string, + count: number, + width: number +): PlexMetadata[] { + const videoResolution = width >= 2000 ? '4k' : '1080'; + return Array.from({ length: count }, (_, i) => ({ + ratingKey: `${prefix}-ep-${i}`, + guid: `plex://episode/${prefix}-ep-${i}`, + type: 'movie' as const, + title: `Episode ${i + 1}`, + Guid: [], + index: i + 1, + leafCount: 0, + viewedLeafCount: 0, + addedAt: 1, + updatedAt: 1, + Media: [ + { + id: i, + duration: 2400, + bitrate: width >= 2000 ? 20000 : 4000, + width, + height: width >= 2000 ? 2160 : 1080, + aspectRatio: 1.78, + audioChannels: 2, + audioCodec: 'aac', + videoCodec: 'h264', + videoResolution, + container: 'mkv', + videoFrameRate: '24p', + videoProfile: 'high', + }, + ], + })); +} + +function fakePlexLibraryShow( ratingKey: string, tmdbId: number, - children: PlexMetadata[] -): PlexMetadata { + title = 'Split Show' +): PlexLibraryItem { return { ratingKey, - title: 'Test Show', - guid: 'plex://show/test-show', + title, + guid: `tmdb://${tmdbId}`, type: 'show', - Guid: [{ id: `tmdb://${tmdbId}` }], - Children: { size: 12, Metadata: children }, - index: 1, - leafCount: 0, - viewedLeafCount: 0, - addedAt: 1700000000, - updatedAt: 1700000000, + addedAt: 1, + updatedAt: 1, Media: [], }; } -function fakePlexSeasonMetadata( - seasonNumber: number, - ratingKey: string +function fakePlexShowMetadata( + ratingKey: string, + tmdbId: number, + seasons: PlexMetadata[] ): PlexMetadata { return { ratingKey, - title: `Season ${seasonNumber}`, - guid: `plex://season/test-season-${seasonNumber}`, - type: 'season', - Guid: [], - index: seasonNumber, + guid: `tmdb://${tmdbId}`, + type: 'show', + title: 'Test Show', + Guid: [{ id: `tmdb://${tmdbId}` }], + Children: { + size: 12, + Metadata: seasons, + }, + index: 1, leafCount: 0, viewedLeafCount: 0, - addedAt: 1700000000, - updatedAt: 1700000000, + addedAt: 1, + updatedAt: 1, Media: [], }; } -function configurePlexWithLibrary( - libraries: Library[] = [ - { id: 'test-library-id', name: 'TV Shows', enabled: true, type: 'show' }, - ] -): void { +function configurePlexLibraries(libraries: Library[]): void { const settings = getSettings(); settings.main.mediaServerType = MediaServerType.PLEX; + settings.sonarr = []; + settings.radarr = []; settings.plex = { ...settings.plex, ip: '127.0.0.1', @@ -222,25 +313,77 @@ function configurePlexWithLibrary( }; } +/** Enables BaseScanner.startRun() 4K show detection via settings.sonarr. */ +function enableSonarr4k(): void { + getSettings().sonarr = [ + { + id: 0, + name: 'Sonarr 4K', + hostname: 'localhost', + port: 8989, + apiKey: 'test-key', + baseUrl: '', + useSsl: false, + activeProfileId: 1, + activeProfileName: 'Default', + activeDirectory: '/tv', + activeLanguageProfileId: 1, + seriesType: 'standard', + animeSeriesType: 'standard', + animeTags: [], + is4k: true, + enableSeasonFolders: true, + monitorNewItems: 'all', + tags: [], + isDefault: true, + syncEnabled: true, + preventSearch: false, + tagRequests: false, + overrideRule: [], + externalUrl: '', + } satisfies SonarrSettings, + ]; +} + +async function assertSeasonsAvailable(tmdbId: number, count: number) { + const updated = await getRepository(Media).findOneOrFail({ + where: { tmdbId, mediaType: MediaType.TV }, + relations: ['seasons'], + }); + + assert.strictEqual(updated.seasons.length, count); + for (const season of updated.seasons) { + assert.strictEqual( + season.status, + MediaStatus.AVAILABLE, + `Season ${season.seasonNumber} should remain AVAILABLE` + ); + } + assert.strictEqual(updated.status, MediaStatus.AVAILABLE); + return updated; +} + describe('Plex Scanner', () => { beforeEach(() => { - getLibrariesImpl = async () => [ - { - key: 'test-library-id', - title: 'TV Shows', - type: 'show', - agent: 'tv.plex.agents.series', - }, - ]; + getLibrariesImpl = async () => []; getLibraryContentsImpl = async () => ({ totalSize: 0, items: [] }); - getMetadataImpl = async () => undefined; + getMetadataImpl = async () => { + throw new Error('404'); + }; getChildrenMetadataImpl = async () => []; getTvShowImpl = async () => fakeTmdbShow(1); }); describe('in-flight request handling', () => { it('leaves an approved request alone when the show has no episodes yet', async () => { - configurePlexWithLibrary(); + configurePlexLibraries([ + { + id: 'test-library-id', + name: 'TV Shows', + enabled: true, + type: 'show', + }, + ]); const mediaRepository = getRepository(Media); const requestRepository = getRepository(MediaRequest); @@ -290,21 +433,37 @@ describe('Plex Scanner', () => { ); getTvShowImpl = async () => fakeTmdbShow(6100); + getLibrariesImpl = async () => [ + { + key: 'test-library-id', + title: 'TV Shows', + type: 'show', + agent: 'tv.plex.agents.series', + }, + ]; getLibraryContentsImpl = async (id: string) => id === 'test-library-id' ? { totalSize: 1, - items: [fakePlexShowItem('plex-inflight-show-key')], + items: [ + fakePlexLibraryShow( + 'plex-inflight-show-key', + 6100, + 'Test Show' + ), + ], } : { totalSize: 0, items: [] }; - getMetadataImpl = async (key: string) => - key === 'plex-inflight-show-key' - ? fakePlexShowMetadata('plex-inflight-show-key', 6100, [ - fakePlexSeasonMetadata(1, 'plex-inflight-s1-key'), - ]) - : undefined; + getMetadataImpl = async (key: string) => { + if (key === 'plex-inflight-show-key') { + return fakePlexShowMetadata('plex-inflight-show-key', 6100, [ + fakePlexSeason(1, 'plex-inflight-s1-key'), + ]); + } + throw new Error('404'); + }; getChildrenMetadataImpl = async () => []; @@ -321,4 +480,279 @@ describe('Plex Scanner', () => { ); }); }); + + describe('split-library availability', () => { + const split1080p: Library = { + id: 'lib-1080', + name: 'TV 1080p', + enabled: true, + type: 'show', + }; + const split4k: Library = { + id: 'lib-4k', + name: 'TV 4K', + enabled: true, + type: 'show', + }; + const libA: Library = { + id: 'lib-a', + name: 'TV A', + enabled: true, + type: 'show', + }; + const libB: Library = { + id: 'lib-b', + name: 'TV B', + enabled: true, + type: 'show', + }; + + function mockSplitShow(tmdbId: number) { + const rk1080 = `split-${tmdbId}-1080`; + const rk4k = `split-${tmdbId}-4k`; + + getTvShowImpl = async () => fakeTmdbShow(tmdbId, tmdbSeasons(4)); + getLibrariesImpl = async () => [ + { + type: 'show', + key: split1080p.id, + title: split1080p.name, + agent: 'tv.plex.agents.series', + }, + { + type: 'show', + key: split4k.id, + title: split4k.name, + agent: 'tv.plex.agents.series', + }, + ]; + getLibraryContentsImpl = async (id: string) => { + if (id === split1080p.id) { + return { + totalSize: 1, + items: [fakePlexLibraryShow(rk1080, tmdbId)], + }; + } + if (id === split4k.id) { + return { + totalSize: 1, + items: [fakePlexLibraryShow(rk4k, tmdbId)], + }; + } + return { totalSize: 0, items: [] }; + }; + getMetadataImpl = async (key: string) => { + if (key === rk1080) { + return fakePlexShowMetadata(rk1080, tmdbId, [ + fakePlexSeason(1, `${rk1080}-s1`), + ]); + } + if (key === rk4k) { + return fakePlexShowMetadata(rk4k, tmdbId, [ + fakePlexSeason(2, `${rk4k}-s2`), + fakePlexSeason(3, `${rk4k}-s3`), + fakePlexSeason(4, `${rk4k}-s4`), + ]); + } + throw new Error('404'); + }; + getChildrenMetadataImpl = async (key: string) => { + if (key === rk1080) { + return [fakePlexSeason(1, `${rk1080}-s1`)]; + } + if (key === rk4k) { + return [ + fakePlexSeason(2, `${rk4k}-s2`), + fakePlexSeason(3, `${rk4k}-s3`), + fakePlexSeason(4, `${rk4k}-s4`), + ]; + } + if (key === `${rk1080}-s1`) { + return fakePlexEpisodes(key, 10, 1920); + } + if (key === `${rk4k}-s2`) { + // One 1080p episode in the 4K library copy, which used to steal ratingKey. + return [ + ...fakePlexEpisodes(`${key}-4k`, 9, 3840), + ...fakePlexEpisodes(`${key}-1080`, 1, 1920), + ]; + } + if (key === `${rk4k}-s3` || key === `${rk4k}-s4`) { + return fakePlexEpisodes(key, 10, 3840); + } + return []; + }; + + return { rk1080, rk4k }; + } + + function mockTwoStandardLibraries(tmdbId: number) { + const rkA = `std-${tmdbId}-a`; + const rkB = `std-${tmdbId}-b`; + + getTvShowImpl = async () => fakeTmdbShow(tmdbId, tmdbSeasons(4)); + getLibrariesImpl = async () => [ + { + type: 'show', + key: libA.id, + title: libA.name, + agent: 'tv.plex.agents.series', + }, + { + type: 'show', + key: libB.id, + title: libB.name, + agent: 'tv.plex.agents.series', + }, + ]; + getLibraryContentsImpl = async (id: string) => { + if (id === libA.id) { + return { totalSize: 1, items: [fakePlexLibraryShow(rkA, tmdbId)] }; + } + if (id === libB.id) { + return { totalSize: 1, items: [fakePlexLibraryShow(rkB, tmdbId)] }; + } + return { totalSize: 0, items: [] }; + }; + getMetadataImpl = async (key: string) => { + if (key === rkA) { + return fakePlexShowMetadata(rkA, tmdbId, [ + fakePlexSeason(1, `${rkA}-s1`), + ]); + } + if (key === rkB) { + return fakePlexShowMetadata(rkB, tmdbId, [ + fakePlexSeason(2, `${rkB}-s2`), + fakePlexSeason(3, `${rkB}-s3`), + fakePlexSeason(4, `${rkB}-s4`), + ]); + } + throw new Error('404'); + }; + getChildrenMetadataImpl = async (key: string) => { + if (key === rkA) { + return [fakePlexSeason(1, `${rkA}-s1`)]; + } + if (key === rkB) { + return [ + fakePlexSeason(2, `${rkB}-s2`), + fakePlexSeason(3, `${rkB}-s3`), + fakePlexSeason(4, `${rkB}-s4`), + ]; + } + if ( + key === `${rkA}-s1` || + key === `${rkB}-s2` || + key === `${rkB}-s3` || + key === `${rkB}-s4` + ) { + return fakePlexEpisodes(key, 10, 1920); + } + return []; + }; + + return { rkA, rkB }; + } + + for (const order of [ + [split1080p, split4k], + [split4k, split1080p], + ] as const) { + it(`keeps every season after scanning ${order.map((library) => library.name).join(' then ')} then running availability sync`, async () => { + const tmdbId = order[0].id === split1080p.id ? 9010 : 9011; + const { rk1080, rk4k } = mockSplitShow(tmdbId); + configurePlexLibraries([...order]); + + await runWithMockTimers(() => plexFullScanner.run()); + + const scanned = await getRepository(Media).findOneOrFail({ + where: { tmdbId, mediaType: MediaType.TV }, + }); + assert.deepStrictEqual( + scanned.plexRatingKeys?.slice().sort(), + [rk1080, rk4k].sort() + ); + + await availabilitySync.run(); + await assertSeasonsAvailable(tmdbId, 4); + }); + } + + for (const order of [ + [libA, libB], + [libB, libA], + ] as const) { + it(`keeps every season after scanning two non-4K libraries ${order.map((library) => library.name).join(' then ')} then running availability sync`, async () => { + const tmdbId = order[0].id === libA.id ? 9020 : 9021; + const { rkA, rkB } = mockTwoStandardLibraries(tmdbId); + configurePlexLibraries([...order]); + + await runWithMockTimers(() => plexFullScanner.run()); + + const scanned = await getRepository(Media).findOneOrFail({ + where: { tmdbId, mediaType: MediaType.TV }, + }); + assert.deepStrictEqual( + scanned.plexRatingKeys?.slice().sort(), + [rkA, rkB].sort() + ); + + await availabilitySync.run(); + await assertSeasonsAvailable(tmdbId, 4); + }); + } + + it('records ratingKey4k and 4K season/show availability when Sonarr enables 4K detection', async () => { + const tmdbId = 9030; + const { rk1080, rk4k } = mockSplitShow(tmdbId); + + // Pure 4K season 2 so the 4K library copy does not also claim ratingKey. + const baseChildren = getChildrenMetadataImpl; + getChildrenMetadataImpl = async (key: string) => { + if (key === `${rk4k}-s2`) { + return fakePlexEpisodes(key, 10, 3840); + } + return baseChildren(key); + }; + + configurePlexLibraries([split1080p, split4k]); + enableSonarr4k(); + + await runWithMockTimers(() => plexFullScanner.run()); + + const scanned = await getRepository(Media).findOneOrFail({ + where: { tmdbId, mediaType: MediaType.TV }, + relations: ['seasons'], + }); + + assert.deepStrictEqual( + scanned.plexRatingKeys?.slice().sort(), + [rk1080, rk4k].sort() + ); + assert.strictEqual(scanned.ratingKey, rk1080); + assert.strictEqual(scanned.ratingKey4k, rk4k); + + const seasons = Object.fromEntries( + scanned.seasons.map((season) => [season.seasonNumber, season]) + ); + + assert.strictEqual(seasons[1].status, MediaStatus.AVAILABLE); + assert.strictEqual(seasons[1].status4k, MediaStatus.UNKNOWN); + for (const seasonNumber of [2, 3, 4]) { + assert.strictEqual( + seasons[seasonNumber].status, + MediaStatus.UNKNOWN, + `Season ${seasonNumber} should have no standard availability` + ); + assert.strictEqual( + seasons[seasonNumber].status4k, + MediaStatus.AVAILABLE, + `Season ${seasonNumber} should be 4K AVAILABLE` + ); + } + + assert.strictEqual(scanned.status, MediaStatus.PARTIALLY_AVAILABLE); + assert.strictEqual(scanned.status4k, MediaStatus.PARTIALLY_AVAILABLE); + }); + }); }); diff --git a/server/migration/postgres/1794666034616-AddMediaPlexRatingKeys.ts b/server/migration/postgres/1794666034616-AddMediaPlexRatingKeys.ts new file mode 100644 index 0000000000..fe0769984c --- /dev/null +++ b/server/migration/postgres/1794666034616-AddMediaPlexRatingKeys.ts @@ -0,0 +1,13 @@ +import type { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddMediaPlexRatingKeys1794666034616 implements MigrationInterface { + name = 'AddMediaPlexRatingKeys1794666034616'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "media" ADD "plexRatingKeys" text`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "media" DROP COLUMN "plexRatingKeys"`); + } +} diff --git a/server/migration/sqlite/1794666022028-AddMediaPlexRatingKeys.ts b/server/migration/sqlite/1794666022028-AddMediaPlexRatingKeys.ts new file mode 100644 index 0000000000..41b1099ed8 --- /dev/null +++ b/server/migration/sqlite/1794666022028-AddMediaPlexRatingKeys.ts @@ -0,0 +1,75 @@ +import type { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddMediaPlexRatingKeys1794666022028 implements MigrationInterface { + name = 'AddMediaPlexRatingKeys1794666022028'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_f8233358694d1677a67899b90a"`); + await queryRunner.query(`DROP INDEX "IDX_5d6218de4f547909391a5c1347"`); + await queryRunner.query(`DROP INDEX "IDX_c730c2d67f271a372c39a07b7e"`); + await queryRunner.query(`DROP INDEX "IDX_7ff2d11f6a83cb52386eaebe74"`); + await queryRunner.query(`DROP INDEX "IDX_41a289eb1fa489c1bc6f38d9c3"`); + await queryRunner.query(`DROP INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5"`); + await queryRunner.query( + `CREATE TABLE "temporary_media" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "mediaType" varchar NOT NULL, "tmdbId" integer NOT NULL, "tvdbId" integer, "imdbId" varchar, "status" integer NOT NULL DEFAULT (1), "status4k" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "updatedAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "lastSeasonChange" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "mediaAddedAt" datetime DEFAULT (CURRENT_TIMESTAMP), "serviceId" integer, "serviceId4k" integer, "externalServiceId" integer, "externalServiceId4k" integer, "externalServiceSlug" varchar, "externalServiceSlug4k" varchar, "ratingKey" varchar, "ratingKey4k" varchar, "jellyfinMediaId" varchar, "jellyfinMediaId4k" varchar, "plexRatingKeys" text, CONSTRAINT "UQ_41a289eb1fa489c1bc6f38d9c3c" UNIQUE ("tvdbId"))` + ); + await queryRunner.query( + `INSERT INTO "temporary_media"("id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "status4k", "createdAt", "updatedAt", "lastSeasonChange", "mediaAddedAt", "serviceId", "serviceId4k", "externalServiceId", "externalServiceId4k", "externalServiceSlug", "externalServiceSlug4k", "ratingKey", "ratingKey4k", "jellyfinMediaId", "jellyfinMediaId4k") SELECT "id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "status4k", "createdAt", "updatedAt", "lastSeasonChange", "mediaAddedAt", "serviceId", "serviceId4k", "externalServiceId", "externalServiceId4k", "externalServiceSlug", "externalServiceSlug4k", "ratingKey", "ratingKey4k", "jellyfinMediaId", "jellyfinMediaId4k" FROM "media"` + ); + await queryRunner.query(`DROP TABLE "media"`); + await queryRunner.query(`ALTER TABLE "temporary_media" RENAME TO "media"`); + await queryRunner.query( + `CREATE INDEX "IDX_f8233358694d1677a67899b90a" ON "media" ("tmdbId", "mediaType") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_5d6218de4f547909391a5c1347" ON "media" ("status4k") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_c730c2d67f271a372c39a07b7e" ON "media" ("status") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_7ff2d11f6a83cb52386eaebe74" ON "media" ("imdbId") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_41a289eb1fa489c1bc6f38d9c3" ON "media" ("tvdbId") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5" ON "media" ("tmdbId") ` + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5"`); + await queryRunner.query(`DROP INDEX "IDX_41a289eb1fa489c1bc6f38d9c3"`); + await queryRunner.query(`DROP INDEX "IDX_7ff2d11f6a83cb52386eaebe74"`); + await queryRunner.query(`DROP INDEX "IDX_c730c2d67f271a372c39a07b7e"`); + await queryRunner.query(`DROP INDEX "IDX_5d6218de4f547909391a5c1347"`); + await queryRunner.query(`DROP INDEX "IDX_f8233358694d1677a67899b90a"`); + await queryRunner.query(`ALTER TABLE "media" RENAME TO "temporary_media"`); + await queryRunner.query( + `CREATE TABLE "media" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "mediaType" varchar NOT NULL, "tmdbId" integer NOT NULL, "tvdbId" integer, "imdbId" varchar, "status" integer NOT NULL DEFAULT (1), "status4k" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "updatedAt" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "lastSeasonChange" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "mediaAddedAt" datetime DEFAULT (CURRENT_TIMESTAMP), "serviceId" integer, "serviceId4k" integer, "externalServiceId" integer, "externalServiceId4k" integer, "externalServiceSlug" varchar, "externalServiceSlug4k" varchar, "ratingKey" varchar, "ratingKey4k" varchar, "jellyfinMediaId" varchar, "jellyfinMediaId4k" varchar, CONSTRAINT "UQ_41a289eb1fa489c1bc6f38d9c3c" UNIQUE ("tvdbId"))` + ); + await queryRunner.query( + `INSERT INTO "media"("id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "status4k", "createdAt", "updatedAt", "lastSeasonChange", "mediaAddedAt", "serviceId", "serviceId4k", "externalServiceId", "externalServiceId4k", "externalServiceSlug", "externalServiceSlug4k", "ratingKey", "ratingKey4k", "jellyfinMediaId", "jellyfinMediaId4k") SELECT "id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "status4k", "createdAt", "updatedAt", "lastSeasonChange", "mediaAddedAt", "serviceId", "serviceId4k", "externalServiceId", "externalServiceId4k", "externalServiceSlug", "externalServiceSlug4k", "ratingKey", "ratingKey4k", "jellyfinMediaId", "jellyfinMediaId4k" FROM "temporary_media"` + ); + await queryRunner.query(`DROP TABLE "temporary_media"`); + await queryRunner.query( + `CREATE INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5" ON "media" ("tmdbId") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_41a289eb1fa489c1bc6f38d9c3" ON "media" ("tvdbId") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_7ff2d11f6a83cb52386eaebe74" ON "media" ("imdbId") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_c730c2d67f271a372c39a07b7e" ON "media" ("status") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_5d6218de4f547909391a5c1347" ON "media" ("status4k") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_f8233358694d1677a67899b90a" ON "media" ("tmdbId", "mediaType") ` + ); + } +}