diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IConsumptionStatisticsRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IConsumptionStatisticsRepository.cs
new file mode 100644
index 00000000..5f84730b
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IConsumptionStatisticsRepository.cs
@@ -0,0 +1,42 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
+using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
+using Lumina.Domain.Common.Primitives;
+using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
+
+///
+/// Interface for the repository for the consumption records used to build consumption statistics.
+///
+public interface IConsumptionStatisticsRepository : IRepository
+{
+ ///
+ /// Gets the consumption records of the user identified by , optionally limited to a media library, a media kind, and a date
+ /// range.
+ ///
+ /// The Id of the user whose consumption records are retrieved.
+ /// The Id of the media library to restrict the records to, or for every library.
+ /// The kind of media to restrict the records to, or for every kind.
+ /// The start of the date range, or when the range has no start.
+ /// The end of the date range, or when the range has no end.
+ /// Cancellation token that can be used to stop the execution.
+ /// An containing either the consumption records of the user, or an error.
+ Task>> GetForUserAsync(Guid userId, Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken);
+
+ ///
+ /// Gets the consumption records of every user, optionally limited to a media library, a media kind, and a date range.
+ ///
+ /// The Id of the media library to restrict the records to, or for every library.
+ /// The kind of media to restrict the records to, or for every kind.
+ /// The start of the date range, or when the range has no start.
+ /// The end of the date range, or when the range has no end.
+ /// Cancellation token that can be used to stop the execution.
+ /// An containing either the consumption records of every user, or an error.
+ Task>> GetForAllUsersAsync(Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken);
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaConsumptionRecordRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaConsumptionRecordRepository.cs
new file mode 100644
index 00000000..4ac364a3
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaConsumptionRecordRepository.cs
@@ -0,0 +1,59 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
+using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
+using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
+using Lumina.Domain.Common.Primitives;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
+
+///
+/// Interface for the repository for media consumption records.
+///
+public interface IMediaConsumptionRecordRepository : IRepository,
+ IInsertRepositoryAction
+{
+ ///
+ /// Gets the consumption record identified by when it belongs to the user identified by , if it exists.
+ ///
+ /// The Id of the consumption record to get.
+ /// The Id of the user the consumption record must belong to.
+ /// Whether the retrieved record should be tracked by the persistence medium, so that changes to it can be saved.
+ /// Cancellation token that can be used to stop the execution.
+ ///
+ /// An containing either the consumption record identified by and owned by
+ /// , when no such record exists, or an error.
+ ///
+ Task> GetByIdForUserAsync(Guid id, Guid userId, bool shouldTrackEntities = true, CancellationToken cancellationToken = default);
+
+ ///
+ /// Updates the consumption record of the user identified by , when the record belongs to that user and still holds the
+ /// version it was read with.
+ ///
+ /// The consumption record whose editable values are applied to the stored record. Its owner must match .
+ /// The Id of the user the consumption record must belong to. It is authoritative for locating the stored record; the owner carried by is not used to select it.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful update, or an error.
+ Task> UpdateForUserAsync(MediaConsumptionRecordEntity data, Guid userId, CancellationToken cancellationToken);
+
+ ///
+ /// Deletes the consumption record identified by when it belongs to the user identified by . A record
+ /// that does not exist is not an error.
+ ///
+ /// The Id of the consumption record to delete.
+ /// The Id of the user the consumption record must belong to.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful deletion, or an error.
+ Task> DeleteForUserAsync(Guid id, Guid userId, CancellationToken cancellationToken);
+
+ ///
+ /// Deletes every consumption record of the user identified by .
+ ///
+ /// The Id of the user whose consumption records are deleted.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful deletion, or an error.
+ Task> DeleteAllForUserAsync(Guid userId, CancellationToken cancellationToken);
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaResumePointRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaResumePointRepository.cs
new file mode 100644
index 00000000..2c25faf4
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Repositories/MediaConsumption/IMediaResumePointRepository.cs
@@ -0,0 +1,70 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
+using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
+using Lumina.Domain.Common.Primitives;
+using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
+
+///
+/// Interface for the repository for media resume points.
+///
+public interface IMediaResumePointRepository : IRepository
+{
+ ///
+ /// Gets the resume point of the user identified by for the media item identified by and
+ /// , if it exists.
+ ///
+ /// The Id of the user the resume point must belong to.
+ /// The kind of the media item to resume.
+ /// The Id of the media item to resume.
+ /// Whether the retrieved resume point should be tracked by the persistence medium, so that changes to it can be saved.
+ /// Cancellation token that can be used to stop the execution.
+ ///
+ /// An containing either the resume point of the user for the media item, when no such resume
+ /// point exists, or an error.
+ ///
+ Task> GetForUserAndMediaItemAsync(Guid userId, MediaKind mediaKind, Guid mediaItemId, bool shouldTrackEntities = true, CancellationToken cancellationToken = default);
+
+ ///
+ /// Saves the resume point of the user identified by , inserting it when it does not exist and updating it otherwise. The
+ /// business key of the resume point, made of the user, the media kind, and the media item, is preserved.
+ ///
+ /// The resume point whose values are saved. Its owner must match .
+ /// The Id of the user the resume point must belong to. It is authoritative for locating the stored resume point and for the owner of a new one; the owner carried by is not used to select it.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful save, or an error.
+ Task> UpsertForUserAsync(MediaResumePointEntity data, Guid userId, CancellationToken cancellationToken);
+
+ ///
+ /// Deletes the resume point of the user identified by for the media item identified by and
+ /// . A resume point that does not exist is not an error.
+ ///
+ /// The Id of the user the resume point must belong to.
+ /// The kind of the media item to resume.
+ /// The Id of the media item to resume.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful deletion, or an error.
+ Task> DeleteForUserAndMediaItemAsync(Guid userId, MediaKind mediaKind, Guid mediaItemId, CancellationToken cancellationToken);
+
+ ///
+ /// Deletes every resume point of the user identified by .
+ ///
+ /// The Id of the user whose resume points are deleted.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful deletion, or an error.
+ Task> DeleteAllForUserAsync(Guid userId, CancellationToken cancellationToken);
+
+ ///
+ /// Deletes every resume point of the user identified by in the media library identified by .
+ ///
+ /// The Id of the user whose resume points are deleted.
+ /// The Id of the media library whose resume points are deleted.
+ /// Cancellation token that can be used to stop the execution.
+ /// An representing either a successful deletion, or an error.
+ Task> DeleteAllForUserInLibraryAsync(Guid userId, Guid libraryId, CancellationToken cancellationToken);
+}
diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicyContext.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicyContext.cs
new file mode 100644
index 00000000..a5d740e6
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicyContext.cs
@@ -0,0 +1,14 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
+
+///
+/// Represents the context required to evaluate the consumption activity authorization policy.
+///
+/// The Id of the user whose consumption activity is being accessed.
+[DebuggerDisplay("TargetUserId: {TargetUserId}")]
+public sealed record ConsumptionActivityAccessPolicyContext(Guid TargetUserId) : PolicyContext;
diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/IConsumptionActivityAccessPolicy.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/IConsumptionActivityAccessPolicy.cs
new file mode 100644
index 00000000..aadaf37c
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/ConsumptionActivity/IConsumptionActivityAccessPolicy.cs
@@ -0,0 +1,12 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
+
+///
+/// Interface for the consumption activity authorization policy.
+///
+public interface IConsumptionActivityAccessPolicy : IAuthorizationPolicy
+{
+}
diff --git a/src/Lumina.Infrastructure/Common/DependencyInjection/InfrastructureLayerServices.cs b/src/Lumina.Infrastructure/Common/DependencyInjection/InfrastructureLayerServices.cs
index 06a35358..b44909f7 100644
--- a/src/Lumina.Infrastructure/Common/DependencyInjection/InfrastructureLayerServices.cs
+++ b/src/Lumina.Infrastructure/Common/DependencyInjection/InfrastructureLayerServices.cs
@@ -2,6 +2,7 @@
using Lumina.Application.Common.DomainEvents;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Over18;
using Lumina.Application.Common.Infrastructure.Models.DTO.Configuration;
@@ -30,6 +31,7 @@ using Lumina.Infrastructure.Common.Networking;
using Lumina.Infrastructure.Core.Authentication;
using Lumina.Infrastructure.Core.Authorization;
using Lumina.Infrastructure.Core.Authorization.Policies.Common.Factory;
+using Lumina.Infrastructure.Core.Authorization.Policies.ConsumptionActivity;
using Lumina.Infrastructure.Core.Authorization.Policies.LibraryOwnership;
using Lumina.Infrastructure.Core.Authorization.Policies.Over18;
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Cancellation;
@@ -103,6 +105,7 @@ public static class InfrastructureLayerServices
// Authorization.
services.AddScoped();
services.AddScoped();
+ services.AddScoped();
services.AddScoped();
services.AddScoped();
diff --git a/src/Lumina.Infrastructure/Core/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicy.cs b/src/Lumina.Infrastructure/Core/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicy.cs
new file mode 100644
index 00000000..7ee4977d
--- /dev/null
+++ b/src/Lumina.Infrastructure/Core/Authorization/Policies/ConsumptionActivity/ConsumptionActivityAccessPolicy.cs
@@ -0,0 +1,51 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Authorization;
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Infrastructure.Core.Authorization.Policies.ConsumptionActivity;
+
+///
+/// Authorization policy that checks whether a user can access the consumption activity of a user: admins can access any user's consumption activity,
+/// while other users can only access their own.
+///
+public class ConsumptionActivityAccessPolicy : IConsumptionActivityAccessPolicy
+{
+ private readonly IAuthorizationService _authorizationService;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Injected service for authorization related functionality.
+ public ConsumptionActivityAccessPolicy(IAuthorizationService authorizationService)
+ {
+ _authorizationService = authorizationService;
+ }
+
+ ///
+ /// Evaluates the policy for the user identified by against the consumption activity of the user identified in
+ /// .
+ ///
+ /// The unique identifier of the user for which to evaluate the policy.
+ /// The context carrying the Id of the user whose consumption activity is being accessed.
+ /// Cancellation token that can be used to stop the execution.
+ /// if the user is an admin or accesses their own consumption activity, otherwise.
+ public async Task EvaluateAsync(Guid userId, PolicyContext? context, CancellationToken cancellationToken)
+ {
+ // An empty user id is a defaulted value, not a real user, so it must never match another empty id.
+ if (userId == Guid.Empty)
+ return false;
+ if (context is not ConsumptionActivityAccessPolicyContext consumptionActivityContext)
+ return false;
+ if (consumptionActivityContext.TargetUserId == Guid.Empty)
+ return false;
+ if (consumptionActivityContext.TargetUserId == userId) // A user can access their own consumption activity.
+ return true;
+ // Admins can access consumption activities all the time.
+ return await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false);
+ }
+}