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

45 lines
1.7 KiB
C#
Raw 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 UpdatePolicyAck : IRequestAck
{
public string Name => "update_policy";
private readonly IChatterGroupManager _groups;
private readonly IUsagePolicy<long> _policies;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public UpdatePolicyAck(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 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 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($"Policy has been updated [policy id: {policy.Id}]");
}
}
}