about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-03 18:40:53 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-03 18:40:53 +0200
commit3d3edeae16252a311704b390cfad6faa435a8b84 (patch)
tree34974194435fbe9789de5140ef9a9c0ddb834c74 /MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
parentAdd policy room discovery ,add room state viewer (diff)
downloadMatrixUtils-3d3edeae16252a311704b390cfad6faa435a8b84.tar.xz
Refactor
Diffstat (limited to '')
-rw-r--r--MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
new file mode 100644
index 0000000..462364c
--- /dev/null
+++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
@@ -0,0 +1,35 @@
+using Blazored.LocalStorage;
+using MatrixRoomUtils.Authentication;
+using MatrixRoomUtils.Responses;
+
+namespace MatrixRoomUtils.Web.Classes;
+
+public partial class LocalStorageWrapper
+{
+    //some basic logic
+    public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
+    {
+        RuntimeCache.AccessToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.token");
+        RuntimeCache.CurrentHomeserver = await localStorage.GetItemAsync<string>("rory.matrixroomutils.current_homeserver");
+        RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.user_cache") ?? new();
+        RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new();
+        Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries");
+        if (RuntimeCache.AccessToken != null && RuntimeCache.CurrentHomeserver != null)
+        {
+            Console.WriteLine($"Access token and current homeserver are not null, creating authenticated home server");
+            RuntimeCache.CurrentHomeServer = new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.AccessToken].LoginResponse.UserId, RuntimeCache.AccessToken, RuntimeCache.LoginSessions[RuntimeCache.AccessToken].LoginResponse.HomeServer);
+            Console.WriteLine("Created authenticated home server");
+        }
+        RuntimeCache.WasLoaded = true;
+    }
+
+    public static async Task SaveToLocalStorage(ILocalStorageService localStorage)
+    {
+        await localStorage.SetItemAsStringAsync("rory.matrixroomutils.token", RuntimeCache.AccessToken);
+        await localStorage.SetItemAsync("rory.matrixroomutils.current_homeserver", RuntimeCache.CurrentHomeserver);
+        await localStorage.SetItemAsync("rory.matrixroomutils.user_cache", RuntimeCache.LoginSessions);
+        await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache", 
+            RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
+                .ToDictionary(x => x.Key, x => x.Value));
+    }
+}
\ No newline at end of file