mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Adds music player consumption reporting and its Web proxy endpoints
This commit is contained in:
1 parent
00d7eff777
commit
bfa7b190b2
20 files changed
+1599
-21
No files matched your search
@@ -0,0 +1,38 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Data transfer object for a media consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="Id">The Id of the consumption record.</param>
|
||||
/// <param name="LibraryId">The Id of the media library that contains the consumed media item.</param>
|
||||
/// <param name="MediaItemId">The Id of the consumed media item.</param>
|
||||
/// <param name="MediaKind">The kind of media that was consumed.</param>
|
||||
/// <param name="Status">The state of the consumption session.</param>
|
||||
/// <param name="StartedOnUtc">The date and time when the session started.</param>
|
||||
/// <param name="LastActivityOnUtc">The date and time when the session last received activity.</param>
|
||||
/// <param name="EndedOnUtc">The date and time when the session completed or stopped, if it has ended.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds.</param>
|
||||
/// <param name="LastPosition">The last known position for the media item, if any.</param>
|
||||
/// <param name="LastProgressFraction">The latest reported progress fraction of the media item.</param>
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the session.</param>
|
||||
[DebuggerDisplay("Id: {Id}; MediaKind: {MediaKind}; Status: {Status}")]
|
||||
public sealed record ConsumptionRecordDto(
|
||||
Guid Id,
|
||||
Guid LibraryId,
|
||||
Guid MediaItemId,
|
||||
MediaKind MediaKind,
|
||||
ConsumptionStatus Status,
|
||||
DateTime StartedOnUtc,
|
||||
DateTime LastActivityOnUtc,
|
||||
DateTime? EndedOnUtc,
|
||||
double ConsumedDurationInSeconds,
|
||||
MediaPositionDto? LastPosition,
|
||||
double LastProgressFraction,
|
||||
int ConsecutivePagesRead
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Data transfer object for a position reached in a media item during a consumption session.
|
||||
/// </summary>
|
||||
/// <param name="Kind">The kind of representation used by the position.</param>
|
||||
/// <param name="TimeOffsetSeconds">The position within the media item, in seconds, when the position is a time offset.</param>
|
||||
/// <param name="Location">The opaque locator within the media item, when the position is not a time offset.</param>
|
||||
[DebuggerDisplay("Kind: {Kind}; TimeOffsetSeconds: {TimeOffsetSeconds}")]
|
||||
public sealed record MediaPositionDto(
|
||||
MediaPositionKind Kind,
|
||||
double? TimeOffsetSeconds,
|
||||
string? Location
|
||||
);
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration for the states of a media consumption session.
|
||||
/// </summary>
|
||||
public enum ConsumptionStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// The consumption session started and has not ended yet.
|
||||
/// </summary>
|
||||
InProgress,
|
||||
|
||||
/// <summary>
|
||||
/// The consumption session reached its explicit completion or the completion threshold of its library.
|
||||
/// </summary>
|
||||
Completed,
|
||||
|
||||
/// <summary>
|
||||
/// The consumption session ended before it completed.
|
||||
/// </summary>
|
||||
Stopped
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration for the kinds of position that a media consumption session can report.
|
||||
/// </summary>
|
||||
public enum MediaPositionKind
|
||||
{
|
||||
/// <summary>
|
||||
/// The position is a time offset within the media item.
|
||||
/// </summary>
|
||||
TimeOffset,
|
||||
|
||||
/// <summary>
|
||||
/// The position is an opaque locator within the media item, such as a reading location of a book.
|
||||
/// </summary>
|
||||
Location
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to report that a media consumption session completed.
|
||||
/// </summary>
|
||||
/// <param name="Position">The final position reported for the media item, if the client knows it. Optional.</param>
|
||||
/// <param name="ProgressFraction">The fraction of the media item that the client reached, between 0 and 1. Required.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds. Required.</param>
|
||||
[DebuggerDisplay("ProgressFraction: {ProgressFraction}; ConsumedDurationInSeconds: {ConsumedDurationInSeconds}")]
|
||||
public sealed record CompleteConsumptionRequest(
|
||||
MediaPositionRequest? Position,
|
||||
double? ProgressFraction,
|
||||
double? ConsumedDurationInSeconds
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to report a position in a media consumption session.
|
||||
/// </summary>
|
||||
/// <param name="Kind">The kind of the position. Required.</param>
|
||||
/// <param name="TimeOffsetSeconds">The position within the media item, in seconds, when the position is a time offset. Optional.</param>
|
||||
/// <param name="Location">The opaque locator within the media item, when the position is not a time offset. Optional.</param>
|
||||
[DebuggerDisplay("Kind: {Kind}; TimeOffsetSeconds: {TimeOffsetSeconds}")]
|
||||
public sealed record MediaPositionRequest(
|
||||
string? Kind,
|
||||
double? TimeOffsetSeconds,
|
||||
string? Location
|
||||
);
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to report the progress of a media consumption session.
|
||||
/// </summary>
|
||||
/// <param name="Position">The last position reported for the media item, if the client knows it. Optional.</param>
|
||||
/// <param name="ProgressFraction">The fraction of the media item that the client reached, between 0 and 1. Required.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds. Required.</param>
|
||||
/// <param name="ConsecutivePagesRead">The number of consecutive pages read in the current session, used by page based media such as books and ignored for other kinds. Defaults to 0. Optional.</param>
|
||||
[DebuggerDisplay("ProgressFraction: {ProgressFraction}; ConsumedDurationInSeconds: {ConsumedDurationInSeconds}; ConsecutivePagesRead: {ConsecutivePagesRead}")]
|
||||
public sealed record ReportConsumptionProgressRequest(
|
||||
MediaPositionRequest? Position,
|
||||
double? ProgressFraction,
|
||||
double? ConsumedDurationInSeconds,
|
||||
int ConsecutivePagesRead = 0
|
||||
);
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to report that a media consumption session stopped.
|
||||
/// </summary>
|
||||
/// <param name="Position">The final position reported for the media item, if the client knows it. Optional.</param>
|
||||
/// <param name="ProgressFraction">The fraction of the media item that the client reached, between 0 and 1. Required.</param>
|
||||
/// <param name="ConsumedDurationInSeconds">The active duration consumed by the session, in seconds. Required.</param>
|
||||
[DebuggerDisplay("ProgressFraction: {ProgressFraction}; ConsumedDurationInSeconds: {ConsumedDurationInSeconds}")]
|
||||
public sealed record StopConsumptionRequest(
|
||||
MediaPositionRequest? Position,
|
||||
double? ProgressFraction,
|
||||
double? ConsumedDurationInSeconds
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Lumina.Presentation.Web.Common.Routes;
|
||||
|
||||
/// <summary>
|
||||
/// Class for the collection of remote API routes called by this Web application.
|
||||
/// </summary>
|
||||
public static partial class ApiRoutes
|
||||
{
|
||||
/// <summary>
|
||||
/// Routes for the media consumption lifecycle endpoints of the remote API.
|
||||
/// </summary>
|
||||
public static class MediaConsumption
|
||||
{
|
||||
public const string START_CONSUMPTION = "libraries/{libraryId}/media-items/{mediaKind}/{mediaItemId}/consumption-records";
|
||||
public const string REPORT_CONSUMPTION_PROGRESS = "users/me/consumption-records/{recordId}/progress";
|
||||
public const string COMPLETE_CONSUMPTION = "users/me/consumption-records/{recordId}/complete";
|
||||
public const string STOP_CONSUMPTION = "users/me/consumption-records/{recordId}/stop";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Lumina.Presentation.Web.Common.Routes;
|
||||
|
||||
/// <summary>
|
||||
/// Class for the collection of routes defined in the Web application.
|
||||
/// </summary>
|
||||
public static partial class WebRoutes
|
||||
{
|
||||
/// <summary>
|
||||
/// Routes for the media consumption lifecycle actions the browser clients report to, proxied to the remote API.
|
||||
/// </summary>
|
||||
public static class MediaConsumption
|
||||
{
|
||||
public const string START_CONSUMPTION = "{culture}/media-consumption/api-start-consumption/{libraryId}/{mediaKind}/{mediaItemId}";
|
||||
public const string REPORT_CONSUMPTION_PROGRESS = "{culture}/media-consumption/api-report-consumption-progress/{recordId}";
|
||||
public const string COMPLETE_CONSUMPTION = "{culture}/media-consumption/api-complete-consumption/{recordId}";
|
||||
public const string STOP_CONSUMPTION = "{culture}/media-consumption/api-stop-consumption/{recordId}";
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/{culture}/media-consumption/api-complete-consumption/{recordId}</c> route.
|
||||
/// </summary>
|
||||
public class CompleteConsumptionEndpoint : BaseEndpoint<CompleteConsumptionRequest, IResult>
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompleteConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
public CompleteConsumptionEndpoint(IApiHttpClient apiHttpClient)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
base.Configure();
|
||||
Verbs(Http.POST);
|
||||
Routes(WebRoutes.MediaConsumption.COMPLETE_CONSUMPTION);
|
||||
DontAutoTag();
|
||||
Options(options => options.WithTags("MediaConsumption"));
|
||||
EnableAntiforgery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes a consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the final position, fraction, and consumed duration of the session.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(CompleteConsumptionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the current user, so no intermediary may store the response, and it varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The record is identified by the route value, not by any id posted in the body.
|
||||
string routeRecordId = HttpContext.Request.RouteValues["recordId"]?.ToString() ?? string.Empty;
|
||||
string recordId = Guid.TryParse(routeRecordId, out Guid parsedRecordId) ? parsedRecordId.ToString() : Guid.Empty.ToString();
|
||||
ConsumptionRecordDto record = await _apiHttpClient
|
||||
.PostAsync<ConsumptionRecordDto, CompleteConsumptionRequest>(
|
||||
ApiRoutes.MediaConsumption.COMPLETE_CONSUMPTION.Replace("{recordId}", recordId, StringComparison.Ordinal),
|
||||
request,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
return JsonSuccess(record);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.DTO.Common;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="CompleteConsumptionEndpoint"/> endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CompleteConsumptionEndpointSummary : Summary<CompleteConsumptionEndpoint, CompleteConsumptionRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompleteConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public CompleteConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Completes a consumption session of the current user.";
|
||||
Description = "Proxies the completion of an in-progress consumption session to the API server. The final position, progress fraction, and active consumed duration are stored, and the resume point of the media item is deleted. A record of another user is reported with the same not found result as a missing record.";
|
||||
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
ExampleRequest = new CompleteConsumptionRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 100, null),
|
||||
ProgressFraction: 1,
|
||||
ConsumedDurationInSeconds: 100
|
||||
);
|
||||
RequestParam(request => request.Position, "The final position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
|
||||
Response(200, "The consumption session is completed and the updated record is returned.",
|
||||
example: new SuccessResponse<ConsumptionRecordDto>(true, new ConsumptionRecordDto(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.Completed,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 1, 40, DateTimeKind.Utc),
|
||||
EndedOnUtc: new DateTime(2026, 10, 24, 12, 1, 40, DateTimeKind.Utc),
|
||||
ConsumedDurationInSeconds: 100,
|
||||
LastPosition: new MediaPositionDto(MediaPositionKind.TimeOffset, 100, null),
|
||||
LastProgressFraction: 1,
|
||||
ConsecutivePagesRead: 0))
|
||||
);
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/{culture}/media-consumption/api-report-consumption-progress/{recordId}</c> route.
|
||||
/// </summary>
|
||||
public class ReportConsumptionProgressEndpoint : BaseEndpoint<ReportConsumptionProgressRequest, IResult>
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReportConsumptionProgressEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
public ReportConsumptionProgressEndpoint(IApiHttpClient apiHttpClient)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
base.Configure();
|
||||
Verbs(Http.POST);
|
||||
Routes(WebRoutes.MediaConsumption.REPORT_CONSUMPTION_PROGRESS);
|
||||
DontAutoTag();
|
||||
Options(options => options.WithTags("MediaConsumption"));
|
||||
EnableAntiforgery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports the progress of a consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the latest position, fraction, and consumed duration of the session.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(ReportConsumptionProgressRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the current user, so no intermediary may store the response, and it varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The record is identified by the route value, not by any id posted in the body.
|
||||
string routeRecordId = HttpContext.Request.RouteValues["recordId"]?.ToString() ?? string.Empty;
|
||||
string recordId = Guid.TryParse(routeRecordId, out Guid parsedRecordId) ? parsedRecordId.ToString() : Guid.Empty.ToString();
|
||||
ConsumptionRecordDto record = await _apiHttpClient
|
||||
.PutAsync<ConsumptionRecordDto, ReportConsumptionProgressRequest>(
|
||||
ApiRoutes.MediaConsumption.REPORT_CONSUMPTION_PROGRESS.Replace("{recordId}", recordId, StringComparison.Ordinal),
|
||||
request,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
return JsonSuccess(record);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.DTO.Common;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="ReportConsumptionProgressEndpoint"/> endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ReportConsumptionProgressEndpointSummary : Summary<ReportConsumptionProgressEndpoint, ReportConsumptionProgressRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReportConsumptionProgressEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public ReportConsumptionProgressEndpointSummary()
|
||||
{
|
||||
Summary = "Reports the progress of a consumption session of the current user.";
|
||||
Description = "Proxies the latest position, progress fraction, and active consumed duration of an in-progress consumption session to the API server. The position and the progress fraction are the latest reported values and may move backwards. The consumed duration is the only monotonic measurement. A record of another user is reported with the same not found result as a missing record.";
|
||||
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
ExampleRequest = new ReportConsumptionProgressRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 92.5, null),
|
||||
ProgressFraction: 0.31,
|
||||
ConsumedDurationInSeconds: 92.5,
|
||||
ConsecutivePagesRead: 0
|
||||
);
|
||||
RequestExamples.Add(new RequestExample(new ReportConsumptionProgressRequest(
|
||||
Position: new MediaPositionRequest("Location", null, "epubcfi(/6/4[chapter-3]!/4/2/2)"),
|
||||
ProgressFraction: 0.42,
|
||||
ConsumedDurationInSeconds: 240,
|
||||
ConsecutivePagesRead: 2
|
||||
)));
|
||||
RequestParam(request => request.Position, "The last position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
RequestParam(request => request.ConsecutivePagesRead, "The number of consecutive pages read in the current session, used by page based media such as books. Optional, defaults to 0.");
|
||||
|
||||
Response(200, "The progress of the consumption session is reported and the updated record is returned.",
|
||||
example: new SuccessResponse<ConsumptionRecordDto>(true, new ConsumptionRecordDto(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.InProgress,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 1, 32, 500, DateTimeKind.Utc),
|
||||
EndedOnUtc: null,
|
||||
ConsumedDurationInSeconds: 92.5,
|
||||
LastPosition: new MediaPositionDto(MediaPositionKind.TimeOffset, 92.5, null),
|
||||
LastProgressFraction: 0.31,
|
||||
ConsecutivePagesRead: 0))
|
||||
);
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/{culture}/media-consumption/api-start-consumption/{libraryId}/{mediaKind}/{mediaItemId}</c> route.
|
||||
/// </summary>
|
||||
public class StartConsumptionEndpoint : BaseEndpoint<EmptyRequest, IResult>
|
||||
{
|
||||
// A value that is not a name of MediaKind, so the API rejects a crafted route value with its own validation error.
|
||||
private const string INVALID_MEDIA_KIND_PLACEHOLDER = "Invalid";
|
||||
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
public StartConsumptionEndpoint(IApiHttpClient apiHttpClient)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
base.Configure();
|
||||
Verbs(Http.POST);
|
||||
Routes(WebRoutes.MediaConsumption.START_CONSUMPTION);
|
||||
DontAutoTag();
|
||||
Options(options => options.WithTags("MediaConsumption"));
|
||||
EnableAntiforgery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a consumption session of a media item for the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request object.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the current user, so no intermediary may store the response, and it varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The identifiers are taken from the route, so they cannot be replaced by values posted in the body.
|
||||
// The media kind must be the name of a MediaKind member, which also rejects numeric strings, comma separated lists, and
|
||||
// dot segments that escaping alone would leave in the relative path of the upstream request.
|
||||
string routeLibraryId = HttpContext.Request.RouteValues["libraryId"]?.ToString() ?? string.Empty;
|
||||
string libraryId = Guid.TryParse(routeLibraryId, out Guid parsedLibraryId) ? parsedLibraryId.ToString() : Guid.Empty.ToString();
|
||||
string routeMediaKind = HttpContext.Request.RouteValues["mediaKind"]?.ToString() ?? string.Empty;
|
||||
string mediaKind = INVALID_MEDIA_KIND_PLACEHOLDER;
|
||||
foreach (string mediaKindName in Enum.GetNames<MediaKind>())
|
||||
if (string.Equals(mediaKindName, routeMediaKind, StringComparison.OrdinalIgnoreCase))
|
||||
mediaKind = mediaKindName;
|
||||
string routeMediaItemId = HttpContext.Request.RouteValues["mediaItemId"]?.ToString() ?? string.Empty;
|
||||
string mediaItemId = Guid.TryParse(routeMediaItemId, out Guid parsedMediaItemId) ? parsedMediaItemId.ToString() : Guid.Empty.ToString();
|
||||
string endpoint = ApiRoutes.MediaConsumption.START_CONSUMPTION
|
||||
.Replace("{libraryId}", libraryId, StringComparison.Ordinal)
|
||||
.Replace("{mediaKind}", mediaKind, StringComparison.Ordinal)
|
||||
.Replace("{mediaItemId}", mediaItemId, StringComparison.Ordinal);
|
||||
ConsumptionRecordDto record = await _apiHttpClient.PostAsync<ConsumptionRecordDto, EmptyRequest>(endpoint, request, cancellationToken).ConfigureAwait(false);
|
||||
return JsonSuccess(record);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.DTO.Common;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StartConsumptionEndpoint"/> endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StartConsumptionEndpointSummary : Summary<StartConsumptionEndpoint>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StartConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Starts a media consumption session of the current user.";
|
||||
Description = "Proxies the start of a consumption record to the API server. The audio player calls it when a track actually begins playback. When the library tracks progress, the returned record carries the stored resume position. A missing media item and a media item that belongs to another library are reported with the same not found result.";
|
||||
|
||||
Params["libraryId"] = "The Id of the media library that contains the media item.";
|
||||
Params["mediaKind"] = "The kind of media that is consumed. Only the member names of MediaKind are accepted, such as MusicTrack or Book. Numeric values are rejected.";
|
||||
Params["mediaItemId"] = "The Id of the consumed media item.";
|
||||
|
||||
Response(200, "The consumption record is started and returned.",
|
||||
example: new SuccessResponse<ConsumptionRecordDto>(true, new ConsumptionRecordDto(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.InProgress,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
EndedOnUtc: null,
|
||||
ConsumedDurationInSeconds: 0,
|
||||
LastPosition: null,
|
||||
LastProgressFraction: 0,
|
||||
ConsecutivePagesRead: 0))
|
||||
);
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/{culture}/media-consumption/api-stop-consumption/{recordId}</c> route.
|
||||
/// </summary>
|
||||
public class StopConsumptionEndpoint : BaseEndpoint<StopConsumptionRequest, IResult>
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
public StopConsumptionEndpoint(IApiHttpClient apiHttpClient)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
base.Configure();
|
||||
Verbs(Http.POST);
|
||||
Routes(WebRoutes.MediaConsumption.STOP_CONSUMPTION);
|
||||
DontAutoTag();
|
||||
Options(options => options.WithTags("MediaConsumption"));
|
||||
EnableAntiforgery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops a consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the final position, fraction, and consumed duration of the session.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(StopConsumptionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the current user, so no intermediary may store the response, and it varies by the sign in cookie.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Cookie";
|
||||
// The record is identified by the route value, not by any id posted in the body.
|
||||
string routeRecordId = HttpContext.Request.RouteValues["recordId"]?.ToString() ?? string.Empty;
|
||||
string recordId = Guid.TryParse(routeRecordId, out Guid parsedRecordId) ? parsedRecordId.ToString() : Guid.Empty.ToString();
|
||||
ConsumptionRecordDto record = await _apiHttpClient
|
||||
.PostAsync<ConsumptionRecordDto, StopConsumptionRequest>(
|
||||
ApiRoutes.MediaConsumption.STOP_CONSUMPTION.Replace("{recordId}", recordId, StringComparison.Ordinal),
|
||||
request,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
return JsonSuccess(record);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.DTO.Common;
|
||||
using Lumina.Presentation.Web.Common.DTO.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Enums.MediaConsumption;
|
||||
using Lumina.Presentation.Web.Common.Requests.MediaConsumption;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StopConsumptionEndpoint"/> endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StopConsumptionEndpointSummary : Summary<StopConsumptionEndpoint, StopConsumptionRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StopConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Stops a consumption session of the current user.";
|
||||
Description = "Proxies the stop of an in-progress consumption session to the API server. The final position, progress fraction, and active consumed duration are stored. The API decides whether the session stopped below the completion threshold of its library, or completed at or above it. A record of another user is reported with the same not found result as a missing record.";
|
||||
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
ExampleRequest = new StopConsumptionRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 92.5, null),
|
||||
ProgressFraction: 0.31,
|
||||
ConsumedDurationInSeconds: 92.5
|
||||
);
|
||||
RequestParam(request => request.Position, "The final position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
|
||||
Response(200, "The consumption session is stopped and the updated record is returned.",
|
||||
example: new SuccessResponse<ConsumptionRecordDto>(true, new ConsumptionRecordDto(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.Stopped,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 1, 32, 500, DateTimeKind.Utc),
|
||||
EndedOnUtc: new DateTime(2026, 10, 24, 12, 1, 32, 500, DateTimeKind.Utc),
|
||||
ConsumedDurationInSeconds: 92.5,
|
||||
LastPosition: new MediaPositionDto(MediaPositionKind.TimeOffset, 92.5, null),
|
||||
LastProgressFraction: 0.31,
|
||||
ConsecutivePagesRead: 0))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,8 @@
|
||||
// The endpoints the audio player reads the stored playback preferences of the user from, writes the volume and mute
|
||||
// state it changes back to, and fetches the playback manifest of a track from, together with the stream route
|
||||
// template the manifest path must match. The routes are the culture aware web routes, so the player never hardcodes
|
||||
// a path, and it never reaches the API server directly.
|
||||
// a path, and it never reaches the API server directly. The consumption route templates carry their named identifiers
|
||||
// unreplaced, so the player fills in the library, the kind, the item, or the record of the session it reports.
|
||||
var audioPlayerConfiguration = {
|
||||
getUserSettingsUrl: '@_urlService.GetRelativeUrl(WebRoutes.Settings.GET_USER_SETTINGS)',
|
||||
getPlaybackStateUrl: '@_urlService.GetRelativeUrl(WebRoutes.Settings.GET_PLAYBACK_STATE)',
|
||||
@@ -44,6 +45,10 @@
|
||||
getPlaybackManifestUrl: '@_urlService.GetRelativeUrl(WebRoutes.Music.GET_PLAYBACK_MANIFEST)',
|
||||
streamTrackRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.Music.STREAM_TRACK)',
|
||||
streamTrackSegmentRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.Music.STREAM_TRACK_SEGMENT)',
|
||||
startConsumptionRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.MediaConsumption.START_CONSUMPTION)',
|
||||
reportConsumptionProgressRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.MediaConsumption.REPORT_CONSUMPTION_PROGRESS)',
|
||||
completeConsumptionRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.MediaConsumption.COMPLETE_CONSUMPTION)',
|
||||
stopConsumptionRouteTemplate: '@_urlService.GetRelativeUrl(WebRoutes.MediaConsumption.STOP_CONSUMPTION)',
|
||||
// The endpoints the queue of the player is built from: the releases of an artist, the tracks of a release, and the
|
||||
// artists of a library, which are also the endpoints the autoplay continuation and the library shuffle use. The
|
||||
// queue script documents which context uses which endpoint, and appends the library and pagination query parameters.
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user