Adds playback preferences and auto-persisted playback state to user settings

This commit is contained in:
BinaryAlley committed 2026-10-15 21:09:38 +03:00
1 parent aab36dcd29
commit dd76309ded
81 files changed
+6102 -173

No files matched your search

@@ -13,5 +13,7 @@ public static partial class ApiRoutes
public const string GET_USER_BY_ID = "/users/{id}";
public const string GET_USER_SETTINGS = "/users/me/settings"; // uses "me" because it is always for the currently authenticated user
public const string UPDATE_USER_SETTINGS = "/users/me/settings";
public const string GET_PLAYBACK_STATE = "/users/me/settings/playback-state";
public const string UPDATE_PLAYBACK_STATE = "/users/me/settings/playback-state";
}
}
@@ -0,0 +1,55 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.UsersManagement.Settings.Queries.GetPlaybackState;
using Lumina.Contracts.Responses.UsersManagement.Settings;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Settings.GetPlaybackState;
/// <summary>
/// API endpoint for the <c>/users/me/settings/playback-state</c> route.
/// </summary>
/// <remarks>
/// Uses "me" because it is always for the currently authenticated user.
/// </remarks>
public class GetPlaybackStateEndpoint : BaseEndpoint<EmptyRequest, IResult>
{
private readonly IQueryHandler<GetPlaybackStateQuery, Result<PlaybackStateResponse>> _getPlaybackStateQueryHandler;
/// <summary>
/// Initializes a new instance of the <see cref="GetPlaybackStateEndpoint"/> class.
/// </summary>
/// <param name="getPlaybackStateQueryHandler">Injected service for handling get playback state queries.</param>
public GetPlaybackStateEndpoint(IQueryHandler<GetPlaybackStateQuery, Result<PlaybackStateResponse>> getPlaybackStateQueryHandler)
{
_getPlaybackStateQueryHandler = getPlaybackStateQueryHandler;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(Http.GET);
Routes(ApiRoutes.Users.GET_PLAYBACK_STATE);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Gets the playback state the player reported for the current user automatically.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
{
Result<PlaybackStateResponse> result = await _getPlaybackStateQueryHandler.HandleAsync(new GetPlaybackStateQuery(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Responses.UsersManagement.Settings;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Settings.GetPlaybackState;
/// <summary>
/// Class used for providing a textual description for the <see cref="GetPlaybackStateEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPlaybackStateEndpointSummary : Summary<GetPlaybackStateEndpoint, EmptyRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetPlaybackStateEndpointSummary"/> class.
/// </summary>
public GetPlaybackStateEndpointSummary()
{
Summary = "Retrieves the playback state the player reported for the current user.";
Description = "Retrieves the last volume and mute state the player reported for the current user. When no settings are stored for the user yet, the default values are returned.";
Response(200, "The playback state of the current user is returned.",
example: new PlaybackStateResponse(
Volume: 1.0,
IsMuted: false
));
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Authentication failed",
instance = "/api/v1/users/me/settings/playback-state"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token has expired",
instance = "/api/v1/users/me/settings/playback-state"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/api/v1/users/me/settings/playback-state"
}
}
);
}
}
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Responses.UsersManagement.Settings;
using Lumina.Domain.SharedKernel.Common.Enums.Playback;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -31,7 +32,12 @@ public class GetUserSettingsEndpointSummary : Summary<GetUserSettingsEndpoint, E
ShouldAggregateMetadataWhenMissing: false,
ShouldAggregateArtworkWhenMissing: false,
ShouldRenderPdfAsImages: false,
ShouldPreserveBookStyles: true
ShouldPreserveBookStyles: true,
ShouldKeepDownloadingWhilePaused: true,
ShouldAutoplay: true,
IsGaplessPlaybackEnabled: true,
ReplayGainMode: ReplayGainMode.Off,
StreamingQuality: PlaybackStreamingQuality.Auto
));
Response(401, "Authentication required.", "application/problem+json",
@@ -0,0 +1,56 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Common.Mapping.UsersManagement.Users;
using Lumina.Application.Core.UsersManagement.Settings.Commands.UpdatePlaybackState;
using Lumina.Contracts.Requests.UsersManagement.Settings;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Settings.UpdatePlaybackState;
/// <summary>
/// API endpoint for the <c>/users/me/settings/playback-state</c> route.
/// </summary>
/// <remarks>
/// Uses "me" because it is always for the currently authenticated user.
/// </remarks>
public class UpdatePlaybackStateEndpoint : BaseEndpoint<UpdatePlaybackStateRequest, IResult>
{
private readonly ICommandHandler<UpdatePlaybackStateCommand, Result<Updated>> _updatePlaybackStateCommandHandler;
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePlaybackStateEndpoint"/> class.
/// </summary>
/// <param name="updatePlaybackStateCommandHandler">Injected service for handling update playback state commands.</param>
public UpdatePlaybackStateEndpoint(ICommandHandler<UpdatePlaybackStateCommand, Result<Updated>> updatePlaybackStateCommandHandler)
{
_updatePlaybackStateCommandHandler = updatePlaybackStateCommandHandler;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(FastEndpoints.Http.PUT);
Routes(ApiRoutes.Users.UPDATE_PLAYBACK_STATE);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Updates the playback state the player reported for the current user automatically.
/// </summary>
/// <param name="request">The request containing the latest playback state of the current user.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(UpdatePlaybackStateRequest request, CancellationToken cancellationToken)
{
Result<Updated> result = await _updatePlaybackStateCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,87 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Requests.UsersManagement.Settings;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Settings.UpdatePlaybackState;
/// <summary>
/// Class used for providing a textual description for the <see cref="UpdatePlaybackStateEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class UpdatePlaybackStateEndpointSummary : Summary<UpdatePlaybackStateEndpoint, UpdatePlaybackStateRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePlaybackStateEndpointSummary"/> class.
/// </summary>
public UpdatePlaybackStateEndpointSummary()
{
Summary = "Updates the playback state the player reported for the current user.";
Description = "Updates the last volume and mute state the player reported for the current user. This is written automatically by the player and is not part of the settings the user edits. When no settings are stored for the user yet, they are created with the provided playback state.";
ExampleRequest = new UpdatePlaybackStateRequest(
Volume: 0.75,
IsMuted: false
);
RequestParam(r => r.Volume, "The last volume the player reported, as a value between 0 and 1. Required.");
RequestParam(r => r.IsMuted, "The last mute state the player reported. Required.");
Response(200, "The playback state of the current user was successfully updated.");
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Authentication failed",
instance = "/api/v1/users/me/settings/playback-state"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token has expired",
instance = "/api/v1/users/me/settings/playback-state"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/api/v1/users/me/settings/playback-state"
}
}
);
Response(422, "The request did not pass validation checks.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
title = "General.Validation",
status = 422,
detail = "OneOrMoreValidationErrorsOccurred",
instance = "/api/v1/users/me/settings/playback-state",
errors = new Dictionary<string, string[]>
{
{
"General.Validation", new[]
{
"VolumeCannotBeNull",
"VolumeMustBeBetweenZeroAndOne",
"IsMutedCannotBeNull"
}
}
},
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
}
);
}
}
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Requests.UsersManagement.Settings;
using Lumina.Domain.SharedKernel.Common.Enums.Playback;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -29,7 +30,12 @@ public class UpdateUserSettingsEndpointSummary : Summary<UpdateUserSettingsEndpo
ShouldAggregateMetadataWhenMissing: false,
ShouldAggregateArtworkWhenMissing: false,
ShouldRenderPdfAsImages: false,
ShouldPreserveBookStyles: true
ShouldPreserveBookStyles: true,
ShouldKeepDownloadingWhilePaused: true,
ShouldAutoplay: true,
IsGaplessPlaybackEnabled: true,
ReplayGainMode: ReplayGainMode.Off,
StreamingQuality: PlaybackStreamingQuality.Auto
);
RequestParam(r => r.IsPaginationEnabled, "Whether pagination is enabled for the user, or not. Required.");
@@ -40,6 +46,11 @@ public class UpdateUserSettingsEndpointSummary : Summary<UpdateUserSettingsEndpo
RequestParam(r => r.ShouldAggregateArtworkWhenMissing, "Whether the artwork of the media library items is aggregated from multiple providers, when it is missing, or not. Required.");
RequestParam(r => r.ShouldRenderPdfAsImages, "Whether PDF books are rendered as page images for the user, or not. Required.");
RequestParam(r => r.ShouldPreserveBookStyles, "Whether the styles of the book content are preserved when it is rendered for the user, or not. Required.");
RequestParam(r => r.ShouldKeepDownloadingWhilePaused, "Whether the player keeps downloading a track while playback is paused, or not. Required.");
RequestParam(r => r.ShouldAutoplay, "Whether the player continues playback at the end of an album or artist, or not. Required.");
RequestParam(r => r.IsGaplessPlaybackEnabled, "Whether the player transitions between consecutive tracks without a gap, or not. Required.");
RequestParam(r => r.ReplayGainMode, "The way ReplayGain normalization is applied to the tracks the user plays. Required.");
RequestParam(r => r.StreamingQuality, "The quality cap applied to the audio the server normalizes for the user. Required.");
Response(200, "The settings of the current user were successfully updated.");
@@ -86,7 +97,14 @@ public class UpdateUserSettingsEndpointSummary : Summary<UpdateUserSettingsEndpo
{
"General.Validation", new[]
{
"ItemsPerPageMustBeGreaterThanZero"
"ItemsPerPageMustBeGreaterThanZero",
"ShouldKeepDownloadingWhilePausedCannotBeNull",
"ShouldAutoplayCannotBeNull",
"IsGaplessPlaybackEnabledCannotBeNull",
"ReplayGainModeCannotBeNull",
"StreamingQualityCannotBeNull",
"InvalidReplayGainMode",
"InvalidStreamingQuality"
}
}
},