diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-23 08:51:02 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-23 08:51:02 +0200 |
commit | 3ef3e5caa65458e595e2303358322626c7da1dda (patch) | |
tree | 6a60d401da8b5f906a423601ac4c135d44e896d1 /MatrixRoomUtils.Core/Room.cs | |
parent | Local changes (diff) | |
download | MatrixUtils-3ef3e5caa65458e595e2303358322626c7da1dda.tar.xz |
Add numerous new things
Diffstat (limited to 'MatrixRoomUtils.Core/Room.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Room.cs | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs index 64db03d..6798d5b 100644 --- a/MatrixRoomUtils.Core/Room.cs +++ b/MatrixRoomUtils.Core/Room.cs @@ -1,10 +1,13 @@ using System.Net.Http.Json; using System.Text.Json; +using System.Web; namespace MatrixRoomUtils.Core; public class Room { + private static SemaphoreSlim _semaphore = new SemaphoreSlim(16, 16); + private readonly HttpClient _httpClient; public string RoomId { get; set; } @@ -13,31 +16,34 @@ public class Room _httpClient = httpClient; RoomId = roomId; } - - public async Task<JsonElement?> GetStateAsync(string type, string state_key="", bool logOnFailure = false) + + public async Task<JsonElement?> GetStateAsync(string type, string state_key = "", bool logOnFailure = false) { + await _semaphore.WaitAsync(); var url = $"/_matrix/client/v3/rooms/{RoomId}/state"; if (!string.IsNullOrEmpty(state_key)) url += $"/{type}/{state_key}"; else if (!string.IsNullOrEmpty(type)) url += $"/{type}"; - var cache_key = "room_states_"+type; + var cache_key = "room_states:" + type; if (!RuntimeCache.GenericResponseCache.ContainsKey(cache_key)) { Console.WriteLine($"[!!] No cache for {cache_key}, creating..."); - RuntimeCache.GenericResponseCache.Add(cache_key, new ObjectCache<object?>() - { - DefaultExpiry = type switch - { - "m.room.name" => TimeSpan.FromMinutes(15), - _ => TimeSpan.FromMinutes(5) - } - }); + RuntimeCache.GenericResponseCache.Add(cache_key, new ObjectCache<object?>()); } - if (RuntimeCache.GenericResponseCache[cache_key][url] != null) + RuntimeCache.GenericResponseCache[cache_key].DefaultExpiry = type switch + { + "m.room.name" => TimeSpan.FromMinutes(30), + "org.matrix.mjolnir.shortcode" => TimeSpan.FromHours(4), + "" => TimeSpan.FromSeconds(0), + _ => TimeSpan.FromMinutes(15) + }; + + if (RuntimeCache.GenericResponseCache[cache_key].Cache.ContainsKey(url) && RuntimeCache.GenericResponseCache[cache_key][url] != null) { - if(RuntimeCache.GenericResponseCache[cache_key][url].ExpiryTime > DateTime.Now) + if (RuntimeCache.GenericResponseCache[cache_key][url].ExpiryTime > DateTime.Now) { // Console.WriteLine($"[:3] Found cached state: {RuntimeCache.GenericResponseCache[cache_key][url].Result}"); + _semaphore.Release(); return (JsonElement?)RuntimeCache.GenericResponseCache[cache_key][url].Result; } else @@ -45,35 +51,55 @@ public class Room Console.WriteLine($"[!!] Cached state expired at {RuntimeCache.GenericResponseCache[cache_key][url].ExpiryTime}: {RuntimeCache.GenericResponseCache[cache_key][url].Result}"); } } - else - { - Console.WriteLine($"[!!] No cached state for {url}"); - } + // else + // { + // Console.WriteLine($"[!!] No cached state for {url}"); + // } var res = await _httpClient.GetAsync(url); if (!res.IsSuccessStatusCode) { - if(logOnFailure) Console.WriteLine($"{RoomId}/{state_key}/{type} - got status: {res.StatusCode}"); + if (logOnFailure) Console.WriteLine($"{RoomId}/{state_key}/{type} - got status: {res.StatusCode}"); + _semaphore.Release(); return null; } + var result = await res.Content.ReadFromJsonAsync<JsonElement>(); + + if (!RuntimeCache.GenericResponseCache.ContainsKey(cache_key) && type != "") + { + Console.WriteLine($"[!!] No cache for {cache_key}, creating..."); + RuntimeCache.GenericResponseCache.Add(cache_key, new ObjectCache<object?>()); + } + RuntimeCache.GenericResponseCache[cache_key][url] = new GenericResult<object>() { Result = result }; + _semaphore.Release(); return result; } - public async Task<string?> GetNameAsync() + + public async Task<string> GetNameAsync() { var res = await GetStateAsync("m.room.name"); if (!res.HasValue) { Console.WriteLine($"Room {RoomId} has no name!"); - return null; + return RoomId; } - var resn = res?.TryGetProperty("name", out var name) ?? false ? name.GetString() : null; + + var resn = res?.TryGetProperty("name", out var name) ?? false ? name.GetString() ?? RoomId : RoomId; //Console.WriteLine($"Got name: {resn}"); return resn; } - + + 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); + } } \ No newline at end of file |