Updated requests to use voice store when possible.

This commit is contained in:
Tom 2024-10-17 19:07:00 +00:00
parent 88104484f6
commit 0a2392b957
5 changed files with 35 additions and 29 deletions

View File

@ -27,16 +27,25 @@ namespace HermesSocketServer.Requests
if (long.TryParse(data["chatter"].ToString(), out long chatter))
data["chatter"] = chatter;
else
return new RequestResult(false, "Invalid Twitch user id");
if (data["voice"] is JsonElement v)
data["voice"] = v.ToString();
else
return new RequestResult(false, "Invalid voice id");
data["user"] = sender;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if (check is not bool state || !state)
return new RequestResult(false, null);
return new RequestResult(false, "Voice is disabled on this channel.");
string sql = "INSERT INTO \"TtsChatVoice\" (\"userId\", \"chatterId\", \"ttsVoiceId\") VALUES (@user, @chatter, @voice)";
var result = await _database.Execute(sql, data);
if (result == 0)
return new RequestResult(false, "Could not insert the user's voice properly.");
_logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]");
return new RequestResult(result == 1, null);
}

View File

@ -1,6 +1,6 @@
using System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -8,13 +8,13 @@ namespace HermesSocketServer.Requests
public class CreateTTSVoice : IRequest
{
public string Name => "create_tts_voice";
private Database _database;
private IStore<string, string> _voices;
private ILogger _logger;
private Random _random;
public CreateTTSVoice(Database database, ILogger logger)
public CreateTTSVoice(VoiceStore voices, ILogger logger)
{
_database = database;
_voices = voices;
_logger = logger;
_random = new Random();
}
@ -28,18 +28,17 @@ namespace HermesSocketServer.Requests
return new RequestResult(false, null);
}
string id = RandomString(25);
data.Add("idd", id);
if (data["voice"] is JsonElement v)
data["voice"] = v.ToString();
else
return new RequestResult(false, "Invalid voice name.");
string sql = "INSERT INTO \"TtsVoice\" (id, name) VALUES (@idd, @voice)";
var result = await _database.Execute(sql, data);
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {data["idd"]}]");
string id = RandomString(25);
data.Remove("idd");
return new RequestResult(result == 1, id);
_voices.Set(id, data["voice"].ToString());
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]");
return new RequestResult(true, id);
}
private string RandomString(int length)

View File

@ -1,6 +1,6 @@
using System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests
public class DeleteTTSVoice : IRequest
{
public string Name => "delete_tts_voice";
private Database _database;
private IStore<string, string> _voices;
private ILogger _logger;
public DeleteTTSVoice(Database database, ILogger logger)
public DeleteTTSVoice(VoiceStore voices, ILogger logger)
{
_database = database;
_voices = voices;
_logger = logger;
}
@ -28,10 +28,9 @@ namespace HermesSocketServer.Requests
if (data["voice"] is JsonElement v)
data["voice"] = v.ToString();
string sql = "DELETE FROM \"TtsVoice\" WHERE id = @voice";
var result = await _database.Execute(sql, data);
_voices.Remove(data["voice"].ToString());
_logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]");
return new RequestResult(result == 1, null);
return new RequestResult(true, null);
}
}
}

View File

@ -1,6 +1,6 @@
using System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests
public class UpdateTTSVoice : IRequest
{
public string Name => "update_tts_voice";
private Database _database;
private IStore<string, string> _voices;
private ILogger _logger;
public UpdateTTSVoice(Database database, ILogger logger)
public UpdateTTSVoice(VoiceStore voices, ILogger logger)
{
_database = database;
_voices = voices;
_logger = logger;
}
@ -30,10 +30,9 @@ namespace HermesSocketServer.Requests
if (data["voiceid"] is JsonElement id)
data["voiceid"] = id.ToString();
string sql = "UPDATE \"TtsVoice\" SET name = @voice WHERE id = @voiceid";
var result = await _database.Execute(sql, data);
_voices.Set(data["voiceid"].ToString(), data["voice"].ToString());
_logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]");
return new RequestResult(result == 1, null);
return new RequestResult(true, null);
}
}
}

View File

@ -79,13 +79,13 @@ namespace HermesSocketLibrary.db
}
}
public async Task<int> Execute(string sql, Action<NpgsqlCommand> action)
public async Task<int> Execute(string sql, Action<NpgsqlCommand> prepare)
{
using (var connection = await _source.OpenConnectionAsync())
{
using (var command = new NpgsqlCommand(sql, connection))
{
action(command);
prepare(command);
await command.PrepareAsync();
return await command.ExecuteNonQueryAsync();