Fixed issues with TTS voice changes. Added proper handling for slave clients. Fixed several stores. Fixed database saving to safely save foreign keys.

This commit is contained in:
Tom
2025-03-06 16:11:36 +00:00
parent 3e717522c2
commit fd0bca5c7c
13 changed files with 139 additions and 96 deletions

View File

@ -1,4 +1,3 @@
using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
@ -8,28 +7,26 @@ namespace HermesSocketServer.Requests
{
public string Name => "create_tts_user";
public string[] RequiredKeys => ["chatter", "voice"];
private Database _database;
private readonly ServerConfiguration _configuration;
private ILogger _logger;
public CreateTTSUser(Database database, ServerConfiguration configuration, ILogger logger)
public CreateTTSUser(ServerConfiguration configuration, ILogger logger)
{
_database = database;
_configuration = configuration;
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
if (!long.TryParse(data["chatter"].ToString(), out long chatterId))
return RequestResult.Failed("Invalid Twitch user id");
return Task.FromResult(RequestResult.Failed("Invalid Twitch user id."));
data["user"] = channel.Id;
data["voice"] = data["voice"].ToString()!;
var voiceId = data["voice"].ToString()!;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
return RequestResult.Failed("Voice is disabled on this channel.");
var check = channel.VoiceStates.Get(voiceId)?.Enabled ?? false;
if (!check && chatterId != _configuration.Tts.OwnerId)
return Task.FromResult(RequestResult.Failed("Voice is disabled on this channel."));
bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
{
@ -40,10 +37,10 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]");
return RequestResult.Successful(null);
_logger.Information($"Selected a tts voice [voice: {voiceId}] for user [chatter: {chatterId}] in channel [channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(null));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -1,6 +1,4 @@
using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using HermesSocketServer.Services;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -9,28 +7,27 @@ namespace HermesSocketServer.Requests
{
public string Name => "update_tts_user";
public string[] RequiredKeys => ["chatter", "voice"];
private Database _database;
private readonly ServerConfiguration _configuration;
private ILogger _logger;
public UpdateTTSUser(Database database, ServerConfiguration configuration, ILogger logger)
public UpdateTTSUser(ServerConfiguration configuration, ILogger logger)
{
_database = database;
_configuration = configuration;
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
if (!long.TryParse(data["chatter"].ToString(), out long chatterId))
return RequestResult.Failed("Chatter should be an integer, representing the Twitch User Id of the chatter.");
return Task.FromResult(RequestResult.Failed("Invalid Twitch user id."));
data["user"] = channel.Id;
var voiceId = data["voice"].ToString()!;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
return RequestResult.Failed("Voice is either non-existent or disabled on this channel.");
var check = channel.VoiceStates.Get(voiceId)?.Enabled ?? false;
if (!check && chatterId != _configuration.Tts.OwnerId)
return Task.FromResult(RequestResult.Failed("Voice is either non-existent or disabled on this channel."));
var voice = new ChatterVoice()
{
@ -43,9 +40,9 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Updated chatter's selected tts voice on channel [chatter id: {chatterId}][voice id: {voiceId}][channel: {channel.Id}]");
return RequestResult.Successful(voice);
return Task.FromResult(RequestResult.Successful(voice));
}
return RequestResult.Failed("Soemthing went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Soemthing went wrong when updating the cache."));
}
}
}

View File

@ -20,20 +20,20 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = data["voice"].ToString()!;
var state = data["state"].ToString() == "True";
var voiceId = data["voice"].ToString()!;
var state = data["state"].ToString()?.ToLower() == "true";
var voiceState = new TTSVoiceState()
{
Id = id,
Id = voiceId,
UserId = channel.Id,
Enabled = state,
};
var result = channel.VoiceStates.Set(id, voiceState);
var result = channel.VoiceStates.Set(voiceId, voiceState);
if (result)
{
_logger.Information($"Updated voice state on channel [voice id: {id}][state: {state}][channel: {channel.Id}]");
_logger.Information($"Updated voice state on channel [voice id: {voiceId}][state: {state}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(voiceState));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the database."));