about summary refs log tree commit diff
path: root/LibMatrix/Services
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
commit46df5b8e335754f1582fc4d41d9546808ed8ee66 (patch)
tree29fed832bed495f1bd233c37275cb560c19f34cf /LibMatrix/Services
parentAdd more stuff, add unit tests (diff)
downloadLibMatrix-bak-46df5b8e335754f1582fc4d41d9546808ed8ee66.tar.xz
Unit tests, small refactors
Diffstat (limited to 'LibMatrix/Services')
-rw-r--r--LibMatrix/Services/HomeserverProviderService.cs30
1 files changed, 14 insertions, 16 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs
index 49167fa..666d2a2 100644
--- a/LibMatrix/Services/HomeserverProviderService.cs
+++ b/LibMatrix/Services/HomeserverProviderService.cs
@@ -9,29 +9,29 @@ using Microsoft.Extensions.Logging;
 namespace LibMatrix.Services;
 
 public class HomeserverProviderService {
-    private readonly TieredStorageService _tieredStorageService;
     private readonly ILogger<HomeserverProviderService> _logger;
     private readonly HomeserverResolverService _homeserverResolverService;
 
-    public HomeserverProviderService(TieredStorageService tieredStorageService,
-        ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
-        _tieredStorageService = tieredStorageService;
+    public HomeserverProviderService(ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
         _logger = logger;
         _homeserverResolverService = homeserverResolverService;
-        logger.LogDebug("New HomeserverProviderService created with TieredStorageService<{}>!",
-            string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name)));
     }
 
     private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
     private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeServerCache = new();
 
+    private static Dictionary<string, SemaphoreSlim> _remoteHomeserverSemaphore = new();
+    private static Dictionary<string, RemoteHomeServer> _remoteHomeServerCache = new();
+
     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)) {
-            sem.Release();
-            return _authenticatedHomeServerCache[homeserver + accessToken];
+        lock (_authenticatedHomeServerCache) {
+            if (_authenticatedHomeServerCache.ContainsKey(homeserver + accessToken)) {
+                sem.Release();
+                return _authenticatedHomeServerCache[homeserver + accessToken];
+            }
         }
 
         var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
@@ -45,12 +45,11 @@ public class HomeserverProviderService {
             hs = new AuthenticatedHomeserverGeneric(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"))!;
+        // (() => hs.WhoAmI) = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;
 
         lock(_authenticatedHomeServerCache)
             _authenticatedHomeServerCache[homeserver + accessToken] = hs;
@@ -60,11 +59,10 @@ public class HomeserverProviderService {
     }
 
     public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) {
-        var hs = new RemoteHomeServer(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);
+        var hs = new RemoteHomeServer(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;
     }