Updated Default TTS requests to use stores. Added a modify method for grouped save stores.

This commit is contained in:
Tom 2024-10-20 18:07:18 +00:00
parent 4d0b38babd
commit 0fa0ddcc45
7 changed files with 58 additions and 29 deletions

View File

@ -1,5 +1,5 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -7,23 +7,24 @@ namespace HermesSocketServer.Requests
public class GetDefaultTTSVoice : IRequest
{
public string Name => "get_default_tts_voice";
private Database _database;
private ILogger _logger;
private readonly UserStore _users;
private readonly ServerConfiguration _configuration;
private readonly ILogger _logger;
public GetDefaultTTSVoice(Database database, ILogger logger)
public GetDefaultTTSVoice(UserStore users, ServerConfiguration configuration, ILogger logger)
{
_database = database;
_users = users;
_configuration = configuration;
_logger = logger;
}
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
{
var temp = new Dictionary<string, object>() { { "user", sender } };
string sql = $"SELECT \"ttsDefaultVoice\" FROM \"User\" WHERE id = @user";
string? value = (string?)await _database.ExecuteScalar(sql, temp);
_logger.Information($"Fetched the default TTS voice for channel [channel: {sender}]");
return new RequestResult(true, value, notifyClientsOnAccount: false);
var user = _users.Get(sender);
if (user == null)
return new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
return new RequestResult(true, user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
}
}
}

View File

@ -1,5 +1,5 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
@ -7,12 +7,12 @@ namespace HermesSocketServer.Requests
public class UpdateDefaultTTSVoice : IRequest
{
public string Name => "update_default_tts_voice";
private Database _database;
private UserStore _users;
private ILogger _logger;
public UpdateDefaultTTSVoice(Database database, ILogger logger)
public UpdateDefaultTTSVoice(UserStore users, ILogger logger)
{
_database = database;
_users = users;
_logger = logger;
}
@ -27,8 +27,10 @@ namespace HermesSocketServer.Requests
data["user"] = data["user"].ToString();
data["voice"] = data["voice"].ToString();
string sql = $"UPDATE \"User\" SET ttsDefaultVoice = @voice WHERE id = @user";
await _database.Execute(sql, data);
var success = _users.Modify(data["user"].ToString(), (user) => user.DefaultVoice = data["voice"].ToString()!);
if (!success)
return new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
_logger.Information($"Updated default TTS voice for channel [channel: {sender}][voice: {data["voice"]}]");
return new RequestResult(true, null);
}

View File

@ -43,15 +43,15 @@ namespace HermesSocketServer.Store
_logger.Information($"Loaded {_store.Count} TTS chatter voices from database.");
}
public override void OnInitialAdd(string key, ChatterVoice value)
protected override void OnInitialAdd(string key, ChatterVoice value)
{
}
public override void OnInitialModify(string key, ChatterVoice value)
protected override void OnInitialModify(string key, ChatterVoice value)
{
}
public override void OnInitialRemove(string key)
protected override void OnInitialRemove(string key)
{
}

View File

@ -25,9 +25,9 @@ namespace HermesSocketServer.Store
}
public abstract Task Load();
public abstract void OnInitialAdd(K key, V value);
public abstract void OnInitialModify(K key, V value);
public abstract void OnInitialRemove(K key);
protected abstract void OnInitialAdd(K key, V value);
protected abstract void OnInitialModify(K key, V value);
protected abstract void OnInitialRemove(K key);
public abstract Task<bool> Save();
public V? Get(K key)
@ -48,6 +48,31 @@ namespace HermesSocketServer.Store
}
}
public bool Modify(K? key, Action<V> action)
{
if (key == null)
return false;
lock (_lock)
{
if (_store.TryGetValue(key, out V? value))
{
if (value == null)
return false;
action(value);
if (!_added.Contains(key) && !_modified.Contains(key))
{
_modified.Add(key);
_logger.Information($"added key to _modified {key}");
}
OnInitialModify(key, value);
return true;
}
}
return false;
}
public void Remove(K? key)
{
if (key == null)

View File

@ -5,6 +5,7 @@ namespace HermesSocketServer.Store
V? Get(K key);
IDictionary<K, V> Get();
Task Load();
bool Modify(K? key, Action<V> action);
void Remove(K? key);
Task<bool> Save();
bool Set(K? key, V? value);

View File

@ -44,15 +44,15 @@ namespace HermesSocketServer.Store
_logger.Information($"Loaded {_store.Count} users from database.");
}
public override void OnInitialAdd(string key, User value)
protected override void OnInitialAdd(string key, User value)
{
}
public override void OnInitialModify(string key, User value)
protected override void OnInitialModify(string key, User value)
{
}
public override void OnInitialRemove(string key)
protected override void OnInitialRemove(string key)
{
}

View File

@ -43,18 +43,18 @@ namespace HermesSocketServer.Store
_logger.Information($"Loaded {_store.Count} TTS voices from database.");
}
public override void OnInitialAdd(string key, Voice value)
protected override void OnInitialAdd(string key, Voice value)
{
_idValidator.Check(value.Id);
_nameValidator.Check(value.Name);
}
public override void OnInitialModify(string key, Voice value)
protected override void OnInitialModify(string key, Voice value)
{
_nameValidator.Check(value.Name);
}
public override void OnInitialRemove(string key)
protected override void OnInitialRemove(string key)
{
}