From d74b132c0f8e2e7c6497838d76af55ae6c0004c0 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 1 Jan 2025 17:26:06 +0000 Subject: [PATCH] Added TTS Filter websocket requests. --- Hermes/Socket/Handlers/LoginAckHandler.cs | 5 ++- Hermes/Socket/Requests/CreatePolicyAck.cs | 6 --- Hermes/Socket/Requests/CreateTTSFilterAck.cs | 41 ++++++++++++++++++ Hermes/Socket/Requests/DeleteTTSFilterAck.cs | 42 +++++++++++++++++++ Hermes/Socket/Requests/GetTTSVoicesAck.cs | 2 +- .../Socket/Requests/GetTTSWordFiltersAck.cs | 4 +- Hermes/Socket/Requests/UpdateTTSFilterAck.cs | 42 +++++++++++++++++++ User.cs | 2 +- 8 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 Hermes/Socket/Requests/CreateTTSFilterAck.cs create mode 100644 Hermes/Socket/Requests/DeleteTTSFilterAck.cs create mode 100644 Hermes/Socket/Requests/UpdateTTSFilterAck.cs diff --git a/Hermes/Socket/Handlers/LoginAckHandler.cs b/Hermes/Socket/Handlers/LoginAckHandler.cs index 3fad6a8..1add60c 100644 --- a/Hermes/Socket/Handlers/LoginAckHandler.cs +++ b/Hermes/Socket/Handlers/LoginAckHandler.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Text.RegularExpressions; using CommonSocketLibrary.Abstract; using CommonSocketLibrary.Common; @@ -42,12 +43,12 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers _user.HermesUserId = message.UserId; _user.OwnerId = message.OwnerId; _user.DefaultTTSVoice = message.DefaultTTSVoice; - _user.VoicesAvailable = message.TTSVoicesAvailable; + _user.VoicesAvailable = new ConcurrentDictionary(message.TTSVoicesAvailable); _user.VoicesEnabled = new HashSet(message.EnabledTTSVoices); _user.TwitchConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "twitch"); _user.NightbotConnection = message.Connections.FirstOrDefault(c => c.Default && c.Type == "nightbot"); - var filters = message.WordFilters.Where(f => f.Search != null && f.Replace != null).ToArray(); + var filters = message.WordFilters.Where(f => f.Search != null && f.Replace != null).ToList(); foreach (var filter in filters) { try diff --git a/Hermes/Socket/Requests/CreatePolicyAck.cs b/Hermes/Socket/Requests/CreatePolicyAck.cs index 837dbf5..64e48a0 100644 --- a/Hermes/Socket/Requests/CreatePolicyAck.cs +++ b/Hermes/Socket/Requests/CreatePolicyAck.cs @@ -24,12 +24,6 @@ namespace TwitchChatTTS.Hermes.Socket.Requests public void Acknowledge(string requestId, string json, IDictionary? requestData) { - if (requestData == null) - { - _logger.Warning("Request data is null."); - return; - } - var policy = JsonSerializer.Deserialize(json, _options); if (policy == null) { diff --git a/Hermes/Socket/Requests/CreateTTSFilterAck.cs b/Hermes/Socket/Requests/CreateTTSFilterAck.cs new file mode 100644 index 0000000..a86ec79 --- /dev/null +++ b/Hermes/Socket/Requests/CreateTTSFilterAck.cs @@ -0,0 +1,41 @@ +using System.Text.Json; +using HermesSocketLibrary.Requests.Messages; +using Serilog; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class CreateTTSFilterAck : IRequestAck + { + public string Name => "create_tts_filter"; + private readonly User _user; + private readonly JsonSerializerOptions _options; + private readonly ILogger _logger; + + public CreateTTSFilterAck(User user, JsonSerializerOptions options, ILogger logger) + { + _user = user; + _options = options; + _logger = logger; + } + + public void Acknowledge(string requestId, string json, IDictionary? requestData) + { + var filter = JsonSerializer.Deserialize(json, _options); + if (filter == null) + { + _logger.Warning($"TTS Filter data received is null."); + return; + } + + if (_user.RegexFilters.Any(f => f.Id == filter.Id)) + { + _logger.Warning($"Filter already exists [filter id: {filter.Id}]"); + return; + } + + _logger.Debug($"Filter data [filter id: {filter.Id}][search: {filter.Search}][replace: {filter.Replace}]"); + _user.RegexFilters.Add(filter); + _logger.Information($"Filter has been updated [filter id: {filter.Id}]"); + } + } +} \ No newline at end of file diff --git a/Hermes/Socket/Requests/DeleteTTSFilterAck.cs b/Hermes/Socket/Requests/DeleteTTSFilterAck.cs new file mode 100644 index 0000000..d72488f --- /dev/null +++ b/Hermes/Socket/Requests/DeleteTTSFilterAck.cs @@ -0,0 +1,42 @@ +using Serilog; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class DeleteTTSFilterAck : IRequestAck + { + public string Name => "delete_tts_filter"; + private readonly User _user; + private readonly ILogger _logger; + + public DeleteTTSFilterAck(User user, ILogger logger) + { + _user = user; + _logger = logger; + } + + public void Acknowledge(string requestId, string json, IDictionary? requestData) + { + if (requestData == null) + { + _logger.Warning("Request data is null."); + return; + } + + var id = requestData["id"].ToString(); + if (string.IsNullOrEmpty(id)) + { + _logger.Warning($"Filter Id is invalid [filter id: {id}]"); + return; + } + var filter = _user.RegexFilters.FirstOrDefault(f => f.Id == id); + if (filter == null) + { + _logger.Warning($"Filter Id does not exist [filter id: {id}]"); + return; + } + + _user.RegexFilters.Remove(filter); + _logger.Information($"Deleted a filter [filter id: {id}][search: {filter.Search}][replace: {filter.Replace}]"); + } + } +} \ No newline at end of file diff --git a/Hermes/Socket/Requests/GetTTSVoicesAck.cs b/Hermes/Socket/Requests/GetTTSVoicesAck.cs index 8741c2e..5f1e360 100644 --- a/Hermes/Socket/Requests/GetTTSVoicesAck.cs +++ b/Hermes/Socket/Requests/GetTTSVoicesAck.cs @@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests public void Acknowledge(string requestId, string json, IDictionary? requestData) { - var voices = JsonSerializer.Deserialize>(json, _options); + var voices = JsonSerializer.Deserialize>(json, _options); if (voices == null) { _logger.Warning("Voices received is null."); diff --git a/Hermes/Socket/Requests/GetTTSWordFiltersAck.cs b/Hermes/Socket/Requests/GetTTSWordFiltersAck.cs index 50b76b3..b779fdb 100644 --- a/Hermes/Socket/Requests/GetTTSWordFiltersAck.cs +++ b/Hermes/Socket/Requests/GetTTSWordFiltersAck.cs @@ -28,7 +28,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests return; } - var filters = wordFilters.Where(f => f.Search != null && f.Replace != null).ToArray(); + var filters = wordFilters.Where(f => f.Search != null && f.Replace != null).ToList(); foreach (var filter in filters) { try @@ -40,7 +40,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests catch (Exception) { } } _user.RegexFilters = filters; - _logger.Information($"TTS word filters [count: {_user.RegexFilters.Count()}] have been refreshed."); + _logger.Information($"TTS word filters [count: {_user.RegexFilters.Count}] have been refreshed."); } } } \ No newline at end of file diff --git a/Hermes/Socket/Requests/UpdateTTSFilterAck.cs b/Hermes/Socket/Requests/UpdateTTSFilterAck.cs new file mode 100644 index 0000000..9d5fe12 --- /dev/null +++ b/Hermes/Socket/Requests/UpdateTTSFilterAck.cs @@ -0,0 +1,42 @@ +using System.Text.Json; +using HermesSocketLibrary.Requests.Messages; +using Serilog; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class UpdateTTSFilterAck : IRequestAck + { + public string Name => "update_tts_filter"; + private readonly User _user; + private readonly JsonSerializerOptions _options; + private readonly ILogger _logger; + + public UpdateTTSFilterAck(User user, JsonSerializerOptions options, ILogger logger) + { + _user = user; + _options = options; + _logger = logger; + } + + public void Acknowledge(string requestId, string json, IDictionary? requestData) + { + var filter = JsonSerializer.Deserialize(json, _options); + if (filter == null) + { + _logger.Warning($"TTS Filter data failed: null"); + return; + } + + _logger.Debug($"Filter data [filter id: {filter.Id}][search: {filter.Search}][group id: {filter.Replace}]"); + var previous = _user.RegexFilters.FirstOrDefault(f => f.Id == filter.Id); + if (previous == null) { + _logger.Warning($"TTS Filter doest exist by id [filter id: {filter.Id}]"); + return; + } + + previous.Search = filter.Search; + previous.Replace = filter.Replace; + _logger.Information($"Filter has been updated [filter id: {filter.Id}]"); + } + } +} \ No newline at end of file diff --git a/User.cs b/User.cs index 2d4fe87..d69fa30 100644 --- a/User.cs +++ b/User.cs @@ -29,7 +29,7 @@ namespace TwitchChatTTS public IDictionary Raids { get; set; } = new Dictionary(); public HashSet Chatters { get; set; } - public TTSWordFilter[] RegexFilters { get; set; } + public IList RegexFilters { get; set; } [JsonIgnore] public Regex? VoiceNameRegex { get; set; }