| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Jellyfin.Api.Attributes; |
| | | 8 | | using Jellyfin.Api.Extensions; |
| | | 9 | | using Jellyfin.Api.Helpers; |
| | | 10 | | using Jellyfin.Api.ModelBinders; |
| | | 11 | | using Jellyfin.Api.Models.PlaylistDtos; |
| | | 12 | | using Jellyfin.Data.Enums; |
| | | 13 | | using Jellyfin.Extensions; |
| | | 14 | | using MediaBrowser.Controller.Dto; |
| | | 15 | | using MediaBrowser.Controller.Library; |
| | | 16 | | using MediaBrowser.Controller.Playlists; |
| | | 17 | | using MediaBrowser.Model.Dto; |
| | | 18 | | using MediaBrowser.Model.Entities; |
| | | 19 | | using MediaBrowser.Model.Playlists; |
| | | 20 | | using MediaBrowser.Model.Querying; |
| | | 21 | | using Microsoft.AspNetCore.Authorization; |
| | | 22 | | using Microsoft.AspNetCore.Http; |
| | | 23 | | using Microsoft.AspNetCore.Mvc; |
| | | 24 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | | 25 | | |
| | | 26 | | namespace Jellyfin.Api.Controllers; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Playlists controller. |
| | | 30 | | /// </summary> |
| | | 31 | | [Authorize] |
| | | 32 | | public class PlaylistsController : BaseJellyfinApiController |
| | | 33 | | { |
| | | 34 | | private readonly IPlaylistManager _playlistManager; |
| | | 35 | | private readonly IDtoService _dtoService; |
| | | 36 | | private readonly IUserManager _userManager; |
| | | 37 | | private readonly ILibraryManager _libraryManager; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of the <see cref="PlaylistsController"/> class. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param> |
| | | 43 | | /// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param> |
| | | 44 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | | 45 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 0 | 46 | | public PlaylistsController( |
| | 0 | 47 | | IDtoService dtoService, |
| | 0 | 48 | | IPlaylistManager playlistManager, |
| | 0 | 49 | | IUserManager userManager, |
| | 0 | 50 | | ILibraryManager libraryManager) |
| | | 51 | | { |
| | 0 | 52 | | _dtoService = dtoService; |
| | 0 | 53 | | _playlistManager = playlistManager; |
| | 0 | 54 | | _userManager = userManager; |
| | 0 | 55 | | _libraryManager = libraryManager; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Creates a new playlist. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <remarks> |
| | | 62 | | /// For backwards compatibility parameters can be sent via Query or Body, with Query having higher precedence. |
| | | 63 | | /// Query parameters are obsolete. |
| | | 64 | | /// </remarks> |
| | | 65 | | /// <param name="name">The playlist name.</param> |
| | | 66 | | /// <param name="ids">The item ids.</param> |
| | | 67 | | /// <param name="userId">The user id.</param> |
| | | 68 | | /// <param name="mediaType">The media type.</param> |
| | | 69 | | /// <param name="createPlaylistRequest">The create playlist payload.</param> |
| | | 70 | | /// <response code="200">Playlist created.</response> |
| | | 71 | | /// <returns> |
| | | 72 | | /// A <see cref="Task" /> that represents the asynchronous operation to create a playlist. |
| | | 73 | | /// The task result contains an <see cref="OkResult"/> indicating success. |
| | | 74 | | /// </returns> |
| | | 75 | | [HttpPost] |
| | | 76 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 77 | | public async Task<ActionResult<PlaylistCreationResult>> CreatePlaylist( |
| | | 78 | | [FromQuery, ParameterObsolete] string? name, |
| | | 79 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder)), ParameterObsolete] IReadOnlyList<Guid> ids |
| | | 80 | | [FromQuery, ParameterObsolete] Guid? userId, |
| | | 81 | | [FromQuery, ParameterObsolete] MediaType? mediaType, |
| | | 82 | | [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] CreatePlaylistDto? createPlaylistRequest) |
| | | 83 | | { |
| | | 84 | | if (ids.Count == 0) |
| | | 85 | | { |
| | | 86 | | ids = createPlaylistRequest?.Ids ?? Array.Empty<Guid>(); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | userId ??= createPlaylistRequest?.UserId ?? default; |
| | | 90 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 91 | | var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest |
| | | 92 | | { |
| | | 93 | | Name = name ?? createPlaylistRequest?.Name, |
| | | 94 | | ItemIdList = ids, |
| | | 95 | | UserId = userId.Value, |
| | | 96 | | MediaType = mediaType ?? createPlaylistRequest?.MediaType, |
| | | 97 | | Users = createPlaylistRequest?.Users.ToArray() ?? [], |
| | | 98 | | Public = createPlaylistRequest?.IsPublic |
| | | 99 | | }).ConfigureAwait(false); |
| | | 100 | | |
| | | 101 | | return result; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Updates a playlist. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="playlistId">The playlist id.</param> |
| | | 108 | | /// <param name="updatePlaylistRequest">The <see cref="UpdatePlaylistDto"/> id.</param> |
| | | 109 | | /// <response code="204">Playlist updated.</response> |
| | | 110 | | /// <response code="403">Access forbidden.</response> |
| | | 111 | | /// <response code="404">Playlist not found.</response> |
| | | 112 | | /// <returns> |
| | | 113 | | /// A <see cref="Task" /> that represents the asynchronous operation to update a playlist. |
| | | 114 | | /// The task result contains an <see cref="OkResult"/> indicating success. |
| | | 115 | | /// </returns> |
| | | 116 | | [HttpPost("{playlistId}")] |
| | | 117 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 118 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 119 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 120 | | public async Task<ActionResult> UpdatePlaylist( |
| | | 121 | | [FromRoute, Required] Guid playlistId, |
| | | 122 | | [FromBody, Required] UpdatePlaylistDto updatePlaylistRequest) |
| | | 123 | | { |
| | | 124 | | var callingUserId = User.GetUserId(); |
| | | 125 | | |
| | | 126 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, callingUserId); |
| | | 127 | | if (playlist is null) |
| | | 128 | | { |
| | | 129 | | return NotFound("Playlist not found"); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | var isPermitted = playlist.OwnerUserId.Equals(callingUserId) |
| | | 133 | | || playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(callingUserId)); |
| | | 134 | | |
| | | 135 | | if (!isPermitted) |
| | | 136 | | { |
| | | 137 | | return Forbid(); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | await _playlistManager.UpdatePlaylist(new PlaylistUpdateRequest |
| | | 141 | | { |
| | | 142 | | UserId = callingUserId, |
| | | 143 | | Id = playlistId, |
| | | 144 | | Name = updatePlaylistRequest.Name, |
| | | 145 | | Ids = updatePlaylistRequest.Ids, |
| | | 146 | | Users = updatePlaylistRequest.Users, |
| | | 147 | | Public = updatePlaylistRequest.IsPublic |
| | | 148 | | }).ConfigureAwait(false); |
| | | 149 | | |
| | | 150 | | return NoContent(); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Get a playlist. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="playlistId">The playlist id.</param> |
| | | 157 | | /// <response code="200">The playlist.</response> |
| | | 158 | | /// <response code="404">Playlist not found.</response> |
| | | 159 | | /// <returns> |
| | | 160 | | /// A <see cref="Playlist"/> objects. |
| | | 161 | | /// </returns> |
| | | 162 | | [HttpGet("{playlistId}")] |
| | | 163 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 164 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 165 | | public ActionResult<PlaylistDto> GetPlaylist( |
| | | 166 | | [FromRoute, Required] Guid playlistId) |
| | | 167 | | { |
| | 0 | 168 | | var userId = User.GetUserId(); |
| | | 169 | | |
| | 0 | 170 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, userId); |
| | 0 | 171 | | if (playlist is null) |
| | | 172 | | { |
| | 0 | 173 | | return NotFound("Playlist not found"); |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | return new PlaylistDto() |
| | 0 | 177 | | { |
| | 0 | 178 | | Shares = playlist.Shares, |
| | 0 | 179 | | OpenAccess = playlist.OpenAccess, |
| | 0 | 180 | | ItemIds = playlist.GetManageableItems().Select(t => t.Item2.Id).ToList() |
| | 0 | 181 | | }; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Get a playlist's users. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="playlistId">The playlist id.</param> |
| | | 188 | | /// <response code="200">Found shares.</response> |
| | | 189 | | /// <response code="403">Access forbidden.</response> |
| | | 190 | | /// <response code="404">Playlist not found.</response> |
| | | 191 | | /// <returns> |
| | | 192 | | /// A list of <see cref="PlaylistUserPermissions"/> objects. |
| | | 193 | | /// </returns> |
| | | 194 | | [HttpGet("{playlistId}/Users")] |
| | | 195 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 196 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 197 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 198 | | public ActionResult<IReadOnlyList<PlaylistUserPermissions>> GetPlaylistUsers( |
| | | 199 | | [FromRoute, Required] Guid playlistId) |
| | | 200 | | { |
| | 0 | 201 | | var userId = User.GetUserId(); |
| | | 202 | | |
| | 0 | 203 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, userId); |
| | 0 | 204 | | if (playlist is null) |
| | | 205 | | { |
| | 0 | 206 | | return NotFound("Playlist not found"); |
| | | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | var isPermitted = playlist.OwnerUserId.Equals(userId); |
| | | 210 | | |
| | 0 | 211 | | return isPermitted ? playlist.Shares.ToList() : Forbid(); |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Get a playlist user. |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="playlistId">The playlist id.</param> |
| | | 218 | | /// <param name="userId">The user id.</param> |
| | | 219 | | /// <response code="200">User permission found.</response> |
| | | 220 | | /// <response code="403">Access forbidden.</response> |
| | | 221 | | /// <response code="404">Playlist not found.</response> |
| | | 222 | | /// <returns> |
| | | 223 | | /// <see cref="PlaylistUserPermissions"/>. |
| | | 224 | | /// </returns> |
| | | 225 | | [HttpGet("{playlistId}/Users/{userId}")] |
| | | 226 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 227 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 228 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 229 | | public ActionResult<PlaylistUserPermissions?> GetPlaylistUser( |
| | | 230 | | [FromRoute, Required] Guid playlistId, |
| | | 231 | | [FromRoute, Required] Guid userId) |
| | | 232 | | { |
| | 0 | 233 | | var callingUserId = User.GetUserId(); |
| | | 234 | | |
| | 0 | 235 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, callingUserId); |
| | 0 | 236 | | if (playlist is null) |
| | | 237 | | { |
| | 0 | 238 | | return NotFound("Playlist not found"); |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | if (playlist.OwnerUserId.Equals(callingUserId)) |
| | | 242 | | { |
| | 0 | 243 | | return new PlaylistUserPermissions(callingUserId, true); |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | var userPermission = playlist.Shares.FirstOrDefault(s => s.UserId.Equals(userId)); |
| | 0 | 247 | | var isPermitted = playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(callingUserId)) |
| | 0 | 248 | | || userId.Equals(callingUserId); |
| | | 249 | | |
| | 0 | 250 | | if (!isPermitted) |
| | | 251 | | { |
| | 0 | 252 | | return Forbid(); |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | if (userPermission is not null) |
| | | 256 | | { |
| | 0 | 257 | | return userPermission; |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | return NotFound("User permissions not found"); |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Modify a user of a playlist's users. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="playlistId">The playlist id.</param> |
| | | 267 | | /// <param name="userId">The user id.</param> |
| | | 268 | | /// <param name="updatePlaylistUserRequest">The <see cref="UpdatePlaylistUserDto"/>.</param> |
| | | 269 | | /// <response code="204">User's permissions modified.</response> |
| | | 270 | | /// <response code="403">Access forbidden.</response> |
| | | 271 | | /// <response code="404">Playlist not found.</response> |
| | | 272 | | /// <returns> |
| | | 273 | | /// A <see cref="Task" /> that represents the asynchronous operation to modify an user's playlist permissions. |
| | | 274 | | /// The task result contains an <see cref="OkResult"/> indicating success. |
| | | 275 | | /// </returns> |
| | | 276 | | [HttpPost("{playlistId}/Users/{userId}")] |
| | | 277 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 278 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 279 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 280 | | public async Task<ActionResult> UpdatePlaylistUser( |
| | | 281 | | [FromRoute, Required] Guid playlistId, |
| | | 282 | | [FromRoute, Required] Guid userId, |
| | | 283 | | [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow), Required] UpdatePlaylistUserDto updatePlaylistUserReques |
| | | 284 | | { |
| | | 285 | | var callingUserId = User.GetUserId(); |
| | | 286 | | |
| | | 287 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, callingUserId); |
| | | 288 | | if (playlist is null) |
| | | 289 | | { |
| | | 290 | | return NotFound("Playlist not found"); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | var isPermitted = playlist.OwnerUserId.Equals(callingUserId); |
| | | 294 | | |
| | | 295 | | if (!isPermitted) |
| | | 296 | | { |
| | | 297 | | return Forbid(); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | await _playlistManager.AddUserToShares(new PlaylistUserUpdateRequest |
| | | 301 | | { |
| | | 302 | | Id = playlistId, |
| | | 303 | | UserId = userId, |
| | | 304 | | CanEdit = updatePlaylistUserRequest.CanEdit |
| | | 305 | | }).ConfigureAwait(false); |
| | | 306 | | |
| | | 307 | | return NoContent(); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Remove a user from a playlist's users. |
| | | 312 | | /// </summary> |
| | | 313 | | /// <param name="playlistId">The playlist id.</param> |
| | | 314 | | /// <param name="userId">The user id.</param> |
| | | 315 | | /// <response code="204">User permissions removed from playlist.</response> |
| | | 316 | | /// <response code="401">Unauthorized access.</response> |
| | | 317 | | /// <response code="404">No playlist or user permissions found.</response> |
| | | 318 | | /// <returns> |
| | | 319 | | /// A <see cref="Task" /> that represents the asynchronous operation to delete a user from a playlist's shares. |
| | | 320 | | /// The task result contains an <see cref="OkResult"/> indicating success. |
| | | 321 | | /// </returns> |
| | | 322 | | [HttpDelete("{playlistId}/Users/{userId}")] |
| | | 323 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 324 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 325 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 326 | | public async Task<ActionResult> RemoveUserFromPlaylist( |
| | | 327 | | [FromRoute, Required] Guid playlistId, |
| | | 328 | | [FromRoute, Required] Guid userId) |
| | | 329 | | { |
| | | 330 | | var callingUserId = User.GetUserId(); |
| | | 331 | | |
| | | 332 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, callingUserId); |
| | | 333 | | if (playlist is null) |
| | | 334 | | { |
| | | 335 | | return NotFound("Playlist not found"); |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | var isPermitted = playlist.OwnerUserId.Equals(callingUserId) |
| | | 339 | | || playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(callingUserId)); |
| | | 340 | | |
| | | 341 | | if (!isPermitted) |
| | | 342 | | { |
| | | 343 | | return Forbid(); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | var share = playlist.Shares.FirstOrDefault(s => s.UserId.Equals(userId)); |
| | | 347 | | if (share is null) |
| | | 348 | | { |
| | | 349 | | return NotFound("User permissions not found"); |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | await _playlistManager.RemoveUserFromShares(playlistId, callingUserId, share).ConfigureAwait(false); |
| | | 353 | | |
| | | 354 | | return NoContent(); |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Adds items to a playlist. |
| | | 359 | | /// </summary> |
| | | 360 | | /// <param name="playlistId">The playlist id.</param> |
| | | 361 | | /// <param name="ids">Item id, comma delimited.</param> |
| | | 362 | | /// <param name="userId">The userId.</param> |
| | | 363 | | /// <response code="204">Items added to playlist.</response> |
| | | 364 | | /// <response code="403">Access forbidden.</response> |
| | | 365 | | /// <response code="404">Playlist not found.</response> |
| | | 366 | | /// <returns>An <see cref="NoContentResult"/> on success.</returns> |
| | | 367 | | [HttpPost("{playlistId}/Items")] |
| | | 368 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 369 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 370 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 371 | | public async Task<ActionResult> AddItemToPlaylist( |
| | | 372 | | [FromRoute, Required] Guid playlistId, |
| | | 373 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] Guid[] ids, |
| | | 374 | | [FromQuery] Guid? userId) |
| | | 375 | | { |
| | | 376 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 377 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, userId.Value); |
| | | 378 | | if (playlist is null) |
| | | 379 | | { |
| | | 380 | | return NotFound("Playlist not found"); |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | var isPermitted = playlist.OwnerUserId.Equals(userId.Value) |
| | | 384 | | || playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(userId.Value)); |
| | | 385 | | |
| | | 386 | | if (!isPermitted) |
| | | 387 | | { |
| | | 388 | | return Forbid(); |
| | | 389 | | } |
| | | 390 | | |
| | | 391 | | await _playlistManager.AddItemToPlaylistAsync(playlistId, ids, userId.Value).ConfigureAwait(false); |
| | | 392 | | return NoContent(); |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Moves a playlist item. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="playlistId">The playlist id.</param> |
| | | 399 | | /// <param name="itemId">The item id.</param> |
| | | 400 | | /// <param name="newIndex">The new index.</param> |
| | | 401 | | /// <response code="204">Item moved to new index.</response> |
| | | 402 | | /// <response code="403">Access forbidden.</response> |
| | | 403 | | /// <response code="404">Playlist not found.</response> |
| | | 404 | | /// <returns>An <see cref="NoContentResult"/> on success.</returns> |
| | | 405 | | [HttpPost("{playlistId}/Items/{itemId}/Move/{newIndex}")] |
| | | 406 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 407 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 408 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 409 | | public async Task<ActionResult> MoveItem( |
| | | 410 | | [FromRoute, Required] string playlistId, |
| | | 411 | | [FromRoute, Required] string itemId, |
| | | 412 | | [FromRoute, Required] int newIndex) |
| | | 413 | | { |
| | | 414 | | var callingUserId = User.GetUserId(); |
| | | 415 | | |
| | | 416 | | var playlist = _playlistManager.GetPlaylistForUser(Guid.Parse(playlistId), callingUserId); |
| | | 417 | | if (playlist is null) |
| | | 418 | | { |
| | | 419 | | return NotFound("Playlist not found"); |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | var isPermitted = playlist.OwnerUserId.Equals(callingUserId) |
| | | 423 | | || playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(callingUserId)); |
| | | 424 | | |
| | | 425 | | if (!isPermitted) |
| | | 426 | | { |
| | | 427 | | return Forbid(); |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | await _playlistManager.MoveItemAsync(playlistId, itemId, newIndex, callingUserId).ConfigureAwait(false); |
| | | 431 | | return NoContent(); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | /// <summary> |
| | | 435 | | /// Removes items from a playlist. |
| | | 436 | | /// </summary> |
| | | 437 | | /// <param name="playlistId">The playlist id.</param> |
| | | 438 | | /// <param name="entryIds">The item ids, comma delimited.</param> |
| | | 439 | | /// <response code="204">Items removed.</response> |
| | | 440 | | /// <response code="403">Access forbidden.</response> |
| | | 441 | | /// <response code="404">Playlist not found.</response> |
| | | 442 | | /// <returns>An <see cref="NoContentResult"/> on success.</returns> |
| | | 443 | | [HttpDelete("{playlistId}/Items")] |
| | | 444 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 445 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 446 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 447 | | public async Task<ActionResult> RemoveItemFromPlaylist( |
| | | 448 | | [FromRoute, Required] string playlistId, |
| | | 449 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] string[] entryIds) |
| | | 450 | | { |
| | | 451 | | var callingUserId = User.GetUserId(); |
| | | 452 | | |
| | | 453 | | if (!callingUserId.IsEmpty()) |
| | | 454 | | { |
| | | 455 | | var playlist = _playlistManager.GetPlaylistForUser(Guid.Parse(playlistId), callingUserId); |
| | | 456 | | if (playlist is null) |
| | | 457 | | { |
| | | 458 | | return NotFound("Playlist not found"); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | var isPermitted = playlist.OwnerUserId.Equals(callingUserId) |
| | | 462 | | || playlist.Shares.Any(s => s.CanEdit && s.UserId.Equals(callingUserId)); |
| | | 463 | | |
| | | 464 | | if (!isPermitted) |
| | | 465 | | { |
| | | 466 | | return Forbid(); |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | else |
| | | 470 | | { |
| | | 471 | | var isApiKey = User.GetIsApiKey(); |
| | | 472 | | |
| | | 473 | | if (!isApiKey) |
| | | 474 | | { |
| | | 475 | | return Forbid(); |
| | | 476 | | } |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | try |
| | | 480 | | { |
| | | 481 | | await _playlistManager.RemoveItemFromPlaylistAsync(playlistId, entryIds).ConfigureAwait(false); |
| | | 482 | | return NoContent(); |
| | | 483 | | } |
| | | 484 | | catch (ArgumentException) |
| | | 485 | | { |
| | | 486 | | return NotFound(); |
| | | 487 | | } |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | /// <summary> |
| | | 491 | | /// Gets the original items of a playlist. |
| | | 492 | | /// </summary> |
| | | 493 | | /// <param name="playlistId">The playlist id.</param> |
| | | 494 | | /// <param name="userId">User id.</param> |
| | | 495 | | /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr |
| | | 496 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 497 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 498 | | /// <param name="enableImages">Optional. Include image information in output.</param> |
| | | 499 | | /// <param name="enableUserData">Optional. Include user data.</param> |
| | | 500 | | /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param> |
| | | 501 | | /// <param name="enableImageTypes">Optional. The image types to include in the output.</param> |
| | | 502 | | /// <response code="200">Original playlist returned.</response> |
| | | 503 | | /// <response code="404">Access forbidden.</response> |
| | | 504 | | /// <response code="404">Playlist not found.</response> |
| | | 505 | | /// <returns>The original playlist items.</returns> |
| | | 506 | | [HttpGet("{playlistId}/Items")] |
| | | 507 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 508 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | | 509 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 510 | | public ActionResult<QueryResult<BaseItemDto>> GetPlaylistItems( |
| | | 511 | | [FromRoute, Required] Guid playlistId, |
| | | 512 | | [FromQuery] Guid? userId, |
| | | 513 | | [FromQuery] int? startIndex, |
| | | 514 | | [FromQuery] int? limit, |
| | | 515 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields, |
| | | 516 | | [FromQuery] bool? enableImages, |
| | | 517 | | [FromQuery] bool? enableUserData, |
| | | 518 | | [FromQuery] int? imageTypeLimit, |
| | | 519 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes) |
| | | 520 | | { |
| | 0 | 521 | | var callingUserId = userId ?? User.GetUserId(); |
| | 0 | 522 | | var playlist = _playlistManager.GetPlaylistForUser(playlistId, callingUserId); |
| | 0 | 523 | | if (playlist is null) |
| | | 524 | | { |
| | 0 | 525 | | return NotFound("Playlist not found"); |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | var isPermitted = playlist.OpenAccess |
| | 0 | 529 | | || playlist.OwnerUserId.Equals(callingUserId) |
| | 0 | 530 | | || playlist.Shares.Any(s => s.UserId.Equals(callingUserId)); |
| | | 531 | | |
| | 0 | 532 | | if (!isPermitted) |
| | | 533 | | { |
| | 0 | 534 | | return Forbid(); |
| | | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | var user = _userManager.GetUserById(callingUserId); |
| | 0 | 538 | | var items = playlist.GetManageableItems().Where(i => i.Item2.IsVisible(user)).ToArray(); |
| | 0 | 539 | | var count = items.Length; |
| | 0 | 540 | | if (startIndex.HasValue) |
| | | 541 | | { |
| | 0 | 542 | | items = items.Skip(startIndex.Value).ToArray(); |
| | | 543 | | } |
| | | 544 | | |
| | 0 | 545 | | if (limit.HasValue) |
| | | 546 | | { |
| | 0 | 547 | | items = items.Take(limit.Value).ToArray(); |
| | | 548 | | } |
| | | 549 | | |
| | 0 | 550 | | var dtoOptions = new DtoOptions { Fields = fields } |
| | 0 | 551 | | .AddClientFields(User) |
| | 0 | 552 | | .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes); |
| | | 553 | | |
| | 0 | 554 | | var dtos = _dtoService.GetBaseItemDtos(items.Select(i => i.Item2).ToList(), dtoOptions, user); |
| | 0 | 555 | | for (int index = 0; index < dtos.Count; index++) |
| | | 556 | | { |
| | 0 | 557 | | dtos[index].PlaylistItemId = items[index].Item1.ItemId?.ToString("N", CultureInfo.InvariantCulture); |
| | | 558 | | } |
| | | 559 | | |
| | 0 | 560 | | var result = new QueryResult<BaseItemDto>( |
| | 0 | 561 | | startIndex, |
| | 0 | 562 | | count, |
| | 0 | 563 | | dtos); |
| | | 564 | | |
| | 0 | 565 | | return result; |
| | | 566 | | } |
| | | 567 | | } |