|  | @@ -2,7 +2,6 @@
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  using System;
 | 
	
		
			
				|  |  |  using System.Collections.Generic;
 | 
	
		
			
				|  |  | -using System.Globalization;
 | 
	
		
			
				|  |  |  using System.Linq;
 | 
	
		
			
				|  |  |  using Jellyfin.Data.Entities;
 | 
	
		
			
				|  |  |  using Jellyfin.Data.Enums;
 | 
	
	
		
			
				|  | @@ -11,7 +10,6 @@ using MediaBrowser.Controller.Dto;
 | 
	
		
			
				|  |  |  using MediaBrowser.Controller.Entities;
 | 
	
		
			
				|  |  |  using MediaBrowser.Controller.Library;
 | 
	
		
			
				|  |  |  using MediaBrowser.Controller.TV;
 | 
	
		
			
				|  |  | -using MediaBrowser.Model.Entities;
 | 
	
		
			
				|  |  |  using MediaBrowser.Model.Querying;
 | 
	
		
			
				|  |  |  using Episode = MediaBrowser.Controller.Entities.TV.Episode;
 | 
	
		
			
				|  |  |  using Series = MediaBrowser.Controller.Entities.TV.Series;
 | 
	
	
		
			
				|  | @@ -23,12 +21,14 @@ namespace Emby.Server.Implementations.TV
 | 
	
		
			
				|  |  |          private readonly IUserManager _userManager;
 | 
	
		
			
				|  |  |          private readonly IUserDataManager _userDataManager;
 | 
	
		
			
				|  |  |          private readonly ILibraryManager _libraryManager;
 | 
	
		
			
				|  |  | +        private readonly IServerConfigurationManager _configurationManager;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager)
 | 
	
		
			
				|  |  | +        public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              _userManager = userManager;
 | 
	
		
			
				|  |  |              _userDataManager = userDataManager;
 | 
	
		
			
				|  |  |              _libraryManager = libraryManager;
 | 
	
		
			
				|  |  | +            _configurationManager = configurationManager;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          public QueryResult<BaseItem> GetNextUp(NextUpQuery request, DtoOptions dtoOptions)
 | 
	
	
		
			
				|  | @@ -200,13 +200,10 @@ namespace Emby.Server.Implementations.TV
 | 
	
		
			
				|  |  |                  ParentIndexNumberNotEquals = 0,
 | 
	
		
			
				|  |  |                  DtoOptions = new DtoOptions
 | 
	
		
			
				|  |  |                  {
 | 
	
		
			
				|  |  | -                    Fields = new ItemFields[]
 | 
	
		
			
				|  |  | -                    {
 | 
	
		
			
				|  |  | -                        ItemFields.SortName
 | 
	
		
			
				|  |  | -                    },
 | 
	
		
			
				|  |  | +                    Fields = new[] { ItemFields.SortName },
 | 
	
		
			
				|  |  |                      EnableImages = false
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -            }).FirstOrDefault();
 | 
	
		
			
				|  |  | +            }).Cast<Episode>().FirstOrDefault();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              Func<Episode> getEpisode = () =>
 | 
	
		
			
				|  |  |              {
 | 
	
	
		
			
				|  | @@ -224,6 +221,39 @@ namespace Emby.Server.Implementations.TV
 | 
	
		
			
				|  |  |                      DtoOptions = dtoOptions
 | 
	
		
			
				|  |  |                  }).Cast<Episode>().FirstOrDefault();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +                if (_configurationManager.Configuration.DisplaySpecialsWithinSeasons)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    var consideredEpisodes = _libraryManager.GetItemList(new InternalItemsQuery(user)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        AncestorWithPresentationUniqueKey = null,
 | 
	
		
			
				|  |  | +                        SeriesPresentationUniqueKey = seriesKey,
 | 
	
		
			
				|  |  | +                        ParentIndexNumber = 0,
 | 
	
		
			
				|  |  | +                        IncludeItemTypes = new[] { nameof(Episode) },
 | 
	
		
			
				|  |  | +                        IsPlayed = false,
 | 
	
		
			
				|  |  | +                        IsVirtualItem = false,
 | 
	
		
			
				|  |  | +                        DtoOptions = dtoOptions
 | 
	
		
			
				|  |  | +                    }).Cast<Episode>().Where(episode => episode.AirsBeforeSeasonNumber != null || episode.AirsAfterSeasonNumber != null).ToList();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    if (lastWatchedEpisode != null)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        // Last watched episode is added, because there could be specials that aired before the last watched episode
 | 
	
		
			
				|  |  | +                        consideredEpisodes.Add(lastWatchedEpisode);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    if (nextEpisode != null)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        consideredEpisodes.Add(nextEpisode);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    var sortedConsideredEpisodes = _libraryManager.Sort(consideredEpisodes, user, new[] { (ItemSortBy.AiredEpisodeOrder, SortOrder.Ascending) }).Cast<Episode>();
 | 
	
		
			
				|  |  | +                    if (lastWatchedEpisode != null)
 | 
	
		
			
				|  |  | +                    {
 | 
	
		
			
				|  |  | +                        sortedConsideredEpisodes = sortedConsideredEpisodes.SkipWhile(episode => episode.Id != lastWatchedEpisode.Id).Skip(1);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    nextEpisode = sortedConsideredEpisodes.FirstOrDefault();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  if (nextEpisode != null)
 | 
	
		
			
				|  |  |                  {
 | 
	
		
			
				|  |  |                      var userData = _userDataManager.GetUserData(user, nextEpisode);
 |