From 257019113200d714d86d22ccab6c18b37cd28283 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 3 Jul 2023 00:43:34 +0200 Subject: Local changes --- MatrixRoomUtils.Core/Filters/SyncFilter.cs | 62 ++++++++++++++++++++++ MatrixRoomUtils.Core/Helpers/SyncHelper.cs | 59 +++++++++++++++----- MatrixRoomUtils.Core/StateEvent.cs | 50 ++++++++--------- .../StateEventTypes/Spec/ServerACLData.cs | 17 ------ .../StateEventTypes/Spec/ServerACLEventData.cs | 17 ++++++ 5 files changed, 149 insertions(+), 56 deletions(-) create mode 100644 MatrixRoomUtils.Core/Filters/SyncFilter.cs delete mode 100644 MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs create mode 100644 MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs (limited to 'MatrixRoomUtils.Core') diff --git a/MatrixRoomUtils.Core/Filters/SyncFilter.cs b/MatrixRoomUtils.Core/Filters/SyncFilter.cs new file mode 100644 index 0000000..7957898 --- /dev/null +++ b/MatrixRoomUtils.Core/Filters/SyncFilter.cs @@ -0,0 +1,62 @@ +using System.Text.Json.Serialization; + +namespace MatrixRoomUtils.Core.Filters; + +public class SyncFilter { + [JsonPropertyName("account_data")] + public AccountDataFilter? AccountData { get; set; } + + [JsonPropertyName("presence")] + public PresenceFilter? Presence { get; set; } + + [JsonPropertyName("room")] + public RoomFilter? Room { get; set; } +} + +public class PresenceFilter { + [JsonPropertyName("not_types")] + public List? NotTypes { get; set; } +} + +public class RoomFilter { + [JsonPropertyName("account_data")] + public AccountDataFilter? AccountData { get; set; } + + [JsonPropertyName("ephemeral")] + public EphemeralFilter? Ephemeral { get; set; } + + public class EphemeralFilter { + [JsonPropertyName("not_types")] + public List? NotTypes { get; set; } + } + + [JsonPropertyName("state")] + public StateFilter? State { get; set; } + + public class StateFilter { + [JsonPropertyName("lazy_load_members")] + public bool? LazyLoadMembers { get; set; } + + [JsonPropertyName("types")] + public List? Types { get; set; } + } + + [JsonPropertyName("timeline")] + public TimelineFilter? Timeline { get; set; } + + public class TimelineFilter { + [JsonPropertyName("limit")] + public int? Limit { get; set; } + + [JsonPropertyName("types")] + public List? Types { get; set; } + + [JsonPropertyName("not_types")] + public List? NotTypes { get; set; } + } +} + +public class AccountDataFilter { + [JsonPropertyName("not_types")] + public List? NotTypes { get; set; } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs index eff412b..f5c7d9d 100644 --- a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs +++ b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs @@ -1,6 +1,8 @@ using System.Diagnostics.CodeAnalysis; using System.Net.Http.Json; using System.Text.Json.Serialization; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Filters; using MatrixRoomUtils.Core.Interfaces; using MatrixRoomUtils.Core.Responses; using MatrixRoomUtils.Core.Responses.Admin; @@ -18,19 +20,26 @@ public class SyncHelper { _storageService = storageService; } - public async Task Sync(string? since = null, CancellationToken? cancellationToken = null) { + public async Task Sync( + string? since = null, + int? timeout = 30000, + string? setPresence = "online", + SyncFilter? filter = null, + CancellationToken? cancellationToken = null) { var outFileName = "sync-" + - (await _storageService.CacheStorageProvider.GetAllKeysAsync()).Count(x => x.StartsWith("sync")) + + (await _storageService.CacheStorageProvider.GetAllKeysAsync()).Count( + x => x.StartsWith("sync")) + ".json"; - var url = "/_matrix/client/v3/sync?timeout=30000&set_presence=online"; + var url = $"/_matrix/client/v3/sync?timeout={timeout}&set_presence={setPresence}"; if (!string.IsNullOrWhiteSpace(since)) url += $"&since={since}"; - else url += "&full_state=true"; + if (filter is not null) url += $"&filter={filter.ToJson(ignoreNull: true, indent: false)}"; + // else url += "&full_state=true"; Console.WriteLine("Calling: " + url); try { var res = await _homeServer._httpClient.GetFromJsonAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None); - await _storageService.CacheStorageProvider.SaveObjectAsync(outFileName, res); - Console.WriteLine($"Wrote file: {outFileName}"); + // await _storageService.CacheStorageProvider.SaveObjectAsync(outFileName, res); + // Console.WriteLine($"Wrote file: {outFileName}"); return res; } catch (TaskCanceledException) { @@ -44,13 +53,20 @@ public class SyncHelper { } [SuppressMessage("ReSharper", "FunctionNeverReturns")] - public async Task RunSyncLoop(CancellationToken? cancellationToken = null, bool skipInitialSyncEvents = true) { + public async Task RunSyncLoop( + bool skipInitialSyncEvents = true, + string? since = null, + int? timeout = 30000, + string? setPresence = "online", + SyncFilter? filter = null, + CancellationToken? cancellationToken = null + ) { SyncResult? sync = null; - string? nextBatch = null; + string? nextBatch = since; while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) { - sync = await Sync(nextBatch, cancellationToken); + sync = await Sync(since: nextBatch, timeout: timeout, setPresence: setPresence, filter: filter, cancellationToken: cancellationToken); nextBatch = sync?.NextBatch ?? nextBatch; - if(sync is null) continue; + if (sync is null) continue; Console.WriteLine($"Got sync, next batch: {nextBatch}!"); if (sync.Rooms is { Invite.Count: > 0 }) { @@ -88,10 +104,11 @@ public class SyncHelper { /// /// Event fired when a room invite is received /// - public List, Task>> - InviteReceivedHandlers { get; } = new(); - public List> TimelineEventHandlers { get; } = new(); - public List> AccountDataReceivedHandlers { get; } = new(); + public List, Task>> + InviteReceivedHandlers { get; } = new(); + + public List> TimelineEventHandlers { get; } = new(); + public List> AccountDataReceivedHandlers { get; } = new(); } public class SyncResult { @@ -109,6 +126,20 @@ public class SyncResult { [JsonPropertyName("rooms")] public RoomsDataStructure? Rooms { get; set; } + + [JsonPropertyName("to_device")] + public EventList? ToDevice { get; set; } + + [JsonPropertyName("device_lists")] + public DeviceListsDataStructure? DeviceLists { get; set; } + + public class DeviceListsDataStructure { + [JsonPropertyName("changed")] + public List? Changed { get; set; } + + [JsonPropertyName("left")] + public List? Left { get; set; } + } // supporting classes public class PresenceDataStructure { diff --git a/MatrixRoomUtils.Core/StateEvent.cs b/MatrixRoomUtils.Core/StateEvent.cs index 901a194..8952403 100644 --- a/MatrixRoomUtils.Core/StateEvent.cs +++ b/MatrixRoomUtils.Core/StateEvent.cs @@ -29,10 +29,10 @@ public class StateEvent { get => _type; set { _type = value; - if (RawContent is not null && this is StateEventResponse stateEventResponse) { - if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; - var x = GetType.Name; - } + // if (RawContent is not null && this is StateEventResponse stateEventResponse) { + // if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; + // var x = GetType.Name; + // } } } @@ -46,10 +46,10 @@ public class StateEvent { get => _rawContent; set { _rawContent = value; - if (Type is not null && this is StateEventResponse stateEventResponse) { - if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; - var x = GetType.Name; - } + // if (Type is not null && this is StateEventResponse stateEventResponse) { + // if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; + // var x = GetType.Name; + // } } } @@ -67,23 +67,23 @@ public class StateEvent { // if (type == typeof(RoomEmotesEventData)) { // RawContent["emote"] = RawContent["emote"]?.AsObject() ?? new JsonObject(); // } - - if (this is StateEventResponse stateEventResponse) { - if (type == null || type == typeof(object)) { - Console.WriteLine($"Warning: unknown event type '{Type}'!"); - Console.WriteLine(RawContent.ToJson()); - Directory.CreateDirectory($"unknown_state_events/{Type}"); - File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json", - RawContent.ToJson()); - Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json"); - } - else if (RawContent is not null && RawContent.FindExtraJsonObjectFields(type)) { - Directory.CreateDirectory($"unknown_state_events/{Type}"); - File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json", - RawContent.ToJson()); - Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json"); - } - } + // + // if (this is StateEventResponse stateEventResponse) { + // if (type == null || type == typeof(object)) { + // Console.WriteLine($"Warning: unknown event type '{Type}'!"); + // Console.WriteLine(RawContent.ToJson()); + // Directory.CreateDirectory($"unknown_state_events/{Type}"); + // File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json", + // RawContent.ToJson()); + // Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json"); + // } + // else if (RawContent is not null && RawContent.FindExtraJsonObjectFields(type)) { + // Directory.CreateDirectory($"unknown_state_events/{Type}"); + // File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json", + // RawContent.ToJson()); + // Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json"); + // } + // } return type ?? typeof(object); } diff --git a/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs b/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs deleted file mode 100644 index 1d56e9c..0000000 --- a/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Text.Json.Serialization; -using MatrixRoomUtils.Core.Extensions; -using MatrixRoomUtils.Core.Interfaces; - -namespace MatrixRoomUtils.Core.StateEventTypes.Spec; - -[MatrixEvent(EventName = "m.room.server_acl")] -public class ServerACLData : IStateEventType { - [JsonPropertyName("allow")] - public List Allow { get; set; } // = null!; - - [JsonPropertyName("deny")] - public List Deny { get; set; } // = null!; - - [JsonPropertyName("allow_ip_literals")] - public bool AllowIpLiterals { get; set; } // = false; -} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs b/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs new file mode 100644 index 0000000..4b559c6 --- /dev/null +++ b/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs @@ -0,0 +1,17 @@ +using System.Text.Json.Serialization; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Interfaces; + +namespace MatrixRoomUtils.Core.StateEventTypes.Spec; + +[MatrixEvent(EventName = "m.room.server_acl")] +public class ServerACLEventData : IStateEventType { + [JsonPropertyName("allow")] + public List Allow { get; set; } // = null!; + + [JsonPropertyName("deny")] + public List Deny { get; set; } // = null!; + + [JsonPropertyName("allow_ip_literals")] + public bool AllowIpLiterals { get; set; } // = false; +} \ No newline at end of file -- cgit 1.5.1