about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes/RoomInfo.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-02 01:01:09 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-02 01:01:09 +0200
commitdef33cc092ae2c6defcc218b108b7c99cbfb8581 (patch)
treeba992ff8c30b7d4e8af0a78350e157e095455a18 /MatrixRoomUtils.Web/Classes/RoomInfo.cs
parentDeduplicate some api calls (diff)
downloadMatrixUtils-def33cc092ae2c6defcc218b108b7c99cbfb8581.tar.xz
Prefetch room info
Diffstat (limited to 'MatrixRoomUtils.Web/Classes/RoomInfo.cs')
-rw-r--r--MatrixRoomUtils.Web/Classes/RoomInfo.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Web/Classes/RoomInfo.cs b/MatrixRoomUtils.Web/Classes/RoomInfo.cs
new file mode 100644
index 0000000..711bf55
--- /dev/null
+++ b/MatrixRoomUtils.Web/Classes/RoomInfo.cs
@@ -0,0 +1,29 @@
+using MatrixRoomUtils.Core;
+using MatrixRoomUtils.Core.Responses;
+using MatrixRoomUtils.Core.RoomTypes;
+
+namespace MatrixRoomUtils.Web.Classes; 
+
+public class RoomInfo {
+    public GenericRoom Room { get; set; }
+    public List<StateEventResponse?> StateEvents { get; } = new();
+    
+    public async Task<StateEventResponse?> GetStateEvent(string type, string 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,
+        };
+        try {
+            @event.TypedContent = await Room.GetStateAsync<object>(type, stateKey);
+        }
+        catch (MatrixException e) {
+            if (e is { ErrorCode: "M_NOT_FOUND" }) @event.TypedContent = default!;
+            else throw;
+        }
+        StateEvents.Add(@event);
+        return @event;
+    }
+}
\ No newline at end of file