about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Web/Classes')
-rw-r--r--MatrixRoomUtils.Web/Classes/LocalStorageProviderService.cs18
-rw-r--r--MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs84
-rw-r--r--MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs91
-rw-r--r--MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs19
4 files changed, 118 insertions, 94 deletions
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageProviderService.cs b/MatrixRoomUtils.Web/Classes/LocalStorageProviderService.cs
index 2a9082d..256c43d 100644
--- a/MatrixRoomUtils.Web/Classes/LocalStorageProviderService.cs
+++ b/MatrixRoomUtils.Web/Classes/LocalStorageProviderService.cs
@@ -4,7 +4,23 @@ using MatrixRoomUtils.Core.Interfaces.Services;
 namespace MatrixRoomUtils.Web.Classes; 
 
 public class LocalStorageProviderService : IStorageProvider {
+    private readonly ILocalStorageService _localStorageService;
+
     public LocalStorageProviderService(ILocalStorageService localStorageService) {
-        
+        _localStorageService = localStorageService;
     }
+
+    async Task IStorageProvider.SaveAllChildrenAsync<T>(string key, T value) => throw new NotImplementedException();
+
+    async Task<T?> IStorageProvider.LoadAllChildrenAsync<T>(string key) where T : default => throw new NotImplementedException();
+
+    async Task IStorageProvider.SaveObjectAsync<T>(string key, T value) => await _localStorageService.SetItemAsync(key, value);
+
+    async Task<T?> IStorageProvider.LoadObjectAsync<T>(string key) where T : default => await _localStorageService.GetItemAsync<T>(key);
+
+    async Task<bool> IStorageProvider.ObjectExistsAsync(string key) => await _localStorageService.ContainKeyAsync(key);
+
+    async Task<List<string>> IStorageProvider.GetAllKeysAsync() => (await _localStorageService.KeysAsync()).ToList();
+
+    async Task IStorageProvider.DeleteObjectAsync(string key) => await _localStorageService.RemoveItemAsync(key);
 }
\ No newline at end of file
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
deleted file mode 100644
index 59c1ab6..0000000
--- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
+++ /dev/null
@@ -1,84 +0,0 @@
-using Blazored.LocalStorage;
-using MatrixRoomUtils.Core;
-
-namespace MatrixRoomUtils.Web.Classes;
-
-public class LocalStorageWrapper {
-    private readonly SemaphoreSlim _semaphoreSlim = new(1);
-    public Settings Settings { get; set; } = new();
-
-    //some basic logic
-    public async Task InitialiseRuntimeVariables(ILocalStorageService localStorage) {
-        //RuntimeCache stuff
-        // async Task Save() => await SaveToLocalStorage(localStorage);
-        // async Task SaveObject(string key, object obj) => await localStorage.SetItemAsync(key, obj);
-        // async Task RemoveObject(string key) => await localStorage.RemoveItemAsync(key);
-        //
-        // RuntimeCache.Save = Save;
-        // RuntimeCache.SaveObject = SaveObject;
-        // RuntimeCache.RemoveObject = RemoveObject;
-        if (RuntimeCache.LastUsedToken != null) {
-            Console.WriteLine("Access token is not null, creating authenticated home server");
-            Console.WriteLine($"Homeserver cache: {RuntimeCache.HomeserverResolutionCache.Count} entries");
-            // Console.WriteLine(RuntimeCache.HomeserverResolutionCache.ToJson());
-            RuntimeCache.CurrentHomeServer = await new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.HomeServer, RuntimeCache.LastUsedToken, null).Configure();
-            Console.WriteLine("Created authenticated home server");
-        }
-    }
-
-    public async Task LoadFromLocalStorage(ILocalStorageService localStorage) {
-        await _semaphoreSlim.WaitAsync();
-        if (RuntimeCache.WasLoaded) {
-            _semaphoreSlim.Release();
-            return;
-        }
-
-        Console.WriteLine("Loading from local storage...");
-        Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new Settings();
-
-        RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token");
-        RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.login_sessions") ?? new Dictionary<string, UserInfo>();
-        RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new Dictionary<string, HomeServerResolutionResult>();
-        Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries");
-
-        //RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new();
-
-        foreach (var s in (await localStorage.KeysAsync()).Where(x => x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList()) {
-            Console.WriteLine($"Loading generic cache entry {s}");
-            RuntimeCache.GenericResponseCache[s.Replace("rory.matrixroomutils.generic_cache:", "")] = await localStorage.GetItemAsync<ObjectCache<object>>(s);
-        }
-
-        await InitialiseRuntimeVariables(localStorage);
-        RuntimeCache.WasLoaded = true;
-        _semaphoreSlim.Release();
-    }
-
-    public async Task SaveToLocalStorage(ILocalStorageService localStorage) {
-        Console.WriteLine("Saving to local storage...");
-        await localStorage.SetItemAsync("rory.matrixroomutils.settings", Settings);
-        if (RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.login_sessions", RuntimeCache.LoginSessions);
-        if (RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken);
-    }
-
-    public async Task SaveCacheToLocalStorage(ILocalStorageService localStorage, bool awaitSave = true, bool saveGenericCache = true) {
-        await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache",
-            RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
-                .ToDictionary(x => x.Key, x => x.Value));
-        //await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache);
-        if (saveGenericCache)
-            foreach (var s in RuntimeCache.GenericResponseCache.Keys) {
-                var t = localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]);
-                if (awaitSave) await t;
-            }
-    }
-}
-
-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;
-}
\ No newline at end of file
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
diff --git a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs
index 4a31928..ca6345f 100644
--- a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs
+++ b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs
@@ -15,25 +15,25 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate {
             InitialState = new List<StateEvent> {
                 new() {
                     Type = "m.room.history_visibility",
-                    Content = new {
+                    TypedContent = new {
                         history_visibility = "world_readable"
                     }
                 },
-                new StateEvent<GuestAccessData> {
+                new() {
                     Type = "m.room.guest_access",
-                    Content = new GuestAccessData {
+                    TypedContent = new GuestAccessData {
                         GuestAccess = "can_join"
                     }
                 },
                 new() {
                     Type = "m.room.join_rules",
-                    Content = new {
-                        join_rule = "public"
+                    TypedContent = new JoinRulesEventData() {
+                        JoinRule = "public"
                     }
                 },
                 new() {
                     Type = "m.room.server_acl",
-                    Content = new {
+                    TypedContent = new {
                         allow = new[] { "*" },
                         deny = Array.Empty<string>(),
                         allow_ip_literals = false
@@ -41,8 +41,8 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate {
                 },
                 new() {
                     Type = "m.room.avatar",
-                    Content = new {
-                        url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj"
+                    TypedContent = new RoomAvatarEventData() {
+                        Url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj"
                     }
                 }
             },
@@ -78,7 +78,8 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate {
                     { "org.matrix.msc3401.call.member", 50 }
                 },
                 Users = new Dictionary<string, int> {
-                    { RuntimeCache.CurrentHomeServer.UserId, 100 }
+                    // { RuntimeCache.CurrentHomeServer.UserId, 100 }
+                    //TODO: re-implement this
                 }
             },
             CreationContent = new JsonObject {