about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Interfaces
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-03 18:40:53 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-03 18:40:53 +0200
commit3d3edeae16252a311704b390cfad6faa435a8b84 (patch)
tree34974194435fbe9789de5140ef9a9c0ddb834c74 /MatrixRoomUtils.Core/Interfaces
parentAdd policy room discovery ,add room state viewer (diff)
downloadMatrixUtils-3d3edeae16252a311704b390cfad6faa435a8b84.tar.xz
Refactor
Diffstat (limited to 'MatrixRoomUtils.Core/Interfaces')
-rw-r--r--MatrixRoomUtils.Core/Interfaces/IHomeServer.cs37
1 files changed, 37 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
new file mode 100644
index 0000000..84714f7
--- /dev/null
+++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
@@ -0,0 +1,37 @@
+using System.Net.Http.Json;
+using System.Text.Json;
+using MatrixRoomUtils.Extensions;
+
+namespace MatrixRoomUtils;
+
+public class IHomeServer
+{
+    public string HomeServerDomain { get; set; }
+    public string FullHomeServerDomain { get; set; }
+
+    private protected HttpClient _httpClient { get; set; } = new();
+    public async Task<string> ResolveHomeserverFromWellKnown(string homeserver)
+    {
+        Console.WriteLine($"Resolving homeserver: {homeserver}");
+        if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
+        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client"))
+        {
+            Console.WriteLine($"Got successful response for client well-known...");
+            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/client");
+            Console.WriteLine($"Response: {resp.ToString()}");
+            var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString();
+            return hs;
+        }
+        Console.WriteLine($"No client well-known...");
+        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server"))
+        {
+            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server");
+            var hs = resp.GetProperty("m.server").GetString();
+            return hs;
+        }
+        Console.WriteLine($"No server well-known...");
+        if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) return homeserver;
+        Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
+        throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
+    }
+}
\ No newline at end of file