From b553d8aef6dbb6211f7743167b9f982fdb733fac Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Mon, 6 Aug 2018 19:35:26 +0200 Subject: [PATCH] Fixed: Saving IndexerSettings into DB which confuses the datamapper. Fixes #2945 --- .../TorrentSeedingSpecificationFixture.cs | 15 ++++++++--- .../AugmentWithReleaseInfoFixture.cs | 26 ++++++++++++++++--- .../RequiredIndexerFlagsSpecification.cs | 26 +++++++++++++------ .../TorrentSeedingSpecification.cs | 19 ++++++++++---- src/NzbDrone.Core/Indexers/IndexerBase.cs | 1 - src/NzbDrone.Core/Indexers/IndexerFactory.cs | 9 ++++--- .../Parser/Augmenters/AugmentWithHistory.cs | 4 +-- .../Augmenters/AugmentWithReleaseInfo.cs | 26 ++++++++++++++----- src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs | 1 - 9 files changed, 94 insertions(+), 33 deletions(-) diff --git a/src/NzbDrone.Core.Test/DecisionEngineTests/Search/TorrentSeedingSpecificationFixture.cs b/src/NzbDrone.Core.Test/DecisionEngineTests/Search/TorrentSeedingSpecificationFixture.cs index 984e656e0..c6531b372 100644 --- a/src/NzbDrone.Core.Test/DecisionEngineTests/Search/TorrentSeedingSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/DecisionEngineTests/Search/TorrentSeedingSpecificationFixture.cs @@ -33,9 +33,18 @@ namespace NzbDrone.Core.Test.DecisionEngineTests.Search IndexerId = 1, Title = "Series.Title.S01.720p.BluRay.X264-RlsGrp", Seeders = 0, - IndexerSettings = new TorrentRssIndexerSettings {MinimumSeeders = 5} + //IndexerSettings = new TorrentRssIndexerSettings {MinimumSeeders = 5} } }; + + _indexerDefinition = new IndexerDefinition + { + Settings = new TorrentRssIndexerSettings { MinimumSeeders = 5 } + }; + + Mocker.GetMock() + .Setup(v => v.Get(1)) + .Returns(_indexerDefinition); } private void GivenReleaseSeeders(int? seeders) @@ -56,7 +65,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests.Search } // These tests are not needed anymore, since indexer settings are saved on the release itself! - /* + [Test] public void should_return_true_if_indexer_not_specified() { @@ -73,7 +82,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests.Search .Callback(i => { throw new ModelNotFoundException(typeof(IndexerDefinition), i); }); Subject.IsSatisfiedBy(_remoteMovie, null).Accepted.Should().BeTrue(); - }*/ + } [Test] public void should_return_true_if_seeds_unknown() diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithReleaseInfoFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithReleaseInfoFixture.cs index 45187c9db..65d8d9ae3 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithReleaseInfoFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithReleaseInfoFixture.cs @@ -3,7 +3,9 @@ using System.Collections.Generic; using System.Linq; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers.Rarbg; +using NzbDrone.Core.Indexers.TorrentRss; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Augmenters; using NzbDrone.Core.Parser.Model; @@ -13,15 +15,31 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests.AugmentersTests [TestFixture] public class AugmentWithReleaseInfoFixture : AugmentMovieInfoFixture { + private IndexerDefinition _indexerDefinition; + + [SetUp] + public void Setup() + { + + } + private ReleaseInfo ReleaseInfoWithLanguages(params Language[] languages) { + _indexerDefinition = new IndexerDefinition + { + Settings = new RarbgSettings { MultiLanguages = languages.ToList().Select(l => (int) l) } + }; + + Mocker.GetMock() + .Setup(v => v.Get(1)) + .Returns(_indexerDefinition); + return new ReleaseInfo { - IndexerSettings = new RarbgSettings - { - MultiLanguages = languages.ToList().Select(l => (int) l) - } + IndexerId = 1 }; + + } [Test] diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/RequiredIndexerFlagsSpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/RequiredIndexerFlagsSpecification.cs index d13ead54a..0f421b74c 100644 --- a/src/NzbDrone.Core/DecisionEngine/Specifications/RequiredIndexerFlagsSpecification.cs +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/RequiredIndexerFlagsSpecification.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using NLog; using NzbDrone.Core.Datastore; @@ -25,24 +26,33 @@ namespace NzbDrone.Core.DecisionEngine.Specifications.Search { var torrentInfo = subject.Release; - if (torrentInfo == null || torrentInfo.IndexerSettings == null) + + IIndexerSettings indexerSettings = null; + try { + indexerSettings = _indexerFactory.Get(subject.Release.IndexerId)?.Settings as IIndexerSettings; + } + catch (Exception e) + { + _logger.Debug("Indexer with id {0} does not exist, skipping required indexer flags specs.", subject.Release.IndexerId); + } + + if (torrentInfo == null || indexerSettings == null) { return Decision.Accept(); } - var torrentIndexerSettings = torrentInfo.IndexerSettings as ITorrentIndexerSettings; - - if (torrentIndexerSettings != null) + if (indexerSettings is ITorrentIndexerSettings torrentIndexerSettings) { var requiredFlags = torrentIndexerSettings.RequiredFlags; var requiredFlag = (IndexerFlags) 0; - if (requiredFlags == null || requiredFlags.Count() == 0) + var enumerable = requiredFlags.ToList(); + if (requiredFlags == null || !enumerable.Any()) { return Decision.Accept(); } - - foreach (var flag in requiredFlags) + + foreach (var flag in enumerable) { if (torrentInfo.IndexerFlags.HasFlag((IndexerFlags)flag)) { @@ -50,7 +60,7 @@ namespace NzbDrone.Core.DecisionEngine.Specifications.Search } requiredFlag |= (IndexerFlags)flag; } - + _logger.Debug("None of the required indexer flags {0} where found. Found flags: {1}", requiredFlag, torrentInfo.IndexerFlags); return Decision.Reject("None of the required indexer flags {0} where found. Found flags: {1}", requiredFlag, torrentInfo.IndexerFlags); } diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/TorrentSeedingSpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/TorrentSeedingSpecification.cs index 58bf625d1..be9fecd48 100644 --- a/src/NzbDrone.Core/DecisionEngine/Specifications/TorrentSeedingSpecification.cs +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/TorrentSeedingSpecification.cs @@ -1,3 +1,4 @@ +using System; using NLog; using NzbDrone.Core.Datastore; using NzbDrone.Core.Indexers; @@ -19,19 +20,27 @@ namespace NzbDrone.Core.DecisionEngine.Specifications.Search //public SpecificationPriority Priority => SpecificationPriority.Default; public RejectionType Type => RejectionType.Permanent; - + public Decision IsSatisfiedBy(RemoteMovie subject, SearchCriteriaBase searchCriteria) { var torrentInfo = subject.Release as TorrentInfo; - if (torrentInfo == null || torrentInfo.IndexerSettings == null) + IIndexerSettings indexerSettings = null; + try { + indexerSettings = _indexerFactory.Get(subject.Release.IndexerId)?.Settings as IIndexerSettings; + } + catch (Exception e) + { + _logger.Debug("Indexer with id {0} does not exist, skipping minimum seeder checks.", subject.Release.IndexerId); + } + + + if (torrentInfo == null || indexerSettings == null) { return Decision.Accept(); } - var torrentIndexerSettings = torrentInfo.IndexerSettings as ITorrentIndexerSettings; - - if (torrentIndexerSettings != null) + if (indexerSettings is ITorrentIndexerSettings torrentIndexerSettings) { var minimumSeeders = torrentIndexerSettings.MinimumSeeders; diff --git a/src/NzbDrone.Core/Indexers/IndexerBase.cs b/src/NzbDrone.Core/Indexers/IndexerBase.cs index 00c7bdb73..28dde1ca6 100644 --- a/src/NzbDrone.Core/Indexers/IndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/IndexerBase.cs @@ -69,7 +69,6 @@ namespace NzbDrone.Core.Indexers { c.IndexerId = Definition.Id; c.Indexer = Definition.Name; - c.IndexerSettings = Definition.Settings as IIndexerSettings; c.DownloadProtocol = Protocol; }); diff --git a/src/NzbDrone.Core/Indexers/IndexerFactory.cs b/src/NzbDrone.Core/Indexers/IndexerFactory.cs index c4903c9c7..7df0ab67d 100644 --- a/src/NzbDrone.Core/Indexers/IndexerFactory.cs +++ b/src/NzbDrone.Core/Indexers/IndexerFactory.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Composition; @@ -21,7 +22,7 @@ namespace NzbDrone.Core.Indexers public IndexerFactory(IIndexerStatusService indexerStatusService, IIndexerRepository providerRepository, IEnumerable providers, - IContainer container, + IContainer container, IEventAggregator eventAggregator, Logger logger) : base(providerRepository, providers, container, eventAggregator, logger) @@ -84,5 +85,7 @@ namespace NzbDrone.Core.Indexers yield return indexer; } } + + } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/Parser/Augmenters/AugmentWithHistory.cs b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithHistory.cs index edf06aa3d..95f3cb88d 100644 --- a/src/NzbDrone.Core/Parser/Augmenters/AugmentWithHistory.cs +++ b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithHistory.cs @@ -40,8 +40,8 @@ namespace NzbDrone.Core.Parser.Augmenters if (int.TryParse(history.Data.GetValueOrDefault("indexerId"), out var indexerId)) { - var indexerSettings = _indexerFactory.Get(indexerId).Settings as IIndexerSettings; - releaseInfo.IndexerSettings = indexerSettings; + //var indexerSettings = _indexerFactory.Get(indexerId).Settings as IIndexerSettings; + releaseInfo.IndexerId = indexerId; } if (int.TryParse(history.Data.GetValueOrDefault("size"), out var size)) diff --git a/src/NzbDrone.Core/Parser/Augmenters/AugmentWithReleaseInfo.cs b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithReleaseInfo.cs index 5c2882c59..bedf8ab20 100644 --- a/src/NzbDrone.Core/Parser/Augmenters/AugmentWithReleaseInfo.cs +++ b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithReleaseInfo.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Indexers; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.Parser.Augmenters @@ -8,6 +9,13 @@ namespace NzbDrone.Core.Parser.Augmenters public class AugmentWithReleaseInfo : IAugmentParsedMovieInfo { + private readonly IIndexerFactory _indexerFactory; + + public AugmentWithReleaseInfo(IIndexerFactory indexerFactory) + { + _indexerFactory = indexerFactory; + } + public Type HelperType { get @@ -18,17 +26,23 @@ namespace NzbDrone.Core.Parser.Augmenters public ParsedMovieInfo AugmentMovieInfo(ParsedMovieInfo movieInfo, object helper) { - var releaseInfo = helper as ReleaseInfo; - - if (releaseInfo != null) + if (helper is ReleaseInfo releaseInfo) { - // First, let's augment the language! + + IIndexerSettings indexerSettings = null; + try { + indexerSettings = _indexerFactory.Get(releaseInfo.IndexerId)?.Settings as IIndexerSettings; + } + catch (Exception e) + { + //_logger.Debug("Indexer with id {0} does not exist, skipping minimum seeder checks.", subject.Release.IndexerId); + } // First, let's augment the language! var languageTitle = movieInfo.SimpleReleaseTitle; if (movieInfo.MovieTitle.IsNotNullOrWhiteSpace()) { - if (languageTitle.ToLower().Contains("multi") && releaseInfo?.IndexerSettings?.MultiLanguages?.Any() == true) + if (languageTitle.ToLower().Contains("multi") && indexerSettings?.MultiLanguages?.Any() == true) { - foreach (var i in releaseInfo.IndexerSettings.MultiLanguages) + foreach (var i in indexerSettings.MultiLanguages) { var language = (Language) i; if (!movieInfo.Languages.Contains(language)) diff --git a/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs b/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs index 9f1c5cb18..6d0d61464 100644 --- a/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs +++ b/src/NzbDrone.Core/Parser/Model/ReleaseInfo.cs @@ -13,7 +13,6 @@ namespace NzbDrone.Core.Parser.Model public string InfoUrl { get; set; } public string CommentUrl { get; set; } public int IndexerId { get; set; } - public IIndexerSettings IndexerSettings { get; set; } public string Indexer { get; set; } public DownloadProtocol DownloadProtocol { get; set; } public int TvdbId { get; set; }