1+ import type { SonarrSeason } from '@server/api/servarr/sonarr' ;
12import TheMovieDb from '@server/api/themoviedb' ;
23import { MediaStatus , MediaType } from '@server/constants/media' ;
34import { getRepository } from '@server/datasource' ;
45import Media from '@server/entity/Media' ;
6+ import { MediaRequest } from '@server/entity/MediaRequest' ;
57import Season from '@server/entity/Season' ;
8+ import SeasonRequest from '@server/entity/SeasonRequest' ;
69import { getSettings } from '@server/lib/settings' ;
710import logger from '@server/logger' ;
811import AsyncLock from '@server/utils/asyncLock' ;
@@ -212,7 +215,7 @@ class BaseScanner<T> {
212215
213216 /**
214217 * processShow takes a TMDB ID and an array of ProcessableSeasons, which
215- * should include the total episodes a sesaon has + the total available
218+ * should include the total episodes a season has + the total available
216219 * episodes that each season currently has. Unlike processMovie, this method
217220 * does not take an `is4k` option. We handle both the 4k _and_ non 4k status
218221 * in one method.
@@ -279,13 +282,14 @@ class BaseScanner<T> {
279282 // force it to stay available (to avoid competing scanners)
280283 existingSeason . status =
281284 ( season . totalEpisodes === season . episodes && season . episodes > 0 ) ||
282- existingSeason . status === MediaStatus . AVAILABLE
285+ ( existingSeason . status === MediaStatus . AVAILABLE &&
286+ season . episodes > 0 )
283287 ? MediaStatus . AVAILABLE
284288 : season . episodes > 0
285289 ? MediaStatus . PARTIALLY_AVAILABLE
286290 : ! season . is4kOverride && season . processing
287291 ? MediaStatus . PROCESSING
288- : settings . main . removeUnmonitoredFromRequestsEnabled &&
292+ : settings . main . removeUnmonitoredEnabled &&
289293 ! season . monitored &&
290294 season . episodes == 0
291295 ? MediaStatus . UNKNOWN
@@ -296,13 +300,14 @@ class BaseScanner<T> {
296300 ( this . enable4kShow &&
297301 season . episodes4k === season . totalEpisodes &&
298302 season . episodes4k > 0 ) ||
299- existingSeason . status4k === MediaStatus . AVAILABLE
303+ ( existingSeason . status4k === MediaStatus . AVAILABLE &&
304+ season . episodes > 0 )
300305 ? MediaStatus . AVAILABLE
301306 : this . enable4kShow && season . episodes4k > 0
302307 ? MediaStatus . PARTIALLY_AVAILABLE
303308 : season . is4kOverride && season . processing
304309 ? MediaStatus . PROCESSING
305- : settings . main . removeUnmonitoredFromRequestsEnabled &&
310+ : settings . main . removeUnmonitoredEnabled &&
306311 ! season . monitored &&
307312 season . episodes4k == 0
308313 ? MediaStatus . UNKNOWN
@@ -316,7 +321,9 @@ class BaseScanner<T> {
316321 ? MediaStatus . AVAILABLE
317322 : season . episodes > 0
318323 ? MediaStatus . PARTIALLY_AVAILABLE
319- : ! season . is4kOverride && season . processing
324+ : ! season . is4kOverride &&
325+ season . processing &&
326+ season . monitored
320327 ? MediaStatus . PROCESSING
321328 : MediaStatus . UNKNOWN ,
322329 status4k :
@@ -326,7 +333,7 @@ class BaseScanner<T> {
326333 ? MediaStatus . AVAILABLE
327334 : this . enable4kShow && season . episodes4k > 0
328335 ? MediaStatus . PARTIALLY_AVAILABLE
329- : season . is4kOverride && season . processing
336+ : season . is4kOverride && season . processing && season . monitored
330337 ? MediaStatus . PROCESSING
331338 : MediaStatus . UNKNOWN ,
332339 } )
@@ -643,6 +650,41 @@ class BaseScanner<T> {
643650 }
644651 } ) ;
645652 }
653+
654+ protected async processUnmonitoredSeason (
655+ tmdbId : number ,
656+ season : SonarrSeason
657+ ) : Promise < void > {
658+ // Remove unmonitored seasons from Requests
659+ const requestRepository = getRepository ( MediaRequest ) ;
660+ const seasonRequestRepository = getRepository ( SeasonRequest ) ;
661+
662+ const existingRequests = await requestRepository
663+ . createQueryBuilder ( 'request' )
664+ . innerJoinAndSelect ( 'request.media' , 'media' )
665+ . innerJoinAndSelect ( 'request.seasons' , 'seasons' )
666+ . where ( 'media.tmdbId = :tmdbId' , { tmdbId : tmdbId } )
667+ . andWhere ( 'media.mediaType = :mediaType' , {
668+ mediaType : MediaType . TV ,
669+ } )
670+ . andWhere ( 'seasons.seasonNumber = :seasonNumber' , {
671+ seasonNumber : season . seasonNumber ,
672+ } )
673+ . getMany ( ) ;
674+
675+ if ( existingRequests && existingRequests . length > 0 ) {
676+ existingRequests . forEach ( ( existingRequest ) => {
677+ existingRequest . seasons . forEach ( async ( requestedSeason ) => {
678+ if ( requestedSeason . seasonNumber === season . seasonNumber ) {
679+ this . log (
680+ `Removing request for Season ${ season . seasonNumber } of tmdbId ${ tmdbId } as it is unmonitored`
681+ ) ;
682+ await seasonRequestRepository . remove ( requestedSeason ) ;
683+ }
684+ } ) ;
685+ } ) ;
686+ }
687+ }
646688}
647689
648690export default BaseScanner ;
0 commit comments