about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Room.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-27 02:23:30 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-27 02:23:30 +0200
commitac7ed016b00941380099d9a0b2601f4bc353f39f (patch)
treea0f9fc1baee763a528aef2cd04a920815ef3906d /MatrixRoomUtils.Core/Room.cs
parentBeen a while since I last committed (diff)
downloadMatrixUtils-ac7ed016b00941380099d9a0b2601f4bc353f39f.tar.xz
Remove a bunch of caching, make room listings more reliable
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs123
1 files changed, 34 insertions, 89 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index b96546e..73a2bc5 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -10,8 +10,6 @@ namespace MatrixRoomUtils.Core;
 
 public class Room
 {
-    private static SemaphoreSlim _semaphore = new SemaphoreSlim(16, 16);
-
     private readonly HttpClient _httpClient;
     public string RoomId { get; set; }
 
@@ -21,72 +19,26 @@ public class Room
         RoomId = roomId;
     }
 
-    public async Task<JsonElement?> GetStateAsync(string type, string state_key = "", bool logOnFailure = false)
+    public async Task<JsonElement?> GetStateAsync(string type, string stateKey = "", bool logOnFailure = true)
     {
-        await _semaphore.WaitAsync();
         var url = $"/_matrix/client/v3/rooms/{RoomId}/state";
-        var stateCombo = "";
-        if (!string.IsNullOrEmpty(state_key)) stateCombo += $"{type}/{state_key}";
-        else if (!string.IsNullOrEmpty(type)) stateCombo += $"{type}";
-        if (!string.IsNullOrEmpty(stateCombo)) url += $"/{stateCombo}";
-        var cache_key = "room_states#" + RoomId;
-        if (!RuntimeCache.GenericResponseCache.ContainsKey(cache_key))
-        {
-            Console.WriteLine($"[!!] No cache for {cache_key}, creating...");
-            RuntimeCache.GenericResponseCache.Add(cache_key, new ObjectCache<object?>()
-            {
-                Name = cache_key
-            });
-        }
-        var cache = RuntimeCache.GenericResponseCache[cache_key];
-
-        if (cache.ContainsKey(stateCombo))
-        {
-            if (cache[stateCombo].ExpiryTime > DateTime.Now)
-            {
-                // Console.WriteLine($"[:3] Found cached state: {RuntimeCache.GenericResponseCache[cache_key][url].Result}");
-                _semaphore.Release();
-                return (JsonElement?) cache[stateCombo].Result;
-            }
-            else
-            {
-                Console.WriteLine($"[!!] Cached state expired at {cache[stateCombo].ExpiryTime}: {cache[stateCombo].Result}");
-                if(cache[stateCombo].ExpiryTime == null)Console.WriteLine("Exiryt time was null");
-            }
-        }
-        // else
-        // {
-        //     Console.WriteLine($"[!!] No cached state for {url}");
-        // }
+        if (!string.IsNullOrEmpty(type)) url += $"/{type}";
+        if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}";
 
         var res = await _httpClient.GetAsync(url);
         if (!res.IsSuccessStatusCode)
         {
-            if (logOnFailure) Console.WriteLine($"{RoomId}/{state_key}/{type} - got status: {res.StatusCode}");
-            _semaphore.Release();
+            if (logOnFailure) Console.WriteLine($"{RoomId}/{stateKey}/{type} - got status: {res.StatusCode}");
             return null;
         }
 
         var result = await res.Content.ReadFromJsonAsync<JsonElement>();
-        var expiryTime = type switch
-        {
-            "m.room.name" => TimeSpan.FromMinutes(30),
-            "org.matrix.mjolnir.shortcode" => TimeSpan.FromHours(4),
-            "" => TimeSpan.FromSeconds(0),
-            _ => TimeSpan.FromMinutes(15)
-        };
-        if(!string.IsNullOrWhiteSpace(type) && !string.IsNullOrWhiteSpace(state_key))
-            cache[stateCombo] = new GenericResult<object>()
-            {
-                Result = result,
-                ExpiryTime = DateTime.Now.Add(expiryTime)
-            };
-        _semaphore.Release();
         return result;
     }
-    public async Task<T?> GetStateAsync<T>(string type, string state_key = "", bool logOnFailure = false)
+
+    public async Task<T?> GetStateAsync<T>(string type, string stateKey = "", bool logOnFailure = false)
     {
-        var res = await GetStateAsync(type, state_key, logOnFailure);
+        var res = await GetStateAsync(type, stateKey, logOnFailure);
         if (res == null) return default;
         return res.Value.Deserialize<T>();
     }
@@ -108,27 +60,28 @@ public class Room
     public async Task JoinAsync(string[]? homeservers = null)
     {
         string join_url = $"/_matrix/client/r0/join/{HttpUtility.UrlEncode(RoomId)}";
-        Console.WriteLine($"Calling {join_url} with {(homeservers == null ? 0 : homeservers.Length)} via's...");
-        if(homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':')[1] };
-        var full_join_url = $"{join_url}?server_name=" + string.Join("&server_name=", homeservers);
-        var res = await _httpClient.PostAsync(full_join_url, null);
+        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()
+
+    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())
         {
-            if(member.GetProperty("Type").GetString() != "m.room.member") continue;
-            var member_id = member.GetProperty("StateKey").GetString();
-            members.Add(member_id);
+            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();
+            members.Add(memberId ?? throw new InvalidOperationException("Event type was member but state key was null!"));
         }
 
         return members;
     }
-    
+
     public async Task<List<string>> GetAliasesAsync()
     {
         var res = await GetStateAsync("m.room.aliases");
@@ -141,54 +94,54 @@ public class Room
 
         return aliases;
     }
-    
+
     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()
     {
         var res = await GetStateAsync("m.room.topic");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("topic").GetString() ?? "";
     }
-    
+
     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()
     {
         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()
     {
         var res = await GetStateAsync("m.room.history_visibility");
         if (!res.HasValue) return "";
         return res.Value.GetProperty("history_visibility").GetString() ?? "";
     }
-    
+
     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()
     {
         var res = await GetStateAsync("m.room.create");
         if (!res.HasValue) return new CreateEvent();
-        
+
         res.FindExtraJsonFields(typeof(CreateEvent));
 
         return res.Value.Deserialize<CreateEvent>() ?? new CreateEvent();
@@ -197,17 +150,11 @@ public class Room
 
 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; }
+    [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
@@ -215,9 +162,7 @@ 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