diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-25 03:07:05 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-25 03:07:05 +0200 |
commit | f866946fbbe962c712049327ade9dcbd43900295 (patch) | |
tree | 3400fcce20f68a6ef3eb130f4ef57733e346d0e9 /MatrixRoomUtils.Core/Responses | |
parent | Working sync (diff) | |
download | MatrixUtils-f866946fbbe962c712049327ade9dcbd43900295.tar.xz |
Working state, refactored Rory&::LibMatrix
Diffstat (limited to 'MatrixRoomUtils.Core/Responses')
5 files changed, 27 insertions, 247 deletions
diff --git a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs index da7d569..8719b5a 100644 --- a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs +++ b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs @@ -1,8 +1,8 @@ -using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Text.RegularExpressions; -using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Interfaces; +using MatrixRoomUtils.Core.StateEventTypes; namespace MatrixRoomUtils.Core.Responses; @@ -20,6 +20,7 @@ public class CreateRoomRequest { //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!; @@ -47,237 +48,12 @@ public class CreateRoomRequest { } } - //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."); + 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; } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Responses/CreationContentBaseType.cs b/MatrixRoomUtils.Core/Responses/CreationContentBaseType.cs new file mode 100644 index 0000000..743c552 --- /dev/null +++ b/MatrixRoomUtils.Core/Responses/CreationContentBaseType.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace MatrixRoomUtils.Core.Responses; + +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; + } + } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Responses/LoginResponse.cs b/MatrixRoomUtils.Core/Responses/LoginResponse.cs index 3259e44..8d0d94f 100644 --- a/MatrixRoomUtils.Core/Responses/LoginResponse.cs +++ b/MatrixRoomUtils.Core/Responses/LoginResponse.cs @@ -1,6 +1,7 @@ using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; +using MatrixRoomUtils.Core.StateEventTypes; namespace MatrixRoomUtils.Core.Responses; @@ -19,7 +20,7 @@ public class LoginResponse { public async Task<ProfileResponse> GetProfile() { var hc = new HttpClient(); - var resp = await hc.GetAsync($"{HomeServer}/_matrix/client/r0/profile/{UserId}"); + var resp = await hc.GetAsync($"{HomeServer}/_matrix/client/v3/profile/{UserId}"); var data = await resp.Content.ReadFromJsonAsync<JsonElement>(); if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data); return data.Deserialize<ProfileResponse>(); diff --git a/MatrixRoomUtils.Core/Responses/ProfileResponse.cs b/MatrixRoomUtils.Core/Responses/ProfileResponse.cs deleted file mode 100644 index db72386..0000000 --- a/MatrixRoomUtils.Core/Responses/ProfileResponse.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Text.Json.Serialization; - -namespace MatrixRoomUtils.Core.Responses; - -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 7b138e0..6e67887 100644 --- a/MatrixRoomUtils.Core/Responses/StateEventResponse.cs +++ b/MatrixRoomUtils.Core/Responses/StateEventResponse.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using MatrixRoomUtils.Core.Interfaces; namespace MatrixRoomUtils.Core.Responses; @@ -26,7 +27,7 @@ public class StateEventResponse : StateEvent { [JsonPropertyName("prev_content")] public dynamic PrevContent { get; set; } - + public class UnsignedData { [JsonPropertyName("age")] public ulong Age { get; set; } @@ -40,9 +41,4 @@ public class StateEventResponse : StateEvent { [JsonPropertyName("transaction_id")] public string? TransactionId { get; set; } } -} - -public class StateEventResponse<T> : StateEventResponse where T : class { - [JsonPropertyName("content")] - public T Content { get; set; } } \ No newline at end of file |