Adds per-library consumption settings to the domain, storage, and API

This commit is contained in:
BinaryAlley committed 2026-10-24 10:20:00 +03:00
1 parent eaca69bf2a
commit b2d274f609
47 files changed
+4371 -11

No files matched your search

@@ -80,6 +80,16 @@ public class LibraryEntity : IStorageEntity, IAuditableEntity
/// </summary>
public bool ShouldSkipUnchangedDirectoriesDuringScan { get; init; }
/// <summary>
/// Gets a value indicating whether the consumption progress of the media library items is tracked.
/// </summary>
public bool ShouldTrackConsumptionProgress { get; init; }
/// <summary>
/// Gets the percentage of a media library item that must be consumed for its session to be considered completed, when progress tracking is enabled.
/// </summary>
public int? CompletionThresholdPercentage { get; init; }
/// <summary>
/// Gets or sets the fingerprint of the metadata provider configuration of the media library, used to detect
/// whether the configuration changed since the last scan, and thus whether the books need re-enrichment.
@@ -29,6 +29,8 @@ public static class AddMediaLibraryRequestMapping
request.CanDownloadMetadataFromWeb,
request.ShouldSaveMetadataInMediaDirectories,
request.ShouldSkipUnchangedDirectoriesDuringScan,
request.ShouldTrackConsumptionProgress,
request.CompletionThresholdPercentage,
request.PathTemplateParts?.Select(part => new LibraryPathTemplatePartDto(part.Kind, part.Representation, part.IsOptional)).ToArray()
);
}
@@ -36,6 +36,8 @@ public static class LibraryEntityMapping
repositoryEntity.CanDownloadMetadataFromWeb,
repositoryEntity.ShouldSaveMetadataInMediaDirectories,
repositoryEntity.ShouldSkipUnchangedDirectoriesDuringScan,
repositoryEntity.ShouldTrackConsumptionProgress,
repositoryEntity.CompletionThresholdPercentage,
[.. repositoryEntity.PathTemplateParts
.OrderBy(part => part.Position)
.Select(part => new LibraryPathTemplatePartResponse(part.Kind.ToString(), part.Representation, part.IsOptional))],
@@ -57,6 +59,10 @@ public static class LibraryEntityMapping
if (pathTemplateResult.IsFailure)
return pathTemplateResult.Errors;
LibraryConsumptionSettings consumptionSettings = LibraryConsumptionSettings.Create(
repositoryEntity.ShouldTrackConsumptionProgress,
Optional<int>.FromNullable(repositoryEntity.CompletionThresholdPercentage));
return Library.Create(
LibraryId.Create(repositoryEntity.Id),
UserId.Create(repositoryEntity.UserId),
@@ -69,6 +75,7 @@ public static class LibraryEntityMapping
repositoryEntity.CanDownloadMetadataFromWeb,
repositoryEntity.ShouldSaveMetadataInMediaDirectories,
repositoryEntity.ShouldSkipUnchangedDirectoriesDuringScan,
consumptionSettings,
pathTemplateResult.Value,
[.. repositoryEntity.LibraryScans.Select(libraryScan => ScanId.Create(libraryScan.Id))]
);
@@ -39,6 +39,10 @@ public static class LibraryMapping
CanDownloadMetadataFromWeb = domainEntity.CanDownloadMetadataFromWeb,
ShouldSaveMetadataInMediaDirectories = domainEntity.ShouldSaveMetadataInMediaDirectories,
ShouldSkipUnchangedDirectoriesDuringScan = domainEntity.ShouldSkipUnchangedDirectoriesDuringScan,
ShouldTrackConsumptionProgress = domainEntity.ConsumptionSettings.ShouldTrackConsumptionProgress,
CompletionThresholdPercentage = domainEntity.ConsumptionSettings.CompletionThresholdPercentage.HasValue
? domainEntity.ConsumptionSettings.CompletionThresholdPercentage.Value
: null,
LibraryScans = [.. domainEntity.ScanIds.Select(scanId => new LibraryScanEntity()
{
Id = scanId.Value,
@@ -31,6 +31,8 @@ public static class UpdateLibraryRequestMapping
request.CanDownloadMetadataFromWeb,
request.ShouldSaveMetadataInMediaDirectories,
request.ShouldSkipUnchangedDirectoriesDuringScan,
request.ShouldTrackConsumptionProgress,
request.CompletionThresholdPercentage,
request.PathTemplateParts?.Select(part => new LibraryPathTemplatePartDto(part.Kind, part.Representation, part.IsOptional)).ToArray()
);
}
@@ -18,6 +18,8 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
/// <param name="CanDownloadMetadataFromWeb">Whether this media library should update the metadata of its elements from the web, or not.</param>
/// <param name="ShouldSaveMetadataInMediaDirectories">Whether this media library should copy the downloaded metadata into the media library content locations, or not.</param>
/// <param name="ShouldSkipUnchangedDirectoriesDuringScan">Whether this media library should skip the directories whose contents have not changed since the last scan, during the scan, or not.</param>
/// <param name="ShouldTrackConsumptionProgress">Whether the consumption progress of the media library items should be tracked, or not. When absent, the default of the library type is used.</param>
/// <param name="CompletionThresholdPercentage">The percentage of a media library item that must be consumed for its session to be considered completed. When absent, the default of the library type is used on creation.</param>
/// <param name="PathTemplateParts">The ordered parts of the template describing the structure of the media library on disk.</param>
[DebuggerDisplay("Title: {Title}")]
public record AddLibraryCommand(
@@ -30,5 +32,7 @@ public record AddLibraryCommand(
bool CanDownloadMetadataFromWeb,
bool ShouldSaveMetadataInMediaDirectories,
bool ShouldSkipUnchangedDirectoriesDuringScan,
bool? ShouldTrackConsumptionProgress,
int? CompletionThresholdPercentage,
LibraryPathTemplatePartDto[]? PathTemplateParts
) : ICommand;
@@ -116,6 +116,17 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
LibraryType libraryType = Enum.Parse<LibraryType>(command.LibraryType!);
// A library created without explicit consumption settings uses the defaults of its type. When progress tracking is on and no threshold is
// supplied, the default threshold of the type is used; a type without one keeps no threshold, which means completion is recorded explicitly.
LibraryConsumptionSettings defaultConsumptionSettings = LibraryConsumptionSettings.CreateDefault(libraryType);
bool shouldTrackConsumptionProgress = command.ShouldTrackConsumptionProgress ?? defaultConsumptionSettings.ShouldTrackConsumptionProgress;
Optional<int> completionThresholdPercentage = Optional<int>.FromNullable(command.CompletionThresholdPercentage);
if (completionThresholdPercentage.HasValue && !shouldTrackConsumptionProgress)
return DomainErrors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff;
if (!completionThresholdPercentage.HasValue && shouldTrackConsumptionProgress)
completionThresholdPercentage = defaultConsumptionSettings.CompletionThresholdPercentage;
LibraryConsumptionSettings consumptionSettings = LibraryConsumptionSettings.Create(shouldTrackConsumptionProgress, completionThresholdPercentage);
// A library always carries a path template: the one provided by the user, or the ideal structure of the library type as its default.
Result<IReadOnlyList<LibraryPathPart>> pathTemplatePartsResult = command.PathTemplateParts.ToDomainParts();
if (pathTemplatePartsResult.IsFailure)
@@ -136,6 +147,7 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
command.CanDownloadMetadataFromWeb,
command.ShouldSaveMetadataInMediaDirectories,
command.ShouldSkipUnchangedDirectoriesDuringScan,
consumptionSettings,
pathTemplateResult.Value,
[]
);
@@ -42,6 +42,14 @@ public class AddLibraryCommandValidator : AbstractValidator<AddLibraryCommand>
.MaximumLength(255)
.WithError(Errors.Library.TitleMustBeMaximum255CharactersLong);
RuleFor(command => command.CompletionThresholdPercentage)
.Must((command, completionThresholdPercentage) => command.ShouldTrackConsumptionProgress != false || completionThresholdPercentage is null)
.WithError(Errors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff);
RuleFor(command => command.CompletionThresholdPercentage)
.InclusiveBetween(0, 100)
.WithError(Errors.Library.CompletionThresholdPercentageMustBeBetweenZeroAndOneHundred);
RuleForEach(command => command.PathTemplateParts)
.ChildRules(part =>
{
@@ -21,6 +21,8 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.UpdateLibrary
/// <param name="CanDownloadMetadataFromWeb">Whether this media library should update the metadata of its elements from the web, or not.</param>
/// <param name="ShouldSaveMetadataInMediaDirectories">Whether this media library should copy the downloaded metadata into the media library content locations, or not.</param>
/// <param name="ShouldSkipUnchangedDirectoriesDuringScan">Whether this media library should skip the directories whose contents have not changed since the last scan, during the scan, or not.</param>
/// <param name="ShouldTrackConsumptionProgress">Whether the consumption progress of the media library items should be tracked, or not. When absent, the stored value is kept.</param>
/// <param name="CompletionThresholdPercentage">The percentage of a media library item that must be consumed for its session to be considered completed. When absent, the stored value is kept.</param>
/// <param name="PathTemplateParts">The ordered parts of the template describing the structure of the media library on disk.</param>
[DebuggerDisplay("Title: {Title}")]
public record UpdateLibraryCommand(
@@ -35,5 +37,7 @@ public record UpdateLibraryCommand(
bool CanDownloadMetadataFromWeb,
bool ShouldSaveMetadataInMediaDirectories,
bool ShouldSkipUnchangedDirectoriesDuringScan,
bool? ShouldTrackConsumptionProgress,
int? CompletionThresholdPercentage,
LibraryPathTemplatePartDto[]? PathTemplateParts
) : ICommand;
@@ -130,6 +130,25 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
// Create a domain library object.
LibraryType newLibraryType = Enum.Parse<LibraryType>(command.LibraryType!);
// Keep the stored consumption settings unless the request explicitly changes them. When progress tracking is enabled and
// no threshold is supplied, the stored threshold is kept; the value object never retains a threshold while tracking is disabled.
LibraryConsumptionSettings existingConsumptionSettings = LibraryConsumptionSettings.Create(
getLibraryResult.Value.ShouldTrackConsumptionProgress,
Optional<int>.FromNullable(getLibraryResult.Value.CompletionThresholdPercentage));
bool shouldTrackConsumptionProgress = command.ShouldTrackConsumptionProgress ?? existingConsumptionSettings.ShouldTrackConsumptionProgress;
bool isEnablingConsumptionProgressTracking = shouldTrackConsumptionProgress && !existingConsumptionSettings.ShouldTrackConsumptionProgress;
Optional<int> completionThresholdPercentage = Optional<int>.FromNullable(command.CompletionThresholdPercentage);
if (completionThresholdPercentage.HasValue && !shouldTrackConsumptionProgress)
return DomainErrors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff;
if (!completionThresholdPercentage.HasValue && shouldTrackConsumptionProgress)
{
completionThresholdPercentage = existingConsumptionSettings.CompletionThresholdPercentage;
// Enabling progress tracking on a library that did not track it yet must use the same type default that creating the library would have used.
if (!completionThresholdPercentage.HasValue && isEnablingConsumptionProgressTracking)
completionThresholdPercentage = LibraryConsumptionSettings.CreateDefault(newLibraryType).CompletionThresholdPercentage;
}
LibraryConsumptionSettings consumptionSettings = LibraryConsumptionSettings.Create(shouldTrackConsumptionProgress, completionThresholdPercentage);
// A library always carries a path template: the one provided by the user, or the ideal structure of the library type as its default.
Result<IReadOnlyList<LibraryPathPart>> pathTemplatePartsResult = command.PathTemplateParts.ToDomainParts();
if (pathTemplatePartsResult.IsFailure)
@@ -150,6 +169,7 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
command.CanDownloadMetadataFromWeb,
command.ShouldSaveMetadataInMediaDirectories,
command.ShouldSkipUnchangedDirectoriesDuringScan,
consumptionSettings,
pathTemplateResult.Value,
[.. getLibraryResult.Value.LibraryScans.Select(libraryScan => ScanId.Create(libraryScan.Id))]
);
@@ -54,6 +54,14 @@ public class UpdateLibraryCommandValidator : AbstractValidator<UpdateLibraryComm
.MaximumLength(255)
.WithError(Errors.Library.TitleMustBeMaximum255CharactersLong);
RuleFor(command => command.CompletionThresholdPercentage)
.Must((command, completionThresholdPercentage) => command.ShouldTrackConsumptionProgress != false || completionThresholdPercentage is null)
.WithError(Errors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff);
RuleFor(command => command.CompletionThresholdPercentage)
.InclusiveBetween(0, 100)
.WithError(Errors.Library.CompletionThresholdPercentageMustBeBetweenZeroAndOneHundred);
RuleForEach(command => command.PathTemplateParts)
.ChildRules(part =>
{