2024-03-15 08:27:35 -04:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-07-16 00:48:55 -04:00
|
|
|
using HermesSocketLibrary.Requests.Callbacks;
|
2024-03-15 08:27:35 -04:00
|
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
|
|
using HermesSocketLibrary.Socket.Data;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.Chat.Emotes;
|
|
|
|
using TwitchChatTTS.Twitch.Redemptions;
|
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-07-16 00:48:55 -04:00
|
|
|
//private readonly RedemptionManager _redemptionManager;
|
|
|
|
private readonly ICallbackManager<HermesRequestData> _callbackManager;
|
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-16 00:48:55 -04:00
|
|
|
|
|
|
|
public RequestAckHandler(
|
|
|
|
User user,
|
|
|
|
//RedemptionManager redemptionManager,
|
|
|
|
ICallbackManager<HermesRequestData> callbackManager,
|
|
|
|
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-07-16 00:48:55 -04:00
|
|
|
//_redemptionManager = redemptionManager;
|
|
|
|
_callbackManager = callbackManager;
|
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-07-16 00:48:55 -04:00
|
|
|
{
|
|
|
|
_logger.Warning("Received a Hermes request message without a proper request.");
|
2024-03-15 08:27:35 -04:00
|
|
|
return;
|
2024-07-16 00:48:55 -04:00
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
HermesRequestData? hermesRequestData = null;
|
|
|
|
if (!string.IsNullOrEmpty(message.Request.RequestId))
|
|
|
|
{
|
|
|
|
hermesRequestData = _callbackManager.Take(message.Request.RequestId);
|
|
|
|
if (hermesRequestData == null)
|
|
|
|
_logger.Warning($"Could not find callback for request [request id: {message.Request.RequestId}][type: {message.Request.Type}]");
|
|
|
|
else if (hermesRequestData.Data == null)
|
|
|
|
hermesRequestData.Data = new Dictionary<string, object>();
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug($"Received a Hermes request message [type: {message.Request.Type}][data: {string.Join(',', message.Request.Data?.Select(entry => entry.Key + '=' + entry.Value) ?? Array.Empty<string>())}]");
|
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-07-16 00:48:55 -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();
|
2024-07-16 00:48:55 -04:00
|
|
|
string voiceId = message.Request.Data["voice"].ToString();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
_user.VoicesSelected.Add(chatterId, voiceId);
|
|
|
|
_logger.Information($"Added new TTS voice [voice: {voiceId}] for user [user id: {userId}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
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();
|
2024-07-16 00:48:55 -04:00
|
|
|
string voiceId = message.Request.Data["voice"].ToString();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
_user.VoicesSelected[chatterId] = voiceId;
|
|
|
|
_logger.Information($"Updated TTS voice [voice: {voiceId}] for user [user id: {userId}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
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-16 00:48:55 -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-16 00:48:55 -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;
|
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
var emoteDb = _serviceProvider.GetRequiredService<IEmoteDatabase>();
|
2024-06-16 20:19:31 -04:00
|
|
|
var count = 0;
|
2024-07-16 00:48:55 -04:00
|
|
|
var duplicateNames = 0;
|
2024-06-16 20:19:31 -04:00
|
|
|
foreach (var emote in emotes)
|
|
|
|
{
|
|
|
|
if (emoteDb.Get(emote.Name) == null)
|
|
|
|
{
|
|
|
|
emoteDb.Add(emote.Name, emote.Id);
|
|
|
|
count++;
|
|
|
|
}
|
2024-07-16 00:48:55 -04:00
|
|
|
else
|
|
|
|
duplicateNames++;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
_logger.Information($"Fetched {count} emotes from various sources.");
|
2024-07-16 00:48:55 -04:00
|
|
|
if (duplicateNames > 0)
|
|
|
|
_logger.Warning($"Found {duplicateNames} emotes with duplicate names.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
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-16 00:48:55 -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
|
|
|
}
|
2024-07-16 00:48:55 -04:00
|
|
|
else if (message.Request.Type == "get_redemptions")
|
|
|
|
{
|
|
|
|
_logger.Verbose("Fetching all the redemptions.");
|
|
|
|
IEnumerable<Redemption>? redemptions = JsonSerializer.Deserialize<IEnumerable<Redemption>>(message.Data!.ToString()!, _options);
|
|
|
|
if (redemptions != null)
|
|
|
|
{
|
|
|
|
_logger.Information($"Redemptions [count: {redemptions.Count()}] loaded.");
|
|
|
|
if (hermesRequestData != null)
|
|
|
|
hermesRequestData.Data!.Add("redemptions", redemptions);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_logger.Information(message.Data.GetType().ToString());
|
|
|
|
}
|
|
|
|
else if (message.Request.Type == "get_redeemable_actions")
|
|
|
|
{
|
|
|
|
_logger.Verbose("Fetching all the redeemable actions.");
|
|
|
|
IEnumerable<RedeemableAction>? actions = JsonSerializer.Deserialize<IEnumerable<RedeemableAction>>(message.Data!.ToString()!, _options);
|
|
|
|
if (actions == null)
|
|
|
|
{
|
|
|
|
_logger.Warning("Failed to read the redeemable actions for redemptions.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hermesRequestData?.Data == null || !(hermesRequestData.Data["redemptions"] is IEnumerable<Redemption> redemptions))
|
|
|
|
{
|
|
|
|
_logger.Warning("Failed to read the redemptions while updating redemption actions.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Information($"Redeemable actions [count: {actions.Count()}] loaded.");
|
|
|
|
var redemptionManager = _serviceProvider.GetRequiredService<RedemptionManager>();
|
|
|
|
redemptionManager.Initialize(redemptions, actions.ToDictionary(a => a.Name, a => a));
|
|
|
|
}
|
|
|
|
else if (message.Request.Type == "get_default_tts_voice")
|
|
|
|
{
|
|
|
|
string? defaultVoice = message.Data?.ToString();
|
|
|
|
if (defaultVoice != null)
|
|
|
|
{
|
|
|
|
_user.DefaultTTSVoice = defaultVoice;
|
|
|
|
_logger.Information($"Default TTS voice was changed to '{defaultVoice}'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (message.Request.Type == "update_default_tts_voice")
|
|
|
|
{
|
|
|
|
if (message.Request.Data?.TryGetValue("voice", out object? voice) == true && voice is string v)
|
|
|
|
{
|
|
|
|
_user.DefaultTTSVoice = v;
|
|
|
|
_logger.Information($"Default TTS voice was changed to '{v}'.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_logger.Warning("Failed to update default TTS voice via request.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.Warning($"Found unknown request type when acknowledging [type: {message.Request.Type}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hermesRequestData != null)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Callback was found for request [request id: {message.Request.RequestId}][type: {message.Request.Type}]");
|
|
|
|
hermesRequestData.Callback?.Invoke(hermesRequestData.Data);
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-16 00:48:55 -04:00
|
|
|
|
|
|
|
public class HermesRequestData
|
|
|
|
{
|
|
|
|
public Action<IDictionary<string, object>?>? Callback { get; set; }
|
|
|
|
public IDictionary<string, object>? Data { get; set; }
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|