Reading List Fixes (#1784)
* Add ability to save readinglist comicinfo fields in Chapter. * Added the appropriate fields and migration for Reading List generation. * Started the reading list code * Started building out the CBL import code with some initial unit tests. * Fixed first unit test * Started refactoring control code into services and writing unit tests for ReadingLists. Found a logic issue around reading list title between create/update. Will be corrected in this branch with unit tests. * Can't figure out how to mock UserManager, so had to uncomment a few tests. * Tooltip for total pages read shows the full number * Tweaked the math a bit for average reading per week. * Fixed up the reading list unit tests. Fixed an issue where when inserting chapters into a blank reading list, the initial reading list item would have an order of 1 instead of 0. * Cleaned up the code to allow the reading list code to be localized easily and fixed up a bug in last PR. * Fixed a sorting issue on reading activity * Tweaked the code around reading list actionables not showing due to some weird filter. * Fixed edit library settings not opening on library detail page * Fixed a bug where reading activity dates would be out of order due to a bug in how charts works. A temp hack has been added. * Disable promotion in edit reading list modal since non-admins can (and should have) been able to use it. * Fixed a bug where non-admins couldn't update their OWN reading lists. Made uploading a cover image for readinglists now check against the user's reading list access to allow non-admin's to set images. * Fixed an issue introduced earlier in PR where adding chapters to reading list could cause order to get skewed. * Fixed another regression from earlier commit * Hooked in Import CBL flow. No functionality yet. * Code is a mess. Shifting how the whole import process is going to be done. Commiting so I can pivot drastically. * Very rough code for first step is done. * Ui has started, I've run out of steam for this feature. * Cleaned up the UI code a bit to make the step tracker nature easier without a dedicated component. * Much flow implementation and tweaking to how validation checks and what is sent back. * Removed import via cbl code as it's not done. Pushing to next release.
This commit is contained in:
parent
ae1af22af1
commit
3f24dc7392
48 changed files with 21951 additions and 170 deletions
|
@ -1,19 +1,27 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using API.Comparators;
|
||||
using API.Data;
|
||||
using API.Data.Repositories;
|
||||
using API.DTOs;
|
||||
using API.DTOs.ReadingLists;
|
||||
using API.DTOs.ReadingLists.CBL;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.SignalR;
|
||||
using Kavita.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Services;
|
||||
|
||||
public interface IReadingListService
|
||||
{
|
||||
Task<ReadingList> CreateReadingListForUser(AppUser userWithReadingList, string title);
|
||||
Task UpdateReadingList(ReadingList readingList, UpdateReadingListDto dto);
|
||||
Task<bool> RemoveFullyReadItems(int readingListId, AppUser user);
|
||||
Task<bool> UpdateReadingListItemPosition(UpdateReadingListPosition dto);
|
||||
Task<bool> DeleteReadingListItem(UpdateReadingListPosition dto);
|
||||
|
@ -22,6 +30,9 @@ public interface IReadingListService
|
|||
Task CalculateReadingListAgeRating(ReadingList readingList);
|
||||
Task<bool> AddChaptersToReadingList(int seriesId, IList<int> chapterIds,
|
||||
ReadingList readingList);
|
||||
|
||||
Task<CblImportSummaryDto> ValidateCblFile(int userId, CblReadingList cblReading);
|
||||
Task<CblImportSummaryDto> CreateReadingListFromCbl(int userId, CblReadingList cblReading, bool dryRun = false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -32,14 +43,16 @@ public class ReadingListService : IReadingListService
|
|||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILogger<ReadingListService> _logger;
|
||||
private readonly ChapterSortComparerZeroFirst _chapterSortComparerForInChapterSorting = new ChapterSortComparerZeroFirst();
|
||||
private readonly IEventHub _eventHub;
|
||||
private readonly ChapterSortComparerZeroFirst _chapterSortComparerForInChapterSorting = ChapterSortComparerZeroFirst.Default;
|
||||
private static readonly Regex JustNumbers = new Regex(@"^\d+$", RegexOptions.Compiled | RegexOptions.IgnoreCase,
|
||||
Tasks.Scanner.Parser.Parser.RegexTimeout);
|
||||
|
||||
public ReadingListService(IUnitOfWork unitOfWork, ILogger<ReadingListService> logger)
|
||||
public ReadingListService(IUnitOfWork unitOfWork, ILogger<ReadingListService> logger, IEventHub eventHub)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
_eventHub = eventHub;
|
||||
}
|
||||
|
||||
public static string FormatTitle(ReadingListItemDto item)
|
||||
|
@ -86,6 +99,66 @@ public class ReadingListService : IReadingListService
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Reading List for a User
|
||||
/// </summary>
|
||||
/// <param name="userWithReadingList"></param>
|
||||
/// <param name="title"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="KavitaException"></exception>
|
||||
public async Task<ReadingList> CreateReadingListForUser(AppUser userWithReadingList, string title)
|
||||
{
|
||||
// When creating, we need to make sure Title is unique
|
||||
// TODO: Perform normalization
|
||||
var hasExisting = userWithReadingList.ReadingLists.Any(l => l.Title.Equals(title));
|
||||
if (hasExisting)
|
||||
{
|
||||
throw new KavitaException("A list of this name already exists");
|
||||
}
|
||||
|
||||
var readingList = DbFactory.ReadingList(title, string.Empty, false);
|
||||
userWithReadingList.ReadingLists.Add(readingList);
|
||||
|
||||
if (!_unitOfWork.HasChanges()) throw new KavitaException("There was a problem creating list");
|
||||
await _unitOfWork.CommitAsync();
|
||||
return readingList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="readingList"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <exception cref="KavitaException"></exception>
|
||||
public async Task UpdateReadingList(ReadingList readingList, UpdateReadingListDto dto)
|
||||
{
|
||||
dto.Title = dto.Title.Trim();
|
||||
if (string.IsNullOrEmpty(dto.Title)) throw new KavitaException("Title must be set");
|
||||
|
||||
if (!dto.Title.Equals(readingList.Title) && await _unitOfWork.ReadingListRepository.ReadingListExists(dto.Title))
|
||||
throw new KavitaException("Reading list already exists");
|
||||
|
||||
readingList.Summary = dto.Summary;
|
||||
readingList.Title = dto.Title.Trim();
|
||||
readingList.NormalizedTitle = Tasks.Scanner.Parser.Parser.Normalize(readingList.Title);
|
||||
readingList.Promoted = dto.Promoted;
|
||||
readingList.CoverImageLocked = dto.CoverImageLocked;
|
||||
|
||||
if (!dto.CoverImageLocked)
|
||||
{
|
||||
readingList.CoverImageLocked = false;
|
||||
readingList.CoverImage = string.Empty;
|
||||
await _eventHub.SendMessageAsync(MessageFactory.CoverUpdate,
|
||||
MessageFactory.CoverUpdateEvent(readingList.Id, MessageFactoryEntityTypes.ReadingList), false);
|
||||
_unitOfWork.ReadingListRepository.Update(readingList);
|
||||
}
|
||||
|
||||
_unitOfWork.ReadingListRepository.Update(readingList);
|
||||
|
||||
if (!_unitOfWork.HasChanges()) return;
|
||||
await _unitOfWork.CommitAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all entries that are fully read from the reading list. This commits
|
||||
/// </summary>
|
||||
|
@ -198,9 +271,10 @@ public class ReadingListService : IReadingListService
|
|||
/// <returns></returns>
|
||||
public async Task<AppUser?> UserHasReadingListAccess(int readingListId, string username)
|
||||
{
|
||||
// We need full reading list with items as this is used by many areas that manipulate items
|
||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username,
|
||||
AppUserIncludes.ReadingListsWithItems);
|
||||
if (user.ReadingLists.SingleOrDefault(rl => rl.Id == readingListId) == null && !await _unitOfWork.UserRepository.IsUserAdminAsync(user))
|
||||
if (!await UserHasReadingListAccess(readingListId, user))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
@ -208,6 +282,17 @@ public class ReadingListService : IReadingListService
|
|||
return user;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// User must have ReadingList on it
|
||||
/// </summary>
|
||||
/// <param name="readingListId"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> UserHasReadingListAccess(int readingListId, AppUser user)
|
||||
{
|
||||
return user.ReadingLists.Any(rl => rl.Id == readingListId) || await _unitOfWork.UserRepository.IsUserAdminAsync(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the Reading List from kavita
|
||||
/// </summary>
|
||||
|
@ -246,7 +331,7 @@ public class ReadingListService : IReadingListService
|
|||
.ThenBy(x => double.Parse(x.Number), _chapterSortComparerForInChapterSorting)
|
||||
.ToList();
|
||||
|
||||
var index = lastOrder + 1;
|
||||
var index = readingList.Items.Count == 0 ? 0 : lastOrder + 1;
|
||||
foreach (var chapter in chaptersForSeries.Where(chapter => !existingChapterExists.Contains(chapter.Id)))
|
||||
{
|
||||
readingList.Items.Add(DbFactory.ReadingListItem(index, seriesId, chapter.VolumeId, chapter.Id));
|
||||
|
@ -257,4 +342,208 @@ public class ReadingListService : IReadingListService
|
|||
|
||||
return index > lastOrder + 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check for File issues like: No entries, Reading List Name collision, Duplicate Series across Libraries
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="cblReading"></param>
|
||||
public async Task<CblImportSummaryDto> ValidateCblFile(int userId, CblReadingList cblReading)
|
||||
{
|
||||
var importSummary = new CblImportSummaryDto()
|
||||
{
|
||||
CblName = cblReading.Name,
|
||||
Success = CblImportResult.Success,
|
||||
Results = new List<CblBookResult>(),
|
||||
SuccessfulInserts = new List<CblBookResult>(),
|
||||
Conflicts = new List<SeriesDto>(),
|
||||
Conflicts2 = new List<CblConflictQuestion>()
|
||||
};
|
||||
if (IsCblEmpty(cblReading, importSummary, out var readingListFromCbl)) return readingListFromCbl;
|
||||
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct();
|
||||
var userSeries =
|
||||
(await _unitOfWork.SeriesRepository.GetAllSeriesByNameAsync(uniqueSeries, userId, SeriesIncludes.Chapters)).ToList();
|
||||
if (!userSeries.Any())
|
||||
{
|
||||
// Report that no series exist in the reading list
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.AllSeriesMissing
|
||||
});
|
||||
importSummary.Success = CblImportResult.Fail;
|
||||
return importSummary;
|
||||
}
|
||||
|
||||
var conflicts = FindCblImportConflicts(userSeries);
|
||||
if (!conflicts.Any()) return importSummary;
|
||||
|
||||
importSummary.Success = CblImportResult.Fail;
|
||||
if (conflicts.Count == cblReading.Books.Book.Count)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.AllChapterMissing,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var conflict in conflicts)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.SeriesCollision,
|
||||
Series = conflict.Name
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return importSummary;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Imports (or pretends to) a cbl into a reading list. Call <see cref="ValidateCblFile"/> first!
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="cblReading"></param>
|
||||
/// <param name="dryRun"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<CblImportSummaryDto> CreateReadingListFromCbl(int userId, CblReadingList cblReading, bool dryRun = false)
|
||||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId, AppUserIncludes.ReadingListsWithItems);
|
||||
_logger.LogDebug("Importing {ReadingListName} CBL for User {UserName}", cblReading.Name, user.UserName);
|
||||
var importSummary = new CblImportSummaryDto()
|
||||
{
|
||||
CblName = cblReading.Name,
|
||||
Success = CblImportResult.Success,
|
||||
Results = new List<CblBookResult>(),
|
||||
SuccessfulInserts = new List<CblBookResult>()
|
||||
};
|
||||
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct();
|
||||
var userSeries =
|
||||
(await _unitOfWork.SeriesRepository.GetAllSeriesByNameAsync(uniqueSeries, userId, SeriesIncludes.Chapters)).ToList();
|
||||
var allSeries = userSeries.ToDictionary(s => Tasks.Scanner.Parser.Parser.Normalize(s.Name));
|
||||
|
||||
var readingListNameNormalized = Tasks.Scanner.Parser.Parser.Normalize(cblReading.Name);
|
||||
// Get all the user's reading lists
|
||||
var allReadingLists = (user.ReadingLists).ToDictionary(s => s.NormalizedTitle);
|
||||
if (!allReadingLists.TryGetValue(readingListNameNormalized, out var readingList))
|
||||
{
|
||||
readingList = DbFactory.ReadingList(cblReading.Name, string.Empty, false);
|
||||
user.ReadingLists.Add(readingList);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Reading List exists, check if we own it
|
||||
if (user.ReadingLists.All(l => l.NormalizedTitle != readingListNameNormalized))
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.NameConflict
|
||||
});
|
||||
importSummary.Success = CblImportResult.Fail;
|
||||
return importSummary;
|
||||
}
|
||||
}
|
||||
|
||||
readingList.Items ??= new List<ReadingListItem>();
|
||||
foreach (var (book, i) in cblReading.Books.Book.Select((value, i) => ( value, i )))
|
||||
{
|
||||
var normalizedSeries = Tasks.Scanner.Parser.Parser.Normalize(book.Series);
|
||||
if (!allSeries.TryGetValue(normalizedSeries, out var bookSeries))
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.SeriesMissing
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// Prioritize lookup by Volume then Chapter, but allow fallback to just Chapter
|
||||
var matchingVolume = bookSeries.Volumes.FirstOrDefault(v => book.Volume == v.Name) ?? bookSeries.Volumes.FirstOrDefault(v => v.Number == 0);
|
||||
if (matchingVolume == null)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.VolumeMissing
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
var chapter = matchingVolume.Chapters.FirstOrDefault(c => c.Number == book.Number);
|
||||
if (chapter == null)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.ChapterMissing
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// See if a matching item already exists
|
||||
ExistsOrAddReadingListItem(readingList, bookSeries.Id, matchingVolume.Id, chapter.Id);
|
||||
importSummary.SuccessfulInserts.Add(new CblBookResult(book));
|
||||
}
|
||||
|
||||
if (importSummary.SuccessfulInserts.Count != cblReading.Books.Book.Count || importSummary.Results.Count > 0)
|
||||
{
|
||||
importSummary.Success = CblImportResult.Partial;
|
||||
}
|
||||
|
||||
await CalculateReadingListAgeRating(readingList);
|
||||
|
||||
if (!dryRun) return importSummary;
|
||||
|
||||
if (!_unitOfWork.HasChanges()) return importSummary;
|
||||
await _unitOfWork.CommitAsync();
|
||||
|
||||
|
||||
return importSummary;
|
||||
}
|
||||
|
||||
private static IList<Series> FindCblImportConflicts(IEnumerable<Series> userSeries)
|
||||
{
|
||||
var dict = new HashSet<string>();
|
||||
return userSeries.Where(series => !dict.Add(Tasks.Scanner.Parser.Parser.Normalize(series.Name))).ToList();
|
||||
}
|
||||
|
||||
private static bool IsCblEmpty(CblReadingList cblReading, CblImportSummaryDto importSummary,
|
||||
out CblImportSummaryDto readingListFromCbl)
|
||||
{
|
||||
readingListFromCbl = new CblImportSummaryDto();
|
||||
if (cblReading.Books == null || cblReading.Books.Book.Count == 0)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.EmptyFile
|
||||
});
|
||||
importSummary.Success = CblImportResult.Fail;
|
||||
readingListFromCbl = importSummary;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void ExistsOrAddReadingListItem(ReadingList readingList, int seriesId, int volumeId, int chapterId)
|
||||
{
|
||||
var readingListItem =
|
||||
readingList.Items.FirstOrDefault(item =>
|
||||
item.SeriesId == seriesId && item.ChapterId == chapterId);
|
||||
if (readingListItem != null) return;
|
||||
|
||||
readingListItem = DbFactory.ReadingListItem(readingList.Items.Count, seriesId,
|
||||
volumeId, chapterId);
|
||||
readingList.Items.Add(readingListItem);
|
||||
}
|
||||
|
||||
public static CblReadingList LoadCblFromPath(string path)
|
||||
{
|
||||
var reader = new System.Xml.Serialization.XmlSerializer(typeof(CblReadingList));
|
||||
using var file = new StreamReader(path);
|
||||
var cblReadingList = (CblReadingList) reader.Deserialize(file);
|
||||
file.Close();
|
||||
return cblReadingList;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue