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 _policies; private readonly JsonSerializerOptions _options; private readonly ILogger _logger; public GetPoliciesAck( 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 policies = JsonSerializer.Deserialize>(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()}]"); } } }