mirror of
https://github.com/fergalmoran/Readarr.git
synced 2026-01-18 14:44:11 +00:00
New: Add tag support to indexers
(cherry picked from commit c3d54b312ef18b837d54605ea78f1a263fd6900b)
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.DecisionEngine.Specifications.RssSync;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.DecisionEngineTests.RssSync
|
||||
{
|
||||
[TestFixture]
|
||||
public class IndexerTagSpecificationFixture : CoreTest<IndexerTagSpecification>
|
||||
{
|
||||
private IndexerTagSpecification _specification;
|
||||
|
||||
private RemoteBook _parseResultMulti;
|
||||
private IndexerDefinition _fakeIndexerDefinition;
|
||||
private Author _fakeAuthor;
|
||||
private Book _firstBook;
|
||||
private Book _secondBook;
|
||||
private ReleaseInfo _fakeRelease;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_fakeIndexerDefinition = new IndexerDefinition
|
||||
{
|
||||
Tags = new HashSet<int>()
|
||||
};
|
||||
|
||||
Mocker
|
||||
.GetMock<IIndexerRepository>()
|
||||
.Setup(m => m.Get(It.IsAny<int>()))
|
||||
.Returns(_fakeIndexerDefinition);
|
||||
|
||||
_specification = Mocker.Resolve<IndexerTagSpecification>();
|
||||
|
||||
_fakeAuthor = Builder<Author>.CreateNew()
|
||||
.With(c => c.Monitored = true)
|
||||
.With(c => c.Tags = new HashSet<int>())
|
||||
.Build();
|
||||
|
||||
_fakeRelease = new ReleaseInfo
|
||||
{
|
||||
IndexerId = 1
|
||||
};
|
||||
|
||||
_firstBook = new Book { Monitored = true };
|
||||
_secondBook = new Book { Monitored = true };
|
||||
|
||||
var doubleBookList = new List<Book> { _firstBook, _secondBook };
|
||||
|
||||
_parseResultMulti = new RemoteBook
|
||||
{
|
||||
Author = _fakeAuthor,
|
||||
Books = doubleBookList,
|
||||
Release = _fakeRelease
|
||||
};
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void indexer_and_author_without_tags_should_return_true()
|
||||
{
|
||||
_fakeIndexerDefinition.Tags = new HashSet<int>();
|
||||
_fakeAuthor.Tags = new HashSet<int>();
|
||||
|
||||
_specification.IsSatisfiedBy(_parseResultMulti, new BookSearchCriteria { MonitoredBooksOnly = true }).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void indexer_with_tags_author_without_tags_should_return_false()
|
||||
{
|
||||
_fakeIndexerDefinition.Tags = new HashSet<int> { 123 };
|
||||
_fakeAuthor.Tags = new HashSet<int>();
|
||||
|
||||
_specification.IsSatisfiedBy(_parseResultMulti, new BookSearchCriteria { MonitoredBooksOnly = true }).Accepted.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void indexer_without_tags_author_with_tags_should_return_true()
|
||||
{
|
||||
_fakeIndexerDefinition.Tags = new HashSet<int>();
|
||||
_fakeAuthor.Tags = new HashSet<int> { 123 };
|
||||
|
||||
_specification.IsSatisfiedBy(_parseResultMulti, new BookSearchCriteria { MonitoredBooksOnly = true }).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void indexer_with_tags_author_with_matching_tags_should_return_true()
|
||||
{
|
||||
_fakeIndexerDefinition.Tags = new HashSet<int> { 123, 456 };
|
||||
_fakeAuthor.Tags = new HashSet<int> { 123, 789 };
|
||||
|
||||
_specification.IsSatisfiedBy(_parseResultMulti, new BookSearchCriteria { MonitoredBooksOnly = true }).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void indexer_with_tags_author_with_different_tags_should_return_false()
|
||||
{
|
||||
_fakeIndexerDefinition.Tags = new HashSet<int> { 456 };
|
||||
_fakeAuthor.Tags = new HashSet<int> { 123, 789 };
|
||||
|
||||
_specification.IsSatisfiedBy(_parseResultMulti, new BookSearchCriteria { MonitoredBooksOnly = true }).Accepted.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.DecisionEngine;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.IndexerSearchTests
|
||||
{
|
||||
public class ReleaseSearchServiceFixture : CoreTest<ReleaseSearchService>
|
||||
{
|
||||
private Mock<IIndexer> _mockIndexer;
|
||||
private Author _author;
|
||||
private Book _firstBook;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_mockIndexer = Mocker.GetMock<IIndexer>();
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1 });
|
||||
_mockIndexer.SetupGet(s => s.SupportsSearch).Returns(true);
|
||||
|
||||
Mocker.GetMock<IIndexerFactory>()
|
||||
.Setup(s => s.AutomaticSearchEnabled(true))
|
||||
.Returns(new List<IIndexer> { _mockIndexer.Object });
|
||||
|
||||
Mocker.GetMock<IMakeDownloadDecision>()
|
||||
.Setup(s => s.GetSearchDecision(It.IsAny<List<Parser.Model.ReleaseInfo>>(), It.IsAny<SearchCriteriaBase>()))
|
||||
.Returns(new List<DownloadDecision>());
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.Build();
|
||||
|
||||
_firstBook = Builder<Book>.CreateNew()
|
||||
.With(e => e.Author = _author)
|
||||
.Build();
|
||||
|
||||
var edition = Builder<Edition>.CreateNew()
|
||||
.With(e => e.Book = _firstBook)
|
||||
.With(e => e.Monitored = true)
|
||||
.Build();
|
||||
|
||||
_firstBook.Editions = new List<Edition> { edition };
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
}
|
||||
|
||||
private List<SearchCriteriaBase> WatchForSearchCriteria()
|
||||
{
|
||||
var result = new List<SearchCriteriaBase>();
|
||||
|
||||
_mockIndexer.Setup(v => v.Fetch(It.IsAny<BookSearchCriteria>()))
|
||||
.Callback<BookSearchCriteria>(s => result.Add(s))
|
||||
.Returns(new List<Parser.Model.ReleaseInfo>());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerTags_AuthorNoTags_IndexerNotIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 3 }
|
||||
});
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerNoTags_AuthorTags_IndexerIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 3 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerAndAuthorTagsMatch_IndexerIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 1, 2, 3 }
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 3, 4, 5 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerAndAuthorTagsMismatch_IndexerNotIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 1, 2, 3 }
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 4, 5, 6 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(028)]
|
||||
public class add_indexer_tags : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Alter.Table("Indexers").AddColumn("Tags").AsString().Nullable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,8 +71,7 @@ namespace NzbDrone.Core.Datastore
|
||||
.Ignore(i => i.Enable)
|
||||
.Ignore(i => i.Protocol)
|
||||
.Ignore(i => i.SupportsRss)
|
||||
.Ignore(i => i.SupportsSearch)
|
||||
.Ignore(d => d.Tags);
|
||||
.Ignore(i => i.SupportsSearch);
|
||||
|
||||
Mapper.Entity<ImportListDefinition>("ImportLists").RegisterModel()
|
||||
.Ignore(x => x.ImplementationName)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync
|
||||
{
|
||||
public class IndexerTagSpecification : IDecisionEngineSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly IIndexerRepository _indexerRepository;
|
||||
|
||||
public IndexerTagSpecification(Logger logger, IIndexerRepository indexerRepository)
|
||||
{
|
||||
_logger = logger;
|
||||
_indexerRepository = indexerRepository;
|
||||
}
|
||||
|
||||
public SpecificationPriority Priority => SpecificationPriority.Default;
|
||||
public RejectionType Type => RejectionType.Permanent;
|
||||
|
||||
public virtual Decision IsSatisfiedBy(RemoteBook subject, SearchCriteriaBase searchCriteria)
|
||||
{
|
||||
// If indexer has tags, check that at least one of them is present on the author
|
||||
var indexerTags = _indexerRepository.Get(subject.Release.IndexerId).Tags;
|
||||
|
||||
if (indexerTags.Any() && indexerTags.Intersect(subject.Author.Tags).Empty())
|
||||
{
|
||||
_logger.Debug("Indexer {0} has tags. None of these are present on author {1}. Rejecting", subject.Release.Indexer, subject.Author);
|
||||
|
||||
return Decision.Reject("Author tags do not match any of the indexer tags");
|
||||
}
|
||||
|
||||
return Decision.Accept();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
|
||||
{
|
||||
using (var mapper = _database.OpenConnection())
|
||||
{
|
||||
var usedTags = new[] { "Authors", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists" }
|
||||
.SelectMany(v => GetUsedTags(v, mapper))
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
var usedTags = new[] { "Authors", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers" }
|
||||
.SelectMany(v => GetUsedTags(v, mapper))
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
|
||||
if (usedTags.Any())
|
||||
{
|
||||
|
||||
@@ -112,6 +112,9 @@ namespace NzbDrone.Core.IndexerSearch
|
||||
_indexerFactory.InteractiveSearchEnabled() :
|
||||
_indexerFactory.AutomaticSearchEnabled();
|
||||
|
||||
// Filter indexers to untagged indexers and indexers with intersecting tags
|
||||
indexers = indexers.Where(i => i.Definition.Tags.Empty() || i.Definition.Tags.Intersect(criteriaBase.Author.Tags).Any()).ToList();
|
||||
|
||||
var reports = new List<ReleaseInfo>();
|
||||
|
||||
_logger.ProgressInfo("Searching indexers for {0}. {1} active indexers", criteriaBase, indexers.Count);
|
||||
|
||||
@@ -375,6 +375,7 @@
|
||||
"IndexerSettings": "Indexer Settings",
|
||||
"IndexerStatusCheckAllClientMessage": "All indexers are unavailable due to failures",
|
||||
"IndexerStatusCheckSingleClientMessage": "Indexers unavailable due to failures: {0}",
|
||||
"IndexerTagsHelpText": "Only use this indexer for authors with at least one matching tag. Leave blank to use with all authors.",
|
||||
"Indexers": "Indexers",
|
||||
"IndexersSettingsSummary": "Indexers and release restrictions",
|
||||
"InstanceName": "Instance Name",
|
||||
|
||||
@@ -12,13 +12,14 @@ namespace NzbDrone.Core.Tags
|
||||
public List<int> RestrictionIds { get; set; }
|
||||
public List<int> DelayProfileIds { get; set; }
|
||||
public List<int> ImportListIds { get; set; }
|
||||
public List<int> IndexerIds { get; set; }
|
||||
public List<int> RootFolderIds { get; set; }
|
||||
|
||||
public bool InUse
|
||||
{
|
||||
get
|
||||
{
|
||||
return AuthorIds.Any() || NotificationIds.Any() || RestrictionIds.Any() || DelayProfileIds.Any() || ImportListIds.Any() || RootFolderIds.Any();
|
||||
return AuthorIds.Any() || NotificationIds.Any() || RestrictionIds.Any() || DelayProfileIds.Any() || ImportListIds.Any() || IndexerIds.Any() || RootFolderIds.Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Linq;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.ImportLists;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Notifications;
|
||||
using NzbDrone.Core.Profiles.Delay;
|
||||
@@ -32,6 +33,7 @@ namespace NzbDrone.Core.Tags
|
||||
private readonly INotificationFactory _notificationFactory;
|
||||
private readonly IReleaseProfileService _releaseProfileService;
|
||||
private readonly IAuthorService _authorService;
|
||||
private readonly IIndexerFactory _indexerService;
|
||||
private readonly IRootFolderService _rootFolderService;
|
||||
|
||||
public TagService(ITagRepository repo,
|
||||
@@ -41,6 +43,7 @@ namespace NzbDrone.Core.Tags
|
||||
INotificationFactory notificationFactory,
|
||||
IReleaseProfileService releaseProfileService,
|
||||
IAuthorService authorService,
|
||||
IIndexerFactory indexerService,
|
||||
IRootFolderService rootFolderService)
|
||||
{
|
||||
_repo = repo;
|
||||
@@ -50,6 +53,7 @@ namespace NzbDrone.Core.Tags
|
||||
_notificationFactory = notificationFactory;
|
||||
_releaseProfileService = releaseProfileService;
|
||||
_authorService = authorService;
|
||||
_indexerService = indexerService;
|
||||
_rootFolderService = rootFolderService;
|
||||
}
|
||||
|
||||
@@ -78,6 +82,7 @@ namespace NzbDrone.Core.Tags
|
||||
var notifications = _notificationFactory.AllForTag(tagId);
|
||||
var restrictions = _releaseProfileService.AllForTag(tagId);
|
||||
var author = _authorService.AllForTag(tagId);
|
||||
var indexers = _indexerService.AllForTag(tagId);
|
||||
var rootFolders = _rootFolderService.AllForTag(tagId);
|
||||
|
||||
return new TagDetails
|
||||
@@ -89,6 +94,7 @@ namespace NzbDrone.Core.Tags
|
||||
NotificationIds = notifications.Select(c => c.Id).ToList(),
|
||||
RestrictionIds = restrictions.Select(c => c.Id).ToList(),
|
||||
AuthorIds = author.Select(c => c.Id).ToList(),
|
||||
IndexerIds = indexers.Select(c => c.Id).ToList(),
|
||||
RootFolderIds = rootFolders.Select(c => c.Id).ToList()
|
||||
};
|
||||
}
|
||||
@@ -101,6 +107,7 @@ namespace NzbDrone.Core.Tags
|
||||
var notifications = _notificationFactory.All();
|
||||
var restrictions = _releaseProfileService.All();
|
||||
var authors = _authorService.GetAllAuthors();
|
||||
var indexers = _indexerService.All();
|
||||
var rootFolders = _rootFolderService.All();
|
||||
|
||||
var details = new List<TagDetails>();
|
||||
@@ -116,6 +123,7 @@ namespace NzbDrone.Core.Tags
|
||||
NotificationIds = notifications.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
||||
RestrictionIds = restrictions.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
||||
AuthorIds = authors.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
||||
IndexerIds = indexers.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
|
||||
RootFolderIds = rootFolders.Where(c => c.DefaultTags.Contains(tag.Id)).Select(c => c.Id).ToList()
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Readarr.Api.V1.Tags
|
||||
public List<int> ImportListIds { get; set; }
|
||||
public List<int> NotificationIds { get; set; }
|
||||
public List<int> RestrictionIds { get; set; }
|
||||
public List<int> IndexerIds { get; set; }
|
||||
public List<int> AuthorIds { get; set; }
|
||||
}
|
||||
|
||||
@@ -32,6 +33,7 @@ namespace Readarr.Api.V1.Tags
|
||||
ImportListIds = model.ImportListIds,
|
||||
NotificationIds = model.NotificationIds,
|
||||
RestrictionIds = model.RestrictionIds,
|
||||
IndexerIds = model.IndexerIds,
|
||||
AuthorIds = model.AuthorIds
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user