From bb0c00531dfacd30ecef1ac4444c2e3281fa36b8 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 22 Nov 2024 06:38:42 +0100 Subject: Extra draupnir/policy event work --- ArcaneLibs | 2 +- .../Interop/Draupnir/DraupnirProtectedRoomsData.cs | 11 +++++++++++ .../Spec/State/Policy/PolicyRuleStateEventContent.cs | 9 +++++++++ LibMatrix/Helpers/SyncHelper.cs | 8 ++++++-- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 LibMatrix.EventTypes/Interop/Draupnir/DraupnirProtectedRoomsData.cs diff --git a/ArcaneLibs b/ArcaneLibs index 3e8e7ec..18b71ba 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit 3e8e7ecc626e9472f975f27ac65c1406061d5aee +Subproject commit 18b71bae063c6b49e74926bbd8094535a282d8fd diff --git a/LibMatrix.EventTypes/Interop/Draupnir/DraupnirProtectedRoomsData.cs b/LibMatrix.EventTypes/Interop/Draupnir/DraupnirProtectedRoomsData.cs new file mode 100644 index 0000000..1917239 --- /dev/null +++ b/LibMatrix.EventTypes/Interop/Draupnir/DraupnirProtectedRoomsData.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Interop.Draupnir; + +[MatrixEvent(EventName = EventId)] +public class DraupnirProtectedRoomsData : EventContent { + public const string EventId = "org.matrix.mjolnir.protected_rooms"; + + [JsonPropertyName("rooms")] + public List Rooms { get; set; } = new(); +} \ No newline at end of file diff --git a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs index 5bfd77b..0569477 100644 --- a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs +++ b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using System.Text.Json.Serialization; +using System.Text.RegularExpressions; using ArcaneLibs.Attributes; using ArcaneLibs.Extensions; @@ -91,6 +92,14 @@ public abstract class PolicyRuleEventContent : EventContent { } public string GetDraupnir2StateKey() => Convert.ToBase64String(SHA256.HashData($"{Entity}{Recommendation}".AsBytes().ToArray())); + + public Regex GetEntityRegex() => new(Entity.Replace(".", "\\.").Replace("*", ".*").Replace("?", ".")); + + public bool EntityMatches(string entity) => + Entity == entity + || (Entity.Contains("*") || Entity.Contains("?") + ? GetEntityRegex().IsMatch(entity) + : entity == Entity); } public static class PolicyRecommendationTypes { diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index c9ca85d..aed56a7 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -1,5 +1,7 @@ using System.Diagnostics; using System.Net.Http.Json; +using System.Text.Json; +using ArcaneLibs.Collections; using ArcaneLibs.Extensions; using LibMatrix.Filters; using LibMatrix.Homeservers; @@ -90,9 +92,11 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg if (httpResp is null) throw new NullReferenceException("Failed to send HTTP request"); logger?.LogTrace("Got sync response: {} bytes, {} elapsed", httpResp.GetContentLength(), sw.Elapsed); var deserializeSw = Stopwatch.StartNew(); - var resp = await httpResp.Content.ReadFromJsonAsync(cancellationToken: cancellationToken ?? CancellationToken.None, + var stream = await httpResp.Content.ReadAsStreamAsync(); + await using var seekableStream = new SeekableStream(stream); + var resp = await JsonSerializer.DeserializeAsync(seekableStream, cancellationToken: cancellationToken ?? CancellationToken.None, jsonTypeInfo: SyncResponseSerializerContext.Default.SyncResponse); - logger?.LogInformation("Deserialized sync response: {} bytes, {} elapsed, {} total", httpResp.GetContentLength(), deserializeSw.Elapsed, sw.Elapsed); + logger?.LogInformation("Deserialized sync response: {} bytes, {} elapsed, {} total", seekableStream.Position, deserializeSw.Elapsed, sw.Elapsed); var timeToWait = MinimumDelay.Subtract(sw.Elapsed); if (timeToWait.TotalMilliseconds > 0) await Task.Delay(timeToWait); -- cgit 1.5.1