about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Room.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 01:49:10 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 01:49:10 +0200
commitfc749b3e57098740377e6eabd5d010d133256fa5 (patch)
treecc97267a3d4222c910769e46bdb37c96c7c31531 /MatrixRoomUtils.Core/Room.cs
parentunknown changes (diff)
downloadMatrixUtils-fc749b3e57098740377e6eabd5d010d133256fa5.tar.xz
Improved many features
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index 73a2bc5..f228271 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -42,6 +42,21 @@ public class Room
         if (res == null) return default;
         return res.Value.Deserialize<T>();
     }
+    
+    public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b", string filter = "")
+    {
+        var url = $"/_matrix/client/r0/rooms/{RoomId}/messages?from={from}&limit={limit}&dir={dir}";
+        if (!string.IsNullOrEmpty(filter)) url += $"&filter={filter}";
+        var res = await _httpClient.GetAsync(url);
+        if (!res.IsSuccessStatusCode)
+        {
+            Console.WriteLine($"Failed to get messages for {RoomId} - got status: {res.StatusCode}");
+            throw new Exception($"Failed to get messages for {RoomId} - got status: {res.StatusCode}");
+        }
+
+        var result = await res.Content.ReadFromJsonAsync<MessagesResponse>();
+        return result ?? new MessagesResponse();
+    }
 
     public async Task<string> GetNameAsync()
     {
@@ -148,6 +163,18 @@ public class Room
     }
 }
 
+public class MessagesResponse
+{
+    [JsonPropertyName("start")]
+    public string Start { get; set; }
+    [JsonPropertyName("end")]
+    public string? End { get; set; }
+    [JsonPropertyName("chunk")]
+    public List<StateEventResponse> Chunk { get; set; } = new();
+    [JsonPropertyName("state")]
+    public List<StateEventResponse> State { get; set; } = new();
+}
+
 public class CreateEvent
 {
     [JsonPropertyName("creator")] public string Creator { get; set; }