Automatic Collection Creation (#1768)

* Made the unread badges slightly smaller and rounded on top right.

* A bit more tweaks on the not read badges. Looking really nice now.

* In order to start the work on managing collections from ScanLoop, I needed to refactor collection apis into the service layer and add unit tests.

Removed ToUpper Normalization for new tags.

* Hooked up ability to auto generate collections from SeriesGroup metadata tag.
This commit is contained in:
Joe Milazzo 2023-01-30 19:57:46 -08:00 committed by GitHub
parent 91a2a6854f
commit 1da27f085c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 2222 additions and 121 deletions

View file

@ -6,7 +6,10 @@ using API.Data;
using API.DTOs.CollectionTags;
using API.Entities.Metadata;
using API.Extensions;
using API.Services;
using API.Services.Tasks.Metadata;
using API.SignalR;
using Kavita.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -18,13 +21,13 @@ namespace API.Controllers;
public class CollectionController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IEventHub _eventHub;
private readonly ICollectionTagService _collectionService;
/// <inheritdoc />
public CollectionController(IUnitOfWork unitOfWork, IEventHub eventHub)
public CollectionController(IUnitOfWork unitOfWork, ICollectionTagService collectionService)
{
_unitOfWork = unitOfWork;
_eventHub = eventHub;
_collectionService = collectionService;
}
/// <summary>
@ -71,8 +74,7 @@ public class CollectionController : BaseApiController
[HttpGet("name-exists")]
public async Task<ActionResult<bool>> DoesNameExists(string name)
{
if (string.IsNullOrEmpty(name.Trim())) return Ok(true);
return Ok(await _unitOfWork.CollectionTagRepository.TagExists(name));
return Ok(await _collectionService.TagExistsByName(name));
}
/// <summary>
@ -85,28 +87,13 @@ public class CollectionController : BaseApiController
[HttpPost("update")]
public async Task<ActionResult> UpdateTag(CollectionTagDto updatedTag)
{
var existingTag = await _unitOfWork.CollectionTagRepository.GetTagAsync(updatedTag.Id);
if (existingTag == null) return BadRequest("This tag does not exist");
var title = updatedTag.Title.Trim();
if (string.IsNullOrEmpty(title)) return BadRequest("Title cannot be empty");
if (!title.Equals(existingTag.Title) && await _unitOfWork.CollectionTagRepository.TagExists(updatedTag.Title))
return BadRequest("A tag with this name already exists");
existingTag.Title = title;
existingTag.Promoted = updatedTag.Promoted;
existingTag.NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(updatedTag.Title);
existingTag.Summary = updatedTag.Summary.Trim();
if (_unitOfWork.HasChanges())
try
{
if (await _unitOfWork.CommitAsync())
{
return Ok("Tag updated successfully");
}
if (await _collectionService.UpdateTag(updatedTag)) return Ok("Tag updated successfully");
}
else
catch (KavitaException ex)
{
return Ok("Tag updated successfully");
return BadRequest(ex.Message);
}
return BadRequest("Something went wrong, please try again");
@ -121,29 +108,11 @@ public class CollectionController : BaseApiController
[HttpPost("update-for-series")]
public async Task<ActionResult> AddToMultipleSeries(CollectionTagBulkAddDto dto)
{
var tag = await _unitOfWork.CollectionTagRepository.GetFullTagAsync(dto.CollectionTagId);
if (tag == null)
{
tag = DbFactory.CollectionTag(0, dto.CollectionTagTitle, String.Empty, false);
_unitOfWork.CollectionTagRepository.Add(tag);
}
// Create a new tag and save
var tag = await _collectionService.GetTagOrCreate(dto.CollectionTagId, dto.CollectionTagTitle);
if (await _collectionService.AddTagToSeries(tag, dto.SeriesIds)) return Ok();
var seriesMetadatas = await _unitOfWork.SeriesRepository.GetSeriesMetadataForIdsAsync(dto.SeriesIds);
foreach (var metadata in seriesMetadatas)
{
if (!metadata.CollectionTags.Any(t => t.Title.Equals(tag.Title, StringComparison.InvariantCulture)))
{
metadata.CollectionTags.Add(tag);
_unitOfWork.SeriesMetadataRepository.Update(metadata);
}
}
if (!_unitOfWork.HasChanges()) return Ok();
if (await _unitOfWork.CommitAsync())
{
return Ok();
}
return BadRequest("There was an issue updating series with collection tag");
}
@ -154,7 +123,7 @@ public class CollectionController : BaseApiController
/// <returns></returns>
[Authorize(Policy = "RequireAdminRole")]
[HttpPost("update-series")]
public async Task<ActionResult> UpdateSeriesForTag(UpdateSeriesForTagDto updateSeriesForTagDto)
public async Task<ActionResult> RemoveTagFromMultipleSeries(UpdateSeriesForTagDto updateSeriesForTagDto)
{
try
{
@ -162,41 +131,8 @@ public class CollectionController : BaseApiController
if (tag == null) return BadRequest("Not a valid Tag");
tag.SeriesMetadatas ??= new List<SeriesMetadata>();
// Check if Tag has updated (Summary)
if (tag.Summary == null || !tag.Summary.Equals(updateSeriesForTagDto.Tag.Summary))
{
tag.Summary = updateSeriesForTagDto.Tag.Summary;
_unitOfWork.CollectionTagRepository.Update(tag);
}
tag.CoverImageLocked = updateSeriesForTagDto.Tag.CoverImageLocked;
if (!updateSeriesForTagDto.Tag.CoverImageLocked)
{
tag.CoverImageLocked = false;
tag.CoverImage = string.Empty;
await _eventHub.SendMessageAsync(MessageFactory.CoverUpdate,
MessageFactory.CoverUpdateEvent(tag.Id, MessageFactoryEntityTypes.CollectionTag), false);
_unitOfWork.CollectionTagRepository.Update(tag);
}
foreach (var seriesIdToRemove in updateSeriesForTagDto.SeriesIdsToRemove)
{
tag.SeriesMetadatas.Remove(tag.SeriesMetadatas.Single(sm => sm.SeriesId == seriesIdToRemove));
}
if (tag.SeriesMetadatas.Count == 0)
{
_unitOfWork.CollectionTagRepository.Remove(tag);
}
if (!_unitOfWork.HasChanges()) return Ok("No updates");
if (await _unitOfWork.CommitAsync())
{
if (await _collectionService.RemoveTagFromSeries(tag, updateSeriesForTagDto.SeriesIdsToRemove))
return Ok("Tag updated");
}
}
catch (Exception)
{

View file

@ -247,11 +247,9 @@ public class LibraryController : BaseApiController
var library = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId, LibraryIncludes.None);
if (TaskScheduler.HasScanTaskRunningForLibrary(libraryId))
{
// TODO: Figure out how to cancel a job
_logger.LogInformation("User is attempting to delete a library while a scan is in progress");
return BadRequest(
"You cannot delete a library while a scan is in progress. Please wait for scan to continue then try to delete");
"You cannot delete a library while a scan is in progress. Please wait for scan to complete or restart Kavita then try to delete");
}
// Due to a bad schema that I can't figure out how to fix, we need to erase all RelatedSeries before we delete the library
@ -336,6 +334,7 @@ public class LibraryController : BaseApiController
library.IncludeInDashboard = dto.IncludeInDashboard;
library.IncludeInRecommended = dto.IncludeInRecommended;
library.IncludeInSearch = dto.IncludeInSearch;
library.ManageCollections = dto.CreateCollections;
_unitOfWork.LibraryRepository.Update(library);

View file

@ -30,6 +30,10 @@ public class LibraryDto
/// </summary>
public bool IncludeInRecommended { get; set; } = true;
/// <summary>
/// Should this library create and manage collections from Metadata
/// </summary>
public bool ManageCollections { get; set; } = true;
/// <summary>
/// Include library series in Search
/// </summary>
public bool IncludeInSearch { get; set; } = true;

View file

@ -1,17 +1,28 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using API.Entities.Enums;
namespace API.DTOs;
public class UpdateLibraryDto
{
[Required]
public int Id { get; init; }
[Required]
public string Name { get; init; }
[Required]
public LibraryType Type { get; set; }
[Required]
public IEnumerable<string> Folders { get; init; }
[Required]
public bool FolderWatching { get; init; }
[Required]
public bool IncludeInDashboard { get; init; }
[Required]
public bool IncludeInRecommended { get; init; }
[Required]
public bool IncludeInSearch { get; init; }
[Required]
public bool CreateCollections { get; init; }
}

View file

@ -104,6 +104,9 @@ public sealed class DataContext : IdentityDbContext<AppUser, AppRole, int,
builder.Entity<Library>()
.Property(b => b.IncludeInSearch)
.HasDefaultValue(true);
builder.Entity<Library>()
.Property(b => b.ManageCollections)
.HasDefaultValue(true);
}

View file

@ -95,7 +95,7 @@ public static class DbFactory
return new CollectionTag()
{
Id = id,
NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(title?.Trim()).ToUpper(),
NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(title?.Trim()),
Title = title?.Trim(),
Summary = summary?.Trim(),
Promoted = promoted
@ -106,7 +106,7 @@ public static class DbFactory
{
return new ReadingList()
{
NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(title?.Trim()).ToUpper(),
NormalizedTitle = Services.Tasks.Scanner.Parser.Parser.Normalize(title?.Trim()),
Title = title?.Trim(),
Summary = summary?.Trim(),
Promoted = promoted,

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace API.Data.Migrations
{
public partial class AutoCollections : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "ManageCollections",
table: "Library",
type: "INTEGER",
nullable: false,
defaultValue: true);
migrationBuilder.AddColumn<string>(
name: "SeriesGroup",
table: "Chapter",
type: "TEXT",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ManageCollections",
table: "Library");
migrationBuilder.DropColumn(
name: "SeriesGroup",
table: "Chapter");
}
}
}

View file

@ -400,6 +400,9 @@ namespace API.Data.Migrations
b.Property<DateTime>("ReleaseDate")
.HasColumnType("TEXT");
b.Property<string>("SeriesGroup")
.HasColumnType("TEXT");
b.Property<string>("Summary")
.HasColumnType("TEXT");
@ -580,6 +583,11 @@ namespace API.Data.Migrations
b.Property<DateTime>("LastScanned")
.HasColumnType("TEXT");
b.Property<bool>("ManageCollections")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(true);
b.Property<string>("Name")
.HasColumnType("TEXT");

View file

@ -73,6 +73,10 @@ public class Chapter : IEntityDate, IHasReadTimeEstimate
/// Number of the Total Count (progress the Series is complete)
/// </summary>
public int Count { get; set; } = 0;
/// <summary>
/// SeriesGroup tag in ComicInfo
/// </summary>
public string SeriesGroup { get; set; }
/// <summary>
/// Total Word count of all chapters in this chapter.

View file

@ -27,6 +27,10 @@ public class Library : IEntityDate
/// Include library series in Search
/// </summary>
public bool IncludeInSearch { get; set; } = true;
/// <summary>
/// Should this library create and manage collections from Metadata
/// </summary>
public bool ManageCollections { get; set; } = true;
public DateTime Created { get; set; }
public DateTime LastModified { get; set; }
/// <summary>

View file

@ -55,6 +55,7 @@ public static class ApplicationServiceExtensions
services.AddScoped<IWordCountAnalyzerService, WordCountAnalyzerService>();
services.AddScoped<ILibraryWatcher, LibraryWatcher>();
services.AddScoped<ITachiyomiService, TachiyomiService>();
services.AddScoped<ICollectionTagService, CollectionTagService>();
services.AddScoped<IPresenceTracker, PresenceTracker>();
services.AddScoped<IEventHub, EventHub>();

View file

@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.DTOs.CollectionTags;
using API.Entities;
using API.Entities.Metadata;
using API.SignalR;
using Kavita.Common;
using Microsoft.Extensions.Logging;
namespace API.Services;
public interface ICollectionTagService
{
Task<bool> TagExistsByName(string name);
Task<bool> UpdateTag(CollectionTagDto dto);
Task<bool> AddTagToSeries(CollectionTag tag, IEnumerable<int> seriesIds);
Task<bool> RemoveTagFromSeries(CollectionTag tag, IEnumerable<int> seriesIds);
Task<CollectionTag> GetTagOrCreate(int tagId, string title);
void AddTagToSeriesMetadata(CollectionTag tag, SeriesMetadata metadata);
}
public class CollectionTagService : ICollectionTagService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IEventHub _eventHub;
public CollectionTagService(IUnitOfWork unitOfWork, IEventHub eventHub)
{
_unitOfWork = unitOfWork;
_eventHub = eventHub;
}
/// <summary>
/// Checks if a collection exists with the name
/// </summary>
/// <param name="name">If empty or null, will return true as that is invalid</param>
/// <returns></returns>
public async Task<bool> TagExistsByName(string name)
{
if (string.IsNullOrEmpty(name.Trim())) return true;
return await _unitOfWork.CollectionTagRepository.TagExists(name);
}
public async Task<bool> UpdateTag(CollectionTagDto dto)
{
var existingTag = await _unitOfWork.CollectionTagRepository.GetTagAsync(dto.Id);
if (existingTag == null) throw new KavitaException("This tag does not exist");
var title = dto.Title.Trim();
if (string.IsNullOrEmpty(title)) throw new KavitaException("Title cannot be empty");
if (!title.Equals(existingTag.Title) && await TagExistsByName(dto.Title))
throw new KavitaException("A tag with this name already exists");
existingTag.SeriesMetadatas ??= new List<SeriesMetadata>();
existingTag.Title = title;
existingTag.NormalizedTitle = Tasks.Scanner.Parser.Parser.Normalize(dto.Title);
existingTag.Promoted = dto.Promoted;
existingTag.CoverImageLocked = dto.CoverImageLocked;
_unitOfWork.CollectionTagRepository.Update(existingTag);
// Check if Tag has updated (Summary)
var summary = dto.Summary.Trim();
if (existingTag.Summary == null || !existingTag.Summary.Equals(summary))
{
existingTag.Summary = summary;
_unitOfWork.CollectionTagRepository.Update(existingTag);
}
// If we unlock the cover image it means reset
if (!dto.CoverImageLocked)
{
existingTag.CoverImageLocked = false;
existingTag.CoverImage = string.Empty;
await _eventHub.SendMessageAsync(MessageFactory.CoverUpdate,
MessageFactory.CoverUpdateEvent(existingTag.Id, MessageFactoryEntityTypes.CollectionTag), false);
_unitOfWork.CollectionTagRepository.Update(existingTag);
}
if (!_unitOfWork.HasChanges()) return true;
return await _unitOfWork.CommitAsync();
}
/// <summary>
/// Adds a set of Series to a Collection
/// </summary>
/// <param name="tag">A full Tag</param>
/// <param name="seriesIds"></param>
/// <returns></returns>
public async Task<bool> AddTagToSeries(CollectionTag tag, IEnumerable<int> seriesIds)
{
var metadatas = await _unitOfWork.SeriesRepository.GetSeriesMetadataForIdsAsync(seriesIds);
foreach (var metadata in metadatas)
{
AddTagToSeriesMetadata(tag, metadata);
}
if (!_unitOfWork.HasChanges()) return true;
return await _unitOfWork.CommitAsync();
}
/// <summary>
/// Adds a collection tag to a SeriesMetadata
/// </summary>
/// <remarks>Does not commit</remarks>
/// <param name="tag"></param>
/// <param name="metadata"></param>
/// <returns></returns>
public void AddTagToSeriesMetadata(CollectionTag tag, SeriesMetadata metadata)
{
metadata.CollectionTags ??= new List<CollectionTag>();
if (metadata.CollectionTags.Any(t => t.Title.Equals(tag.Title, StringComparison.InvariantCulture))) return;
metadata.CollectionTags.Add(tag);
_unitOfWork.SeriesMetadataRepository.Update(metadata);
}
public async Task<bool> RemoveTagFromSeries(CollectionTag tag, IEnumerable<int> seriesIds)
{
foreach (var seriesIdToRemove in seriesIds)
{
tag.SeriesMetadatas.Remove(tag.SeriesMetadatas.Single(sm => sm.SeriesId == seriesIdToRemove));
}
if (tag.SeriesMetadatas.Count == 0)
{
_unitOfWork.CollectionTagRepository.Remove(tag);
}
if (!_unitOfWork.HasChanges()) return true;
return await _unitOfWork.CommitAsync();
}
/// <summary>
/// Tries to fetch the full tag, else returns a new tag. Adds to tracking but does not commit
/// </summary>
/// <param name="tagId"></param>
/// <param name="title"></param>
/// <returns></returns>
public async Task<CollectionTag> GetTagOrCreate(int tagId, string title)
{
var tag = await _unitOfWork.CollectionTagRepository.GetFullTagAsync(tagId);
if (tag == null)
{
tag = DbFactory.CollectionTag(0, title, string.Empty, false);
_unitOfWork.CollectionTagRepository.Add(tag);
}
return tag;
}
}

View file

@ -45,6 +45,7 @@ public class ProcessSeries : IProcessSeries
private readonly IFileService _fileService;
private readonly IMetadataService _metadataService;
private readonly IWordCountAnalyzerService _wordCountAnalyzerService;
private readonly ICollectionTagService _collectionTagService;
private IList<Genre> _genres;
private IList<Person> _people;
@ -52,7 +53,8 @@ public class ProcessSeries : IProcessSeries
public ProcessSeries(IUnitOfWork unitOfWork, ILogger<ProcessSeries> logger, IEventHub eventHub,
IDirectoryService directoryService, ICacheHelper cacheHelper, IReadingItemService readingItemService,
IFileService fileService, IMetadataService metadataService, IWordCountAnalyzerService wordCountAnalyzerService)
IFileService fileService, IMetadataService metadataService, IWordCountAnalyzerService wordCountAnalyzerService,
ICollectionTagService collectionTagService)
{
_unitOfWork = unitOfWork;
_logger = logger;
@ -63,6 +65,7 @@ public class ProcessSeries : IProcessSeries
_fileService = fileService;
_metadataService = metadataService;
_wordCountAnalyzerService = wordCountAnalyzerService;
_collectionTagService = collectionTagService;
}
/// <summary>
@ -151,7 +154,7 @@ public class ProcessSeries : IProcessSeries
series.NormalizedLocalizedName = Parser.Parser.Normalize(series.LocalizedName);
}
UpdateSeriesMetadata(series, library.Type);
await UpdateSeriesMetadata(series, library);
// Update series FolderPath here
await UpdateSeriesFolderPath(parsedInfos, library, series);
@ -223,10 +226,10 @@ public class ProcessSeries : IProcessSeries
BackgroundJob.Enqueue(() => _wordCountAnalyzerService.ScanSeries(libraryId, seriesId, forceUpdate));
}
private static void UpdateSeriesMetadata(Series series, LibraryType libraryType)
private async Task UpdateSeriesMetadata(Series series, Library library)
{
series.Metadata ??= DbFactory.SeriesMetadata(new List<CollectionTag>());
var isBook = libraryType == LibraryType.Book;
var isBook = library.Type == LibraryType.Book;
var firstChapter = SeriesService.GetFirstChapterForMetadata(series, isBook);
var firstFile = firstChapter?.Files.FirstOrDefault();
@ -278,6 +281,14 @@ public class ProcessSeries : IProcessSeries
series.Metadata.Language = firstChapter.Language;
}
if (!string.IsNullOrEmpty(firstChapter.SeriesGroup) && library.ManageCollections)
{
_logger.LogDebug("Collection tag found for {SeriesName}", series.Name);
var tag = await _collectionTagService.GetTagOrCreate(0, firstChapter.SeriesGroup);
_collectionTagService.AddTagToSeriesMetadata(tag, series.Metadata);
}
// Handle People
foreach (var chapter in chapters)
{
@ -629,6 +640,11 @@ public class ProcessSeries : IProcessSeries
chapter.Language = comicInfo.LanguageISO;
}
if (!string.IsNullOrEmpty(comicInfo.SeriesGroup))
{
chapter.SeriesGroup = comicInfo.SeriesGroup;
}
if (comicInfo.Count > 0)
{
chapter.TotalCount = comicInfo.Count;