diff options
Diffstat (limited to 'MatrixRoomUtils.Web/Pages/Rooms/Index.razor')
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Rooms/Index.razor | 102 |
1 files changed, 61 insertions, 41 deletions
diff --git a/MatrixRoomUtils.Web/Pages/Rooms/Index.razor b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor index 2ac4bcb..6cabe82 100644 --- a/MatrixRoomUtils.Web/Pages/Rooms/Index.razor +++ b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor @@ -1,25 +1,21 @@ @page "/Rooms" @using LibMatrix.Filters @using LibMatrix.Helpers -@using LibMatrix.EventTypes.Spec.State -@using LibMatrix -@using LibMatrix.Homeservers -@using ArcaneLibs.Extensions @using LibMatrix.Extensions @using LibMatrix.Responses @using System.Collections.ObjectModel @using System.Diagnostics +@using ArcaneLibs.Extensions +@using MatrixRoomUtils.Abstractions @inject ILogger<Index> logger <h3>Room list</h3> <p>@Status</p> <p>@Status2</p> -@* @if (RenderContents) { *@ + +<LinkButton href="/Rooms/Create">Create new room</LinkButton> + <RoomList Rooms="Rooms" GlobalProfile="@GlobalProfile" @bind-StillFetching="RenderContents"></RoomList> -@* } *@ -@* else { *@ -@* <RoomList Rooms="Rooms" GlobalProfile="@GlobalProfile" StillFetching="true"></RoomList> *@ -@* } *@ @code { private ObservableCollection<RoomInfo> Rooms { get; } = new(); @@ -47,9 +43,9 @@ }, State = new SyncFilter.RoomFilter.StateFilter { Types = new List<string> { + "m.room.create", "m.room.name", "m.room.avatar", - "m.room.create", "org.matrix.mjolnir.shortcode", "m.room.power_levels", } @@ -92,49 +88,72 @@ // } // }; + private SyncHelper syncHelper; + + // SyncHelper profileSyncHelper; + protected override async Task OnInitializedAsync() { Homeserver = await MRUStorage.GetCurrentSessionOrNavigate(); if (Homeserver is null) return; var rooms = await Homeserver.GetJoinedRooms(); - foreach (var room in rooms) { - Rooms.Add(new(){Room = room}); + // SemaphoreSlim _semaphore = new(160, 160); + + var roomTasks = rooms.Select(async room => { + RoomInfo ri; + // await _semaphore.WaitAsync(); + ri = new() { Room = room }; + await Task.WhenAll((filter.Room?.State?.Types ?? []).Select(x => ri.GetStateEvent(x))); + return ri; + }).ToAsyncEnumerable(); + + await foreach (var room in roomTasks) { + Rooms.Add(room); + StateHasChanged(); + // await Task.Delay(50); + // _semaphore.Release(); } - - GlobalProfile = await Homeserver.GetProfileAsync(Homeserver.WhoAmI.UserId); - var syncHelper = new SyncHelper(Homeserver, logger) { - Timeout = 10000, + if (rooms.Count >= 150) RenderContents = true; + + GlobalProfile = await Homeserver.GetProfileAsync(Homeserver.WhoAmI.UserId); + syncHelper = new SyncHelper(Homeserver, logger) { + Timeout = 30000, Filter = filter, MinimumDelay = TimeSpan.FromMilliseconds(5000) }; - // profileUpdateFilter.Room.State.Senders.Add(Homeserver.WhoAmI.UserId); - // var profileSyncHelper = new SyncHelper(Homeserver, logger) { + // profileSyncHelper = new SyncHelper(Homeserver, logger) { // Timeout = 10000, // Filter = profileUpdateFilter, // MinimumDelay = TimeSpan.FromMilliseconds(5000) - // }; + // }; + // profileUpdateFilter.Room.State.Senders.Add(Homeserver.WhoAmI.UserId); + RunSyncLoop(syncHelper); // RunSyncLoop(profileSyncHelper); RunQueueProcessor(); + await base.OnInitializedAsync(); } - + private async Task RunQueueProcessor() { var renderTimeSw = Stopwatch.StartNew(); + var isInitialSync = true; while (true) { try { - if (queue.Count == 0) { - while (queue.Count == 0) { - Console.WriteLine("Queue is empty, waiting..."); - await Task.Delay(2500); - } - Console.WriteLine("Queue no longer empty!"); + while (queue.Count == 0) { + Console.WriteLine("Queue is empty, waiting..."); + await Task.Delay(isInitialSync ? 100 : 2500); } - while (queue.TryDequeue(out var queueEntry)) { + + Console.WriteLine($"Queue no longer empty after {renderTimeSw.Elapsed}!"); + + int maxUpdates = 10; + isInitialSync = false; + while (maxUpdates-- > 0 && queue.TryDequeue(out var queueEntry)) { var (roomId, roomData) = queueEntry; Console.WriteLine($"Dequeued room {roomId}"); RoomInfo room; - + if (Rooms.Any(x => x.Room.RoomId == roomId)) { room = Rooms.First(x => x.Room.RoomId == roomId); Console.WriteLine($"QueueWorker: {roomId} already known with {room.StateEvents?.Count ?? 0} state events"); @@ -146,26 +165,23 @@ }; Rooms.Add(room); } - + if (room.StateEvents is null) { Console.WriteLine($"QueueWorker: {roomId} does not have state events on record?"); throw new InvalidDataException("Somehow this is null???"); } - if (roomData.State?.Events is {Count: >0 }) + + if (roomData.State?.Events is { Count: > 0 }) room.StateEvents.MergeStateEventLists(roomData.State.Events); else { Console.WriteLine($"QueueWorker: could not merge state for {room.Room.RoomId} as new data contains no state events!"); } - if (Random.Shared.Next(101) < 20 || true) { - Status = $"Got {Rooms.Count} rooms so far! {queue.Count} entries in processing queue..."; - } - RenderContents |= queue.Count == 0; - if (queue.Count > 10) RenderContents = false; - await Task.Delay(RenderContents ? 25 : 6); } - // else { - // Console.WriteLine("Failed to dequeue item"); - // } + Console.WriteLine($"QueueWorker: {queue.Count} entries left in queue, {maxUpdates} maxUpdates left, RenderContents: {RenderContents}"); + Status = $"Got {Rooms.Count} rooms so far! {queue.Count} entries in processing queue..."; + + RenderContents |= queue.Count == 0; + await Task.Delay(Rooms.Count); } catch (Exception e) { Console.WriteLine("QueueWorker exception: " + e); @@ -214,11 +230,15 @@ // We can't trust servers to give us what we ask for, and this ruins performance // Thanks, Conduit. joinedRoom.Value.State.Events.RemoveAll(x => filter.Room?.State?.Types?.Contains(x.Type) == false); - if(filter.Room?.State?.NotSenders?.Any() ?? false) + if (filter.Room?.State?.NotSenders?.Any() ?? false) joinedRoom.Value.State.Events.RemoveAll(x => filter.Room?.State?.NotSenders?.Contains(x.Sender) ?? false); - + queue.Enqueue(joinedRoom); } + if (sync.Rooms.Leave is {Count: > 0}) + foreach (var leftRoom in sync.Rooms.Leave) + if (Rooms.Any(x => x.Room.RoomId == leftRoom.Key)) + Rooms.Remove(Rooms.First(x => x.Room.RoomId == leftRoom.Key)); Status = $"Got {Rooms.Count} rooms so far! {queue.Count} entries in processing queue... " + $"{sync?.Rooms?.Join?.Count ?? 0} new updates!"; |