diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-07-17 03:14:38 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-07-17 03:14:38 +0200 |
commit | d8b91f1c5e07a1aa30963977a145ff8ecde10a41 (patch) | |
tree | af12f3a2f81f75f5cd8e7e39c1b86d0f25374ffc | |
parent | Handle external logouts (diff) | |
download | MatrixUtils-d8b91f1c5e07a1aa30963977a145ff8ecde10a41.tar.xz |
Improve timeline
-rw-r--r-- | MatrixRoomUtils.Core/StateEvent.cs | 13 | ||||
-rw-r--r-- | MatrixRoomUtils.Core/StateEventTypes/Spec/ProfileResponseEventData.cs | 3 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj | 4 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Rooms/Index.razor | 8 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor (renamed from MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor) | 31 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/InlineUserItem.razor | 26 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/TimelineComponents/BaseTimelineItem.razor | 15 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor | 56 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor | 7 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineRoomCreateItem.razor | 18 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor | 15 |
11 files changed, 125 insertions, 71 deletions
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<IStateEventType>().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<JsonObject>(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 @@ <ProjectReference Include="..\MatrixRoomUtils.Core\MatrixRoomUtils.Core.csproj" /> </ItemGroup> - <ItemGroup> - <Folder Include="Shared\TimelineComponents\TimelineMessageComponents" /> - </ItemGroup> - </Project> 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/RoomManager/RoomManagerTimeline.razor b/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor index 3225c15..2234dd9 100644 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor +++ b/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor @@ -1,13 +1,16 @@ @page "/Rooms/{RoomId}/Timeline" @using MatrixRoomUtils.Web.Shared.TimelineComponents @using MatrixRoomUtils.Core.Responses +@using MatrixRoomUtils.Core.StateEventTypes.Spec <h3>RoomManagerTimeline</h3> <hr/> <p>Loaded @Events.Count events...</p> @foreach (var evt in Events) { <div type="@evt.Type" key="@evt.StateKey" itemid="@evt.EventId"> - <DynamicComponent Type="@ComponentType(evt)" Parameters="@(new Dictionary<string, object> { { "Event", evt }, { "Events", Events } })"></DynamicComponent> + <DynamicComponent Type="@ComponentType(evt)" + Parameters="@(new Dictionary<string, object> { { "Event", evt }, { "Events", Events }, { "HomeServer", HomeServer!} })"> + </DynamicComponent> </div> } @@ -19,36 +22,32 @@ private List<MessagesResponse> Messages { get; } = new(); private List<StateEventResponse> Events { get; } = new(); + private AuthenticatedHomeServer? HomeServer { get; set; } + 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); + HomeServer = await MRUStorage.GetCurrentSessionOrNavigate(); + if (HomeServer is null) return; + var room = await HomeServer.GetRoom(RoomId); MessagesResponse? msgs = null; do { - msgs = await room.GetMessagesAsync(limit: 250, from: msgs?.End, dir: "b"); + 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); - 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), + private Type ComponentType(StateEventResponse Event) => Event.TypedContent switch { + RoomMessageEventData => typeof(TimelineMessageItem), + RoomMemberEventData => typeof(TimelineMemberItem), _ => typeof(TimelineUnknownItem) }; - private Dictionary<string, object> ComponentParameters(Type componentType, StateEventResponse @event) => componentType switch { - not null when componentType == typeof(TimelineMessageItem) => new Dictionary<string, object> { { "Event", @event }, { "Events", Events } }, - not null when componentType == typeof(TimelineMemberItem) => new Dictionary<string, object> { { "Event", @event }, { "Events", Events } }, - _ => new Dictionary<string, object> { { "Event", @event }, { "Events", Events } } - }; - -} \ No newline at end of file +} 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 @@ -23,6 +23,9 @@ public ProfileResponseEventData User { get; set; } [Parameter] + public ProfileResponseEventData MemberEvent { get; set; } + + [Parameter] public string? UserId { get; set; } [Parameter] @@ -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 +<h3>BaseTimelineItem</h3> + +@code { + + [Parameter] + public StateEventResponse Event { get; set; } + + [Parameter] + public List<StateEventResponse> 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") { - <i>@Event.StateKey was banned</i> -} -else if (roomMemberData.Membership == "invite") { - <i>@Event.StateKey was invited</i> -} -else if (roomMemberData.Membership == "join") { - @if (Event.ReplacesState is not null) { - <i>@Event.StateKey changed their display name to @(roomMemberData.Displayname ?? Event.Sender)</i> +@if (roomMemberData is not null) { + @if (roomMemberData.Membership == "ban") { + <i>@Event.StateKey was banned</i> + } + else if (roomMemberData.Membership == "invite") { + <i>@Event.StateKey was invited</i> + } + else if (roomMemberData.Membership == "join") { + @if (Event.ReplacesState is not null) { + <i>@Event.StateKey changed their display name to @(roomMemberData.Displayname ?? Event.Sender)</i> + } + else { + <i><InlineUserItem User="new ProfileResponseEventData()" HomeServer="HomeServer" UserId="@Event.StateKey"></InlineUserItem> joined</i> + } + } + else if (roomMemberData.Membership == "leave") { + <i>@Event.StateKey left</i> + } + else if (roomMemberData.Membership == "knock") { + <i>@Event.StateKey knocked</i> } else { - <i><InlineUserItem UserId="@Event.StateKey"></InlineUserItem> joined</i> + <i>@Event.StateKey has an unknown state:</i> + <pre> + @Event.ToJson() + </pre> } } -else if (roomMemberData.Membership == "leave") { - <i>@Event.StateKey left</i> -} -else if (roomMemberData.Membership == "knock") { - <i>@Event.StateKey knocked</i> -} else { - <i>@Event.StateKey has an unknown state:</i> - <pre> - @Event.ToJson() - </pre> + <details> + <summary>Unknown membership event for @Event.StateKey</summary> + <pre> + @Event.ToJson() + </pre> + </details> } @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 + <pre> @ObjectExtensions.ToJson(Event.RawContent, indent: false) </pre> @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 + +<p> + @Event.Sender created the room with room version @CreationEventContent.RoomVersion + @CreationEventContent.Federate ? "and" : "without" federating with other servers.<br/> + This room is of type @(CreationEventContent.Type ?? "Untyped room (usually a chat room)") +</p> +<pre> + @ObjectExtensions.ToJson(Event.RawContent, indent: false) +</pre> + +@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 @@ -<div> +@inherits BaseTimelineItem +<div> <details style="display: inline;"> <summary> - <i style="color: red;"> - Unknown event type: <pre style="display: inline;">@Event.Type</pre> - </i> + <i style="color: red;">Unknown event type: <pre style="display: inline;">@Event.Type</pre></i> </summary> - <pre> - @Event.ToJson() - </pre> + <pre>@Event.ToJson()</pre> </details> </div> @code { - [Parameter] - public StateEvent Event { get; set; } -} \ No newline at end of file +} |