about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-11-05 18:17:11 +0100
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-11-05 18:17:11 +0100
commit1d6399d7f4649472333da946669ce9f1fa349b89 (patch)
tree95baedad8e42956de6d4d339d5c4fdc44ae2396b /LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
parentsplit client and server http client for homeservers (diff)
downloadLibMatrix-1d6399d7f4649472333da946669ce9f1fa349b89.tar.xz
Cleanup, fixes, fix proxy support
Diffstat (limited to 'LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs')
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs46
1 files changed, 19 insertions, 27 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
index c3684a1..37696eb 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
@@ -13,43 +13,41 @@ using LibMatrix.Services;
 
 namespace LibMatrix.Homeservers;
 
-public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) : RemoteHomeserver(baseUrl) {
-    public static async Task<T> Create<T>(string baseUrl, string accessToken) where T : AuthenticatedHomeserverGeneric {
-        var instance = Activator.CreateInstance(typeof(T), baseUrl, accessToken) as T
+public class AuthenticatedHomeserverGeneric(string serverName, string accessToken) : RemoteHomeserver(serverName) {
+    public static async Task<T> Create<T>(string serverName, string accessToken, string? proxy = null) where T : AuthenticatedHomeserverGeneric {
+        var instance = Activator.CreateInstance(typeof(T), serverName, accessToken) as T
                        ?? throw new InvalidOperationException($"Failed to create instance of {typeof(T).Name}");
-        var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl);
-        
+        HomeserverResolverService.WellKnownUris? urls = null;
+        if(proxy is null)
+            urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(serverName);
+
         instance.ClientHttpClient = new() {
-            BaseAddress = new Uri(urls.client
-                                  ?? throw new InvalidOperationException("Failed to resolve homeserver")),
+            BaseAddress = new Uri(proxy ?? urls?.Client
+                ?? throw new InvalidOperationException("Failed to resolve homeserver")),
             Timeout = TimeSpan.FromMinutes(15),
             DefaultRequestHeaders = {
                 Authorization = new AuthenticationHeaderValue("Bearer", accessToken)
             }
         };
         instance.ServerHttpClient = new() {
-            BaseAddress = new Uri(urls.server
-                                  ?? throw new InvalidOperationException("Failed to resolve homeserver")),
+            BaseAddress = new Uri(proxy ?? urls?.Server
+                ?? throw new InvalidOperationException("Failed to resolve homeserver")),
             Timeout = TimeSpan.FromMinutes(15),
             DefaultRequestHeaders = {
                 Authorization = new AuthenticationHeaderValue("Bearer", accessToken)
             }
         };
+
         instance.WhoAmI = await instance.ClientHttpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami");
+
+        if (proxy is not null) {
+            instance.ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", serverName);
+            instance.ServerHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", serverName);
+        }
+
         return instance;
     }
 
-    // Activator.CreateInstance(baseUrl, accessToken) {
-    //     _httpClient = new() {
-    //         BaseAddress = new Uri(await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl)
-    //                               ?? throw new InvalidOperationException("Failed to resolve homeserver")),
-    //         Timeout = TimeSpan.FromMinutes(15),
-    //         DefaultRequestHeaders = {
-    //             Authorization = new AuthenticationHeaderValue("Bearer", accessToken)
-    //         }
-    //     }
-    // };
-
     public WhoAmIResponse? WhoAmI { get; set; }
     public string? UserId => WhoAmI?.UserId;
     public string? UserLocalpart => UserId?.Split(":")[0][1..];
@@ -176,12 +174,6 @@ public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken)
 
 #endregion
 
-    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 async Task UpdateProfileAsync(UserProfileResponse? newProfile, bool preserveCustomRoomProfile = true) {
         if (newProfile is null) return;
         Console.WriteLine($"Updating profile for {WhoAmI.UserId} to {newProfile.ToJson(ignoreNull: true)} (preserving room profiles: {preserveCustomRoomProfile})");
@@ -247,7 +239,7 @@ public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken)
             if (sync.Rooms is null) break;
             List<Task> tasks = new();
             foreach (var (roomId, roomData) in sync.Rooms.Join) {
-                if (roomData.State is { Events: { Count: > 0 } }) {
+                if (roomData.State is { Events.Count: > 0 }) {
                     var incommingRoomProfile =
                         roomData.State?.Events?.FirstOrDefault(x => x.Type == "m.room.member" && x.StateKey == WhoAmI.UserId)?.TypedContent as RoomMemberEventContent;
                     if (incommingRoomProfile is null) continue;