mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-02-15 09:02:25 +00:00
Merge pull request #15138 from kevgrig/issue15137
Add moveToTop option to IPlaylistManager.AddItemToPlaylistAsync
This commit is contained in:
@@ -198,17 +198,22 @@ namespace Emby.Server.Implementations.Playlists
|
||||
return Playlist.GetPlaylistItems(items, user, options);
|
||||
}
|
||||
|
||||
public Task AddItemToPlaylistAsync(Guid playlistId, IReadOnlyCollection<Guid> itemIds, Guid userId)
|
||||
public Task AddItemToPlaylistAsync(Guid playlistId, IReadOnlyCollection<Guid> itemIds, int? position, Guid userId)
|
||||
{
|
||||
var user = userId.IsEmpty() ? null : _userManager.GetUserById(userId);
|
||||
|
||||
return AddToPlaylistInternal(playlistId, itemIds, user, new DtoOptions(false)
|
||||
{
|
||||
EnableImages = true
|
||||
});
|
||||
return AddToPlaylistInternal(
|
||||
playlistId,
|
||||
itemIds,
|
||||
user,
|
||||
new DtoOptions(false)
|
||||
{
|
||||
EnableImages = true
|
||||
},
|
||||
position);
|
||||
}
|
||||
|
||||
private async Task AddToPlaylistInternal(Guid playlistId, IReadOnlyCollection<Guid> newItemIds, User user, DtoOptions options)
|
||||
private async Task AddToPlaylistInternal(Guid playlistId, IReadOnlyCollection<Guid> newItemIds, User user, DtoOptions options, int? position = null)
|
||||
{
|
||||
// Retrieve the existing playlist
|
||||
var playlist = _libraryManager.GetItemById(playlistId) as Playlist
|
||||
@@ -243,7 +248,30 @@ namespace Emby.Server.Implementations.Playlists
|
||||
}
|
||||
|
||||
// Update the playlist in the repository
|
||||
playlist.LinkedChildren = [.. playlist.LinkedChildren, .. childrenToAdd];
|
||||
if (position.HasValue)
|
||||
{
|
||||
if (position.Value <= 0)
|
||||
{
|
||||
playlist.LinkedChildren = [.. childrenToAdd, .. playlist.LinkedChildren];
|
||||
}
|
||||
else if (position.Value >= playlist.LinkedChildren.Length)
|
||||
{
|
||||
playlist.LinkedChildren = [.. playlist.LinkedChildren, .. childrenToAdd];
|
||||
}
|
||||
else
|
||||
{
|
||||
playlist.LinkedChildren = [
|
||||
.. playlist.LinkedChildren[0..position.Value],
|
||||
.. childrenToAdd,
|
||||
.. playlist.LinkedChildren[position.Value..playlist.LinkedChildren.Length]
|
||||
];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
playlist.LinkedChildren = [.. playlist.LinkedChildren, .. childrenToAdd];
|
||||
}
|
||||
|
||||
playlist.DateLastMediaAdded = DateTime.UtcNow;
|
||||
|
||||
await UpdatePlaylistInternal(playlist).ConfigureAwait(false);
|
||||
|
||||
@@ -359,6 +359,7 @@ public class PlaylistsController : BaseJellyfinApiController
|
||||
/// </summary>
|
||||
/// <param name="playlistId">The playlist id.</param>
|
||||
/// <param name="ids">Item id, comma delimited.</param>
|
||||
/// <param name="position">Optional. 0-based index where to place the items or at the end if <c>null</c>.</param>
|
||||
/// <param name="userId">The userId.</param>
|
||||
/// <response code="204">Items added to playlist.</response>
|
||||
/// <response code="403">Access forbidden.</response>
|
||||
@@ -371,6 +372,7 @@ public class PlaylistsController : BaseJellyfinApiController
|
||||
public async Task<ActionResult> AddItemToPlaylist(
|
||||
[FromRoute, Required] Guid playlistId,
|
||||
[FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] Guid[] ids,
|
||||
[FromQuery] int? position,
|
||||
[FromQuery] Guid? userId)
|
||||
{
|
||||
userId = RequestHelpers.GetUserId(User, userId);
|
||||
@@ -388,7 +390,7 @@ public class PlaylistsController : BaseJellyfinApiController
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
await _playlistManager.AddItemToPlaylistAsync(playlistId, ids, userId.Value).ConfigureAwait(false);
|
||||
await _playlistManager.AddItemToPlaylistAsync(playlistId, ids, position, userId.Value).ConfigureAwait(false);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
@@ -61,9 +61,10 @@ namespace MediaBrowser.Controller.Playlists
|
||||
/// </summary>
|
||||
/// <param name="playlistId">The playlist identifier.</param>
|
||||
/// <param name="itemIds">The item ids.</param>
|
||||
/// <param name="position">Optional. 0-based index where to place the items or at the end if null.</param>
|
||||
/// <param name="userId">The user identifier.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task AddItemToPlaylistAsync(Guid playlistId, IReadOnlyCollection<Guid> itemIds, Guid userId);
|
||||
Task AddItemToPlaylistAsync(Guid playlistId, IReadOnlyCollection<Guid> itemIds, int? position, Guid userId);
|
||||
|
||||
/// <summary>
|
||||
/// Removes from playlist.
|
||||
|
||||
Reference in New Issue
Block a user