From cb8846a7a3310f8513989da5aadb5202f048a1b3 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 14 Aug 2023 19:46:11 +0200 Subject: Code cleanup --- LibMatrix/Services/HomeserverProviderService.cs | 9 ++++----- LibMatrix/Services/HomeserverResolverService.cs | 21 ++++++++++----------- LibMatrix/Services/ServiceInstaller.cs | 16 ++++++++++------ 3 files changed, 24 insertions(+), 22 deletions(-) (limited to 'LibMatrix/Services') diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 61c449a..366f0ca 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -14,12 +14,11 @@ public class HomeserverProviderService { public HomeserverProviderService(TieredStorageService tieredStorageService, ILogger logger, HomeserverResolverService homeserverResolverService) { - Console.WriteLine("Homeserver provider service instantiated!"); _tieredStorageService = tieredStorageService; _logger = logger; _homeserverResolverService = homeserverResolverService; - logger.LogDebug( - $"New HomeserverProviderService created with TieredStorageService<{string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))}>!"); + logger.LogDebug("New HomeserverProviderService created with TieredStorageService<{}>!", + string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))); } private static Dictionary _authenticatedHomeserverSemaphore = new(); @@ -27,7 +26,7 @@ public class HomeserverProviderService { public async Task GetAuthenticatedWithToken(string homeserver, string accessToken, string? overrideFullDomain = null) { - SemaphoreSlim sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1)); + var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) { sem.Release(); @@ -64,7 +63,7 @@ public class HomeserverProviderService { string? overrideFullDomain = null) { var hs = await GetRemoteHomeserver(homeserver, overrideFullDomain); var payload = new LoginRequest { - Identifier = new() { User = user }, + Identifier = new LoginRequest.LoginIdentifier { User = user }, Password = password }; var resp = await hs._httpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload); diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 4d3bc46..78c5c37 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -18,13 +18,13 @@ public class HomeserverResolverService { public async Task ResolveHomeserverFromWellKnown(string homeserver) { var res = await _resolveHomeserverFromWellKnown(homeserver); if (!res.StartsWith("http")) res = "https://" + res; - if (res.EndsWith(":443")) res = res.Substring(0, res.Length - 4); + if (res.EndsWith(":443")) res = res[..^4]; return res; } private async Task _resolveHomeserverFromWellKnown(string homeserver) { if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); - SemaphoreSlim sem = _wellKnownSemaphores.GetOrCreate(homeserver, _ => new SemaphoreSlim(1, 1)); + var sem = _wellKnownSemaphores.GetOrCreate(homeserver, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); if (_wellKnownCache.ContainsKey(homeserver)) { sem.Release(); @@ -32,19 +32,18 @@ public class HomeserverResolverService { } string? result = null; - _logger.LogInformation($"Attempting to resolve homeserver: {homeserver}"); + _logger.LogInformation("Attempting to resolve homeserver: {}", homeserver); result ??= await _tryResolveFromClientWellknown(homeserver); result ??= await _tryResolveFromServerWellknown(homeserver); result ??= await _tryCheckIfDomainHasHomeserver(homeserver); - if (result is not null) { - _logger.LogInformation($"Resolved homeserver: {homeserver} -> {result}"); - _wellKnownCache[homeserver] = result; - sem.Release(); - return result; - } + if (result is null) throw new InvalidDataException($"Failed to resolve homeserver for {homeserver}! Is it online and configured correctly?"); - throw new InvalidDataException($"Failed to resolve homeserver for {homeserver}! Is it online and configured correctly?"); + //success! + _logger.LogInformation("Resolved homeserver: {} -> {}", homeserver, result); + _wellKnownCache[homeserver] = result; + sem.Release(); + return result; } private async Task _tryResolveFromClientWellknown(string homeserver) { @@ -72,7 +71,7 @@ public class HomeserverResolverService { } private async Task _tryCheckIfDomainHasHomeserver(string homeserver) { - _logger.LogInformation($"Checking if {homeserver} hosts a homeserver..."); + _logger.LogInformation("Checking if {} hosts a homeserver...", homeserver); if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) return homeserver; _logger.LogInformation("No homeserver on shortname..."); diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index 96a1963..b1c98e1 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -9,14 +9,18 @@ public static class ServiceInstaller { if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) throw new Exception("[MRUCore/DI] No TieredStorageService has been registered!"); //Add config - if(config is not null) - services.AddSingleton(config); - else { - services.AddSingleton(new RoryLibMatrixConfiguration()); - } + services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); + //Add services - services.AddSingleton(); services.AddSingleton(); + + if (services.First(x => x.ServiceType == typeof(TieredStorageService)).Lifetime == ServiceLifetime.Singleton) { + services.AddSingleton(); + } + else { + services.AddScoped(); + } + // services.AddScoped(); return services; } -- cgit 1.4.1