about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/LoginPage.razor
blob: e19dd040b85648a976a8e2c55175579266806f12 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
@page "/Login"
@using System.Text.Json
@using MatrixRoomUtils.Core.Responses
@using MatrixRoomUtils.Web.Shared.SimpleComponents
@inject ILocalStorageService LocalStorage
@inject IJSRuntime JsRuntime
<h3>Login</h3>
<hr/>

<span>
    <span>@@</span><!--
    --><FancyTextBox @bind-Value="@newRecordInput.username"></FancyTextBox><!--
    --><span>:</span><!--
    --><FancyTextBox @bind-Value="@newRecordInput.homeserver"></FancyTextBox>
</span>
<span style="display: block;">
    <label>Password:</label>
    <FancyTextBox @bind-Value="@newRecordInput.password" IsPassword="true"></FancyTextBox>
</span>
<button @onclick="AddRecord">Add account to queue</button>
<br/>

<InputFile OnChange="@FileChanged" accept=".tsv"></InputFile>
<br/>
<br/><br/>
<h4>Parsed records</h4>
<hr/>
<table border="1">
    <thead>
        <td>Username</td>
        <td>Homeserver</td>
    </thead>
    @foreach (var (homeserver, username, password) in records) {
        var record = (homeserver, username, password);
        <tr style="background-color: @(LoggedInSessions.Any(x => x.UserId == $"@{username}:{homeserver}") ? "green" : "unset")">
            <td style="border-width: 1px;">@username</td>
            <td style="border-width: 1px;">@homeserver</td>
            <td><a role="button" @onclick="() => records.Remove(record)">Remove</a></td>
        </tr>
    }
</table>
<br/>
<button @onclick="Login">Login</button>
<br/>
<LogView></LogView>

@code {
    readonly List<(string homeserver, string username, string password)> records = new();
    (string homeserver, string username, string password) newRecordInput = ("", "", "");

    List<LoginResponse> LoggedInSessions { get; set; } = new();
    
    async Task Login() {
        var loginTasks = records.Select(async record => {
            var (homeserver, username, password) = record;
            if (LoggedInSessions.Any(x => x.UserId == $"@{username}:{homeserver}")) return;
            try {
                var result = await HomeserverProvider.Login(homeserver, username, password);
                if (result == null) {
                    Console.WriteLine($"Failed to login to {homeserver} as {username}!");
                    return;
                }
                Console.WriteLine($"Obtained access token for {result.UserId}!");

                await MRUStorage.AddToken(result);
                LoggedInSessions = await MRUStorage.GetAllTokens();
            }
            catch (Exception e) {
                Console.WriteLine($"Failed to login to {homeserver} as {username}!");
                Console.WriteLine(e);
            }
            StateHasChanged();
        });
        await Task.WhenAll(loginTasks);
    }

    private async Task FileChanged(InputFileChangeEventArgs obj) {
        LoggedInSessions = await MRUStorage.GetAllTokens();
        Console.WriteLine(JsonSerializer.Serialize(obj, new JsonSerializerOptions {
            WriteIndented = true
        }));
        await using var rs = obj.File.OpenReadStream();
        using var sr = new StreamReader(rs);
        var TsvData = await sr.ReadToEndAsync();
        records.Clear();
        foreach (var line in TsvData.Split('\n')) {
            var parts = line.Split('\t');
            if (parts.Length != 3)
                continue;
            records.Add((parts[0], parts[1], parts[2]));
        }
    }

    private async Task AddRecord() {
        LoggedInSessions = await MRUStorage.GetAllTokens();
        records.Add(newRecordInput);
        newRecordInput = ("", "", "");
    }

}