diff options
author | Rory& <root@rory.gay> | 2024-01-24 02:31:56 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-24 17:05:25 +0100 |
commit | 03313562d21d5db9bf6a14ebbeab80e06c883d3a (patch) | |
tree | e000546a2ee8e6a886a7ed9fd01ad674178fb7cb /MatrixRoomUtils.Web/Pages/Tools | |
parent | Make RMU installable (diff) | |
download | MatrixUtils-03313562d21d5db9bf6a14ebbeab80e06c883d3a.tar.xz |
MRU->RMU, fixes, cleanup
Diffstat (limited to 'MatrixRoomUtils.Web/Pages/Tools')
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Tools/CopyPowerlevel.razor | 84 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Tools/KnownHomeserverList.razor | 54 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Tools/MassJoinRoom.razor | 110 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Tools/MediaLocator.razor | 111 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Tools/SpaceDebug.razor | 113 |
5 files changed, 0 insertions, 472 deletions
diff --git a/MatrixRoomUtils.Web/Pages/Tools/CopyPowerlevel.razor b/MatrixRoomUtils.Web/Pages/Tools/CopyPowerlevel.razor deleted file mode 100644 index bcbc974..0000000 --- a/MatrixRoomUtils.Web/Pages/Tools/CopyPowerlevel.razor +++ /dev/null @@ -1,84 +0,0 @@ -@page "/CopyPowerlevel" -@using System.Diagnostics -@using ArcaneLibs.Extensions -@using LibMatrix -@using LibMatrix.EventTypes.Spec.State -@using LibMatrix.Homeservers -@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 MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - var sessions = await MRUStorage.GetAllTokens(); - foreach (var userAuth in sessions) { - var session = await MRUStorage.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 ""; - } - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/Tools/KnownHomeserverList.razor b/MatrixRoomUtils.Web/Pages/Tools/KnownHomeserverList.razor deleted file mode 100644 index dbf2f5f..0000000 --- a/MatrixRoomUtils.Web/Pages/Tools/KnownHomeserverList.razor +++ /dev/null @@ -1,54 +0,0 @@ -@page "/KnownHomeserverList" -@using System.Diagnostics -@using ArcaneLibs.Extensions -@using LibMatrix.Homeservers -@using LibMatrix.RoomTypes -<h3>Known Homeserver List</h3> -<hr/> - -@if (!IsFinished) { - <p> - <b>Loading...</b> - </p> -} - -@foreach (var (homeserver, members) in counts.OrderByDescending(x => x.Value)) { - <p>@homeserver - @members</p> -} -<hr/> - -@code { - Dictionary<string, List<string>> homeservers { get; set; } = new(); - Dictionary<string, int> counts { get; set; } = new(); - // List<HomeserverInfo> Homeservers = new(); - bool IsFinished { get; set; } - // HomeserverInfoQueryProgress QueryProgress { get; set; } = new(); - AuthenticatedHomeserverGeneric? hs { get; set; } - - protected override async Task OnInitializedAsync() { - hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - var fetchTasks = (await hs.GetJoinedRooms()).Select(x=>x.GetMembersByHomeserverAsync()).ToAsyncEnumerable(); - await foreach (var result in fetchTasks) { - foreach (var (resHomeserver, resMembers) in result) { - if (!homeservers.TryAdd(resHomeserver, resMembers)) { - homeservers[resHomeserver].AddRange(resMembers); - } - counts[resHomeserver] = homeservers[resHomeserver].Count; - } - // StateHasChanged(); - // await Task.Delay(250); - } - - foreach (var resHomeserver in homeservers.Keys) { - homeservers[resHomeserver] = homeservers[resHomeserver].Distinct().ToList(); - counts[resHomeserver] = homeservers[resHomeserver].Count; - } - - IsFinished = true; - StateHasChanged(); - Console.WriteLine("Rerendered!"); - await base.OnInitializedAsync(); - } - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/Tools/MassJoinRoom.razor b/MatrixRoomUtils.Web/Pages/Tools/MassJoinRoom.razor deleted file mode 100644 index 816b7db..0000000 --- a/MatrixRoomUtils.Web/Pages/Tools/MassJoinRoom.razor +++ /dev/null @@ -1,110 +0,0 @@ -@page "/MassRoomJoin" -@using System.Diagnostics -@using ArcaneLibs.Extensions -@using LibMatrix -@using LibMatrix.EventTypes.Spec.State -@using LibMatrix.Homeservers -@using LibMatrix.RoomTypes -<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 MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - var sessions = await MRUStorage.GetAllTokens(); - foreach (var userAuth in sessions) { - var session = await MRUStorage.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 ""; - } - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/Tools/MediaLocator.razor b/MatrixRoomUtils.Web/Pages/Tools/MediaLocator.razor deleted file mode 100644 index 20aa639..0000000 --- a/MatrixRoomUtils.Web/Pages/Tools/MediaLocator.razor +++ /dev/null @@ -1,111 +0,0 @@ -@page "/MediaLocator" -@using LibMatrix.Homeservers -@inject HttpClient Http -<h3>Media locator</h3> -<hr/> - -<b>This is going to expose your IP address to all these homeservers!</b> -<details> - <summary>Checked homeserver list (@homeservers.Count entries)</summary> - <ul> - @foreach (var hs in homeservers) { - <li>@hs</li> - } - </ul> -</details> -<button @onclick="addMoreHomeservers">Add more homeservers</button> -<br/> -<span>MXC URL: </span> -<input type="text" @bind="mxcUrl"/> -<button @onclick="executeSearch">Search</button> - -@if (successResults.Count > 0) { - <h4>Successes</h4> - <ul> - @foreach (var result in successResults) { - <li>@result</li> - } - </ul> -} - -@if (errorResults.Count > 0) { - <h4>Errors</h4> - <ul> - @foreach (var result in errorResults) { - <li>@result</li> - } - </ul> -} - - -@code { - string mxcUrl { get; set; } - readonly List<string> successResults = new(); - readonly List<string> errorResults = new(); - readonly List<string> homeservers = new(); - - protected override async Task OnInitializedAsync() { - await base.OnInitializedAsync(); - homeservers.AddRange(new[] { - "matrix.org", - "feline.support", - "rory.gay", - "the-apothecary.club", - "envs.net", - "projectsegfau.lt" - }); - } - - Task executeSearch() { - var sem = new SemaphoreSlim(128, 128); - homeservers.ForEach(async hs => { - await sem.WaitAsync(); - var httpClient = new HttpClient { BaseAddress = new Uri(hs) }; - httpClient.Timeout = TimeSpan.FromSeconds(5); - var rmu = mxcUrl.Replace("mxc://", $"{hs}/_matrix/media/v3/download/"); - try { - var res = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, rmu)); - if (res.IsSuccessStatusCode) { - successResults.Add($"{hs}: found - {res.Content.Headers.ContentLength} bytes"); - StateHasChanged(); - return; - } - errorResults.Add($"Error: {hs} - {res.StatusCode}\n" + await res.Content.ReadAsStringAsync()); - } - catch (Exception e) { - errorResults.Add($"Error: {e}"); - } - finally { - sem.Release(); - } - StateHasChanged(); - }); - return Task.CompletedTask; - } - - async Task addMoreHomeservers() { - var res = await Http.GetAsync("/homeservers.txt"); - var content = await res.Content.ReadAsStringAsync(); - homeservers.Clear(); - var lines = content.Split("\n"); - - var rhs = new RemoteHomeserver("rory.gay"); - var sem = new SemaphoreSlim(128, 128); - lines.ToList().ForEach(async line => { - await sem.WaitAsync(); - try { - homeservers.Add((await hsResolver.ResolveHomeserverFromWellKnown(line)).Client); - StateHasChanged(); - } - catch (Exception e) { - Console.WriteLine(e); - } - finally { - sem.Release(); - } - }); - - StateHasChanged(); - } - -} diff --git a/MatrixRoomUtils.Web/Pages/Tools/SpaceDebug.razor b/MatrixRoomUtils.Web/Pages/Tools/SpaceDebug.razor deleted file mode 100644 index 5b4815d..0000000 --- a/MatrixRoomUtils.Web/Pages/Tools/SpaceDebug.razor +++ /dev/null @@ -1,113 +0,0 @@ -@page "/SpaceDebug" -@using LibMatrix.Filters -@using LibMatrix.Helpers -<h3>SpaceDebug</h3> -<hr/> - -<p>@Status</p> - -<b>Has parent:</b> -<br/> - -@foreach (var (roomId, parents) in SpaceParents) { - <p>@roomId's parents</p> - <ul> - @foreach (var parent in parents) { - <li>@parent</li> - } - </ul> -} - -<b>Space children:</b> - -@foreach (var (roomId, children) in SpaceChildren) { - <p>@roomId's children</p> - <ul> - @foreach (var child in children) { - <li>@child</li> - } - </ul> -} - -@code { - private string _status = "Loading..."; - - public string Status { - get => _status; - set { - _status = value; - StateHasChanged(); - } - } - - public Dictionary<string, List<string>> SpaceChildren { get; set; } = new(); - public Dictionary<string, List<string>> SpaceParents { get; set; } = new(); - - protected override async Task OnInitializedAsync() { - Status = "Getting homeserver..."; - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - 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) - } - }; - - Status = "Syncing..."; - - var syncs = syncHelper.EnumerateSyncAsync(); - await foreach (var sync in syncs) { - if (sync is null) { - Status = "Sync failed"; - continue; - } - - if (sync.Rooms is null) { - Status = "No rooms in sync..."; - break; - } - - if (sync.Rooms.Join is null) { - Status = "No joined rooms in sync..."; - break; - } - - if (sync.Rooms.Join.Count == 0) { - Status = "Joined rooms list was empty..."; - break; - } - - // nextBatch = sync.NextBatch; - foreach (var (roomId, data) in sync.Rooms!.Join!) { - data.State?.Events?.ForEach(e => { - if (e.Type == "m.space.child") { - if (!SpaceChildren.ContainsKey(roomId)) SpaceChildren[roomId] = new(); - if (e.RawContent is null) e.StateKey += " (null)"; - else if (e.RawContent.Count == 0) e.StateKey += " (empty)"; - SpaceChildren[roomId].Add(e.StateKey); - } - if (e.Type == "m.space.parent") { - if (!SpaceParents.ContainsKey(roomId)) SpaceParents[roomId] = new(); - if (e.RawContent is null) e.StateKey += " (null)"; - else if (e.RawContent.Count == 0) e.StateKey += " (empty)"; - SpaceParents[roomId].Add(e.StateKey); - } - }); - } - Status = $"Synced {sync.Rooms.Join.Count} rooms, found {SpaceChildren.Count} spaces, {SpaceParents.Count} parents"; - } - Status = $"Synced: found {SpaceChildren.Count}->{SpaceChildren.Sum(x => x.Value.Count)} spaces, {SpaceParents.Count}->{SpaceParents.Sum(x => x.Value.Count)} parents!"; - - await base.OnInitializedAsync(); - } - - -} |