diff options
Diffstat (limited to 'MatrixRoomUtils.Core/Interfaces/IHomeServer.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Interfaces/IHomeServer.cs | 26 |
1 files changed, 26 insertions, 0 deletions
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 |