about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverProviderService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/Services/HomeserverProviderService.cs')
-rw-r--r--LibMatrix/Services/HomeserverProviderService.cs74
1 files changed, 33 insertions, 41 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs
index 366f0ca..776c7eb 100644
--- a/LibMatrix/Services/HomeserverProviderService.cs
+++ b/LibMatrix/Services/HomeserverProviderService.cs
@@ -1,7 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Net.Http.Headers;
 using System.Net.Http.Json;
 using System.Text.Json.Serialization;
+using System.Threading;
+using System.Threading.Tasks;
+using ArcaneLibs.Extensions;
 using LibMatrix.Extensions;
+using LibMatrix.Homeservers;
 using LibMatrix.Responses;
 using Microsoft.Extensions.Logging;
 
@@ -22,46 +29,53 @@ public class HomeserverProviderService {
     }
 
     private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
-    private static Dictionary<string, AuthenticatedHomeServer> _authenticatedHomeServerCache = new();
+    private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeServerCache = new();
 
-    public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken,
-        string? overrideFullDomain = null) {
-        var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1));
+    public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken,
+        string? proxy = null) {
+        var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver + accessToken, _ => new SemaphoreSlim(1, 1));
         await sem.WaitAsync();
-        if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) {
+        if (_authenticatedHomeServerCache.ContainsKey(homeserver + accessToken)) {
             sem.Release();
-            return _authenticatedHomeServerCache[homeserver+accessToken];
+            return _authenticatedHomeServerCache[homeserver + accessToken];
         }
 
-        var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken);
-        hs.FullHomeServerDomain = overrideFullDomain ??
-                                  await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
-        hs._httpClient.Dispose();
-        hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) };
-        hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
+        var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
+        var hc = new MatrixHttpClient { BaseAddress = new Uri(domain) };
+
+        AuthenticatedHomeserverGeneric hs;
+        if (true) {
+            hs = new AuthenticatedHomeserverMxApiExtended(_tieredStorageService, homeserver, accessToken);
+        }
+        else {
+            hs = new AuthenticatedHomeserverGeneric(_tieredStorageService, homeserver, accessToken);
+        }
+
+        hs.FullHomeServerDomain = domain;
+        hs._httpClient = hc;
+        hs._httpClient.Timeout = TimeSpan.FromMinutes(15);
         hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
 
         hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;
 
-        _authenticatedHomeServerCache[homeserver+accessToken] = hs;
+        lock(_authenticatedHomeServerCache)
+            _authenticatedHomeServerCache[homeserver + accessToken] = hs;
         sem.Release();
 
         return hs;
     }
 
-    public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? overrideFullDomain = null) {
+    public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) {
         var hs = new RemoteHomeServer(homeserver);
-        hs.FullHomeServerDomain = overrideFullDomain ??
-                                  await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
+        hs.FullHomeServerDomain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
         hs._httpClient.Dispose();
         hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) };
         hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
         return hs;
     }
 
-    public async Task<LoginResponse> Login(string homeserver, string user, string password,
-        string? overrideFullDomain = null) {
-        var hs = await GetRemoteHomeserver(homeserver, overrideFullDomain);
+    public async Task<LoginResponse> Login(string homeserver, string user, string password, string? proxy = null) {
+        var hs = await GetRemoteHomeserver(homeserver, proxy);
         var payload = new LoginRequest {
             Identifier = new LoginRequest.LoginIdentifier { User = user },
             Password = password
@@ -70,26 +84,4 @@ public class HomeserverProviderService {
         var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
         return data!;
     }
-
-    private class LoginRequest {
-        [JsonPropertyName("type")]
-        public string Type { get; set; } = "m.login.password";
-
-        [JsonPropertyName("identifier")]
-        public LoginIdentifier Identifier { get; set; } = new();
-
-        [JsonPropertyName("password")]
-        public string Password { get; set; } = "";
-
-        [JsonPropertyName("initial_device_display_name")]
-        public string InitialDeviceDisplayName { get; set; } = "Rory&::LibMatrix";
-
-        public class LoginIdentifier {
-            [JsonPropertyName("type")]
-            public string Type { get; set; } = "m.id.user";
-
-            [JsonPropertyName("user")]
-            public string User { get; set; } = "";
-        }
-    }
 }