about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools/UserTrace.razor
blob: d78c58a47b45782f1329b39e4073a2adf4e38020 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
@page "/Tools/UserTrace"
@using ArcaneLibs.Extensions
@using LibMatrix.RoomTypes
@using System.Collections.ObjectModel
@using LibMatrix
@using System.Collections.Frozen
@using LibMatrix.EventTypes.Spec.State
<h3>User Trace</h3>
<hr/>

<p>Users: </p>
<InputTextArea @bind-Value="@UserIdString"></InputTextArea>
<br/>
<InputText @bind-Value="@ImportFromRoomId"></InputText><LinkButton OnClick="@DoImportFromRoomId">Import from room (ID)</LinkButton>

<details>
    <summary>Rooms to be searched (@rooms.Count)</summary>
    @foreach (var room in rooms) {
        <span>@room.RoomId</span>
        <br/>
    }
</details>
<br/>
<LinkButton OnClick="Execute">Execute</LinkButton>
<br/>

<details>
    <summary>Results</summary>
    @foreach (var (userId, events) in matches) {
        <h4>@userId</h4>
        <ul>
            @foreach (var eventResponse in events) {
                <li>@eventResponse.Room.RoomId</li>
            }
        </ul>
    }
</details>

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

@code {
    private ObservableCollection<string> log { get; set; } = new();
    List<AuthenticatedHomeserverGeneric> hss { get; set; } = new();
    ObservableCollection<GenericRoom> rooms { get; set; } = new();
    Dictionary<GenericRoom, FrozenSet<StateEventResponse>> roomMembers { get; set; } = new();
    Dictionary<string, List<Matches>> matches = new();

    private string UserIdString {
        get => string.Join("\n", UserIDs);
        set => UserIDs = value.Split("\n").Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
    }

    private List<string> UserIDs { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        log.CollectionChanged += (sender, args) => StateHasChanged();
        var hs = await RMUStorage.GetCurrentSessionOrNavigate();
        if (hs is null) return;
        rooms.CollectionChanged += (sender, args) => StateHasChanged();
        var sessions = await RMUStorage.GetAllTokens();
        foreach (var userAuth in sessions) {
            var session = await RMUStorage.GetSession(userAuth);
            if (session is not null) {
                var sessionRooms = await session.GetJoinedRooms();
                foreach (var room in sessionRooms) {
                    rooms.Add(room);
                }

                StateHasChanged();
                log.Add($"Got {sessionRooms.Count} rooms for {userAuth.UserId}");
            }
        }

        log.Add("Done fetching rooms!");

        var distinctRooms = rooms.DistinctBy(x => x.RoomId).ToArray();
        Random.Shared.Shuffle(distinctRooms);
        rooms = new ObservableCollection<GenericRoom>(distinctRooms);
        rooms.CollectionChanged += (sender, args) => StateHasChanged();
        try {
            var stateTasks = rooms.Select(async x => {
                for (int i = 0; i < 10; i++) {
                    try {
                        return (x, await x.GetMembersListAsync(false));
                    }
                    catch {
                        //
                    }
                }

                return (x, new List<StateEventResponse>().ToFrozenSet());
            }).ToAsyncEnumerable();

            await foreach (var (room, state) in stateTasks) {
                roomMembers.Add(room, state);
                log.Add($"Got {state.Count} members for {room.RoomId}...");
            }
        }
        catch {
            //
        }

        log.Add($"Done fetching members!");

        UserIDs.RemoveAll(x => sessions.Any(y => y.UserId == x));

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

    private async Task<string> Execute() {
        foreach (var userId in UserIDs) {
            matches.Add(userId, new List<Matches>());
            foreach (var (room, events) in roomMembers) {
                if (events.Any(x => x.Type == RoomMemberEventContent.EventId && x.StateKey == userId)) {
                    matches[userId].Add(new() {
                        Event = events.First(x => x.StateKey == userId && x.Type == RoomMemberEventContent.EventId),
                        Room = room,
                    });
                }
            }
        }

        return "";
    }

    public string? ImportFromRoomId { get; set; }

    private async Task DoImportFromRoomId() {
        try {
            if (ImportFromRoomId is null) return;
            var room = rooms.FirstOrDefault(x => x.RoomId == ImportFromRoomId);
            UserIdString = string.Join("\n", (await room.GetMembersListAsync()).Select(x => x.StateKey));
        }
        catch (Exception e) {
            Console.WriteLine(e);
            log.Add("Could not fetch members list!\n" + e.ToString());
        }

        StateHasChanged();
    }

    private class Matches {
        public GenericRoom Room;

        public StateEventResponse Event;
        // public 
    }

}