about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Core')
-rw-r--r--MatrixRoomUtils.Core/Filters/SyncFilter.cs62
-rw-r--r--MatrixRoomUtils.Core/Helpers/SyncHelper.cs59
-rw-r--r--MatrixRoomUtils.Core/StateEvent.cs50
-rw-r--r--MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs (renamed from MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs)2
4 files changed, 133 insertions, 40 deletions
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<string>? 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<string>? 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<string>? Types { get; set; } + } + + [JsonPropertyName("timeline")] + public TimelineFilter? Timeline { get; set; } + + public class TimelineFilter { + [JsonPropertyName("limit")] + public int? Limit { get; set; } + + [JsonPropertyName("types")] + public List<string>? Types { get; set; } + + [JsonPropertyName("not_types")] + public List<string>? NotTypes { get; set; } + } +} + +public class AccountDataFilter { + [JsonPropertyName("not_types")] + public List<string>? 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<SyncResult?> Sync(string? since = null, CancellationToken? cancellationToken = null) { + public async Task<SyncResult?> 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<SyncResult>(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 { /// <summary> /// Event fired when a room invite is received /// </summary> - public List<Func<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>, Task>> - InviteReceivedHandlers { get; } = new(); - public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; } = new(); - public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new(); + public List<Func<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>, Task>> + InviteReceivedHandlers { get; } = new(); + + public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; } = new(); + public List<Func<StateEventResponse, Task>> 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<string>? Changed { get; set; } + + [JsonPropertyName("left")] + public List<string>? 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/ServerACLEventData.cs
index 1d56e9c..4b559c6 100644 --- a/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLData.cs +++ b/MatrixRoomUtils.Core/StateEventTypes/Spec/ServerACLEventData.cs
@@ -5,7 +5,7 @@ using MatrixRoomUtils.Core.Interfaces; namespace MatrixRoomUtils.Core.StateEventTypes.Spec; [MatrixEvent(EventName = "m.room.server_acl")] -public class ServerACLData : IStateEventType { +public class ServerACLEventData : IStateEventType { [JsonPropertyName("allow")] public List<string> Allow { get; set; } // = null!;