From f5447484512d726f4403f0d7725777d0a95601fb Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 19 Sep 2023 00:16:36 +0200 Subject: Add more stuff, add unit tests --- LibMatrix/Homeservers/RemoteHomeServer.cs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'LibMatrix/Homeservers/RemoteHomeServer.cs') 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 GetProfile(string mxid) { - if(mxid is null) throw new ArgumentNullException(nameof(mxid)); + public async Task 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 GetClientVersions() { + public async Task GetClientVersionsAsync() { var resp = await _httpClient.GetAsync($"/_matrix/client/versions"); var data = await resp.Content.ReadFromJsonAsync(); if (!resp.IsSuccessStatusCode) Console.WriteLine("ClientVersions: " + data); return data; } + + public async Task ResolveRoomAliasAsync(string alias) { + var resp = await _httpClient.GetAsync($"/_matrix/client/v3/directory/room/{alias.Replace("#", "%23")}"); + var data = await resp.Content.ReadFromJsonAsync(); + 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 Servers { get; set; } = null!; } -- cgit 1.4.1