about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
blob: 334c05c3c6b9e0ffc790134a68735f939abd859c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using MatrixRoomUtils.Core.Interfaces;
using MatrixRoomUtils.Core.StateEventTypes;
using MatrixRoomUtils.Core.StateEventTypes.Spec;

namespace MatrixRoomUtils.Core.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<StateEvent> InitialState { get; set; } = null!;

    [JsonPropertyName("visibility")]
    public string Visibility { get; set; } = null!;

    [JsonPropertyName("power_level_content_override")]
    public PowerLevelEventData PowerLevelContentOverride { get; set; } = null!;

    [JsonPropertyName("creation_content")]
    public JsonObject CreationContent { get; set; } = new();

    /// <summary>
    ///     For use only when you can't use the CreationContent property
    /// </summary>

    public StateEvent this[string event_type, string event_key = ""] {
        get => InitialState.First(x => x.Type == event_type && x.StateKey == event_key);
        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<string, string> Validate() {
        Dictionary<string, string> 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;
    }
}