about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
blob: da7d56912e825b299d67a2c02e83568ef14fa0f1 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using MatrixRoomUtils.Core.Extensions;

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 PowerLevelEvent 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;
        }
    }

    //extra properties
    [JsonIgnore]
    public string HistoryVisibility {
        get {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
            if (stateEvent == null) {
                InitialState.Add(new StateEvent {
                    Type = "m.room.history_visibility",
                    Content = new JsonObject {
                        ["history_visibility"] = "shared"
                    }
                });
                return "shared";
            }

            return stateEvent.ContentAsJsonNode["history_visibility"].GetValue<string>();
        }
        set {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
            if (stateEvent == null)
                InitialState.Add(new StateEvent {
                    Type = "m.room.history_visibility",
                    Content = new JsonObject {
                        ["history_visibility"] = value
                    }
                });
            else {
                var v = stateEvent.ContentAsJsonNode;
                v["history_visibility"] = value;
                stateEvent.ContentAsJsonNode = v;
            }
        }
    }

    [JsonIgnore]
    public string RoomIcon {
        get {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
            if (stateEvent == null) {
                InitialState.Add(new StateEvent {
                    Type = "m.room.avatar",
                    Content = new JsonObject {
                        ["url"] = ""
                    }
                });
                return "";
            }

            return stateEvent.ContentAsJsonNode["url"].GetValue<string>();
        }
        set {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
            if (stateEvent == null)
                InitialState.Add(new StateEvent {
                    Type = "m.room.avatar",
                    Content = new JsonObject {
                        ["url"] = value
                    }
                });
            else {
                var v = stateEvent.ContentAsJsonNode;
                v["url"] = value;
                stateEvent.ContentAsJsonNode = v;
            }
        }
    }

    // [JsonIgnore]
    // public string GuestAccess
    // {
    //     get
    //     {
    //         var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
    //         if (stateEvent == null)
    //         {
    //             InitialState.Add(new StateEvent()
    //             {
    //                 Type = "m.room.guest_access",
    //                 Content = new JsonObject()
    //                 {
    //                     ["guest_access"] = "can_join"
    //                 }
    //             });
    //             return "can_join";
    //         }
    //
    //         return stateEvent.ContentAsJsonNode["guest_access"].GetValue<string>();
    //     }
    //     set
    //     {
    //         var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
    //         if (stateEvent == null)
    //         {
    //             InitialState.Add(new StateEvent()
    //             {
    //                 Type = "m.room.guest_access",
    //                 Content = new JsonObject()
    //                 {
    //                     ["guest_access"] = value
    //                 }
    //             });
    //         }
    //         else
    //         {
    //             var v = stateEvent.ContentAsJsonNode;
    //             v["guest_access"] = value;
    //             stateEvent.ContentAsJsonNode = v;
    //         }
    //     }
    // }

    public ServerACL ServerACLs {
        get {
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.server_acl");
            if (stateEvent == null) {
                InitialState.Add(new StateEvent {
                    Type = "m.room.server_acl",
                    Content = new JsonObject {
                        ["allow"] = new JsonArray {
                            "*"
                        },
                        ["deny"] = new JsonArray()
                    }
                });
                return new ServerACL {
                    Allow = new List<string> {
                        "*"
                    },
                    Deny = new List<string>(),
                    AllowIpLiterals = true
                };
            }

            return new ServerACL {
                Allow = stateEvent.ContentAsJsonNode["allow"].Deserialize<List<string>>(),
                Deny = stateEvent.ContentAsJsonNode["deny"].Deserialize<List<string>>(),
                AllowIpLiterals = true
            };
        }
        set {
            Console.WriteLine($"Setting server acl to {value.ToJson()}");
            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.server_acl");
            if (stateEvent == null)
                InitialState.Add(new StateEvent {
                    Type = "m.room.server_acl",
                    Content = new JsonObject {
                        ["allow"] = JsonNode.Parse(JsonSerializer.Serialize(value.Allow)),
                        ["deny"] = JsonNode.Parse(JsonSerializer.Serialize(value.Deny))
                            ["allow_ip_literals"] = value.AllowIpLiterals
                    }
                });
            else {
                var v = stateEvent.ContentAsJsonNode;
                v["allow"] = JsonNode.Parse(JsonSerializer.Serialize(value.Allow));
                v["deny"] = JsonNode.Parse(JsonSerializer.Serialize(value.Deny));
                v["allow_ip_literals"] = value.AllowIpLiterals;
                stateEvent.ContentAsJsonNode = v;
                Console.WriteLine($"v={v.ToJson()}");
                Console.WriteLine($"stateEvent.ContentAsJsonNode={stateEvent.ContentAsJsonNode.ToJson()}");
            }
        }
    }

    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 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;
        }
    }
}

public class PowerLevelEvent {
    [JsonPropertyName("ban")]
    public int Ban { get; set; } // = 50;

    [JsonPropertyName("events_default")]
    public int EventsDefault { get; set; } // = 0;

    [JsonPropertyName("events")]
    public Dictionary<string, int> Events { get; set; } // = null!;

    [JsonPropertyName("invite")]
    public int Invite { get; set; } // = 50;

    [JsonPropertyName("kick")]
    public int Kick { get; set; } // = 50;

    [JsonPropertyName("notifications")]
    public NotificationsPL NotificationsPl { get; set; } // = null!;

    [JsonPropertyName("redact")]
    public int Redact { get; set; } // = 50;

    [JsonPropertyName("state_default")]
    public int StateDefault { get; set; } // = 50;

    [JsonPropertyName("users")]
    public Dictionary<string, int> Users { get; set; } // = null!;

    [JsonPropertyName("users_default")]
    public int UsersDefault { get; set; } // = 0;
}

public class NotificationsPL {
    [JsonPropertyName("room")]
    public int Room { get; set; } = 50;
}

public class ServerACL {
    [JsonPropertyName("allow")]
    public List<string> Allow { get; set; } // = null!;

    [JsonPropertyName("deny")]
    public List<string> Deny { get; set; } // = null!;

    [JsonPropertyName("allow_ip_literals")]
    public bool AllowIpLiterals { get; set; } // = false;
}