about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-01-31 12:10:03 +0100
committerRory& <root@rory.gay>2024-01-31 12:10:03 +0100
commit83e6d98d2d7586fb518ed1b2097c59ea9b8af223 (patch)
tree995dacaec65725007e6a55c88f597aed1d13145a /MatrixUtils.Web/Pages/Tools
parentRoom list fixes, migration fix, update available handler (diff)
downloadMatrixUtils-83e6d98d2d7586fb518ed1b2097c59ea9b8af223.tar.xz
New tools, fix room list items
Diffstat (limited to 'MatrixUtils.Web/Pages/Tools')
-rw-r--r--MatrixUtils.Web/Pages/Tools/RoomIntersections.razor199
-rw-r--r--MatrixUtils.Web/Pages/Tools/SessionCount.razor155
-rw-r--r--MatrixUtils.Web/Pages/Tools/SpaceDebug.razor22
-rw-r--r--MatrixUtils.Web/Pages/Tools/UserTrace.razor139
-rw-r--r--MatrixUtils.Web/Pages/Tools/ViewAccountData.razor30
5 files changed, 535 insertions, 10 deletions
diff --git a/MatrixUtils.Web/Pages/Tools/RoomIntersections.razor b/MatrixUtils.Web/Pages/Tools/RoomIntersections.razor
new file mode 100644
index 0000000..395f84c
--- /dev/null
+++ b/MatrixUtils.Web/Pages/Tools/RoomIntersections.razor
@@ -0,0 +1,199 @@
+@page "/Tools/RoomIntersections"
+@using ArcaneLibs.Extensions
+@using LibMatrix.RoomTypes
+@using System.Collections.ObjectModel
+@using LibMatrix
+@using System.Collections.Frozen
+@using LibMatrix.EventTypes.Spec.State
+<h3>Room intersections</h3>
+<hr/>
+
+<p>Set A: </p>
+<InputText @bind-Value="@ImportSetASpaceId"></InputText>
+<LinkButton OnClick="@(() => AppendSet(ImportSetASpaceId, RoomsA))">Append Set A</LinkButton>
+
+<p>Set B: </p>
+<InputText @bind-Value="@ImportSetBSpaceId"></InputText>
+<LinkButton OnClick="@(() => AppendSet(ImportSetBSpaceId, RoomsB))">Append Set B</LinkButton>
+<br/>
+<LinkButton OnClick="@Execute">Execute</LinkButton>
+<br/>
+
+<details>
+    <summary>Results</summary>
+    <pre>
+        @{
+            var userColWidth = matches.Count == 0 ? 0 : matches.Keys.Max(x => x.Length);
+        }
+        <table border="1">
+            @foreach (var (userId, sets) in matches) {
+                <tr>
+                    <td>@userId.PadRight(userColWidth + 5)</td>
+                    <td>@sets.Item1[0].Room.RoomId</td>
+                    <td>@((sets.Item1[0].Member.TypedContent as RoomMemberEventContent).Membership)</td>
+                    <td>@(roomNames.ContainsKey(sets.Item1[0].Room) ? roomNames[sets.Item1[0].Room] : "")</td>
+                    <td>@(roomAliasses.ContainsKey(sets.Item1[0].Room) ? roomAliasses[sets.Item1[0].Room] : "")</td>
+                    <td>@sets.Item2[0].Room.RoomId</td>
+                    <td>@((sets.Item2[0].Member.TypedContent as RoomMemberEventContent).Membership)</td>
+                    <td>@(roomNames.ContainsKey(sets.Item2[0].Room) ? roomNames[sets.Item2[0].Room] : "")</td>
+                    <td>@(roomAliasses.ContainsKey(sets.Item2[0].Room) ? roomAliasses[sets.Item2[0].Room] : "")</td>
+                </tr>
+                @for (int i = 1; i < Math.Max(sets.Item1.Count, sets.Item2.Count); i++) {
+                    <tr>
+                        <td/>
+                        @if (sets.Item1.Count > i) {
+                            <td>@sets.Item1[i].Room.RoomId</td>
+                            <td>@((sets.Item1[i].Member.TypedContent as RoomMemberEventContent).Membership)</td>
+                            <td>@(roomNames.ContainsKey(sets.Item1[i].Room) ? roomNames[sets.Item1[i].Room] : "")</td>
+                            <td>@(roomAliasses.ContainsKey(sets.Item1[i].Room) ? roomAliasses[sets.Item1[i].Room] : "")</td>
+                        }
+                        else {
+                            <td/>
+                            <td/>
+                            <td/>
+                            <td/>
+                        }
+                        @if (sets.Item2.Count > i) {
+                            <td>@sets.Item2[0].Room.RoomId</td>
+                            <td>@((sets.Item2[i].Member.TypedContent as RoomMemberEventContent).Membership)</td>
+                            <td>@(roomNames.ContainsKey(sets.Item2[i].Room) ? roomNames[sets.Item2[i].Room] : "")</td>
+                            <td>@(roomAliasses.ContainsKey(sets.Item2[i].Room) ? roomAliasses[sets.Item2[i].Room] : "")</td> 
+                        }
+                        else {
+                            <td/>
+                            <td/>
+                            <td/>
+                            <td/>
+                        }
+                    </tr>
+                }
+            }
+            
+        </table>
+        <br/>
+    </pre>
+</details>
+
+<br/>
+@foreach (var line in Log.Reverse()) {
+    <pre>@line</pre>
+}
+
+@code {
+    private ObservableCollection<string> Log { get; set; } = new();
+    List<GenericRoom> RoomsA { get; set; } = new();
+    List<GenericRoom> RoomsB { get; set; } = new();
+
+    [Parameter, SupplyParameterFromQuery(Name = "a")]
+    public string ImportSetASpaceId { get; set; } = "";
+
+    [Parameter, SupplyParameterFromQuery(Name = "b")]
+    public string ImportSetBSpaceId { get; set; } = "";
+
+    Dictionary<string, Dictionary<GenericRoom, StateEventResponse>> roomMembers { get; set; } = new();
+
+    Dictionary<string, (List<Match>, List<Match>)> matches { get; set; } = new();
+
+    AuthenticatedHomeserverGeneric hs { get; set; }
+
+    // private string RoomListAString {
+    //     get => string.Join("\n", RoomIdsA);
+    //     set => RoomIdsA = value.Split("\n").Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
+    // }
+    //
+    // private string RoomListBString {
+    //     get => string.Join("\n", RoomIdsB);
+    //     set => RoomIdsB = value.Split("\n").Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
+    // }
+
+    // private List<string> RoomIdsA { get; set; } = new();
+    // private List<string> RoomIdsB { get; set; } = new();
+
+    // room info
+    Dictionary<GenericRoom, string> roomNames { get; set; } = new();
+    Dictionary<GenericRoom, string?> roomAliasses { get; set; } = new();
+
+    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 Execute() {
+        // get all users which are in any room of both sets of rooms, and which rooms
+        var setAusers = new Dictionary<string, List<Match>>();
+        var setBusers = new Dictionary<string, List<Match>>();
+
+        await Task.WhenAll(GetMembers(RoomsA, setAusers), GetMembers(RoomsB, setBusers));
+        
+        Log.Add($"Got {setAusers.Count} users in set A");
+        Log.Add($"Got {setBusers.Count} users in set B");
+        Log.Add("Calculating intersections...");
+
+        // get all users which are in both sets of rooms
+        // var users = setAusers.Keys.Intersect(setBusers.Keys).ToList();
+        // var groups = setAusers.IntersectBy(setBusers, (x,y) => x.Key).ToList();
+        matches = setAusers.Keys.Intersect(setBusers.Keys).Select(x => (x, setAusers[x], setBusers[x])).ToDictionary(x => x.x, x => (x.Item2, x.Item3));
+
+        Log.Add($"Found {matches.Count} users in both sets of rooms");
+        StateHasChanged();
+    }
+
+    public async Task GetMembers(List<GenericRoom> rooms, Dictionary<string, List<Match>> users) {
+        foreach (var room in rooms) {
+            Log.Add($"Getting members for {room.RoomId}");
+            var members = await room.GetMembersListAsync(false);
+            foreach (var member in members) {
+                if (member.RawContent?["membership"]?.ToString() == "ban") continue;
+                if (member.RawContent?["membership"]?.ToString() == "invite") continue;
+                if (!users.ContainsKey(member.StateKey)) users[member.StateKey] = new();
+                users[member.StateKey].Add(new() {
+                    Room = room,
+                    Member = member
+                });
+            }
+        }
+    }
+
+    public async Task AppendSet(string spaceId, List<GenericRoom> rooms) {
+        var space = hs.GetRoom(spaceId).AsSpace;
+        Log.Add($"Found space {spaceId}");
+        var roomIdsEnum = space.GetChildrenAsync(true);
+        List<Task> tasks = new();
+        await foreach (var room in roomIdsEnum) {
+            tasks.Add(loadRoomData(room, rooms));
+        }
+
+        await Task.WhenAll(tasks);
+
+        async Task loadRoomData(GenericRoom room, List<GenericRoom> rooms) {
+            Log.Add($"Found room {room.RoomId}");
+            try {
+                await room.GetPowerLevelsAsync();
+                rooms.Add(room);
+                try {
+                    roomAliasses[room] = (await room.GetCanonicalAliasAsync()).Alias;
+                }
+                catch { }
+
+                try {
+                    roomNames[room] = await room.GetNameOrFallbackAsync();
+                }
+                catch { }
+            }
+            catch (MatrixException e) {
+                Log.Add($"Failed to get power levels for {room.RoomId}: {e.Message}");
+            }
+        }
+    }
+
+    public class Match {
+        public GenericRoom Room { get; set; }
+        public StateEventResponse Member { get; set; }
+    }
+
+}
\ No newline at end of file
diff --git a/MatrixUtils.Web/Pages/Tools/SessionCount.razor b/MatrixUtils.Web/Pages/Tools/SessionCount.razor
new file mode 100644
index 0000000..3b68bfa
--- /dev/null
+++ b/MatrixUtils.Web/Pages/Tools/SessionCount.razor
@@ -0,0 +1,155 @@
+@page "/Tools/SessionCount"
+@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>
+<details>
+    <summary>Results text</summary>
+    @{
+        var col1Width = matches.Keys.Max(x => x.Length);
+    }
+    <pre>
+        @foreach (var (userId, events) in matches) {
+            <p>
+                <span>@userId.PadRight(col1Width)</span>
+                @foreach (var @event in events) {
+    
+}
+            </p>
+        }
+    </pre>
+</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();
+
+        var stateTasks = rooms.Select(async x => (x, await x.GetMembersListAsync(false))).ToAsyncEnumerable();
+
+        await foreach (var (room, state) in stateTasks) {
+            roomMembers.Add(room, state);
+            log.Add($"Got {state.Count} members for {room.RoomId}...");
+        }
+
+        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 
+    }
+
+}
\ No newline at end of file
diff --git a/MatrixUtils.Web/Pages/Tools/SpaceDebug.razor b/MatrixUtils.Web/Pages/Tools/SpaceDebug.razor
index 5d9b8eb..09e5b12 100644
--- a/MatrixUtils.Web/Pages/Tools/SpaceDebug.razor
+++ b/MatrixUtils.Web/Pages/Tools/SpaceDebug.razor
@@ -1,6 +1,7 @@
 @page "/Tools/SpaceDebug"
 @using LibMatrix.Filters
 @using LibMatrix.Helpers
+@using LibMatrix.Utilities
 <h3>SpaceDebug</h3>
 <hr/>
 
@@ -49,16 +50,17 @@
         if (hs is null) return;
 
         var syncHelper = new SyncHelper(hs) {
-            Filter = new SyncFilter() {
-                Presence = new(0),
-                Room = new() {
-                    AccountData = new(limit: 0),
-                    Ephemeral = new(limit: 0),
-                    State = new(limit: 1000, types: new() { "m.space.child", "m.space.parent" }),
-                    Timeline = new(limit: 0)
-                },
-                AccountData = new(limit: 0)
-            }
+            // Filter = new SyncFilter() {
+                // Presence = new(0),
+                // Room = new() {
+                    // AccountData = new(limit: 0),
+                    // Ephemeral = new(limit: 0),
+                    // State = new(limit: 1000, types: new() { "m.space.child", "m.space.parent" }),
+                    // Timeline = new(limit: 0)
+                // },
+                // AccountData = new(limit: 0)
+            // }
+            NamedFilterName = CommonSyncFilters.GetSpaceRelations
         };
         
         Status = "Syncing...";
diff --git a/MatrixUtils.Web/Pages/Tools/UserTrace.razor b/MatrixUtils.Web/Pages/Tools/UserTrace.razor
new file mode 100644
index 0000000..b3a7487
--- /dev/null
+++ b/MatrixUtils.Web/Pages/Tools/UserTrace.razor
@@ -0,0 +1,139 @@
+@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();
+
+        var stateTasks = rooms.Select(async x => (x, await x.GetMembersListAsync(false))).ToAsyncEnumerable();
+
+        await foreach (var (room, state) in stateTasks) {
+            roomMembers.Add(room, state);
+            log.Add($"Got {state.Count} members for {room.RoomId}...");
+        }
+
+        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 
+    }
+
+}
\ No newline at end of file
diff --git a/MatrixUtils.Web/Pages/Tools/ViewAccountData.razor b/MatrixUtils.Web/Pages/Tools/ViewAccountData.razor
new file mode 100644
index 0000000..398c7ce
--- /dev/null
+++ b/MatrixUtils.Web/Pages/Tools/ViewAccountData.razor
@@ -0,0 +1,30 @@
+@page "/Tools/ViewAccountData"
+@using ArcaneLibs.Extensions
+@using LibMatrix
+@using LibMatrix.Filters
+@using LibMatrix.Helpers
+@using LibMatrix.Utilities
+<h3>View account data</h3>
+<hr/>
+<pre>@globalAccountData?.Events.ToJson(ignoreNull: true)</pre>
+<br/>
+
+@foreach (var (key, value) in perRoomAccountData) {
+    <u>@key</u><br/><hr/>
+    <pre>@value?.Events.ToJson(ignoreNull: true)</pre>
+}
+
+@code {
+    EventList? globalAccountData;
+    Dictionary<string, EventList?> perRoomAccountData = new();
+
+    protected override async Task OnInitializedAsync() {
+        var hs = await RMUStorage.GetCurrentSessionOrNavigate();
+        if (hs is null) return;
+        perRoomAccountData = await hs.EnumerateAccountDataPerRoom();
+        globalAccountData = await hs.EnumerateAccountData();
+
+        StateHasChanged();
+    }
+
+}
\ No newline at end of file