about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Room.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 20:25:05 +0200
commit712ad189c99570f686ab779782b2a873e172428e (patch)
tree6102e4719416e71522e9143fa4e06951258bd77c /MatrixRoomUtils.Core/Room.cs
parentFix passwords being visible during editing (diff)
downloadMatrixUtils-712ad189c99570f686ab779782b2a873e172428e.tar.xz
Change syntax style
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs112
1 files changed, 50 insertions, 62 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index f228271..a867c0c 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -1,6 +1,4 @@
-using System.Diagnostics.CodeAnalysis;
 using System.Net.Http.Json;
-using System.Reflection;
 using System.Text.Json;
 using System.Text.Json.Serialization;
 using System.Web;
@@ -8,26 +6,23 @@ using MatrixRoomUtils.Core.Extensions;
 
 namespace MatrixRoomUtils.Core;
 
-public class Room
-{
+public class Room {
     private readonly HttpClient _httpClient;
-    public string RoomId { get; set; }
 
-    public Room(HttpClient httpClient, string roomId)
-    {
+    public Room(HttpClient httpClient, string roomId) {
         _httpClient = httpClient;
         RoomId = roomId;
     }
 
-    public async Task<JsonElement?> GetStateAsync(string type, string stateKey = "", bool logOnFailure = true)
-    {
+    public string RoomId { get; set; }
+
+    public async Task<JsonElement?> GetStateAsync(string type, string stateKey = "", bool logOnFailure = true) {
         var url = $"/_matrix/client/v3/rooms/{RoomId}/state";
         if (!string.IsNullOrEmpty(type)) url += $"/{type}";
         if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}";
 
         var res = await _httpClient.GetAsync(url);
-        if (!res.IsSuccessStatusCode)
-        {
+        if (!res.IsSuccessStatusCode) {
             if (logOnFailure) Console.WriteLine($"{RoomId}/{stateKey}/{type} - got status: {res.StatusCode}");
             return null;
         }
@@ -36,20 +31,17 @@ public class Room
         return result;
     }
 
-    public async Task<T?> GetStateAsync<T>(string type, string stateKey = "", bool logOnFailure = false)
-    {
+    public async Task<T?> GetStateAsync<T>(string type, string stateKey = "", bool logOnFailure = false) {
         var res = await GetStateAsync(type, stateKey, logOnFailure);
         if (res == null) return default;
         return res.Value.Deserialize<T>();
     }
-    
-    public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b", string filter = "")
-    {
+
+    public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b", string filter = "") {
         var url = $"/_matrix/client/r0/rooms/{RoomId}/messages?from={from}&limit={limit}&dir={dir}";
         if (!string.IsNullOrEmpty(filter)) url += $"&filter={filter}";
         var res = await _httpClient.GetAsync(url);
-        if (!res.IsSuccessStatusCode)
-        {
+        if (!res.IsSuccessStatusCode) {
             Console.WriteLine($"Failed to get messages for {RoomId} - got status: {res.StatusCode}");
             throw new Exception($"Failed to get messages for {RoomId} - got status: {res.StatusCode}");
         }
@@ -58,11 +50,9 @@ public class Room
         return result ?? new MessagesResponse();
     }
 
-    public async Task<string> GetNameAsync()
-    {
+    public async Task<string> GetNameAsync() {
         var res = await GetStateAsync("m.room.name");
-        if (!res.HasValue)
-        {
+        if (!res.HasValue) {
             Console.WriteLine($"Room {RoomId} has no name!");
             return RoomId;
         }
@@ -72,22 +62,19 @@ public class Room
         return resn;
     }
 
-    public async Task JoinAsync(string[]? homeservers = null)
-    {
-        string join_url = $"/_matrix/client/r0/join/{HttpUtility.UrlEncode(RoomId)}";
+    public async Task JoinAsync(string[]? homeservers = null) {
+        var join_url = $"/_matrix/client/r0/join/{HttpUtility.UrlEncode(RoomId)}";
         Console.WriteLine($"Calling {join_url} with {homeservers?.Length ?? 0} via's...");
         if (homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':')[1] };
         var fullJoinUrl = $"{join_url}?server_name=" + string.Join("&server_name=", homeservers);
         var res = await _httpClient.PostAsync(fullJoinUrl, null);
     }
 
-    public async Task<List<string>> GetMembersAsync(bool joinedOnly = true)
-    {
+    public async Task<List<string>> GetMembersAsync(bool joinedOnly = true) {
         var res = await GetStateAsync("");
         if (!res.HasValue) return new List<string>();
         var members = new List<string>();
-        foreach (var member in res.Value.EnumerateArray())
-        {
+        foreach (var member in res.Value.EnumerateArray()) {
             if (member.GetProperty("type").GetString() != "m.room.member") continue;
             if (joinedOnly && member.GetProperty("content").GetProperty("membership").GetString() != "join") continue;
             var memberId = member.GetProperty("state_key").GetString();
@@ -97,63 +84,52 @@ public class Room
         return members;
     }
 
-    public async Task<List<string>> GetAliasesAsync()
-    {
+    public async Task<List<string>> GetAliasesAsync() {
         var res = await GetStateAsync("m.room.aliases");
         if (!res.HasValue) return new List<string>();
         var aliases = new List<string>();
-        foreach (var alias in res.Value.GetProperty("aliases").EnumerateArray())
-        {
-            aliases.Add(alias.GetString() ?? "");
-        }
+        foreach (var alias in res.Value.GetProperty("aliases").EnumerateArray()) aliases.Add(alias.GetString() ?? "");
 
         return aliases;
     }
 
-    public async Task<string> GetCanonicalAliasAsync()
-    {
+    public async Task<string> GetCanonicalAliasAsync() {
         var res = await GetStateAsync("m.room.canonical_alias");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("alias").GetString() ?? "";
     }
 
-    public async Task<string> GetTopicAsync()
-    {
+    public async Task<string> GetTopicAsync() {
         var res = await GetStateAsync("m.room.topic");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("topic").GetString() ?? "";
     }
 
-    public async Task<string> GetAvatarUrlAsync()
-    {
+    public async Task<string> GetAvatarUrlAsync() {
         var res = await GetStateAsync("m.room.avatar");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("url").GetString() ?? "";
     }
 
-    public async Task<JoinRules> GetJoinRuleAsync()
-    {
+    public async Task<JoinRules> GetJoinRuleAsync() {
         var res = await GetStateAsync("m.room.join_rules");
         if (!res.HasValue) return new JoinRules();
         return res.Value.Deserialize<JoinRules>() ?? new JoinRules();
     }
 
-    public async Task<string> GetHistoryVisibilityAsync()
-    {
+    public async Task<string> GetHistoryVisibilityAsync() {
         var res = await GetStateAsync("m.room.history_visibility");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("history_visibility").GetString() ?? "";
     }
 
-    public async Task<string> GetGuestAccessAsync()
-    {
+    public async Task<string> GetGuestAccessAsync() {
         var res = await GetStateAsync("m.room.guest_access");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("guest_access").GetString() ?? "";
     }
 
-    public async Task<CreateEvent> GetCreateEventAsync()
-    {
+    public async Task<CreateEvent> GetCreateEventAsync() {
         var res = await GetStateAsync("m.room.create");
         if (!res.HasValue) return new CreateEvent();
 
@@ -163,33 +139,45 @@ public class Room
     }
 }
 
-public class MessagesResponse
-{
+public class MessagesResponse {
     [JsonPropertyName("start")]
     public string Start { get; set; }
+
     [JsonPropertyName("end")]
     public string? End { get; set; }
+
     [JsonPropertyName("chunk")]
     public List<StateEventResponse> Chunk { get; set; } = new();
+
     [JsonPropertyName("state")]
     public List<StateEventResponse> State { get; set; } = new();
 }
 
-public class CreateEvent
-{
-    [JsonPropertyName("creator")] public string Creator { get; set; }
-    [JsonPropertyName("room_version")] public string RoomVersion { get; set; }
-    [JsonPropertyName("type")] public string? Type { get; set; }
-    [JsonPropertyName("predecessor")] public object? Predecessor { get; set; }
-    [JsonPropertyName("m.federate")] public bool Federate { get; set; }
+public class CreateEvent {
+    [JsonPropertyName("creator")]
+    public string Creator { get; set; }
+
+    [JsonPropertyName("room_version")]
+    public string RoomVersion { get; set; }
+
+    [JsonPropertyName("type")]
+    public string? Type { get; set; }
+
+    [JsonPropertyName("predecessor")]
+    public object? Predecessor { get; set; }
+
+    [JsonPropertyName("m.federate")]
+    public bool Federate { get; set; }
 }
 
-public class JoinRules
-{
+public class JoinRules {
     private const string Public = "public";
     private const string Invite = "invite";
     private const string Knock = "knock";
 
-    [JsonPropertyName("join_rule")] public string JoinRule { get; set; }
-    [JsonPropertyName("allow")] public List<string> Allow { get; set; }
+    [JsonPropertyName("join_rule")]
+    public string JoinRule { get; set; }
+
+    [JsonPropertyName("allow")]
+    public List<string> Allow { get; set; }
 }
\ No newline at end of file