Added & modified several message types.

This commit is contained in:
Tom 2024-08-10 19:33:33 +00:00
parent d8522584c4
commit 9d838e66ad
21 changed files with 190 additions and 72 deletions

View File

@ -8,16 +8,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog" Version="4.0.0" /> <PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.1-dev-10391" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" /> <PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" /> <PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972" /> <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
<PackageReference Include="Serilog.Sinks.Trace" Version="4.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,43 @@
namespace HermesSocketLibrary.Requests.Callbacks
{
public class CallbackManager<A> : ICallbackManager<A> where A : class
{
private readonly IDictionary<string, A> _callbacks;
public CallbackManager()
{
_callbacks = new Dictionary<string, A>();
}
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");
}
}
}

View File

@ -0,0 +1,9 @@
namespace HermesSocketLibrary.Requests.Callbacks
{
public interface ICallbackManager<A> where A : class
{
string GenerateKeyForCallback(A callback);
A? Get(string key);
A? Take(string key);
}
}

View File

@ -4,6 +4,6 @@ namespace HermesSocketLibrary.Requests
{ {
string Name { get; } string Name { get; }
Task<RequestResult> Grant(string sender, IDictionary<string, object> data); Task<RequestResult> Grant(string sender, IDictionary<string, object>? data);
} }
} }

View File

@ -0,0 +1,9 @@
using HermesSocketLibrary.Socket.Data;
namespace HermesSocketLibrary.Requests
{
public interface IRequestManager
{
Task<RequestResult> Grant(string sender, RequestMessage? message);
}
}

View File

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HermesSocketLibrary.Requests.Messages namespace HermesSocketLibrary.Requests.Messages
{ {
public class EmoteInfo public class EmoteInfo

View File

@ -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; }
}
}

View File

@ -0,0 +1,8 @@
namespace HermesSocketLibrary.Requests.Messages
{
public class GroupChatter
{
public string GroupId { get; set; }
public long ChatterId { get; set;}
}
}

View File

@ -0,0 +1,9 @@
namespace HermesSocketLibrary.Requests.Messages
{
public class GroupInfo
{
public IEnumerable<Group> Groups { get; set; }
public IEnumerable<GroupChatter> GroupChatters { get; set; }
public IEnumerable<GroupPermission> GroupPermissions { get; set; }
}
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,9 @@
namespace HermesSocketLibrary.Requests.Messages
{
public class RedeemableAction
{
public string Name { get; set; }
public string Type { get; set; }
public IDictionary<string, string> Data { get; set; }
}
}

View File

@ -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; }
}
}

View File

@ -1,3 +1,6 @@
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace HermesSocketLibrary.Requests.Messages namespace HermesSocketLibrary.Requests.Messages
{ {
public class TTSWordFilter public class TTSWordFilter
@ -5,7 +8,8 @@ namespace HermesSocketLibrary.Requests.Messages
public string? Id { get; set; } public string? Id { get; set; }
public string? Search { get; set; } public string? Search { get; set; }
public string? Replace { get; set; } public string? Replace { get; set; }
[JsonIgnore]
public Regex? Regex { get; set; }
public bool IsRegex { get; set; } public bool IsRegex { get; set; }

View File

@ -1,40 +1,19 @@
using System.Collections.Concurrent;
using System.Reflection;
using HermesSocketLibrary.Socket.Data; using HermesSocketLibrary.Socket.Data;
using Microsoft.Extensions.DependencyInjection;
using Serilog; using Serilog;
namespace HermesSocketLibrary.Requests namespace HermesSocketLibrary.Requests
{ {
public abstract class RequestManager public class RequestManager : IRequestManager
{ {
private readonly IDictionary<string, IRequest> _requests; private readonly IDictionary<string, IRequest> _requests;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger _logger; private readonly ILogger _logger;
public RequestManager(IServiceProvider serviceProvider, ILogger logger) public RequestManager(IEnumerable<IRequest> requests, ILogger logger)
{ {
_serviceProvider = serviceProvider;
_logger = logger; _logger = logger;
_requests = new ConcurrentDictionary<string, IRequest>(); _requests = requests.ToDictionary(r => r.Name, r => r);
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<IRequest>(type.Name);
_requests.Add(request.Name, request);
}
} }
public async Task<RequestResult> Grant(string sender, RequestMessage? message) public async Task<RequestResult> Grant(string sender, RequestMessage? message)

19
Socket/Data/Connection.cs Normal file
View File

@ -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; }
}
}

View File

@ -2,6 +2,6 @@ namespace HermesSocketLibrary.Socket.Data
{ {
public class EmoteDetailsMessage public class EmoteDetailsMessage
{ {
public Dictionary<string, string> Emotes { get; set; } public IDictionary<string, string> Emotes { get; set; }
} }
} }

View File

@ -5,7 +5,7 @@ namespace HermesSocketLibrary.Socket.Data
public string MessageId { get; set; } public string MessageId { get; set; }
public DateTime DateTime { get; set; } public DateTime DateTime { get; set; }
public long BroadcasterId { get; set; } public long BroadcasterId { get; set; }
public HashSet<string> Emotes { get; set; } public ICollection<string> Emotes { get; set; }
public long ChatterId { get; set; } public long ChatterId { get; set; }
} }
} }

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -2,6 +2,7 @@ namespace HermesSocketLibrary.Socket.Data
{ {
public class RequestMessage public class RequestMessage
{ {
public string RequestId { get; set; }
public string? Type { get; set; } public string? Type { get; set; }
public IDictionary<string, object>? Data { get; set; } public IDictionary<string, object>? Data { get; set; }
public string? Nounce { get; set; } public string? Nounce { get; set; }

View File

@ -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; }
}
}