about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
blob: a439ea14a19f1c398e6ba14b2830c9b5d7a41fdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Blazored.LocalStorage;
using MatrixRoomUtils.Core;
using MatrixRoomUtils.Core.Extensions;

namespace MatrixRoomUtils.Web.Classes;

public partial class LocalStorageWrapper
{
    public static Settings Settings { get; set; } = new();
    
    //some basic logic
    public static async Task ReloadLocalStorage(ILocalStorageService localStorage)
    {
        await SaveToLocalStorage(localStorage);
        await LoadFromLocalStorage(localStorage);
    }
    public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
    {
        Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new();
        // 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");
        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.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.UserId, RuntimeCache.LastUsedToken, RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.HomeServer).Configure();
            Console.WriteLine("Created authenticated home server");
        }
        RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new();
        RuntimeCache.WasLoaded = true;
    }

    public static async Task SaveToLocalStorage(ILocalStorageService localStorage)
    {
        await localStorage.SetItemAsync("rory.matrixroomutils.settings", Settings);
        // if(RuntimeCache.AccessToken != null) await localStorage.SetItemAsStringAsync("rory.matrixroomutils.token", RuntimeCache.AccessToken);
        // if(RuntimeCache.CurrentHomeserver != null) await localStorage.SetItemAsync("rory.matrixroomutils.current_homeserver", RuntimeCache.CurrentHomeserver);
        if(RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.user_cache", RuntimeCache.LoginSessions);
        if(RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken);
        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);
    }
}


public class Settings
{
    public DeveloperSettings DeveloperSettings { get; set; } = new();
}


public class DeveloperSettings
{
    public bool EnableLogViewers { get; set; } = false;
}