about summary refs log tree commit diff
path: root/MatrixUtils.Abstractions
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-03-22 17:47:29 +0100
committerRory& <root@rory.gay>2024-03-22 17:47:29 +0100
commitc69e5c790b2b277d9b11265b8f0883e9f90fe3b9 (patch)
treea2ad72230772d7459605ebc4ba13337e70d3bda4 /MatrixUtils.Abstractions
parentChanges (diff)
downloadMatrixUtils-c69e5c790b2b277d9b11265b8f0883e9f90fe3b9.tar.xz
Changes
Diffstat (limited to 'MatrixUtils.Abstractions')
-rw-r--r--MatrixUtils.Abstractions/RoomInfo.cs59
1 files changed, 33 insertions, 26 deletions
diff --git a/MatrixUtils.Abstractions/RoomInfo.cs b/MatrixUtils.Abstractions/RoomInfo.cs
index 53acbee..5c258a4 100644
--- a/MatrixUtils.Abstractions/RoomInfo.cs
+++ b/MatrixUtils.Abstractions/RoomInfo.cs
@@ -11,6 +11,20 @@ using LibMatrix.RoomTypes;
 namespace MatrixUtils.Abstractions;
 
 public class RoomInfo : NotifyPropertyChanged {
+    public RoomInfo(GenericRoom room) {
+        Room = room;
+        _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId);
+        RegisterEventListener();
+    }
+
+    public RoomInfo(GenericRoom room, List<StateEventResponse>? stateEvents) {
+        Room = room;
+        _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId);
+        if (stateEvents is { Count: > 0 }) StateEvents = new(stateEvents!);
+        RegisterEventListener();
+        ProcessNewItems(stateEvents!);
+    }
+    
     public readonly GenericRoom Room;
     public ObservableCollection<StateEventResponse?> StateEvents { get; private set; } = new();
 
@@ -131,37 +145,30 @@ public class RoomInfo : NotifyPropertyChanged {
         set => SetField(ref _ownMembership, value);
     }
 
-    public RoomInfo(GenericRoom room) {
-        Room = room;
-        _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId);
-        registerEventListener();
-    }
-
-    public RoomInfo(GenericRoom room, List<StateEventResponse>? stateEvents) {
-        Room = room;
-        _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId);
-        if (stateEvents is { Count: > 0 }) StateEvents = new(stateEvents!);
-        registerEventListener();
-    }
-
-    private void registerEventListener() {
+    private void RegisterEventListener() {
         StateEvents.CollectionChanged += (_, args) => {
             if (args.NewItems is { Count: > 0 })
-                foreach (StateEventResponse? newState in args.NewItems) {
-                    // TODO: switch statement benchmark?
-                    if (newState is null) continue;
-                    if (newState.Type == RoomNameEventContent.EventId && newState.TypedContent is RoomNameEventContent roomNameContent)
-                        RoomName = roomNameContent.Name;
-                    else if (newState is { Type: RoomAvatarEventContent.EventId, TypedContent: RoomAvatarEventContent roomAvatarContent })
-                        RoomIcon = roomAvatarContent.Url;
-                    else if (newState is { Type: RoomCreateEventContent.EventId, TypedContent: RoomCreateEventContent roomCreateContent }) {
-                        CreationEventContent = roomCreateContent;
-                        RoomCreator = newState.Sender;
-                    }
-                }
+                ProcessNewItems(args.NewItems.OfType<StateEventResponse>());
         };
     }
 
+    private void ProcessNewItems(IEnumerable<StateEventResponse?> newItems) {
+        foreach (StateEventResponse? newState in newItems) {
+            if (newState is null) continue;
+            // TODO: Benchmark switch statement
+            
+            if(newState.StateKey != "") continue;
+            if (newState.Type == RoomNameEventContent.EventId && newState.TypedContent is RoomNameEventContent roomNameContent)
+                RoomName = roomNameContent.Name;
+            else if (newState is { Type: RoomAvatarEventContent.EventId, TypedContent: RoomAvatarEventContent roomAvatarContent })
+                RoomIcon = roomAvatarContent.Url;
+            else if (newState is { Type: RoomCreateEventContent.EventId, TypedContent: RoomCreateEventContent roomCreateContent }) {
+                CreationEventContent = roomCreateContent;
+                RoomCreator = newState.Sender;
+            }
+        }
+    }
+
     public async Task FetchAllStateAsync() {
         var stateEvents = Room.GetFullStateAsync();
         await foreach (var stateEvent in stateEvents) {