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/HomeserverResolverService.cs | |
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 'LibMatrix/Services/HomeserverResolverService.cs')
-rw-r--r-- | LibMatrix/Services/HomeserverResolverService.cs | 32 |
1 files changed, 15 insertions, 17 deletions
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/"); + } } |