From def33cc092ae2c6defcc218b108b7c99cbfb8581 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sun, 2 Jul 2023 01:01:09 +0200 Subject: Prefetch room info --- MatrixRoomUtils.Web/Shared/InlineUserItem.razor | 3 +- MatrixRoomUtils.Web/Shared/RoomList.razor | 33 ++++++++++++++-------- .../RoomListComponents/RoomListCategory.razor | 18 +++++++----- .../Shared/RoomListComponents/RoomListSpace.razor | 18 ++++++------ MatrixRoomUtils.Web/Shared/RoomListItem.razor | 32 ++++++++++++++------- .../TimelineComponents/TimelineMemberItem.razor | 1 + MatrixRoomUtils.Web/Shared/UserListItem.razor | 3 +- 7 files changed, 69 insertions(+), 39 deletions(-) (limited to 'MatrixRoomUtils.Web/Shared') diff --git a/MatrixRoomUtils.Web/Shared/InlineUserItem.razor b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor index a498c70..ffccc25 100644 --- a/MatrixRoomUtils.Web/Shared/InlineUserItem.razor +++ b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor @@ -1,6 +1,7 @@ @using MatrixRoomUtils.Core.Responses @using MatrixRoomUtils.Core.StateEventTypes @using MatrixRoomUtils.Core.Helpers +@using MatrixRoomUtils.Core.StateEventTypes.Spec
@ProfileName @@ -19,7 +20,7 @@ public RenderFragment? ChildContent { get; set; } [Parameter] - public ProfileResponse User { get; set; } + public ProfileResponseEventData User { get; set; } [Parameter] public string? UserId { get; set; } diff --git a/MatrixRoomUtils.Web/Shared/RoomList.razor b/MatrixRoomUtils.Web/Shared/RoomList.razor index 7e002ed..db2d059 100644 --- a/MatrixRoomUtils.Web/Shared/RoomList.razor +++ b/MatrixRoomUtils.Web/Shared/RoomList.razor @@ -1,5 +1,7 @@ @using MatrixRoomUtils.Web.Shared.RoomListComponents; @using MatrixRoomUtils.Core.StateEventTypes +@using MatrixRoomUtils.Core.StateEventTypes.Common +@using MatrixRoomUtils.Core.StateEventTypes.Spec

@Rooms.Count rooms total, @RoomsWithTypes.Sum(x=>x.Value.Count) fetched so far...

@if(Rooms.Count != RoomsWithTypes.Sum(x=>x.Value.Count)) {

Fetching more rooms...

@@ -16,14 +18,14 @@ else { @code { [Parameter] - public List Rooms { get; set; } + public List Rooms { get; set; } [Parameter] - public ProfileResponse? GlobalProfile { get; set; } + public ProfileResponseEventData? GlobalProfile { get; set; } - Dictionary> RoomsWithTypes = new(); + Dictionary> RoomsWithTypes = new(); protected override async Task OnInitializedAsync() { - GlobalProfile ??= await (await MRUStorage.GetCurrentSession()!).GetProfile((await MRUStorage.GetCurrentSession()!).WhoAmI.UserId); + GlobalProfile ??= await (await MRUStorage.GetCurrentSession())!.GetProfile((await MRUStorage.GetCurrentSession())!.WhoAmI.UserId); if (RoomsWithTypes.Any()) return; var tasks = Rooms.Select(AddRoom); @@ -35,35 +37,42 @@ else { private string GetRoomTypeName(string? roomType) => roomType switch { "m.space" => "Space", "msc3588.stories.stories-room" => "Story room", + "support.feline.policy.lists.msc.v1" => "MSC3784 Policy list (v1)", null => "Room", _ => roomType }; - private static SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(4, 4); - private async Task AddRoom(GenericRoom room) { + private static SemaphoreSlim _semaphoreSlim = new(8, 8); + private async Task AddRoom(RoomInfo room) { await _semaphoreSlim.WaitAsync(); string roomType; try { - var createEvent = await room.GetCreateEventAsync(); + RoomCreateEventData createEvent = (await room.GetStateEvent("m.room.create")).TypedContent as RoomCreateEventData; roomType = GetRoomTypeName(createEvent.Type); if (roomType == "Room") { - var shortcodeState = await room.GetStateAsync("org.matrix.mjolnir.shortcode"); - if (shortcodeState is not null) roomType = "Legacy policy room"; + var mjolnirData = await room.GetStateEvent("org.matrix.mjolnir.shortcode"); + if(mjolnirData?.RawContent?.ToJson(ignoreNull: true) is not null and not "{}") + roomType = "Legacy policy room"; } + //prefetch some stuff + await Task.WhenAll( + room.GetStateEvent("m.room.name"), + room.GetStateEvent("m.room.name") + ); } catch (MatrixException e) { roomType = $"Error: {e.ErrorCode}"; } if (!RoomsWithTypes.ContainsKey(roomType)) { - RoomsWithTypes.Add(roomType, new List()); + RoomsWithTypes.Add(roomType, new List()); } RoomsWithTypes[roomType].Add(room); - // if (RoomsWithTypes.Count % 10 == 0) - StateHasChanged(); + // if (RoomsWithTypes[roomType].Count % 10 == 0) + StateHasChanged(); // await Task.Delay(100); _semaphoreSlim.Release(); } diff --git a/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListCategory.razor b/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListCategory.razor index e860321..4be3c1f 100644 --- a/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListCategory.razor +++ b/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListCategory.razor @@ -1,12 +1,16 @@ @using MatrixRoomUtils.Core.StateEventTypes +@using MatrixRoomUtils.Core.StateEventTypes.Spec
@roomType (@rooms.Count) @foreach (var room in rooms) {
- - View timeline - View state - Edit state + + @if (room.StateEvents.Any(x => x.Type == "m.room.create")) { + + } + View timeline + View state + Edit state @if (roomType == "Space") { @@ -19,12 +23,12 @@ @code { [Parameter] - public KeyValuePair> Category { get; set; } + public KeyValuePair> Category { get; set; } [Parameter] - public ProfileResponse? GlobalProfile { get; set; } + public ProfileResponseEventData? GlobalProfile { get; set; } private string roomType => Category.Key; - private List rooms => Category.Value; + private List rooms => Category.Value; } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListSpace.razor b/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListSpace.razor index 73dc334..5153658 100644 --- a/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListSpace.razor +++ b/MatrixRoomUtils.Web/Shared/RoomListComponents/RoomListSpace.razor @@ -1,4 +1,4 @@ -Manage space +Manage space
@@ -14,23 +14,25 @@ @code { [Parameter] - public GenericRoom Space { get; set; } + public RoomInfo Space { get; set; } [Parameter, CascadingParameter] public string? Breadcrumbs { - get => _breadcrumbs + Space.RoomId; + get => _breadcrumbs + Space.Room.RoomId; set => _breadcrumbs = value; } - private List Children { get; set; } = new(); + private List Children { get; set; } = new(); protected override async Task OnInitializedAsync() { if (Breadcrumbs == null) throw new ArgumentNullException(nameof(Breadcrumbs)); await Task.Delay(Random.Shared.Next(1000, 10000)); - var rooms = Space.AsSpace.GetRoomsAsync(); + var rooms = Space.Room.AsSpace.GetRoomsAsync(); await foreach (var room in rooms) { - if(Breadcrumbs.Contains(room.RoomId)) continue; - Children.Add(room); + if (Breadcrumbs.Contains(room.RoomId)) continue; + Children.Add(new() { + Room = room + }); } await base.OnInitializedAsync(); } @@ -41,7 +43,7 @@ private async Task SpaceChildrenOpened() { if (_shouldRenderChildren) return; _shouldRenderChildren = true; - Console.WriteLine($"[RoomList] Rendering children of {Space.RoomId}"); + Console.WriteLine($"[RoomList] Rendering children of {Space.Room.RoomId}"); } } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/RoomListItem.razor b/MatrixRoomUtils.Web/Shared/RoomListItem.razor index 13cc02d..d35c9ab 100644 --- a/MatrixRoomUtils.Web/Shared/RoomListItem.razor +++ b/MatrixRoomUtils.Web/Shared/RoomListItem.razor @@ -2,6 +2,7 @@ @using System.Text.Json @using MatrixRoomUtils.Core.Helpers @using MatrixRoomUtils.Core.StateEventTypes +@using MatrixRoomUtils.Core.StateEventTypes.Spec
@if (OwnMemberState != null) { ("m.room.member", hs.UserId); - GlobalProfile ??= await hs.GetProfile(hs.UserId, true); + OwnMemberState ??= (await RoomInfo.GetStateEvent("m.room.member", hs.UserId)).TypedContent as RoomMemberEventData; + GlobalProfile ??= await hs.GetProfile(hs.UserId); } catch (MatrixException e) { if (e is { ErrorCode: "M_FORBIDDEN" }) { @@ -93,7 +105,7 @@ private async Task CheckRoomVersion() { try { - var ce = await Room.GetCreateEventAsync(); + var ce = (await RoomInfo.GetStateEvent("m.room.create")).TypedContent as RoomCreateEventData; if (int.TryParse(ce.RoomVersion, out var rv)) { if (rv < 10) hasOldRoomVersion = true; @@ -115,9 +127,9 @@ private async Task GetRoomInfo() { try { - roomName ??= await Room.GetNameAsync(); + roomName ??= ((await RoomInfo.GetStateEvent("m.room.name"))?.TypedContent as RoomNameEventData)?.Name ?? RoomId; - var state = await Room.GetStateAsync("m.room.avatar"); + var state = (await RoomInfo.GetStateEvent("m.room.avatar")).TypedContent as RoomAvatarEventData; if (state?.Url is { } url) { roomIcon = MediaResolver.ResolveMediaUri(hs.FullHomeServerDomain, url); Console.WriteLine($"Got avatar for room {RoomId}: {roomIcon} ({url})"); diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor index b7e0220..519e0b2 100644 --- a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor @@ -1,5 +1,6 @@ @using MatrixRoomUtils.Core.Responses @using MatrixRoomUtils.Core.StateEventTypes +@using MatrixRoomUtils.Core.StateEventTypes.Spec @if (roomMemberData.Membership == "ban") { @Event.StateKey was banned diff --git a/MatrixRoomUtils.Web/Shared/UserListItem.razor b/MatrixRoomUtils.Web/Shared/UserListItem.razor index a41ce49..20b1c8c 100644 --- a/MatrixRoomUtils.Web/Shared/UserListItem.razor +++ b/MatrixRoomUtils.Web/Shared/UserListItem.razor @@ -1,6 +1,7 @@ @using MatrixRoomUtils.Core.Responses @using MatrixRoomUtils.Core.StateEventTypes @using MatrixRoomUtils.Core.Helpers +@using MatrixRoomUtils.Core.StateEventTypes.Spec
@profileName @@ -19,7 +20,7 @@ public RenderFragment? ChildContent { get; set; } [Parameter] - public ProfileResponse User { get; set; } + public ProfileResponseEventData User { get; set; } [Parameter] public string UserId { get; set; } -- cgit 1.4.1