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:
Joe Milazzo 2023-02-12 08:20:51 -08:00 committed by GitHub
parent ae1af22af1
commit 3f24dc7392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 21951 additions and 170 deletions

View file

@ -1,17 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
using API.Comparators;
using API.Data;
using API.Data.Repositories;
using API.DTOs.ReadingLists;
using API.DTOs.ReadingLists.CBL;
using API.Entities;
using API.Extensions;
using API.Helpers;
using API.Services;
using API.SignalR;
using Kavita.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@ -22,12 +27,14 @@ public class ReadingListController : BaseApiController
private readonly IUnitOfWork _unitOfWork;
private readonly IEventHub _eventHub;
private readonly IReadingListService _readingListService;
private readonly IDirectoryService _directoryService;
public ReadingListController(IUnitOfWork unitOfWork, IEventHub eventHub, IReadingListService readingListService)
public ReadingListController(IUnitOfWork unitOfWork, IEventHub eventHub, IReadingListService readingListService, IDirectoryService directoryService)
{
_unitOfWork = unitOfWork;
_eventHub = eventHub;
_readingListService = readingListService;
_directoryService = directoryService;
}
/// <summary>
@ -180,21 +187,16 @@ public class ReadingListController : BaseApiController
public async Task<ActionResult<ReadingListDto>> CreateList(CreateReadingListDto dto)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.ReadingListsWithItems);
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.ReadingLists);
// When creating, we need to make sure Title is unique
var hasExisting = user.ReadingLists.Any(l => l.Title.Equals(dto.Title));
if (hasExisting)
try
{
return BadRequest("A list of this name already exists");
await _readingListService.CreateReadingListForUser(user, dto.Title);
}
catch (KavitaException ex)
{
return BadRequest(ex.Message);
}
var readingList = DbFactory.ReadingList(dto.Title, string.Empty, false);
user.ReadingLists.Add(readingList);
if (!_unitOfWork.HasChanges()) return BadRequest("There was a problem creating list");
await _unitOfWork.CommitAsync();
return Ok(await _unitOfWork.ReadingListRepository.GetReadingListDtoByTitleAsync(user.Id, dto.Title));
}
@ -216,37 +218,16 @@ public class ReadingListController : BaseApiController
return BadRequest("You do not have permissions on this reading list or the list doesn't exist");
}
dto.Title = dto.Title.Trim();
if (string.IsNullOrEmpty(dto.Title)) return BadRequest("Title must be set");
if (!dto.Title.Equals(readingList.Title) && await _unitOfWork.ReadingListRepository.ReadingListExists(dto.Title))
return BadRequest("Reading list already exists");
readingList.Summary = dto.Summary;
readingList.Title = dto.Title;
readingList.NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(readingList.Title);
readingList.Promoted = dto.Promoted;
readingList.CoverImageLocked = dto.CoverImageLocked;
if (!dto.CoverImageLocked)
try
{
readingList.CoverImageLocked = false;
readingList.CoverImage = string.Empty;
await _eventHub.SendMessageAsync(MessageFactory.CoverUpdate,
MessageFactory.CoverUpdateEvent(readingList.Id, MessageFactoryEntityTypes.ReadingList), false);
_unitOfWork.ReadingListRepository.Update(readingList);
await _readingListService.UpdateReadingList(readingList, dto);
}
catch (KavitaException ex)
{
return BadRequest(ex.Message);
}
_unitOfWork.ReadingListRepository.Update(readingList);
if (!_unitOfWork.HasChanges()) return Ok("Updated");
if (await _unitOfWork.CommitAsync())
{
return Ok("Updated");
}
return BadRequest("Could not update reading list");
return Ok("Updated");
}
/// <summary>
@ -503,4 +484,22 @@ public class ReadingListController : BaseApiController
if (string.IsNullOrEmpty(name)) return true;
return Ok(await _unitOfWork.ReadingListRepository.ReadingListExists(name));
}
// [HttpPost("import-cbl")]
// public async Task<ActionResult<CblImportSummaryDto>> ImportCbl([FromForm(Name = "cbl")] IFormFile file, [FromForm(Name = "dryRun")] bool dryRun = false)
// {
// var userId = User.GetUserId();
// var filename = Path.GetRandomFileName();
// var outputFile = Path.Join(_directoryService.TempDirectory, filename);
//
// await using var stream = System.IO.File.Create(outputFile);
// await file.CopyToAsync(stream);
// stream.Close();
// var cbl = ReadingListService.LoadCblFromPath(outputFile);
//
// // We need to pass the temp file back
//
// var importSummary = await _readingListService.ValidateCblFile(userId, cbl);
// return importSummary.Results.Any() ? Ok(importSummary) : Ok(await _readingListService.CreateReadingListFromCbl(userId, cbl, dryRun));
// }
}

View file

@ -17,7 +17,6 @@ namespace API.Controllers;
/// <summary>
///
/// </summary>
[Authorize(Policy = "RequireAdminRole")]
public class UploadController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
@ -26,10 +25,11 @@ public class UploadController : BaseApiController
private readonly ITaskScheduler _taskScheduler;
private readonly IDirectoryService _directoryService;
private readonly IEventHub _eventHub;
private readonly IReadingListService _readingListService;
/// <inheritdoc />
public UploadController(IUnitOfWork unitOfWork, IImageService imageService, ILogger<UploadController> logger,
ITaskScheduler taskScheduler, IDirectoryService directoryService, IEventHub eventHub)
ITaskScheduler taskScheduler, IDirectoryService directoryService, IEventHub eventHub, IReadingListService readingListService)
{
_unitOfWork = unitOfWork;
_imageService = imageService;
@ -37,6 +37,7 @@ public class UploadController : BaseApiController
_taskScheduler = taskScheduler;
_directoryService = directoryService;
_eventHub = eventHub;
_readingListService = readingListService;
}
/// <summary>
@ -170,9 +171,9 @@ public class UploadController : BaseApiController
/// <summary>
/// Replaces reading list cover image and locks it with a base64 encoded image
/// </summary>
/// <remarks>This is the only API that can be called by non-admins, but the authenticated user must have a readinglist permission</remarks>
/// <param name="uploadFileDto"></param>
/// <returns></returns>
[Authorize(Policy = "RequireAdminRole")]
[RequestSizeLimit(8_000_000)]
[HttpPost("reading-list")]
public async Task<ActionResult> UploadReadingListCoverImageFromUrl(UploadFileDto uploadFileDto)
@ -184,6 +185,9 @@ public class UploadController : BaseApiController
return BadRequest("You must pass a url to use");
}
if (_readingListService.UserHasReadingListAccess(uploadFileDto.Id, User.GetUsername()) == null)
return Unauthorized("You do not have access");
try
{
var filePath = _imageService.CreateThumbnailFromBase64(uploadFileDto.Url, $"{ImageService.GetReadingListFormat(uploadFileDto.Id)}");

View file

@ -0,0 +1,29 @@
using System.Xml.Serialization;
namespace API.DTOs.ReadingLists.CBL;
[XmlRoot(ElementName="Book")]
public class CblBook
{
[XmlAttribute("Series")]
public string Series { get; set; }
/// <summary>
/// Chapter Number
/// </summary>
[XmlAttribute("Number")]
public string Number { get; set; }
/// <summary>
/// Volume Number (usually for Comics they are the year)
/// </summary>
[XmlAttribute("Volume")]
public string Volume { get; set; }
[XmlAttribute("Year")]
public string Year { get; set; }
/// <summary>
/// The underlying filetype
/// </summary>
/// <remarks>This is not part of the standard and explicitly for Kavita to support non cbz/cbr files</remarks>
[XmlAttribute("FileType")]
public string FileType { get; set; }
}

View file

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace API.DTOs.ReadingLists.CBL;
public class CblConflictQuestion
{
public string SeriesName { get; set; }
public IList<int> LibrariesIds { get; set; }
}

View file

@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.ComponentModel;
using API.DTOs.ReadingLists.CBL;
namespace API.DTOs.ReadingLists;
public enum CblImportResult {
/// <summary>
/// There was an issue which prevented processing
/// </summary>
[Description("Fail")]
Fail = 0,
/// <summary>
/// Some items were added, but not all
/// </summary>
[Description("Partial")]
Partial = 1,
/// <summary>
/// Everything was imported correctly
/// </summary>
[Description("Success")]
Success = 2
}
public enum CblImportReason
{
/// <summary>
/// The Chapter is not present in Kavita
/// </summary>
[Description("Chapter missing")]
ChapterMissing = 0,
/// <summary>
/// The Volume is not present in Kavita or no Volume field present in CBL and there is no chapter matching
/// </summary>
[Description("Volume missing")]
VolumeMissing = 1,
/// <summary>
/// The Series is not present in Kavita or the user does not have access to the Series due to some account restrictions
/// </summary>
[Description("Series missing")]
SeriesMissing = 2,
/// <summary>
/// The CBL Name conflicts with another Reading List in the system
/// </summary>
[Description("Name Conflict")]
NameConflict = 3,
/// <summary>
/// Every Series in the Reading list is missing from within Kavita or user has access restrictions to
/// </summary>
[Description("All Series Missing")]
AllSeriesMissing = 4,
/// <summary>
/// There are no Book entries in the CBL
/// </summary>
[Description("Empty File")]
EmptyFile = 5,
/// <summary>
/// Series Collides between Libraries
/// </summary>
[Description("Series Collision")]
SeriesCollision = 6,
/// <summary>
/// Every book chapter is missing or can't be matched
/// </summary>
[Description("All Chapters Missing")]
AllChapterMissing = 7,
}
public class CblBookResult
{
public string Series { get; set; }
public string Volume { get; set; }
public string Number { get; set; }
public CblImportReason Reason { get; set; }
public CblBookResult(CblBook book)
{
Series = book.Series;
Volume = book.Volume;
Number = book.Number;
}
public CblBookResult()
{
}
}
/// <summary>
/// Represents the summary from the Import of a given CBL
/// </summary>
public class CblImportSummaryDto
{
public string CblName { get; set; }
public ICollection<CblBookResult> Results { get; set; }
public CblImportResult Success { get; set; }
public ICollection<CblBookResult> SuccessfulInserts { get; set; }
/// <summary>
/// A list of Series that are within the CBL but map to multiple libraries within Kavita
/// </summary>
public IList<SeriesDto> Conflicts { get; set; }
public IList<CblConflictQuestion> Conflicts2 { get; set; }
}

View file

@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Xml.Serialization;
namespace API.DTOs.ReadingLists.CBL;
[XmlRoot(ElementName="Books")]
public class CblBooks
{
[XmlElement(ElementName="Book")]
public List<CblBook> Book { get; set; }
}
[XmlRoot(ElementName="ReadingList")]
public class CblReadingList
{
/// <summary>
/// Name of the Reading List
/// </summary>
[XmlElement(ElementName="Name")]
public string Name { get; set; }
[XmlElement(ElementName="Books")]
public CblBooks Books { get; set; }
}

View file

@ -1,5 +1,6 @@
using System;
using System.Linq;
using API.Entities;
using API.Entities.Enums;
using Kavita.Common.Extensions;
@ -54,13 +55,27 @@ public class ComicInfo
/// User's rating of the content
/// </summary>
public float UserRating { get; set; }
public string StoryArc { get; set; } = string.Empty;
/// <summary>
/// Can contain multiple comma separated strings, each create a <see cref="CollectionTag"/>
/// </summary>
public string SeriesGroup { get; set; } = string.Empty;
/// <summary>
///
/// </summary>
public string StoryArc { get; set; } = string.Empty;
/// <summary>
/// Can contain multiple comma separated numbers that match with StoryArc
/// </summary>
public string StoryArcNumber { get; set; } = string.Empty;
public string AlternateNumber { get; set; } = string.Empty;
public string AlternateSeries { get; set; } = string.Empty;
/// <summary>
/// Not used
/// </summary>
[System.ComponentModel.DefaultValueAttribute(0)]
public int AlternateCount { get; set; } = 0;
public string AlternateSeries { get; set; } = string.Empty;
/// <summary>
/// This is Epub only: calibre:title_sort

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,66 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace API.Data.Migrations
{
public partial class ReadingListFields : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AlternateCount",
table: "Chapter",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "AlternateNumber",
table: "Chapter",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "AlternateSeries",
table: "Chapter",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "StoryArc",
table: "Chapter",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "StoryArcNumber",
table: "Chapter",
type: "TEXT",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AlternateCount",
table: "Chapter");
migrationBuilder.DropColumn(
name: "AlternateNumber",
table: "Chapter");
migrationBuilder.DropColumn(
name: "AlternateSeries",
table: "Chapter");
migrationBuilder.DropColumn(
name: "StoryArc",
table: "Chapter");
migrationBuilder.DropColumn(
name: "StoryArcNumber",
table: "Chapter");
}
}
}

View file

@ -378,6 +378,15 @@ namespace API.Data.Migrations
b.Property<int>("AgeRating")
.HasColumnType("INTEGER");
b.Property<int>("AlternateCount")
.HasColumnType("INTEGER");
b.Property<string>("AlternateNumber")
.HasColumnType("TEXT");
b.Property<string>("AlternateSeries")
.HasColumnType("TEXT");
b.Property<int>("AvgHoursToRead")
.HasColumnType("INTEGER");
@ -429,6 +438,12 @@ namespace API.Data.Migrations
b.Property<string>("SeriesGroup")
.HasColumnType("TEXT");
b.Property<string>("StoryArc")
.HasColumnType("TEXT");
b.Property<string>("StoryArcNumber")
.HasColumnType("TEXT");
b.Property<string>("Summary")
.HasColumnType("TEXT");

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs.ReadingLists;
@ -31,6 +32,7 @@ public interface IReadingListRepository
Task<string> GetCoverImageAsync(int readingListId);
Task<IList<string>> GetAllCoverImagesAsync();
Task<bool> ReadingListExists(string name);
Task<List<ReadingList>> GetAllReadingListsAsync();
}
public class ReadingListRepository : IReadingListRepository
@ -84,6 +86,15 @@ public class ReadingListRepository : IReadingListRepository
.AnyAsync(x => x.NormalizedTitle.Equals(normalized));
}
public async Task<List<ReadingList>> GetAllReadingListsAsync()
{
return await _context.ReadingList
.Include(r => r.Items.OrderBy(i => i.Order))
.AsSplitQuery()
.OrderBy(l => l.Title)
.ToListAsync();
}
public void Remove(ReadingListItem item)
{
_context.ReadingListItem.Remove(item);

View file

@ -35,6 +35,7 @@ public enum SeriesIncludes
Metadata = 4,
Related = 8,
Library = 16,
Chapters = 32
}
/// <summary>
@ -115,6 +116,11 @@ public interface ISeriesRepository
Task<PagedList<SeriesDto>> GetWantToReadForUserAsync(int userId, UserParams userParams, FilterDto filter);
Task<bool> IsSeriesInWantToRead(int userId, int seriesId);
Task<Series> GetSeriesByFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None);
Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IEnumerable<string> normalizedNames,
int userId, SeriesIncludes includes = SeriesIncludes.None);
Task<IEnumerable<SeriesDto>> GetAllSeriesDtosByNameAsync(IEnumerable<string> normalizedNames,
int userId, SeriesIncludes includes = SeriesIncludes.None);
Task<Series> GetFullSeriesByAnyName(string seriesName, string localizedName, int libraryId, MangaFormat format, bool withFullIncludes = true);
Task<IList<Series>> RemoveSeriesNotInList(IList<ParsedSeries> seenSeries, int libraryId);
Task<IDictionary<string, IList<SeriesModified>>> GetFolderPathMap(int libraryId);
@ -545,6 +551,7 @@ public class SeriesRepository : ISeriesRepository
.ToListAsync();
}
public async Task AddSeriesModifiers(int userId, List<SeriesDto> series)
{
var userProgress = await _context.AppUserProgresses
@ -1200,6 +1207,35 @@ public class SeriesRepository : ISeriesRepository
.SingleOrDefaultAsync();
}
public async Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IEnumerable<string> normalizedNames,
int userId, SeriesIncludes includes = SeriesIncludes.None)
{
var libraryIds = _context.Library.GetUserLibraries(userId);
var userRating = await _context.AppUser.GetUserAgeRestriction(userId);
return await _context.Series
.Where(s => normalizedNames.Contains(s.NormalizedName))
.Where(s => libraryIds.Contains(s.LibraryId))
.RestrictAgainstAgeRestriction(userRating)
.Includes(includes)
.ToListAsync();
}
public async Task<IEnumerable<SeriesDto>> GetAllSeriesDtosByNameAsync(IEnumerable<string> normalizedNames, int userId,
SeriesIncludes includes = SeriesIncludes.None)
{
var libraryIds = _context.Library.GetUserLibraries(userId);
var userRating = await _context.AppUser.GetUserAgeRestriction(userId);
return await _context.Series
.Where(s => normalizedNames.Contains(s.NormalizedName))
.Where(s => libraryIds.Contains(s.LibraryId))
.RestrictAgainstAgeRestriction(userRating)
.Includes(includes)
.ProjectTo<SeriesDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
/// <summary>
/// Finds a series by series name or localized name for a given library.
/// </summary>

View file

@ -119,6 +119,7 @@ public class UserRepository : IUserRepository
var query = _context.Users
.Where(x => x.UserName == username);
// TODO: Move to QueryExtensions
query = AddIncludesToQuery(query, includeFlags);
return await query.SingleOrDefaultAsync();
@ -201,9 +202,7 @@ public class UserRepository : IUserRepository
query = query.Include(u => u.Devices);
}
return query;
return query.AsSplitQuery();
}

View file

@ -80,6 +80,15 @@ public class Chapter : IEntityDate, IHasReadTimeEstimate
/// SeriesGroup tag in ComicInfo
/// </summary>
public string SeriesGroup { get; set; }
public string StoryArc { get; set; } = string.Empty;
public string StoryArcNumber { get; set; } = string.Empty;
public string AlternateNumber { get; set; } = string.Empty;
public string AlternateSeries { get; set; } = string.Empty;
/// <summary>
/// Not currently used in Kavita
/// </summary>
public int AlternateCount { get; set; } = 0;
/// <summary>
/// Total Word count of all chapters in this chapter.

View file

@ -156,6 +156,13 @@ public static class QueryableExtensions
query = query.Include(s => s.Volumes);
}
if (includeFlags.HasFlag(SeriesIncludes.Chapters))
{
query = query
.Include(s => s.Volumes)
.ThenInclude(v => v.Chapters);
}
if (includeFlags.HasFlag(SeriesIncludes.Related))
{
query = query.Include(s => s.Relations)

View file

@ -150,13 +150,7 @@ public class CollectionTagService : ICollectionTagService
/// <returns></returns>
public async Task<CollectionTag> GetTagOrCreate(int tagId, string title)
{
var tag = await _unitOfWork.CollectionTagRepository.GetFullTagAsync(tagId);
if (tag == null)
{
tag = CreateTag(title);
}
return tag;
return await _unitOfWork.CollectionTagRepository.GetFullTagAsync(tagId) ?? CreateTag(title);
}
/// <summary>

View file

@ -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;
}
}

View file

@ -105,10 +105,20 @@ public class StatisticService : IStatisticService
.ToListAsync();
// var averageReadingTimePerWeek = _context.AppUserProgresses
// .Where(p => p.AppUserId == userId)
// .Join(_context.Chapter, p => p.ChapterId, c => c.Id,
// (p, c) => (p.PagesRead / (float) c.Pages) * c.AvgHoursToRead)
// .Average() / 7.0;
var averageReadingTimePerWeek = _context.AppUserProgresses
.Where(p => p.AppUserId == userId)
.Join(_context.Chapter, p => p.ChapterId, c => c.Id,
(p, c) => (p.PagesRead / (float) c.Pages) * c.AvgHoursToRead)
(p, c) => new
{
AverageReadingHours = Math.Min((float) p.PagesRead / (float) c.Pages, 1.0) * ((float) c.AvgHoursToRead)
})
.Select(x => x.AverageReadingHours)
.Average() / 7.0;
return new UserReadStatistics()
@ -373,7 +383,22 @@ public class StatisticService : IStatisticService
var minDay = results.Min(d => d.Value);
for (var date = minDay; date < DateTime.Now; date = date.AddDays(1))
{
if (results.Any(d => d.Value == date)) continue;
var resultsForDay = results.Where(d => d.Value == date).ToList();
if (resultsForDay.Count > 0)
{
// Add in types that aren't there (there is a bug in UI library that will cause dates to get out of order)
var existingFormats = resultsForDay.Select(r => r.Format).Distinct();
foreach (var format in Enum.GetValues(typeof(MangaFormat)).Cast<MangaFormat>().Where(f => f != MangaFormat.Unknown && !existingFormats.Contains(f)))
{
results.Add(new PagesReadOnADayCount<DateTime>()
{
Format = format,
Value = date,
Count = 0
});
}
continue;
}
results.Add(new PagesReadOnADayCount<DateTime>()
{
Format = MangaFormat.Archive,
@ -401,7 +426,7 @@ public class StatisticService : IStatisticService
}
}
return results;
return results.OrderBy(r => r.Value);
}
public IEnumerable<StatCount<DayOfWeek>> GetDayBreakdown()

View file

@ -161,6 +161,8 @@ public class ProcessSeries : IProcessSeries
UpdateSeriesMetadata(series, library);
//CreateReadingListsFromSeries(series, library); This will be implemented later when I solution it
// Update series FolderPath here
await UpdateSeriesFolderPath(parsedInfos, library, series);
@ -203,6 +205,27 @@ public class ProcessSeries : IProcessSeries
EnqueuePostSeriesProcessTasks(series.LibraryId, series.Id);
}
private void CreateReadingListsFromSeries(Series series, Library library)
{
//if (!library.ManageReadingLists) return;
_logger.LogInformation("Generating Reading Lists for {SeriesName}", series.Name);
series.Metadata ??= DbFactory.SeriesMetadata(new List<CollectionTag>());
foreach (var chapter in series.Volumes.SelectMany(v => v.Chapters))
{
if (!string.IsNullOrEmpty(chapter.StoryArc))
{
var readingLists = chapter.StoryArc.Split(',');
var readingListOrders = chapter.StoryArcNumber.Split(',');
if (readingListOrders.Length == 0)
{
_logger.LogDebug("[ScannerService] There are no StoryArc orders listed, all reading lists fueled from StoryArc will be unordered");
}
}
}
}
private async Task UpdateSeriesFolderPath(IEnumerable<ParserInfo> parsedInfos, Library library, Series series)
{
var seriesDirs = _directoryService.FindHighestDirectoriesFromFiles(library.Folders.Select(l => l.Path),
@ -660,6 +683,33 @@ public class ProcessSeries : IProcessSeries
chapter.SeriesGroup = comicInfo.SeriesGroup;
}
if (!string.IsNullOrEmpty(comicInfo.StoryArc))
{
chapter.StoryArc = comicInfo.StoryArc;
}
if (!string.IsNullOrEmpty(comicInfo.AlternateSeries))
{
chapter.AlternateSeries = comicInfo.AlternateSeries;
}
if (!string.IsNullOrEmpty(comicInfo.AlternateNumber))
{
chapter.AlternateNumber = comicInfo.AlternateNumber;
}
if (!string.IsNullOrEmpty(comicInfo.StoryArcNumber))
{
chapter.StoryArcNumber = comicInfo.StoryArcNumber;
}
if (comicInfo.AlternateCount > 0)
{
chapter.AlternateCount = comicInfo.AlternateCount;
}
if (comicInfo.Count > 0)
{
chapter.TotalCount = comicInfo.Count;
@ -759,7 +809,7 @@ public class ProcessSeries : IProcessSeries
if (!string.IsNullOrEmpty(comicInfoTagSeparatedByComma))
{
return comicInfoTagSeparatedByComma.Split(",").Select(s => s.Trim()).DistinctBy(s => s.Normalize()).ToList();
return comicInfoTagSeparatedByComma.Split(",").Select(s => s.Trim()).DistinctBy(Parser.Parser.Normalize).ToList();
}
return ImmutableList<string>.Empty;
}