From 1b96a009c488333dccc52fb44833fca4703b038f Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 20 Aug 2024 22:10:54 +0300 Subject: [PATCH] api: filter indexers by state --- src/Jackett.Common/Models/DTO/Indexer.cs | 12 +++++++ .../Controllers/IndexerApiController.cs | 33 ++++++++++++++++--- src/Jackett.Server/Startup.cs | 15 ++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/Jackett.Common/Models/DTO/Indexer.cs b/src/Jackett.Common/Models/DTO/Indexer.cs index 2216b107f..bbd91914b 100644 --- a/src/Jackett.Common/Models/DTO/Indexer.cs +++ b/src/Jackett.Common/Models/DTO/Indexer.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; +using Jackett.Common.Extensions; using Jackett.Common.Indexers; namespace Jackett.Common.Models.DTO @@ -40,6 +41,9 @@ namespace Jackett.Common.Models.DTO [DataMember] public bool potatoenabled { get; private set; } + [DataMember] + public IndexerState state => last_error.IsNotNullOrWhiteSpace() ? IndexerState.Error : IndexerState.Success; + [DataMember] public IEnumerable caps { get; private set; } @@ -67,4 +71,12 @@ namespace Jackett.Common.Models.DTO }); } } + + [DataContract] + public enum IndexerState + { + Unknown, + Error, + Success + } } diff --git a/src/Jackett.Server/Controllers/IndexerApiController.cs b/src/Jackett.Server/Controllers/IndexerApiController.cs index 564138f31..035f6162e 100644 --- a/src/Jackett.Server/Controllers/IndexerApiController.cs +++ b/src/Jackett.Server/Controllers/IndexerApiController.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Jackett.Common.Indexers; using Jackett.Common.Models; +using Jackett.Common.Models.DTO; using Jackett.Common.Services.Interfaces; using Jackett.Common.Utils; using Microsoft.AspNetCore.Mvc; @@ -105,19 +106,36 @@ namespace Jackett.Server.Controllers } catch { - var baseIndexer = CurrentIndexer as BaseIndexer; - if (null != baseIndexer) + if (CurrentIndexer is BaseIndexer baseIndexer) + { baseIndexer.ResetBaseConfig(); + } + throw; } } [HttpGet] [Route("")] - public IEnumerable Indexers([FromQuery(Name = "configured")] bool configured) + public IEnumerable Indexers([FromQuery(Name = "configured")] bool configured, [FromQuery(Name = "state")] FilterIndexerState filterState) { var dto = IndexerService.GetAllIndexers().Select(i => new Common.Models.DTO.Indexer(i)); - dto = configured ? dto.Where(i => i.configured) : dto; + + if (configured) + { + dto = dto.Where(i => i.configured); + } + + switch (filterState) + { + case FilterIndexerState.Success: + dto = dto.Where(i => i.state == IndexerState.Success); + break; + case FilterIndexerState.Error: + dto = dto.Where(i => i.state == IndexerState.Error); + break; + } + return dto; } @@ -177,4 +195,11 @@ namespace Jackett.Server.Controllers } } + + public enum FilterIndexerState + { + All, + Error, + Success + } } diff --git a/src/Jackett.Server/Startup.cs b/src/Jackett.Server/Startup.cs index 963f54fcc..8aeac7f48 100644 --- a/src/Jackett.Server/Startup.cs +++ b/src/Jackett.Server/Startup.cs @@ -61,16 +61,23 @@ namespace Jackett.Server config => config.Filters.Add( new AuthorizeFilter( new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()))) - .AddJsonOptions(options => options.SerializerSettings.ContractResolver = - new DefaultContractResolver()); //Web app uses Pascal Case JSON); + .AddJsonOptions(options => + { + // Web app uses Pascal Case JSON) + options.SerializerSettings.ContractResolver = new DefaultContractResolver(); + options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter(new CamelCaseNamingStrategy())); + }); #else services.AddControllers( config => config.Filters.Add( new AuthorizeFilter( new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()))) - .AddNewtonsoftJson( - options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); + .AddNewtonsoftJson(options => + { + options.SerializerSettings.ContractResolver = new DefaultContractResolver(); + options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter(new CamelCaseNamingStrategy())); + }); #endif var runtimeSettings = new RuntimeSettings();