about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/RemoteHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-19 00:16:36 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-19 00:16:36 +0200
commitf5447484512d726f4403f0d7725777d0a95601fb (patch)
tree47fe88a4c021f62ff42663f6d8c5229e71d21813 /LibMatrix/Homeservers/RemoteHomeServer.cs
parentImprove README (diff)
downloadLibMatrix-f5447484512d726f4403f0d7725777d0a95601fb.tar.xz
Add more stuff, add unit tests
Diffstat (limited to '')
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs27
1 files changed, 23 insertions, 4 deletions
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs
index caed397..ab3ab51 100644
--- a/LibMatrix/Homeservers/RemoteHomeServer.cs
+++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -1,7 +1,9 @@
 using System.Net.Http.Json;
+using System.Text.Json.Serialization;
+using ArcaneLibs.Extensions;
+using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.Extensions;
 using LibMatrix.Responses;
-using LibMatrix.StateEventTypes.Spec;
 
 namespace LibMatrix.Homeservers;
 
@@ -13,12 +15,13 @@ public class RemoteHomeServer(string canonicalHomeServerDomain) {
     public string FullHomeServerDomain { get; set; }
     public MatrixHttpClient _httpClient { get; set; } = new();
 
-    public async Task<ProfileResponseEventContent> GetProfile(string mxid) {
-        if(mxid is null) throw new ArgumentNullException(nameof(mxid));
+    public async Task<ProfileResponseEventContent> GetProfileAsync(string mxid) {
+        if (mxid is null) throw new ArgumentNullException(nameof(mxid));
         if (_profileCache.TryGetValue(mxid, out var value)) {
             if (value is SemaphoreSlim s) await s.WaitAsync();
             if (value is ProfileResponseEventContent p) return p;
         }
+
         _profileCache[mxid] = new SemaphoreSlim(1);
 
         var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}");
@@ -29,10 +32,26 @@ public class RemoteHomeServer(string canonicalHomeServerDomain) {
         return data;
     }
 
-    public async Task<ClientVersionsResponse> GetClientVersions() {
+    public async Task<ClientVersionsResponse> GetClientVersionsAsync() {
         var resp = await _httpClient.GetAsync($"/_matrix/client/versions");
         var data = await resp.Content.ReadFromJsonAsync<ClientVersionsResponse>();
         if (!resp.IsSuccessStatusCode) Console.WriteLine("ClientVersions: " + data);
         return data;
     }
+
+    public async Task<AliasResult> ResolveRoomAliasAsync(string alias) {
+        var resp = await _httpClient.GetAsync($"/_matrix/client/v3/directory/room/{alias.Replace("#", "%23")}");
+        var data = await resp.Content.ReadFromJsonAsync<AliasResult>();
+        var text = await resp.Content.ReadAsStringAsync();
+        if (!resp.IsSuccessStatusCode) Console.WriteLine("ResolveAlias: " + data.ToJson());
+        return data;
+    }
+}
+
+public class AliasResult {
+    [JsonPropertyName("room_id")]
+    public string RoomId { get; set; } = null!;
+
+    [JsonPropertyName("servers")]
+    public List<string> Servers { get; set; } = null!;
 }