about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools/MassJoinRoom.razor
blob: a2ad38811fd26bc84cd723c63a4f6a2bb8421504 (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
@page "/Tools/MassRoomJoin"
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
<h3>Mass join room</h3>
<hr/>
<p>Room: </p>
<FancyTextBox @bind-Value="@roomId"></FancyTextBox>

<p>Users: </p>
@foreach (var hs in hss) {
    <p>@hs.WhoAmI.UserId</p>
}

<br/>
<LinkButton OnClick="Execute">Execute</LinkButton>
<br/>
@foreach (var line in Enumerable.Reverse(log)) {
    <p>@line</p>
}

@code {
    private List<string> log { get; set; } = new();
    List<AuthenticatedHomeserverGeneric> hss { get; set; } = new();
    string roomId { get; set; }

    protected override async Task OnInitializedAsync() {
        var hs = await RMUStorage.GetCurrentSessionOrNavigate();
        if (hs is null) return;
        var sessions = await RMUStorage.GetAllTokens();
        foreach (var userAuth in sessions) {
            var session = await RMUStorage.GetSession(userAuth);
            if (session is not null) {
                hss.Add(session);
                StateHasChanged();
            }
        }

        StateHasChanged();
        Console.WriteLine("Rerendered!");
        await base.OnInitializedAsync();
    }

    private async Task Execute() {
    // foreach (var hs in hss) {
    // var rooms = await hs.GetJoinedRooms();
        var tasks = hss.Select(ExecuteInvite).ToAsyncEnumerable();
        await foreach (var a in tasks) {
            if (!string.IsNullOrWhiteSpace(a)) {
                log.Add(a);
                StateHasChanged();
            }
        }
        tasks = hss.Select(ExecuteJoin).ToAsyncEnumerable();
        await foreach (var a in tasks) {
            if (!string.IsNullOrWhiteSpace(a)) {
                log.Add(a);
                StateHasChanged();
            }
        }
    // }
    }

    private async Task<string> ExecuteInvite(AuthenticatedHomeserverGeneric hs) {
        var room = hs.GetRoom(roomId);
        try {
            try {
                var joinRule = await room.GetJoinRuleAsync();
                if (joinRule.JoinRule == RoomJoinRulesEventContent.JoinRules.Public) return "Room is public, no invite needed";
            }
            catch { }
            var pls = await room.GetPowerLevelsAsync();
            if (pls.GetUserPowerLevel(hs.WhoAmI.UserId) < pls.Invite) return "I do not have permission to send invite in " + room.RoomId;
            await room.InviteUsersAsync(hss.Select(x => x.WhoAmI.UserId).ToList());
            log.Add($"Invited to {room.RoomId} to {pls.GetUserPowerLevel(hs.WhoAmI.UserId)}");
        }
        catch (MatrixException e) {
            return $"Failed to invite in {room.RoomId}: {e.Message}";
        }
        catch (Exception e) {
            return $"Failed to invite in {room.RoomId}: {e.Message}";
        }
        StateHasChanged();
        return "";
    }

    private async Task<string> ExecuteJoin(AuthenticatedHomeserverGeneric hs) {
        var room = hs.GetRoom(roomId);
        try {
            try {
                var mse = await room.GetStateOrNullAsync<RoomMemberEventContent>(RoomMemberEventContent.EventId, hs.WhoAmI.UserId);
                if (mse?.Membership == "join") return $"User {hs.WhoAmI.UserId} already in room";
            }
            catch { }
            await room.JoinAsync();
        }
        catch (MatrixException e) {
            return $"Failed to join {hs.WhoAmI.UserId} to {room.RoomId}: {e.Message}";
        }
        catch (Exception e) {
            return $"Failed to join {hs.WhoAmI.UserId} to {room.RoomId}: {e.Message}";
        }
        StateHasChanged();
        return "";
    }

}