hermes-server/Requests/CreateTTSVoice.cs

48 lines
1.5 KiB
C#

using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class CreateTTSVoice : IRequest
{
public string Name => "create_tts_voice";
public string[] RequiredKeys => ["voice"];
private IStore<string, TTSVoice> _voices;
private ILogger _logger;
private Random _random;
public CreateTTSVoice(VoiceStore voices, ILogger logger)
{
_voices = voices;
_logger = logger;
_random = new Random();
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string voice = data["voice"].ToString()!;
string id = RandomString(25);
var result = _voices.Set(id, new TTSVoice()
{
Id = id,
Name = voice
});
if (result) {
_logger.Information($"Added a new voice [voice: {voice}][voice id: {id}]");
return Task.FromResult(RequestResult.Successful(id));
}
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
private string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[_random.Next(s.Length)]).ToArray());
}
}
}