about summary refs log tree commit diff
path: root/LibMatrix/Interfaces/IHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-08-14 04:09:13 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-08-14 04:09:13 +0200
commit0d0511e35d9965fc0ea5190ae3347c3d77c3334c (patch)
treeb4e0c336cbccc37bd14952b447868a577ea15540 /LibMatrix/Interfaces/IHomeServer.cs
downloadLibMatrix-0d0511e35d9965fc0ea5190ae3347c3d77c3334c.tar.xz
Split LibMatrix into separate repo
Diffstat (limited to 'LibMatrix/Interfaces/IHomeServer.cs')
-rw-r--r--LibMatrix/Interfaces/IHomeServer.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/LibMatrix/Interfaces/IHomeServer.cs b/LibMatrix/Interfaces/IHomeServer.cs
new file mode 100644
index 0000000..5e7e374
--- /dev/null
+++ b/LibMatrix/Interfaces/IHomeServer.cs
@@ -0,0 +1,29 @@
+using System.Net.Http.Json;
+using LibMatrix.Extensions;
+using LibMatrix.StateEventTypes.Spec;
+
+namespace LibMatrix.Interfaces;
+
+public class IHomeServer {
+    private readonly Dictionary<string, object> _profileCache = new();
+    public string HomeServerDomain { get; set; }
+    public string FullHomeServerDomain { get; set; }
+
+    public MatrixHttpClient _httpClient { get; set; } = new();
+
+    public async Task<ProfileResponseEventData> GetProfile(string mxid) {
+        if(mxid is null) throw new ArgumentNullException(nameof(mxid));
+        if (_profileCache.ContainsKey(mxid)) {
+            if (_profileCache[mxid] is SemaphoreSlim s) await s.WaitAsync();
+            if (_profileCache[mxid] is ProfileResponseEventData p) return p;
+        }
+        _profileCache[mxid] = new SemaphoreSlim(1);
+
+        var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}");
+        var data = await resp.Content.ReadFromJsonAsync<ProfileResponseEventData>();
+        if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data);
+        _profileCache[mxid] = data;
+
+        return data;
+    }
+}