From cf455ed8de20bbee011289223e7d8d5775dfd69e Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 5 Sep 2023 06:28:52 +0200 Subject: Media moderator PoC works, abstract command handling to library --- LibMatrix/Services/HomeserverResolverService.cs | 32 ++++++++++++------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'LibMatrix/Services/HomeserverResolverService.cs') 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? logger) { private readonly MatrixHttpClient _httpClient = new(); - private readonly ILogger _logger; private static readonly Dictionary _wellKnownCache = new(); private static readonly Dictionary _wellKnownSemaphores = new(); - public HomeserverResolverService(ILogger logger) { - _logger = logger; - } - public async Task 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 _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 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/"); + } } -- cgit 1.4.1