about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-01 20:51:15 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-01 20:51:15 +0200
commit7978f08235ceca22eacae11a88a7703513238cb3 (patch)
tree6a7058f21f6040cc1e9396a85774bc16ea133c9e /MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs
parentTodays progress (diff)
downloadMatrixUtils-7978f08235ceca22eacae11a88a7703513238cb3.tar.xz
Deduplicate some api calls
Diffstat (limited to 'MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs')
-rw-r--r--MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs21
1 files changed, 18 insertions, 3 deletions
diff --git a/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs b/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs
index f57c855..879ae6b 100644
--- a/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs
+++ b/MatrixRoomUtils.Core/RoomTypes/GenericRoom.cs
@@ -44,7 +44,17 @@ public class GenericRoom {
         var url = $"/_matrix/client/v3/rooms/{RoomId}/state";
         if (!string.IsNullOrEmpty(type)) url += $"/{type}";
         if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}";
-        return await _httpClient.GetFromJsonAsync<T>(url);
+        try {
+            var resp = await _httpClient.GetFromJsonAsync<T>(url);
+            return resp;
+        }
+        catch (MatrixException e) {
+            if (e is not { ErrorCode: "M_NOT_FOUND" }) {
+                throw;
+            }
+            Console.WriteLine(e);
+            return default;
+        }
     }
 
     public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b",
@@ -56,8 +66,13 @@ public class GenericRoom {
     }
 
     public async Task<string> GetNameAsync() {
-        var res = await GetStateAsync<RoomNameEventData>("m.room.name");
-        return res.Name ?? RoomId;
+        try {
+            var res = await GetStateAsync<RoomNameEventData>("m.room.name");
+            return res?.Name ?? RoomId;
+        }
+        catch (MatrixException e) {
+            return $"{RoomId} ({e.ErrorCode})";
+        }
     }
 
     public async Task JoinAsync(string[]? homeservers = null, string? reason = null) {