using System.Threading;
using Kavita.API.Services;
using Kavita.Models.Entities.Progress;
using Kavita.Models.Entities.User;
namespace Kavita.Server.Middleware;
///
/// Thread-safe accessor for client information using AsyncLocal storage.
/// Client info is set by middleware at the start of each request and automatically
/// cleared when the request completes.
///
public class ClientInfoAccessor : IClientInfoAccessor
{
private static readonly AsyncLocal ClientInfo = new();
private static readonly AsyncLocal UiFingerprint = new();
private static readonly AsyncLocal DeviceId = new();
public ClientInfoData? Current => ClientInfo.Value;
public string? CurrentUiFingerprint => UiFingerprint.Value;
public int? CurrentDeviceId => DeviceId.Value;
///
/// Sets the client info for the current async context.
/// Should only be called by middleware.
///
internal static void SetClientInfo(ClientInfoData? info)
{
ClientInfo.Value = info;
}
///
/// Sets the client fingerprint for the current async context.
/// Should only be called by middleware.
///
internal static void SetUiFingerprint(string uiFingerprint)
{
UiFingerprint.Value = uiFingerprint;
}
///
/// Sets the for the current async context.
/// Should only be called by middleware.
///
internal static void SetDeviceId(int clientDeviceId)
{
DeviceId.Value = clientDeviceId;
}
}