From 712ad189c99570f686ab779782b2a873e172428e Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 13 Jun 2023 20:25:05 +0200 Subject: Change syntax style --- .../Responses/Admin/AdminRoomListingResult.cs | 6 +- .../Responses/CreateRoomRequest.cs | 234 +++++++++------------ MatrixRoomUtils.Core/Responses/LoginResponse.cs | 19 +- MatrixRoomUtils.Core/Responses/ProfileResponse.cs | 4 +- .../Responses/StateEventResponse.cs | 24 ++- 5 files changed, 132 insertions(+), 155 deletions(-) (limited to 'MatrixRoomUtils.Core/Responses') diff --git a/MatrixRoomUtils.Core/Responses/Admin/AdminRoomListingResult.cs b/MatrixRoomUtils.Core/Responses/Admin/AdminRoomListingResult.cs index 8ec0e4f..37bb3ba 100644 --- a/MatrixRoomUtils.Core/Responses/Admin/AdminRoomListingResult.cs +++ b/MatrixRoomUtils.Core/Responses/Admin/AdminRoomListingResult.cs @@ -2,8 +2,7 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Responses.Admin; -public class AdminRoomListingResult -{ +public class AdminRoomListingResult { [JsonPropertyName("offset")] public int Offset { get; set; } @@ -19,8 +18,7 @@ public class AdminRoomListingResult [JsonPropertyName("rooms")] public List Rooms { get; set; } = new(); - public class AdminRoomListingResultRoom - { + public class AdminRoomListingResultRoom { [JsonPropertyName("room_id")] public string RoomId { get; set; } diff --git a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs index 5df99f7..da7d569 100644 --- a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs +++ b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs @@ -6,58 +6,56 @@ using MatrixRoomUtils.Core.Extensions; namespace MatrixRoomUtils.Core.Responses; -public class CreateRoomRequest -{ - [JsonPropertyName("name")] public string Name { get; set; } = null!; +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!; + [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("initial_state")] + public List 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(); + [JsonPropertyName("creation_content")] + public JsonObject CreationContent { get; set; } = new(); /// - /// For use only when you can't use the CreationContent property + /// For use only when you can't use the CreationContent property /// - public StateEvent this[string event_type, string event_key = ""] - { + public StateEvent this[string event_type, string event_key = ""] { get => InitialState.First(x => x.Type == event_type && x.StateKey == event_key); - set - { + 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 - { + public string HistoryVisibility { + get { var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility"); - if (stateEvent == null) - { - InitialState.Add(new StateEvent() - { + if (stateEvent == null) { + InitialState.Add(new StateEvent { Type = "m.room.history_visibility", - Content = new JsonObject() - { + Content = new JsonObject { ["history_visibility"] = "shared" } }); @@ -66,22 +64,16 @@ public class CreateRoomRequest return stateEvent.ContentAsJsonNode["history_visibility"].GetValue(); } - set - { + set { var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility"); if (stateEvent == null) - { - InitialState.Add(new StateEvent() - { + InitialState.Add(new StateEvent { Type = "m.room.history_visibility", - Content = new JsonObject() - { + Content = new JsonObject { ["history_visibility"] = value } }); - } - else - { + else { var v = stateEvent.ContentAsJsonNode; v["history_visibility"] = value; stateEvent.ContentAsJsonNode = v; @@ -90,18 +82,13 @@ public class CreateRoomRequest } [JsonIgnore] - public string RoomIcon - { - get - { + public string RoomIcon { + get { var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar"); - if (stateEvent == null) - { - InitialState.Add(new StateEvent() - { + if (stateEvent == null) { + InitialState.Add(new StateEvent { Type = "m.room.avatar", - Content = new JsonObject() - { + Content = new JsonObject { ["url"] = "" } }); @@ -110,22 +97,16 @@ public class CreateRoomRequest return stateEvent.ContentAsJsonNode["url"].GetValue(); } - set - { + set { var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar"); if (stateEvent == null) - { - InitialState.Add(new StateEvent() - { + InitialState.Add(new StateEvent { Type = "m.room.avatar", - Content = new JsonObject() - { + Content = new JsonObject { ["url"] = value } }); - } - else - { + else { var v = stateEvent.ContentAsJsonNode; v["url"] = value; stateEvent.ContentAsJsonNode = v; @@ -177,64 +158,50 @@ public class CreateRoomRequest // } // } - public ServerACL ServerACLs - { - get - { + public ServerACL ServerACLs { + get { var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.server_acl"); - if (stateEvent == null) - { - InitialState.Add(new StateEvent() - { + if (stateEvent == null) { + InitialState.Add(new StateEvent { Type = "m.room.server_acl", - Content = new JsonObject() - { - ["allow"] = new JsonArray() - { + Content = new JsonObject { + ["allow"] = new JsonArray { "*" }, ["deny"] = new JsonArray() } }); - return new ServerACL() - { - Allow = new List() - { + return new ServerACL { + Allow = new List { "*" }, Deny = new List(), AllowIpLiterals = true }; } - return new ServerACL() - { - Allow = JsonSerializer.Deserialize>(stateEvent.ContentAsJsonNode["allow"]), - Deny = JsonSerializer.Deserialize>(stateEvent.ContentAsJsonNode["deny"]), + + return new ServerACL { + Allow = stateEvent.ContentAsJsonNode["allow"].Deserialize>(), + Deny = stateEvent.ContentAsJsonNode["deny"].Deserialize>(), AllowIpLiterals = true }; } - set - { + 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() - { + InitialState.Add(new StateEvent { Type = "m.room.server_acl", - Content = new JsonObject() - { - ["allow"] = JsonArray.Parse(JsonSerializer.Serialize(value.Allow)), - ["deny"] = JsonArray.Parse(JsonSerializer.Serialize(value.Deny)) - ["allow_ip_literals"] = value.AllowIpLiterals + Content = new JsonObject { + ["allow"] = JsonNode.Parse(JsonSerializer.Serialize(value.Allow)), + ["deny"] = JsonNode.Parse(JsonSerializer.Serialize(value.Deny)) + ["allow_ip_literals"] = value.AllowIpLiterals } }); - } - else - { + else { var v = stateEvent.ContentAsJsonNode; - v["allow"] = JsonArray.Parse(JsonSerializer.Serialize(value.Allow)); - v["deny"] = JsonArray.Parse(JsonSerializer.Serialize(value.Deny)); + 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()}"); @@ -243,14 +210,7 @@ public class CreateRoomRequest } } - - [JsonIgnore] public CreationContentBaseType _creationContentBaseType; - - public CreateRoomRequest() => _creationContentBaseType = new(this); - - - public Dictionary Validate() - { + 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."); @@ -259,49 +219,65 @@ public class CreateRoomRequest } } -public class CreationContentBaseType -{ +public class CreationContentBaseType { private readonly CreateRoomRequest createRoomRequest; - public CreationContentBaseType(CreateRoomRequest createRoomRequest) - { - this.createRoomRequest = createRoomRequest; - } + public CreationContentBaseType(CreateRoomRequest createRoomRequest) => this.createRoomRequest = createRoomRequest; [JsonPropertyName("type")] - public string Type - { + public string Type { get => (string)createRoomRequest.CreationContent["type"]; - set - { + 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 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 Users { get; set; } // = null!; - [JsonPropertyName("users_default")] public int UsersDefault { get; set; } // = 0; +public class PowerLevelEvent { + [JsonPropertyName("ban")] + public int Ban { get; set; } // = 50; + + [JsonPropertyName("events_default")] + public int EventsDefault { get; set; } // = 0; + + [JsonPropertyName("events")] + public Dictionary 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 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 NotificationsPL { + [JsonPropertyName("room")] + public int Room { get; set; } = 50; } -public class ServerACL -{ - [JsonPropertyName("allow")] public List Allow { get; set; } // = null!; - [JsonPropertyName("deny")] public List Deny { get; set; } // = null!; - [JsonPropertyName("allow_ip_literals")] public bool AllowIpLiterals { get; set; } // = false; +public class ServerACL { + [JsonPropertyName("allow")] + public List Allow { get; set; } // = null!; + + [JsonPropertyName("deny")] + public List Deny { get; set; } // = null!; + + [JsonPropertyName("allow_ip_literals")] + public bool AllowIpLiterals { get; set; } // = false; } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Responses/LoginResponse.cs b/MatrixRoomUtils.Core/Responses/LoginResponse.cs index 34b42d1..3259e44 100644 --- a/MatrixRoomUtils.Core/Responses/LoginResponse.cs +++ b/MatrixRoomUtils.Core/Responses/LoginResponse.cs @@ -4,27 +4,26 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Responses; -public class LoginResponse -{ +public class LoginResponse { [JsonPropertyName("access_token")] public string AccessToken { get; set; } + [JsonPropertyName("device_id")] public string DeviceId { get; set; } + [JsonPropertyName("home_server")] public string HomeServer { get; set; } + [JsonPropertyName("user_id")] public string UserId { get; set; } - - public async Task GetProfile() - { + + public async Task GetProfile() { var hc = new HttpClient(); var resp = await hc.GetAsync($"{HomeServer}/_matrix/client/r0/profile/{UserId}"); var data = await resp.Content.ReadFromJsonAsync(); - if(!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data.ToString()); + if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data); return data.Deserialize(); } - public async Task GetCanonicalHomeserverUrl() - { - return (await new RemoteHomeServer(HomeServer).Configure()).FullHomeServerDomain; - } + + public async Task GetCanonicalHomeserverUrl() => (await new RemoteHomeServer(HomeServer).Configure()).FullHomeServerDomain; } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Responses/ProfileResponse.cs b/MatrixRoomUtils.Core/Responses/ProfileResponse.cs index 2c0b679..db72386 100644 --- a/MatrixRoomUtils.Core/Responses/ProfileResponse.cs +++ b/MatrixRoomUtils.Core/Responses/ProfileResponse.cs @@ -2,10 +2,10 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Responses; -public class ProfileResponse -{ +public class ProfileResponse { [JsonPropertyName("avatar_url")] public string? AvatarUrl { get; set; } = ""; + [JsonPropertyName("displayname")] public string? DisplayName { get; set; } = ""; } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Responses/StateEventResponse.cs b/MatrixRoomUtils.Core/Responses/StateEventResponse.cs index 670c121..36f0a36 100644 --- a/MatrixRoomUtils.Core/Responses/StateEventResponse.cs +++ b/MatrixRoomUtils.Core/Responses/StateEventResponse.cs @@ -2,43 +2,47 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core; -public class StateEventResponse : StateEvent -{ +public class StateEventResponse : StateEvent { [JsonPropertyName("origin_server_ts")] public ulong OriginServerTs { get; set; } + [JsonPropertyName("room_id")] public string RoomId { get; set; } + [JsonPropertyName("sender")] public string Sender { get; set; } + [JsonPropertyName("unsigned")] public UnsignedData? Unsigned { get; set; } + [JsonPropertyName("event_id")] public string EventId { get; set; } + [JsonPropertyName("user_id")] public string UserId { get; set; } + [JsonPropertyName("replaces_state")] public string ReplacesState { get; set; } + [JsonPropertyName("prev_content")] public dynamic PrevContent { get; set; } - - - public class UnsignedData - { + + public class UnsignedData { [JsonPropertyName("age")] public ulong Age { get; set; } + [JsonPropertyName("prev_content")] public dynamic? PrevContent { get; set; } + [JsonPropertyName("redacted_because")] public dynamic? RedactedBecause { get; set; } + [JsonPropertyName("transaction_id")] public string? TransactionId { get; set; } - } } -public class StateEventResponse : StateEventResponse where T : class -{ - +public class StateEventResponse : StateEventResponse where T : class { [JsonPropertyName("content")] public T Content { get; set; } } \ No newline at end of file -- cgit 1.5.1