From 0d0511e35d9965fc0ea5190ae3347c3d77c3334c Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 14 Aug 2023 04:09:13 +0200 Subject: Split LibMatrix into separate repo --- .../Responses/Admin/AdminRoomDeleteRequest.cs | 18 ++++++ .../Responses/Admin/AdminRoomListingResult.cs | 64 +++++++++++++++++++ LibMatrix/Responses/CreateRoomRequest.cs | 74 ++++++++++++++++++++++ LibMatrix/Responses/CreationContentBaseType.cs | 18 ++++++ LibMatrix/Responses/LoginResponse.cs | 17 +++++ LibMatrix/Responses/StateEventResponse.cs | 47 ++++++++++++++ 6 files changed, 238 insertions(+) create mode 100644 LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs create mode 100644 LibMatrix/Responses/Admin/AdminRoomListingResult.cs create mode 100644 LibMatrix/Responses/CreateRoomRequest.cs create mode 100644 LibMatrix/Responses/CreationContentBaseType.cs create mode 100644 LibMatrix/Responses/LoginResponse.cs create mode 100644 LibMatrix/Responses/StateEventResponse.cs (limited to 'LibMatrix/Responses') diff --git a/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs b/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs new file mode 100644 index 0000000..f22c8d2 --- /dev/null +++ b/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses.Admin; + +public class AdminRoomDeleteRequest { + [JsonPropertyName("new_room_user_id")] + public string? NewRoomUserId { get; set; } + [JsonPropertyName("room_name")] + public string? RoomName { get; set; } + [JsonPropertyName("block")] + public bool Block { get; set; } + [JsonPropertyName("purge")] + public bool Purge { get; set; } + [JsonPropertyName("message")] + public string? Message { get; set; } + [JsonPropertyName("force_purge")] + public bool ForcePurge { get; set; } +} diff --git a/LibMatrix/Responses/Admin/AdminRoomListingResult.cs b/LibMatrix/Responses/Admin/AdminRoomListingResult.cs new file mode 100644 index 0000000..f035184 --- /dev/null +++ b/LibMatrix/Responses/Admin/AdminRoomListingResult.cs @@ -0,0 +1,64 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses.Admin; + +public class AdminRoomListingResult { + [JsonPropertyName("offset")] + public int Offset { get; set; } + + [JsonPropertyName("total_rooms")] + public int TotalRooms { get; set; } + + [JsonPropertyName("next_batch")] + public int? NextBatch { get; set; } + + [JsonPropertyName("prev_batch")] + public int? PrevBatch { get; set; } + + [JsonPropertyName("rooms")] + public List Rooms { get; set; } = new(); + + public class AdminRoomListingResultRoom { + [JsonPropertyName("room_id")] + public string RoomId { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("canonical_alias")] + public string? CanonicalAlias { get; set; } + + [JsonPropertyName("joined_members")] + public int JoinedMembers { get; set; } + + [JsonPropertyName("joined_local_members")] + public int JoinedLocalMembers { get; set; } + + [JsonPropertyName("version")] + public string Version { get; set; } + + [JsonPropertyName("creator")] + public string Creator { get; set; } + + [JsonPropertyName("encryption")] + public string? Encryption { get; set; } + + [JsonPropertyName("federatable")] + public bool Federatable { get; set; } + + [JsonPropertyName("public")] + public bool Public { get; set; } + + [JsonPropertyName("join_rules")] + public string? JoinRules { get; set; } + + [JsonPropertyName("guest_access")] + public string? GuestAccess { get; set; } + + [JsonPropertyName("history_visibility")] + public string? HistoryVisibility { get; set; } + + [JsonPropertyName("state_events")] + public int StateEvents { get; set; } + } +} diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs new file mode 100644 index 0000000..d59e6fd --- /dev/null +++ b/LibMatrix/Responses/CreateRoomRequest.cs @@ -0,0 +1,74 @@ +using System.Reflection; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; +using LibMatrix.Extensions; +using LibMatrix.StateEventTypes.Spec; + +namespace LibMatrix.Responses; + +public class CreateRoomRequest { + [JsonIgnore] public CreationContentBaseType _creationContentBaseType; + + public CreateRoomRequest() => _creationContentBaseType = new CreationContentBaseType(this); + + [JsonPropertyName("name")] + public string Name { get; set; } = null!; + + [JsonPropertyName("room_alias_name")] + public string RoomAliasName { get; set; } = null!; + + //we dont want to use this, we want more control + // [JsonPropertyName("preset")] + // public string Preset { get; set; } = null!; + + [JsonPropertyName("initial_state")] + public List InitialState { get; set; } = null!; + + [JsonPropertyName("visibility")] + public string Visibility { get; set; } = null!; + + [JsonPropertyName("power_level_content_override")] + public RoomPowerLevelEventData PowerLevelContentOverride { get; set; } = null!; + + [JsonPropertyName("creation_content")] + public JsonObject CreationContent { get; set; } = new(); + + /// + /// For use only when you can't use the CreationContent property + /// + + public StateEvent this[string event_type, string event_key = ""] { + get { + var stateEvent = InitialState.FirstOrDefault(x => x.Type == event_type && x.StateKey == event_key); + if (stateEvent == null) { + InitialState.Add(stateEvent = new StateEvent { + Type = event_type, + StateKey = event_key, + TypedContent = Activator.CreateInstance( + StateEvent.KnownStateEventTypes.FirstOrDefault(x => + x.GetCustomAttributes()? + .Any(y => y.EventName == event_type) ?? false) ?? typeof(object) + ) + }); + } + return stateEvent; + } + set { + var stateEvent = InitialState.FirstOrDefault(x => x.Type == event_type && x.StateKey == event_key); + if (stateEvent == null) + InitialState.Add(value); + else + InitialState[InitialState.IndexOf(stateEvent)] = value; + } + } + + public Dictionary Validate() { + Dictionary errors = new(); + if (!Regex.IsMatch(RoomAliasName, @"[a-zA-Z0-9_\-]+$")) + errors.Add("room_alias_name", + "Room alias name must only contain letters, numbers, underscores, and hyphens."); + + return errors; + } +} diff --git a/LibMatrix/Responses/CreationContentBaseType.cs b/LibMatrix/Responses/CreationContentBaseType.cs new file mode 100644 index 0000000..ba3ce5e --- /dev/null +++ b/LibMatrix/Responses/CreationContentBaseType.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses; + +public class CreationContentBaseType { + private readonly CreateRoomRequest createRoomRequest; + + public CreationContentBaseType(CreateRoomRequest createRoomRequest) => this.createRoomRequest = createRoomRequest; + + [JsonPropertyName("type")] + public string Type { + get => (string)createRoomRequest.CreationContent["type"]; + set { + if (value is "null" or "") createRoomRequest.CreationContent.Remove("type"); + else createRoomRequest.CreationContent["type"] = value; + } + } +} diff --git a/LibMatrix/Responses/LoginResponse.cs b/LibMatrix/Responses/LoginResponse.cs new file mode 100644 index 0000000..2800a9c --- /dev/null +++ b/LibMatrix/Responses/LoginResponse.cs @@ -0,0 +1,17 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses; + +public class LoginResponse { + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + + [JsonPropertyName("device_id")] + public string DeviceId { get; set; } + + [JsonPropertyName("home_server")] + public string Homeserver { get; set; } + + [JsonPropertyName("user_id")] + public string UserId { get; set; } +} diff --git a/LibMatrix/Responses/StateEventResponse.cs b/LibMatrix/Responses/StateEventResponse.cs new file mode 100644 index 0000000..b3d5b96 --- /dev/null +++ b/LibMatrix/Responses/StateEventResponse.cs @@ -0,0 +1,47 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses; + +public class StateEventResponse : StateEvent { + [JsonPropertyName("origin_server_ts")] + public ulong OriginServerTs { get; set; } + + [JsonPropertyName("room_id")] + public string RoomId { get; set; } + + [JsonPropertyName("sender")] + public string Sender { get; set; } + + [JsonPropertyName("unsigned")] + public UnsignedData? Unsigned { get; set; } + + [JsonPropertyName("event_id")] + public string EventId { get; set; } + + [JsonPropertyName("user_id")] + public string UserId { get; set; } + + [JsonPropertyName("replaces_state")] + public string ReplacesState { get; set; } + + public class UnsignedData { + [JsonPropertyName("age")] + public ulong? Age { get; set; } + + [JsonPropertyName("redacted_because")] + public object? RedactedBecause { get; set; } + + [JsonPropertyName("transaction_id")] + public string? TransactionId { get; set; } + + [JsonPropertyName("replaces_state")] + public string? ReplacesState { get; set; } + + [JsonPropertyName("prev_sender")] + public string? PrevSender { get; set; } + + [JsonPropertyName("prev_content")] + public JsonObject? PrevContent { get; set; } + } +} -- cgit 1.4.1