diff options
author | Emma@Rory& <root@rory.gay> | 2023-07-26 21:02:50 +0200 |
---|---|---|
committer | Emma@Rory& <root@rory.gay> | 2023-07-26 21:02:50 +0200 |
commit | 2e89a0717a60598904c92499adb7e01b2075fbb9 (patch) | |
tree | 56b9b7f07180154d9d98dafa91d7c16a0a872100 /MatrixRoomUtils.Core/Services/HomeserverProviderService.cs | |
parent | Start of nix flake (diff) | |
download | MatrixUtils-2e89a0717a60598904c92499adb7e01b2075fbb9.tar.xz |
MRU desktop start
Diffstat (limited to 'MatrixRoomUtils.Core/Services/HomeserverProviderService.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Services/HomeserverProviderService.cs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs index b2ea987..4bc785a 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs @@ -23,8 +23,18 @@ public class HomeserverProviderService { $"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, AuthenticatedHomeServer> _authenticatedHomeServerCache = new(); + public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken, string? overrideFullDomain = null) { + SemaphoreSlim sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1)); + await sem.WaitAsync(); + if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) { + sem.Release(); + return _authenticatedHomeServerCache[homeserver+accessToken]; + } + var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken); hs.FullHomeServerDomain = overrideFullDomain ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); @@ -34,6 +44,10 @@ public class HomeserverProviderService { hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; + + _authenticatedHomeServerCache[homeserver+accessToken] = hs; + sem.Release(); + return hs; } @@ -80,4 +94,4 @@ public class HomeserverProviderService { public string User { get; set; } = ""; } } -} \ No newline at end of file +} |