Updated Default TTS requests to use stores. Added a modify method for grouped save stores.
This commit is contained in:
parent
4d0b38babd
commit
0fa0ddcc45
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user