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)) if (long.TryParse(data["chatter"].ToString(), out long chatter))
data["chatter"] = chatter; data["chatter"] = chatter;
else
return new RequestResult(false, "Invalid Twitch user id");
if (data["voice"] is JsonElement v) if (data["voice"] is JsonElement v)
data["voice"] = v.ToString(); data["voice"] = v.ToString();
else
return new RequestResult(false, "Invalid voice id");
data["user"] = sender; data["user"] = sender;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if (check is not bool state || !state) 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)"; string sql = "INSERT INTO \"TtsChatVoice\" (\"userId\", \"chatterId\", \"ttsVoiceId\") VALUES (@user, @chatter, @voice)";
var result = await _database.Execute(sql, data); 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"]}]"); _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); return new RequestResult(result == 1, null);
} }

View File

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

View File

@ -1,6 +1,6 @@
using System.Text.Json; using System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests
public class DeleteTTSVoice : IRequest public class DeleteTTSVoice : IRequest
{ {
public string Name => "delete_tts_voice"; public string Name => "delete_tts_voice";
private Database _database; private IStore<string, string> _voices;
private ILogger _logger; private ILogger _logger;
public DeleteTTSVoice(Database database, ILogger logger) public DeleteTTSVoice(VoiceStore voices, ILogger logger)
{ {
_database = database; _voices = voices;
_logger = logger; _logger = logger;
} }
@ -28,10 +28,9 @@ namespace HermesSocketServer.Requests
if (data["voice"] is JsonElement v) if (data["voice"] is JsonElement v)
data["voice"] = v.ToString(); data["voice"] = v.ToString();
string sql = "DELETE FROM \"TtsVoice\" WHERE id = @voice"; _voices.Remove(data["voice"].ToString());
var result = await _database.Execute(sql, data);
_logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]"); _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 System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -8,12 +8,12 @@ namespace HermesSocketServer.Requests
public class UpdateTTSVoice : IRequest public class UpdateTTSVoice : IRequest
{ {
public string Name => "update_tts_voice"; public string Name => "update_tts_voice";
private Database _database; private IStore<string, string> _voices;
private ILogger _logger; private ILogger _logger;
public UpdateTTSVoice(Database database, ILogger logger) public UpdateTTSVoice(VoiceStore voices, ILogger logger)
{ {
_database = database; _voices = voices;
_logger = logger; _logger = logger;
} }
@ -30,10 +30,9 @@ namespace HermesSocketServer.Requests
if (data["voiceid"] is JsonElement id) if (data["voiceid"] is JsonElement id)
data["voiceid"] = id.ToString(); data["voiceid"] = id.ToString();
string sql = "UPDATE \"TtsVoice\" SET name = @voice WHERE id = @voiceid"; _voices.Set(data["voiceid"].ToString(), data["voice"].ToString());
var result = await _database.Execute(sql, data);
_logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); _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 connection = await _source.OpenConnectionAsync())
{ {
using (var command = new NpgsqlCommand(sql, connection)) using (var command = new NpgsqlCommand(sql, connection))
{ {
action(command); prepare(command);
await command.PrepareAsync(); await command.PrepareAsync();
return await command.ExecuteNonQueryAsync(); return await command.ExecuteNonQueryAsync();