jellyfin-server/Jellyfin.Api/Helpers/RequestHelpers.cs

158 lines
5.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-04-23 14:54:28 +00:00
using System.Linq;
2021-04-10 20:57:25 +00:00
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2020-06-12 16:54:25 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Dto;
2020-06-24 16:54:25 +00:00
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
2020-04-23 14:54:28 +00:00
2020-05-24 18:33:16 +00:00
namespace Jellyfin.Api.Helpers
2020-04-23 14:54:28 +00:00
{
/// <summary>
/// Request Extensions.
/// </summary>
2020-05-24 18:33:16 +00:00
public static class RequestHelpers
2020-04-23 14:54:28 +00:00
{
/// <summary>
/// Get Order By.
/// </summary>
/// <param name="sortBy">Sort By. Comma delimited string.</param>
/// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
/// <returns>Order By.</returns>
2021-01-24 10:43:05 +00:00
public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
2020-04-23 14:54:28 +00:00
{
if (sortBy.Count == 0)
2020-04-23 14:54:28 +00:00
{
return Array.Empty<ValueTuple<string, SortOrder>>();
}
2021-01-24 10:43:05 +00:00
var result = new (string, SortOrder)[sortBy.Count];
var i = 0;
// Add elements which have a SortOrder specified
for (; i < requestedSortOrder.Count; i++)
2020-04-23 14:54:28 +00:00
{
2021-01-24 10:43:05 +00:00
result[i] = (sortBy[i], requestedSortOrder[i]);
}
2020-04-23 14:54:28 +00:00
2021-01-24 16:55:25 +00:00
// Add remaining elements with the first specified SortOrder
// or the default one if no SortOrders are specified
var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
2021-01-24 10:43:05 +00:00
for (; i < sortBy.Count; i++)
{
2021-01-24 16:55:25 +00:00
result[i] = (sortBy[i], order);
2020-04-23 14:54:28 +00:00
}
return result;
}
/// <summary>
/// Checks if the user can update an entry.
/// </summary>
/// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
/// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
/// <param name="userId">The user id.</param>
/// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
/// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
2021-05-21 03:56:59 +00:00
internal static async Task<bool> AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
{
2021-05-21 03:56:59 +00:00
var auth = await authContext.GetAuthorizationInfo(requestContext).ConfigureAwait(false);
var authenticatedUser = auth.User;
// If they're going to update the record of another user, they must be an administrator
if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
|| (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
{
return false;
}
return true;
}
2021-04-10 20:57:25 +00:00
internal static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
2020-06-12 16:54:25 +00:00
{
2021-05-21 03:56:59 +00:00
var authorization = await authContext.GetAuthorizationInfo(request).ConfigureAwait(false);
var user = authorization.User;
2021-04-10 20:57:25 +00:00
var session = await sessionManager.LogSessionActivity(
authorization.Client,
authorization.Version,
authorization.DeviceId,
authorization.Device,
request.HttpContext.GetNormalizedRemoteIp().ToString(),
2021-04-10 20:57:25 +00:00
user).ConfigureAwait(false);
2020-06-12 16:54:25 +00:00
if (session == null)
{
throw new ArgumentException("Session not found.");
}
return session;
}
2020-06-20 22:03:19 +00:00
2021-04-10 20:57:25 +00:00
internal static async Task<string> GetSessionId(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
{
var session = await GetSession(sessionManager, authContext, request).ConfigureAwait(false);
return session.Id;
}
internal static QueryResult<BaseItemDto> CreateQueryResult(
QueryResult<(BaseItem, ItemCounts)> result,
DtoOptions dtoOptions,
IDtoService dtoService,
bool includeItemTypes,
User? user)
{
var dtos = result.Items.Select(i =>
{
var (baseItem, counts) = i;
var dto = dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
if (includeItemTypes)
{
dto.ChildCount = counts.ItemCount;
dto.ProgramCount = counts.ProgramCount;
dto.SeriesCount = counts.SeriesCount;
dto.EpisodeCount = counts.EpisodeCount;
dto.MovieCount = counts.MovieCount;
dto.TrailerCount = counts.TrailerCount;
dto.AlbumCount = counts.AlbumCount;
dto.SongCount = counts.SongCount;
dto.ArtistCount = counts.ArtistCount;
}
return dto;
});
return new QueryResult<BaseItemDto>
{
Items = dtos.ToArray(),
TotalRecordCount = result.TotalRecordCount
};
}
2021-02-11 00:09:23 +00:00
internal static string[] GetItemTypeStrings(IReadOnlyList<BaseItemKind> itemKinds)
{
if (itemKinds.Count == 0)
{
return Array.Empty<string>();
}
var itemTypes = new string[itemKinds.Count];
for (var i = 0; i < itemKinds.Count; i++)
{
itemTypes[i] = itemKinds[i].ToString();
}
return itemTypes;
}
2020-04-23 14:54:28 +00:00
}
}