From f41b6e5ec431c88bc1d94e4832d8ba49ddc42004 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 5 Mar 2024 11:19:52 +0100 Subject: HomeserverEmulator work --- LibMatrix/EventIdResponse.cs | 6 +++-- LibMatrix/Helpers/SyncHelper.cs | 2 +- LibMatrix/MatrixException.cs | 35 +++++++++++++++++++++++++ LibMatrix/Responses/CreateRoomRequest.cs | 6 +++-- LibMatrix/Responses/RoomKeysResponse.cs | 19 ++++++++++++++ LibMatrix/Responses/SyncResponse.cs | 7 +++++ LibMatrix/Services/HomeserverProviderService.cs | 2 +- LibMatrix/StateEvent.cs | 26 +++++++++++++----- 8 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 LibMatrix/Responses/RoomKeysResponse.cs (limited to 'LibMatrix') diff --git a/LibMatrix/EventIdResponse.cs b/LibMatrix/EventIdResponse.cs index 6a04229..c2ad273 100644 --- a/LibMatrix/EventIdResponse.cs +++ b/LibMatrix/EventIdResponse.cs @@ -2,7 +2,9 @@ using System.Text.Json.Serialization; namespace LibMatrix; -public class EventIdResponse { +public class EventIdResponse(string eventId) { + public EventIdResponse(StateEventResponse stateEventResponse) : this(stateEventResponse.EventId ?? throw new NullReferenceException("State event ID is null!")) { } + [JsonPropertyName("event_id")] - public required string EventId { get; set; } + public string EventId { get; set; } = eventId; } \ No newline at end of file diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index f9a7cb7..9d339e4 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -55,7 +55,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg private async Task updateFilterAsync() { if (!string.IsNullOrWhiteSpace(NamedFilterName)) { - _filterId = await homeserver.GetNamedFilterIdOrNullAsync(NamedFilterName); + _filterId = await homeserver.GetOrUploadNamedFilterIdAsync(NamedFilterName); if (_filterId is null) if (logger is null) Console.WriteLine($"Failed to get filter ID for named filter {NamedFilterName}"); else logger.LogWarning("Failed to get filter ID for named filter {NamedFilterName}", NamedFilterName); diff --git a/LibMatrix/MatrixException.cs b/LibMatrix/MatrixException.cs index 8ec8fd5..eb207da 100644 --- a/LibMatrix/MatrixException.cs +++ b/LibMatrix/MatrixException.cs @@ -61,4 +61,39 @@ public class MatrixException : Exception { "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM" => $"Cannot leave server notice room: {Error}", _ => $"Unknown error: {new { ErrorCode, Error, SoftLogout, RetryAfterMs }.ToJson(ignoreNull: true)}" }}"; + + public static class ErrorCodes { + public const string M_FORBIDDEN = "M_FORBIDDEN"; + public const string M_UNKNOWN_TOKEN = "M_UNKNOWN_TOKEN"; + public const string M_MISSING_TOKEN = "M_MISSING_TOKEN"; + public const string M_BAD_JSON = "M_BAD_JSON"; + public const string M_NOT_JSON = "M_NOT_JSON"; + public const string M_NOT_FOUND = "M_NOT_FOUND"; + public const string M_LIMIT_EXCEEDED = "M_LIMIT_EXCEEDED"; + public const string M_UNRECOGNISED = "M_UNRECOGNISED"; + public const string M_UNKOWN = "M_UNKOWN"; + public const string M_UNAUTHORIZED = "M_UNAUTHORIZED"; + public const string M_USER_DEACTIVATED = "M_USER_DEACTIVATED"; + public const string M_USER_IN_USE = "M_USER_IN_USE"; + public const string M_INVALID_USERNAME = "M_INVALID_USERNAME"; + public const string M_ROOM_IN_USE = "M_ROOM_IN_USE"; + public const string M_INVALID_ROOM_STATE = "M_INVALID_ROOM_STATE"; + public const string M_THREEPID_IN_USE = "M_THREEPID_IN_USE"; + public const string M_THREEPID_NOT_FOUND = "M_THREEPID_NOT_FOUND"; + public const string M_THREEPID_AUTH_FAILED = "M_THREEPID_AUTH_FAILED"; + public const string M_THREEPID_DENIED = "M_THREEPID_DENIED"; + public const string M_SERVER_NOT_TRUSTED = "M_SERVER_NOT_TRUSTED"; + public const string M_UNSUPPORTED_ROOM_VERSION = "M_UNSUPPORTED_ROOM_VERSION"; + public const string M_INCOMPATIBLE_ROOM_VERSION = "M_INCOMPATIBLE_ROOM_VERSION"; + public const string M_BAD_STATE = "M_BAD_STATE"; + public const string M_GUEST_ACCESS_FORBIDDEN = "M_GUEST_ACCESS_FORBIDDEN"; + public const string M_CAPTCHA_NEEDED = "M_CAPTCHA_NEEDED"; + public const string M_CAPTCHA_INVALID = "M_CAPTCHA_INVALID"; + public const string M_MISSING_PARAM = "M_MISSING_PARAM"; + public const string M_INVALID_PARAM = "M_INVALID_PARAM"; + public const string M_TOO_LARGE = "M_TOO_LARGE"; + public const string M_EXCLUSIVE = "M_EXCLUSIVE"; + public const string M_RESOURCE_LIMIT_EXCEEDED = "M_RESOURCE_LIMIT_EXCEEDED"; + public const string M_CANNOT_LEAVE_SERVER_NOTICE_ROOM = "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM"; + } } \ No newline at end of file diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs index d78f574..ee4317e 100644 --- a/LibMatrix/Responses/CreateRoomRequest.cs +++ b/LibMatrix/Responses/CreateRoomRequest.cs @@ -12,7 +12,9 @@ namespace LibMatrix.Responses; public class CreateRoomRequest { [JsonIgnore] public CreationContentBaseType CreationContentBaseType; - public CreateRoomRequest() => CreationContentBaseType = new CreationContentBaseType(this); + public CreateRoomRequest() { + CreationContentBaseType = new CreationContentBaseType(this); + } [JsonPropertyName("name")] public string? Name { get; set; } @@ -37,7 +39,7 @@ public class CreateRoomRequest { public string? Visibility { get; set; } [JsonPropertyName("power_level_content_override")] - public RoomPowerLevelEventContent PowerLevelContentOverride { get; set; } = null!; + public RoomPowerLevelEventContent? PowerLevelContentOverride { get; set; } = null!; [JsonPropertyName("creation_content")] public JsonObject CreationContent { get; set; } = new(); diff --git a/LibMatrix/Responses/RoomKeysResponse.cs b/LibMatrix/Responses/RoomKeysResponse.cs new file mode 100644 index 0000000..8dc305a --- /dev/null +++ b/LibMatrix/Responses/RoomKeysResponse.cs @@ -0,0 +1,19 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses; + +public class RoomKeysRequest { + [JsonPropertyName("algorithm")] + public string Algorithm { get; set; } + + [JsonPropertyName("auth_data")] + public JsonObject AuthData { get; set; } +} +public class RoomKeysResponse : RoomKeysRequest { + [JsonPropertyName("version")] + public string Version { get; set; } + + [JsonPropertyName("etag")] + public string Etag { get; set; } +} \ No newline at end of file diff --git a/LibMatrix/Responses/SyncResponse.cs b/LibMatrix/Responses/SyncResponse.cs index 49259d0..e4addb6 100644 --- a/LibMatrix/Responses/SyncResponse.cs +++ b/LibMatrix/Responses/SyncResponse.cs @@ -83,6 +83,13 @@ public class SyncResponse { public SummaryDataStructure? Summary { get; set; } public class TimelineDataStructure { + public TimelineDataStructure() { } + + public TimelineDataStructure(List? events, bool? limited) { + Events = events; + Limited = limited; + } + [JsonPropertyName("events")] public List? Events { get; set; } diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 7a13816..8e2e15b 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -43,7 +43,7 @@ public class HomeserverProviderService(ILogger logger serverVersion = serverVersion = await (rhs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult(null)!); } catch (Exception e) { - logger.LogError(e, "Failed to get server version for {homeserver}", homeserver); + logger.LogWarning(e, "Failed to get server version for {homeserver}", homeserver); sem.Release(); throw; } diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 019c428..541fb78 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -1,5 +1,6 @@ using System.Collections.Frozen; using System.Collections.Immutable; +using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; @@ -25,7 +26,7 @@ public class StateEvent { return dict; }).ToFrozenDictionary(); - public static Type GetStateEventType(string type) => KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); + public static Type GetStateEventType(string? type) => string.IsNullOrWhiteSpace(type) ? typeof(UnknownEventContent) : KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); [JsonIgnore] public Type MappedType => GetStateEventType(Type); @@ -73,10 +74,10 @@ public class StateEvent { } [JsonPropertyName("state_key")] - public string StateKey { get; set; } = ""; + public string StateKey { get; set; } [JsonPropertyName("type")] - public required string Type { get; set; } + public string Type { get; set; } [JsonPropertyName("replaces_state")] public string? ReplacesState { get; set; } @@ -138,7 +139,7 @@ public class StateEvent { public class StateEventResponse : StateEvent { [JsonPropertyName("origin_server_ts")] - public ulong? OriginServerTs { get; set; } + public long? OriginServerTs { get; set; } [JsonPropertyName("room_id")] public string? RoomId { get; set; } @@ -152,9 +153,6 @@ public class StateEventResponse : StateEvent { [JsonPropertyName("event_id")] public string? EventId { get; set; } - [JsonPropertyName("replaces_state")] - public new string? ReplacesState { get; set; } - public class UnsignedData { [JsonPropertyName("age")] public ulong? Age { get; set; } @@ -181,6 +179,12 @@ public class StateEventResponse : StateEvent { internal partial class ChunkedStateEventResponseSerializerContext : JsonSerializerContext; public class EventList { + public EventList() { } + + public EventList(List? events) { + Events = events; + } + [JsonPropertyName("events")] public List? Events { get; set; } = new(); } @@ -190,6 +194,14 @@ public class ChunkedStateEventResponse { public List? Chunk { get; set; } = new(); } +public class PaginatedChunkedStateEventResponse : ChunkedStateEventResponse { + [JsonPropertyName("start")] + public string? Start { get; set; } + + [JsonPropertyName("end")] + public string? End { get; set; } +} + #region Unused code /* -- cgit 1.4.1