MatrixRoomUtils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

LocalStorageWrapper.cs (4718B)


      1 using Blazored.LocalStorage;
      2 using MatrixRoomUtils.Core;
      3 
      4 namespace MatrixRoomUtils.Web.Classes;
      5 
      6 public partial class LocalStorageWrapper
      7 {
      8     private static SemaphoreSlim _semaphoreSlim = new(1);
      9     public static Settings Settings { get; set; } = new();
     10 
     11     //some basic logic
     12     public static async Task InitialiseRuntimeVariables(ILocalStorageService localStorage)
     13     {
     14         //RuntimeCache stuff
     15         async void Save() => await SaveToLocalStorage(localStorage);
     16 
     17         RuntimeCache.Save = Save;
     18         RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj);
     19         if (RuntimeCache.LastUsedToken != null)
     20         {
     21             Console.WriteLine($"Access token is not null, creating authenticated home server");
     22             Console.WriteLine($"Homeserver cache: {RuntimeCache.HomeserverResolutionCache.Count} entries");
     23             // Console.WriteLine(RuntimeCache.HomeserverResolutionCache.ToJson());
     24             RuntimeCache.CurrentHomeServer = await new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.UserId, RuntimeCache.LastUsedToken,
     25                 RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.HomeServer).Configure();
     26             Console.WriteLine("Created authenticated home server");
     27         }
     28     }
     29 
     30     public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
     31     {
     32         await _semaphoreSlim.WaitAsync();
     33         if (RuntimeCache.WasLoaded)
     34         {
     35             _semaphoreSlim.Release();
     36             return;
     37         }
     38         Console.WriteLine("Loading from local storage...");
     39         Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new();
     40 
     41         RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token");
     42         RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.login_sessions") ?? new();
     43         RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new();
     44         Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries");
     45 
     46         //RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new();
     47 
     48         foreach (var s in (await localStorage.KeysAsync()).Where(x => x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList())
     49         {
     50             Console.WriteLine($"Loading generic cache entry {s}");
     51             RuntimeCache.GenericResponseCache[s.Replace("rory.matrixroomutils.generic_cache:", "")] = await localStorage.GetItemAsync<ObjectCache<object>>(s);
     52         }
     53 
     54         await InitialiseRuntimeVariables(localStorage);
     55         RuntimeCache.WasLoaded = true;
     56         _semaphoreSlim.Release();
     57     }
     58 
     59     public static async Task SaveToLocalStorage(ILocalStorageService localStorage)
     60     {
     61         Console.WriteLine("Saving to local storage...");
     62         await localStorage.SetItemAsync("rory.matrixroomutils.settings", Settings);
     63         if (RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.login_sessions", RuntimeCache.LoginSessions);
     64         if (RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken);
     65     }
     66 
     67     public static async Task SaveCacheToLocalStorage(ILocalStorageService localStorage, bool awaitSave = true, bool saveGenericCache = true)
     68     {
     69         await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache",
     70             RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
     71                 .ToDictionary(x => x.Key, x => x.Value));
     72         //await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache);
     73         if(saveGenericCache)
     74             foreach (var s in RuntimeCache.GenericResponseCache.Keys)
     75             {
     76                 var t = localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]);
     77                 if (awaitSave) await t;
     78             }
     79     }
     80 }
     81 
     82 public class Settings
     83 {
     84     public DeveloperSettings DeveloperSettings { get; set; } = new();
     85 }
     86 
     87 public class DeveloperSettings
     88 {
     89     public bool EnableLogViewers { get; set; } = false;
     90     public bool EnableConsoleLogging { get; set; } = true;
     91     public bool EnablePortableDevtools { get; set; } = false;
     92 }