-
-
Notifications
You must be signed in to change notification settings - Fork 914
fix(watchlistsync): re-request deleted media from watchlist #3072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fallenbagel
merged 1 commit into
develop
from
fix/watchlist-sync-deleted-media-rerequests
May 25, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import type { PlexWatchlistItem } from '@server/api/plextv'; | ||
| import PlexTvAPI from '@server/api/plextv'; | ||
| import { | ||
| MediaRequestStatus, | ||
| MediaStatus, | ||
| MediaType, | ||
| } from '@server/constants/media'; | ||
| import { getRepository } from '@server/datasource'; | ||
| import Media from '@server/entity/Media'; | ||
| import { MediaRequest } from '@server/entity/MediaRequest'; | ||
| import { User } from '@server/entity/User'; | ||
| import { UserSettings } from '@server/entity/UserSettings'; | ||
| import { Permission } from '@server/lib/permissions'; | ||
| import { setupTestDb } from '@server/test/db'; | ||
| import assert from 'node:assert/strict'; | ||
| import { beforeEach, describe, it } from 'node:test'; | ||
|
|
||
| let watchlistItems: PlexWatchlistItem[] = []; | ||
|
|
||
| Object.defineProperty(PlexTvAPI.prototype, 'getWatchlist', { | ||
| get() { | ||
| return async () => ({ | ||
| offset: 0, | ||
| size: 20, | ||
| totalSize: watchlistItems.length, | ||
| items: watchlistItems, | ||
| }); | ||
| }, | ||
| set() {}, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| let requestCalls: { mediaId: number; mediaType: MediaType }[] = []; | ||
|
|
||
| Object.defineProperty(MediaRequest, 'request', { | ||
| value: async (body: { mediaId: number; mediaType: MediaType }) => { | ||
| requestCalls.push({ mediaId: body.mediaId, mediaType: body.mediaType }); | ||
| return {} as MediaRequest; | ||
| }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| import watchlistSync from '@server/lib/watchlistsync'; | ||
|
|
||
| setupTestDb(); | ||
|
|
||
| async function configureSyncUser(): Promise<User> { | ||
| const userRepository = getRepository(User); | ||
| const admin = await userRepository.findOneOrFail({ where: { id: 1 } }); | ||
|
|
||
| admin.plexToken = 'test-plex-token'; | ||
| admin.permissions = Permission.AUTO_REQUEST; | ||
| await userRepository.save(admin); | ||
|
|
||
| const userSettingsRepository = getRepository(UserSettings); | ||
| await userSettingsRepository.save( | ||
| new UserSettings({ | ||
| user: admin, | ||
| watchlistSyncMovies: true, | ||
| watchlistSyncTv: true, | ||
| }) | ||
| ); | ||
|
|
||
| return admin; | ||
| } | ||
|
|
||
| async function seedMedia( | ||
| tmdbId: number, | ||
| mediaType: MediaType, | ||
| status: MediaStatus | ||
| ): Promise<void> { | ||
| const mediaRepository = getRepository(Media); | ||
| await mediaRepository.save( | ||
| new Media({ | ||
| tmdbId, | ||
| mediaType, | ||
| status, | ||
| status4k: MediaStatus.UNKNOWN, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| function movieItem(tmdbId: number, title: string): PlexWatchlistItem { | ||
| return { ratingKey: `rk-${tmdbId}`, tmdbId, title, type: 'movie' }; | ||
| } | ||
|
|
||
| function showItem(tmdbId: number, title: string): PlexWatchlistItem { | ||
| return { | ||
| ratingKey: `rk-${tmdbId}`, | ||
| tmdbId, | ||
| tvdbId: tmdbId * 1000, | ||
| title, | ||
| type: 'show', | ||
| }; | ||
| } | ||
|
|
||
| describe('WatchlistSync re-request gating', () => { | ||
| beforeEach(() => { | ||
| requestCalls = []; | ||
| watchlistItems = []; | ||
| }); | ||
|
|
||
| it('re-requests DELETED watchlist items and skips non-requestable ones', async () => { | ||
| await configureSyncUser(); | ||
|
|
||
| await seedMedia(100, MediaType.MOVIE, MediaStatus.DELETED); | ||
| await seedMedia(101, MediaType.MOVIE, MediaStatus.UNKNOWN); | ||
| await seedMedia(102, MediaType.MOVIE, MediaStatus.AVAILABLE); | ||
| await seedMedia(103, MediaType.MOVIE, MediaStatus.BLOCKLISTED); | ||
|
|
||
| await seedMedia(200, MediaType.TV, MediaStatus.DELETED); | ||
| await seedMedia(201, MediaType.TV, MediaStatus.AVAILABLE); | ||
|
|
||
| watchlistItems = [ | ||
| movieItem(100, 'Deleted Movie'), | ||
| movieItem(101, 'Unknown Movie'), | ||
| movieItem(102, 'Available Movie'), | ||
| movieItem(103, 'Blocklisted Movie'), | ||
| showItem(200, 'Deleted Show'), | ||
| showItem(201, 'Available Show'), | ||
| ]; | ||
|
|
||
| await watchlistSync.syncWatchlist(); | ||
|
|
||
| const requestedArray = requestCalls.map( | ||
| (c) => `${c.mediaType}:${c.mediaId}` | ||
| ); | ||
| const requested = new Set(requestedArray); | ||
|
|
||
| assert.strictEqual( | ||
| requestedArray.length, | ||
| requested.size, | ||
| 'Each item should be requested exactly once' | ||
| ); | ||
|
|
||
|
fallenbagel marked this conversation as resolved.
|
||
| assert.ok( | ||
| requested.has(`${MediaType.MOVIE}:100`), | ||
| 'DELETED movie on the watchlist should be re-requested' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| requested.has(`${MediaType.MOVIE}:101`), | ||
| 'UNKNOWN movie should be requested' | ||
| ); | ||
| assert.ok( | ||
| !requested.has(`${MediaType.MOVIE}:102`), | ||
| 'AVAILABLE movie should NOT be requested' | ||
| ); | ||
| assert.ok( | ||
| !requested.has(`${MediaType.MOVIE}:103`), | ||
| 'BLOCKLISTED movie should NOT be requested' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| requested.has(`${MediaType.TV}:200`), | ||
| 'DELETED show should be re-requested' | ||
| ); | ||
| assert.ok( | ||
| !requested.has(`${MediaType.TV}:201`), | ||
| 'AVAILABLE show should NOT be requested' | ||
| ); | ||
| }); | ||
|
|
||
| it('re-requests DELETED watchlist items even when a stale auto-request exists', async () => { | ||
| const user = await configureSyncUser(); | ||
|
|
||
| await seedMedia(100, MediaType.MOVIE, MediaStatus.DELETED); | ||
|
|
||
| const media = await getRepository(Media).findOneOrFail({ | ||
| where: { tmdbId: 100, mediaType: MediaType.MOVIE }, | ||
| }); | ||
|
|
||
| await getRepository(MediaRequest).save( | ||
| new MediaRequest({ | ||
| type: MediaType.MOVIE, | ||
| status: MediaRequestStatus.COMPLETED, | ||
| media, | ||
| requestedBy: user, | ||
| is4k: false, | ||
| isAutoRequest: true, | ||
| }) | ||
| ); | ||
|
|
||
| watchlistItems = [movieItem(100, 'Deleted Movie')]; | ||
|
|
||
| await watchlistSync.syncWatchlist(); | ||
|
|
||
| const calls = requestCalls.filter( | ||
| (c) => c.mediaType === MediaType.MOVIE && c.mediaId === 100 | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| calls.length, | ||
| 1, | ||
| 'DELETED movie should be re-requested even when a stale auto-request exists' | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.