diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-11-05 18:17:11 +0100 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-11-05 18:17:11 +0100 |
commit | 1d6399d7f4649472333da946669ce9f1fa349b89 (patch) | |
tree | 95baedad8e42956de6d4d339d5c4fdc44ae2396b /LibMatrix/Homeservers/RemoteHomeServer.cs | |
parent | split client and server http client for homeservers (diff) | |
download | LibMatrix-1d6399d7f4649472333da946669ce9f1fa349b89.tar.xz |
Cleanup, fixes, fix proxy support
Diffstat (limited to '')
-rw-r--r-- | LibMatrix/Homeservers/RemoteHomeServer.cs | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index 0757f6e..55a3a02 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -10,24 +10,31 @@ using LibMatrix.Services; namespace LibMatrix.Homeservers; 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) - } + public static async Task<RemoteHomeserver> Create(string baseUrl, string? proxy = null) { + var homeserver = new RemoteHomeserver(baseUrl); + homeserver.WellKnownUris = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl); + homeserver.ClientHttpClient = new() { + BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Client ?? throw new InvalidOperationException("Failed to resolve homeserver")), + Timeout = TimeSpan.FromSeconds(120) + }; + homeserver.ServerHttpClient = new() { + BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Server ?? throw new InvalidOperationException("Failed to resolve homeserver")), + Timeout = TimeSpan.FromSeconds(120) }; + + if (proxy is not null) { + homeserver.ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); + homeserver.ServerHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); + } + + return homeserver; } private Dictionary<string, object> _profileCache { get; set; } = new(); public string BaseUrl { get; } = baseUrl; public MatrixHttpClient ClientHttpClient { get; set; } public MatrixHttpClient ServerHttpClient { get; set; } + public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } public async Task<UserProfileResponse> GetProfileAsync(string mxid) { if (mxid is null) throw new ArgumentNullException(nameof(mxid)); @@ -100,6 +107,13 @@ public class RemoteHomeserver(string baseUrl) { public async Task<ServerVersionResponse> GetServerVersionAsync() { return await ServerHttpClient.GetFromJsonAsync<ServerVersionResponse>("/_matrix/federation/v1/version"); } + + + public string? ResolveMediaUri(string? mxcUri) { + if (mxcUri is null) return null; + if (mxcUri.StartsWith("https://")) return mxcUri; + return $"{ClientHttpClient.BaseAddress}/_matrix/media/v3/download/{mxcUri.Replace("mxc://", "")}".Replace("//_matrix", "/_matrix"); + } } public class ServerVersionResponse { |