From a3a60fda1492c600383e166a20dd5e6f52120616 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 19 Jun 2024 23:32:59 +0200 Subject: Some cleanup, update libs --- MatrixUtils.Web/Pages/Tools/Index.razor | 4 ++ .../Pages/Tools/Info/PolicyListActivity.razor | 2 +- MatrixUtils.Web/Pages/Tools/InviteCounter.razor | 73 +++++++++++++++++++++ MatrixUtils.Web/Pages/Tools/MassCMEBan.razor | 75 ++++++++++++++++++++++ .../Pages/Tools/Room/SpaceRestrictedJoins.razor | 75 ++++++++++++++++++++++ 5 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 MatrixUtils.Web/Pages/Tools/InviteCounter.razor create mode 100644 MatrixUtils.Web/Pages/Tools/MassCMEBan.razor create mode 100644 MatrixUtils.Web/Pages/Tools/Room/SpaceRestrictedJoins.razor (limited to 'MatrixUtils.Web/Pages/Tools') diff --git a/MatrixUtils.Web/Pages/Tools/Index.razor b/MatrixUtils.Web/Pages/Tools/Index.razor index 3aec2e3..e68bb9a 100644 --- a/MatrixUtils.Web/Pages/Tools/Index.razor +++ b/MatrixUtils.Web/Pages/Tools/Index.razor @@ -13,6 +13,10 @@ Copy highest powerlevel across all session
View account data
+

Room tools

+
+Change space children join access rules
+

Moderation tools


Count invites by inviter
diff --git a/MatrixUtils.Web/Pages/Tools/Info/PolicyListActivity.razor b/MatrixUtils.Web/Pages/Tools/Info/PolicyListActivity.razor index c94d0b0..de0bfe7 100644 --- a/MatrixUtils.Web/Pages/Tools/Info/PolicyListActivity.razor +++ b/MatrixUtils.Web/Pages/Tools/Info/PolicyListActivity.razor @@ -1,4 +1,4 @@ -@page "/Tools/PolicyListActivity" +@page "/Tools/Info/PolicyListActivity" @using LibMatrix.EventTypes.Spec.State.Policy @using System.Diagnostics @using LibMatrix.RoomTypes diff --git a/MatrixUtils.Web/Pages/Tools/InviteCounter.razor b/MatrixUtils.Web/Pages/Tools/InviteCounter.razor new file mode 100644 index 0000000..8f4b4dd --- /dev/null +++ b/MatrixUtils.Web/Pages/Tools/InviteCounter.razor @@ -0,0 +1,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 +

User Trace

+
+ +
+Room ID: + +Execute + +
+ +
+ Results + @foreach (var (userId, events) in invites.OrderByDescending(x=>x.Value).ToList()) { +

@userId: @events

+ } +
+ +
+@foreach (var line in log.Reverse()) { +
@line
+} + +@code { + private ObservableCollection log { get; set; } = new(); + private Dictionary 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 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 ""; + } + +} \ No newline at end of file diff --git a/MatrixUtils.Web/Pages/Tools/MassCMEBan.razor b/MatrixUtils.Web/Pages/Tools/MassCMEBan.razor new file mode 100644 index 0000000..cbbca9e --- /dev/null +++ b/MatrixUtils.Web/Pages/Tools/MassCMEBan.razor @@ -0,0 +1,75 @@ +@page "/Tools/MassCMEBan" +@using ArcaneLibs.Extensions +@using LibMatrix.RoomTypes +@using System.Collections.ObjectModel +@using LibMatrix +@using System.Collections.Frozen +@using LibMatrix.EventTypes.Spec.State +@using LibMatrix.EventTypes.Spec.State.Policy +@using MatrixUtils.Abstractions +

User Trace

+
+ +
+Users: + +Execute + +
+ +
+@foreach (var line in log.Reverse()) { +
@line
+} + +@code { + // TODO: Properly implement page to be more useful + private ObservableCollection log { 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 Execute() { + var room = hs.GetRoom("!fTjMjIzNKEsFlUIiru:neko.dev"); + // var room = hs.GetRoom("!yf7OpOiRDXx6zUGpT6:conduit.rory.gay"); + var users = roomId.Split("\n").Select(x => x.Trim()).Where(x=>x.StartsWith('@')).ToList(); + foreach (var user in users) { + var exists = false; + try { + exists = !string.IsNullOrWhiteSpace((await room.GetStateAsync(UserPolicyRuleEventContent.EventId, user.Replace('@', '_'))).Entity); + } catch (Exception e) { + log.Add($"Failed to get {user}"); + } + + if (!exists) { + var evt = await room.SendStateEventAsync(UserPolicyRuleEventContent.EventId, user.Replace('@', '_'), new UserPolicyRuleEventContent() { + Entity = user, + Reason = "spam (invite)", + Recommendation = "m.ban" + }); + log.Add($"Sent {evt.EventId} to ban {user}"); + } + else { + log.Add($"User {user} already exists"); + } + } + + + StateHasChanged(); + + return ""; + } + +} \ No newline at end of file diff --git a/MatrixUtils.Web/Pages/Tools/Room/SpaceRestrictedJoins.razor b/MatrixUtils.Web/Pages/Tools/Room/SpaceRestrictedJoins.razor new file mode 100644 index 0000000..80a03f2 --- /dev/null +++ b/MatrixUtils.Web/Pages/Tools/Room/SpaceRestrictedJoins.razor @@ -0,0 +1,75 @@ +@page "/Tools/Room/SpaceRestrictedJoins" +@using System.Collections.ObjectModel +@using LibMatrix.EventTypes.Spec.State +

Allow space to restricted join children

+
+ +

Room ID:

+ +

Change guest access:

+

Change knock access:

+ +
+Execute +
+ +
+@foreach (var line in log.Reverse()) { +
@line
+} + +@code { + private ObservableCollection log { get; set; } = new(); + + private string RoomId { get; set; } + private AuthenticatedHomeserverGeneric hs { get; set; } + + private bool ChangeGuestAccess { get; set; } + private bool GuestAccess { get; set; } + private bool ChangeKnocking { get; set; } + private bool Knocking { 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 Execute() { + var space = hs.GetRoom(RoomId).AsSpace; + await foreach (var room in space.GetChildrenAsync()) { + log.Add($"Got room {room.RoomId}"); + if (ChangeGuestAccess) { + var targetGuestAccess = GuestAccess ? "can_join" : "forbidden"; + var currentGuestAccess = (await room.GetStateAsync(RoomGuestAccessEventContent.EventId))?.GuestAccess; + if (currentGuestAccess != targetGuestAccess) { + log.Add($"Changing guest access from {currentGuestAccess} to {targetGuestAccess}"); + await room.SendStateEventAsync(RoomGuestAccessEventContent.EventId, new RoomGuestAccessEventContent { GuestAccess = targetGuestAccess }); + } + } + + var currentJoinRules = (await room.GetStateOrNullAsync(RoomJoinRulesEventContent.EventId)) ?? new(); + if (ChangeKnocking) { + var targetJoinRule = Knocking ? "knock_restricted" : "restricted"; + if (currentJoinRules.JoinRuleValue != targetJoinRule) { + log.Add($"Changing knocking from {currentJoinRules.JoinRuleValue} to {targetJoinRule}"); + currentJoinRules.JoinRuleValue = targetJoinRule; + await room.SendStateEventAsync(RoomJoinRulesEventContent.EventId, currentJoinRules); + } + } + + if (currentJoinRules.Allow == null || !currentJoinRules.Allow.Any(x => x.Type == "m.room_membership" && x.RoomId == RoomId)) { + log.Add($"Adding {RoomId} to allowed rooms"); + currentJoinRules.Allow ??= new(); + currentJoinRules.Allow.Add(new RoomJoinRulesEventContent.AllowEntry() { Type = "m.room_membership", RoomId = RoomId }); + await room.SendStateEventAsync(RoomJoinRulesEventContent.EventId, currentJoinRules); + } + } + log.Add("Done!"); + } + +} \ No newline at end of file -- cgit 1.4.1