| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Api.Helpers; |
| | | 7 | | using Jellyfin.Api.ModelBinders; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Database.Implementations.Enums; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using MediaBrowser.Controller.Channels; |
| | | 12 | | using MediaBrowser.Controller.Dto; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Model.Channels; |
| | | 16 | | using MediaBrowser.Model.Dto; |
| | | 17 | | using MediaBrowser.Model.Querying; |
| | | 18 | | using Microsoft.AspNetCore.Authorization; |
| | | 19 | | using Microsoft.AspNetCore.Http; |
| | | 20 | | using Microsoft.AspNetCore.Mvc; |
| | | 21 | | |
| | | 22 | | namespace Jellyfin.Api.Controllers; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Channels Controller. |
| | | 26 | | /// </summary> |
| | | 27 | | [Authorize] |
| | | 28 | | public class ChannelsController : BaseJellyfinApiController |
| | | 29 | | { |
| | | 30 | | private readonly IChannelManager _channelManager; |
| | | 31 | | private readonly IUserManager _userManager; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="ChannelsController"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="channelManager">Instance of the <see cref="IChannelManager"/> interface.</param> |
| | | 37 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | 0 | 38 | | public ChannelsController(IChannelManager channelManager, IUserManager userManager) |
| | | 39 | | { |
| | 0 | 40 | | _channelManager = channelManager; |
| | 0 | 41 | | _userManager = userManager; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets available channels. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="userId">User Id to filter by. Use <see cref="Guid.Empty"/> to not filter by user.</param> |
| | | 48 | | /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr |
| | | 49 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 50 | | /// <param name="supportsLatestItems">Optional. Filter by channels that support getting latest items.</param> |
| | | 51 | | /// <param name="supportsMediaDeletion">Optional. Filter by channels that support media deletion.</param> |
| | | 52 | | /// <param name="isFavorite">Optional. Filter by channels that are favorite.</param> |
| | | 53 | | /// <response code="200">Channels returned.</response> |
| | | 54 | | /// <returns>An <see cref="OkResult"/> containing the channels.</returns> |
| | | 55 | | [HttpGet] |
| | | 56 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 57 | | public async Task<ActionResult<QueryResult<BaseItemDto>>> GetChannels( |
| | | 58 | | [FromQuery] Guid? userId, |
| | | 59 | | [FromQuery] int? startIndex, |
| | | 60 | | [FromQuery] int? limit, |
| | | 61 | | [FromQuery] bool? supportsLatestItems, |
| | | 62 | | [FromQuery] bool? supportsMediaDeletion, |
| | | 63 | | [FromQuery] bool? isFavorite) |
| | | 64 | | { |
| | | 65 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 66 | | return await _channelManager.GetChannelsAsync(new ChannelQuery |
| | | 67 | | { |
| | | 68 | | Limit = limit, |
| | | 69 | | StartIndex = startIndex, |
| | | 70 | | UserId = userId.Value, |
| | | 71 | | SupportsLatestItems = supportsLatestItems, |
| | | 72 | | SupportsMediaDeletion = supportsMediaDeletion, |
| | | 73 | | IsFavorite = isFavorite |
| | | 74 | | }).ConfigureAwait(false); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Get all channel features. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <response code="200">All channel features returned.</response> |
| | | 81 | | /// <returns>An <see cref="OkResult"/> containing the channel features.</returns> |
| | | 82 | | [HttpGet("Features")] |
| | | 83 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 84 | | public ActionResult<IEnumerable<ChannelFeatures>> GetAllChannelFeatures() |
| | | 85 | | { |
| | 0 | 86 | | return _channelManager.GetAllChannelFeatures(); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Get channel features. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="channelId">Channel id.</param> |
| | | 93 | | /// <response code="200">Channel features returned.</response> |
| | | 94 | | /// <returns>An <see cref="OkResult"/> containing the channel features.</returns> |
| | | 95 | | [HttpGet("{channelId}/Features")] |
| | | 96 | | public ActionResult<ChannelFeatures> GetChannelFeatures([FromRoute, Required] Guid channelId) |
| | | 97 | | { |
| | 0 | 98 | | return _channelManager.GetChannelFeatures(channelId); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Get channel items. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="channelId">Channel Id.</param> |
| | | 105 | | /// <param name="folderId">Optional. Folder Id.</param> |
| | | 106 | | /// <param name="userId">Optional. User Id.</param> |
| | | 107 | | /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr |
| | | 108 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 109 | | /// <param name="sortOrder">Optional. Sort Order - Ascending,Descending.</param> |
| | | 110 | | /// <param name="filters">Optional. Specify additional filters to apply.</param> |
| | | 111 | | /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar |
| | | 112 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 113 | | /// <response code="200">Channel items returned.</response> |
| | | 114 | | /// <returns> |
| | | 115 | | /// A <see cref="Task"/> representing the request to get the channel items. |
| | | 116 | | /// The task result contains an <see cref="OkResult"/> containing the channel items. |
| | | 117 | | /// </returns> |
| | | 118 | | [HttpGet("{channelId}/Items")] |
| | | 119 | | public async Task<ActionResult<QueryResult<BaseItemDto>>> GetChannelItems( |
| | | 120 | | [FromRoute, Required] Guid channelId, |
| | | 121 | | [FromQuery] Guid? folderId, |
| | | 122 | | [FromQuery] Guid? userId, |
| | | 123 | | [FromQuery] int? startIndex, |
| | | 124 | | [FromQuery] int? limit, |
| | | 125 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[] sortOrder, |
| | | 126 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFilter[] filters, |
| | | 127 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[] sortBy, |
| | | 128 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields) |
| | | 129 | | { |
| | | 130 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 131 | | var user = userId.IsNullOrEmpty() |
| | | 132 | | ? null |
| | | 133 | | : _userManager.GetUserById(userId.Value); |
| | | 134 | | |
| | | 135 | | var query = new InternalItemsQuery(user) |
| | | 136 | | { |
| | | 137 | | Limit = limit, |
| | | 138 | | StartIndex = startIndex, |
| | | 139 | | ChannelIds = new[] { channelId }, |
| | | 140 | | ParentId = folderId ?? Guid.Empty, |
| | | 141 | | OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder), |
| | | 142 | | DtoOptions = new DtoOptions { Fields = fields } |
| | | 143 | | }; |
| | | 144 | | |
| | | 145 | | foreach (var filter in filters) |
| | | 146 | | { |
| | | 147 | | switch (filter) |
| | | 148 | | { |
| | | 149 | | case ItemFilter.IsFolder: |
| | | 150 | | query.IsFolder = true; |
| | | 151 | | break; |
| | | 152 | | case ItemFilter.IsNotFolder: |
| | | 153 | | query.IsFolder = false; |
| | | 154 | | break; |
| | | 155 | | case ItemFilter.IsUnplayed: |
| | | 156 | | query.IsPlayed = false; |
| | | 157 | | break; |
| | | 158 | | case ItemFilter.IsPlayed: |
| | | 159 | | query.IsPlayed = true; |
| | | 160 | | break; |
| | | 161 | | case ItemFilter.IsFavorite: |
| | | 162 | | query.IsFavorite = true; |
| | | 163 | | break; |
| | | 164 | | case ItemFilter.IsResumable: |
| | | 165 | | query.IsResumable = true; |
| | | 166 | | break; |
| | | 167 | | case ItemFilter.Likes: |
| | | 168 | | query.IsLiked = true; |
| | | 169 | | break; |
| | | 170 | | case ItemFilter.Dislikes: |
| | | 171 | | query.IsLiked = false; |
| | | 172 | | break; |
| | | 173 | | case ItemFilter.IsFavoriteOrLikes: |
| | | 174 | | query.IsFavoriteOrLiked = true; |
| | | 175 | | break; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | return await _channelManager.GetChannelItems(query, CancellationToken.None).ConfigureAwait(false); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets latest channel items. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="userId">Optional. User Id.</param> |
| | | 186 | | /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr |
| | | 187 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 188 | | /// <param name="filters">Optional. Specify additional filters to apply.</param> |
| | | 189 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 190 | | /// <param name="channelIds">Optional. Specify one or more channel id's, comma delimited.</param> |
| | | 191 | | /// <response code="200">Latest channel items returned.</response> |
| | | 192 | | /// <returns> |
| | | 193 | | /// A <see cref="Task"/> representing the request to get the latest channel items. |
| | | 194 | | /// The task result contains an <see cref="OkResult"/> containing the latest channel items. |
| | | 195 | | /// </returns> |
| | | 196 | | [HttpGet("Items/Latest")] |
| | | 197 | | public async Task<ActionResult<QueryResult<BaseItemDto>>> GetLatestChannelItems( |
| | | 198 | | [FromQuery] Guid? userId, |
| | | 199 | | [FromQuery] int? startIndex, |
| | | 200 | | [FromQuery] int? limit, |
| | | 201 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFilter[] filters, |
| | | 202 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields, |
| | | 203 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] Guid[] channelIds) |
| | | 204 | | { |
| | | 205 | | userId = RequestHelpers.GetUserId(User, userId); |
| | | 206 | | var user = userId.IsNullOrEmpty() |
| | | 207 | | ? null |
| | | 208 | | : _userManager.GetUserById(userId.Value); |
| | | 209 | | |
| | | 210 | | var query = new InternalItemsQuery(user) |
| | | 211 | | { |
| | | 212 | | Limit = limit, |
| | | 213 | | StartIndex = startIndex, |
| | | 214 | | ChannelIds = channelIds, |
| | | 215 | | DtoOptions = new DtoOptions { Fields = fields } |
| | | 216 | | }; |
| | | 217 | | |
| | | 218 | | foreach (var filter in filters) |
| | | 219 | | { |
| | | 220 | | switch (filter) |
| | | 221 | | { |
| | | 222 | | case ItemFilter.IsFolder: |
| | | 223 | | query.IsFolder = true; |
| | | 224 | | break; |
| | | 225 | | case ItemFilter.IsNotFolder: |
| | | 226 | | query.IsFolder = false; |
| | | 227 | | break; |
| | | 228 | | case ItemFilter.IsUnplayed: |
| | | 229 | | query.IsPlayed = false; |
| | | 230 | | break; |
| | | 231 | | case ItemFilter.IsPlayed: |
| | | 232 | | query.IsPlayed = true; |
| | | 233 | | break; |
| | | 234 | | case ItemFilter.IsFavorite: |
| | | 235 | | query.IsFavorite = true; |
| | | 236 | | break; |
| | | 237 | | case ItemFilter.IsResumable: |
| | | 238 | | query.IsResumable = true; |
| | | 239 | | break; |
| | | 240 | | case ItemFilter.Likes: |
| | | 241 | | query.IsLiked = true; |
| | | 242 | | break; |
| | | 243 | | case ItemFilter.Dislikes: |
| | | 244 | | query.IsLiked = false; |
| | | 245 | | break; |
| | | 246 | | case ItemFilter.IsFavoriteOrLikes: |
| | | 247 | | query.IsFavoriteOrLiked = true; |
| | | 248 | | break; |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | return await _channelManager.GetLatestChannelItems(query, CancellationToken.None).ConfigureAwait(false); |
| | | 253 | | } |
| | | 254 | | } |