about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools/InviteCounter.razor
blob: 8f4b4ddb49503c85fc16d1d5250154fa292217e6 (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
@page "/Tools/InviteCounter"
@using ArcaneLibs.Extensions
@using LibMatrix.RoomTypes
@using System.Collections.ObjectModel
@using LibMatrix
@using System.Collections.Frozen
@using LibMatrix.EventTypes.Spec.State
@using MatrixUtils.Abstractions
<h3>User Trace</h3>
<hr/>

<br/>
<span>Room ID: </span>
<InputText @bind-Value="@roomId"></InputText>
<LinkButton OnClick="@Execute">Execute</LinkButton>

<br/>

<details>
    <summary>Results</summary>
    @foreach (var (userId, events) in invites.OrderByDescending(x=>x.Value).ToList()) {
        <p>@userId: @events</p>
    }
</details>

<br/>
@foreach (var line in log.Reverse()) {
    <pre>@line</pre>
}

@code {
    private ObservableCollection<string> log { get; set; } = new();
    private Dictionary<string, int> invites { get; set; } = new();
    private AuthenticatedHomeserverGeneric hs { get; set; }
    
    [Parameter, SupplyParameterFromQuery(Name = "room")]
    public string roomId { get; set; }
    

    protected override async Task OnInitializedAsync() {
        log.CollectionChanged += (sender, args) => StateHasChanged();
        hs = await RMUStorage.GetCurrentSessionOrNavigate();
        if (hs is null) return;
       
        StateHasChanged();
        Console.WriteLine("Rerendered!");
        await base.OnInitializedAsync();
    }

    private async Task<string> Execute() {
        var room = hs.GetRoom(roomId);
        var events = room.GetManyMessagesAsync(limit: int.MaxValue);
        await foreach (var resp in events) {
            var all = resp.State.Concat(resp.Chunk);
            foreach (var evt in all) {
                if(evt.Type != RoomMemberEventContent.EventId) continue;
                var content = evt.TypedContent as RoomMemberEventContent;
                if(content.Membership != "invite") continue;
                if(!invites.ContainsKey(evt.Sender)) invites[evt.Sender] = 0;
                invites[evt.Sender]++;
            }

            log.Add($"{resp.State.Count} state, {resp.Chunk.Count} timeline");
        }
        
        
        
        StateHasChanged();

        return "";
    }

}