2024-03-15 08:27:35 -04:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
|
|
using HermesSocketLibrary.Socket.Data;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
|
|
|
using TwitchChatTTS.Seven;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.Hermes.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class RequestAckHandler : IWebSocketHandler
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
private User _user;
|
2024-03-15 08:27:35 -04:00
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
private readonly JsonSerializerOptions _options;
|
|
|
|
private readonly ILogger _logger;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
private readonly object _voicesAvailableLock = new object();
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public int OperationCode { get; } = 4;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
public RequestAckHandler(User user, IServiceProvider serviceProvider, JsonSerializerOptions options, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_user = user;
|
2024-03-15 08:27:35 -04:00
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
_options = options;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-15 08:27:35 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not RequestAckMessage message || message == null)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-06-24 18:11:36 -04:00
|
|
|
if (message.Request == null)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-07-06 23:42:33 -04:00
|
|
|
if (_user == null)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
if (message.Request.Type == "get_tts_voices")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating all available voices for TTS.");
|
2024-06-24 18:11:36 -04:00
|
|
|
var voices = JsonSerializer.Deserialize<IEnumerable<VoiceDetails>>(message.Data.ToString(), _options);
|
2024-03-15 08:27:35 -04:00
|
|
|
if (voices == null)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
lock (_voicesAvailableLock)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesAvailable = voices.ToDictionary(e => e.Id, e => e.Name);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
_logger.Information("Updated all available voices for TTS.");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "create_tts_user")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Adding new tts voice for user.");
|
2024-06-24 18:11:36 -04:00
|
|
|
if (!long.TryParse(message.Request.Data["user"].ToString(), out long chatterId))
|
2024-07-06 23:42:33 -04:00
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to parse chatter id [chatter id: {message.Request.Data["chatter"]}]");
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-07-06 23:42:33 -04:00
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
string userId = message.Request.Data["user"].ToString();
|
|
|
|
string voice = message.Request.Data["voice"].ToString();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesSelected.Add(chatterId, voice);
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Added new TTS voice [voice: {voice}] for user [user id: {userId}]");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "update_tts_user")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating user's voice");
|
2024-06-24 18:11:36 -04:00
|
|
|
if (!long.TryParse(message.Request.Data["chatter"].ToString(), out long chatterId))
|
2024-07-06 23:42:33 -04:00
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to parse chatter id [chatter id: {message.Request.Data["chatter"]}]");
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-07-06 23:42:33 -04:00
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
string userId = message.Request.Data["user"].ToString();
|
|
|
|
string voice = message.Request.Data["voice"].ToString();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesSelected[chatterId] = voice;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Updated TTS voice [voice: {voice}] for user [user id: {userId}]");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "create_tts_voice")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Creating new tts voice.");
|
2024-06-24 18:11:36 -04:00
|
|
|
string? voice = message.Request.Data["voice"].ToString();
|
|
|
|
string? voiceId = message.Data.ToString();
|
2024-03-15 08:27:35 -04:00
|
|
|
if (voice == null || voiceId == null)
|
|
|
|
return;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
lock (_voicesAvailableLock)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
var list = _user.VoicesAvailable.ToDictionary(k => k.Key, v => v.Value);
|
2024-06-16 20:19:31 -04:00
|
|
|
list.Add(voiceId, voice);
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesAvailable = list;
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Created new tts voice [voice: {voice}][id: {voiceId}].");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "delete_tts_voice")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Deleting tts voice.");
|
2024-06-24 18:11:36 -04:00
|
|
|
var voice = message.Request.Data["voice"].ToString();
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!_user.VoicesAvailable.TryGetValue(voice, out string voiceName) || voiceName == null)
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
lock (_voicesAvailableLock)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
var dict = _user.VoicesAvailable.ToDictionary(k => k.Key, v => v.Value);
|
2024-06-16 20:19:31 -04:00
|
|
|
dict.Remove(voice);
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesAvailable.Remove(voice);
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Deleted a voice [voice: {voiceName}]");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "update_tts_voice")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating TTS voice.");
|
2024-06-24 18:11:36 -04:00
|
|
|
string voiceId = message.Request.Data["idd"].ToString();
|
|
|
|
string voice = message.Request.Data["voice"].ToString();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!_user.VoicesAvailable.TryGetValue(voiceId, out string voiceName) || voiceName == null)
|
2024-06-16 20:19:31 -04:00
|
|
|
return;
|
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesAvailable[voiceId] = voice;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Updated TTS voice [voice: {voice}][id: {voiceId}]");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "get_tts_users")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating all chatters' selected voice.");
|
2024-06-24 18:11:36 -04:00
|
|
|
var users = JsonSerializer.Deserialize<IDictionary<long, string>>(message.Data.ToString(), _options);
|
2024-03-15 08:27:35 -04:00
|
|
|
if (users == null)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var temp = new ConcurrentDictionary<long, string>();
|
|
|
|
foreach (var entry in users)
|
|
|
|
temp.TryAdd(entry.Key, entry.Value);
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesSelected = temp;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Updated {temp.Count()} chatters' selected voice.");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "get_chatter_ids")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Fetching all chatters' id.");
|
2024-06-24 18:11:36 -04:00
|
|
|
var chatters = JsonSerializer.Deserialize<IEnumerable<long>>(message.Data.ToString(), _options);
|
2024-06-16 20:19:31 -04:00
|
|
|
if (chatters == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var client = _serviceProvider.GetRequiredService<ChatMessageHandler>();
|
|
|
|
client.Chatters = [.. chatters];
|
|
|
|
_logger.Information($"Fetched {chatters.Count()} chatters' id.");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "get_emotes")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating emotes.");
|
2024-06-24 18:11:36 -04:00
|
|
|
var emotes = JsonSerializer.Deserialize<IEnumerable<EmoteInfo>>(message.Data.ToString(), _options);
|
2024-06-16 20:19:31 -04:00
|
|
|
if (emotes == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var emoteDb = _serviceProvider.GetRequiredService<EmoteDatabase>();
|
|
|
|
var count = 0;
|
|
|
|
foreach (var emote in emotes)
|
|
|
|
{
|
|
|
|
if (emoteDb.Get(emote.Name) == null)
|
|
|
|
{
|
|
|
|
emoteDb.Add(emote.Name, emote.Id);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_logger.Information($"Fetched {count} emotes from various sources.");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
else if (message.Request.Type == "update_tts_voice_state")
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger.Verbose("Updating TTS voice states.");
|
2024-06-24 18:11:36 -04:00
|
|
|
string voiceId = message.Request.Data["voice"].ToString();
|
2024-07-06 23:42:33 -04:00
|
|
|
bool state = message.Request.Data["state"].ToString().ToLower() == "true";
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!_user.VoicesAvailable.TryGetValue(voiceId, out string voiceName) || voiceName == null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Warning($"Failed to find voice by id [id: {voiceId}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state)
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesEnabled.Add(voiceId);
|
2024-06-16 20:19:31 -04:00
|
|
|
else
|
2024-07-06 23:42:33 -04:00
|
|
|
_user.VoicesEnabled.Remove(voiceId);
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Updated voice state [voice: {voiceName}][new state: {(state ? "enabled" : "disabled")}]");
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|