< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.TvShowsController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/TvShowsController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 126
Coverable lines: 126
Total lines: 387
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/25/2025 - 12:09:58 AM Line coverage: 0% (0/130) Branch coverage: 0% (0/46) Total lines: 39412/1/2025 - 12:11:46 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3901/29/2026 - 12:13:32 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 387 10/25/2025 - 12:09:58 AM Line coverage: 0% (0/130) Branch coverage: 0% (0/46) Total lines: 39412/1/2025 - 12:11:46 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 3901/29/2026 - 12:13:32 AM Line coverage: 0% (0/126) Branch coverage: 0% (0/46) Total lines: 387

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetNextUp(...)0%2040%
GetUpcomingEpisodes(...)0%2040%
GetEpisodes(...)0%930300%
GetSeasons(...)0%2040%
ApplyPaging(...)0%2040%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/TvShowsController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.DataAnnotations;
 4using System.Linq;
 5using Jellyfin.Api.Extensions;
 6using Jellyfin.Api.Helpers;
 7using Jellyfin.Api.ModelBinders;
 8using Jellyfin.Data.Enums;
 9using Jellyfin.Database.Implementations.Enums;
 10using Jellyfin.Extensions;
 11using MediaBrowser.Controller.Dto;
 12using MediaBrowser.Controller.Entities;
 13using MediaBrowser.Controller.Entities.TV;
 14using MediaBrowser.Controller.Library;
 15using MediaBrowser.Controller.TV;
 16using MediaBrowser.Model.Dto;
 17using MediaBrowser.Model.Entities;
 18using MediaBrowser.Model.Querying;
 19using Microsoft.AspNetCore.Authorization;
 20using Microsoft.AspNetCore.Http;
 21using Microsoft.AspNetCore.Mvc;
 22
 23namespace Jellyfin.Api.Controllers;
 24
 25/// <summary>
 26/// The tv shows controller.
 27/// </summary>
 28[Route("Shows")]
 29[Authorize]
 30public class TvShowsController : BaseJellyfinApiController
 31{
 32    private readonly IUserManager _userManager;
 33    private readonly ILibraryManager _libraryManager;
 34    private readonly IDtoService _dtoService;
 35    private readonly ITVSeriesManager _tvSeriesManager;
 36
 37    /// <summary>
 38    /// Initializes a new instance of the <see cref="TvShowsController"/> class.
 39    /// </summary>
 40    /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 41    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 42    /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
 43    /// <param name="tvSeriesManager">Instance of the <see cref="ITVSeriesManager"/> interface.</param>
 044    public TvShowsController(
 045        IUserManager userManager,
 046        ILibraryManager libraryManager,
 047        IDtoService dtoService,
 048        ITVSeriesManager tvSeriesManager)
 49    {
 050        _userManager = userManager;
 051        _libraryManager = libraryManager;
 052        _dtoService = dtoService;
 053        _tvSeriesManager = tvSeriesManager;
 054    }
 55
 56    /// <summary>
 57    /// Gets a list of next up episodes.
 58    /// </summary>
 59    /// <param name="userId">The user id of the user to get the next up episodes for.</param>
 60    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 61    /// <param name="limit">Optional. The maximum number of records to return.</param>
 62    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 63    /// <param name="seriesId">Optional. Filter by series id.</param>
 64    /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use t
 65    /// <param name="enableImages">Optional. Include image information in output.</param>
 66    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 67    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 68    /// <param name="enableUserData">Optional. Include user data.</param>
 69    /// <param name="nextUpDateCutoff">Optional. Starting date of shows to show in Next Up section.</param>
 70    /// <param name="enableTotalRecordCount">Whether to enable the total records count. Defaults to true.</param>
 71    /// <param name="enableResumable">Whether to include resumable episodes in next up results.</param>
 72    /// <param name="enableRewatching">Whether to include watched episodes in next up results.</param>
 73    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the next up episodes.</returns>
 74    [HttpGet("NextUp")]
 75    [ProducesResponseType(StatusCodes.Status200OK)]
 76    public ActionResult<QueryResult<BaseItemDto>> GetNextUp(
 77        [FromQuery] Guid? userId,
 78        [FromQuery] int? startIndex,
 79        [FromQuery] int? limit,
 80        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 81        [FromQuery] Guid? seriesId,
 82        [FromQuery] Guid? parentId,
 83        [FromQuery] bool? enableImages,
 84        [FromQuery] int? imageTypeLimit,
 85        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 86        [FromQuery] bool? enableUserData,
 87        [FromQuery] DateTime? nextUpDateCutoff,
 88        [FromQuery] bool enableTotalRecordCount = true,
 89        [FromQuery] bool enableResumable = true,
 90        [FromQuery] bool enableRewatching = false)
 91    {
 092        var user = _userManager.GetUserById(RequestHelpers.GetUserId(User, userId));
 093        if (user is null)
 94        {
 095            return NotFound();
 96        }
 97
 098        var options = new DtoOptions { Fields = fields }
 099            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 100
 0101        var result = _tvSeriesManager.GetNextUp(
 0102            new NextUpQuery
 0103            {
 0104                Limit = limit,
 0105                ParentId = parentId,
 0106                SeriesId = seriesId,
 0107                StartIndex = startIndex,
 0108                User = user,
 0109                EnableTotalRecordCount = enableTotalRecordCount,
 0110                NextUpDateCutoff = nextUpDateCutoff ?? DateTime.MinValue,
 0111                EnableResumable = enableResumable,
 0112                EnableRewatching = enableRewatching
 0113            },
 0114            options);
 115
 0116        var returnItems = _dtoService.GetBaseItemDtos(result.Items, options, user);
 117
 0118        return new QueryResult<BaseItemDto>(
 0119            startIndex,
 0120            result.TotalRecordCount,
 0121            returnItems);
 122    }
 123
 124    /// <summary>
 125    /// Gets a list of upcoming episodes.
 126    /// </summary>
 127    /// <param name="userId">The user id of the user to get the upcoming episodes for.</param>
 128    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 129    /// <param name="limit">Optional. The maximum number of records to return.</param>
 130    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 131    /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use t
 132    /// <param name="enableImages">Optional. Include image information in output.</param>
 133    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 134    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 135    /// <param name="enableUserData">Optional. Include user data.</param>
 136    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the upcoming episodes.</returns>
 137    [HttpGet("Upcoming")]
 138    [ProducesResponseType(StatusCodes.Status200OK)]
 139    public ActionResult<QueryResult<BaseItemDto>> GetUpcomingEpisodes(
 140        [FromQuery] Guid? userId,
 141        [FromQuery] int? startIndex,
 142        [FromQuery] int? limit,
 143        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 144        [FromQuery] Guid? parentId,
 145        [FromQuery] bool? enableImages,
 146        [FromQuery] int? imageTypeLimit,
 147        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 148        [FromQuery] bool? enableUserData)
 149    {
 0150        userId = RequestHelpers.GetUserId(User, userId);
 0151        var user = userId.IsNullOrEmpty()
 0152            ? null
 0153            : _userManager.GetUserById(userId.Value);
 154
 0155        var minPremiereDate = DateTime.UtcNow.Date.AddDays(-1);
 156
 0157        var parentIdGuid = parentId ?? Guid.Empty;
 158
 0159        var options = new DtoOptions { Fields = fields }
 0160            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 161
 0162        var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
 0163        {
 0164            IncludeItemTypes = new[] { BaseItemKind.Episode },
 0165            OrderBy = new[] { (ItemSortBy.PremiereDate, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending)
 0166            MinPremiereDate = minPremiereDate,
 0167            StartIndex = startIndex,
 0168            Limit = limit,
 0169            ParentId = parentIdGuid,
 0170            Recursive = true,
 0171            DtoOptions = options
 0172        });
 173
 0174        var returnItems = _dtoService.GetBaseItemDtos(itemsResult, options, user);
 175
 0176        return new QueryResult<BaseItemDto>(
 0177            startIndex,
 0178            itemsResult.Count,
 0179            returnItems);
 180    }
 181
 182    /// <summary>
 183    /// Gets episodes for a tv season.
 184    /// </summary>
 185    /// <param name="seriesId">The series id.</param>
 186    /// <param name="userId">The user id.</param>
 187    /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows mul
 188    /// <param name="season">Optional filter by season number.</param>
 189    /// <param name="seasonId">Optional. Filter by season id.</param>
 190    /// <param name="isMissing">Optional. Filter by items that are missing episodes or not.</param>
 191    /// <param name="adjacentTo">Optional. Return items that are siblings of a supplied item.</param>
 192    /// <param name="startItemId">Optional. Skip through the list until a given item is found.</param>
 193    /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr
 194    /// <param name="limit">Optional. The maximum number of records to return.</param>
 195    /// <param name="enableImages">Optional, include image information in output.</param>
 196    /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
 197    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 198    /// <param name="enableUserData">Optional. Include user data.</param>
 199    /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar
 200    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the episodes on success or a <see cref="NotFoundResult"/>
 201    [HttpGet("{seriesId}/Episodes")]
 202    [ProducesResponseType(StatusCodes.Status200OK)]
 203    [ProducesResponseType(StatusCodes.Status404NotFound)]
 204    public ActionResult<QueryResult<BaseItemDto>> GetEpisodes(
 205        [FromRoute, Required] Guid seriesId,
 206        [FromQuery] Guid? userId,
 207        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 208        [FromQuery] int? season,
 209        [FromQuery] Guid? seasonId,
 210        [FromQuery] bool? isMissing,
 211        [FromQuery] Guid? adjacentTo,
 212        [FromQuery] Guid? startItemId,
 213        [FromQuery] int? startIndex,
 214        [FromQuery] int? limit,
 215        [FromQuery] bool? enableImages,
 216        [FromQuery] int? imageTypeLimit,
 217        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 218        [FromQuery] bool? enableUserData,
 219        [FromQuery] ItemSortBy? sortBy)
 220    {
 0221        userId = RequestHelpers.GetUserId(User, userId);
 0222        var user = userId.IsNullOrEmpty()
 0223            ? null
 0224            : _userManager.GetUserById(userId.Value);
 225
 226        List<BaseItem> episodes;
 227
 0228        var dtoOptions = new DtoOptions { Fields = fields }
 0229            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 0230        var shouldIncludeMissingEpisodes = (user is not null && user.DisplayMissingEpisodes) || User.GetIsApiKey();
 231
 0232        if (seasonId.HasValue) // Season id was supplied. Get episodes by season id.
 233        {
 0234            var item = _libraryManager.GetItemById<BaseItem>(seasonId.Value);
 0235            if (item is not Season seasonItem)
 236            {
 0237                return NotFound("No season exists with Id " + seasonId);
 238            }
 239
 0240            episodes = seasonItem.GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes);
 241        }
 0242        else if (season.HasValue) // Season number was supplied. Get episodes by season number
 243        {
 0244            var series = _libraryManager.GetItemById<Series>(seriesId);
 0245            if (series is null)
 246            {
 0247                return NotFound("Series not found");
 248            }
 249
 0250            var seasonItem = series
 0251                .GetSeasons(user, dtoOptions)
 0252                .FirstOrDefault(i => i.IndexNumber == season.Value);
 253
 0254            episodes = seasonItem is null ?
 0255                new List<BaseItem>()
 0256                : ((Season)seasonItem).GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes);
 257        }
 258        else // No season number or season id was supplied. Returning all episodes.
 259        {
 0260            if (_libraryManager.GetItemById<BaseItem>(seriesId) is not Series series)
 261            {
 0262                return NotFound("Series not found");
 263            }
 264
 0265            episodes = series.GetEpisodes(user, dtoOptions, shouldIncludeMissingEpisodes).ToList();
 266        }
 267
 268        // Filter after the fact in case the ui doesn't want them
 0269        if (isMissing.HasValue)
 270        {
 0271            var val = isMissing.Value;
 0272            episodes = episodes
 0273                .Where(i => ((Episode)i).IsMissingEpisode == val)
 0274                .ToList();
 275        }
 276
 0277        if (startItemId.HasValue)
 278        {
 0279            episodes = episodes
 0280                .SkipWhile(i => !startItemId.Value.Equals(i.Id))
 0281                .ToList();
 282        }
 283
 284        // This must be the last filter
 0285        if (!adjacentTo.IsNullOrEmpty())
 286        {
 0287            episodes = UserViewBuilder.FilterForAdjacency(episodes, adjacentTo.Value).ToList();
 288        }
 289
 0290        if (sortBy == ItemSortBy.Random)
 291        {
 0292            episodes.Shuffle();
 293        }
 294
 0295        var returnItems = episodes;
 296
 0297        if (startIndex.HasValue || limit.HasValue)
 298        {
 0299            returnItems = ApplyPaging(episodes, startIndex, limit).ToList();
 300        }
 301
 0302        var dtos = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user);
 303
 0304        return new QueryResult<BaseItemDto>(
 0305            startIndex,
 0306            episodes.Count,
 0307            dtos);
 308    }
 309
 310    /// <summary>
 311    /// Gets seasons for a tv series.
 312    /// </summary>
 313    /// <param name="seriesId">The series id.</param>
 314    /// <param name="userId">The user id.</param>
 315    /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows mul
 316    /// <param name="isSpecialSeason">Optional. Filter by special season.</param>
 317    /// <param name="isMissing">Optional. Filter by items that are missing episodes or not.</param>
 318    /// <param name="adjacentTo">Optional. Return items that are siblings of a supplied item.</param>
 319    /// <param name="enableImages">Optional. Include image information in output.</param>
 320    /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
 321    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 322    /// <param name="enableUserData">Optional. Include user data.</param>
 323    /// <returns>A <see cref="QueryResult{BaseItemDto}"/> on success or a <see cref="NotFoundResult"/> if the series was
 324    [HttpGet("{seriesId}/Seasons")]
 325    [ProducesResponseType(StatusCodes.Status200OK)]
 326    [ProducesResponseType(StatusCodes.Status404NotFound)]
 327    public ActionResult<QueryResult<BaseItemDto>> GetSeasons(
 328        [FromRoute, Required] Guid seriesId,
 329        [FromQuery] Guid? userId,
 330        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
 331        [FromQuery] bool? isSpecialSeason,
 332        [FromQuery] bool? isMissing,
 333        [FromQuery] Guid? adjacentTo,
 334        [FromQuery] bool? enableImages,
 335        [FromQuery] int? imageTypeLimit,
 336        [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
 337        [FromQuery] bool? enableUserData)
 338    {
 0339        userId = RequestHelpers.GetUserId(User, userId);
 0340        var user = userId.IsNullOrEmpty()
 0341            ? null
 0342            : _userManager.GetUserById(userId.Value);
 0343        var item = _libraryManager.GetItemById<Series>(seriesId, user);
 0344        if (item is null)
 345        {
 0346            return NotFound();
 347        }
 348
 0349        var seasons = item.GetItemList(new InternalItemsQuery(user)
 0350        {
 0351            IsMissing = isMissing,
 0352            IsSpecialSeason = isSpecialSeason,
 0353            AdjacentTo = adjacentTo
 0354        });
 355
 0356        var dtoOptions = new DtoOptions { Fields = fields }
 0357            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 358
 0359        var returnItems = _dtoService.GetBaseItemDtos(seasons, dtoOptions, user);
 360
 0361        return new QueryResult<BaseItemDto>(returnItems);
 362    }
 363
 364    /// <summary>
 365    /// Applies the paging.
 366    /// </summary>
 367    /// <param name="items">The items.</param>
 368    /// <param name="startIndex">The start index.</param>
 369    /// <param name="limit">The limit.</param>
 370    /// <returns>IEnumerable{BaseItem}.</returns>
 371    private IEnumerable<BaseItem> ApplyPaging(IEnumerable<BaseItem> items, int? startIndex, int? limit)
 372    {
 373        // Start at
 0374        if (startIndex.HasValue)
 375        {
 0376            items = items.Skip(startIndex.Value);
 377        }
 378
 379        // Return limit
 0380        if (limit.HasValue)
 381        {
 0382            items = items.Take(limit.Value);
 383        }
 384
 0385        return items;
 386    }
 387}

Methods/Properties

.ctor(MediaBrowser.Controller.Library.IUserManager,MediaBrowser.Controller.Library.ILibraryManager,MediaBrowser.Controller.Dto.IDtoService,MediaBrowser.Controller.TV.ITVSeriesManager)
GetNextUp(System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>,System.Nullable`1<System.DateTime>,System.Boolean,System.Boolean,System.Boolean)
GetUpcomingEpisodes(System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>)
GetEpisodes(System.Guid,System.Nullable`1<System.Guid>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Int32>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>,System.Nullable`1<Jellyfin.Data.Enums.ItemSortBy>)
GetSeasons(System.Guid,System.Nullable`1<System.Guid>,MediaBrowser.Model.Querying.ItemFields[],System.Nullable`1<System.Boolean>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Guid>,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>,MediaBrowser.Model.Entities.ImageType[],System.Nullable`1<System.Boolean>)
ApplyPaging(System.Collections.Generic.IEnumerable`1<MediaBrowser.Controller.Entities.BaseItem>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>)