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 --- LibMatrix/Responses/CreateRoomRequest.cs | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 LibMatrix/Responses/CreateRoomRequest.cs (limited to 'LibMatrix/Responses/CreateRoomRequest.cs') 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; + } +} -- cgit 1.4.1