| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Jellyfin.Data.Enums; |
| | | 5 | | using Jellyfin.Data.Queries; |
| | | 6 | | using Jellyfin.Database.Implementations.Enums; |
| | | 7 | | using MediaBrowser.Common.Api; |
| | | 8 | | using MediaBrowser.Model.Activity; |
| | | 9 | | using MediaBrowser.Model.Querying; |
| | | 10 | | using Microsoft.AspNetCore.Authorization; |
| | | 11 | | using Microsoft.AspNetCore.Http; |
| | | 12 | | using Microsoft.AspNetCore.Mvc; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Api.Controllers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Activity log controller. |
| | | 19 | | /// </summary> |
| | | 20 | | [Route("System/ActivityLog")] |
| | | 21 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 22 | | public class ActivityLogController : BaseJellyfinApiController |
| | | 23 | | { |
| | | 24 | | private readonly IActivityManager _activityManager; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="ActivityLogController"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="activityManager">Instance of <see cref="IActivityManager"/> interface.</param> |
| | 1 | 30 | | public ActivityLogController(IActivityManager activityManager) |
| | | 31 | | { |
| | 1 | 32 | | _activityManager = activityManager; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets activity log entries. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="startIndex">The record index to start at. All items with a lower index will be dropped from the res |
| | | 39 | | /// <param name="limit">The maximum number of records to return.</param> |
| | | 40 | | /// <param name="minDate">The minimum date.</param> |
| | | 41 | | /// <param name="maxDate">The maximum date.</param> |
| | | 42 | | /// <param name="hasUserId">Filter log entries if it has user id, or not.</param> |
| | | 43 | | /// <param name="name">Filter by name.</param> |
| | | 44 | | /// <param name="overview">Filter by overview.</param> |
| | | 45 | | /// <param name="shortOverview">Filter by short overview.</param> |
| | | 46 | | /// <param name="type">Filter by type.</param> |
| | | 47 | | /// <param name="itemId">Filter by item id.</param> |
| | | 48 | | /// <param name="username">Filter by username.</param> |
| | | 49 | | /// <param name="severity">Filter by log severity.</param> |
| | | 50 | | /// <param name="sortBy">Specify one or more sort orders. Format: SortBy=Name,Type.</param> |
| | | 51 | | /// <param name="sortOrder">Sort Order..</param> |
| | | 52 | | /// <response code="200">Activity log returned.</response> |
| | | 53 | | /// <returns>A <see cref="QueryResult{ActivityLogEntry}"/> containing the log entries.</returns> |
| | | 54 | | [HttpGet("Entries")] |
| | | 55 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 56 | | public async Task<ActionResult<QueryResult<ActivityLogEntry>>> GetLogEntries( |
| | | 57 | | [FromQuery] int? startIndex, |
| | | 58 | | [FromQuery] int? limit, |
| | | 59 | | [FromQuery] DateTime? minDate, |
| | | 60 | | [FromQuery] DateTime? maxDate, |
| | | 61 | | [FromQuery] bool? hasUserId, |
| | | 62 | | [FromQuery] string? name, |
| | | 63 | | [FromQuery] string? overview, |
| | | 64 | | [FromQuery] string? shortOverview, |
| | | 65 | | [FromQuery] string? type, |
| | | 66 | | [FromQuery] Guid? itemId, |
| | | 67 | | [FromQuery] string? username, |
| | | 68 | | [FromQuery] LogLevel? severity, |
| | | 69 | | [FromQuery] ActivityLogSortBy[]? sortBy, |
| | | 70 | | [FromQuery] SortOrder[]? sortOrder) |
| | | 71 | | { |
| | | 72 | | var query = new ActivityLogQuery |
| | | 73 | | { |
| | | 74 | | Skip = startIndex, |
| | | 75 | | Limit = limit, |
| | | 76 | | MinDate = minDate, |
| | | 77 | | MaxDate = maxDate, |
| | | 78 | | HasUserId = hasUserId, |
| | | 79 | | Name = name, |
| | | 80 | | Overview = overview, |
| | | 81 | | ShortOverview = shortOverview, |
| | | 82 | | Type = type, |
| | | 83 | | ItemId = itemId, |
| | | 84 | | Username = username, |
| | | 85 | | Severity = severity, |
| | | 86 | | OrderBy = GetOrderBy(sortBy ?? [], sortOrder ?? []), |
| | | 87 | | }; |
| | | 88 | | |
| | | 89 | | return await _activityManager.GetPagedResultAsync(query).ConfigureAwait(false); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static (ActivityLogSortBy SortBy, SortOrder SortOrder)[] GetOrderBy( |
| | | 93 | | IReadOnlyList<ActivityLogSortBy> sortBy, |
| | | 94 | | IReadOnlyList<SortOrder> requestedSortOrder) |
| | | 95 | | { |
| | 1 | 96 | | if (sortBy.Count == 0) |
| | | 97 | | { |
| | 1 | 98 | | return []; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var result = new (ActivityLogSortBy, SortOrder)[sortBy.Count]; |
| | 0 | 102 | | var i = 0; |
| | 0 | 103 | | for (; i < requestedSortOrder.Count; i++) |
| | | 104 | | { |
| | 0 | 105 | | result[i] = (sortBy[i], requestedSortOrder[i]); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | // Add remaining elements with the first specified SortOrder |
| | | 109 | | // or the default one if no SortOrders are specified |
| | 0 | 110 | | var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending; |
| | 0 | 111 | | for (; i < sortBy.Count; i++) |
| | | 112 | | { |
| | 0 | 113 | | result[i] = (sortBy[i], order); |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return result; |
| | | 117 | | } |
| | | 118 | | } |