using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; using Jellyfin.Data.Entities; using Jellyfin.Data.Enums; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Querying; using MediaBrowser.Model.Users; namespace MediaBrowser.Controller.Entities.TV { /// /// Class Season /// public class Season : Folder, IHasSeries, IHasLookupInfo { [JsonIgnore] public override bool SupportsAddingToPlaylist => true; [JsonIgnore] public override bool IsPreSorted => true; [JsonIgnore] public override bool SupportsDateLastMediaAdded => false; [JsonIgnore] public override bool SupportsPeople => true; [JsonIgnore] public override bool SupportsInheritedParentImages => true; [JsonIgnore] public override Guid DisplayParentId => SeriesId; public override double GetDefaultPrimaryImageAspectRatio() { double value = 2; value /= 3; return value; } public string FindSeriesSortName() { var series = Series; return series == null ? SeriesName : series.SortName; } public override List GetUserDataKeys() { var list = base.GetUserDataKeys(); var series = Series; if (series != null) { list.InsertRange(0, series.GetUserDataKeys().Select(i => i + (IndexNumber ?? 0).ToString("000"))); } return list; } public override int GetChildCount(User user) { var result = GetChildren(user, true).Count; return result; } /// /// This Episode's Series Instance /// /// The series. [JsonIgnore] public Series Series { get { var seriesId = SeriesId; if (seriesId == Guid.Empty) { seriesId = FindSeriesId(); } return seriesId == Guid.Empty ? null : (LibraryManager.GetItemById(seriesId) as Series); } } [JsonIgnore] public string SeriesPath { get { var series = Series; if (series != null) { return series.Path; } return System.IO.Path.GetDirectoryName(Path); } } public override string CreatePresentationUniqueKey() { if (IndexNumber.HasValue) { var series = Series; if (series != null) { return series.PresentationUniqueKey + "-" + (IndexNumber ?? 0).ToString("000"); } } return base.CreatePresentationUniqueKey(); } /// /// Creates the name of the sort. /// /// System.String. protected override string CreateSortName() { return IndexNumber != null ? IndexNumber.Value.ToString("0000") : Name; } protected override QueryResult GetItemsInternal(InternalItemsQuery query) { if (query.User == null) { return base.GetItemsInternal(query); } var user = query.User; Func filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager); var items = GetEpisodes(user, query.DtoOptions).Where(filter); return PostFilterAndSort(items, query, false); } /// /// Gets the episodes. /// public List GetEpisodes(User user, DtoOptions options) { return GetEpisodes(Series, user, options); } public List GetEpisodes(Series series, User user, DtoOptions options) { return GetEpisodes(series, user, null, options); } public List GetEpisodes(Series series, User user, IEnumerable allSeriesEpisodes, DtoOptions options) { return series.GetSeasonEpisodes(this, user, allSeriesEpisodes, options); } public List GetEpisodes() { return Series.GetSeasonEpisodes(this, null, null, new DtoOptions(true)); } public override List GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query) { return GetEpisodes(user, new DtoOptions(true)); } protected override bool GetBlockUnratedValue(User config) { // Don't block. Let either the entire series rating or episode rating determine it return false; } public override UnratedItem GetBlockUnratedType() { return UnratedItem.Series; } [JsonIgnore] public string SeriesPresentationUniqueKey { get; set; } [JsonIgnore] public string SeriesName { get; set; } [JsonIgnore] public Guid SeriesId { get; set; } public string FindSeriesPresentationUniqueKey() { var series = Series; return series == null ? null : series.PresentationUniqueKey; } public string FindSeriesName() { var series = Series; return series == null ? SeriesName : series.Name; } public Guid FindSeriesId() { var series = FindParent(); return series?.Id ?? Guid.Empty; } /// /// Gets the lookup information. /// /// SeasonInfo. public SeasonInfo GetLookupInfo() { var id = GetItemLookupInfo(); var series = Series; if (series != null) { id.SeriesProviderIds = series.ProviderIds; } return id; } /// /// This is called before any metadata refresh and returns true or false indicating if changes were made /// /// true if XXXX, false otherwise. public override bool BeforeMetadataRefresh(bool replaceAllMetdata) { var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata); if (!IndexNumber.HasValue && !string.IsNullOrEmpty(Path)) { IndexNumber ??= LibraryManager.GetSeasonNumberFromPath(Path); // If a change was made record it if (IndexNumber.HasValue) { hasChanges = true; } } return hasChanges; } } }