From 9d838e66ad667958c6e5500bff336a30293174e9 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 10 Aug 2024 19:33:33 +0000 Subject: [PATCH] Added & modified several message types. --- HermesSocketLibrary.csproj | 8 +---- Requests/Callbacks/CallbackManager.cs | 43 ++++++++++++++++++++++++++ Requests/Callbacks/ICallbackManager.cs | 9 ++++++ Requests/IRequest.cs | 2 +- Requests/IRequestManager.cs | 9 ++++++ Requests/Messages/EmoteInfo.cs | 5 --- Requests/Messages/Group.cs | 9 ++++++ Requests/Messages/GroupChatter.cs | 8 +++++ Requests/Messages/GroupInfo.cs | 9 ++++++ Requests/Messages/GroupPermission.cs | 10 ++++++ Requests/Messages/RedeemableAction.cs | 9 ++++++ Requests/Messages/Redemption.cs | 11 +++++++ Requests/Messages/TTSWordFilter.cs | 6 +++- Requests/RequestManager.cs | 27 ++-------------- Socket/Data/Connection.cs | 19 ++++++++++++ Socket/Data/EmoteDetailsMessage.cs | 2 +- Socket/Data/EmoteUsageMessage.cs | 2 +- Socket/Data/ErrorMessage.cs | 20 ------------ Socket/Data/LoggingMessage.cs | 41 ++++++++++++++++++++++++ Socket/Data/RequestMessage.cs | 1 + Socket/Data/SocketMessage.cs | 12 ------- 21 files changed, 190 insertions(+), 72 deletions(-) create mode 100644 Requests/Callbacks/CallbackManager.cs create mode 100644 Requests/Callbacks/ICallbackManager.cs create mode 100644 Requests/IRequestManager.cs create mode 100644 Requests/Messages/Group.cs create mode 100644 Requests/Messages/GroupChatter.cs create mode 100644 Requests/Messages/GroupInfo.cs create mode 100644 Requests/Messages/GroupPermission.cs create mode 100644 Requests/Messages/RedeemableAction.cs create mode 100644 Requests/Messages/Redemption.cs create mode 100644 Socket/Data/Connection.cs delete mode 100644 Socket/Data/ErrorMessage.cs create mode 100644 Socket/Data/LoggingMessage.cs delete mode 100644 Socket/Data/SocketMessage.cs diff --git a/HermesSocketLibrary.csproj b/HermesSocketLibrary.csproj index 33b508e..b623d3a 100644 --- a/HermesSocketLibrary.csproj +++ b/HermesSocketLibrary.csproj @@ -8,16 +8,10 @@ - - - - - - - + diff --git a/Requests/Callbacks/CallbackManager.cs b/Requests/Callbacks/CallbackManager.cs new file mode 100644 index 0000000..bfc1ace --- /dev/null +++ b/Requests/Callbacks/CallbackManager.cs @@ -0,0 +1,43 @@ +namespace HermesSocketLibrary.Requests.Callbacks +{ + public class CallbackManager : ICallbackManager where A : class + { + private readonly IDictionary _callbacks; + + + public CallbackManager() + { + _callbacks = new Dictionary(); + } + + + public string GenerateKeyForCallback(A callback) + { + string key = GenerateKey(); + _callbacks.Add(key, callback); + return key; + } + + public A? Get(string key) + { + if (_callbacks.TryGetValue(key, out var callback)) + return callback; + return null; + } + + public A? Take(string key) + { + if (_callbacks.TryGetValue(key, out var callback)) + { + _callbacks.Remove(key); + return callback; + } + return null; + } + + private string GenerateKey() + { + return Guid.NewGuid().ToString("D"); + } + } +} \ No newline at end of file diff --git a/Requests/Callbacks/ICallbackManager.cs b/Requests/Callbacks/ICallbackManager.cs new file mode 100644 index 0000000..bf22a06 --- /dev/null +++ b/Requests/Callbacks/ICallbackManager.cs @@ -0,0 +1,9 @@ +namespace HermesSocketLibrary.Requests.Callbacks +{ + public interface ICallbackManager where A : class + { + string GenerateKeyForCallback(A callback); + A? Get(string key); + A? Take(string key); + } +} \ No newline at end of file diff --git a/Requests/IRequest.cs b/Requests/IRequest.cs index f44bf94..cb89e51 100644 --- a/Requests/IRequest.cs +++ b/Requests/IRequest.cs @@ -4,6 +4,6 @@ namespace HermesSocketLibrary.Requests { string Name { get; } - Task Grant(string sender, IDictionary data); + Task Grant(string sender, IDictionary? data); } } \ No newline at end of file diff --git a/Requests/IRequestManager.cs b/Requests/IRequestManager.cs new file mode 100644 index 0000000..2eefd99 --- /dev/null +++ b/Requests/IRequestManager.cs @@ -0,0 +1,9 @@ +using HermesSocketLibrary.Socket.Data; + +namespace HermesSocketLibrary.Requests +{ + public interface IRequestManager + { + Task Grant(string sender, RequestMessage? message); + } +} \ No newline at end of file diff --git a/Requests/Messages/EmoteInfo.cs b/Requests/Messages/EmoteInfo.cs index 79666ef..c4c8e1c 100644 --- a/Requests/Messages/EmoteInfo.cs +++ b/Requests/Messages/EmoteInfo.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace HermesSocketLibrary.Requests.Messages { public class EmoteInfo diff --git a/Requests/Messages/Group.cs b/Requests/Messages/Group.cs new file mode 100644 index 0000000..b1a3894 --- /dev/null +++ b/Requests/Messages/Group.cs @@ -0,0 +1,9 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class Group + { + public string Id { get; set; } + public string Name { get; set; } + public int Priority { get; set; } + } +} \ No newline at end of file diff --git a/Requests/Messages/GroupChatter.cs b/Requests/Messages/GroupChatter.cs new file mode 100644 index 0000000..50c5f35 --- /dev/null +++ b/Requests/Messages/GroupChatter.cs @@ -0,0 +1,8 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class GroupChatter + { + public string GroupId { get; set; } + public long ChatterId { get; set;} + } +} \ No newline at end of file diff --git a/Requests/Messages/GroupInfo.cs b/Requests/Messages/GroupInfo.cs new file mode 100644 index 0000000..66903ff --- /dev/null +++ b/Requests/Messages/GroupInfo.cs @@ -0,0 +1,9 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class GroupInfo + { + public IEnumerable Groups { get; set; } + public IEnumerable GroupChatters { get; set; } + public IEnumerable GroupPermissions { get; set; } + } +} \ No newline at end of file diff --git a/Requests/Messages/GroupPermission.cs b/Requests/Messages/GroupPermission.cs new file mode 100644 index 0000000..4d7b21d --- /dev/null +++ b/Requests/Messages/GroupPermission.cs @@ -0,0 +1,10 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class GroupPermission + { + public string Id { get; set; } + public string GroupId { get; set; } + public string Path { get; set; } + public bool? Allow { get; set; } + } +} \ No newline at end of file diff --git a/Requests/Messages/RedeemableAction.cs b/Requests/Messages/RedeemableAction.cs new file mode 100644 index 0000000..e3689ec --- /dev/null +++ b/Requests/Messages/RedeemableAction.cs @@ -0,0 +1,9 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class RedeemableAction + { + public string Name { get; set; } + public string Type { get; set; } + public IDictionary Data { get; set; } + } +} \ No newline at end of file diff --git a/Requests/Messages/Redemption.cs b/Requests/Messages/Redemption.cs new file mode 100644 index 0000000..5046700 --- /dev/null +++ b/Requests/Messages/Redemption.cs @@ -0,0 +1,11 @@ +namespace HermesSocketLibrary.Requests.Messages +{ + public class Redemption + { + public string Id { get; set; } + public string RedemptionId { get; set; } + public string ActionName { get; set; } + public int Order { get; set; } + public bool State { get; set; } + } +} \ No newline at end of file diff --git a/Requests/Messages/TTSWordFilter.cs b/Requests/Messages/TTSWordFilter.cs index 394a3f1..c5a1de7 100644 --- a/Requests/Messages/TTSWordFilter.cs +++ b/Requests/Messages/TTSWordFilter.cs @@ -1,3 +1,6 @@ +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; + namespace HermesSocketLibrary.Requests.Messages { public class TTSWordFilter @@ -5,7 +8,8 @@ namespace HermesSocketLibrary.Requests.Messages public string? Id { get; set; } public string? Search { get; set; } public string? Replace { get; set; } - + [JsonIgnore] + public Regex? Regex { get; set; } public bool IsRegex { get; set; } diff --git a/Requests/RequestManager.cs b/Requests/RequestManager.cs index bdddd10..9d317bc 100644 --- a/Requests/RequestManager.cs +++ b/Requests/RequestManager.cs @@ -1,40 +1,19 @@ -using System.Collections.Concurrent; -using System.Reflection; using HermesSocketLibrary.Socket.Data; -using Microsoft.Extensions.DependencyInjection; using Serilog; namespace HermesSocketLibrary.Requests { - public abstract class RequestManager + public class RequestManager : IRequestManager { private readonly IDictionary _requests; - private readonly IServiceProvider _serviceProvider; private readonly ILogger _logger; - public RequestManager(IServiceProvider serviceProvider, ILogger logger) + public RequestManager(IEnumerable requests, ILogger logger) { - _serviceProvider = serviceProvider; _logger = logger; - _requests = new ConcurrentDictionary(); - LoadRequests(); - } - - protected abstract string AssemblyName { get; } - - private void LoadRequests() - { - Type basetype = typeof(IRequest); - var types = Assembly.Load(AssemblyName).GetTypes().Where(t => t.IsClass && !t.IsAbstract && basetype.IsAssignableFrom(t)); - - foreach (var type in types) - { - _logger.Debug($"Loading IRequest for '{type.Name}'."); - var request = _serviceProvider.GetRequiredKeyedService(type.Name); - _requests.Add(request.Name, request); - } + _requests = requests.ToDictionary(r => r.Name, r => r); } public async Task Grant(string sender, RequestMessage? message) diff --git a/Socket/Data/Connection.cs b/Socket/Data/Connection.cs new file mode 100644 index 0000000..7942c7e --- /dev/null +++ b/Socket/Data/Connection.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace HermesSocketLibrary.Socket.Data +{ + public class Connection + { + public string Name { get; set; } + public string Type { get; set; } + public string ClientId { get; set; } + public string AccessToken { get; set; } + public string GrantType { get; set; } + public string Scope { get; set; } + public DateTime ExpiresIn { get; set; } + public bool Default { get; set; } + } +} \ No newline at end of file diff --git a/Socket/Data/EmoteDetailsMessage.cs b/Socket/Data/EmoteDetailsMessage.cs index cdccf95..b2c9cff 100644 --- a/Socket/Data/EmoteDetailsMessage.cs +++ b/Socket/Data/EmoteDetailsMessage.cs @@ -2,6 +2,6 @@ namespace HermesSocketLibrary.Socket.Data { public class EmoteDetailsMessage { - public Dictionary Emotes { get; set; } + public IDictionary Emotes { get; set; } } } \ No newline at end of file diff --git a/Socket/Data/EmoteUsageMessage.cs b/Socket/Data/EmoteUsageMessage.cs index fb2ef7d..d5fcf9d 100644 --- a/Socket/Data/EmoteUsageMessage.cs +++ b/Socket/Data/EmoteUsageMessage.cs @@ -5,7 +5,7 @@ namespace HermesSocketLibrary.Socket.Data public string MessageId { get; set; } public DateTime DateTime { get; set; } public long BroadcasterId { get; set; } - public HashSet Emotes { get; set; } + public ICollection Emotes { get; set; } public long ChatterId { get; set; } } } \ No newline at end of file diff --git a/Socket/Data/ErrorMessage.cs b/Socket/Data/ErrorMessage.cs deleted file mode 100644 index 7d6711b..0000000 --- a/Socket/Data/ErrorMessage.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace HermesSocketLibrary.Socket.Data -{ - public class ErrorMessage - { - public Exception? Exception { get; set; } - public string Message { get; set; } - - public ErrorMessage(Exception exception, string message) - { - Exception = exception; - Message = message; - } - - public ErrorMessage(string message) - { - Message = message; - Exception = null; - } - } -} \ No newline at end of file diff --git a/Socket/Data/LoggingMessage.cs b/Socket/Data/LoggingMessage.cs new file mode 100644 index 0000000..ec24fed --- /dev/null +++ b/Socket/Data/LoggingMessage.cs @@ -0,0 +1,41 @@ +namespace HermesSocketLibrary.Socket.Data +{ + public enum HermesLoggingLevel + { + Trace, + Debug, + Info, + Warn, + Error, + Critical, + None + } + + public class LoggingMessage + { + public Exception? Exception { get; set; } + public string Message { get; set; } + public HermesLoggingLevel Level { get; set; } + + public LoggingMessage(Exception exception, string message, HermesLoggingLevel level) + { + Exception = exception; + Message = message; + Level = level; + } + + public LoggingMessage(string message, HermesLoggingLevel level) + { + Message = message; + Level = level; + Exception = null; + } + + public LoggingMessage() + { + Message = string.Empty; + Level = HermesLoggingLevel.Info; + Exception = null; + } + } +} \ No newline at end of file diff --git a/Socket/Data/RequestMessage.cs b/Socket/Data/RequestMessage.cs index 80492df..f2a7e68 100644 --- a/Socket/Data/RequestMessage.cs +++ b/Socket/Data/RequestMessage.cs @@ -2,6 +2,7 @@ namespace HermesSocketLibrary.Socket.Data { public class RequestMessage { + public string RequestId { get; set; } public string? Type { get; set; } public IDictionary? Data { get; set; } public string? Nounce { get; set; } diff --git a/Socket/Data/SocketMessage.cs b/Socket/Data/SocketMessage.cs deleted file mode 100644 index a294a58..0000000 --- a/Socket/Data/SocketMessage.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.Json.Serialization; - -namespace HermesSocketLibrary.Socket.Data -{ - public class SocketMessage - { - [JsonPropertyName("op")] - public int? OpCode { get; set; } - [JsonPropertyName("d")] - public object? Data { get; set; } - } -} \ No newline at end of file