about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-30 03:36:58 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-30 03:36:58 +0200
commitbb8c2637af3b7982e7a4b2fd15e2fbec613d0848 (patch)
treeb8075ba7e507aad3f96f354712ad920ac421e474 /MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
parentUpdate stuff (diff)
downloadMatrixUtils-bb8c2637af3b7982e7a4b2fd15e2fbec613d0848.tar.xz
Todays progress
Diffstat (limited to 'MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs')
-rw-r--r--MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs91
1 files changed, 91 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
new file mode 100644
index 0000000..41f604d
--- /dev/null
+++ b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
@@ -0,0 +1,91 @@
+using MatrixRoomUtils.Core;
+using MatrixRoomUtils.Core.Responses;
+using MatrixRoomUtils.Core.Services;
+using Microsoft.AspNetCore.Components;
+
+namespace MatrixRoomUtils.Web.Classes;
+
+public class MRUStorageWrapper {
+    private readonly TieredStorageService _storageService;
+    private readonly HomeserverProviderService _homeserverProviderService;
+    private readonly NavigationManager _navigationManager;
+
+    public MRUStorageWrapper(
+        TieredStorageService storageService,
+        HomeserverProviderService homeserverProviderService,
+        NavigationManager navigationManager
+    ) {
+        _storageService = storageService;
+        _homeserverProviderService = homeserverProviderService;
+        _navigationManager = navigationManager;
+    }
+
+    public async Task<List<LoginResponse>?> GetAllTokens() {
+        return await _storageService.DataStorageProvider.LoadObjectAsync<List<LoginResponse>>("mru.tokens") ?? new List<LoginResponse>();
+    }
+    public async Task<LoginResponse?> GetCurrentToken() {
+        var currentToken = await _storageService.DataStorageProvider.LoadObjectAsync<LoginResponse>("token");
+        var allTokens = await GetAllTokens();
+        if (allTokens is null or { Count: 0 }) {
+            await SetCurrentToken(null);
+            return null;
+        }
+        if (currentToken is null) {
+            await SetCurrentToken(currentToken = allTokens[0]);
+        }
+        if(!allTokens.Any(x=>x.AccessToken == currentToken.AccessToken)) {
+            await SetCurrentToken(currentToken = allTokens[0]);
+        }
+        return currentToken;
+    }
+
+    public async Task AddToken(LoginResponse loginResponse) {
+        var tokens = await GetAllTokens();
+        if (tokens == null) {
+            tokens = new List<LoginResponse>();
+        }
+
+        tokens.Add(loginResponse);
+        await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens);
+    }
+
+    public async Task<AuthenticatedHomeServer?> GetCurrentSession() {
+        var token = await GetCurrentToken();
+        if (token == null) {
+            return null;
+        }
+        
+        return await _homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken); 
+    }
+
+    public async Task<AuthenticatedHomeServer?> GetCurrentSessionOrNavigate() {
+        var session = await GetCurrentSession();
+        if (session == null) {
+            _navigationManager.NavigateTo("/Login");
+        }
+        return session;
+    }
+    public class Settings {
+        public DeveloperSettings DeveloperSettings { get; set; } = new();
+    }
+
+    public class DeveloperSettings {
+        public bool EnableLogViewers { get; set; } = false;
+        public bool EnableConsoleLogging { get; set; } = true;
+        public bool EnablePortableDevtools { get; set; } = false;
+    }
+
+    public async Task RemoveToken(LoginResponse auth) {
+        var tokens = await GetAllTokens();
+        if (tokens == null) {
+            return;
+        }
+
+        tokens.RemoveAll(x=>x.AccessToken == auth.AccessToken);
+        await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens);
+    }
+
+    public async Task SetCurrentToken(LoginResponse? auth) {
+        _storageService.DataStorageProvider.SaveObjectAsync("token", auth);
+    }
+}
\ No newline at end of file