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 _policies; private readonly JsonSerializerOptions _options; private readonly ILogger _logger; public UpdatePolicyAck(IChatterGroupManager groups, IUsagePolicy policies, JsonSerializerOptions options, ILogger logger) { _groups = groups; _policies = policies; _options = options; _logger = logger; } public void Acknowledge(string requestId, string json, IDictionary? requestData) { var policy = JsonSerializer.Deserialize(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}]"); } } }