about summary refs log tree commit diff
path: root/MatrixUtils.Web/Classes/RMUStorageWrapper.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-01-24 02:31:56 +0100
committerRory& <root@rory.gay>2024-01-24 17:05:25 +0100
commit03313562d21d5db9bf6a14ebbeab80e06c883d3a (patch)
treee000546a2ee8e6a886a7ed9fd01ad674178fb7cb /MatrixUtils.Web/Classes/RMUStorageWrapper.cs
parentMake RMU installable (diff)
downloadMatrixUtils-03313562d21d5db9bf6a14ebbeab80e06c883d3a.tar.xz
MRU->RMU, fixes, cleanup
Diffstat (limited to 'MatrixUtils.Web/Classes/RMUStorageWrapper.cs')
-rw-r--r--MatrixUtils.Web/Classes/RMUStorageWrapper.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/MatrixUtils.Web/Classes/RMUStorageWrapper.cs b/MatrixUtils.Web/Classes/RMUStorageWrapper.cs
new file mode 100644
index 0000000..31e7734
--- /dev/null
+++ b/MatrixUtils.Web/Classes/RMUStorageWrapper.cs
@@ -0,0 +1,98 @@
+using LibMatrix;
+using LibMatrix.Homeservers;
+using LibMatrix.Services;
+using Microsoft.AspNetCore.Components;
+
+namespace MatrixUtils.Web.Classes;
+
+public class RMUStorageWrapper(TieredStorageService storageService, HomeserverProviderService homeserverProviderService, NavigationManager navigationManager) {
+    public async Task<List<UserAuth>?> GetAllTokens() {
+        return await storageService.DataStorageProvider.LoadObjectAsync<List<UserAuth>>("rmu.tokens") ??
+               new List<UserAuth>();
+    }
+
+    public async Task<UserAuth?> GetCurrentToken() {
+        var currentToken = await storageService.DataStorageProvider.LoadObjectAsync<UserAuth>("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(UserAuth UserAuth) {
+        var tokens = await GetAllTokens() ?? new List<UserAuth>();
+
+        tokens.Add(UserAuth);
+        await storageService.DataStorageProvider.SaveObjectAsync("rmu.tokens", tokens);
+    }
+
+    private async Task<AuthenticatedHomeserverGeneric?> GetCurrentSession() {
+        var token = await GetCurrentToken();
+        if (token == null) {
+            return null;
+        }
+
+        return await homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken, token.Proxy);
+    }
+
+    public async Task<AuthenticatedHomeserverGeneric?> GetSession(UserAuth userAuth) {
+        return await homeserverProviderService.GetAuthenticatedWithToken(userAuth.Homeserver, userAuth.AccessToken, userAuth.Proxy);
+    }
+
+    public async Task<AuthenticatedHomeserverGeneric?> GetCurrentSessionOrNavigate() {
+        AuthenticatedHomeserverGeneric? session = null;
+
+        try {
+            //catch if the token is invalid
+            session = await GetCurrentSession();
+        }
+        catch (MatrixException e) {
+            if (e.ErrorCode == "M_UNKNOWN_TOKEN") {
+                var token = await GetCurrentToken();
+                navigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken);
+                return null;
+            }
+
+            throw;
+        }
+
+        if (session is null) {
+            navigationManager.NavigateTo("/Login");
+        }
+
+        return session;
+    }
+
+    public class Settings {
+        public DeveloperSettings DeveloperSettings { get; set; } = new();
+    }
+
+    public class DeveloperSettings {
+        public bool EnableLogViewers { get; set; }
+        public bool EnableConsoleLogging { get; set; } = true;
+        public bool EnablePortableDevtools { get; set; }
+    }
+
+    public async Task RemoveToken(UserAuth auth) {
+        var tokens = await GetAllTokens();
+        if (tokens == null) {
+            return;
+        }
+
+        tokens.RemoveAll(x => x.AccessToken == auth.AccessToken);
+        await storageService.DataStorageProvider.SaveObjectAsync("rmu.tokens", tokens);
+    }
+
+    public async Task SetCurrentToken(UserAuth? auth) => await storageService.DataStorageProvider.SaveObjectAsync("token", auth);
+}