hermes-client/Hermes/Socket/Requests/GetPoliciesAck.cs

53 lines
2.0 KiB
C#
Raw Permalink Normal View History

using System.Text.Json;
using HermesSocketServer.Models;
using Serilog;
using TwitchChatTTS.Chat.Commands.Limits;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class GetPoliciesAck : IRequestAck
{
public string Name => "get_policies";
private readonly IChatterGroupManager _groups;
private readonly IUsagePolicy<long> _policies;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public GetPoliciesAck(
IChatterGroupManager groups,
IUsagePolicy<long> policies,
JsonSerializerOptions options,
ILogger logger)
{
_groups = groups;
_policies = policies;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var policies = JsonSerializer.Deserialize<IEnumerable<PolicyMessage>>(json, _options);
if (policies == null || !policies.Any())
{
_logger.Information($"No policies have been found. Policies have been set to default.");
_policies.Set("everyone", "tts", 25, TimeSpan.FromSeconds(15));
return;
}
foreach (var policy in policies)
{
var group = _groups.Get(policy.GroupId.ToString());
if (group == null)
{
_logger.Debug($"Policy data failed: group id not found [group id: {policy.GroupId}][policy id: {policy.Id}]");
continue;
}
_logger.Debug($"Policy data loaded [policy id: {policy.Id}][path: {policy.Path}][group id: {policy.GroupId}][group name: {group.Name}]");
_policies.Set(group.Name, policy.Path, policy.Usage, TimeSpan.FromMilliseconds(policy.Span));
}
_logger.Information($"Policies have been loaded [count: {policies.Count()}]");
}
}
}