about summary refs log tree commit diff
path: root/LibMatrix/Responses/CreateRoomRequest.cs
blob: 6919b84a07e3e67f9ec3535ecef9d498ea95325f (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using LibMatrix.LegacyEvents.EventTypes;
using LibMatrix.LegacyEvents.EventTypes.Spec.State;
using LibMatrix.Homeservers;

namespace LibMatrix.Responses;

public class CreateRoomRequest {
    [JsonIgnore] public CreationContentBaseType CreationContentBaseType;

    public CreateRoomRequest() {
        CreationContentBaseType = new CreationContentBaseType(this);
    }

    [JsonPropertyName("name")]
    public string? Name { get; set; }

    /// <summary>
    /// Room alias local name. Must be unique on the homeserver.
    /// </summary>
    [JsonPropertyName("room_alias_name")]
    public string? RoomAliasName { get; set; }

    //we dont want to use this, we want more control
    // [JsonPropertyName("preset")]
    // public string Preset { get; set; } = null!;

    [JsonPropertyName("initial_state")]
    public List<LegacyMatrixEvent>? InitialState { get; set; }

    /// <summary>
    /// One of: ["public", "private"]
    /// </summary>
    [JsonPropertyName("visibility")]
    public string? Visibility { get; set; }

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

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

    [JsonPropertyName("invite")]
    public List<string>? Invite { get; set; }

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

    public LegacyMatrixEvent this[string eventType, string eventKey = ""] {
        get {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == eventType && x.StateKey == eventKey);
            if (stateEvent == null)
                InitialState.Add(stateEvent = new LegacyMatrixEvent {
                    Type = eventType,
                    StateKey = eventKey,
                    TypedContent = (EventContent)Activator.CreateInstance(
                        LegacyMatrixEvent.KnownStateEventTypes.FirstOrDefault(x =>
                            x.GetCustomAttributes<MatrixEventAttribute>()?
                                .Any(y => y.EventName == eventType) ?? false) ?? typeof(UnknownEventContent)
                    )!
                });

            return stateEvent;
        }
        set {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == eventType && x.StateKey == eventKey);
            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;
    }

    public static CreateRoomRequest CreatePublic(AuthenticatedHomeserverGeneric hs, string? name = null, string? roomAliasName = null) {
        var request = new CreateRoomRequest {
            Name = name ?? "New public Room",
            Visibility = "public",
            CreationContent = new JsonObject(),
            PowerLevelContentOverride = new RoomPowerLevelEventContent {
                EventsDefault = 0,
                UsersDefault = 0,
                Kick = 50,
                Ban = 50,
                Invite = 25,
                StateDefault = 10,
                Redact = 50,
                NotificationsPl = new RoomPowerLevelEventContent.NotificationsPL {
                    Room = 10
                },
                Events = new Dictionary<string, long> {
                    { "m.room.avatar", 50 },
                    { "m.room.canonical_alias", 50 },
                    { "m.room.encryption", 100 },
                    { "m.room.history_visibility", 100 },
                    { "m.room.name", 50 },
                    { "m.room.power_levels", 100 },
                    { "m.room.server_acl", 100 },
                    { "m.room.tombstone", 100 }
                },
                Users = new Dictionary<string, long> {
                    {
                        hs.UserId,
                        101
                    }
                }
            },
            RoomAliasName = roomAliasName,
            InitialState = new List<LegacyMatrixEvent>()
        };

        return request;
    }

    public static CreateRoomRequest CreatePrivate(AuthenticatedHomeserverGeneric hs, string? name = null, string? roomAliasName = null) {
        var request = new CreateRoomRequest {
            Name = name ?? "New private Room",
            Visibility = "private",
            CreationContent = new JsonObject(),
            PowerLevelContentOverride = new RoomPowerLevelEventContent {
                EventsDefault = 0,
                UsersDefault = 0,
                Kick = 50,
                Ban = 50,
                Invite = 25,
                StateDefault = 10,
                Redact = 50,
                NotificationsPl = new RoomPowerLevelEventContent.NotificationsPL {
                    Room = 10
                },
                Events = new Dictionary<string, long> {
                    { "m.room.avatar", 50 },
                    { "m.room.canonical_alias", 50 },
                    { "m.room.encryption", 100 },
                    { "m.room.history_visibility", 100 },
                    { "m.room.name", 50 },
                    { "m.room.power_levels", 100 },
                    { "m.room.server_acl", 100 },
                    { "m.room.tombstone", 100 }
                },
                Users = new Dictionary<string, long> {
                    {
                        hs.UserId,
                        101
                    }
                }
            },
            RoomAliasName = roomAliasName,
            InitialState = new List<LegacyMatrixEvent>()
        };

        return request;
    }
}