about summary refs log tree commit diff
path: root/MatrixUtils.Abstractions/RoomInfo.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixUtils.Abstractions/RoomInfo.cs')
-rw-r--r--MatrixUtils.Abstractions/RoomInfo.cs111
1 files changed, 72 insertions, 39 deletions
diff --git a/MatrixUtils.Abstractions/RoomInfo.cs b/MatrixUtils.Abstractions/RoomInfo.cs
index 0cd4dc1..877246b 100644
--- a/MatrixUtils.Abstractions/RoomInfo.cs
+++ b/MatrixUtils.Abstractions/RoomInfo.cs
@@ -1,9 +1,11 @@
+using System.Collections.Concurrent;
 using System.Collections.ObjectModel;
 using System.Text.Json.Nodes;
 using ArcaneLibs;
 using LibMatrix;
 using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.EventTypes.Spec.State.RoomInfo;
+using LibMatrix.Homeservers;
 using LibMatrix.RoomTypes;
 
 namespace MatrixUtils.Abstractions;
@@ -12,51 +14,22 @@ public class RoomInfo : NotifyPropertyChanged {
     public required GenericRoom Room { get; set; }
     public ObservableCollection<StateEventResponse?> StateEvents { get; } = new();
 
+    private static ConcurrentBag<AuthenticatedHomeserverGeneric> homeserversWithoutEventFormatSupport = new();
+    
     public async Task<StateEventResponse?> GetStateEvent(string type, string stateKey = "") {
+        if (homeserversWithoutEventFormatSupport.Contains(Room.Homeserver)) return await GetStateEventForged(type, stateKey);
         var @event = StateEvents.FirstOrDefault(x => x?.Type == type && x.StateKey == stateKey);
         if (@event is not null) return @event;
-        // @event = new StateEventResponse {
-        //     RoomId = Room.RoomId,
-        //     Type = type,
-        //     StateKey = stateKey,
-        //     Sender = null, //TODO implement
-        //     EventId = null
-        // };
-        // // if (Room is null) return null;
-        // try {
-        //     @event.RawContent = await Room.GetStateAsync<JsonObject>(type, stateKey);
-        // }
-        // catch (MatrixException e) {
-        //     if (e is { ErrorCode: "M_NOT_FOUND" }) {
-        //         if (type == "m.room.name")
-        //             @event = new() {
-        //                 Type = type,
-        //                 StateKey = stateKey,
-        //                 TypedContent = new RoomNameEventContent() {
-        //                     Name = await Room.GetNameOrFallbackAsync()
-        //                 },
-        //                 //TODO implement
-        //                 RoomId = null,
-        //                 Sender = null,
-        //                 EventId = null
-        //             };
-        //         else
-        //             @event.RawContent = default!;
-        //     }
-        //     else {
-        //         throw;
-        //     }
-        // }
-        // catch (Exception e) {
-        //     await Task.Delay(1000);
-        //     return await GetStateEvent(type, stateKey);
-        // }
-
+        
         try {
             @event = await Room.GetStateEventOrNullAsync(type, stateKey);
             StateEvents.Add(@event);
         }
         catch (Exception e) {
+            if (e is InvalidDataException) {
+                homeserversWithoutEventFormatSupport.Add(Room.Homeserver);
+                return await GetStateEventForged(type, stateKey);
+            }
             Console.Error.WriteLine(e);
             await Task.Delay(1000);
             return await GetStateEvent(type, stateKey);
@@ -65,6 +38,46 @@ public class RoomInfo : NotifyPropertyChanged {
         return @event;
     }
 
+    private async Task<StateEventResponse?> GetStateEventForged(string type, string stateKey = "") {
+        var @event = new StateEventResponse {
+            RoomId = Room.RoomId,
+            Type = type,
+            StateKey = stateKey,
+            Sender = null, //TODO implement
+            EventId = null
+        };
+        try {
+            @event.RawContent = await Room.GetStateAsync<JsonObject>(type, stateKey);
+        }
+        catch (MatrixException e) {
+            if (e is { ErrorCode: "M_NOT_FOUND" }) {
+                if (type == "m.room.name")
+                    @event = new() {
+                        Type = type,
+                        StateKey = stateKey,
+                        TypedContent = new RoomNameEventContent() {
+                            Name = await Room.GetNameOrFallbackAsync()
+                        },
+                        //TODO implement
+                        RoomId = null,
+                        Sender = null,
+                        EventId = null
+                    };
+                else
+                    @event.RawContent = default!;
+            }
+            else {
+                throw;
+            }
+        }
+        catch (Exception e) {
+            await Task.Delay(1000);
+            return await GetStateEvent(type, stateKey);
+        }
+
+        return @event;
+    }
+
     public string? RoomIcon {
         get => _roomIcon ?? "https://api.dicebear.com/6.x/identicon/svg?seed=" + Room.RoomId;
         set => SetField(ref _roomIcon, value);
@@ -92,11 +105,31 @@ public class RoomInfo : NotifyPropertyChanged {
     private string? _roomName;
     private RoomCreateEventContent? _creationEventContent;
     private string? _roomCreator;
+    private string? _overrideRoomType;
+    private string? _defaultRoomName;
+    private RoomMemberEventContent? _ownMembership;
+
+    public string? DefaultRoomName {
+        get => _defaultRoomName;
+        set {
+            if (SetField(ref _defaultRoomName, value)) OnPropertyChanged(nameof(RoomName));
+        }
+    }
+
+    public string? OverrideRoomType {
+        get => _overrideRoomType;
+        set {
+            if (SetField(ref _overrideRoomType, value)) OnPropertyChanged(nameof(RoomType));
+        }
+    }
 
-    public string? DefaultRoomName { get; set; }
-    public string? OverrideRoomType { get; set; }
     public string? RoomType => OverrideRoomType ?? CreationEventContent?.Type;
 
+    public RoomMemberEventContent? OwnMembership {
+        get => _ownMembership;
+        set => SetField(ref _ownMembership, value);
+    }
+
     public RoomInfo() {
         StateEvents.CollectionChanged += (_, args) => {
             if (args.NewItems is { Count: > 0 })