jellyfin/Jellyfin.Api/Helpers/RequestHelpers.cs

141 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-04-23 10:54:28 -04:00
using System.Linq;
2021-04-10 16:57:25 -04: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 12:54:25 -04:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Dto;
2020-06-24 12:54:25 -04:00
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
2020-04-23 10:54:28 -04:00
2020-05-24 14:33:16 -04:00
namespace Jellyfin.Api.Helpers
2020-04-23 10:54:28 -04:00
{
/// <summary>
/// Request Extensions.
/// </summary>
2020-05-24 14:33:16 -04:00
public static class RequestHelpers
2020-04-23 10:54:28 -04: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 05:43:05 -05:00
public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
2020-04-23 10:54:28 -04:00
{
if (sortBy.Count == 0)
2020-04-23 10:54:28 -04:00
{
2021-12-24 16:18:24 -05:00
return Array.Empty<(string, SortOrder)>();
2020-04-23 10:54:28 -04:00
}
2021-01-24 05:43:05 -05: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 10:54:28 -04:00
{
2021-01-24 05:43:05 -05:00
result[i] = (sortBy[i], requestedSortOrder[i]);
}
2020-04-23 10:54:28 -04:00
2021-01-24 11:55:25 -05: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 05:43:05 -05:00
for (; i < sortBy.Count; i++)
{
2021-01-24 11:55:25 -05:00
result[i] = (sortBy[i], order);
2020-04-23 10:54:28 -04: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-20 23:56:59 -04:00
internal static async Task<bool> AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
{
2021-05-20 23:56:59 -04: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 16:57:25 -04:00
internal static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
2020-06-12 12:54:25 -04:00
{
2021-05-20 23:56:59 -04:00
var authorization = await authContext.GetAuthorizationInfo(request).ConfigureAwait(false);
var user = authorization.User;
2021-04-10 16:57:25 -04:00
var session = await sessionManager.LogSessionActivity(
authorization.Client,
authorization.Version,
authorization.DeviceId,
authorization.Device,
request.HttpContext.GetNormalizedRemoteIp().ToString(),
2021-04-10 16:57:25 -04:00
user).ConfigureAwait(false);
2020-06-12 12:54:25 -04:00
if (session == null)
{
throw new ArgumentException("Session not found.");
}
return session;
}
2020-06-20 18:03:19 -04:00
2021-04-10 16:57:25 -04: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(
2022-01-07 05:48:59 -05:00
QueryResult<(BaseItem Item, ItemCounts 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;
});
2022-01-20 10:46:17 -05:00
return new QueryResult<BaseItemDto>(
result.StartIndex,
result.TotalRecordCount,
dtos.ToArray());
}
2020-04-23 10:54:28 -04:00
}
}