diff options
| author | Jake Mannens <jake@asger.xyz> | 2026-05-22 12:46:00 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2026-05-23 22:13:00 +1000 |
| commit | 4ea3ddb38d010c2f85c22b7f1c3f2d7e0c1355e3 (patch) | |
| tree | 90af9203059d645eb77216f1a091722ee9702438 /Services/UserService.cs | |
| parent | 6de5d7f5364fe1d54703da6d6b7cb08ea26e939f (diff) | |
Initial commitwasm-oldserver
Diffstat (limited to 'Services/UserService.cs')
| -rw-r--r-- | Services/UserService.cs | 76 |
1 files changed, 0 insertions, 76 deletions
diff --git a/Services/UserService.cs b/Services/UserService.cs deleted file mode 100644 index 9e79dc6..0000000 --- a/Services/UserService.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Microsoft.AspNetCore.Cryptography.KeyDerivation; - -namespace HyperBooru.Services; - -public interface IUserService { - public UserSessionState UserSessionState { get; } -} - -public class UserService : IUserService { - public UserSessionState UserSessionState => - globalUserService.GetSessionState(httpContext.Session.Id); - - private IHttpContextAccessor httpContextAccessor; - private IGlobalUserService globalUserService; - - private HttpContext httpContext => - httpContextAccessor.HttpContext!; - - public UserService( - IHttpContextAccessor httpContextAccessor, - IGlobalUserService globalUserService) { - - this.httpContextAccessor = httpContextAccessor; - this.globalUserService = globalUserService; - - // HTTP context session states are discarded if no values - // are set. Set a dummy value so that the session state - // will not be discarded later when we actually need it. - httpContext.Session.SetInt32("Persist", 1); - } - - public static string HashPassword(string password) => - Convert.ToBase64String( - KeyDerivation.Pbkdf2( - password, - Array.Empty<byte>(), - KeyDerivationPrf.HMACSHA512, - 100_000, - 512 / 8)); -} - -public interface IGlobalUserService { - public UserSessionState GetSessionState(string id); -} - -public class GlobalUserService : IGlobalUserService { - // TODO: prune this list periodically - private Dictionary<string, UserSessionState> sessionStates = new(); - - public UserSessionState GetSessionState(string id) { - sessionStates.TryGetValue(id, out var state); - - if(state is null) { - state = new(); - sessionStates[id] = state; - } - - return state; - } -} - -public record UserSessionState { - public event UserSessionStateChange OnStateChange; - - public bool ShowNsfw { - get => showNsfw; - set { - showNsfw = value; - OnStateChange.Invoke(this); - } - } - - private bool showNsfw = false; -} - -public delegate void UserSessionStateChange(UserSessionState sessionState); |
