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 00:39:49 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-27 00:39:49 +0200
commita67276252c8bfcd6b6c5344e70debc6d67d917a9 (patch)
treeaf2806e9729f1013a5db6fbda80a7ad1cd7126fa /MatrixRoomUtils.Core/Room.cs
parentidk (diff)
downloadMatrixUtils-a67276252c8bfcd6b6c5344e70debc6d67d917a9.tar.xz
Been a while since I last committed
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r--MatrixRoomUtils.Core/Room.cs34
1 files changed, 20 insertions, 14 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs
index 362abf4..b96546e 100644
--- a/MatrixRoomUtils.Core/Room.cs
+++ b/MatrixRoomUtils.Core/Room.cs
@@ -40,14 +40,6 @@ public class Room
         }
         var cache = RuntimeCache.GenericResponseCache[cache_key];
 
-        cache.DefaultExpiry = type switch
-        {
-            "m.room.name" => TimeSpan.FromMinutes(30),
-            "org.matrix.mjolnir.shortcode" => TimeSpan.FromHours(4),
-            "" => TimeSpan.FromSeconds(0),
-            _ => TimeSpan.FromMinutes(15)
-        };
-
         if (cache.ContainsKey(stateCombo))
         {
             if (cache[stateCombo].ExpiryTime > DateTime.Now)
@@ -76,14 +68,28 @@ public class Room
         }
 
         var result = await res.Content.ReadFromJsonAsync<JsonElement>();
-
-        cache[stateCombo] = new GenericResult<object>()
+        var expiryTime = type switch
         {
-            Result = result
+            "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)
+    {
+        var res = await GetStateAsync(type, state_key, logOnFailure);
+        if (res == null) return default;
+        return res.Value.Deserialize<T>();
+    }
 
     public async Task<string> GetNameAsync()
     {
@@ -115,8 +121,8 @@ public class Room
         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("state_key").GetString();
+            if(member.GetProperty("Type").GetString() != "m.room.member") continue;
+            var member_id = member.GetProperty("StateKey").GetString();
             members.Add(member_id);
         }
 
@@ -196,7 +202,7 @@ public class CreateEvent
     [JsonPropertyName("room_version")]
     public string RoomVersion { get; set; }
     [JsonPropertyName("type")]
-    public string Type { get; set; }
+    public string? Type { get; set; }
     [JsonPropertyName("predecessor")]
     public object? Predecessor { get; set; }