about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-02-03 19:06:08 +0100
committerRory& <root@rory.gay>2025-02-03 19:06:08 +0100
commit040c244d35ff42b59993c85a5110d4e7aedc0237 (patch)
treed3d42436cc40a2f09b8dec87624ef45ea9acd2d7
parentThe rest of warning cleanup so far. (diff)
downloadLibMatrix-040c244d35ff42b59993c85a5110d4e7aedc0237.tar.xz
Some code cleanup
Diffstat (limited to '')
-rw-r--r--LibMatrix.EventTypes/LibMatrix.EventTypes.csproj2
-rw-r--r--LibMatrix/LibMatrix.csproj2
-rw-r--r--LibMatrix/Responses/CreateRoomRequest.cs1
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs1
-rw-r--r--LibMatrix/Services/HomeserverProviderService.cs10
-rw-r--r--LibMatrix/Services/HomeserverResolverService.cs27
-rw-r--r--LibMatrix/Utilities/CommonSyncFilters.cs1
-rw-r--r--Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs1
-rw-r--r--Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs7
-rw-r--r--Tests/LibMatrix.Tests/Tests/RoomTests/BasicRoomEventTests/RoomNameTests.cs1
-rw-r--r--Tests/LibMatrix.Tests/Tests/RoomTests/RoomMembershipTests.cs1
-rw-r--r--Tests/LibMatrix.Tests/Tests/RoomTests/RoomTests.cs1
-rw-r--r--Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs1
-rw-r--r--Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs1
-rw-r--r--Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj6
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs2
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs13
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs3
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs1
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj2
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Program.cs8
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs18
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs16
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs6
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs28
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs10
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs1
32 files changed, 63 insertions, 113 deletions
diff --git a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj

index aaa37e4..b8c4132 100644 --- a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj +++ b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj
@@ -7,7 +7,7 @@ </PropertyGroup> <ItemGroup> - <PackageReference Include="ArcaneLibs" Version="1.0.0-preview.20241122-053825" Condition="'$(Configuration)' == 'Release'"/> + <PackageReference Include="ArcaneLibs" Version="1.0.0-preview.20250123-182609" Condition="'$(Configuration)' == 'Release'"/> <ProjectReference Include="..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" Condition="'$(Configuration)' == 'Debug'"/> </ItemGroup> diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj
index 1fdf560..c074e81 100644 --- a/LibMatrix/LibMatrix.csproj +++ b/LibMatrix/LibMatrix.csproj
@@ -18,7 +18,7 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="ArcaneLibs" Version="1.0.0-preview.20241122-053825" Condition="'$(Configuration)' == 'Release'"/> + <PackageReference Include="ArcaneLibs" Version="1.0.0-preview.20250123-182609" Condition="'$(Configuration)' == 'Release'"/> <ProjectReference Include="..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" Condition="'$(Configuration)' == 'Debug'"/> </ItemGroup> diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs
index 741bea1..1da6ff3 100644 --- a/LibMatrix/Responses/CreateRoomRequest.cs +++ b/LibMatrix/Responses/CreateRoomRequest.cs
@@ -3,7 +3,6 @@ using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using LibMatrix.EventTypes; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Homeservers; diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index 14276d7..b68a891 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -7,7 +7,6 @@ using System.Web; using ArcaneLibs.Extensions; using LibMatrix.EventTypes; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Filters; using LibMatrix.Helpers; diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs
index 104a244..0fa0e83 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs
@@ -7,9 +7,9 @@ using Microsoft.Extensions.Logging; namespace LibMatrix.Services; public class HomeserverProviderService(ILogger<HomeserverProviderService> logger, HomeserverResolverService hsResolver) { - private static SemaphoreCache<AuthenticatedHomeserverGeneric> AuthenticatedHomeserverCache = new(); - private static SemaphoreCache<RemoteHomeserver> RemoteHomeserverCache = new(); - private static SemaphoreCache<FederationClient> FederationClientCache = new(); + private static readonly SemaphoreCache<AuthenticatedHomeserverGeneric> AuthenticatedHomeserverCache = new(); + private static readonly SemaphoreCache<RemoteHomeserver> RemoteHomeserverCache = new(); + private static readonly SemaphoreCache<FederationClient> FederationClientCache = new(); public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null, string? impersonatedMxid = null, bool useGeneric = false, bool enableClient = true, bool enableServer = true) { @@ -22,7 +22,7 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger AuthenticatedHomeserverGeneric? hs = null; if (!useGeneric) { - ClientVersionsResponse? clientVersions = new(); + ClientVersionsResponse clientVersions = new(); try { clientVersions = await rhs.GetClientVersionsAsync(); } @@ -64,7 +64,7 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger }); } - public async Task<RemoteHomeserver> GetRemoteHomeserver(string homeserver, string? proxy = null, bool useCache = true, bool enableServer = true) => + public async Task<RemoteHomeserver> GetRemoteHomeserver(string homeserver, string? proxy = null, bool useCache = true, bool enableServer = false) => useCache ? await RemoteHomeserverCache.GetOrAdd($"{homeserver}{proxy}", async () => { return new RemoteHomeserver(homeserver, await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableServer: enableServer), proxy); }) diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs
index ddbc335..fa75f1e 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -9,9 +9,7 @@ using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Services; public class HomeserverResolverService { - private readonly MatrixHttpClient _httpClient = new() { - // Timeout = TimeSpan.FromSeconds(60) // TODO: Re-implement this - }; + private readonly MatrixHttpClient _httpClient = new(); private static readonly SemaphoreCache<WellKnownUris> WellKnownCache = new(); @@ -22,49 +20,31 @@ public class HomeserverResolverService { if (logger is NullLogger<HomeserverResolverService>) { var stackFrame = new StackTrace(true).GetFrame(1); Console.WriteLine( - $"WARN | Null logger provided to HomeserverResolverService!\n{stackFrame.GetMethod().DeclaringType} at {stackFrame.GetFileName()}:{stackFrame.GetFileLineNumber()}"); + $"WARN | Null logger provided to HomeserverResolverService!\n{stackFrame?.GetMethod()?.DeclaringType?.ToString() ?? "null"} at {stackFrame?.GetFileName() ?? "null"}:{stackFrame?.GetFileLineNumber().ToString() ?? "null"}"); } } - // private static SemaphoreSlim _wellKnownSemaphore = new(1, 1); - public async Task<WellKnownUris> ResolveHomeserverFromWellKnown(string homeserver, bool enableClient = true, bool enableServer = true) { ArgumentNullException.ThrowIfNull(homeserver); return await WellKnownCache.GetOrAdd(homeserver, async () => { - // await _wellKnownSemaphore.WaitAsync(); _logger.LogTrace($"Resolving homeserver well-knowns: {homeserver}"); var client = enableClient ? _tryResolveClientEndpoint(homeserver) : null; var server = enableServer ? _tryResolveServerEndpoint(homeserver) : null; var res = new WellKnownUris(); - // try { if (client != null) res.Client = (await client)?.TrimEnd('/') ?? throw new Exception($"Could not resolve client URL for {homeserver}."); - // } - // catch (Exception e) { - // _logger.LogError(e, "Error resolving client well-known for {hs}", homeserver); - // } - // try { if (server != null) res.Server = (await server)?.TrimEnd('/') ?? throw new Exception($"Could not resolve server URL for {homeserver}."); - // } - // catch (Exception e) { - // _logger.LogError(e, "Error resolving server well-known for {hs}", homeserver); - // } _logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false)); - // _wellKnownSemaphore.Release(); return res; }); } - // private async Task<WellKnownUris> InternalResolveHomeserverFromWellKnown(string homeserver) { - - // } - private async Task<string?> _tryResolveClientEndpoint(string homeserver) { ArgumentNullException.ThrowIfNull(homeserver); _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); @@ -133,7 +113,8 @@ public class HomeserverResolverService { _logger.LogInformation("No server well-known for {server}...", homeserver); return null; } - + + [Obsolete("Use authenticated media, available on AuthenticatedHomeserverGeneric", true)] 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)); diff --git a/LibMatrix/Utilities/CommonSyncFilters.cs b/LibMatrix/Utilities/CommonSyncFilters.cs
index 65c9564..503cc1f 100644 --- a/LibMatrix/Utilities/CommonSyncFilters.cs +++ b/LibMatrix/Utilities/CommonSyncFilters.cs
@@ -1,6 +1,5 @@ using System.Collections.Frozen; using LibMatrix.EventTypes.Common; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.EventTypes.Spec.State.Space; using LibMatrix.Filters; diff --git a/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs b/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs
index 0caac5c..88c0353 100644 --- a/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs +++ b/Tests/LibMatrix.Tests/Abstractions/RoomAbstraction.cs
@@ -1,5 +1,4 @@ using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.EventTypes.Spec.State.Space; using LibMatrix.Homeservers; diff --git a/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs b/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs
index ef2426d..7226fe9 100644 --- a/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs +++ b/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs
@@ -33,11 +33,4 @@ public class HomeserverResolverTests : TestBed<TestFixture> { }).ToList(); await Task.WhenAll(tasks); } - - [Fact] - public async Task ResolveMedia() { - var media = await _resolver.ResolveMediaUri("matrix.org", "mxc://matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo"); - - Assert.Equal("https://matrix-client.matrix.org/_matrix/media/v3/download/matrix.org/eqwrRZRoPpNbcMeUwyXAuVRo", media); - } } \ No newline at end of file diff --git a/Tests/LibMatrix.Tests/Tests/RoomTests/BasicRoomEventTests/RoomNameTests.cs b/Tests/LibMatrix.Tests/Tests/RoomTests/BasicRoomEventTests/RoomNameTests.cs
index 1afda84..7f53b08 100644 --- a/Tests/LibMatrix.Tests/Tests/RoomTests/BasicRoomEventTests/RoomNameTests.cs +++ b/Tests/LibMatrix.Tests/Tests/RoomTests/BasicRoomEventTests/RoomNameTests.cs
@@ -1,4 +1,3 @@ -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Tests.Abstractions; using LibMatrix.Tests.Fixtures; diff --git a/Tests/LibMatrix.Tests/Tests/RoomTests/RoomMembershipTests.cs b/Tests/LibMatrix.Tests/Tests/RoomTests/RoomMembershipTests.cs
index 6699b9d..e0784c4 100644 --- a/Tests/LibMatrix.Tests/Tests/RoomTests/RoomMembershipTests.cs +++ b/Tests/LibMatrix.Tests/Tests/RoomTests/RoomMembershipTests.cs
@@ -1,5 +1,4 @@ using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Homeservers; using LibMatrix.Tests.Abstractions; diff --git a/Tests/LibMatrix.Tests/Tests/RoomTests/RoomTests.cs b/Tests/LibMatrix.Tests/Tests/RoomTests/RoomTests.cs
index 113730a..fa9812f 100644 --- a/Tests/LibMatrix.Tests/Tests/RoomTests/RoomTests.cs +++ b/Tests/LibMatrix.Tests/Tests/RoomTests/RoomTests.cs
@@ -1,7 +1,6 @@ using System.Text; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Responses; using LibMatrix.Tests.Abstractions; diff --git a/Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs b/Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs
index 21e9fa9..148fb70 100644 --- a/Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs +++ b/Tests/LibMatrix.Tests/Tests/RoomTests/SpaceTests.cs
@@ -1,5 +1,4 @@ using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.Space; using LibMatrix.Responses; using LibMatrix.RoomTypes; diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs
index b1df152..c650e2b 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs
@@ -1,7 +1,6 @@ using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.ExampleBot.Bot.Interfaces; using LibMatrix.Helpers; diff --git a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj
index ba8d08f..3f4c9d8 100644 --- a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj +++ b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj
@@ -18,13 +18,15 @@ </PropertyGroup> <ItemGroup> + <PackageReference Include="ArcaneLibs.StringNormalisation" Version="1.0.0-preview.20250123-182609" Condition="'$(Configuration)' == 'Release'"/> + <ProjectReference Include="..\..\ArcaneLibs\ArcaneLibs.StringNormalisation\ArcaneLibs.StringNormalisation.csproj" Condition="'$(Configuration)' == 'Debug'"/> <ProjectReference Include="..\..\LibMatrix\LibMatrix.csproj"/> </ItemGroup> <ItemGroup> - <PackageReference Include="ArcaneLibs.StringNormalisation" Version="1.0.0-1.0.0-preview.20241122-053825"/> - <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" /> + <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1"/> </ItemGroup> + <ItemGroup> <Content Include="appsettings*.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs
index 4e6efa5..4f10601 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs
@@ -7,7 +7,7 @@ namespace LibMatrix.HomeserverEmulator.Controllers; [ApiController] [Route("/_matrix/client/{version}/")] -public class AuthController(ILogger<AuthController> logger, UserStore userStore, TokenService tokenService, HSEConfiguration config) : ControllerBase { +public class AuthController(ILogger<AuthController> logger, UserStore userStore, TokenService tokenService, HseConfiguration config) : ControllerBase { [HttpPost("login")] public async Task<LoginResponse> Login(LoginRequest request) { if (!request.Identifier.User.StartsWith('@')) diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
index 9b61929..2e232d9 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
@@ -1,5 +1,4 @@ using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Services; using LibMatrix.Homeservers; diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs
index 3826c39..245770e 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs
@@ -1,7 +1,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Services; using Microsoft.AspNetCore.Mvc; diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs
index 81b7440..6048bbb 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs
@@ -3,6 +3,7 @@ using System.Text.RegularExpressions; using LibMatrix.HomeserverEmulator.Services; using LibMatrix.Services; using Microsoft.AspNetCore.Mvc; +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member namespace LibMatrix.HomeserverEmulator.Controllers.Media; @@ -12,12 +13,12 @@ public class MediaController( ILogger<MediaController> logger, TokenService tokenService, UserStore userStore, - HSEConfiguration cfg, + HseConfiguration cfg, HomeserverResolverService hsResolver, MediaStore mediaStore) : ControllerBase { [HttpPost("upload")] - public async Task<object> UploadMedia([FromHeader(Name = "Content-Type")] string ContentType, [FromQuery] string filename, [FromBody] Stream file) { + public async Task<object> UploadMedia([FromHeader(Name = "Content-Type")] string contentType, [FromQuery] string filename, [FromBody] Stream file) { var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { @@ -75,7 +76,9 @@ public class MediaController( if (cfg.StoreData) { var path = Path.Combine(cfg.DataStoragePath, "media", serverName, mediaId); if (!System.IO.File.Exists(path)) { - var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + // var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + var homeserver = (await hsResolver.ResolveHomeserverFromWellKnown(serverName)).Client; + var mediaUrl = homeserver is null ? null : $"{homeserver}/_matrix/media/v3/download/"; if (mediaUrl is null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", @@ -90,7 +93,9 @@ public class MediaController( return new FileStream(path, FileMode.Open); } else { - var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + // var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + var homeserver = (await hsResolver.ResolveHomeserverFromWellKnown(serverName)).Client; + var mediaUrl = homeserver is null ? null : $"{homeserver}/_matrix/media/v3/download/"; if (mediaUrl is null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
index c854d0b..6c57cc4 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
@@ -1,4 +1,3 @@ -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Services; using Microsoft.AspNetCore.Mvc; diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs
index 5c81c3a..b6ec6bf 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs
@@ -3,7 +3,6 @@ using System.Text.Json.Nodes; using ArcaneLibs; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.EventTypes.Spec.State.Space; using LibMatrix.Helpers; diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs
index 0571d5a..9dae2e5 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs
@@ -1,5 +1,4 @@ using System.Text.Json.Serialization; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Services; using LibMatrix.Responses; diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs
index aef56fc..cfab3a6 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs
@@ -1,7 +1,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Extensions; using LibMatrix.HomeserverEmulator.Services; @@ -12,7 +11,7 @@ namespace LibMatrix.HomeserverEmulator.Controllers; [ApiController] [Route("/_matrix/client/{version}/")] -public class SyncController(ILogger<SyncController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore, HSEConfiguration cfg) : ControllerBase { +public class SyncController(ILogger<SyncController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore, HseConfiguration cfg) : ControllerBase { [HttpGet("sync")] [SuppressMessage("ReSharper.DPA", "DPA0011: High execution time of MVC action", Justification = "Endpoint is expected to wait until data is available or timeout.")] public async Task<SyncResponse> Sync([FromQuery] string? since = null, [FromQuery] int? timeout = 5000) { diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs
index e99992a..40f3667 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs
@@ -1,5 +1,4 @@ using System.Text.Json.Serialization; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Services; using Microsoft.AspNetCore.Mvc; diff --git a/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj
index 4b0530b..2218fb2 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj +++ b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj
@@ -6,7 +6,7 @@ <ImplicitUsings>enable</ImplicitUsings> <InvariantGlobalization>true</InvariantGlobalization> <LangVersion>preview</LangVersion> - <GenerateDocumentationFile>true</GenerateDocumentationFile> +<!-- <GenerateDocumentationFile>true</GenerateDocumentationFile>--> </PropertyGroup> <ItemGroup> diff --git a/Utilities/LibMatrix.HomeserverEmulator/Program.cs b/Utilities/LibMatrix.HomeserverEmulator/Program.cs
index 0a50c80..c72df5a 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Program.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Program.cs
@@ -31,7 +31,7 @@ builder.Services.AddSwaggerGen(c => { }); builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); -builder.Services.AddSingleton<HSEConfiguration>(); +builder.Services.AddSingleton<HseConfiguration>(); builder.Services.AddSingleton<UserStore>(); builder.Services.AddSingleton<RoomStore>(); builder.Services.AddSingleton<MediaStore>(); @@ -76,7 +76,7 @@ app.UseExceptionHandler(exceptionHandlerApp => { var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>(); if (exceptionHandlerPathFeature?.Error is not null) - Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!); + Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()); if (exceptionHandlerPathFeature?.Error is MatrixException mxe) { context.Response.StatusCode = mxe.ErrorCode switch { @@ -85,14 +85,14 @@ app.UseExceptionHandler(exceptionHandlerApp => { _ => StatusCodes.Status500InternalServerError }; context.Response.ContentType = MediaTypeNames.Application.Json; - await context.Response.WriteAsync(mxe.GetAsJson()!); + await context.Response.WriteAsync(mxe.GetAsJson()); } else { context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.ContentType = MediaTypeNames.Application.Json; await context.Response.WriteAsync(new MatrixException() { ErrorCode = "M_UNKNOWN", - Error = exceptionHandlerPathFeature?.Error.ToString() + Error = exceptionHandlerPathFeature?.Error?.ToString() ?? "Unknown error" }.GetAsJson()); } }); diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
index bcfb629..04ce050 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
@@ -4,12 +4,12 @@ using ArcaneLibs.Extensions; namespace LibMatrix.HomeserverEmulator.Services; -public class HSEConfiguration { - private static ILogger<HSEConfiguration> _logger; - public static HSEConfiguration Current { get; set; } +public class HseConfiguration { + private static ILogger<HseConfiguration> _logger; + public static HseConfiguration Current { get; set; } [RequiresUnreferencedCode("Uses reflection binding")] - public HSEConfiguration(ILogger<HSEConfiguration> logger, IConfiguration config, HostBuilderContext host) { + public HseConfiguration(ILogger<HseConfiguration> logger, IConfiguration config, HostBuilderContext host) { Current = this; _logger = logger; logger.LogInformation("Loading configuration for environment: {}...", host.HostingEnvironment.EnvironmentName); @@ -22,15 +22,15 @@ public class HSEConfiguration { _logger.LogInformation("Configuration loaded: {}", this.ToJson()); } - public string CacheStoragePath { get; set; } + public required string CacheStoragePath { get; set; } - public string DataStoragePath { get; set; } + public required string DataStoragePath { get; set; } - public bool StoreData { get; set; } = true; + public required bool StoreData { get; set; } = true; - public string ServerName { get; set; } = "localhost"; + public required string ServerName { get; set; } = "localhost"; - public bool UnknownSyncTokenIsInitialSync { get; set; } = true; + public required bool UnknownSyncTokenIsInitialSync { get; set; } = true; private static string ExpandPath(string path, bool retry = true) { _logger.LogInformation("Expanding path `{}`", path); diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs
index 00f2a42..7945d3a 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs
@@ -4,18 +4,18 @@ using LibMatrix.Services; namespace LibMatrix.HomeserverEmulator.Services; public class MediaStore { - private readonly HSEConfiguration _config; + private readonly HseConfiguration _config; private readonly HomeserverResolverService _hsResolver; - private List<MediaInfo> index = new(); + private List<MediaInfo> _mediaIndex = new(); - public MediaStore(HSEConfiguration config, HomeserverResolverService hsResolver) { + public MediaStore(HseConfiguration config, HomeserverResolverService hsResolver) { _config = config; _hsResolver = hsResolver; if (config.StoreData) { var path = Path.Combine(config.DataStoragePath, "media"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); if (File.Exists(Path.Combine(path, "index.json"))) - index = JsonSerializer.Deserialize<List<MediaInfo>>(File.ReadAllText(Path.Combine(path, "index.json"))); + _mediaIndex = JsonSerializer.Deserialize<List<MediaInfo>>(File.ReadAllText(Path.Combine(path, "index.json"))); } else Console.WriteLine("Data storage is disabled, not loading rooms from disk"); @@ -36,7 +36,9 @@ public class MediaStore { if (_config.StoreData) { var path = Path.Combine(_config.DataStoragePath, "media", serverName, mediaId); if (!File.Exists(path)) { - var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + // var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + var homeserver = (await _hsResolver.ResolveHomeserverFromWellKnown(serverName)).Client; + var mediaUrl = homeserver is null ? null : $"{homeserver}/_matrix/media/v3/download/"; if (mediaUrl is null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", @@ -50,7 +52,9 @@ public class MediaStore { return new FileStream(path, FileMode.Open); } else { - var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + // var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + var homeserver = (await _hsResolver.ResolveHomeserverFromWellKnown(serverName)).Client; + var mediaUrl = homeserver is null ? null : $"{homeserver}/_matrix/media/v3/download/"; if (mediaUrl is null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs
index 0128ba6..0603a2d 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs
@@ -35,15 +35,15 @@ public class PaginationTokenResolverService(ILogger<PaginationTokenResolverServi } } - public async Task<StateEventResponse?> ResolveTokenToEvent(string token, RoomStore.Room room) { + public Task<StateEventResponse?> ResolveTokenToEvent(string token, RoomStore.Room room) { if (token.StartsWith('$')) { //we have an event ID logger.LogTrace("ResolveTokenToEvent(EventId({token}), Room({room})): searching for event...", token, room.RoomId); var evt = room.Timeline.SingleOrDefault(x => x.EventId == token); - if (evt is not null) return evt; + if (evt is not null) return Task.FromResult(evt); logger.LogTrace("ResolveTokenToEvent({token}, Room({room})): event not in requested room...", token, room.RoomId); - return null; + return Task.FromResult<StateEventResponse?>(null); } else { // we have a sync token diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs
index 8b35b3a..2f5fa32 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs
@@ -7,7 +7,6 @@ using System.Text.Json.Nodes; using ArcaneLibs; using ArcaneLibs.Collections; using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.HomeserverEmulator.Controllers.Rooms; using LibMatrix.Responses; @@ -19,7 +18,7 @@ public class RoomStore { public ConcurrentBag<Room> _rooms = new(); private FrozenDictionary<string, Room> _roomsById = FrozenDictionary<string, Room>.Empty; - public RoomStore(ILogger<RoomStore> logger, HSEConfiguration config) { + public RoomStore(ILogger<RoomStore> logger, HseConfiguration config) { _logger = logger; if (config.StoreData) { var path = Path.Combine(config.DataStoragePath, "rooms"); @@ -34,26 +33,9 @@ public class RoomStore { RebuildIndexes(); } - - private SemaphoreSlim a = new(1, 1); + private void RebuildIndexes() { - // a.Wait(); - // lock (_roomsById) - // _roomsById = new ConcurrentDictionary<string, Room>(_rooms.ToDictionary(u => u.RoomId)); - // foreach (var room in _rooms) { - // _roomsById.AddOrUpdate(room.RoomId, room, (key, old) => room); - // } - // - // var roomsArr = _rooms.ToArray(); - // foreach (var (id, room) in _roomsById) { - // if (!roomsArr.Any(x => x.RoomId == id)) - // _roomsById.TryRemove(id, out _); - // } - - // _roomsById = new ConcurrentDictionary<string, Room>(_rooms.ToDictionary(u => u.RoomId)); _roomsById = _rooms.ToFrozenDictionary(u => u.RoomId); - - // a.Release(); } public Room? GetRoomById(string roomId, bool createIfNotExists = false) { @@ -64,14 +46,14 @@ public class RoomStore { if (!createIfNotExists) return null; - return CreateRoom(new() { }); + return CreateRoom(new()); } public Room CreateRoom(CreateRoomRequest request, UserStore.User? user = null) { var room = new Room(roomId: $"!{Guid.NewGuid().ToString()}"); var newCreateEvent = new StateEvent() { Type = RoomCreateEventContent.EventId, - RawContent = new() { } + RawContent = new() }; foreach (var (key, value) in request.CreationContent) { @@ -246,7 +228,7 @@ public class RoomStore { Task.Run(async () => { await saveSemaphore.WaitAsync(); try { - var path = Path.Combine(HSEConfiguration.Current.DataStoragePath, "rooms", $"{RoomId}.json"); + var path = Path.Combine(HseConfiguration.Current.DataStoragePath, "rooms", $"{RoomId}.json"); Console.WriteLine($"Saving room {RoomId} to {path}!"); await File.WriteAllTextAsync(path, this.ToJson(ignoreNull: true)); } diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs
index 1e08d28..d1b0a30 100644 --- a/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs
@@ -12,14 +12,14 @@ namespace LibMatrix.HomeserverEmulator.Services; public class UserStore { public ConcurrentBag<User> _users = new(); - private readonly HSEConfiguration _config; + private readonly HseConfiguration _config; private readonly RoomStore _roomStore; - public UserStore(HSEConfiguration config, RoomStore roomStore) { + public UserStore(HseConfiguration config, RoomStore roomStore) { _config = config; _roomStore = roomStore; if (config.StoreData) { - var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users"); + var dataDir = Path.Combine(HseConfiguration.Current.DataStoragePath, "users"); if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); foreach (var userId in Directory.GetDirectories(dataDir)) { var tokensDir = Path.Combine(dataDir, userId, "tokens.json"); @@ -207,12 +207,12 @@ public class UserStore { public bool IsGuest { get; set; } public async Task SaveDebounced() { - if (!HSEConfiguration.Current.StoreData) return; + if (!HseConfiguration.Current.StoreData) return; await _debounceCts.CancelAsync(); _debounceCts = new CancellationTokenSource(); try { await Task.Delay(250, _debounceCts.Token); - var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users", _userId); + var dataDir = Path.Combine(HseConfiguration.Current.DataStoragePath, "users", _userId); if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); var tokensDir = Path.Combine(dataDir, "tokens.json"); var path = Path.Combine(dataDir, $"user.json"); diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
index 1639b88..b5b5a2b 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
@@ -1,5 +1,4 @@ using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Filters; using LibMatrix.Helpers;