diff options
Diffstat (limited to 'LibMatrix/Homeservers/RemoteHomeServer.cs')
-rw-r--r-- | LibMatrix/Homeservers/RemoteHomeServer.cs | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index a8d0326..0757f6e 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -9,19 +9,25 @@ using LibMatrix.Services; namespace LibMatrix.Homeservers; -public class RemoteHomeServer(string baseUrl) { - public static async Task<RemoteHomeServer> Create(string baseUrl) => - new(baseUrl) { - _httpClient = new() { - BaseAddress = new Uri(await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl) - ?? throw new InvalidOperationException("Failed to resolve homeserver")), +public class RemoteHomeserver(string baseUrl) { + public static async Task<RemoteHomeserver> Create(string baseUrl) { + var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl); + return new RemoteHomeserver(baseUrl) { + ClientHttpClient = new() { + BaseAddress = new Uri(urls.client ?? throw new InvalidOperationException("Failed to resolve homeserver")), + Timeout = TimeSpan.FromSeconds(120) + }, + ServerHttpClient = new() { + BaseAddress = new Uri(urls.server ?? throw new InvalidOperationException("Failed to resolve homeserver")), Timeout = TimeSpan.FromSeconds(120) } }; + } private Dictionary<string, object> _profileCache { get; set; } = new(); public string BaseUrl { get; } = baseUrl; - public MatrixHttpClient _httpClient { get; set; } + public MatrixHttpClient ClientHttpClient { get; set; } + public MatrixHttpClient ServerHttpClient { get; set; } public async Task<UserProfileResponse> GetProfileAsync(string mxid) { if (mxid is null) throw new ArgumentNullException(nameof(mxid)); @@ -32,7 +38,7 @@ public class RemoteHomeServer(string baseUrl) { _profileCache[mxid] = new SemaphoreSlim(1); - var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}"); + var resp = await ClientHttpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}"); var data = await resp.Content.ReadFromJsonAsync<UserProfileResponse>(); if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data); _profileCache[mxid] = data; @@ -41,14 +47,14 @@ public class RemoteHomeServer(string baseUrl) { } public async Task<ClientVersionsResponse> GetClientVersionsAsync() { - var resp = await _httpClient.GetAsync($"/_matrix/client/versions"); + var resp = await ClientHttpClient.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 resp = await ClientHttpClient.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()); @@ -58,7 +64,7 @@ public class RemoteHomeServer(string baseUrl) { #region Authentication public async Task<LoginResponse> LoginAsync(string username, string password, string? deviceName = null) { - var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/login", new { + var resp = await ClientHttpClient.PostAsJsonAsync("/_matrix/client/r0/login", new { type = "m.login.password", identifier = new { type = "m.id.user", @@ -73,7 +79,7 @@ public class RemoteHomeServer(string baseUrl) { } public async Task<LoginResponse> RegisterAsync(string username, string password, string? deviceName = null) { - var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/register", new { + var resp = await ClientHttpClient.PostAsJsonAsync("/_matrix/client/r0/register", new { kind = "user", auth = new { type = "m.login.dummy" @@ -90,6 +96,24 @@ public class RemoteHomeServer(string baseUrl) { } #endregion + + public async Task<ServerVersionResponse> GetServerVersionAsync() { + return await ServerHttpClient.GetFromJsonAsync<ServerVersionResponse>("/_matrix/federation/v1/version"); + } +} + +public class ServerVersionResponse { + + [JsonPropertyName("server")] + public ServerInfo Server { get; set; } + + public class ServerInfo { + [JsonPropertyName("name")] + public string Name { get; set; } + + [JsonPropertyName("version")] + public string Version { get; set; } + } } public class AliasResult { |