about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/RemoteHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 00:13:25 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 00:18:46 +0200
commitb933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1 (patch)
tree4176eb2b7f1befca685d00e119d842eb3f07da1f /MatrixRoomUtils.Core/RemoteHomeServer.cs
parentSmall refactoring (diff)
downloadMatrixUtils-b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1.tar.xz
Refactoring
Diffstat (limited to 'MatrixRoomUtils.Core/RemoteHomeServer.cs')
-rw-r--r--MatrixRoomUtils.Core/RemoteHomeServer.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/RemoteHomeServer.cs b/MatrixRoomUtils.Core/RemoteHomeServer.cs
new file mode 100644
index 0000000..6bd2251
--- /dev/null
+++ b/MatrixRoomUtils.Core/RemoteHomeServer.cs
@@ -0,0 +1,57 @@
+using System.Net.Http.Headers;
+using System.Net.Http.Json;
+using System.Text.Json;
+using MatrixRoomUtils.Core.Extensions;
+using MatrixRoomUtils.Core.Interfaces;
+using MatrixRoomUtils.Core.Responses;
+
+namespace MatrixRoomUtils.Core;
+
+public class RemoteHomeServer : IHomeServer
+{
+    public RemoteHomeServer(string canonicalHomeServerDomain)
+    {
+        HomeServerDomain = canonicalHomeServerDomain;
+        _httpClient = new HttpClient();
+    }
+    public async Task<RemoteHomeServer> Configure()
+    {
+        FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain);
+        _httpClient.Dispose();
+        _httpClient = new HttpClient { BaseAddress = new Uri(FullHomeServerDomain) };
+        Console.WriteLine("[RHS] Finished setting up http client");
+
+        return this;
+    }
+    public async Task<ProfileResponse> GetProfile(string mxid)
+    {
+        var resp = await _httpClient.GetAsync($"/_matrix/client/r0/profile/{mxid}");
+        var data = await resp.Content.ReadFromJsonAsync<JsonElement>();
+        if(!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data.ToString());
+        return data.Deserialize<ProfileResponse>();
+    }
+    
+    public async Task<Room> GetRoom(string roomId)
+    {
+        return new Room(_httpClient, roomId);
+    }
+
+    public async Task<List<Room>> GetJoinedRooms()
+    {
+        var rooms = new List<Room>();
+        var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms");
+        if (!roomQuery.IsSuccessStatusCode)
+        {
+            Console.WriteLine($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
+            throw new InvalidDataException($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
+        }
+
+        var roomsJson = await roomQuery.Content.ReadFromJsonAsync<JsonElement>();
+        foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray())
+        {
+            rooms.Add(new Room(_httpClient, room.GetString()));
+        }
+
+        return rooms;
+    }
+}
\ No newline at end of file