From 91319ba62de889bde645b6f1df4dd6a960ee7de4 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 21 Sep 2025 15:49:54 +0200 Subject: Dependency updates, some fixes, partial msc2545 support --- ArcaneLibs | 2 +- .../Msc2545EmoteRoomsAccountDataEventContent.cs | 31 ++++++++++ LibMatrix/Extensions/MatrixHttpClient.Single.cs | 48 +++++++++++++-- LibMatrix/Helpers/RoomBuilder.cs | 10 ++- LibMatrix/LibMatrix.csproj | 4 +- LibMatrix/RoomTypes/GenericRoom.cs | 12 ++++ LibMatrix/Services/HomeserverProviderService.cs | 2 +- LibMatrix/Services/HomeserverResolverService.cs | 4 +- .../LibMatrix.DebugDataValidationApi.csproj | 4 +- .../Bot/StartupTasks/ServerRoomSizeCalulator.cs | 2 +- .../LibMatrix.DevTestBot.csproj | 72 +++++++++++----------- .../LibMatrix.E2eeTestKit.csproj | 46 +++++++------- .../LibMatrix.TestDataGenerator.csproj | 2 +- .../LibMatrix.Utilities.Bot.csproj | 6 +- 14 files changed, 168 insertions(+), 77 deletions(-) create mode 100644 LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs diff --git a/ArcaneLibs b/ArcaneLibs index f0f37be..c8d11c9 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit f0f37be308ce87f55c7299f08c8ce5077b057c89 +Subproject commit c8d11c9c6f7aa2a0eb86c24c1a7bbdad4928a3ec diff --git a/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs b/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs new file mode 100644 index 0000000..4fd5c29 --- /dev/null +++ b/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec; + +[MatrixEvent(EventName = EventId)] +public class Msc2545EmoteRoomsAccountDataEventContent : EventContent { + public const string EventId = "im.ponies.emote_rooms"; + + [JsonPropertyName("rooms")] + public Dictionary> Rooms { get; set; } = new(); + + // Dummy type to provide easy access to the by-spec empty content + public class EnabledEmotePackEntry { + [JsonExtensionData] + public Dictionary? AdditionalData { get; set; } = []; + + public T? GetAdditionalData(string key) where T : class { + if (AdditionalData == null || !AdditionalData.TryGetValue(key, out var value)) + return null; + + if (value is T tValue) + return tValue; + if (value is JsonElement jsonElement) + return jsonElement.Deserialize(); + + throw new InvalidCastException($"Value for key '{key}' ({value.GetType()}) cannot be cast to type '{typeof(T)}'. Cannot continue."); + } + } +} \ No newline at end of file diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs index 26fb31f..aa188dd 100644 --- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs +++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs @@ -55,6 +55,10 @@ public class MatrixHttpClient { public Dictionary AdditionalQueryParameters { get; set; } = new(); public Uri? BaseAddress { get; set; } + public bool RetryOnNetworkError { get; set; } = true; + public bool RetryOnMatrixError { get; set; } = true; + + private Dictionary _retries = []; // default headers, not bound to client public HttpRequestHeaders DefaultRequestHeaders { get; set; } = @@ -151,8 +155,37 @@ public class MatrixHttpClient { } public async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) { - var responseMessage = await SendUnhandledAsync(request, cancellationToken); - if (responseMessage.IsSuccessStatusCode) return responseMessage; + _retries.TryAdd(request, 10); + HttpResponseMessage responseMessage; + try { + responseMessage = await SendUnhandledAsync(request, cancellationToken); + } + catch (HttpRequestException ex) { + if (RetryOnNetworkError) { + if (_retries[request]-- <= 0) throw; + if (ex.InnerException?.GetType().FullName == "System.Runtime.InteropServices.JavaScript.JSException") + Console.WriteLine("Got JSException, likely a CORS error due to a reverse proxy misconfiguration and error, retrying..."); + else + Console.WriteLine(new { + ex.HttpRequestError, + ex.StatusCode, + ex.Data, + ex.Message, + InnerException = ex.InnerException?.ToString(), + InnerExceptionType = ex.InnerException?.GetType().FullName + }.ToJson()); + + await Task.Delay(Random.Shared.Next(1000, 2000), cancellationToken); + request.ResetSendStatus(); + return await SendAsync(request, cancellationToken); + } + throw; + } + + if (responseMessage.IsSuccessStatusCode) { + _retries.Remove(request); + return responseMessage; + } //retry on gateway timeout // if (responseMessage.StatusCode == HttpStatusCode.GatewayTimeout) { @@ -192,14 +225,19 @@ public class MatrixHttpClient { request.ResetSendStatus(); return await SendAsync(request, cancellationToken); } + throw ex; } if (responseMessage.StatusCode == HttpStatusCode.BadGateway) { // spread out retries - await Task.Delay(Random.Shared.Next(1000, 2000), cancellationToken); - request.ResetSendStatus(); - return await SendAsync(request, cancellationToken); + if (RetryOnNetworkError) { + if (_retries[request]-- <= 0) throw new InvalidDataException("Encountered invalid data:\n" + content); + Console.WriteLine("Got 502 Bad Gateway, retrying..."); + await Task.Delay(Random.Shared.Next(1000, 2000), cancellationToken); + request.ResetSendStatus(); + return await SendAsync(request, cancellationToken); + } } responseMessage.EnsureSuccessStatusCode(); diff --git a/LibMatrix/Helpers/RoomBuilder.cs b/LibMatrix/Helpers/RoomBuilder.cs index 0b0b5f8..6e24103 100644 --- a/LibMatrix/Helpers/RoomBuilder.cs +++ b/LibMatrix/Helpers/RoomBuilder.cs @@ -83,7 +83,15 @@ public class RoomBuilder { { RoomPowerLevelEventContent.EventId, 100 }, { RoomServerAclEventContent.EventId, 100 }, { RoomTombstoneEventContent.EventId, 150 }, - { RoomPolicyServerEventContent.EventId, 100 } + { RoomPolicyServerEventContent.EventId, 100 }, + { RoomPinnedEventContent.EventId, 50 }, + // recommended extensions + { "im.vector.modular.widgets", 50}, + // { "m.reaction", 0 }, // we probably don't want these to end up as room state + // - prevent calls + { "io.element.voice_broadcast_info", 50 }, + { "org.matrix.msc3401.call", 50 }, + { "org.matrix.msc3401.call.member", 50 }, } }; diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj index c1049b5..c6c1dce 100644 --- a/LibMatrix/LibMatrix.csproj +++ b/LibMatrix/LibMatrix.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 0077acb..58e5434 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -704,6 +704,18 @@ public class GenericRoom { } public async Task> GetHomeserversInRoom() => (await GetMemberIdsListAsync("join")).Select(x => x.Split(':', 2)[1]).Distinct().ToList(); + + public async Task IsJoinedAsync() { + try { + var member = await GetStateOrNullAsync(RoomMemberEventContent.EventId, Homeserver.UserId); + return member?.Membership == "join"; + } + catch (MatrixException e) { + if (e.ErrorCode == "M_NOT_FOUND") return false; + if (e.ErrorCode == "M_FORBIDDEN") return false; + throw; + } + } } public class RoomIdResponse { diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index c984c34..52aadd2 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -17,7 +17,7 @@ public class HomeserverProviderService(ILogger logger if (!enableClient && !enableServer) throw new ArgumentException("At least one of enableClient or enableServer must be true"); - return await AuthenticatedHomeserverCache.GetOrAdd($"{homeserver}{accessToken}{proxy}{impersonatedMxid}", async () => { + return await AuthenticatedHomeserverCache.GetOrAdd($"{homeserver}{accessToken}{proxy}{impersonatedMxid}{useGeneric}{enableClient}{enableServer}", async () => { var wellKnownUris = await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableClient, enableServer); var rhs = new RemoteHomeserver(homeserver, wellKnownUris, proxy); diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 94a3826..7d30b7b 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -9,7 +9,9 @@ using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Services; public class HomeserverResolverService { - private readonly MatrixHttpClient _httpClient = new(); + private readonly MatrixHttpClient _httpClient = new() { + RetryOnNetworkError = false + }; private static readonly SemaphoreCache WellKnownCache = new(); diff --git a/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj index 92da98f..1e61742 100644 --- a/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj +++ b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index fdc7717..1339f23 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -42,7 +42,7 @@ public class ServerRoomSizeCalulator : IHostedService { var roomSize = stateList.Count; if (roomSize > 10000) await File.AppendAllLinesAsync("large_rooms.txt", new[] { $"{{ \"{room.RoomId}\", {roomSize} }}," }, cancellationToken); - var roomHs = room.RoomId.Split(":")[1]; + var roomHs = await room.GetOriginHomeserverAsync(); if (totalRoomSize.ContainsKey(roomHs)) totalRoomSize[roomHs] += roomSize; else diff --git a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj index 8a0107b..9225e74 100644 --- a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj +++ b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj @@ -1,36 +1,36 @@ - - - - Exe - net9.0 - preview - enable - enable - false - true - LibMatrix.ExampleBot - - - - - - - - - - - - - - - - - - - - - - Always - - - + + + + Exe + net9.0 + preview + enable + enable + false + true + LibMatrix.ExampleBot + + + + + + + + + + + + + + + + + + + + + + Always + + + diff --git a/Utilities/LibMatrix.E2eeTestKit/LibMatrix.E2eeTestKit.csproj b/Utilities/LibMatrix.E2eeTestKit/LibMatrix.E2eeTestKit.csproj index 0da4c48..100e1de 100644 --- a/Utilities/LibMatrix.E2eeTestKit/LibMatrix.E2eeTestKit.csproj +++ b/Utilities/LibMatrix.E2eeTestKit/LibMatrix.E2eeTestKit.csproj @@ -1,23 +1,23 @@ - - - - net9.0 - enable - enable - service-worker-assets.js - - - - - - - - - - - - - - - - + + + + net9.0 + enable + enable + service-worker-assets.js + + + + + + + + + + + + + + + + diff --git a/Utilities/LibMatrix.TestDataGenerator/LibMatrix.TestDataGenerator.csproj b/Utilities/LibMatrix.TestDataGenerator/LibMatrix.TestDataGenerator.csproj index 93de8ab..0eea87a 100644 --- a/Utilities/LibMatrix.TestDataGenerator/LibMatrix.TestDataGenerator.csproj +++ b/Utilities/LibMatrix.TestDataGenerator/LibMatrix.TestDataGenerator.csproj @@ -17,7 +17,7 @@ - + diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj index 615808d..bd4adc5 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj @@ -12,9 +12,9 @@ - - - + + + -- cgit 1.5.1