about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/Index.razor
blob: 845bbb985ea9bcb28d0ef333f2a8a65bec3e08c7 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@page "/"
@using LibMatrix.Responses
@using LibMatrix
@using LibMatrix.Helpers
@using LibMatrix.Homeservers
@using ArcaneLibs.Extensions

<PageTitle>Index</PageTitle>

<h3>Rory&::MatrixUtils</h3>
Small collection of tools to do not-so-everyday things.

<br/><br/>
<h5>Signed in accounts - <a href="/Login">Add new account</a></h5>
<hr/>
<form>
    <table>
        @foreach (var (auth, user) in _users.OrderByDescending(x => x.Value.RoomCount)) {
            var _auth = auth;
            <tr class="user-entry">
                <td>
                    <img class="avatar" src="@user.AvatarUrl"/>
                </td>
                <td class="user-info">
                    @* <div class="user-info"> *@
                    <p>
                        <input type="radio" name="csa" checked="@(_currentSession.AccessToken == _auth.AccessToken)" @onclick="@(() => SwitchSession(_auth))" style="text-decoration-line: unset;"/>
                        <b>@user.DisplayName</b> on <b>@_auth.Homeserver</b>
                        @if (_auth.Proxy != null) {
                            <span class="badge badge-info"> (proxied via @_auth.Proxy)</span>
                        }
                    </p>
                    <p>Member of @user.RoomCount rooms</p>

                    <p>Not proxied</p>
                </td>
                <td>
                    <p>
                        <LinkButton href="">Manage</LinkButton>
                        <LinkButton OnClick="@(() => RemoveUser(_auth))">Remove</LinkButton>
                        <LinkButton OnClick="@(() => RemoveUser(_auth, true))">Log out</LinkButton>
                    </p>
                </td>
                @* </div> *@
            </tr>
        }
    </table>
</form>

@code
{
    private Dictionary<UserAuth, UserInfo> _users = new();

    protected override async Task OnInitializedAsync() {
        _currentSession = await MRUStorage.GetCurrentToken();
        _users.Clear();
        var tokens = await MRUStorage.GetAllTokens();
        var profileTasks = tokens.Select(async token => {
            UserInfo userInfo = new();
            AuthenticatedHomeserverGeneric hs;
            try {
                hs = await hsProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken);
            }
            catch (MatrixException e) {
                if (e.ErrorCode == "M_UNKNOWN_TOKEN") {
                    NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken);
                    return;
                }
                throw;
            }
            var roomCountTask = hs.GetJoinedRooms();
            var profile = await hs.GetProfileAsync(hs.WhoAmI.UserId);
            userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId;
            Console.WriteLine(profile.ToJson());
            userInfo.AvatarUrl = await hsResolver.ResolveMediaUri(hs.FullHomeServerDomain,
                profile.AvatarUrl
                ?? "https://api.dicebear.com/6.x/identicon/svg?seed=" + hs.WhoAmI.UserId
                );
            userInfo.RoomCount = (await roomCountTask).Count;
            _users.Add(token, userInfo);
    // StateHasChanged();
        });
        await Task.WhenAll(profileTasks);
        await base.OnInitializedAsync();
    }

    private class UserInfo {
        internal string AvatarUrl { get; set; }
        internal string DisplayName { get; set; }
        internal int RoomCount { get; set; }
    }

    private async Task RemoveUser(UserAuth auth, bool logout = false) {
        try {
            if (logout) {
                await (await hsProvider.GetAuthenticatedWithToken(auth.Homeserver, auth.AccessToken)).Logout();
            }
        }
        catch (Exception e) {
            if(e is MatrixException {ErrorCode: "M_UNKNOWN_TOKEN" }) {
                //todo: handle this
                return;
            }
            Console.WriteLine(e);
        }
        await MRUStorage.RemoveToken(auth);
        if ((await MRUStorage.GetCurrentToken())?.AccessToken == auth.AccessToken)
            await MRUStorage.SetCurrentToken((await MRUStorage.GetAllTokens() ?? throw new InvalidOperationException()).FirstOrDefault());
        await OnInitializedAsync();
    }

    private LoginResponse _currentSession;

    private async Task SwitchSession(UserAuth auth) {
        Console.WriteLine($"Switching to {auth.Homeserver} {auth.UserId} via {auth.Proxy}");
        await MRUStorage.SetCurrentToken(auth);
        await OnInitializedAsync();
    }
}