diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-05 06:28:52 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-05 06:28:52 +0200 |
commit | cf455ed8de20bbee011289223e7d8d5775dfd69e (patch) | |
tree | cbdfdbc207af64a105b4d21941a6f0e71ca65e9d /LibMatrix/Services | |
parent | Add start of Media Moderator PoC bot (diff) | |
download | LibMatrix-cf455ed8de20bbee011289223e7d8d5775dfd69e.tar.xz |
Media moderator PoC works, abstract command handling to library
Diffstat (limited to '')
-rw-r--r-- | LibMatrix/Services/HomeserverProviderService.cs | 6 | ||||
-rw-r--r-- | LibMatrix/Services/HomeserverResolverService.cs | 32 | ||||
-rw-r--r-- | LibMatrix/Services/ServiceInstaller.cs | 2 |
3 files changed, 15 insertions, 25 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 776c7eb..71d9860 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -1,11 +1,5 @@ -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; diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index dcd0fe9..f2c0781 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -1,26 +1,16 @@ -using System; -using System.Collections.Generic; -using System.IO; using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; using ArcaneLibs.Extensions; using LibMatrix.Extensions; using Microsoft.Extensions.Logging; namespace LibMatrix.Services; -public class HomeserverResolverService { +public class HomeserverResolverService(ILogger<HomeserverResolverService>? logger) { private readonly MatrixHttpClient _httpClient = new(); - private readonly ILogger<HomeserverResolverService> _logger; private static readonly Dictionary<string, string> _wellKnownCache = new(); private static readonly Dictionary<string, SemaphoreSlim> _wellKnownSemaphores = new(); - public HomeserverResolverService(ILogger<HomeserverResolverService> logger) { - _logger = logger; - } - public async Task<string> ResolveHomeserverFromWellKnown(string homeserver) { var res = await _resolveHomeserverFromWellKnown(homeserver); if (!res.StartsWith("http")) res = "https://" + res; @@ -38,7 +28,7 @@ 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); @@ -46,7 +36,7 @@ public class HomeserverResolverService { if (result is null) throw new InvalidDataException($"Failed to resolve homeserver for {homeserver}! Is it online and configured correctly?"); //success! - _logger.LogInformation("Resolved homeserver: {} -> {}", homeserver, result); + logger?.LogInformation("Resolved homeserver: {} -> {}", homeserver, result); _wellKnownCache[homeserver] = result; sem.Release(); return result; @@ -60,7 +50,7 @@ public class HomeserverResolverService { return hs; } - _logger.LogInformation("No client well-known..."); + logger?.LogInformation("No client well-known..."); return null; } @@ -72,15 +62,15 @@ public class HomeserverResolverService { return hs; } - _logger.LogInformation("No server well-known..."); + logger?.LogInformation("No server well-known..."); return null; } private async Task<string?> _tryCheckIfDomainHasHomeserver(string homeserver) { - _logger.LogInformation("Checking if {} hosts a homeserver...", 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..."); + logger?.LogInformation("No homeserver on shortname..."); return null; } @@ -88,4 +78,12 @@ public class HomeserverResolverService { homeserver = homeserver.Replace("https://", $"https://{subdomain}."); return await _tryCheckIfDomainHasHomeserver(homeserver); } + + public async Task<string?> ResolveMediaUri(string homeserver, string mxc) { + if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + if (mxc is null) throw new ArgumentNullException(nameof(mxc)); + if (!mxc.StartsWith("mxc://")) throw new InvalidDataException("mxc must start with mxc://"); + homeserver = await ResolveHomeserverFromWellKnown(homeserver); + return mxc.Replace("mxc://", $"{homeserver}/_matrix/media/v3/download/"); + } } diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index 9c4cdb9..b1c98e1 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.Services; |