diff --git a/Requests/GetDefaultTTSVoice.cs b/Requests/GetDefaultTTSVoice.cs index 7faa37d..887980a 100644 --- a/Requests/GetDefaultTTSVoice.cs +++ b/Requests/GetDefaultTTSVoice.cs @@ -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 Grant(string sender, IDictionary? data) { - var temp = new Dictionary() { { "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); } } } \ No newline at end of file diff --git a/Requests/UpdateDefaultTTSVoice.cs b/Requests/UpdateDefaultTTSVoice.cs index ba9dc12..19928e8 100644 --- a/Requests/UpdateDefaultTTSVoice.cs +++ b/Requests/UpdateDefaultTTSVoice.cs @@ -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); } diff --git a/Store/ChatterStore.cs b/Store/ChatterStore.cs index 8bc425d..ca27a4a 100644 --- a/Store/ChatterStore.cs +++ b/Store/ChatterStore.cs @@ -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) { } diff --git a/Store/GroupedSaveStore.cs b/Store/GroupedSaveStore.cs index 996010d..839bb1c 100644 --- a/Store/GroupedSaveStore.cs +++ b/Store/GroupedSaveStore.cs @@ -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 Save(); public V? Get(K key) @@ -48,6 +48,31 @@ namespace HermesSocketServer.Store } } + public bool Modify(K? key, Action 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) diff --git a/Store/IStore.cs b/Store/IStore.cs index 42abf75..97c61ef 100644 --- a/Store/IStore.cs +++ b/Store/IStore.cs @@ -5,6 +5,7 @@ namespace HermesSocketServer.Store V? Get(K key); IDictionary Get(); Task Load(); + bool Modify(K? key, Action action); void Remove(K? key); Task Save(); bool Set(K? key, V? value); diff --git a/Store/UserStore.cs b/Store/UserStore.cs index ba7004c..cdae3bb 100644 --- a/Store/UserStore.cs +++ b/Store/UserStore.cs @@ -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) { } diff --git a/Store/VoiceStore.cs b/Store/VoiceStore.cs index 995178e..e37ae13 100644 --- a/Store/VoiceStore.cs +++ b/Store/VoiceStore.cs @@ -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) { }