about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Web')
-rw-r--r--MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs17
-rw-r--r--MatrixRoomUtils.Web/Shared/RoomListItem.razor34
2 files changed, 46 insertions, 5 deletions
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
index c224160..4a00a8a 100644
--- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
+++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
@@ -16,6 +16,12 @@ public partial class LocalStorageWrapper
     public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
     {
         Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new();
+        
+        //RuntimeCache stuff
+        async void Save() => await SaveToLocalStorage(localStorage);
+
+        RuntimeCache.Save = Save;
+        RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj); 
         // RuntimeCache.AccessToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.token");
         RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token");
         // RuntimeCache.CurrentHomeserver = await localStorage.GetItemAsync<string>("rory.matrixroomutils.current_homeserver");
@@ -31,6 +37,12 @@ public partial class LocalStorageWrapper
             Console.WriteLine("Created authenticated home server");
         }
         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);
+        }
         RuntimeCache.WasLoaded = true;
     }
 
@@ -45,6 +57,10 @@ public partial class LocalStorageWrapper
             RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
                 .ToDictionary(x => x.Key, x => x.Value));
         await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache);
+        // foreach (var s in RuntimeCache.GenericResponseCache.Keys)
+        // {
+            // await localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]);
+        // }
     }
     public static async Task SaveFieldToLocalStorage(ILocalStorageService localStorage, string key)
     {
@@ -55,7 +71,6 @@ public partial class LocalStorageWrapper
         if (key == "rory.matrixroomutils.last_used_token") await localStorage.SetItemAsync(key, RuntimeCache.LastUsedToken);
         if (key == "rory.matrixroomutils.homeserver_resolution_cache") await localStorage.SetItemAsync(key, RuntimeCache.HomeserverResolutionCache);
         if (key == "rory.matrixroomutils.generic_cache") await localStorage.SetItemAsync(key, RuntimeCache.GenericResponseCache);
-        
     }
 }
 
diff --git a/MatrixRoomUtils.Web/Shared/RoomListItem.razor b/MatrixRoomUtils.Web/Shared/RoomListItem.razor
index 16ced75..317d25a 100644
--- a/MatrixRoomUtils.Web/Shared/RoomListItem.razor
+++ b/MatrixRoomUtils.Web/Shared/RoomListItem.razor
@@ -1,6 +1,7 @@
 @using MatrixRoomUtils.Core.Authentication
 @using System.Text.Json
-<div style="background-color: #ffffff11; border-radius: 25px; margin: 8px; width: fit-content;">
+@using MatrixRoomUtils.Core.Extensions
+<div style="background-color: #ffffff11; border-radius: 25px; margin: 8px; width: fit-content; @(hasDangerousRoomVersion ? "border: red 4px solid;" : hasOldRoomVersion ? "border: #FF0 1px solid;" : "")">
     @if (ShowOwnProfile)
     {
         <img style="@(ChildContent != null ? "vertical-align: baseline;":"") width: 32px; height:  32px; border-radius: 50%; @(hasCustomProfileAvatar ? "border-color: red; border-width: 3px; border-style: dashed;" : "")" src="@profileAvatar"/>
@@ -39,6 +40,9 @@
     private string profileName { get; set; } = "Loading...";
     private bool hasCustomProfileAvatar { get; set; } = false;
     private bool hasCustomProfileName { get; set; } = false;
+    
+    private bool hasOldRoomVersion { get; set; } = false;
+    private bool hasDangerousRoomVersion { get; set; } = false;
 
     protected override async Task OnInitializedAsync()
     {
@@ -69,13 +73,35 @@
             roomName = "Unnamed room: " + RoomId;
         }
 
+        var ce = await Room.GetCreateEventAsync();
+        if (ce != null)
+        {
+            if (int.TryParse(ce.RoomVersion, out int rv) && rv < 10)
+            {
+                hasOldRoomVersion = true;
+            }
+            if (new[] { "1", "8" }.Contains(ce.RoomVersion))
+            {
+                hasDangerousRoomVersion = true;
+                roomName = "Dangerous room: " + roomName;
+            }
+        }
+        
+
         var state = await Room.GetStateAsync("m.room.avatar");
         if (state != null)
         {
-            var url = state.Value.GetProperty("url").GetString();
-            if (url != null)
+            try
+            {
+                var url = state.Value.GetProperty("url").GetString();
+                if (url != null)
+                {
+                    roomIcon = await RuntimeCache.CurrentHomeServer.ResolveMediaUri(url);
+                }
+            }
+            catch (InvalidOperationException e)
             {
-                roomIcon = await RuntimeCache.CurrentHomeServer.ResolveMediaUri(url);
+                Console.WriteLine($"Failed to get avatar for room {RoomId}: {e.Message}\n{state.Value.ToJson()}");
             }
         }