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

52 lines
1.9 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 CreatePolicyAck : IRequestAck
{
public string Name => "create_policy";
private readonly IChatterGroupManager _groups;
private readonly IUsagePolicy<long> _policies;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public CreatePolicyAck(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)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
var policy = JsonSerializer.Deserialize<PolicyMessage>(json, _options);
if (policy == null)
{
_logger.Warning($"Policy data failed: null");
return;
}
var group = _groups.Get(policy.GroupId.ToString());
if (group == null)
{
_logger.Warning($"Policy data failed: group id not found [group id: {policy.GroupId}][policy id: {policy.Id}]");
return;
}
_logger.Debug($"Policy data [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($"Policy has been updated [policy id: {policy.Id}]");
}
}
}