From 10a999e270950951ce29613c363d5fbdf466f522 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 21 Feb 2021 18:03:07 -0500 Subject: [PATCH] Consolidate sync code to ApplicationService --- .../Applications/ApplicationBase.cs | 1 - .../Applications/ApplicationService.cs | 32 +++++++++++++-- .../Applications/IApplication.cs | 2 +- .../Applications/Lidarr/Lidarr.cs | 41 +------------------ .../Applications/Radarr/Radarr.cs | 41 +------------------ .../Applications/Readarr/Readarr.cs | 41 +------------------ .../Applications/Sonarr/Sonarr.cs | 41 +------------------ 7 files changed, 37 insertions(+), 162 deletions(-) diff --git a/src/NzbDrone.Core/Applications/ApplicationBase.cs b/src/NzbDrone.Core/Applications/ApplicationBase.cs index 4c241dda0..9707ffb5d 100644 --- a/src/NzbDrone.Core/Applications/ApplicationBase.cs +++ b/src/NzbDrone.Core/Applications/ApplicationBase.cs @@ -54,7 +54,6 @@ namespace NzbDrone.Core.Applications public abstract void AddIndexer(IndexerDefinition indexer); public abstract void UpdateIndexer(IndexerDefinition indexer); public abstract void RemoveIndexer(int indexerId); - public abstract void SyncIndexers(); public virtual object RequestAction(string action, IDictionary query) { diff --git a/src/NzbDrone.Core/Applications/ApplicationService.cs b/src/NzbDrone.Core/Applications/ApplicationService.cs index a2a5292bd..720c35b25 100644 --- a/src/NzbDrone.Core/Applications/ApplicationService.cs +++ b/src/NzbDrone.Core/Applications/ApplicationService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Net; using NLog; @@ -17,13 +18,17 @@ namespace NzbDrone.Core.Applications IExecute { private readonly IApplicationFactory _applicationsFactory; + private readonly IAppIndexerMapService _appIndexerMapService; + private readonly IIndexerFactory _indexerFactory; private readonly IApplicationStatusService _applicationStatusService; private readonly Logger _logger; - public ApplicationService(IApplicationFactory applicationsFactory, IApplicationStatusService applicationStatusService, Logger logger) + public ApplicationService(IApplicationFactory applicationsFactory, IApplicationStatusService applicationStatusService, IAppIndexerMapService appIndexerMapService, IIndexerFactory indexerFactory, Logger logger) { _applicationsFactory = applicationsFactory; _applicationStatusService = applicationStatusService; + _appIndexerMapService = appIndexerMapService; + _indexerFactory = indexerFactory; _logger = logger; } @@ -36,7 +41,7 @@ namespace NzbDrone.Core.Applications { var app = _applicationsFactory.GetInstance(appDefinition); - ExecuteAction(a => a.SyncIndexers(), app); + SyncIndexers(new List { app }); } } @@ -76,9 +81,28 @@ namespace NzbDrone.Core.Applications { var enabledApps = _applicationsFactory.SyncEnabled(); - foreach (var app in enabledApps) + SyncIndexers(enabledApps); + } + + private void SyncIndexers(List applications) + { + var indexers = _indexerFactory.Enabled(); + + foreach (var app in applications) { - ExecuteAction(a => a.SyncIndexers(), app); + var indexerMappings = _appIndexerMapService.GetMappingsForApp(app.Definition.Id); + + foreach (var indexer in indexers) + { + if (indexerMappings.Any(x => x.IndexerId == indexer.Definition.Id)) + { + continue; + } + + var definition = (IndexerDefinition)indexer.Definition; + + ExecuteAction(a => a.AddIndexer(definition), app); + } } } diff --git a/src/NzbDrone.Core/Applications/IApplication.cs b/src/NzbDrone.Core/Applications/IApplication.cs index 4311523d5..27ed112ba 100644 --- a/src/NzbDrone.Core/Applications/IApplication.cs +++ b/src/NzbDrone.Core/Applications/IApplication.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using NzbDrone.Core.Indexers; using NzbDrone.Core.ThingiProvider; @@ -5,7 +6,6 @@ namespace NzbDrone.Core.Applications { public interface IApplication : IProvider { - void SyncIndexers(); void AddIndexer(IndexerDefinition indexer); void UpdateIndexer(IndexerDefinition indexer); void RemoveIndexer(int indexerId); diff --git a/src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs b/src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs index e48afb1dd..5a2e50223 100644 --- a/src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs +++ b/src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; @@ -13,14 +14,12 @@ namespace NzbDrone.Core.Applications.Lidarr public override string Name => "Lidarr"; private readonly ILidarrV1Proxy _lidarrV1Proxy; - private readonly IIndexerFactory _indexerFactory; private readonly IConfigFileProvider _configFileProvider; - public Lidarr(ILidarrV1Proxy lidarrV1Proxy, IIndexerFactory indexerFactory, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) + public Lidarr(ILidarrV1Proxy lidarrV1Proxy, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) : base(appIndexerMapService, logger) { _lidarrV1Proxy = lidarrV1Proxy; - _indexerFactory = indexerFactory; _configFileProvider = configFileProvider; } @@ -65,42 +64,6 @@ namespace NzbDrone.Core.Applications.Lidarr throw new System.NotImplementedException(); } - public override void SyncIndexers() - { - // Pull Schema so we get the field mapping right - var schema = _lidarrV1Proxy.GetIndexerSchema(Settings); - var newznab = schema.Where(i => i.Implementation == "Newznab").First(); - var torznab = schema.Where(i => i.Implementation == "Torznab").First(); - - // Pull existing indexers from Lidarr - var indexers = _lidarrV1Proxy.GetIndexers(Settings); - - //Pull all local indexers (TODO only those that support movie categories.) - var prowlarrIndexers = _indexerFactory.Enabled(); - - //Pull mapping so we can check the mapping to see what already exists. - var indexerMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id); - - //Add new Indexers - foreach (var indexer in prowlarrIndexers) - { - //Don't add if it already exists in our mappings for this app (TODO should we check that it exists remote?) - if (indexerMappings.Any(x => x.IndexerId == indexer.Definition.Id)) - { - continue; - } - - var definition = (IndexerDefinition)indexer.Definition; - - var lidarrIndexer = BuildLidarrIndexer(definition, definition.Protocol == DownloadProtocol.Usenet ? newznab : torznab); - - var remoteIndexer = _lidarrV1Proxy.AddIndexer(lidarrIndexer, Settings); - _appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = definition.Id, RemoteIndexerId = remoteIndexer.Id }); - } - - //Delete Indexers that need Deleting. - } - private LidarrIndexer BuildLidarrIndexer(IndexerDefinition indexer, LidarrIndexer schema) { var lidarrIndexer = new LidarrIndexer diff --git a/src/NzbDrone.Core/Applications/Radarr/Radarr.cs b/src/NzbDrone.Core/Applications/Radarr/Radarr.cs index 2114a392f..2d6787fe4 100644 --- a/src/NzbDrone.Core/Applications/Radarr/Radarr.cs +++ b/src/NzbDrone.Core/Applications/Radarr/Radarr.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; @@ -13,14 +14,12 @@ namespace NzbDrone.Core.Applications.Radarr public override string Name => "Radarr"; private readonly IRadarrV3Proxy _radarrV3Proxy; - private readonly IIndexerFactory _indexerFactory; private readonly IConfigFileProvider _configFileProvider; - public Radarr(IRadarrV3Proxy radarrV3Proxy, IIndexerFactory indexerFactory, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) + public Radarr(IRadarrV3Proxy radarrV3Proxy, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) : base(appIndexerMapService, logger) { _radarrV3Proxy = radarrV3Proxy; - _indexerFactory = indexerFactory; _configFileProvider = configFileProvider; } @@ -65,42 +64,6 @@ namespace NzbDrone.Core.Applications.Radarr throw new System.NotImplementedException(); } - public override void SyncIndexers() - { - // Pull Schema so we get the field mapping right - var schema = _radarrV3Proxy.GetIndexerSchema(Settings); - var newznab = schema.Where(i => i.Implementation == "Newznab").First(); - var torznab = schema.Where(i => i.Implementation == "Torznab").First(); - - // Pull existing indexers from Radarr - var indexers = _radarrV3Proxy.GetIndexers(Settings); - - //Pull all local indexers (TODO only those that support movie categories.) - var prowlarrIndexers = _indexerFactory.Enabled(); - - //Pull mapping so we can check the mapping to see what already exists. - var indexerMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id); - - //Add new Indexers - foreach (var indexer in prowlarrIndexers) - { - //Don't add if it already exists in our mappings for this app (TODO should we check that it exists remote?) - if (indexerMappings.Any(x => x.IndexerId == indexer.Definition.Id)) - { - continue; - } - - var definition = (IndexerDefinition)indexer.Definition; - - var radarrIndexer = BuildRadarrIndexer(definition, definition.Protocol == DownloadProtocol.Usenet ? newznab : torznab); - - var remoteIndexer = _radarrV3Proxy.AddIndexer(radarrIndexer, Settings); - _appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = definition.Id, RemoteIndexerId = remoteIndexer.Id }); - } - - //Delete Indexers that need Deleting. - } - private RadarrIndexer BuildRadarrIndexer(IndexerDefinition indexer, RadarrIndexer schema) { var radarrIndexer = new RadarrIndexer diff --git a/src/NzbDrone.Core/Applications/Readarr/Readarr.cs b/src/NzbDrone.Core/Applications/Readarr/Readarr.cs index 278ec53f7..11da039f6 100644 --- a/src/NzbDrone.Core/Applications/Readarr/Readarr.cs +++ b/src/NzbDrone.Core/Applications/Readarr/Readarr.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; @@ -13,14 +14,12 @@ namespace NzbDrone.Core.Applications.Readarr public override string Name => "Readarr"; private readonly IReadarrV1Proxy _readarrV1Proxy; - private readonly IIndexerFactory _indexerFactory; private readonly IConfigFileProvider _configFileProvider; - public Readarr(IReadarrV1Proxy readarrV1Proxy, IIndexerFactory indexerFactory, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) + public Readarr(IReadarrV1Proxy readarrV1Proxy, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) : base(appIndexerMapService, logger) { _readarrV1Proxy = readarrV1Proxy; - _indexerFactory = indexerFactory; _configFileProvider = configFileProvider; } @@ -65,42 +64,6 @@ namespace NzbDrone.Core.Applications.Readarr throw new System.NotImplementedException(); } - public override void SyncIndexers() - { - // Pull Schema so we get the field mapping right - var schema = _readarrV1Proxy.GetIndexerSchema(Settings); - var newznab = schema.Where(i => i.Implementation == "Newznab").First(); - var torznab = schema.Where(i => i.Implementation == "Torznab").First(); - - // Pull existing indexers from Readarr - var indexers = _readarrV1Proxy.GetIndexers(Settings); - - //Pull all local indexers (TODO only those that support movie categories.) - var prowlarrIndexers = _indexerFactory.Enabled(); - - //Pull mapping so we can check the mapping to see what already exists. - var indexerMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id); - - //Add new Indexers - foreach (var indexer in prowlarrIndexers) - { - //Don't add if it already exists in our mappings for this app (TODO should we check that it exists remote?) - if (indexerMappings.Any(x => x.IndexerId == indexer.Definition.Id)) - { - continue; - } - - var definition = (IndexerDefinition)indexer.Definition; - - var readarrIndexer = BuildReadarrIndexer(definition, definition.Protocol == DownloadProtocol.Usenet ? newznab : torznab); - - var remoteIndexer = _readarrV1Proxy.AddIndexer(readarrIndexer, Settings); - _appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = definition.Id, RemoteIndexerId = remoteIndexer.Id }); - } - - //Delete Indexers that need Deleting. - } - private ReadarrIndexer BuildReadarrIndexer(IndexerDefinition indexer, ReadarrIndexer schema) { var readarrIndexer = new ReadarrIndexer diff --git a/src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs b/src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs index f0b734bfb..4123e05ff 100644 --- a/src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs +++ b/src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using FluentValidation.Results; @@ -13,14 +14,12 @@ namespace NzbDrone.Core.Applications.Sonarr public override string Name => "Sonarr"; private readonly ISonarrV3Proxy _sonarrV3Proxy; - private readonly IIndexerFactory _indexerFactory; private readonly IConfigFileProvider _configFileProvider; - public Sonarr(ISonarrV3Proxy sonarrV3Proxy, IIndexerFactory indexerFactory, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) + public Sonarr(ISonarrV3Proxy sonarrV3Proxy, IConfigFileProvider configFileProvider, IAppIndexerMapService appIndexerMapService, Logger logger) : base(appIndexerMapService, logger) { _sonarrV3Proxy = sonarrV3Proxy; - _indexerFactory = indexerFactory; _configFileProvider = configFileProvider; } @@ -65,42 +64,6 @@ namespace NzbDrone.Core.Applications.Sonarr throw new System.NotImplementedException(); } - public override void SyncIndexers() - { - // Pull Schema so we get the field mapping right - var schema = _sonarrV3Proxy.GetIndexerSchema(Settings); - var newznab = schema.Where(i => i.Implementation == "Newznab").First(); - var torznab = schema.Where(i => i.Implementation == "Torznab").First(); - - // Pull existing indexers from Sonarr - var indexers = _sonarrV3Proxy.GetIndexers(Settings); - - //Pull all local indexers (TODO only those that support movie categories.) - var prowlarrIndexers = _indexerFactory.Enabled(); - - //Pull mapping so we can check the mapping to see what already exists. - var indexerMappings = _appIndexerMapService.GetMappingsForApp(Definition.Id); - - //Add new Indexers - foreach (var indexer in prowlarrIndexers) - { - //Don't add if it already exists in our mappings for this app (TODO should we check that it exists remote?) - if (indexerMappings.Any(x => x.IndexerId == indexer.Definition.Id)) - { - continue; - } - - var definition = (IndexerDefinition)indexer.Definition; - - var sonarrIndexer = BuildSonarrIndexer(definition, definition.Protocol == DownloadProtocol.Usenet ? newznab : torznab); - - var remoteIndexer = _sonarrV3Proxy.AddIndexer(sonarrIndexer, Settings); - _appIndexerMapService.Insert(new AppIndexerMap { AppId = Definition.Id, IndexerId = definition.Id, RemoteIndexerId = remoteIndexer.Id }); - } - - //Delete Indexers that need Deleting. - } - private SonarrIndexer BuildSonarrIndexer(IndexerDefinition indexer, SonarrIndexer schema) { var sonarrIndexer = new SonarrIndexer