| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Jellyfin.Api.Extensions; |
| | | 8 | | using Jellyfin.Api.Helpers; |
| | | 9 | | using Jellyfin.Api.ModelBinders; |
| | | 10 | | using Jellyfin.Data.Enums; |
| | | 11 | | using Jellyfin.Database.Implementations.Entities; |
| | | 12 | | using Jellyfin.Extensions; |
| | | 13 | | using MediaBrowser.Controller.Dto; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Controller.Providers; |
| | | 18 | | using MediaBrowser.Model.Dto; |
| | | 19 | | using MediaBrowser.Model.Entities; |
| | | 20 | | using MediaBrowser.Model.IO; |
| | | 21 | | using MediaBrowser.Model.Querying; |
| | | 22 | | using Microsoft.AspNetCore.Authorization; |
| | | 23 | | using Microsoft.AspNetCore.Http; |
| | | 24 | | using Microsoft.AspNetCore.Mvc; |
| | | 25 | | |
| | | 26 | | namespace Jellyfin.Api.Controllers; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// User library controller. |
| | | 30 | | /// </summary> |
| | | 31 | | [Route("")] |
| | | 32 | | [Authorize] |
| | | 33 | | public class UserLibraryController : BaseJellyfinApiController |
| | | 34 | | { |
| | | 35 | | private readonly IUserManager _userManager; |
| | | 36 | | private readonly IUserDataManager _userDataRepository; |
| | | 37 | | private readonly ILibraryManager _libraryManager; |
| | | 38 | | private readonly IDtoService _dtoService; |
| | | 39 | | private readonly IUserViewManager _userViewManager; |
| | | 40 | | private readonly IFileSystem _fileSystem; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of the <see cref="UserLibraryController"/> class. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | | 46 | | /// <param name="userDataRepository">Instance of the <see cref="IUserDataManager"/> interface.</param> |
| | | 47 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 48 | | /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param> |
| | | 49 | | /// <param name="userViewManager">Instance of the <see cref="IUserViewManager"/> interface.</param> |
| | | 50 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | 15 | 51 | | public UserLibraryController( |
| | 15 | 52 | | IUserManager userManager, |
| | 15 | 53 | | IUserDataManager userDataRepository, |
| | 15 | 54 | | ILibraryManager libraryManager, |
| | 15 | 55 | | IDtoService dtoService, |
| | 15 | 56 | | IUserViewManager userViewManager, |
| | 15 | 57 | | IFileSystem fileSystem) |
| | | 58 | | { |
| | 15 | 59 | | _userManager = userManager; |
| | 15 | 60 | | _userDataRepository = userDataRepository; |
| | 15 | 61 | | _libraryManager = libraryManager; |
| | 15 | 62 | | _dtoService = dtoService; |
| | 15 | 63 | | _userViewManager = userViewManager; |
| | 15 | 64 | | _fileSystem = fileSystem; |
| | 15 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets an item from a user's library. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="userId">User id.</param> |
| | | 71 | | /// <param name="itemId">Item id.</param> |
| | | 72 | | /// <response code="200">Item returned.</response> |
| | | 73 | | /// <returns>An <see cref="OkResult"/> containing the item.</returns> |
| | | 74 | | [HttpGet("Items/{itemId}")] |
| | | 75 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 76 | | public async Task<ActionResult<BaseItemDto>> GetItem( |
| | | 77 | | [FromQuery] Guid? userId, |
| | | 78 | | [FromRoute, Required] Guid itemId) |
| | | 79 | | { |
| | | 80 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 81 | | var user = _userManager.GetUserById(userId.Value); |
| | | 82 | | if (user is null) |
| | | 83 | | { |
| | | 84 | | return NotFound(); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | var item = itemId.IsEmpty() |
| | | 88 | | ? _libraryManager.GetUserRootFolder() |
| | | 89 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | | 90 | | if (item is null) |
| | | 91 | | { |
| | | 92 | | return NotFound(); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | await RefreshItemOnDemandIfNeeded(item).ConfigureAwait(false); |
| | | 96 | | |
| | | 97 | | var dtoOptions = new DtoOptions().AddClientFields(User); |
| | | 98 | | |
| | | 99 | | return _dtoService.GetBaseItemDto(item, dtoOptions, user); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets an item from a user's library. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="userId">User id.</param> |
| | | 106 | | /// <param name="itemId">Item id.</param> |
| | | 107 | | /// <response code="200">Item returned.</response> |
| | | 108 | | /// <returns>An <see cref="OkResult"/> containing the item.</returns> |
| | | 109 | | [HttpGet("Users/{userId}/Items/{itemId}")] |
| | | 110 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 111 | | [Obsolete("Kept for backwards compatibility")] |
| | | 112 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 113 | | public Task<ActionResult<BaseItemDto>> GetItemLegacy( |
| | | 114 | | [FromRoute, Required] Guid userId, |
| | | 115 | | [FromRoute, Required] Guid itemId) |
| | | 116 | | => GetItem(userId, itemId); |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the root folder from a user's library. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="userId">User id.</param> |
| | | 122 | | /// <response code="200">Root folder returned.</response> |
| | | 123 | | /// <returns>An <see cref="OkResult"/> containing the user's root folder.</returns> |
| | | 124 | | [HttpGet("Items/Root")] |
| | | 125 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 126 | | public ActionResult<BaseItemDto> GetRootFolder([FromQuery] Guid? userId) |
| | | 127 | | { |
| | 7 | 128 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 7 | 129 | | var user = _userManager.GetUserById(userId.Value); |
| | 7 | 130 | | if (user is null) |
| | | 131 | | { |
| | 1 | 132 | | return NotFound(); |
| | | 133 | | } |
| | | 134 | | |
| | 6 | 135 | | var item = _libraryManager.GetUserRootFolder(); |
| | 6 | 136 | | var dtoOptions = new DtoOptions().AddClientFields(User); |
| | 6 | 137 | | return _dtoService.GetBaseItemDto(item, dtoOptions, user); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets the root folder from a user's library. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="userId">User id.</param> |
| | | 144 | | /// <response code="200">Root folder returned.</response> |
| | | 145 | | /// <returns>An <see cref="OkResult"/> containing the user's root folder.</returns> |
| | | 146 | | [HttpGet("Users/{userId}/Items/Root")] |
| | | 147 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 148 | | [Obsolete("Kept for backwards compatibility")] |
| | | 149 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 150 | | public ActionResult<BaseItemDto> GetRootFolderLegacy( |
| | | 151 | | [FromRoute, Required] Guid userId) |
| | | 152 | | => GetRootFolder(userId); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Gets intros to play before the main media item plays. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="userId">User id.</param> |
| | | 158 | | /// <param name="itemId">Item id.</param> |
| | | 159 | | /// <response code="200">Intros returned.</response> |
| | | 160 | | /// <returns>An <see cref="OkResult"/> containing the intros to play.</returns> |
| | | 161 | | [HttpGet("Items/{itemId}/Intros")] |
| | | 162 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 163 | | public async Task<ActionResult<QueryResult<BaseItemDto>>> GetIntros( |
| | | 164 | | [FromQuery] Guid? userId, |
| | | 165 | | [FromRoute, Required] Guid itemId) |
| | | 166 | | { |
| | | 167 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 168 | | var user = _userManager.GetUserById(userId.Value); |
| | | 169 | | if (user is null) |
| | | 170 | | { |
| | | 171 | | return NotFound(); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | var item = itemId.IsEmpty() |
| | | 175 | | ? _libraryManager.GetUserRootFolder() |
| | | 176 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | | 177 | | if (item is null) |
| | | 178 | | { |
| | | 179 | | return NotFound(); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | var items = await _libraryManager.GetIntros(item, user).ConfigureAwait(false); |
| | | 183 | | var dtoOptions = new DtoOptions().AddClientFields(User); |
| | | 184 | | var dtos = items.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user)).ToArray(); |
| | | 185 | | |
| | | 186 | | return new QueryResult<BaseItemDto>(dtos); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Gets intros to play before the main media item plays. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="userId">User id.</param> |
| | | 193 | | /// <param name="itemId">Item id.</param> |
| | | 194 | | /// <response code="200">Intros returned.</response> |
| | | 195 | | /// <returns>An <see cref="OkResult"/> containing the intros to play.</returns> |
| | | 196 | | [HttpGet("Users/{userId}/Items/{itemId}/Intros")] |
| | | 197 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 198 | | [Obsolete("Kept for backwards compatibility")] |
| | | 199 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 200 | | public Task<ActionResult<QueryResult<BaseItemDto>>> GetIntrosLegacy( |
| | | 201 | | [FromRoute, Required] Guid userId, |
| | | 202 | | [FromRoute, Required] Guid itemId) |
| | | 203 | | => GetIntros(userId, itemId); |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Marks an item as a favorite. |
| | | 207 | | /// </summary> |
| | | 208 | | /// <param name="userId">User id.</param> |
| | | 209 | | /// <param name="itemId">Item id.</param> |
| | | 210 | | /// <response code="200">Item marked as favorite.</response> |
| | | 211 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 212 | | [HttpPost("UserFavoriteItems/{itemId}")] |
| | | 213 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 214 | | public ActionResult<UserItemDataDto> MarkFavoriteItem( |
| | | 215 | | [FromQuery] Guid? userId, |
| | | 216 | | [FromRoute, Required] Guid itemId) |
| | | 217 | | { |
| | 0 | 218 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 219 | | var user = _userManager.GetUserById(userId.Value); |
| | 0 | 220 | | if (user is null) |
| | | 221 | | { |
| | 0 | 222 | | return NotFound(); |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | var item = itemId.IsEmpty() |
| | 0 | 226 | | ? _libraryManager.GetUserRootFolder() |
| | 0 | 227 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 0 | 228 | | if (item is null) |
| | | 229 | | { |
| | 0 | 230 | | return NotFound(); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | return MarkFavorite(user, item, true); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Marks an item as a favorite. |
| | | 238 | | /// </summary> |
| | | 239 | | /// <param name="userId">User id.</param> |
| | | 240 | | /// <param name="itemId">Item id.</param> |
| | | 241 | | /// <response code="200">Item marked as favorite.</response> |
| | | 242 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 243 | | [HttpPost("Users/{userId}/FavoriteItems/{itemId}")] |
| | | 244 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 245 | | [Obsolete("Kept for backwards compatibility")] |
| | | 246 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 247 | | public ActionResult<UserItemDataDto> MarkFavoriteItemLegacy( |
| | | 248 | | [FromRoute, Required] Guid userId, |
| | | 249 | | [FromRoute, Required] Guid itemId) |
| | | 250 | | => MarkFavoriteItem(userId, itemId); |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Unmarks item as a favorite. |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="userId">User id.</param> |
| | | 256 | | /// <param name="itemId">Item id.</param> |
| | | 257 | | /// <response code="200">Item unmarked as favorite.</response> |
| | | 258 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 259 | | [HttpDelete("UserFavoriteItems/{itemId}")] |
| | | 260 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 261 | | public ActionResult<UserItemDataDto> UnmarkFavoriteItem( |
| | | 262 | | [FromQuery] Guid? userId, |
| | | 263 | | [FromRoute, Required] Guid itemId) |
| | | 264 | | { |
| | 0 | 265 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 266 | | var user = _userManager.GetUserById(userId.Value); |
| | 0 | 267 | | if (user is null) |
| | | 268 | | { |
| | 0 | 269 | | return NotFound(); |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | var item = itemId.IsEmpty() |
| | 0 | 273 | | ? _libraryManager.GetUserRootFolder() |
| | 0 | 274 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 0 | 275 | | if (item is null) |
| | | 276 | | { |
| | 0 | 277 | | return NotFound(); |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | return MarkFavorite(user, item, false); |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary> |
| | | 284 | | /// Unmarks item as a favorite. |
| | | 285 | | /// </summary> |
| | | 286 | | /// <param name="userId">User id.</param> |
| | | 287 | | /// <param name="itemId">Item id.</param> |
| | | 288 | | /// <response code="200">Item unmarked as favorite.</response> |
| | | 289 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 290 | | [HttpDelete("Users/{userId}/FavoriteItems/{itemId}")] |
| | | 291 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 292 | | [Obsolete("Kept for backwards compatibility")] |
| | | 293 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 294 | | public ActionResult<UserItemDataDto> UnmarkFavoriteItemLegacy( |
| | | 295 | | [FromRoute, Required] Guid userId, |
| | | 296 | | [FromRoute, Required] Guid itemId) |
| | | 297 | | => UnmarkFavoriteItem(userId, itemId); |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Deletes a user's saved personal rating for an item. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <param name="userId">User id.</param> |
| | | 303 | | /// <param name="itemId">Item id.</param> |
| | | 304 | | /// <response code="200">Personal rating removed.</response> |
| | | 305 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 306 | | [HttpDelete("UserItems/{itemId}/Rating")] |
| | | 307 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 308 | | public ActionResult<UserItemDataDto?> DeleteUserItemRating( |
| | | 309 | | [FromQuery] Guid? userId, |
| | | 310 | | [FromRoute, Required] Guid itemId) |
| | | 311 | | { |
| | 0 | 312 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 313 | | var user = _userManager.GetUserById(userId.Value); |
| | 0 | 314 | | if (user is null) |
| | | 315 | | { |
| | 0 | 316 | | return NotFound(); |
| | | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | var item = itemId.IsEmpty() |
| | 0 | 320 | | ? _libraryManager.GetUserRootFolder() |
| | 0 | 321 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 0 | 322 | | if (item is null) |
| | | 323 | | { |
| | 0 | 324 | | return NotFound(); |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | return UpdateUserItemRatingInternal(user, item, null); |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Deletes a user's saved personal rating for an item. |
| | | 332 | | /// </summary> |
| | | 333 | | /// <param name="userId">User id.</param> |
| | | 334 | | /// <param name="itemId">Item id.</param> |
| | | 335 | | /// <response code="200">Personal rating removed.</response> |
| | | 336 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 337 | | [HttpDelete("Users/{userId}/Items/{itemId}/Rating")] |
| | | 338 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 339 | | [Obsolete("Kept for backwards compatibility")] |
| | | 340 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 341 | | public ActionResult<UserItemDataDto?> DeleteUserItemRatingLegacy( |
| | | 342 | | [FromRoute, Required] Guid userId, |
| | | 343 | | [FromRoute, Required] Guid itemId) |
| | | 344 | | => DeleteUserItemRating(userId, itemId); |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Updates a user's rating for an item. |
| | | 348 | | /// </summary> |
| | | 349 | | /// <param name="userId">User id.</param> |
| | | 350 | | /// <param name="itemId">Item id.</param> |
| | | 351 | | /// <param name="likes">Whether this <see cref="UpdateUserItemRating" /> is likes.</param> |
| | | 352 | | /// <response code="200">Item rating updated.</response> |
| | | 353 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 354 | | [HttpPost("UserItems/{itemId}/Rating")] |
| | | 355 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 356 | | public ActionResult<UserItemDataDto?> UpdateUserItemRating( |
| | | 357 | | [FromQuery] Guid? userId, |
| | | 358 | | [FromRoute, Required] Guid itemId, |
| | | 359 | | [FromQuery] bool? likes) |
| | | 360 | | { |
| | 0 | 361 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 362 | | var user = _userManager.GetUserById(userId.Value); |
| | 0 | 363 | | if (user is null) |
| | | 364 | | { |
| | 0 | 365 | | return NotFound(); |
| | | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | var item = itemId.IsEmpty() |
| | 0 | 369 | | ? _libraryManager.GetUserRootFolder() |
| | 0 | 370 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 0 | 371 | | if (item is null) |
| | | 372 | | { |
| | 0 | 373 | | return NotFound(); |
| | | 374 | | } |
| | | 375 | | |
| | 0 | 376 | | return UpdateUserItemRatingInternal(user, item, likes); |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | /// <summary> |
| | | 380 | | /// Updates a user's rating for an item. |
| | | 381 | | /// </summary> |
| | | 382 | | /// <param name="userId">User id.</param> |
| | | 383 | | /// <param name="itemId">Item id.</param> |
| | | 384 | | /// <param name="likes">Whether this <see cref="UpdateUserItemRating" /> is likes.</param> |
| | | 385 | | /// <response code="200">Item rating updated.</response> |
| | | 386 | | /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>.</returns> |
| | | 387 | | [HttpPost("Users/{userId}/Items/{itemId}/Rating")] |
| | | 388 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 389 | | [Obsolete("Kept for backwards compatibility")] |
| | | 390 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 391 | | public ActionResult<UserItemDataDto?> UpdateUserItemRatingLegacy( |
| | | 392 | | [FromRoute, Required] Guid userId, |
| | | 393 | | [FromRoute, Required] Guid itemId, |
| | | 394 | | [FromQuery] bool? likes) |
| | | 395 | | => UpdateUserItemRating(userId, itemId, likes); |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Gets local trailers for an item. |
| | | 399 | | /// </summary> |
| | | 400 | | /// <param name="userId">User id.</param> |
| | | 401 | | /// <param name="itemId">Item id.</param> |
| | | 402 | | /// <response code="200">An <see cref="OkResult"/> containing the item's local trailers.</response> |
| | | 403 | | /// <returns>The items local trailers.</returns> |
| | | 404 | | [HttpGet("Items/{itemId}/LocalTrailers")] |
| | | 405 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 406 | | public ActionResult<IEnumerable<BaseItemDto>> GetLocalTrailers( |
| | | 407 | | [FromQuery] Guid? userId, |
| | | 408 | | [FromRoute, Required] Guid itemId) |
| | | 409 | | { |
| | 2 | 410 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 2 | 411 | | var user = _userManager.GetUserById(userId.Value); |
| | 2 | 412 | | if (user is null) |
| | | 413 | | { |
| | 1 | 414 | | return NotFound(); |
| | | 415 | | } |
| | | 416 | | |
| | 1 | 417 | | var item = itemId.IsEmpty() |
| | 1 | 418 | | ? _libraryManager.GetUserRootFolder() |
| | 1 | 419 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 420 | | if (item is null) |
| | | 421 | | { |
| | 1 | 422 | | return NotFound(); |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | var dtoOptions = new DtoOptions().AddClientFields(User); |
| | 0 | 426 | | if (item is IHasTrailers hasTrailers) |
| | | 427 | | { |
| | 0 | 428 | | var trailers = hasTrailers.LocalTrailers; |
| | 0 | 429 | | return Ok(_dtoService.GetBaseItemDtos(trailers, dtoOptions, user, item).AsEnumerable()); |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | return Ok(item.GetExtras() |
| | 0 | 433 | | .Where(e => e.ExtraType == ExtraType.Trailer) |
| | 0 | 434 | | .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item))); |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | /// <summary> |
| | | 438 | | /// Gets local trailers for an item. |
| | | 439 | | /// </summary> |
| | | 440 | | /// <param name="userId">User id.</param> |
| | | 441 | | /// <param name="itemId">Item id.</param> |
| | | 442 | | /// <response code="200">An <see cref="OkResult"/> containing the item's local trailers.</response> |
| | | 443 | | /// <returns>The items local trailers.</returns> |
| | | 444 | | [HttpGet("Users/{userId}/Items/{itemId}/LocalTrailers")] |
| | | 445 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 446 | | [Obsolete("Kept for backwards compatibility")] |
| | | 447 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 448 | | public ActionResult<IEnumerable<BaseItemDto>> GetLocalTrailersLegacy( |
| | | 449 | | [FromRoute, Required] Guid userId, |
| | | 450 | | [FromRoute, Required] Guid itemId) |
| | | 451 | | => GetLocalTrailers(userId, itemId); |
| | | 452 | | |
| | | 453 | | /// <summary> |
| | | 454 | | /// Gets special features for an item. |
| | | 455 | | /// </summary> |
| | | 456 | | /// <param name="userId">User id.</param> |
| | | 457 | | /// <param name="itemId">Item id.</param> |
| | | 458 | | /// <response code="200">Special features returned.</response> |
| | | 459 | | /// <returns>An <see cref="OkResult"/> containing the special features.</returns> |
| | | 460 | | [HttpGet("Items/{itemId}/SpecialFeatures")] |
| | | 461 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 462 | | public ActionResult<IEnumerable<BaseItemDto>> GetSpecialFeatures( |
| | | 463 | | [FromQuery] Guid? userId, |
| | | 464 | | [FromRoute, Required] Guid itemId) |
| | | 465 | | { |
| | 2 | 466 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 2 | 467 | | var user = _userManager.GetUserById(userId.Value); |
| | 2 | 468 | | if (user is null) |
| | | 469 | | { |
| | 1 | 470 | | return NotFound(); |
| | | 471 | | } |
| | | 472 | | |
| | 1 | 473 | | var item = itemId.IsEmpty() |
| | 1 | 474 | | ? _libraryManager.GetUserRootFolder() |
| | 1 | 475 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 476 | | if (item is null) |
| | | 477 | | { |
| | 1 | 478 | | return NotFound(); |
| | | 479 | | } |
| | | 480 | | |
| | 0 | 481 | | var dtoOptions = new DtoOptions().AddClientFields(User); |
| | | 482 | | |
| | 0 | 483 | | return Ok(item |
| | 0 | 484 | | .GetExtras() |
| | 0 | 485 | | .Where(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value)) |
| | 0 | 486 | | .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item))); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Gets special features for an item. |
| | | 491 | | /// </summary> |
| | | 492 | | /// <param name="userId">User id.</param> |
| | | 493 | | /// <param name="itemId">Item id.</param> |
| | | 494 | | /// <response code="200">Special features returned.</response> |
| | | 495 | | /// <returns>An <see cref="OkResult"/> containing the special features.</returns> |
| | | 496 | | [HttpGet("Users/{userId}/Items/{itemId}/SpecialFeatures")] |
| | | 497 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 498 | | [Obsolete("Kept for backwards compatibility")] |
| | | 499 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 500 | | public ActionResult<IEnumerable<BaseItemDto>> GetSpecialFeaturesLegacy( |
| | | 501 | | [FromRoute, Required] Guid userId, |
| | | 502 | | [FromRoute, Required] Guid itemId) |
| | | 503 | | => GetSpecialFeatures(userId, itemId); |
| | | 504 | | |
| | | 505 | | /// <summary> |
| | | 506 | | /// Gets latest media. |
| | | 507 | | /// </summary> |
| | | 508 | | /// <param name="userId">User id.</param> |
| | | 509 | | /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</ |
| | | 510 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 511 | | /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows |
| | | 512 | | /// <param name="isPlayed">Filter by items that are played, or not.</param> |
| | | 513 | | /// <param name="enableImages">Optional. include image information in output.</param> |
| | | 514 | | /// <param name="imageTypeLimit">Optional. the max number of images to return, per image type.</param> |
| | | 515 | | /// <param name="enableImageTypes">Optional. The image types to include in the output.</param> |
| | | 516 | | /// <param name="enableUserData">Optional. include user data.</param> |
| | | 517 | | /// <param name="limit">Return item limit.</param> |
| | | 518 | | /// <param name="groupItems">Whether or not to group items into a parent container.</param> |
| | | 519 | | /// <response code="200">Latest media returned.</response> |
| | | 520 | | /// <returns>An <see cref="OkResult"/> containing the latest media.</returns> |
| | | 521 | | [HttpGet("Items/Latest")] |
| | | 522 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 523 | | public ActionResult<IEnumerable<BaseItemDto>> GetLatestMedia( |
| | | 524 | | [FromQuery] Guid? userId, |
| | | 525 | | [FromQuery] Guid? parentId, |
| | | 526 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields, |
| | | 527 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes, |
| | | 528 | | [FromQuery] bool? isPlayed, |
| | | 529 | | [FromQuery] bool? enableImages, |
| | | 530 | | [FromQuery] int? imageTypeLimit, |
| | | 531 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes, |
| | | 532 | | [FromQuery] bool? enableUserData, |
| | | 533 | | [FromQuery] int limit = 20, |
| | | 534 | | [FromQuery] bool groupItems = true) |
| | | 535 | | { |
| | 0 | 536 | | var requestUserId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 537 | | var user = _userManager.GetUserById(requestUserId); |
| | 0 | 538 | | if (user is null) |
| | | 539 | | { |
| | 0 | 540 | | return NotFound(); |
| | | 541 | | } |
| | | 542 | | |
| | 0 | 543 | | if (!isPlayed.HasValue) |
| | | 544 | | { |
| | 0 | 545 | | if (user.HidePlayedInLatest) |
| | | 546 | | { |
| | 0 | 547 | | isPlayed = false; |
| | | 548 | | } |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | var dtoOptions = new DtoOptions { Fields = fields } |
| | 0 | 552 | | .AddClientFields(User) |
| | 0 | 553 | | .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes); |
| | | 554 | | |
| | 0 | 555 | | var list = _userViewManager.GetLatestItems( |
| | 0 | 556 | | new LatestItemsQuery |
| | 0 | 557 | | { |
| | 0 | 558 | | GroupItems = groupItems, |
| | 0 | 559 | | IncludeItemTypes = includeItemTypes, |
| | 0 | 560 | | IsPlayed = isPlayed, |
| | 0 | 561 | | Limit = limit, |
| | 0 | 562 | | ParentId = parentId ?? Guid.Empty, |
| | 0 | 563 | | User = user, |
| | 0 | 564 | | }, |
| | 0 | 565 | | dtoOptions); |
| | | 566 | | |
| | 0 | 567 | | var dtos = list.Select(i => |
| | 0 | 568 | | { |
| | 0 | 569 | | var item = i.Item2[0]; |
| | 0 | 570 | | var childCount = 0; |
| | 0 | 571 | | |
| | 0 | 572 | | if (i.Item1 is not null && (i.Item2.Count > 1 || i.Item1 is MusicAlbum)) |
| | 0 | 573 | | { |
| | 0 | 574 | | item = i.Item1; |
| | 0 | 575 | | childCount = i.Item2.Count; |
| | 0 | 576 | | } |
| | 0 | 577 | | |
| | 0 | 578 | | var dto = _dtoService.GetBaseItemDto(item, dtoOptions, user); |
| | 0 | 579 | | |
| | 0 | 580 | | dto.ChildCount = childCount; |
| | 0 | 581 | | |
| | 0 | 582 | | return dto; |
| | 0 | 583 | | }); |
| | | 584 | | |
| | 0 | 585 | | return Ok(dtos); |
| | | 586 | | } |
| | | 587 | | |
| | | 588 | | /// <summary> |
| | | 589 | | /// Gets latest media. |
| | | 590 | | /// </summary> |
| | | 591 | | /// <param name="userId">User id.</param> |
| | | 592 | | /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</ |
| | | 593 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 594 | | /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows |
| | | 595 | | /// <param name="isPlayed">Filter by items that are played, or not.</param> |
| | | 596 | | /// <param name="enableImages">Optional. include image information in output.</param> |
| | | 597 | | /// <param name="imageTypeLimit">Optional. the max number of images to return, per image type.</param> |
| | | 598 | | /// <param name="enableImageTypes">Optional. The image types to include in the output.</param> |
| | | 599 | | /// <param name="enableUserData">Optional. include user data.</param> |
| | | 600 | | /// <param name="limit">Return item limit.</param> |
| | | 601 | | /// <param name="groupItems">Whether or not to group items into a parent container.</param> |
| | | 602 | | /// <response code="200">Latest media returned.</response> |
| | | 603 | | /// <returns>An <see cref="OkResult"/> containing the latest media.</returns> |
| | | 604 | | [HttpGet("Users/{userId}/Items/Latest")] |
| | | 605 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 606 | | [Obsolete("Kept for backwards compatibility")] |
| | | 607 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | | 608 | | public ActionResult<IEnumerable<BaseItemDto>> GetLatestMediaLegacy( |
| | | 609 | | [FromRoute, Required] Guid userId, |
| | | 610 | | [FromQuery] Guid? parentId, |
| | | 611 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields, |
| | | 612 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes, |
| | | 613 | | [FromQuery] bool? isPlayed, |
| | | 614 | | [FromQuery] bool? enableImages, |
| | | 615 | | [FromQuery] int? imageTypeLimit, |
| | | 616 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes, |
| | | 617 | | [FromQuery] bool? enableUserData, |
| | | 618 | | [FromQuery] int limit = 20, |
| | | 619 | | [FromQuery] bool groupItems = true) |
| | | 620 | | => GetLatestMedia( |
| | | 621 | | userId, |
| | | 622 | | parentId, |
| | | 623 | | fields, |
| | | 624 | | includeItemTypes, |
| | | 625 | | isPlayed, |
| | | 626 | | enableImages, |
| | | 627 | | imageTypeLimit, |
| | | 628 | | enableImageTypes, |
| | | 629 | | enableUserData, |
| | | 630 | | limit, |
| | | 631 | | groupItems); |
| | | 632 | | |
| | | 633 | | private async Task RefreshItemOnDemandIfNeeded(BaseItem item) |
| | | 634 | | { |
| | | 635 | | if (item is Person) |
| | | 636 | | { |
| | | 637 | | var hasMetadata = !string.IsNullOrWhiteSpace(item.Overview) && item.HasImage(ImageType.Primary); |
| | | 638 | | var performFullRefresh = !hasMetadata && (DateTime.UtcNow - item.DateLastRefreshed).TotalDays >= 3; |
| | | 639 | | |
| | | 640 | | if (!hasMetadata) |
| | | 641 | | { |
| | | 642 | | var options = new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | | 643 | | { |
| | | 644 | | MetadataRefreshMode = MetadataRefreshMode.FullRefresh, |
| | | 645 | | ImageRefreshMode = MetadataRefreshMode.FullRefresh, |
| | | 646 | | ForceSave = performFullRefresh |
| | | 647 | | }; |
| | | 648 | | |
| | | 649 | | await item.RefreshMetadata(options, CancellationToken.None).ConfigureAwait(false); |
| | | 650 | | } |
| | | 651 | | } |
| | | 652 | | } |
| | | 653 | | |
| | | 654 | | /// <summary> |
| | | 655 | | /// Marks the favorite. |
| | | 656 | | /// </summary> |
| | | 657 | | /// <param name="user">The user.</param> |
| | | 658 | | /// <param name="item">The item.</param> |
| | | 659 | | /// <param name="isFavorite">if set to <c>true</c> [is favorite].</param> |
| | | 660 | | private UserItemDataDto MarkFavorite(User user, BaseItem item, bool isFavorite) |
| | | 661 | | { |
| | | 662 | | // Get the user data for this item |
| | 0 | 663 | | var data = _userDataRepository.GetUserData(user, item); |
| | | 664 | | |
| | 0 | 665 | | if (data is not null) |
| | | 666 | | { |
| | | 667 | | // Set favorite status |
| | 0 | 668 | | data.IsFavorite = isFavorite; |
| | | 669 | | |
| | 0 | 670 | | _userDataRepository.SaveUserData(user, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.No |
| | | 671 | | } |
| | | 672 | | |
| | 0 | 673 | | return _userDataRepository.GetUserDataDto(item, user)!; |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | /// <summary> |
| | | 677 | | /// Updates the user item rating. |
| | | 678 | | /// </summary> |
| | | 679 | | /// <param name="user">The user.</param> |
| | | 680 | | /// <param name="item">The item.</param> |
| | | 681 | | /// <param name="likes">if set to <c>true</c> [likes].</param> |
| | | 682 | | private UserItemDataDto? UpdateUserItemRatingInternal(User user, BaseItem item, bool? likes) |
| | | 683 | | { |
| | | 684 | | // Get the user data for this item |
| | 0 | 685 | | var data = _userDataRepository.GetUserData(user, item); |
| | | 686 | | |
| | 0 | 687 | | if (data is not null) |
| | | 688 | | { |
| | 0 | 689 | | data.Likes = likes; |
| | | 690 | | |
| | 0 | 691 | | _userDataRepository.SaveUserData(user, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.No |
| | | 692 | | } |
| | | 693 | | |
| | 0 | 694 | | return _userDataRepository.GetUserDataDto(item, user); |
| | | 695 | | } |
| | | 696 | | } |