about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
commit712ad189c99570f686ab779782b2a873e172428e (patch)
tree6102e4719416e71522e9143fa4e06951258bd77c /MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
parentFix passwords being visible during editing (diff)
downloadMatrixUtils-712ad189c99570f686ab779782b2a873e172428e.tar.xz
Change syntax style
Diffstat (limited to 'MatrixRoomUtils.Core/Interfaces/IHomeServer.cs')
-rw-r--r--MatrixRoomUtils.Core/Interfaces/IHomeServer.cs86
1 files changed, 37 insertions, 49 deletions
diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
index 3ae1355..9a9ba7a 100644
--- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
+++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
@@ -5,35 +5,32 @@ using MatrixRoomUtils.Core.Responses;
 
 namespace MatrixRoomUtils.Core.Interfaces;
 
-public class IHomeServer
-{
-    private Dictionary<string, ProfileResponse?> _profileCache = new();
+public class IHomeServer {
+    private readonly Dictionary<string, ProfileResponse?> _profileCache = new();
     public string HomeServerDomain { get; set; }
     public string FullHomeServerDomain { get; set; }
 
-    private protected HttpClient _httpClient { get; set; } = new();
+    private protected MatrixHttpClient _httpClient { get; set; } = new();
 
-    public async Task<string> ResolveHomeserverFromWellKnown(string homeserver)
-    {
+    public async Task<string> ResolveHomeserverFromWellKnown(string homeserver) {
         var res = await _resolveHomeserverFromWellKnown(homeserver);
-        if(!res.StartsWith("http")) res = "https://" + res;
-        if(res.EndsWith(":443")) res = res.Substring(0, res.Length - 4);
+        if (!res.StartsWith("http")) res = "https://" + res;
+        if (res.EndsWith(":443")) res = res.Substring(0, res.Length - 4);
         return res;
     }
-    private async Task<string> _resolveHomeserverFromWellKnown(string homeserver)
-    {
-        if (RuntimeCache.HomeserverResolutionCache.Count == 0)
-        {
+
+    private async Task<string> _resolveHomeserverFromWellKnown(string homeserver) {
+        if (RuntimeCache.HomeserverResolutionCache.Count == 0) {
             Console.WriteLine("No cached homeservers, resolving...");
             await Task.Delay(Random.Shared.Next(1000, 5000));
-        } 
-        if (RuntimeCache.HomeserverResolutionCache.ContainsKey(homeserver))
-        {
-            if (RuntimeCache.HomeserverResolutionCache[homeserver].ResolutionTime < DateTime.Now.AddHours(1))
-            {
+        }
+
+        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;
             }
+
             Console.WriteLine($"Cached homeserver expired, removing: {RuntimeCache.HomeserverResolutionCache[homeserver].Result}");
             RuntimeCache.HomeserverResolutionCache.Remove(homeserver);
         }
@@ -42,29 +39,25 @@ public class IHomeServer
         string result = null;
         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...");
+        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();
             result = hs;
         }
-        else
-        {
-            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
-                {
+            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!");
@@ -72,30 +65,27 @@ public class IHomeServer
             }
         }
 
-        if (result != null)
-        {
+        if (result != null) {
             Console.WriteLine($"Resolved homeserver: {homeserver} -> {result}");
-            RuntimeCache.HomeserverResolutionCache.TryAdd(homeserver, new()
-            {
+            RuntimeCache.HomeserverResolutionCache.TryAdd(homeserver, new HomeServerResolutionResult {
                 Result = result,
                 ResolutionTime = DateTime.Now
             });
             return result;
         }
+
         throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!");
     }
-    public async Task<ProfileResponse> GetProfile(string mxid, bool debounce = false, bool cache = true)
-    {
-        if (cache)
-        {
-            if(debounce) await Task.Delay(Random.Shared.Next(100, 500));
-            if (_profileCache.ContainsKey(mxid))
-            {
-                while (_profileCache[mxid] == null)
-                {
+
+    public async Task<ProfileResponse> GetProfile(string mxid, bool debounce = false, bool cache = true) {
+        if (cache) {
+            if (debounce) await Task.Delay(Random.Shared.Next(100, 500));
+            if (_profileCache.ContainsKey(mxid)) {
+                while (_profileCache[mxid] == null) {
                     Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson() ?? "null"} within {_profileCache.Count} profiles...");
                     await Task.Delay(Random.Shared.Next(50, 500));
                 }
+
                 return _profileCache[mxid];
             }
         }
@@ -103,13 +93,11 @@ public class IHomeServer
         _profileCache.Add(mxid, null);
         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());
+        if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data);
         var profile = data.Deserialize<ProfileResponse>();
         _profileCache[mxid] = profile;
         return profile;
     }
-    public string? ResolveMediaUri(string mxc)
-    {
-        return mxc.Replace("mxc://", $"{FullHomeServerDomain}/_matrix/media/r0/download/");
-    }
+
+    public string? ResolveMediaUri(string mxc) => mxc.Replace("mxc://", $"{FullHomeServerDomain}/_matrix/media/r0/download/");
 }
\ No newline at end of file