about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Interfaces/IHomeServer.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/Interfaces/IHomeServer.cs
parentSmall refactoring (diff)
downloadMatrixUtils-b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1.tar.xz
Refactoring
Diffstat (limited to 'MatrixRoomUtils.Core/Interfaces/IHomeServer.cs')
-rw-r--r--MatrixRoomUtils.Core/Interfaces/IHomeServer.cs57
1 files changed, 46 insertions, 11 deletions
diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
index 84714f7..438709f 100644
--- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
+++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
@@ -1,8 +1,8 @@
 using System.Net.Http.Json;
 using System.Text.Json;
-using MatrixRoomUtils.Extensions;
+using MatrixRoomUtils.Core.Extensions;
 
-namespace MatrixRoomUtils;
+namespace MatrixRoomUtils.Core.Interfaces;
 
 public class IHomeServer
 {
@@ -10,8 +10,21 @@ public class IHomeServer
     public string FullHomeServerDomain { get; set; }
 
     private protected HttpClient _httpClient { get; set; } = new();
+
     public async Task<string> ResolveHomeserverFromWellKnown(string homeserver)
     {
+        if (RuntimeCache.HomeserverResolutionCache.ContainsKey(homeserver))
+        {
+            if (RuntimeCache.HomeserverResolutionCache[homeserver].ResolutionTime < DateTime.Now.AddHours(1))
+            {
+                Console.WriteLine($"Found cached homeserver: {RuntimeCache.HomeserverResolutionCache[homeserver].Result}");
+                return RuntimeCache.HomeserverResolutionCache[homeserver].Result;
+            }
+            RuntimeCache.HomeserverResolutionCache.Remove(homeserver);
+        }
+        //throw new NotImplementedException();
+
+        string result = null;
         Console.WriteLine($"Resolving homeserver: {homeserver}");
         if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
         if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client"))
@@ -20,18 +33,40 @@ public class IHomeServer
             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;
+            result = hs;
         }
-        Console.WriteLine($"No client well-known...");
-        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server"))
+        else
+        {
+            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();
+                result = hs;
+            }
+            else
+            {
+                Console.WriteLine($"No server well-known...");
+                if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) result = homeserver;
+                else
+                {
+                    Console.WriteLine("No homeserver on shortname...");
+                    if (await _httpClient.CheckSuccessStatus($"{homeserver.Replace("//", "//matrix.")}/_matrix/client/versions")) result = homeserver.Replace("//", "//matrix.");
+                    else Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
+                }
+            }
+        }
+
+        if (result != null)
         {
-            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server");
-            var hs = resp.GetProperty("m.server").GetString();
-            return hs;
+            Console.WriteLine($"Resolved homeserver: {homeserver} -> {result}");
+            RuntimeCache.HomeserverResolutionCache.TryAdd(homeserver, new()
+            {
+                Result = result,
+                ResolutionTime = DateTime.Now
+            });
+            return result;
         }
-        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