about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 20:34:16 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 20:34:16 +0200
commit383f7b633471dedf515907cb8a8752bc5885ae64 (patch)
tree793225216c9c38be1533e1960166b7b3dd472890 /MatrixRoomUtils.Core
parentDark theme, fancier room list (diff)
downloadMatrixUtils-383f7b633471dedf515907cb8a8752bc5885ae64.tar.xz
Add room manager, profile caching
Diffstat (limited to 'MatrixRoomUtils.Core')
-rw-r--r--MatrixRoomUtils.Core/Authentication/MatrixAuth.cs1
-rw-r--r--MatrixRoomUtils.Core/Interfaces/IHomeServer.cs26
-rw-r--r--MatrixRoomUtils.Core/RemoteHomeServer.cs9
-rw-r--r--MatrixRoomUtils.Core/Room.cs3
4 files changed, 31 insertions, 8 deletions
diff --git a/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs b/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs
index e744c4f..7bcd75f 100644
--- a/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs
+++ b/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs
@@ -39,6 +39,7 @@ public class MatrixAuth
         //return token;
     }
 
+    [Obsolete("Migrate to IHomeServer instance")]
     public static async Task<ProfileResponse> GetProfile(string homeserver, string mxid) => 
         await (await new RemoteHomeServer(homeserver).Configure()).GetProfile(mxid);
 
diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
index 438709f..c6d788c 100644
--- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
+++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs
@@ -1,11 +1,13 @@
 using System.Net.Http.Json;
 using System.Text.Json;
 using MatrixRoomUtils.Core.Extensions;
+using MatrixRoomUtils.Core.Responses;
 
 namespace MatrixRoomUtils.Core.Interfaces;
 
 public class IHomeServer
 {
+    private Dictionary<string, ProfileResponse?> _profileCache = new();
     public string HomeServerDomain { get; set; }
     public string FullHomeServerDomain { get; set; }
 
@@ -69,4 +71,28 @@ public class IHomeServer
         }
         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)
+                {
+                    Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson()} within {_profileCache.Count} profiles...");
+                    await Task.Delay(Random.Shared.Next(50, 500));
+                }
+                return _profileCache[mxid];
+            }
+        }
+
+        _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());
+        var profile = data.Deserialize<ProfileResponse>();
+        _profileCache[mxid] = profile;
+        return profile;
+    }
 }
\ No newline at end of file
diff --git a/MatrixRoomUtils.Core/RemoteHomeServer.cs b/MatrixRoomUtils.Core/RemoteHomeServer.cs
index 1acea89..9c096c8 100644
--- a/MatrixRoomUtils.Core/RemoteHomeServer.cs
+++ b/MatrixRoomUtils.Core/RemoteHomeServer.cs
@@ -7,6 +7,8 @@ namespace MatrixRoomUtils.Core;
 
 public class RemoteHomeServer : IHomeServer
 {
+
+
     public RemoteHomeServer(string canonicalHomeServerDomain)
     {
         HomeServerDomain = canonicalHomeServerDomain;
@@ -21,13 +23,6 @@ public class RemoteHomeServer : IHomeServer
 
         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)
     {
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index d5eee2b..be470fa 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -33,10 +33,11 @@ public class Room
         var res = await GetStateAsync("m.room.name");
         if (!res.HasValue)
         {
+            Console.WriteLine($"Room {RoomId} has no name!");
             return null;
         }
         var resn = res?.TryGetProperty("name", out var name) ?? false ? name.GetString() : null;
-        Console.WriteLine($"Got name: {resn}");
+        //Console.WriteLine($"Got name: {resn}");
         return resn;
     }