about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-04-05 18:58:32 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2024-04-05 18:58:32 +0200
commit37b97d65c0a5262539a5de560e911048166b8bba (patch)
treef704a9c703b0ec47122a460576e151e0cb06fdc6 /LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
parentFix merge conficts between machines (diff)
downloadLibMatrix-37b97d65c0a5262539a5de560e911048166b8bba.tar.xz
Fix homeserver resolution, rewrite homeserver initialisation, HSE work
Diffstat (limited to '')
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs48
1 files changed, 17 insertions, 31 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
index 727c0ea..afa6a6c 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
@@ -14,49 +14,35 @@ using LibMatrix.Responses;
 using LibMatrix.RoomTypes;
 using LibMatrix.Services;
 using LibMatrix.Utilities;
+using Microsoft.Extensions.Logging.Abstractions;
 
 namespace LibMatrix.Homeservers;
 
-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 =>
-        await Create(typeof(T), serverName, accessToken, proxy) as T ?? throw new InvalidOperationException($"Failed to create instance of {typeof(T).Name}");
-
-    public static async Task<AuthenticatedHomeserverGeneric> Create(Type type, string serverName, string accessToken, string? proxy = null) {
-        if (string.IsNullOrWhiteSpace(proxy))
-            proxy = null;
-        if (!type.IsAssignableTo(typeof(AuthenticatedHomeserverGeneric))) throw new ArgumentException("Type must be a subclass of AuthenticatedHomeserverGeneric", nameof(type));
-        var instance = Activator.CreateInstance(type, serverName, accessToken) as AuthenticatedHomeserverGeneric
-                       ?? throw new InvalidOperationException($"Failed to create instance of {type.Name}");
-
-        instance.ClientHttpClient = new MatrixHttpClient {
-            Timeout = TimeSpan.FromMinutes(15),
-            DefaultRequestHeaders = {
-                Authorization = new AuthenticationHeaderValue("Bearer", accessToken)
-            }
-        };
-        instance.FederationClient = await FederationClient.TryCreate(serverName, proxy);
+public class AuthenticatedHomeserverGeneric : RemoteHomeserver {
+    public AuthenticatedHomeserverGeneric(string serverName, HomeserverResolverService.WellKnownUris wellKnownUris, ref string? proxy, string accessToken) : base(serverName,
+        wellKnownUris, ref proxy) {
+        AccessToken = accessToken;
+        ClientHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
 
-        if (string.IsNullOrWhiteSpace(proxy)) {
-            var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(serverName);
-            instance.ClientHttpClient.BaseAddress = new Uri(urls?.Client ?? throw new InvalidOperationException("Failed to resolve homeserver"));
-        }
-        else {
-            instance.ClientHttpClient.BaseAddress = new Uri(proxy);
-            instance.ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", serverName);
-        }
+        NamedCaches = new HsNamedCaches(this);
+    }
+
+    public async Task Initialise() {
+        WhoAmI = await ClientHttpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami");
+    }
 
-        instance.WhoAmI = await instance.ClientHttpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami");
-        instance.NamedCaches = new HsNamedCaches(instance);
+    private WhoAmIResponse? _whoAmI;
 
-        return instance;
+    public WhoAmIResponse WhoAmI {
+        get => _whoAmI ?? throw new Exception("Initialise was not called or awaited, WhoAmI is null!");
+        private set => _whoAmI = value;
     }
 
-    public WhoAmIResponse WhoAmI { get; set; }
     public string UserId => WhoAmI.UserId;
     public string UserLocalpart => UserId.Split(":")[0][1..];
     public string ServerName => UserId.Split(":", 2)[1];
 
-    public string AccessToken { get; set; } = accessToken;
+    public string AccessToken { get; set; }
 
     public HsNamedCaches NamedCaches { get; set; } = null!;