diff options
Diffstat (limited to 'LibMatrix/Services')
-rw-r--r-- | LibMatrix/Services/HomeserverProviderService.cs | 18 | ||||
-rw-r--r-- | LibMatrix/Services/HomeserverResolverService.cs | 6 | ||||
-rw-r--r-- | LibMatrix/Services/ServiceInstaller.cs | 11 | ||||
-rw-r--r-- | LibMatrix/Services/TieredStorageService.cs | 2 |
4 files changed, 19 insertions, 18 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 5ac47f1..7a13816 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -47,9 +47,9 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger sem.Release(); throw; } - + try { - if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out bool a) && a) + if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out var a) && a) hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverMxApiExtended>(homeserver, accessToken, proxy); else { if (serverVersion is { Server.Name: "Synapse" }) @@ -64,11 +64,13 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger throw; } - if(impersonatedMxid is not null) + if (impersonatedMxid is not null) await hs.SetImpersonate(impersonatedMxid); - - lock (AuthenticatedHomeserverCache) + + lock (AuthenticatedHomeserverCache) { AuthenticatedHomeserverCache[cacheKey] = hs; + } + sem.Release(); return hs; @@ -88,10 +90,12 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger hs = await RemoteHomeserver.Create(homeserver, proxy); - lock (RemoteHomeserverCache) + lock (RemoteHomeserverCache) { RemoteHomeserverCache[cacheKey] = hs; + } + sem.Release(); - + return hs; } diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 9f937c5..a4a18e5 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -16,7 +16,7 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge public async Task<WellKnownUris> ResolveHomeserverFromWellKnown(string homeserver) { if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); - WellKnownSemaphores.TryAdd(homeserver, new(1, 1)); + WellKnownSemaphores.TryAdd(homeserver, new SemaphoreSlim(1, 1)); await WellKnownSemaphores[homeserver].WaitAsync(); if (WellKnownCache.TryGetValue(homeserver, out var known)) { WellKnownSemaphores[homeserver].Release(); @@ -53,7 +53,7 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge try { var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server"); var hs = resp.GetProperty("m.server").GetString(); - if(hs is null) throw new InvalidDataException("m.server is null"); + if (hs is null) throw new InvalidDataException("m.server is null"); if (!hs.StartsWithAnyOf("http://", "https://")) hs = $"https://{hs}"; return hs; @@ -83,4 +83,4 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge public string? Client { get; set; } public string? Server { get; set; } } -} +} \ No newline at end of file diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index 358dc2a..0f07b61 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -3,11 +3,10 @@ using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.Services; public static class ServiceInstaller { - public static IServiceCollection AddRoryLibMatrixServices(this IServiceCollection services, RoryLibMatrixConfiguration? config = null) { //Check required services // if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) - // throw new Exception("[RMUCore/DI] No TieredStorageService has been registered!"); + // throw new Exception("[RMUCore/DI] No TieredStorageService has been registered!"); //Add config services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); @@ -15,19 +14,17 @@ public static class ServiceInstaller { services.AddSingleton<HomeserverResolverService>(); // if (services.First(x => x.ServiceType == typeof(TieredStorageService)).Lifetime == ServiceLifetime.Singleton) { - services.AddSingleton<HomeserverProviderService>(); + services.AddSingleton<HomeserverProviderService>(); // } // else { - // services.AddScoped<HomeserverProviderService>(); + // services.AddScoped<HomeserverProviderService>(); // } // services.AddScoped<MatrixHttpClient>(); return services; } - - } public class RoryLibMatrixConfiguration { public string AppName { get; set; } = "Rory&::LibMatrix"; -} +} \ No newline at end of file diff --git a/LibMatrix/Services/TieredStorageService.cs b/LibMatrix/Services/TieredStorageService.cs index 280340e..9e411de 100644 --- a/LibMatrix/Services/TieredStorageService.cs +++ b/LibMatrix/Services/TieredStorageService.cs @@ -5,4 +5,4 @@ namespace LibMatrix.Services; public class TieredStorageService(IStorageProvider? cacheStorageProvider, IStorageProvider? dataStorageProvider) { public IStorageProvider? CacheStorageProvider { get; } = cacheStorageProvider; public IStorageProvider? DataStorageProvider { get; } = dataStorageProvider; -} +} \ No newline at end of file |