From ccbd813dad73b43da1944ce448c8fafe004bae51 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 26 Jan 2024 05:05:33 +0000 Subject: Room member migrations --- MatrixUtils.Web/Pages/Tools/MigrateRoom.razor | 104 ++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 MatrixUtils.Web/Pages/Tools/MigrateRoom.razor (limited to 'MatrixUtils.Web/Pages/Tools/MigrateRoom.razor') diff --git a/MatrixUtils.Web/Pages/Tools/MigrateRoom.razor b/MatrixUtils.Web/Pages/Tools/MigrateRoom.razor new file mode 100644 index 0000000..3234098 --- /dev/null +++ b/MatrixUtils.Web/Pages/Tools/MigrateRoom.razor @@ -0,0 +1,104 @@ +@page "/Tools/MigrateRoom" +@using ArcaneLibs.Extensions +@using LibMatrix +@using LibMatrix.EventTypes.Spec.State +@using LibMatrix.RoomTypes +

Migrate room

+
+Old room: +
+New room: +
+ +
+ Users: + @foreach (var user in users) { +

@user

+ } +
+ +
+Execute +
+@foreach (var line in Enumerable.Reverse(log)) { +

@line

+} + +@code { + private string _roomId; + private List log { get; set; } = new(); + private List users { get; set; } = new(); + + string roomId { + get => _roomId; + set { + _roomId = value; + TryFetchUsers(); + } + } + + private string newRoomId { get; set; } + + protected override async Task OnInitializedAsync() { + var hs = await RMUStorage.GetCurrentSessionOrNavigate(); + if (hs is null) return; + + StateHasChanged(); + Console.WriteLine("Rerendered!"); + await base.OnInitializedAsync(); + } + + private async Task Execute() { + var hs = await RMUStorage.GetCurrentSessionOrNavigate(); + if (hs is null) return; + var oldRoom = hs.GetRoom(roomId); + var newRoom = hs.GetRoom(newRoomId); + var members = await oldRoom.GetMembersListAsync(); + var tasks = members.Select(x => ExecuteInvite(hs, newRoom, x.StateKey)).ToAsyncEnumerable(); + // var tasks = hss.Select(ExecuteInvite).ToAsyncEnumerable(); + await foreach (var a in tasks) { + if (!string.IsNullOrWhiteSpace(a)) { + log.Add(a); + StateHasChanged(); + } + } + } + + private async Task ExecuteInvite(AuthenticatedHomeserverGeneric hs, GenericRoom newRoom, string mxid) { + try { + var pls = await newRoom.GetPowerLevelsAsync(); + if (pls.GetUserPowerLevel(hs.WhoAmI.UserId) < pls.Invite) return "I do not have permission to send invite in " + newRoom.RoomId; + await newRoom.InviteUserAsync(mxid); + return $"Invited {mxid} to {newRoom.RoomId}"; + } + catch (MatrixException e) { + log.Add($"Failed to invite {mxid} to {newRoom.RoomId}: {e.Message}"); + if (e is { ErrorCode: "M_LIMIT_EXCEEDED" }) { + log.Add($"Retrying after {e.RetryAfterMs}"); + await Task.Delay(e.RetryAfterMs!.Value); + return await ExecuteInvite(hs, newRoom, mxid); + } + + return ""; + } + catch (Exception e) { + return $"Failed to invite {mxid} to {newRoom.RoomId}: {e.Message}"; + } + + StateHasChanged(); + return ""; + } + + private async Task TryFetchUsers() { + try { + var hs = await RMUStorage.GetCurrentSessionOrNavigate(); + if (hs is null) return; + var room = hs.GetRoom(roomId); + var members = await room.GetMembersListAsync(); + users = members.Select(x => x.StateKey).ToList(); + StateHasChanged(); + } + catch { } + } + +} \ No newline at end of file -- cgit 1.4.1