about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/DataExportPage.razor
blob: f9a4c373bd1616639494025e9ba7796dd114ea36 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@page "/export"
@using MatrixRoomUtils.Web.Classes
@using MatrixRoomUtils.Web.Shared.IndexComponents
@using Blazored.LocalStorage
@using MatrixRoomUtils.Authentication
@using System.Text.Json
@using Microsoft.AspNetCore.Components.Rendering
@inject NavigationManager NavigationManager
@inject ILocalStorageService LocalStorage

<PageTitle>Export</PageTitle>

<h1>Data export</h1>

<br/><br/>
<h5>Signed in accounts - <a href="/Login">Add new account</a> or <a href="/ImportUsers">Import from TSV</a></h5>
<hr/>
@if (_isLoaded)
{
@foreach (var (token, user) in RuntimeCache.LoginSessions)
{
    <IndexUserItem User="@user" Token="@token"/>
    <pre>
@user.LoginResponse.UserId[1..].Split(":")[0]\auth\access_token=@token
@user.LoginResponse.UserId[1..].Split(":")[0]\auth\device_id=@user.LoginResponse.DeviceId
@user.LoginResponse.UserId[1..].Split(":")[0]\auth\home_server=@(RuntimeCache.HomeserverResolutionCache.ContainsKey(user.LoginResponse.HomeServer) ? RuntimeCache.HomeserverResolutionCache[user.LoginResponse.HomeServer].Result : "loading...")
@user.LoginResponse.UserId[1..].Split(":")[0]\auth\user_id=@@@user.LoginResponse.UserId
@user.LoginResponse.UserId[1..].Split(":")[0]\user\automatically_share_keys_with_trusted_users=true
@user.LoginResponse.UserId[1..].Split(":")[0]\user\muted_tags=global
@user.LoginResponse.UserId[1..].Split(":")[0]\user\online_key_backup=true
@user.LoginResponse.UserId[1..].Split(":")[0]\user\only_share_keys_with_verified_users=false
    </pre>
}
}
else
{
    <p>Loading...</p>
    <p>@resolvedHomeservers/@totalHomeservers homeservers resolved...</p>
}

@code {
    private bool _isLoaded;
    private int resolvedHomeservers;
    private int totalHomeservers;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        if (!RuntimeCache.WasLoaded)
        {
            await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);

            var homeservers = RuntimeCache.LoginSessions.Values.Select(x => x.LoginResponse.HomeServer).Distinct();
            totalHomeservers = homeservers.Count();
            StateHasChanged();
            foreach (var hs in homeservers)
            {
                if (LocalStorageWrapper.HomeserverResolutionCache.ContainsKey(hs)) continue;
                var resolvedHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(hs);

                if (LocalStorageWrapper.HomeserverResolutionCache.ContainsKey(hs))
                    LocalStorageWrapper.HomeserverResolutionCache.Remove(hs);
                LocalStorageWrapper.HomeserverResolutionCache.Add(hs, new() { Result = resolvedHomeserver, ResolutionTime = DateTime.Now });
                await LocalStorageWrapper.SaveToLocalStorage(LocalStorage);

                Console.WriteLine("Saved to local storage:");
                Console.WriteLine(JsonSerializer.Serialize(LocalStorageWrapper.HomeserverResolutionCache, new JsonSerializerOptions()
                {
                    WriteIndented = true
                }));
                resolvedHomeservers++;
                StateHasChanged();
            }
            StateHasChanged();
        }
        _isLoaded = true;
    }

}