From d8b91f1c5e07a1aa30963977a145ff8ecde10a41 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 17 Jul 2023 03:14:38 +0200 Subject: Improve timeline --- MatrixRoomUtils.Core/StateEvent.cs | 13 ++++- .../Spec/ProfileResponseEventData.cs | 3 +- MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj | 4 -- .../Pages/RoomManager/RoomManagerTimeline.razor | 54 --------------------- MatrixRoomUtils.Web/Pages/Rooms/Index.razor | 8 ++-- MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor | 53 ++++++++++++++++++++ MatrixRoomUtils.Web/Shared/InlineUserItem.razor | 26 ++++++++-- .../TimelineComponents/BaseTimelineItem.razor | 15 ++++++ .../TimelineComponents/TimelineMemberItem.razor | 56 ++++++++++++---------- .../TimelineComponents/TimelineMessageItem.razor | 7 ++- .../TimelineRoomCreateItem.razor | 18 +++++++ .../TimelineComponents/TimelineUnknownItem.razor | 15 ++---- 12 files changed, 163 insertions(+), 109 deletions(-) delete mode 100644 MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor create mode 100644 MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor create mode 100644 MatrixRoomUtils.Web/Shared/TimelineComponents/BaseTimelineItem.razor create mode 100644 MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineRoomCreateItem.razor diff --git a/MatrixRoomUtils.Core/StateEvent.cs b/MatrixRoomUtils.Core/StateEvent.cs index 8952403..1c0a1cf 100644 --- a/MatrixRoomUtils.Core/StateEvent.cs +++ b/MatrixRoomUtils.Core/StateEvent.cs @@ -15,7 +15,16 @@ public class StateEvent { new ClassCollector().ResolveFromAllAccessibleAssemblies(); public object TypedContent { - get => RawContent.Deserialize(GetType)!; + get { + try { + return RawContent.Deserialize(GetType)!; + } + catch (JsonException e) { + Console.WriteLine(e); + Console.WriteLine("Content:\n"+ObjectExtensions.ToJson(RawContent)); + } + return null; + } set => RawContent = JsonSerializer.Deserialize(JsonSerializer.Serialize(value)); } @@ -101,4 +110,4 @@ public class StateEvent { } public string cdtype => TypedContent.GetType().Name; -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Core/StateEventTypes/Spec/ProfileResponseEventData.cs b/MatrixRoomUtils.Core/StateEventTypes/Spec/ProfileResponseEventData.cs index 4596de9..f3a0ce9 100644 --- a/MatrixRoomUtils.Core/StateEventTypes/Spec/ProfileResponseEventData.cs +++ b/MatrixRoomUtils.Core/StateEventTypes/Spec/ProfileResponseEventData.cs @@ -4,11 +4,10 @@ using MatrixRoomUtils.Core.Interfaces; namespace MatrixRoomUtils.Core.StateEventTypes.Spec; -[MatrixEvent(EventName = "m.room.member")] public class ProfileResponseEventData : IStateEventType { [JsonPropertyName("avatar_url")] public string? AvatarUrl { get; set; } = ""; [JsonPropertyName("displayname")] public string? DisplayName { get; set; } = ""; -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj b/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj index 5e72471..13a120c 100644 --- a/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj +++ b/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj @@ -18,8 +18,4 @@ - - - - diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor deleted file mode 100644 index 3225c15..0000000 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor +++ /dev/null @@ -1,54 +0,0 @@ -@page "/Rooms/{RoomId}/Timeline" -@using MatrixRoomUtils.Web.Shared.TimelineComponents -@using MatrixRoomUtils.Core.Responses -

RoomManagerTimeline

-
-

Loaded @Events.Count events...

- -@foreach (var evt in Events) { -
- -
-} - -@code { - - [Parameter] - public string RoomId { get; set; } - - private List Messages { get; } = new(); - private List Events { get; } = new(); - - protected override async Task OnInitializedAsync() { - Console.WriteLine("RoomId: " + RoomId); - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - var room = await hs.GetRoom(RoomId); - MessagesResponse? msgs = null; - do { - msgs = await room.GetMessagesAsync(limit: 250, from: msgs?.End, dir: "b"); - Messages.Add(msgs); - Console.WriteLine($"Got {msgs.Chunk.Count} messages"); - msgs.Chunk.Reverse(); - Events.InsertRange(0, msgs.Chunk); - StateHasChanged(); - } while (msgs.End is not null); - - await base.OnInitializedAsync(); - } - - private StateEventResponse GetProfileEventBefore(StateEventResponse Event) => Events.TakeWhile(x => x != Event).Last(e => e.Type == "m.room.member" && e.StateKey == Event.Sender); - - private Type ComponentType(StateEventResponse Event) => Event.Type switch { - "m.room.message" => typeof(TimelineMessageItem), - "m.room.member" => typeof(TimelineMemberItem), - _ => typeof(TimelineUnknownItem) - }; - - private Dictionary ComponentParameters(Type componentType, StateEventResponse @event) => componentType switch { - not null when componentType == typeof(TimelineMessageItem) => new Dictionary { { "Event", @event }, { "Events", Events } }, - not null when componentType == typeof(TimelineMemberItem) => new Dictionary { { "Event", @event }, { "Events", Events } }, - _ => new Dictionary { { "Event", @event }, { "Events", Events } } - }; - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/Rooms/Index.razor b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor index a70ed9d..816299f 100644 --- a/MatrixRoomUtils.Web/Pages/Rooms/Index.razor +++ b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor @@ -52,7 +52,7 @@ SyncResult? sync = null; string? nextBatch = null; while (sync is null or { Rooms.Join.Count: > 10}) { - sync = await hs.SyncHelper.Sync(since: nextBatch, filter: filter); + sync = await hs.SyncHelper.Sync(since: nextBatch, filter: filter, timeout: 0); nextBatch = sync?.NextBatch ?? nextBatch; if (sync is null) continue; Console.WriteLine($"Got sync, next batch: {nextBatch}!"); @@ -91,7 +91,7 @@ roomInfo.StateEvents.Add(new StateEventResponse() { Type = "m.room.avatar", TypedContent = new RoomAvatarEventData() { - + } }); } @@ -135,7 +135,7 @@ } private bool RenderContents { get; set; } = false; - + private string _status; public string Status { @@ -146,4 +146,4 @@ } } -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor b/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor new file mode 100644 index 0000000..2234dd9 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor @@ -0,0 +1,53 @@ +@page "/Rooms/{RoomId}/Timeline" +@using MatrixRoomUtils.Web.Shared.TimelineComponents +@using MatrixRoomUtils.Core.Responses +@using MatrixRoomUtils.Core.StateEventTypes.Spec +

RoomManagerTimeline

+
+

Loaded @Events.Count events...

+ +@foreach (var evt in Events) { +
+ + +
+} + +@code { + + [Parameter] + public string RoomId { get; set; } + + private List Messages { get; } = new(); + private List Events { get; } = new(); + + private AuthenticatedHomeServer? HomeServer { get; set; } + + protected override async Task OnInitializedAsync() { + Console.WriteLine("RoomId: " + RoomId); + HomeServer = await MRUStorage.GetCurrentSessionOrNavigate(); + if (HomeServer is null) return; + var room = await HomeServer.GetRoom(RoomId); + MessagesResponse? msgs = null; + do { + msgs = await room.GetMessagesAsync(limit: 1000, from: msgs?.End, dir: "b"); + Messages.Add(msgs); + Console.WriteLine($"Got {msgs.Chunk.Count} messages"); + msgs.Chunk.Reverse(); + Events.InsertRange(0, msgs.Chunk); + } while (msgs.End is not null); + + + await base.OnInitializedAsync(); + } + + private StateEventResponse GetProfileEventBefore(StateEventResponse Event) => Events.TakeWhile(x => x != Event).Last(e => e.Type == "m.room.member" && e.StateKey == Event.Sender); + + private Type ComponentType(StateEventResponse Event) => Event.TypedContent switch { + RoomMessageEventData => typeof(TimelineMessageItem), + RoomMemberEventData => typeof(TimelineMemberItem), + _ => typeof(TimelineUnknownItem) + }; + +} diff --git a/MatrixRoomUtils.Web/Shared/InlineUserItem.razor b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor index f9cef91..25db838 100644 --- a/MatrixRoomUtils.Web/Shared/InlineUserItem.razor +++ b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor @@ -22,6 +22,9 @@ [Parameter] public ProfileResponseEventData User { get; set; } + [Parameter] + public ProfileResponseEventData MemberEvent { get; set; } + [Parameter] public string? UserId { get; set; } @@ -31,21 +34,34 @@ [Parameter] public string? ProfileName { get; set; } = null; + [Parameter] + public AuthenticatedHomeServer? HomeServer { get; set; } + private static SemaphoreSlim _semaphoreSlim = new(128); protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if(hs is null) return; + HomeServer ??= await MRUStorage.GetCurrentSessionOrNavigate(); + if(HomeServer is null) return; await _semaphoreSlim.WaitAsync(); - if (User == null && UserId == null) + if (User == null && UserId == null && MemberEvent != null) throw new ArgumentNullException(nameof(UserId)); - User ??= await hs.GetProfile(UserId); + + if (MemberEvent != null) { + User = new() { + AvatarUrl = MemberEvent.AvatarUrl, + DisplayName = MemberEvent.DisplayName + }; + } + + if (User is null && UserId is not null) { + User ??= await HomeServer.GetProfile(UserId); + } - ProfileAvatar ??= MediaResolver.ResolveMediaUri(hs.FullHomeServerDomain, User.AvatarUrl); + ProfileAvatar ??= MediaResolver.ResolveMediaUri(HomeServer.FullHomeServerDomain, User.AvatarUrl); ProfileName ??= User.DisplayName; _semaphoreSlim.Release(); diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/BaseTimelineItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/BaseTimelineItem.razor new file mode 100644 index 0000000..920a38d --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/BaseTimelineItem.razor @@ -0,0 +1,15 @@ +@using MatrixRoomUtils.Core.Responses +

BaseTimelineItem

+ +@code { + + [Parameter] + public StateEventResponse Event { get; set; } + + [Parameter] + public List Events { get; set; } + + [Parameter] + public AuthenticatedHomeServer HomeServer { get; set; } + +} diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor index 519e0b2..44eb06e 100644 --- a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor @@ -1,39 +1,47 @@ @using MatrixRoomUtils.Core.Responses @using MatrixRoomUtils.Core.StateEventTypes @using MatrixRoomUtils.Core.StateEventTypes.Spec +@inherits BaseTimelineItem -@if (roomMemberData.Membership == "ban") { - @Event.StateKey was banned -} -else if (roomMemberData.Membership == "invite") { - @Event.StateKey was invited -} -else if (roomMemberData.Membership == "join") { - @if (Event.ReplacesState is not null) { - @Event.StateKey changed their display name to @(roomMemberData.Displayname ?? Event.Sender) +@if (roomMemberData is not null) { + @if (roomMemberData.Membership == "ban") { + @Event.StateKey was banned + } + else if (roomMemberData.Membership == "invite") { + @Event.StateKey was invited + } + else if (roomMemberData.Membership == "join") { + @if (Event.ReplacesState is not null) { + @Event.StateKey changed their display name to @(roomMemberData.Displayname ?? Event.Sender) + } + else { + joined + } + } + else if (roomMemberData.Membership == "leave") { + @Event.StateKey left + } + else if (roomMemberData.Membership == "knock") { + @Event.StateKey knocked } else { - joined + @Event.StateKey has an unknown state: +
+        @Event.ToJson()
+    
} } -else if (roomMemberData.Membership == "leave") { - @Event.StateKey left -} -else if (roomMemberData.Membership == "knock") { - @Event.StateKey knocked -} else { - @Event.StateKey has an unknown state: -
-        @Event.ToJson()
-    
+
+ Unknown membership event for @Event.StateKey +
+            @Event.ToJson()
+        
+
} @code { - [Parameter] - public StateEventResponse Event { get; set; } - private RoomMemberEventData? roomMemberData => Event.TypedContent as RoomMemberEventData; -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor index b1ce146..bf0e083 100644 --- a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor @@ -1,11 +1,10 @@ @using MatrixRoomUtils.Core.Responses +@inherits BaseTimelineItem +
     @ObjectExtensions.ToJson(Event.RawContent, indent: false)
 
@code { - [Parameter] - public StateEventResponse Event { get; set; } - -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineRoomCreateItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineRoomCreateItem.razor new file mode 100644 index 0000000..de204a3 --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineRoomCreateItem.razor @@ -0,0 +1,18 @@ +@using MatrixRoomUtils.Core.Responses +@using MatrixRoomUtils.Core.StateEventTypes.Spec +@inherits BaseTimelineItem + +

+ @Event.Sender created the room with room version @CreationEventContent.RoomVersion + @CreationEventContent.Federate ? "and" : "without" federating with other servers.
+ This room is of type @(CreationEventContent.Type ?? "Untyped room (usually a chat room)") +

+
+    @ObjectExtensions.ToJson(Event.RawContent, indent: false)
+
+ +@code { + + private RoomCreateEventData CreationEventContent => Event.TypedContent as RoomCreateEventData; + +} diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor index d8ea7e2..d0871c3 100644 --- a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor @@ -1,20 +1,15 @@ -
+@inherits BaseTimelineItem +
- - Unknown event type:
@Event.Type
-
+ Unknown event type:
@Event.Type
-
-           @Event.ToJson()
-        
+
@Event.ToJson()
@code { - [Parameter] - public StateEvent Event { get; set; } -} \ No newline at end of file +} -- cgit 1.5.1