From bb8c2637af3b7982e7a4b2fd15e2fbec613d0848 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 30 Jun 2023 03:36:58 +0200 Subject: Todays progress --- MatrixRoomUtils.Core/Interfaces/IHomeServer.cs | 42 +++++++++++----------- .../Interfaces/Services/IStorageProvider.cs | 28 +++++++-------- 2 files changed, 36 insertions(+), 34 deletions(-) (limited to 'MatrixRoomUtils.Core/Interfaces') diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs index 4fa6c1b..4ee2a3e 100644 --- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs +++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs @@ -6,34 +6,36 @@ using MatrixRoomUtils.Core.StateEventTypes; namespace MatrixRoomUtils.Core.Interfaces; public class IHomeServer { - private readonly Dictionary _profileCache = new(); + private readonly Dictionary _profileCache = new(); public string HomeServerDomain { get; set; } public string FullHomeServerDomain { get; set; } protected internal MatrixHttpClient _httpClient { get; set; } = new(); - public async Task GetProfile(string mxid, bool debounce = false, bool cache = true) { - if (cache) { - if (debounce) await Task.Delay(Random.Shared.Next(100, 500)); - if (_profileCache.ContainsKey(mxid)) { - while (_profileCache[mxid] == null) { - Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson() ?? "null"} within {_profileCache.Count} profiles..."); - await Task.Delay(Random.Shared.Next(50, 500)); - } - - return _profileCache[mxid]; - } + // if (cache) { + // if (debounce) await Task.Delay(Random.Shared.Next(100, 500)); + // if (_profileCache.ContainsKey(mxid)) { + // while (_profileCache[mxid] == null) { + // Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson() ?? "null"} within {_profileCache.Count} profiles..."); + // await Task.Delay(Random.Shared.Next(50, 500)); + // } + // + // return _profileCache[mxid]; + // } + // } + if(mxid is null) throw new ArgumentNullException(nameof(mxid)); + if (_profileCache.ContainsKey(mxid)) { + if (_profileCache[mxid] is SemaphoreSlim s) await s.WaitAsync(); + if (_profileCache[mxid] is ProfileResponse p) return p; } - - _profileCache.Add(mxid, null); + _profileCache[mxid] = new SemaphoreSlim(1); + var resp = await _httpClient.GetAsync($"/_matrix/client/v3/profile/{mxid}"); - var data = await resp.Content.ReadFromJsonAsync(); + var data = await resp.Content.ReadFromJsonAsync(); if (!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data); - var profile = data.Deserialize(); - _profileCache[mxid] = profile; - return profile; + _profileCache[mxid] = data; + + return data; } - - public string? ResolveMediaUri(string mxc) => mxc.Replace("mxc://", $"{FullHomeServerDomain}/_matrix/media/v3/download/"); } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Interfaces/Services/IStorageProvider.cs b/MatrixRoomUtils.Core/Interfaces/Services/IStorageProvider.cs index 01314da..eefb79c 100644 --- a/MatrixRoomUtils.Core/Interfaces/Services/IStorageProvider.cs +++ b/MatrixRoomUtils.Core/Interfaces/Services/IStorageProvider.cs @@ -2,45 +2,45 @@ namespace MatrixRoomUtils.Core.Interfaces.Services; public interface IStorageProvider { // save all children of a type with reflection - public Task SaveAllChildren(string key, T value) { + public Task SaveAllChildrenAsync(string key, T value) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveAllChildren(key, value)!"); - return Task.CompletedTask; + throw new NotImplementedException(); } // load all children of a type with reflection - public Task LoadAllChildren(string key) { + public Task LoadAllChildrenAsync(string key) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement LoadAllChildren(key)!"); - return Task.FromResult(default(T)); + throw new NotImplementedException(); } - public Task SaveObject(string key, T value) { + public Task SaveObjectAsync(string key, T value) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveObject(key, value)!"); - return Task.CompletedTask; + throw new NotImplementedException(); } // load - public Task LoadObject(string key) { + public Task LoadObjectAsync(string key) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement LoadObject(key)!"); - return Task.FromResult(default(T)); + throw new NotImplementedException(); } // check if exists - public Task ObjectExists(string key) { + public Task ObjectExistsAsync(string key) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement ObjectExists(key)!"); - return Task.FromResult(false); + throw new NotImplementedException(); } // get all keys - public Task> GetAllKeys() { + public Task> GetAllKeysAsync() { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement GetAllKeys()!"); - return Task.FromResult(new List()); + throw new NotImplementedException(); } // delete - public Task DeleteObject(string key) { + public Task DeleteObjectAsync(string key) { Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement DeleteObject(key)!"); - return Task.CompletedTask; + throw new NotImplementedException(); } } \ No newline at end of file -- cgit 1.5.1