mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-22 01:54:42 +01:00
Added a completely separate DTOBaseItem to remove the ApiBaseItemWrapper mess and shrink json output size.
This commit is contained in:
parent
f32f000298
commit
7835d690a1
@@ -1,11 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using MediaBrowser.Model.Users;
|
||||
using System.Linq;
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
public abstract class BaseItem : BaseEntity
|
||||
public abstract class BaseItem : BaseEntity, IHasProviderIds
|
||||
{
|
||||
public string SortName { get; set; }
|
||||
|
||||
@@ -16,25 +15,25 @@ namespace MediaBrowser.Model.Entities
|
||||
|
||||
public string Path { get; set; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public Folder Parent { get; set; }
|
||||
|
||||
public string LogoImagePath { get; set; }
|
||||
|
||||
public string ArtImagePath { get; set; }
|
||||
|
||||
public string ThumbnailImagePath { get; set; }
|
||||
|
||||
public string BannerImagePath { get; set; }
|
||||
|
||||
public IEnumerable<string> BackdropImagePaths { get; set; }
|
||||
|
||||
public string OfficialRating { get; set; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public string CustomRating { get; set; }
|
||||
|
||||
public string Overview { get; set; }
|
||||
public string Tagline { get; set; }
|
||||
public IEnumerable<string> Taglines { get; set; }
|
||||
|
||||
[IgnoreDataMember]
|
||||
public IEnumerable<PersonInfo> People { get; set; }
|
||||
|
||||
public IEnumerable<string> Studios { get; set; }
|
||||
@@ -44,7 +43,7 @@ namespace MediaBrowser.Model.Entities
|
||||
public string DisplayMediaType { get; set; }
|
||||
|
||||
public float? UserRating { get; set; }
|
||||
public int? RunTimeInMilliseconds { get; set; }
|
||||
public long? RunTimeTicks { get; set; }
|
||||
|
||||
public string AspectRatio { get; set; }
|
||||
public int? ProductionYear { get; set; }
|
||||
@@ -61,46 +60,26 @@ namespace MediaBrowser.Model.Entities
|
||||
|
||||
public Dictionary<string, string> ProviderIds { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a provider id
|
||||
/// </summary>
|
||||
public string GetProviderId(MetadataProviders provider)
|
||||
{
|
||||
return GetProviderId(provider.ToString());
|
||||
}
|
||||
public Dictionary<Guid, UserItemData> UserData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a provider id
|
||||
/// </summary>
|
||||
public string GetProviderId(string name)
|
||||
public UserItemData GetUserData(User user)
|
||||
{
|
||||
if (ProviderIds == null)
|
||||
if (UserData == null || !UserData.ContainsKey(user.Id))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ProviderIds[name];
|
||||
return UserData[user.Id];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a provider id
|
||||
/// </summary>
|
||||
public void SetProviderId(string name, string value)
|
||||
public void AddUserData(User user, UserItemData data)
|
||||
{
|
||||
if (ProviderIds == null)
|
||||
if (UserData == null)
|
||||
{
|
||||
ProviderIds = new Dictionary<string, string>();
|
||||
UserData = new Dictionary<Guid, UserItemData>();
|
||||
}
|
||||
|
||||
ProviderIds[name] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a provider id
|
||||
/// </summary>
|
||||
public void SetProviderId(MetadataProviders provider, string value)
|
||||
{
|
||||
SetProviderId(provider.ToString(), value);
|
||||
UserData[user.Id] = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,5 +89,23 @@ namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds an item by ID, recursively
|
||||
/// </summary>
|
||||
public virtual BaseItem FindItemById(Guid id)
|
||||
{
|
||||
if (Id == id)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (LocalTrailers != null)
|
||||
{
|
||||
return LocalTrailers.FirstOrDefault(i => i.Id == id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using MediaBrowser.Model.Users;
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
@@ -18,7 +16,6 @@ namespace MediaBrowser.Model.Entities
|
||||
}
|
||||
}
|
||||
|
||||
[IgnoreDataMember]
|
||||
public BaseItem[] Children { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -50,6 +47,23 @@ namespace MediaBrowser.Model.Entities
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Since it can be slow to make all of these calculations at once, this method will provide a way to get them all back together
|
||||
/// </summary>
|
||||
public ItemSpecialCounts GetSpecialCounts(User user)
|
||||
{
|
||||
ItemSpecialCounts counts = new ItemSpecialCounts();
|
||||
|
||||
IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);
|
||||
|
||||
counts.RecentlyAddedItemCount = GetRecentlyAddedItems(recursiveChildren, user).Count();
|
||||
counts.RecentlyAddedUnPlayedItemCount = GetRecentlyAddedUnplayedItems(recursiveChildren, user).Count();
|
||||
counts.InProgressItemCount = GetInProgressItems(recursiveChildren, user).Count();
|
||||
counts.WatchedPercentage = GetWatchedPercentage(recursiveChildren, user);
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds all recursive items within a top-level parent that contain the given genre and are allowed for the current user
|
||||
/// </summary>
|
||||
@@ -77,21 +91,30 @@ namespace MediaBrowser.Model.Entities
|
||||
/// <summary>
|
||||
/// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
|
||||
/// </summary>
|
||||
/// <param name="personType">Specify this to limit results to a specific PersonType</param>
|
||||
public IEnumerable<BaseItem> GetItemsWithPerson(string person, PersonType? personType, User user)
|
||||
public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
|
||||
{
|
||||
return GetParentalAllowedRecursiveChildren(user).Where(c =>
|
||||
{
|
||||
if (c.People != null)
|
||||
{
|
||||
if (personType.HasValue)
|
||||
{
|
||||
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase) && p.PersonType == personType.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
|
||||
/// </summary>
|
||||
/// <param name="personType">Specify this to limit results to a specific PersonType</param>
|
||||
public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
|
||||
{
|
||||
return GetParentalAllowedRecursiveChildren(user).Where(c =>
|
||||
{
|
||||
if (c.People != null)
|
||||
{
|
||||
return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase) && p.Type == personType);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -103,9 +126,7 @@ namespace MediaBrowser.Model.Entities
|
||||
/// </summary>
|
||||
public IEnumerable<BaseItem> GetRecentlyAddedItems(User user)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
return GetParentalAllowedRecursiveChildren(user).Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < user.RecentItemDays);
|
||||
return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -113,12 +134,7 @@ namespace MediaBrowser.Model.Entities
|
||||
/// </summary>
|
||||
public IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(User user)
|
||||
{
|
||||
return GetRecentlyAddedItems(user).Where(i =>
|
||||
{
|
||||
var userdata = user.GetItemData(i.Id);
|
||||
|
||||
return userdata == null || userdata.PlayCount == 0;
|
||||
});
|
||||
return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -126,45 +142,95 @@ namespace MediaBrowser.Model.Entities
|
||||
/// </summary>
|
||||
public IEnumerable<BaseItem> GetInProgressItems(User user)
|
||||
{
|
||||
return GetParentalAllowedRecursiveChildren(user).Where(i =>
|
||||
return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
|
||||
}
|
||||
|
||||
private static IEnumerable<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
return itemSet.Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < user.RecentItemDays);
|
||||
}
|
||||
|
||||
private static IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
|
||||
{
|
||||
return GetRecentlyAddedItems(itemSet, user).Where(i =>
|
||||
{
|
||||
var userdata = i.GetUserData(user);
|
||||
|
||||
return userdata == null || userdata.PlayCount == 0;
|
||||
});
|
||||
}
|
||||
|
||||
private static IEnumerable<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
|
||||
{
|
||||
return itemSet.Where(i =>
|
||||
{
|
||||
if (i is Folder)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var userdata = user.GetItemData(i.Id);
|
||||
var userdata = i.GetUserData(user);
|
||||
|
||||
return userdata != null && userdata.PlaybackPosition.Ticks > 0;
|
||||
return userdata != null && userdata.PlaybackPositionTicks > 0;
|
||||
});
|
||||
}
|
||||
|
||||
private static decimal GetWatchedPercentage(IEnumerable<BaseItem> itemSet, User user)
|
||||
{
|
||||
itemSet = itemSet.Where(i => !(i is Folder));
|
||||
|
||||
if (!itemSet.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
decimal totalPercent = 0;
|
||||
|
||||
foreach (BaseItem item in itemSet)
|
||||
{
|
||||
UserItemData data = item.GetUserData(user);
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data.PlayCount > 0)
|
||||
{
|
||||
totalPercent += 100;
|
||||
}
|
||||
else if (data.PlaybackPositionTicks > 0 && item.RunTimeTicks.HasValue)
|
||||
{
|
||||
decimal itemPercent = data.PlaybackPositionTicks;
|
||||
itemPercent /= item.RunTimeTicks.Value;
|
||||
totalPercent += itemPercent;
|
||||
}
|
||||
}
|
||||
|
||||
return totalPercent / itemSet.Count();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds an item by ID, recursively
|
||||
/// </summary>
|
||||
public BaseItem FindById(Guid id)
|
||||
public override BaseItem FindItemById(Guid id)
|
||||
{
|
||||
if (Id == id)
|
||||
var result = base.FindItemById(id);
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
return this;
|
||||
return result;
|
||||
}
|
||||
|
||||
foreach (BaseItem item in Children)
|
||||
{
|
||||
var folder = item as Folder;
|
||||
result = item.FindItemById(id);
|
||||
|
||||
if (folder != null)
|
||||
if (result != null)
|
||||
{
|
||||
var foundItem = folder.FindById(id);
|
||||
|
||||
if (foundItem != null)
|
||||
{
|
||||
return foundItem;
|
||||
}
|
||||
}
|
||||
else if (item.Id == id)
|
||||
{
|
||||
return item;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
57
MediaBrowser.Model/Entities/IHasProviderIds.cs
Normal file
57
MediaBrowser.Model/Entities/IHasProviderIds.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Since BaseItem and DTOBaseItem both have ProviderIds, this interface helps avoid code repition using extension methods
|
||||
/// </summary>
|
||||
public interface IHasProviderIds
|
||||
{
|
||||
Dictionary<string, string> ProviderIds { get; set; }
|
||||
}
|
||||
|
||||
public static class IProviderIdsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a provider id
|
||||
/// </summary>
|
||||
public static string GetProviderId(this IHasProviderIds instance, MetadataProviders provider)
|
||||
{
|
||||
return instance.GetProviderId(provider.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a provider id
|
||||
/// </summary>
|
||||
public static string GetProviderId(this IHasProviderIds instance, string name)
|
||||
{
|
||||
if (instance.ProviderIds == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return instance.ProviderIds[name];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a provider id
|
||||
/// </summary>
|
||||
public static void SetProviderId(this IHasProviderIds instance, string name, string value)
|
||||
{
|
||||
if (instance.ProviderIds == null)
|
||||
{
|
||||
instance.ProviderIds = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
instance.ProviderIds[name] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a provider id
|
||||
/// </summary>
|
||||
public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
|
||||
{
|
||||
instance.SetProviderId(provider.ToString(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
MediaBrowser.Model/Entities/ItemSpecialCounts.cs
Normal file
14
MediaBrowser.Model/Entities/ItemSpecialCounts.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Since it can be slow to collect this data. This class helps provide a way to calculate them all at once.
|
||||
/// </summary>
|
||||
public class ItemSpecialCounts
|
||||
{
|
||||
public int RecentlyAddedItemCount { get; set; }
|
||||
public int RecentlyAddedUnPlayedItemCount { get; set; }
|
||||
public int InProgressItemCount { get; set; }
|
||||
public decimal WatchedPercentage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -15,20 +15,11 @@ namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Overview { get; set; }
|
||||
public PersonType PersonType { get; set; }
|
||||
public string Type { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
public enum PersonType
|
||||
{
|
||||
Other,
|
||||
Actor,
|
||||
Director,
|
||||
Writer,
|
||||
Producer
|
||||
}
|
||||
}
|
||||
|
||||
15
MediaBrowser.Model/Entities/User.cs
Normal file
15
MediaBrowser.Model/Entities/User.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
public class User : BaseEntity
|
||||
{
|
||||
public string MaxParentalRating { get; set; }
|
||||
|
||||
public int RecentItemDays { get; set; }
|
||||
|
||||
public User()
|
||||
{
|
||||
RecentItemDays = 14;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
MediaBrowser.Model/Entities/UserItemData.cs
Normal file
20
MediaBrowser.Model/Entities/UserItemData.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
public class UserItemData
|
||||
{
|
||||
public UserItemRating Rating { get; set; }
|
||||
|
||||
public long PlaybackPositionTicks { get; set; }
|
||||
|
||||
public int PlayCount { get; set; }
|
||||
}
|
||||
|
||||
public enum UserItemRating
|
||||
{
|
||||
Likes,
|
||||
Dislikes,
|
||||
Favorite
|
||||
}
|
||||
}
|
||||
@@ -6,28 +6,27 @@ namespace MediaBrowser.Model.Entities
|
||||
{
|
||||
public VideoType VideoType { get; set; }
|
||||
|
||||
private IEnumerable<string> _Subtitles = new string[] { };
|
||||
public IEnumerable<string> Subtitles { get { return _Subtitles; } set { _Subtitles = value; } }
|
||||
|
||||
private IEnumerable<AudioStream> _AudioStreams = new AudioStream[] { };
|
||||
public IEnumerable<AudioStream> AudioStreams { get { return _AudioStreams; } set { _AudioStreams = value; } }
|
||||
public IEnumerable<string> Subtitles { get; set; }
|
||||
public IEnumerable<AudioStream> AudioStreams { get; set; }
|
||||
|
||||
public int Height { get; set; }
|
||||
public int Width { get; set; }
|
||||
public string ScanType { get; set; }
|
||||
public string FrameRate { get; set; }
|
||||
public int VideoBitRate { get; set; }
|
||||
public string VideoCodec { get; set; }
|
||||
public int BitRate { get; set; }
|
||||
public string Codec { get; set; }
|
||||
}
|
||||
|
||||
public class AudioStream
|
||||
{
|
||||
public string AudioFormat { get; set; }
|
||||
public string AudioProfile { get; set; }
|
||||
public string Format { get; set; }
|
||||
public string Profile { get; set; }
|
||||
public string Language { get; set; }
|
||||
public int BitRate { get; set; }
|
||||
public int Channels { get; set; }
|
||||
public int SampleRate { get; set; }
|
||||
public bool IsDefault { get; set; }
|
||||
public bool IsForced { get; set; }
|
||||
}
|
||||
|
||||
public enum VideoType
|
||||
|
||||
Reference in New Issue
Block a user