mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 02:11:10 +00:00
Wires book reader consumption reporting and resume
This commit is contained in:
1 parent
bfa7b190b2
commit
8011c532a1
28 files changed
+1037
-167
No files matched your search
+2
-1
@@ -24,7 +24,8 @@ public static class CompleteConsumptionRequestMapping
|
||||
request.Position?.TimeOffsetSeconds,
|
||||
request.Position?.Location,
|
||||
request.ProgressFraction,
|
||||
request.ConsumedDurationInSeconds
|
||||
request.ConsumedDurationInSeconds,
|
||||
request.ConsecutivePagesRead
|
||||
);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -24,7 +24,8 @@ public static class StopConsumptionRequestMapping
|
||||
request.Position?.TimeOffsetSeconds,
|
||||
request.Position?.Location,
|
||||
request.ProgressFraction,
|
||||
request.ConsumedDurationInSeconds
|
||||
request.ConsumedDurationInSeconds,
|
||||
request.ConsecutivePagesRead
|
||||
);
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -14,12 +14,15 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.CompleteConsumption;
|
||||
/// <param name="PositionLocation">The final opaque locator within the media item, when the position is not a time offset.</param>
|
||||
/// <param name="ProgressFraction">The final fraction of the media item that the client reached, between 0 and 1.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds.</param>
|
||||
[DebuggerDisplay("ConsumptionRecordId: {ConsumptionRecordId}; ProgressFraction: {ProgressFraction}")]
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the session, used by page based media such as books.
|
||||
/// It must be absent or zero for other kinds. When it is absent, the count of the last progress report is kept.</param>
|
||||
[DebuggerDisplay("ConsumptionRecordId: {ConsumptionRecordId}; ProgressFraction: {ProgressFraction}; ConsecutivePagesRead: {ConsecutivePagesRead}")]
|
||||
public record CompleteConsumptionCommand(
|
||||
string? ConsumptionRecordId,
|
||||
string? PositionKind,
|
||||
double? PositionTimeOffsetInSeconds,
|
||||
string? PositionLocation,
|
||||
double? ProgressFraction,
|
||||
double? ConsumedDurationInSeconds
|
||||
double? ConsumedDurationInSeconds,
|
||||
int? ConsecutivePagesRead
|
||||
) : ICommand;
|
||||
+67
-39
@@ -14,6 +14,7 @@ using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaCon
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
@@ -27,6 +28,12 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.CompleteConsumption;
|
||||
/// </summary>
|
||||
public class CompleteConsumptionCommandHandler : ICommandHandler<CompleteConsumptionCommand, Result<ConsumptionRecordResponse>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum number of times the terminal state is applied when a concurrent progress report changes the record between its
|
||||
/// read and its save.
|
||||
/// </summary>
|
||||
private const int MAXIMUM_TERMINAL_SAVE_ATTEMPTS = 3;
|
||||
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IDomainEventsQueue _domainEventsQueue;
|
||||
@@ -74,53 +81,74 @@ public class CompleteConsumptionCommandHandler : ICommandHandler<CompleteConsump
|
||||
// The validator guarantees that the route identifier and the reported measurements are valid before this point.
|
||||
Guid consumptionRecordId = Guid.Parse(command.ConsumptionRecordId!);
|
||||
|
||||
// The record is loaded scoped to the current user, so a record of another user is indistinguishable from a missing one.
|
||||
Result<MediaConsumptionRecordEntity?> getRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(consumptionRecordId, userId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getRecordResult.IsFailure)
|
||||
return getRecordResult.Errors;
|
||||
if (getRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
Result<MediaConsumptionRecord> toDomainEntityResult = getRecordResult.Value.ToDomainEntity();
|
||||
if (toDomainEntityResult.IsFailure)
|
||||
return toDomainEntityResult.Errors;
|
||||
MediaConsumptionRecord record = toDomainEntityResult.Value;
|
||||
|
||||
Optional<MediaPosition> position = ToMediaPosition(command.PositionKind, command.PositionTimeOffsetInSeconds, command.PositionLocation);
|
||||
Optional<int> consecutivePagesRead = command.ConsecutivePagesRead.HasValue
|
||||
? Optional<int>.Some(command.ConsecutivePagesRead.Value)
|
||||
: Optional<int>.None();
|
||||
|
||||
Result<Updated> completeResult = record.Complete(position, command.ProgressFraction!.Value, TimeSpan.FromSeconds(command.ConsumedDurationInSeconds!.Value));
|
||||
if (completeResult.IsFailure)
|
||||
return completeResult.Errors;
|
||||
// A terminal report can race with an in-flight progress report on the same record. The record is read, then saved with the
|
||||
// version it was read with, so a progress report that commits in between makes the save fail with a concurrency conflict.
|
||||
// The terminal payload is absolute, so the record is reloaded and the terminal state is applied again to the fresh version.
|
||||
for (int attempt = 1; attempt <= MAXIMUM_TERMINAL_SAVE_ATTEMPTS; attempt++)
|
||||
{
|
||||
// The record is loaded scoped to the current user, so a record of another user is indistinguishable from a missing one.
|
||||
Result<MediaConsumptionRecordEntity?> getRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(consumptionRecordId, userId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getRecordResult.IsFailure)
|
||||
return getRecordResult.Errors;
|
||||
if (getRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
// Completion discards the resume point, because the media item no longer has a point to continue from. A missing resume point is not an
|
||||
// error, so an explicit completion after a completion that already deleted it stays idempotent.
|
||||
Result<Deleted> deleteResumePointResult = await _unitOfWork.MediaResumePointRepository
|
||||
.DeleteForUserAndMediaItemAsync(userId, record.MediaKind, record.MediaItemId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteResumePointResult.IsFailure)
|
||||
return deleteResumePointResult.Errors;
|
||||
Result<MediaConsumptionRecord> toDomainEntityResult = getRecordResult.Value.ToDomainEntity();
|
||||
if (toDomainEntityResult.IsFailure)
|
||||
return toDomainEntityResult.Errors;
|
||||
MediaConsumptionRecord record = toDomainEntityResult.Value;
|
||||
|
||||
Result<Updated> updateResult = await _unitOfWork.MediaConsumptionRecordRepository.UpdateForUserAsync(record.ToRepositoryEntity(), userId, cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
Result<Updated> completeResult = record.Complete(position, command.ProgressFraction!.Value, TimeSpan.FromSeconds(command.ConsumedDurationInSeconds!.Value), consecutivePagesRead);
|
||||
if (completeResult.IsFailure)
|
||||
return completeResult.Errors;
|
||||
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
// Completion discards the resume point, because the media item no longer has a point to continue from. A missing resume point is not an
|
||||
// error, so an explicit completion after a completion that already deleted it stays idempotent.
|
||||
Result<Deleted> deleteResumePointResult = await _unitOfWork.MediaResumePointRepository
|
||||
.DeleteForUserAndMediaItemAsync(userId, record.MediaKind, record.MediaItemId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteResumePointResult.IsFailure)
|
||||
return deleteResumePointResult.Errors;
|
||||
|
||||
// Queue any domain events, to be published after the request has completed successfully.
|
||||
foreach (IDomainEvent domainEvent in record.GetDomainEvents())
|
||||
_domainEventsQueue.Enqueue(domainEvent);
|
||||
Result<Updated> updateResult = await _unitOfWork.MediaConsumptionRecordRepository.UpdateForUserAsync(record.ToRepositoryEntity(), userId, cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
|
||||
// The persisted row is read back, so the response carries the timestamps and version the persistence medium actually stored.
|
||||
Result<MediaConsumptionRecordEntity?> getPersistedRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(record.Id.Value, userId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getPersistedRecordResult.IsFailure)
|
||||
return getPersistedRecordResult.Errors;
|
||||
if (getPersistedRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
{
|
||||
// Only a concurrency conflict is worth retrying. The failed attempt is discarded, so the next loop reads the version
|
||||
// the progress report committed and applies the terminal state to it.
|
||||
if (attempt < MAXIMUM_TERMINAL_SAVE_ATTEMPTS && saveChangesResult.Errors.Any(error => error == ApplicationErrors.Persistence.ConcurrencyConflict))
|
||||
{
|
||||
_unitOfWork.ClearTrackedEntities();
|
||||
continue;
|
||||
}
|
||||
return saveChangesResult.Errors;
|
||||
}
|
||||
|
||||
return getPersistedRecordResult.Value.ToDto().ToResponse();
|
||||
// Queue any domain events, to be published after the request has completed successfully.
|
||||
foreach (IDomainEvent domainEvent in record.GetDomainEvents())
|
||||
_domainEventsQueue.Enqueue(domainEvent);
|
||||
|
||||
// The persisted row is read back, so the response carries the timestamps and version the persistence medium actually stored.
|
||||
Result<MediaConsumptionRecordEntity?> getPersistedRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(record.Id.Value, userId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getPersistedRecordResult.IsFailure)
|
||||
return getPersistedRecordResult.Errors;
|
||||
if (getPersistedRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
return getPersistedRecordResult.Value.ToDto().ToResponse();
|
||||
}
|
||||
|
||||
// Every attempt conflicted with a concurrent write, so the terminal state could not be stored.
|
||||
return ApplicationErrors.Persistence.ConcurrencyConflict;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+4
@@ -62,6 +62,10 @@ public class CompleteConsumptionCommandValidator : AbstractValidator<CompleteCon
|
||||
.Must(consumedDuration => consumedDuration is null ||
|
||||
consumedDuration.Value <= MAXIMUM_REPORTED_DURATION_IN_SECONDS)
|
||||
.WithError(Errors.MediaConsumption.ConsumedDurationIsOutOfRange);
|
||||
|
||||
RuleFor(command => command.ConsecutivePagesRead)
|
||||
.Must(consecutivePagesRead => consecutivePagesRead is null || consecutivePagesRead.Value >= 0)
|
||||
.WithError(Errors.MediaConsumption.ConsecutivePagesReadMustBeNonNegative);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.ReportConsumptionPro
|
||||
/// <param name="PositionLocation">The opaque locator within the media item, when the position is not a time offset.</param>
|
||||
/// <param name="ProgressFraction">The fraction of the media item that the client reached, between 0 and 1.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds.</param>
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the current session, used by page based media such as books.</param>
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the current session, used by page based media such as books. It must be zero for other kinds. Defaults to 0. Optional.</param>
|
||||
[DebuggerDisplay("ConsumptionRecordId: {ConsumptionRecordId}; ProgressFraction: {ProgressFraction}")]
|
||||
public record ReportConsumptionProgressCommand(
|
||||
string? ConsumptionRecordId,
|
||||
|
||||
+4
-2
@@ -14,12 +14,14 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.StopConsumption;
|
||||
/// <param name="PositionLocation">The final opaque locator within the media item, when the position is not a time offset.</param>
|
||||
/// <param name="ProgressFraction">The final fraction of the media item that the client reached, between 0 and 1.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds.</param>
|
||||
[DebuggerDisplay("ConsumptionRecordId: {ConsumptionRecordId}; ProgressFraction: {ProgressFraction}")]
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the session, used by page based media such as books. It must be absent or zero for other kinds. When it is absent, the count of the last progress report is kept.</param>
|
||||
[DebuggerDisplay("ConsumptionRecordId: {ConsumptionRecordId}; ProgressFraction: {ProgressFraction}; ConsecutivePagesRead: {ConsecutivePagesRead}")]
|
||||
public record StopConsumptionCommand(
|
||||
string? ConsumptionRecordId,
|
||||
string? PositionKind,
|
||||
double? PositionTimeOffsetInSeconds,
|
||||
string? PositionLocation,
|
||||
double? ProgressFraction,
|
||||
double? ConsumedDurationInSeconds
|
||||
double? ConsumedDurationInSeconds,
|
||||
int? ConsecutivePagesRead
|
||||
) : ICommand;
|
||||
+100
-72
@@ -18,6 +18,7 @@ using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaRes
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
@@ -31,6 +32,12 @@ namespace Lumina.Application.Core.MediaConsumption.Commands.StopConsumption;
|
||||
/// </summary>
|
||||
public class StopConsumptionCommandHandler : ICommandHandler<StopConsumptionCommand, Result<ConsumptionRecordResponse>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum number of times the terminal state is applied when a concurrent progress report changes the record between its
|
||||
/// read and its save.
|
||||
/// </summary>
|
||||
private const int MAXIMUM_TERMINAL_SAVE_ATTEMPTS = 3;
|
||||
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IDomainEventsQueue _domainEventsQueue;
|
||||
@@ -78,85 +85,106 @@ public class StopConsumptionCommandHandler : ICommandHandler<StopConsumptionComm
|
||||
// The validator guarantees that the route identifier and the reported measurements are valid before this point.
|
||||
Guid consumptionRecordId = Guid.Parse(command.ConsumptionRecordId!);
|
||||
|
||||
// The record is loaded scoped to the current user, so a record of another user is indistinguishable from a missing one.
|
||||
Result<MediaConsumptionRecordEntity?> getRecordResult = await _unitOfWork.MediaConsumptionRecordRepository.GetByIdForUserAsync(consumptionRecordId, userId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getRecordResult.IsFailure)
|
||||
return getRecordResult.Errors;
|
||||
if (getRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
Result<MediaConsumptionRecord> toDomainEntityResult = getRecordResult.Value.ToDomainEntity();
|
||||
if (toDomainEntityResult.IsFailure)
|
||||
return toDomainEntityResult.Errors;
|
||||
MediaConsumptionRecord record = toDomainEntityResult.Value;
|
||||
|
||||
// The settings of the library decide whether reaching a threshold turns the stop into a completion. If the library was deleted after
|
||||
// the session started, the defaults of the media kind are used instead, so the reported session can still be closed consistently.
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository
|
||||
.GetByIdAsync(record.LibraryId.Value, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure)
|
||||
return getLibraryResult.Errors;
|
||||
LibraryConsumptionSettings consumptionSettings = getLibraryResult.Value is null
|
||||
? LibraryConsumptionSettings.CreateDefault(record.MediaKind)
|
||||
: getLibraryResult.Value.ToDomainValueObject();
|
||||
|
||||
Optional<MediaPosition> position = ToMediaPosition(command.PositionKind, command.PositionTimeOffsetInSeconds, command.PositionLocation);
|
||||
Optional<int> consecutivePagesRead = command.ConsecutivePagesRead.HasValue
|
||||
? Optional<int>.Some(command.ConsecutivePagesRead.Value)
|
||||
: Optional<int>.None();
|
||||
double progressFraction = command.ProgressFraction!.Value;
|
||||
TimeSpan consumedDuration = TimeSpan.FromSeconds(command.ConsumedDurationInSeconds!.Value);
|
||||
|
||||
// A session stopped at or after the completion threshold of its library is recorded as completed, not stopped, and loses its resume
|
||||
// point. Tracking on without a threshold, and tracking off, never complete a session implicitly. The percentage is divided instead of
|
||||
// multiplying the fraction by one hundred, because the multiplication can round a fraction that is exactly at the threshold below it.
|
||||
bool isAtCompletionThreshold = consumptionSettings.ShouldTrackConsumptionProgress &&
|
||||
consumptionSettings.CompletionThresholdPercentage.HasValue &&
|
||||
progressFraction >= consumptionSettings.CompletionThresholdPercentage.Value / 100.0;
|
||||
|
||||
Result<Updated> terminalResult = isAtCompletionThreshold
|
||||
? record.Complete(position, progressFraction, consumedDuration)
|
||||
: record.Stop(position, progressFraction, consumedDuration);
|
||||
if (terminalResult.IsFailure)
|
||||
return terminalResult.Errors;
|
||||
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(record.MediaKind);
|
||||
if (!ruleResult.HasValue)
|
||||
return DomainErrors.MediaConsumption.UnsupportedMediaKind;
|
||||
|
||||
if (isAtCompletionThreshold)
|
||||
// A terminal report can race with an in-flight progress report on the same record. The record is read, then saved with the
|
||||
// version it was read with, so a progress report that commits in between makes the save fail with a concurrency conflict.
|
||||
// The terminal payload is absolute, so the record is reloaded and the terminal state is applied again to the fresh version.
|
||||
for (int attempt = 1; attempt <= MAXIMUM_TERMINAL_SAVE_ATTEMPTS; attempt++)
|
||||
{
|
||||
// Completion discards the resume point, because the media item no longer has a point to continue from.
|
||||
Result<Deleted> deleteResumePointResult = await _unitOfWork.MediaResumePointRepository.DeleteForUserAndMediaItemAsync(userId, record.MediaKind, record.MediaItemId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteResumePointResult.IsFailure)
|
||||
return deleteResumePointResult.Errors;
|
||||
}
|
||||
else if (consumptionSettings.ShouldTrackConsumptionProgress && ruleResult.Value.CanResume && record.LastPosition.HasValue)
|
||||
{
|
||||
// A stopped session that did not reach the completion threshold keeps a resume point. Without a known position there is nothing to resume from, so no resume point is written.
|
||||
Result<Updated> upsertResumePointResult = await UpsertResumePointAsync(record, record.LastPosition.Value, record.LastProgressFraction, userId, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResumePointResult.IsFailure)
|
||||
return upsertResumePointResult.Errors;
|
||||
// The record is loaded scoped to the current user, so a record of another user is indistinguishable from a missing one.
|
||||
Result<MediaConsumptionRecordEntity?> getRecordResult = await _unitOfWork.MediaConsumptionRecordRepository.GetByIdForUserAsync(consumptionRecordId, userId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getRecordResult.IsFailure)
|
||||
return getRecordResult.Errors;
|
||||
if (getRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
Result<MediaConsumptionRecord> toDomainEntityResult = getRecordResult.Value.ToDomainEntity();
|
||||
if (toDomainEntityResult.IsFailure)
|
||||
return toDomainEntityResult.Errors;
|
||||
MediaConsumptionRecord record = toDomainEntityResult.Value;
|
||||
|
||||
// The settings of the library decide whether reaching a threshold turns the stop into a completion. If the library was deleted after
|
||||
// the session started, the defaults of the media kind are used instead, so the reported session can still be closed consistently.
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository
|
||||
.GetByIdAsync(record.LibraryId.Value, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure)
|
||||
return getLibraryResult.Errors;
|
||||
LibraryConsumptionSettings consumptionSettings = getLibraryResult.Value is null
|
||||
? LibraryConsumptionSettings.CreateDefault(record.MediaKind)
|
||||
: getLibraryResult.Value.ToDomainValueObject();
|
||||
|
||||
// A session stopped at or after the completion threshold of its library is recorded as completed, not stopped, and loses its resume
|
||||
// point. Tracking on without a threshold, and tracking off, never complete a session implicitly. The percentage is divided instead of
|
||||
// multiplying the fraction by one hundred, because the multiplication can round a fraction that is exactly at the threshold below it.
|
||||
bool isAtCompletionThreshold = consumptionSettings.ShouldTrackConsumptionProgress &&
|
||||
consumptionSettings.CompletionThresholdPercentage.HasValue &&
|
||||
progressFraction >= consumptionSettings.CompletionThresholdPercentage.Value / 100.0;
|
||||
|
||||
Result<Updated> terminalResult = isAtCompletionThreshold
|
||||
? record.Complete(position, progressFraction, consumedDuration, consecutivePagesRead)
|
||||
: record.Stop(position, progressFraction, consumedDuration, consecutivePagesRead);
|
||||
if (terminalResult.IsFailure)
|
||||
return terminalResult.Errors;
|
||||
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(record.MediaKind);
|
||||
if (!ruleResult.HasValue)
|
||||
return DomainErrors.MediaConsumption.UnsupportedMediaKind;
|
||||
|
||||
if (isAtCompletionThreshold)
|
||||
{
|
||||
// Completion discards the resume point, because the media item no longer has a point to continue from.
|
||||
Result<Deleted> deleteResumePointResult = await _unitOfWork.MediaResumePointRepository.DeleteForUserAndMediaItemAsync(userId, record.MediaKind, record.MediaItemId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteResumePointResult.IsFailure)
|
||||
return deleteResumePointResult.Errors;
|
||||
}
|
||||
else if (consumptionSettings.ShouldTrackConsumptionProgress && ruleResult.Value.CanResume && record.LastPosition.HasValue)
|
||||
{
|
||||
// A stopped session that did not reach the completion threshold keeps a resume point. Without a known position there is nothing to resume from, so no resume point is written.
|
||||
Result<Updated> upsertResumePointResult = await UpsertResumePointAsync(record, record.LastPosition.Value, record.LastProgressFraction, userId, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResumePointResult.IsFailure)
|
||||
return upsertResumePointResult.Errors;
|
||||
}
|
||||
|
||||
Result<Updated> updateResult = await _unitOfWork.MediaConsumptionRecordRepository.UpdateForUserAsync(record.ToRepositoryEntity(), userId, cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
{
|
||||
// Only a concurrency conflict is worth retrying. The failed attempt is discarded, so the next loop reads the version
|
||||
// the progress report committed and applies the terminal state to it.
|
||||
if (attempt < MAXIMUM_TERMINAL_SAVE_ATTEMPTS && saveChangesResult.Errors.Any(error => error == ApplicationErrors.Persistence.ConcurrencyConflict))
|
||||
{
|
||||
_unitOfWork.ClearTrackedEntities();
|
||||
continue;
|
||||
}
|
||||
return saveChangesResult.Errors;
|
||||
}
|
||||
|
||||
// Queue any domain events, to be published after the request has completed successfully.
|
||||
foreach (IDomainEvent domainEvent in record.GetDomainEvents())
|
||||
_domainEventsQueue.Enqueue(domainEvent);
|
||||
|
||||
// The persisted row is read back, so the response carries the timestamps and version the persistence medium actually stored.
|
||||
Result<MediaConsumptionRecordEntity?> getPersistedRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(record.Id.Value, userId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getPersistedRecordResult.IsFailure)
|
||||
return getPersistedRecordResult.Errors;
|
||||
if (getPersistedRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
return getPersistedRecordResult.Value.ToDto().ToResponse();
|
||||
}
|
||||
|
||||
Result<Updated> updateResult = await _unitOfWork.MediaConsumptionRecordRepository.UpdateForUserAsync(record.ToRepositoryEntity(), userId, cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
// Queue any domain events, to be published after the request has completed successfully.
|
||||
foreach (IDomainEvent domainEvent in record.GetDomainEvents())
|
||||
_domainEventsQueue.Enqueue(domainEvent);
|
||||
|
||||
// The persisted row is read back, so the response carries the timestamps and version the persistence medium actually stored.
|
||||
Result<MediaConsumptionRecordEntity?> getPersistedRecordResult = await _unitOfWork.MediaConsumptionRecordRepository
|
||||
.GetByIdForUserAsync(record.Id.Value, userId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getPersistedRecordResult.IsFailure)
|
||||
return getPersistedRecordResult.Errors;
|
||||
if (getPersistedRecordResult.Value is null)
|
||||
return DomainErrors.MediaConsumption.ConsumptionRecordNotFound;
|
||||
|
||||
return getPersistedRecordResult.Value.ToDto().ToResponse();
|
||||
// Every attempt conflicted with a concurrent write, so the terminal state could not be stored.
|
||||
return ApplicationErrors.Persistence.ConcurrencyConflict;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+4
@@ -62,6 +62,10 @@ public class StopConsumptionCommandValidator : AbstractValidator<StopConsumption
|
||||
.Must(consumedDuration => consumedDuration is null ||
|
||||
consumedDuration.Value <= MAXIMUM_REPORTED_DURATION_IN_SECONDS)
|
||||
.WithError(Errors.MediaConsumption.ConsumedDurationIsOutOfRange);
|
||||
|
||||
RuleFor(command => command.ConsecutivePagesRead)
|
||||
.Must(consecutivePagesRead => consecutivePagesRead is null || consecutivePagesRead.Value >= 0)
|
||||
.WithError(Errors.MediaConsumption.ConsecutivePagesReadMustBeNonNegative);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user