Adds book deletion and restore to the web client.

Soft deletes books so the scan keeps their paths reserved, lists the deleted books on a themed page, and lets the user restore them. Updates the API, bundled themes, localization, documentation and tests.

Closes #213
This commit is contained in:
BinaryAlley committed 2026-11-13 18:00:00 +02:00
1 parent ef5ede6325
commit f26b07d4ff
125 files changed
+9856 -378

No files matched your search

@@ -149,6 +149,13 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
builder.Property(book => book.MetadataProvider)
.HasMaxLength(100)
.HasColumnOrder(40);
builder.Property(book => book.IsDeleted)
.IsRequired()
.HasDefaultValue(false)
.HasColumnOrder(41);
builder.Property(book => book.DeletedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(42);
builder.HasMany(book => book.Contributors)
.WithOne()
@@ -226,5 +233,8 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
builder.HasIndex(book => new { book.LibraryId, book.Path })
.IsUnique(); // the same physical book on disk cannot appear twice in the same library
builder.HasIndex(book => new { book.LibraryId, book.MetadataStatus });
// The deleted items page filters by library and deletion state, and orders by the stored deletion time, the title and the Id, so the
// index follows the same order and no temporary B-tree is needed for the sort.
builder.HasIndex(book => new { book.LibraryId, book.IsDeleted, book.DeletedOnUtc, book.Title, book.Id });
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,59 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations
{
/// <inheritdoc />
public partial class AddsBookDeletionState : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "DeletedOnUtc",
table: "Books",
type: "TEXT",
nullable: true)
.Annotation("Relational:ColumnOrder", 42);
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "Books",
type: "INTEGER",
nullable: false,
defaultValue: false)
.Annotation("Relational:ColumnOrder", 41);
// This migration introduces both deletion columns together, so a database coming from an older schema has no deleted books to backfill.
// The statement is kept for databases that were created by an intermediate build that already had IsDeleted.
migrationBuilder.Sql("""
UPDATE Books
SET DeletedOnUtc = COALESCE(UpdatedOnUtc, CreatedOnUtc)
WHERE IsDeleted = 1 AND DeletedOnUtc IS NULL;
""");
migrationBuilder.CreateIndex(
name: "IX_Books_LibraryId_IsDeleted_DeletedOnUtc_Title_Id",
table: "Books",
columns: new[] { "LibraryId", "IsDeleted", "DeletedOnUtc", "Title", "Id" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Books_LibraryId_IsDeleted_DeletedOnUtc_Title_Id",
table: "Books");
migrationBuilder.DropColumn(
name: "DeletedOnUtc",
table: "Books");
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "Books");
}
}
}
@@ -1913,6 +1913,10 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("TEXT")
.HasColumnOrder(32);
b.Property<DateTime?>("DeletedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(42);
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("TEXT")
@@ -1936,6 +1940,12 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("TEXT")
.HasColumnOrder(27);
b.Property<bool>("IsDeleted")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(false)
.HasColumnOrder(41);
b.Property<string>("LCCN")
.HasColumnType("TEXT")
.HasColumnOrder(23);
@@ -2067,6 +2077,8 @@ namespace Lumina.DataAccess.Common.Migrations
b.HasIndex("LibraryId", "Path")
.IsUnique();
b.HasIndex("LibraryId", "IsDeleted", "DeletedOnUtc", "Title", "Id");
b.ToTable("Books", (string)null);
});
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Time;
using Lumina.Application.Common.Specifications;
using Lumina.DataAccess.Common.Persistence;
using Lumina.DataAccess.Core.Repositories.BookLibrary.Specifications;
@@ -38,14 +39,17 @@ internal sealed class BookRepository : IBookRepository
private static readonly MethodInfo s_substringMethod = typeof(string).GetMethod(nameof(string.Substring), [typeof(int)])!;
private readonly LuminaDbContext _luminaDbContext;
private readonly IDateTimeProvider _dateTimeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="BookRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public BookRepository(LuminaDbContext luminaDbContext)
/// <param name="dateTimeProvider">Injected provider for the current UTC time, used to stamp deletions deterministically.</param>
public BookRepository(LuminaDbContext luminaDbContext, IDateTimeProvider dateTimeProvider)
{
_luminaDbContext = luminaDbContext;
_dateTimeProvider = dateTimeProvider;
}
/// <summary>
@@ -88,7 +92,7 @@ internal sealed class BookRepository : IBookRepository
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="BookEntity"/>, or an error.</returns>
public async Task<Result<BookEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
{
IQueryable<BookEntity> query = _luminaDbContext.Books;
IQueryable<BookEntity> query = _luminaDbContext.Books.Where(book => !book.IsDeleted);
if (!shouldTrackEntities)
query = query.AsNoTracking();
if (shouldIncludeNavigationProperties)
@@ -119,7 +123,7 @@ internal sealed class BookRepository : IBookRepository
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="PaginatedResultDto{BookEntity}"/>, or an error.</returns>
public async Task<Result<PaginatedResultDto<BookEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
{
IQueryable<BookEntity> booksQuery = _luminaDbContext.Books;
IQueryable<BookEntity> booksQuery = _luminaDbContext.Books.Where(book => !book.IsDeleted);
if (!shouldTrackEntities)
booksQuery = booksQuery.AsNoTracking();
if (shouldIncludeNavigationProperties)
@@ -202,7 +206,7 @@ internal sealed class BookRepository : IBookRepository
return Errors.Library.FilterMustIncludeLibraryId;
// The lightweight read models are retrieved without tracking by default, because they are never modified by the caller.
IQueryable<BookEntity> booksQuery = _luminaDbContext.Books;
IQueryable<BookEntity> booksQuery = _luminaDbContext.Books.Where(book => !book.IsDeleted);
if (!shouldTrackEntities)
booksQuery = booksQuery.AsNoTracking();
if (shouldIncludeNavigationProperties)
@@ -272,6 +276,10 @@ internal sealed class BookRepository : IBookRepository
/// <summary>
/// Gets the book of the media library identified by <paramref name="libraryId"/> that is stored at the provided <paramref name="path"/>.
/// </summary>
/// <remarks>
/// Soft deleted books are included on purpose: when the file disappears from disk, the scan deletion strategy must be able to permanently
/// remove a book that the user had already deleted, instead of leaving its soft deleted row behind.
/// </remarks>
/// <param name="libraryId">The Id of the media library whose book is retrieved.</param>
/// <param name="path">The file system path of the book to be retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -287,9 +295,83 @@ internal sealed class BookRepository : IBookRepository
.FirstOrDefaultAsync(book => book.LibraryId == libraryId && book.Path == path, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the soft deleted book identified by <paramref name="id"/>.
/// </summary>
/// <param name="id">The Id of the soft deleted book to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the soft deleted <see cref="BookEntity"/>, <see langword="null"/> when the book does not exist or is not soft deleted, or an error.</returns>
public async Task<Result<BookEntity?>> GetSoftDeletedByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.Books
.Include(book => book.Artwork)
.AsSplitQuery()
.FirstOrDefaultAsync(book => book.Id == id && book.IsDeleted, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the soft deleted books of the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose soft deleted books are retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the soft deleted books, or an error.</returns>
public async Task<Result<IReadOnlyList<BookEntity>>> GetSoftDeletedByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
{
return await _luminaDbContext.Books
.AsNoTracking()
.Include(book => book.Artwork)
.AsSplitQuery()
.Where(book => book.LibraryId == libraryId && book.IsDeleted)
// The deletion time is stored when the book is deleted, so later writes to the hidden row cannot change the order of the deleted items.
// Every deleted row carries a deletion time after the backfill and the delete operation. All terms sort descending, so the
// (LibraryId, IsDeleted, DeletedOnUtc, Title, Id) index can be scanned backwards and the database does not build a temporary B-tree.
.OrderByDescending(book => book.DeletedOnUtc)
.ThenByDescending(book => book.Title)
.ThenByDescending(book => book.Id)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Soft deletes the book identified by <paramref name="id"/>, without removing its stored metadata or artwork.
/// </summary>
/// <param name="id">The Id of the book to soft delete.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Updated>> SoftDeleteByIdAsync(Guid id, CancellationToken cancellationToken)
{
BookEntity? book = await _luminaDbContext.Books.FirstOrDefaultAsync(repositoryBook => repositoryBook.Id == id, cancellationToken).ConfigureAwait(false);
if (book is null || book.IsDeleted)
return Errors.WrittenContent.BookNotFound;
book.IsDeleted = true;
book.DeletedOnUtc = _dateTimeProvider.UtcNow;
return Result.Updated;
}
/// <summary>
/// Restores the soft deleted book identified by <paramref name="id"/>.
/// </summary>
/// <param name="id">The Id of the book to restore.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Updated>> RestoreSoftDeletedByIdAsync(Guid id, CancellationToken cancellationToken)
{
BookEntity? book = await _luminaDbContext.Books.FirstOrDefaultAsync(repositoryBook => repositoryBook.Id == id, cancellationToken).ConfigureAwait(false);
if (book is null || !book.IsDeleted)
return Errors.WrittenContent.BookNotFound;
book.IsDeleted = false;
book.DeletedOnUtc = null;
return Result.Updated;
}
/// <summary>
/// Gets the subset of <paramref name="paths"/> that is already used by a book of the library identified by <paramref name="libraryId"/>.
/// </summary>
/// <remarks>
/// Soft deleted books are included on purpose: their paths must still count as taken, so that a scan never materializes a book that the user
/// deleted back into the catalogue.
/// </remarks>
/// <param name="libraryId">The Id of the library whose books are searched.</param>
/// <param name="paths">The book paths to check.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -331,6 +413,7 @@ internal sealed class BookRepository : IBookRepository
.Include(book => book.Contributors)
.AsSplitQuery()
.Where(book => book.LibraryId == libraryId
&& !book.IsDeleted
&& book.MetadataStatus != MetadataStatus.Enriched
&& (lastPath == null || book.Path.CompareTo(lastPath) > 0))
.OrderBy(book => book.Path)
@@ -347,7 +430,7 @@ internal sealed class BookRepository : IBookRepository
public async Task<Result<int>> GetBooksNeedingMetadataCountAsync(Guid libraryId, CancellationToken cancellationToken)
{
return await _luminaDbContext.Books
.CountAsync(book => book.LibraryId == libraryId && book.MetadataStatus != MetadataStatus.Enriched, cancellationToken).ConfigureAwait(false);
.CountAsync(book => book.LibraryId == libraryId && !book.IsDeleted && book.MetadataStatus != MetadataStatus.Enriched, cancellationToken).ConfigureAwait(false);
}
/// <summary>
@@ -368,6 +451,7 @@ internal sealed class BookRepository : IBookRepository
.Include(book => book.Artwork)
.AsSplitQuery()
.Where(book => book.LibraryId == libraryId
&& !book.IsDeleted
&& !book.Artwork.Any(artwork => artwork.ArtworkType == ArtworkType.Cover && artwork.Status == ArtworkStatus.Enriched)
&& (lastPath == null || book.Path.CompareTo(lastPath) > 0))
.OrderBy(book => book.Path)
@@ -385,6 +469,7 @@ internal sealed class BookRepository : IBookRepository
{
return await _luminaDbContext.Books
.CountAsync(book => book.LibraryId == libraryId
&& !book.IsDeleted
&& !book.Artwork.Any(artwork => artwork.ArtworkType == ArtworkType.Cover && artwork.Status == ArtworkStatus.Enriched), cancellationToken)
.ConfigureAwait(false);
}
@@ -393,6 +478,9 @@ internal sealed class BookRepository : IBookRepository
/// Resets the enrichment state of the books stored at the provided <paramref name="paths"/> in the media library identified by
/// <paramref name="libraryId"/>, so that they are re-enriched, because their content changed since the last scan.
/// </summary>
/// <remarks>
/// Soft deleted books are included on purpose: if such a book is restored after its content changed, it must be re-enriched instead of keeping stale metadata.
/// </remarks>
/// <param name="libraryId">The Id of the media library whose books are reset.</param>
/// <param name="paths">The file system paths of the books whose enrichment state is reset.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -426,6 +514,9 @@ internal sealed class BookRepository : IBookRepository
/// Resets the metadata enrichment status of all the books of the media library identified by <paramref name="libraryId"/>,
/// so that they are re-enriched, because the metadata provider configuration of the library changed.
/// </summary>
/// <remarks>
/// Soft deleted books are included on purpose: if such a book is restored after the provider configuration changed, it must be re-enriched with the new providers.
/// </remarks>
/// <param name="libraryId">The Id of the media library whose books are reset.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
@@ -443,6 +534,9 @@ internal sealed class BookRepository : IBookRepository
/// Resets the artwork status of all the artwork of the books of the media library identified by <paramref name="libraryId"/>,
/// so that they are re-resolved, because the artwork provider configuration of the library changed.
/// </summary>
/// <remarks>
/// Soft deleted books are included on purpose: if such a book is restored after the provider configuration changed, its artwork must be re-resolved with the new providers.
/// </remarks>
/// <param name="libraryId">The Id of the media library whose artwork is reset.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
@@ -514,7 +608,7 @@ internal sealed class BookRepository : IBookRepository
const int LOOKUP_CHUNK_SIZE = 500;
foreach (Guid[] chunk in bookIds.Chunk(LOOKUP_CHUNK_SIZE))
{
IQueryable<BookEntity> query = _luminaDbContext.Books.AsNoTracking();
IQueryable<BookEntity> query = _luminaDbContext.Books.AsNoTracking().Where(book => !book.IsDeleted);
if (shouldIncludeNavigationProperties)
{
query = query
@@ -570,20 +664,29 @@ internal sealed class BookRepository : IBookRepository
.Include(book => book.Ratings)
.Include(book => book.Contributors)
.AsSplitQuery()
.FirstOrDefaultAsync(book => book.Id == data.Id, cancellationToken).ConfigureAwait(false);
.FirstOrDefaultAsync(book => book.Id == data.Id && !book.IsDeleted, cancellationToken).ConfigureAwait(false);
if (foundBook is null)
return Errors.WrittenContent.BookNotFound;
// The stored enrichment and identity columns are never overwritten by an edit: they are owned by the scan and by the record itself. The audit columns are
// only ever written by the auditing interceptor, which is why the copier restores them after copying the editable values.
// The stored enrichment, identity and deletion columns are never overwritten by an edit: they are owned by the scan, by the record itself
// and by the explicit delete and restore operations. The audit columns are only ever written by the auditing interceptor, which is why the
// copier restores them after copying the editable values.
Guid libraryId = foundBook.LibraryId;
string path = foundBook.Path;
bool isDeleted = foundBook.IsDeleted;
DateTime? deletedOnUtc = foundBook.DeletedOnUtc;
MetadataStatus metadataStatus = foundBook.MetadataStatus;
DateTime? lastMetadataUpdateUtc = foundBook.LastMetadataUpdateUtc;
string? metadataProvider = foundBook.MetadataProvider;
EditableValuesCopier.CopyEditableValues(_luminaDbContext, foundBook, data);
foundBook.LibraryId = libraryId;
foundBook.Path = path;
foundBook.IsDeleted = isDeleted;
foundBook.DeletedOnUtc = deletedOnUtc;
// The deletion state is owned by the explicit delete and restore operations. Clearing the modified flags keeps it out of the UPDATE statement,
// so an edit that raced with a delete can never write the deletion state back and resurrect the book.
_luminaDbContext.Entry(foundBook).Property(book => book.IsDeleted).IsModified = false;
_luminaDbContext.Entry(foundBook).Property(book => book.DeletedOnUtc).IsModified = false;
foundBook.MetadataStatus = metadataStatus;
foundBook.LastMetadataUpdateUtc = lastMetadataUpdateUtc;
foundBook.MetadataProvider = metadataProvider;
@@ -25,8 +25,9 @@ namespace Lumina.DataAccess.Core.Repositories.Favorites;
/// </summary>
/// <remarks>
/// A favorite references its media item by kind and Id without a foreign key, so reading a page of favorites resolves the display data of
/// the referenced items with one batched query per media kind present in the page, and a favorite whose item no longer exists is kept
/// without display data. Duplicate favorites of the same user on the same item are rejected by the unique (UserId, MediaKind, MediaItemId) index.
/// the referenced items with one batched query per media kind present in the page. A favorite whose item no longer exists, or whose book
/// was soft deleted, is kept without display data. Duplicate favorites of the same user on the same item are rejected by the unique
/// (UserId, MediaKind, MediaItemId) index.
/// </remarks>
internal sealed class FavoriteRepository : IFavoriteRepository
{
@@ -127,7 +128,7 @@ internal sealed class FavoriteRepository : IFavoriteRepository
.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false),
FavoriteMediaKind.Book => await _luminaDbContext.Books
.AsNoTracking()
.Where(book => book.Id == mediaItemId)
.Where(book => book.Id == mediaItemId && !book.IsDeleted)
.Select(book => (Guid?)book.LibraryId)
.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false),
_ => null,
@@ -350,7 +351,7 @@ internal sealed class FavoriteRepository : IFavoriteRepository
// the same author the book listing shows, and the Id is the tiebreaker between authors that share a display name.
rows = await _luminaDbContext.Books
.AsNoTracking()
.Where(book => requestedIds.Contains(book.Id))
.Where(book => requestedIds.Contains(book.Id) && !book.IsDeleted)
.Select(book => new DisplayRow(
book.Id,
book.LibraryId,
+6 -2
View File
@@ -13,6 +13,7 @@ using Lumina.Application.Common.DataAccess.Repositories.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Time;
using Lumina.DataAccess.Core.Repositories.Authorization;
using Lumina.DataAccess.Core.Repositories.BookLibrary;
using Lumina.DataAccess.Core.Repositories.Favorites;
@@ -42,6 +43,7 @@ namespace Lumina.DataAccess.Core.UoW;
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly LuminaDbContext _luminaDbContext;
private readonly IDateTimeProvider _dateTimeProvider;
private IDbContextTransaction? _transaction;
private IPermissionRepository? _permissionRepository;
@@ -131,7 +133,7 @@ public class UnitOfWork : IUnitOfWork, IDisposable
{
get
{
_bookRepository ??= new BookRepository(_luminaDbContext);
_bookRepository ??= new BookRepository(_luminaDbContext, _dateTimeProvider);
return _bookRepository;
}
}
@@ -449,9 +451,11 @@ public class UnitOfWork : IUnitOfWork, IDisposable
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public UnitOfWork(LuminaDbContext luminaDbContext)
/// <param name="dateTimeProvider">Injected provider for the current UTC time, passed to the repositories that stamp their own timestamps.</param>
public UnitOfWork(LuminaDbContext luminaDbContext, IDateTimeProvider dateTimeProvider)
{
_luminaDbContext = luminaDbContext;
_dateTimeProvider = dateTimeProvider;
}
/// <summary>