Added TTS Filter websocket requests.

This commit is contained in:
Tom 2025-01-01 17:26:06 +00:00
parent 4f5dd8f24e
commit d74b132c0f
8 changed files with 132 additions and 12 deletions

View File

@ -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<string, string>(message.TTSVoicesAvailable);
_user.VoicesEnabled = new HashSet<string>(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

View File

@ -24,12 +24,6 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
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)
{

View File

@ -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<string, object>? requestData)
{
var filter = JsonSerializer.Deserialize<TTSWordFilter>(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}]");
}
}
}

View File

@ -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<string, object>? 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}]");
}
}
}

View File

@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var voices = JsonSerializer.Deserialize<IEnumerable<VoiceDetails>>(json, _options);
var voices = JsonSerializer.Deserialize<IEnumerable<TTSVoice>>(json, _options);
if (voices == null)
{
_logger.Warning("Voices received is null.");

View File

@ -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.");
}
}
}

View File

@ -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<string, object>? requestData)
{
var filter = JsonSerializer.Deserialize<TTSWordFilter>(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}]");
}
}
}

View File

@ -29,7 +29,7 @@ namespace TwitchChatTTS
public IDictionary<string, RaidInfo> Raids { get; set; } = new Dictionary<string, RaidInfo>();
public HashSet<long> Chatters { get; set; }
public TTSWordFilter[] RegexFilters { get; set; }
public IList<TTSWordFilter> RegexFilters { get; set; }
[JsonIgnore]
public Regex? VoiceNameRegex { get; set; }