about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools/CopyPowerlevel.razor
blob: 667b51868647c77329239b9d15952ebf6bdd710c (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
@page "/Tools/CopyPowerlevel"
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.RoomTypes
<h3>Copy powerlevel</h3>
<hr/>

<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();

    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 = rooms.Select(x=>Execute(hs, x)).ToAsyncEnumerable();
            await foreach (var a in tasks) {
                if (!string.IsNullOrWhiteSpace(a)) {
                    log.Add(a);
                    StateHasChanged();
                }
            }
        }
    }

    private async Task<string> Execute(AuthenticatedHomeserverGeneric hs, GenericRoom room) {
        try {
            var pls = await room.GetPowerLevelsAsync();
            // if (pls.GetUserPowerLevel(hs.WhoAmI.UserId) == pls.UsersDefault) return "I am default PL in " + room.RoomId;
            if (!pls.UserHasStatePermission(hs.WhoAmI.UserId, RoomPowerLevelEventContent.EventId)) return "I do not have permission to send PL in " + room.RoomId;
            foreach (var ahs in hss) {
                if (pls.GetUserPowerLevel(hs.WhoAmI.UserId) == pls.GetUserPowerLevel(ahs.WhoAmI.UserId)) {
                    log.Add("I am same PL in " + room.RoomId);
                    continue;
                }
                
                pls.SetUserPowerLevel(ahs.WhoAmI.UserId, pls.GetUserPowerLevel(hs.WhoAmI.UserId));
                await room.SendStateEventAsync(RoomPowerLevelEventContent.EventId, pls);
                log.Add($"Updated powerlevel of {room.RoomId} to {pls.GetUserPowerLevel(ahs.WhoAmI.UserId)}");
            }

        }
        catch (MatrixException e) {
            return $"Failed to update PLs in {room.RoomId}: {e.Message}";
        }
        catch (Exception e) {
            return $"Failed to update PLs in {room.RoomId}: {e.Message}";
        }
        StateHasChanged();
        return "";
    }

}