mirror of
https://github.com/Readarr/Readarr
synced 2026-02-26 08:31:49 +01:00
New: Refresh books added to match existing files
Fixes #1146 (cherry picked from commit 40c99fb67587b5890d4d2a3a3af1d61034598e1f)
This commit is contained in:
parent
d8d09c2517
commit
4712fedb0e
3 changed files with 46 additions and 3 deletions
21
src/NzbDrone.Core/Books/Commands/BulkRefreshBookCommand.cs
Normal file
21
src/NzbDrone.Core/Books/Commands/BulkRefreshBookCommand.cs
Normal file
|
|
@ -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<int> bookIds)
|
||||
{
|
||||
BookIds = bookIds;
|
||||
}
|
||||
|
||||
public List<int> BookIds { get; set; }
|
||||
|
||||
public override bool SendUpdatesToClient => true;
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,8 @@ public interface IRefreshBookService
|
|||
|
||||
public class RefreshBookService : RefreshEntityServiceBase<Book, Edition>,
|
||||
IRefreshBookService,
|
||||
IExecute<RefreshBookCommand>
|
||||
IExecute<RefreshBookCommand>,
|
||||
IExecute<BulkRefreshBookCommand>
|
||||
{
|
||||
private readonly IBookService _bookService;
|
||||
private readonly IAuthorService _authorService;
|
||||
|
|
@ -374,6 +375,16 @@ public bool RefreshBookInfo(Book book)
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ public List<ImportResult> Import(List<ImportDecision<LocalBook>> decisions, bool
|
|||
var allImportedTrackFiles = new List<BookFile>();
|
||||
var allOldTrackFiles = new List<BookFile>();
|
||||
var addedAuthors = new List<Author>();
|
||||
var addedBooks = new List<Book>();
|
||||
|
||||
var bookDecisions = decisions.Where(e => e.Item.Book != null && e.Approved)
|
||||
.GroupBy(e => e.Item.Book.ForeignBookId).ToList();
|
||||
|
|
@ -98,7 +99,7 @@ public List<ImportResult> Import(List<ImportDecision<LocalBook>> decisions, bool
|
|||
continue;
|
||||
}
|
||||
|
||||
var book = EnsureBookAdded(decisionList);
|
||||
var book = EnsureBookAdded(decisionList, addedBooks);
|
||||
|
||||
if (book == null)
|
||||
{
|
||||
|
|
@ -297,6 +298,15 @@ public List<ImportResult> Import(List<ImportDecision<LocalBook>> decisions, bool
|
|||
_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 @@ private Author EnsureAuthorAdded(List<ImportDecision<LocalBook>> decisions, List
|
|||
return author;
|
||||
}
|
||||
|
||||
private Book EnsureBookAdded(List<ImportDecision<LocalBook>> decisions)
|
||||
private Book EnsureBookAdded(List<ImportDecision<LocalBook>> decisions, List<Book> addedBooks)
|
||||
{
|
||||
var book = decisions.First().Item.Book;
|
||||
|
||||
|
|
@ -385,6 +395,7 @@ private Book EnsureBookAdded(List<ImportDecision<LocalBook>> decisions)
|
|||
book.Monitored = book.Author.Value.Monitored;
|
||||
book.Added = DateTime.UtcNow;
|
||||
_bookService.InsertMany(new List<Book> { book });
|
||||
addedBooks.Add(book);
|
||||
|
||||
book.Editions.Value.ForEach(x => x.BookId = book.Id);
|
||||
_editionService.InsertMany(book.Editions.Value);
|
||||
|
|
|
|||
Loading…
Reference in a new issue