Skip to content

Commit

Permalink
Move userId in API from route to optional query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Feb 27, 2024
1 parent 4f0f364 commit ccf150e
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions Jellyfin.Api/Controllers/UserViewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Api.Models.UserViewDtos;
using Jellyfin.Data.Enums;
Expand Down Expand Up @@ -59,19 +60,17 @@ public UserViewsController(
/// <param name="includeHidden">Whether or not to include hidden content.</param>
/// <response code="200">User views returned.</response>
/// <returns>An <see cref="OkResult"/> containing the user views.</returns>
[HttpGet("Users/{userId}/Views")]
[HttpGet("UserViews")]
[ProducesResponseType(StatusCodes.Status200OK)]
public QueryResult<BaseItemDto> GetUserViews(
[FromRoute, Required] Guid userId,
[FromQuery] Guid? userId,
[FromQuery] bool? includeExternalContent,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] CollectionType?[] presetViews,
[FromQuery] bool includeHidden = false)
{
var query = new UserViewQuery
{
UserId = userId,
IncludeHidden = includeHidden
};
userId = RequestHelpers.GetUserId(User, userId);

var query = new UserViewQuery { UserId = userId.Value, IncludeHidden = includeHidden };

if (includeExternalContent.HasValue)
{
Expand All @@ -92,14 +91,33 @@ public QueryResult<BaseItemDto> GetUserViews(
fields.Add(ItemFields.DisplayPreferencesId);
dtoOptions.Fields = fields.ToArray();

var user = _userManager.GetUserById(userId);
var user = _userManager.GetUserById(userId.Value);

var dtos = folders.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user))
.ToArray();

return new QueryResult<BaseItemDto>(dtos);
}

/// <summary>
/// Get user views.
/// </summary>
/// <param name="userId">User id.</param>
/// <param name="includeExternalContent">Whether or not to include external views such as channels or live tv.</param>
/// <param name="presetViews">Preset views.</param>
/// <param name="includeHidden">Whether or not to include hidden content.</param>
/// <response code="200">User views returned.</response>
/// <returns>An <see cref="OkResult"/> containing the user views.</returns>
[HttpGet("Users/{userId}/Views")]
[ProducesResponseType(StatusCodes.Status200OK)]
[Obsolete("Kept for backwards compatibility")]
[ApiExplorerSettings(IgnoreApi = true)]
public QueryResult<BaseItemDto> GetUserViews2(
[FromRoute, Required] Guid userId,
[FromQuery] bool? includeExternalContent,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] CollectionType?[] presetViews,
[FromQuery] bool includeHidden = false) => GetUserViews(userId, includeExternalContent, presetViews, includeHidden);

/// <summary>
/// Get user view grouping options.
/// </summary>
Expand All @@ -110,12 +128,13 @@ public QueryResult<BaseItemDto> GetUserViews(
/// An <see cref="OkResult"/> containing the user view grouping options
/// or a <see cref="NotFoundResult"/> if user not found.
/// </returns>
[HttpGet("Users/{userId}/GroupingOptions")]
[HttpGet("Users/GroupingOptions")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<IEnumerable<SpecialViewOptionDto>> GetGroupingOptions([FromRoute, Required] Guid userId)
public ActionResult<IEnumerable<SpecialViewOptionDto>> GetGroupingOptions([FromQuery] Guid? userId)
{
var user = _userManager.GetUserById(userId);
userId = RequestHelpers.GetUserId(User, userId);
var user = _userManager.GetUserById(userId.Value);
if (user is null)
{
return NotFound();
Expand All @@ -133,4 +152,22 @@ public ActionResult<IEnumerable<SpecialViewOptionDto>> GetGroupingOptions([FromR
.OrderBy(i => i.Name)
.AsEnumerable());
}

/// <summary>
/// Get user view grouping options.
/// </summary>
/// <param name="userId">User id.</param>
/// <response code="200">User view grouping options returned.</response>
/// <response code="404">User not found.</response>
/// <returns>
/// An <see cref="OkResult"/> containing the user view grouping options
/// or a <see cref="NotFoundResult"/> if user not found.
/// </returns>
[HttpGet("Users/{userId}/GroupingOptions")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Obsolete("Kept for backwards compatibility")]
[ApiExplorerSettings(IgnoreApi = true)]
public ActionResult<IEnumerable<SpecialViewOptionDto>> GetGroupingOptions2(
[FromRoute, Required] Guid userId) => GetGroupingOptions(userId);
}

0 comments on commit ccf150e

Please sign in to comment.