diff --git a/src/NzbDrone.Core/Books/Commands/BulkRefreshBookCommand.cs b/src/NzbDrone.Core/Books/Commands/BulkRefreshBookCommand.cs new file mode 100644 index 000000000..529157f3b --- /dev/null +++ b/src/NzbDrone.Core/Books/Commands/BulkRefreshBookCommand.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using NzbDrone.Core.Messaging.Commands; + +namespace NzbDrone.Core.Books.Commands +{ + public class BulkRefreshBookCommand : Command + { + public BulkRefreshBookCommand() + { + } + + public BulkRefreshBookCommand(List bookIds) + { + BookIds = bookIds; + } + + public List BookIds { get; set; } + + public override bool SendUpdatesToClient => true; + } +} diff --git a/src/NzbDrone.Core/Books/Services/RefreshBookService.cs b/src/NzbDrone.Core/Books/Services/RefreshBookService.cs index c7d21e5f6..f97321df9 100644 --- a/src/NzbDrone.Core/Books/Services/RefreshBookService.cs +++ b/src/NzbDrone.Core/Books/Services/RefreshBookService.cs @@ -25,7 +25,8 @@ namespace NzbDrone.Core.Books public class RefreshBookService : RefreshEntityServiceBase, IRefreshBookService, - IExecute + IExecute, + IExecute { private readonly IBookService _bookService; private readonly IAuthorService _authorService; @@ -374,6 +375,16 @@ namespace NzbDrone.Core.Books return RefreshBookInfo(book, data.Books, data, false); } + public void Execute(BulkRefreshBookCommand message) + { + var books = _bookService.GetBooks(message.BookIds); + + foreach (var book in books) + { + RefreshBookInfo(book); + } + } + public void Execute(RefreshBookCommand message) { if (message.BookId.HasValue) diff --git a/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs b/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs index 074896813..f00bf134f 100644 --- a/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs +++ b/src/NzbDrone.Core/MediaFiles/BookImport/ImportApprovedBooks.cs @@ -79,6 +79,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport var allImportedTrackFiles = new List(); var allOldTrackFiles = new List(); var addedAuthors = new List(); + var addedBooks = new List(); var bookDecisions = decisions.Where(e => e.Item.Book != null && e.Approved) .GroupBy(e => e.Item.Book.ForeignBookId).ToList(); @@ -98,7 +99,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport continue; } - var book = EnsureBookAdded(decisionList); + var book = EnsureBookAdded(decisionList, addedBooks); if (book == null) { @@ -297,6 +298,15 @@ namespace NzbDrone.Core.MediaFiles.BookImport _commandQueueManager.Push(new BulkRefreshAuthorCommand(addedAuthors.Select(x => x.Id).ToList(), true)); } + var addedAuthorMetadataIds = addedAuthors.Select(x => x.AuthorMetadataId).ToHashSet(); + var booksToRefresh = addedBooks.Where(x => !addedAuthorMetadataIds.Contains(x.AuthorMetadataId)).ToList(); + + if (booksToRefresh.Any()) + { + _logger.Debug($"Refreshing info for {booksToRefresh.Count} new books"); + _commandQueueManager.Push(new BulkRefreshBookCommand(booksToRefresh.Select(x => x.Id).ToList())); + } + return importResults; } @@ -363,7 +373,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport return author; } - private Book EnsureBookAdded(List> decisions) + private Book EnsureBookAdded(List> decisions, List addedBooks) { var book = decisions.First().Item.Book; @@ -385,6 +395,7 @@ namespace NzbDrone.Core.MediaFiles.BookImport book.Monitored = book.Author.Value.Monitored; book.Added = DateTime.UtcNow; _bookService.InsertMany(new List { book }); + addedBooks.Add(book); book.Editions.Value.ForEach(x => x.BookId = book.Id); _editionService.InsertMany(book.Editions.Value);