MatrixRoomUtils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

CreateRoomRequest.cs (10531B)


      1 using System.Text.Json;
      2 using System.Text.Json.Nodes;
      3 using System.Text.Json.Serialization;
      4 using System.Text.RegularExpressions;
      5 using MatrixRoomUtils.Core.Extensions;
      6 
      7 namespace MatrixRoomUtils.Core.Responses;
      8 
      9 public class CreateRoomRequest
     10 {
     11     [JsonPropertyName("name")] public string Name { get; set; } = null!;
     12 
     13     [JsonPropertyName("room_alias_name")] public string RoomAliasName { get; set; } = null!;
     14 
     15     //we dont want to use this, we want more control
     16     // [JsonPropertyName("preset")]
     17     // public string Preset { get; set; } = null!;
     18     [JsonPropertyName("initial_state")] public List<StateEvent> InitialState { get; set; } = null!;
     19     [JsonPropertyName("visibility")] public string Visibility { get; set; } = null!;
     20 
     21     [JsonPropertyName("power_level_content_override")]
     22     public PowerLevelEvent PowerLevelContentOverride { get; set; } = null!;
     23 
     24     [JsonPropertyName("creation_content")] public JsonObject CreationContent { get; set; } = new();
     25 
     26     /// <summary>
     27     /// For use only when you can't use the CreationContent property
     28     /// </summary>
     29 
     30     public StateEvent this[string event_type, string event_key = ""]
     31     {
     32         get => InitialState.First(x => x.Type == event_type && x.StateKey == event_key);
     33         set
     34         {
     35             var stateEvent = InitialState.FirstOrDefault(x => x.Type == event_type && x.StateKey == event_key);
     36             if (stateEvent == null)
     37             {
     38                 InitialState.Add(value);
     39             }
     40             else
     41             {
     42                 InitialState[InitialState.IndexOf(stateEvent)] = value;
     43             }
     44         }
     45     }
     46 
     47     //extra properties
     48     [JsonIgnore]
     49     public string HistoryVisibility
     50     {
     51         get
     52         {
     53             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
     54             if (stateEvent == null)
     55             {
     56                 InitialState.Add(new StateEvent()
     57                 {
     58                     Type = "m.room.history_visibility",
     59                     Content = new JsonObject()
     60                     {
     61                         ["history_visibility"] = "shared"
     62                     }
     63                 });
     64                 return "shared";
     65             }
     66 
     67             return stateEvent.ContentAsJsonNode["history_visibility"].GetValue<string>();
     68         }
     69         set
     70         {
     71             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
     72             if (stateEvent == null)
     73             {
     74                 InitialState.Add(new StateEvent()
     75                 {
     76                     Type = "m.room.history_visibility",
     77                     Content = new JsonObject()
     78                     {
     79                         ["history_visibility"] = value
     80                     }
     81                 });
     82             }
     83             else
     84             {
     85                 var v = stateEvent.ContentAsJsonNode;
     86                 v["history_visibility"] = value;
     87                 stateEvent.ContentAsJsonNode = v;
     88             }
     89         }
     90     }
     91 
     92     [JsonIgnore]
     93     public string RoomIcon
     94     {
     95         get
     96         {
     97             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
     98             if (stateEvent == null)
     99             {
    100                 InitialState.Add(new StateEvent()
    101                 {
    102                     Type = "m.room.avatar",
    103                     Content = new JsonObject()
    104                     {
    105                         ["url"] = ""
    106                     }
    107                 });
    108                 return "";
    109             }
    110 
    111             return stateEvent.ContentAsJsonNode["url"].GetValue<string>();
    112         }
    113         set
    114         {
    115             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
    116             if (stateEvent == null)
    117             {
    118                 InitialState.Add(new StateEvent()
    119                 {
    120                     Type = "m.room.avatar",
    121                     Content = new JsonObject()
    122                     {
    123                         ["url"] = value
    124                     }
    125                 });
    126             }
    127             else
    128             {
    129                 var v = stateEvent.ContentAsJsonNode;
    130                 v["url"] = value;
    131                 stateEvent.ContentAsJsonNode = v;
    132             }
    133         }
    134     }
    135 
    136     // [JsonIgnore]
    137     // public string GuestAccess
    138     // {
    139     //     get
    140     //     {
    141     //         var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
    142     //         if (stateEvent == null)
    143     //         {
    144     //             InitialState.Add(new StateEvent()
    145     //             {
    146     //                 Type = "m.room.guest_access",
    147     //                 Content = new JsonObject()
    148     //                 {
    149     //                     ["guest_access"] = "can_join"
    150     //                 }
    151     //             });
    152     //             return "can_join";
    153     //         }
    154     //
    155     //         return stateEvent.ContentAsJsonNode["guest_access"].GetValue<string>();
    156     //     }
    157     //     set
    158     //     {
    159     //         var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
    160     //         if (stateEvent == null)
    161     //         {
    162     //             InitialState.Add(new StateEvent()
    163     //             {
    164     //                 Type = "m.room.guest_access",
    165     //                 Content = new JsonObject()
    166     //                 {
    167     //                     ["guest_access"] = value
    168     //                 }
    169     //             });
    170     //         }
    171     //         else
    172     //         {
    173     //             var v = stateEvent.ContentAsJsonNode;
    174     //             v["guest_access"] = value;
    175     //             stateEvent.ContentAsJsonNode = v;
    176     //         }
    177     //     }
    178     // }
    179 
    180     public ServerACL ServerACLs
    181     {
    182         get
    183         {
    184             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.server_acl");
    185             if (stateEvent == null)
    186             {
    187                 InitialState.Add(new StateEvent()
    188                 {
    189                     Type = "m.room.server_acl",
    190                     Content = new JsonObject()
    191                     {
    192                         ["allow"] = new JsonArray()
    193                         {
    194                             "*"
    195                         },
    196                         ["deny"] = new JsonArray()
    197                     }
    198                 });
    199                 return new ServerACL()
    200                 {
    201                     Allow = new List<string>()
    202                     {
    203                         "*"
    204                     },
    205                     Deny = new List<string>(),
    206                     AllowIpLiterals = true
    207                 };
    208             }
    209             return new ServerACL()
    210             {
    211                 Allow = JsonSerializer.Deserialize<List<string>>(stateEvent.ContentAsJsonNode["allow"]),
    212                 Deny = JsonSerializer.Deserialize<List<string>>(stateEvent.ContentAsJsonNode["deny"]),
    213                 AllowIpLiterals = true
    214             };
    215         }
    216         set
    217         {
    218             Console.WriteLine($"Setting server acl to {value.ToJson()}");
    219             var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.server_acl");
    220             if (stateEvent == null)
    221             {
    222                 InitialState.Add(new StateEvent()
    223                 {
    224                     Type = "m.room.server_acl",
    225                     Content = new JsonObject()
    226                     {
    227                         ["allow"] = JsonArray.Parse(JsonSerializer.Serialize(value.Allow)),
    228                         ["deny"] = JsonArray.Parse(JsonSerializer.Serialize(value.Deny))
    229                         ["allow_ip_literals"] = value.AllowIpLiterals
    230                     }
    231                 });
    232             }
    233             else
    234             {
    235                 var v = stateEvent.ContentAsJsonNode;
    236                 v["allow"] = JsonArray.Parse(JsonSerializer.Serialize(value.Allow));
    237                 v["deny"] = JsonArray.Parse(JsonSerializer.Serialize(value.Deny));
    238                 v["allow_ip_literals"] = value.AllowIpLiterals;
    239                 stateEvent.ContentAsJsonNode = v;
    240                 Console.WriteLine($"v={v.ToJson()}");
    241                 Console.WriteLine($"stateEvent.ContentAsJsonNode={stateEvent.ContentAsJsonNode.ToJson()}");
    242             }
    243         }
    244     }
    245 
    246 
    247     [JsonIgnore] public CreationContentBaseType _creationContentBaseType;
    248 
    249     public CreateRoomRequest() => _creationContentBaseType = new(this);
    250 
    251 
    252     public Dictionary<string, string> Validate()
    253     {
    254         Dictionary<string, string> errors = new();
    255         if (!Regex.IsMatch(RoomAliasName, @"[a-zA-Z0-9_\-]+$"))
    256             errors.Add("room_alias_name", "Room alias name must only contain letters, numbers, underscores, and hyphens.");
    257 
    258         return errors;
    259     }
    260 }
    261 
    262 public class CreationContentBaseType
    263 {
    264     private readonly CreateRoomRequest createRoomRequest;
    265 
    266     public CreationContentBaseType(CreateRoomRequest createRoomRequest)
    267     {
    268         this.createRoomRequest = createRoomRequest;
    269     }
    270 
    271     [JsonPropertyName("type")]
    272     public string Type
    273     {
    274         get => (string)createRoomRequest.CreationContent["type"];
    275         set
    276         {
    277             if (value is "null" or "") createRoomRequest.CreationContent.Remove("type");
    278             else createRoomRequest.CreationContent["type"] = value;
    279         }
    280     }
    281 }
    282 
    283 public class PowerLevelEvent
    284 {
    285     [JsonPropertyName("ban")] public int Ban { get; set; } // = 50;
    286     [JsonPropertyName("events_default")] public int EventsDefault { get; set; } // = 0;
    287     [JsonPropertyName("events")] public Dictionary<string, int> Events { get; set; } // = null!;
    288     [JsonPropertyName("invite")] public int Invite { get; set; } // = 50;
    289     [JsonPropertyName("kick")] public int Kick { get; set; } // = 50;
    290     [JsonPropertyName("notifications")] public NotificationsPL NotificationsPl { get; set; } // = null!;
    291     [JsonPropertyName("redact")] public int Redact { get; set; } // = 50;
    292     [JsonPropertyName("state_default")] public int StateDefault { get; set; } // = 50;
    293     [JsonPropertyName("users")] public Dictionary<string, int> Users { get; set; } // = null!;
    294     [JsonPropertyName("users_default")] public int UsersDefault { get; set; } // = 0;
    295 }
    296 
    297 public class NotificationsPL
    298 {
    299     [JsonPropertyName("room")] public int Room { get; set; } = 50;
    300 }
    301 
    302 public class ServerACL
    303 {
    304     [JsonPropertyName("allow")] public List<string> Allow { get; set; } // = null!;
    305     [JsonPropertyName("deny")] public List<string> Deny { get; set; } // = null!;
    306     [JsonPropertyName("allow_ip_literals")] public bool AllowIpLiterals { get; set; } // = false;
    307 }