jellyfin-server/Jellyfin.Api/Extensions/DtoExtensions.cs

115 lines
3.9 KiB
C#
Raw Normal View History

2020-06-18 16:42:48 +00:00
using System;
using System.Collections.Generic;
using System.Security.Claims;
using Jellyfin.Extensions;
2020-06-18 16:42:48 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Extensions;
/// <summary>
/// Dto Extensions.
/// </summary>
public static class DtoExtensions
2020-06-18 16:42:48 +00:00
{
/// <summary>
2023-01-31 11:18:10 +00:00
/// Add additional fields depending on client.
2020-06-18 16:42:48 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <remarks>
/// Use in place of GetDtoOptions.
/// Legacy order: 2.
/// </remarks>
/// <param name="dtoOptions">DtoOptions object.</param>
/// <param name="user">Current claims principal.</param>
/// <returns>Modified DtoOptions object.</returns>
internal static DtoOptions AddClientFields(
this DtoOptions dtoOptions, ClaimsPrincipal user)
2020-06-18 16:42:48 +00:00
{
2023-01-31 11:18:10 +00:00
dtoOptions.Fields ??= Array.Empty<ItemFields>();
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
string? client = user.GetClient();
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
// No client in claim
if (string.IsNullOrEmpty(client))
{
return dtoOptions;
}
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
if (!dtoOptions.ContainsField(ItemFields.RecursiveItemCount))
{
if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1)
2020-06-18 16:42:48 +00:00
{
2023-01-31 11:18:10 +00:00
int oldLen = dtoOptions.Fields.Count;
var arr = new ItemFields[oldLen + 1];
dtoOptions.Fields.CopyTo(arr, 0);
arr[oldLen] = ItemFields.RecursiveItemCount;
dtoOptions.Fields = arr;
2020-06-18 16:42:48 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
if (!dtoOptions.ContainsField(ItemFields.ChildCount))
{
if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("samsung", StringComparison.OrdinalIgnoreCase) != -1 ||
client.IndexOf("androidtv", StringComparison.OrdinalIgnoreCase) != -1)
2020-06-18 16:42:48 +00:00
{
2023-01-31 11:18:10 +00:00
int oldLen = dtoOptions.Fields.Count;
var arr = new ItemFields[oldLen + 1];
dtoOptions.Fields.CopyTo(arr, 0);
arr[oldLen] = ItemFields.ChildCount;
dtoOptions.Fields = arr;
2020-06-18 16:42:48 +00:00
}
}
2023-01-31 11:18:10 +00:00
return dtoOptions;
}
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Add additional DtoOptions.
/// </summary>
/// <remarks>
/// Converted from IHasDtoOptions.
/// Legacy order: 3.
/// </remarks>
/// <param name="dtoOptions">DtoOptions object.</param>
/// <param name="enableImages">Enable images.</param>
/// <param name="enableUserData">Enable user data.</param>
/// <param name="imageTypeLimit">Image type limit.</param>
/// <param name="enableImageTypes">Enable image types.</param>
/// <returns>Modified DtoOptions object.</returns>
internal static DtoOptions AddAdditionalDtoOptions(
this DtoOptions dtoOptions,
bool? enableImages,
bool? enableUserData,
int? imageTypeLimit,
IReadOnlyList<ImageType> enableImageTypes)
{
dtoOptions.EnableImages = enableImages ?? true;
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
if (imageTypeLimit.HasValue)
{
dtoOptions.ImageTypeLimit = imageTypeLimit.Value;
}
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
if (enableUserData.HasValue)
{
dtoOptions.EnableUserData = enableUserData.Value;
}
2020-06-18 16:42:48 +00:00
2023-01-31 11:18:10 +00:00
if (enableImageTypes.Count != 0)
{
dtoOptions.ImageTypes = enableImageTypes;
2020-06-18 16:42:48 +00:00
}
2023-01-31 11:18:10 +00:00
return dtoOptions;
2020-06-18 16:42:48 +00:00
}
}