diff --git a/Requests/CreateTTSUser.cs b/Requests/CreateTTSUser.cs index f5d3121..82d89fa 100644 --- a/Requests/CreateTTSUser.cs +++ b/Requests/CreateTTSUser.cs @@ -1,9 +1,6 @@ -using System.Text.Json; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketServer.Models; using HermesSocketServer.Services; -using HermesSocketServer.Store; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -11,6 +8,8 @@ namespace HermesSocketServer.Requests public class CreateTTSUser : IRequest { public string Name => "create_tts_user"; + + public string[] RequiredKeys => ["chatter", "voice"]; private ChannelManager _channels; private Database _database; private readonly ServerConfiguration _configuration; @@ -26,40 +25,31 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - if (long.TryParse(data["chatter"].ToString(), out long chatterId)) data["chatter"] = chatterId; else - return new RequestResult(false, "Invalid Twitch user id"); - - if (data["voice"] is JsonElement v) - data["voice"] = v.ToString(); - else - return new RequestResult(false, "Invalid voice id"); - - data["user"] = sender; + return RequestResult.Failed("Invalid Twitch user id"); + + data["voice"] = data["voice"].ToString(); var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId) - return new RequestResult(false, "Voice is disabled on this channel."); + return RequestResult.Failed("Voice is disabled on this channel."); var channel = _channels.Get(sender); - if (channel == null) - return new RequestResult(false, null); - - channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() + bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() { UserId = sender, ChatterId = chatterId.ToString(), VoiceId = data["voice"].ToString()! }); - _logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]"); - return new RequestResult(true, null); + + if (result) + { + _logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]"); + return RequestResult.Successful(null); + } + return RequestResult.Failed("Something went wrong when updating the cache."); } } } \ No newline at end of file diff --git a/Requests/CreateTTSVoice.cs b/Requests/CreateTTSVoice.cs index 8e691f7..eaa07ab 100644 --- a/Requests/CreateTTSVoice.cs +++ b/Requests/CreateTTSVoice.cs @@ -1,5 +1,3 @@ -using System.Text.Json; -using HermesSocketLibrary.Requests; using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class CreateTTSVoice : IRequest { public string Name => "create_tts_voice"; + public string[] RequiredKeys => ["voice"]; private IStore _voices; private ILogger _logger; private Random _random; @@ -23,27 +22,20 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - - if (data["voice"] is JsonElement v) - data["voice"] = v.ToString(); - else - return new RequestResult(false, "Invalid voice name."); - + data["voice"] = data["voice"].ToString()!; string id = RandomString(25); - _voices.Set(id, new Voice() + var result = _voices.Set(id, new Voice() { Id = id, - Name = data["voice"].ToString()! + Name = data["voice"].ToString() }); - _logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]"); - return new RequestResult(true, id); + if (result) { + _logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]"); + return RequestResult.Successful(id); + } + return RequestResult.Failed("Something went wrong when updating the cache."); } private string RandomString(int length) diff --git a/Requests/DeleteTTSVoice.cs b/Requests/DeleteTTSVoice.cs index a77efe9..4f82f85 100644 --- a/Requests/DeleteTTSVoice.cs +++ b/Requests/DeleteTTSVoice.cs @@ -1,5 +1,3 @@ -using System.Text.Json; -using HermesSocketLibrary.Requests; using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class DeleteTTSVoice : IRequest { public string Name => "delete_tts_voice"; + public string[] RequiredKeys => ["voice"]; private IStore _voices; private ILogger _logger; @@ -20,18 +19,9 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - - if (data["voice"] is JsonElement v) - data["voice"] = v.ToString(); - - _voices.Remove(data["voice"].ToString()); + _voices.Remove(data!["voice"].ToString()); _logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]"); - return new RequestResult(true, null); + return RequestResult.Successful(null); } } } \ No newline at end of file diff --git a/Requests/GetChatterIds.cs b/Requests/GetChatterIds.cs index 3728f55..38dd397 100644 --- a/Requests/GetChatterIds.cs +++ b/Requests/GetChatterIds.cs @@ -1,5 +1,4 @@ using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,6 +6,7 @@ namespace HermesSocketServer.Requests public class GetChatterIds : IRequest { public string Name => "get_chatter_ids"; + public string[] RequiredKeys => []; private Database _database; private ILogger _logger; @@ -22,7 +22,7 @@ namespace HermesSocketServer.Requests string sql = $"SELECT id FROM \"Chatter\""; await _database.Execute(sql, (IDictionary?) null, (r) => ids.Add(r.GetInt64(0))); _logger.Information($"Fetched all chatters for channel [channel: {sender}]"); - return new RequestResult(true, ids, notifyClientsOnAccount: false); + return RequestResult.Successful(ids, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetConnections.cs b/Requests/GetConnections.cs index d55f944..3539f58 100644 --- a/Requests/GetConnections.cs +++ b/Requests/GetConnections.cs @@ -1,5 +1,4 @@ using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Socket.Data; namespace HermesSocketServer.Requests @@ -7,7 +6,7 @@ namespace HermesSocketServer.Requests public class GetConnections : IRequest { public string Name => "get_connections"; - + public string[] RequiredKeys => []; private Database _database; private Serilog.ILogger _logger; @@ -22,8 +21,8 @@ namespace HermesSocketServer.Requests var temp = new Dictionary() { { "user", sender } }; var connections = new List(); - string sql3 = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user"; - await _database.Execute(sql3, temp, sql => + string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user"; + await _database.Execute(sql, temp, sql => connections.Add(new Connection() { Name = sql.GetString(0), @@ -36,7 +35,7 @@ namespace HermesSocketServer.Requests Default = sql.GetBoolean(7) }) ); - return new RequestResult(true, connections, false); + return RequestResult.Successful(connections, false); } } } \ No newline at end of file diff --git a/Requests/GetDefaultTTSVoice.cs b/Requests/GetDefaultTTSVoice.cs index 887980a..de43cc1 100644 --- a/Requests/GetDefaultTTSVoice.cs +++ b/Requests/GetDefaultTTSVoice.cs @@ -1,4 +1,3 @@ -using HermesSocketLibrary.Requests; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -7,6 +6,7 @@ namespace HermesSocketServer.Requests public class GetDefaultTTSVoice : IRequest { public string Name => "get_default_tts_voice"; + public string[] RequiredKeys => []; private readonly UserStore _users; private readonly ServerConfiguration _configuration; private readonly ILogger _logger; @@ -22,9 +22,9 @@ namespace HermesSocketServer.Requests { var user = _users.Get(sender); if (user == null) - return new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false); + return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false); - return new RequestResult(true, user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false); + return RequestResult.Successful(user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetEmotes.cs b/Requests/GetEmotes.cs index 5f956a9..82daf09 100644 --- a/Requests/GetEmotes.cs +++ b/Requests/GetEmotes.cs @@ -1,6 +1,4 @@ -using System.Text.Json; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class GetEmotes : IRequest { public string Name => "get_emotes"; + public string[] RequiredKeys => []; private Database _database; private ILogger _logger; @@ -28,7 +27,7 @@ namespace HermesSocketServer.Requests Name = r.GetString(1) })); _logger.Information($"Fetched all emotes for channel [channel: {sender}]"); - return new RequestResult(true, emotes, notifyClientsOnAccount: false); + return RequestResult.Successful(emotes, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetEnabledTTSVoices.cs b/Requests/GetEnabledTTSVoices.cs index ade970f..430a999 100644 --- a/Requests/GetEnabledTTSVoices.cs +++ b/Requests/GetEnabledTTSVoices.cs @@ -1,5 +1,4 @@ using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,7 +6,7 @@ namespace HermesSocketServer.Requests public class GetEnabledTTSVoices : IRequest { public string Name => "get_enabled_tts_voices"; - + public string[] RequiredKeys => []; private readonly Database _database; private readonly ILogger _logger; @@ -27,7 +26,7 @@ namespace HermesSocketServer.Requests + "WHERE \"userId\" = @user AND state = true"; await _database.Execute(sql, temp, (r) => voices.Add(r.GetString(0))); _logger.Information($"Fetched all enabled TTS voice for channel [channel: {sender}]"); - return new RequestResult(true, voices, notifyClientsOnAccount: false); + return RequestResult.Successful(voices, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetPermissions.cs b/Requests/GetPermissions.cs index 9fc6468..d4f1df3 100644 --- a/Requests/GetPermissions.cs +++ b/Requests/GetPermissions.cs @@ -1,5 +1,4 @@ using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; @@ -8,7 +7,7 @@ namespace HermesSocketServer.Requests public class GetPermissions : IRequest { public string Name => "get_permissions"; - + public string[] RequiredKeys => []; private readonly Database _database; private readonly ILogger _logger; @@ -56,7 +55,7 @@ namespace HermesSocketServer.Requests GroupChatters = groupChatters, GroupPermissions = groupPermissions }; - return new RequestResult(true, info, notifyClientsOnAccount: false); + return RequestResult.Successful(info, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetRedeemableActions.cs b/Requests/GetRedeemableActions.cs index 1a15780..cda0b3a 100644 --- a/Requests/GetRedeemableActions.cs +++ b/Requests/GetRedeemableActions.cs @@ -1,6 +1,5 @@ using System.Text.Json; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; @@ -9,7 +8,7 @@ namespace HermesSocketServer.Requests public class GetRedeemableActions : IRequest { public string Name => "get_redeemable_actions"; - + public string[] RequiredKeys => []; private readonly JsonSerializerOptions _options; private readonly Database _database; private readonly ILogger _logger; @@ -34,7 +33,7 @@ namespace HermesSocketServer.Requests Data = JsonSerializer.Deserialize>(r.GetString(2), _options)! })); _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]"); - return new RequestResult(true, redemptions, notifyClientsOnAccount: false); + return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetRedemptions.cs b/Requests/GetRedemptions.cs index 9f03982..a4245df 100644 --- a/Requests/GetRedemptions.cs +++ b/Requests/GetRedemptions.cs @@ -1,5 +1,4 @@ using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; @@ -8,7 +7,7 @@ namespace HermesSocketServer.Requests public class GetRedemptions : IRequest { public string Name => "get_redemptions"; - + public string[] RequiredKeys => []; private readonly Database _database; private readonly ILogger _logger; @@ -33,7 +32,7 @@ namespace HermesSocketServer.Requests State = r.GetBoolean(4) })); _logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); - return new RequestResult(true, redemptions, notifyClientsOnAccount: false); + return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetTTSUsers.cs b/Requests/GetTTSUsers.cs index f037e01..9f83ca1 100644 --- a/Requests/GetTTSUsers.cs +++ b/Requests/GetTTSUsers.cs @@ -1,4 +1,3 @@ -using HermesSocketLibrary.Requests; using HermesSocketServer.Services; using ILogger = Serilog.ILogger; @@ -7,6 +6,7 @@ namespace HermesSocketServer.Requests public class GetTTSUsers : IRequest { public string Name => "get_tts_users"; + public string[] RequiredKeys => []; private ChannelManager _channels; private ILogger _logger; @@ -19,12 +19,9 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { var channel = _channels.Get(sender); - if (channel == null) - return new RequestResult(false, null, notifyClientsOnAccount: false); - - var temp = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId); + var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId); _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]"); - return new RequestResult(true, temp, notifyClientsOnAccount: false); + return RequestResult.Successful(results, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetTTSVoices.cs b/Requests/GetTTSVoices.cs index 79d9a6d..e75a468 100644 --- a/Requests/GetTTSVoices.cs +++ b/Requests/GetTTSVoices.cs @@ -1,5 +1,3 @@ -using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class GetTTSVoices : IRequest { public string Name => "get_tts_voices"; + public string[] RequiredKeys => []; private VoiceStore _voices; private ILogger _logger; @@ -27,7 +26,7 @@ namespace HermesSocketServer.Requests }); _logger.Information($"Fetched all TTS voices for channel [channel: {sender}]"); - return new RequestResult(true, voices, notifyClientsOnAccount: false); + return RequestResult.Successful(voices, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/GetTTSWordFilters.cs b/Requests/GetTTSWordFilters.cs index 90baf03..498e4ae 100644 --- a/Requests/GetTTSWordFilters.cs +++ b/Requests/GetTTSWordFilters.cs @@ -1,6 +1,4 @@ -using System.Text.RegularExpressions; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class GetTTSWordFilters : IRequest { public string Name => "get_tts_word_filters"; + public string[] RequiredKeys => []; private readonly Database _database; private readonly ILogger _logger; @@ -31,7 +30,7 @@ namespace HermesSocketServer.Requests Replace = r.GetString(2) })); _logger.Information($"Fetched all word filters for channel [channel: {sender}]"); - return new RequestResult(true, filters, notifyClientsOnAccount: false); + return RequestResult.Successful(filters, notifyClientsOnAccount: false); } } } \ No newline at end of file diff --git a/Requests/IRequest.cs b/Requests/IRequest.cs new file mode 100644 index 0000000..0ae4c56 --- /dev/null +++ b/Requests/IRequest.cs @@ -0,0 +1,10 @@ +namespace HermesSocketServer.Requests +{ + public interface IRequest + { + string Name { get; } + string[] RequiredKeys { get; } + + Task Grant(string sender, IDictionary? data); + } +} \ No newline at end of file diff --git a/Requests/IRequestManager.cs b/Requests/IRequestManager.cs new file mode 100644 index 0000000..39d13ff --- /dev/null +++ b/Requests/IRequestManager.cs @@ -0,0 +1,9 @@ +using HermesSocketLibrary.Socket.Data; + +namespace HermesSocketServer.Requests +{ + public interface IRequestManager + { + Task Grant(string sender, RequestMessage? message); + } +} \ No newline at end of file diff --git a/Requests/RequestManager.cs b/Requests/RequestManager.cs new file mode 100644 index 0000000..18a0daa --- /dev/null +++ b/Requests/RequestManager.cs @@ -0,0 +1,59 @@ +using HermesSocketLibrary.Socket.Data; +using HermesSocketServer.Services; +using Serilog; + +namespace HermesSocketServer.Requests +{ + public class RequestManager : IRequestManager + { + private readonly IDictionary _requests; + private readonly ChannelManager _channels; + private readonly Serilog.ILogger _logger; + + + public RequestManager(IEnumerable requests, ChannelManager channels, Serilog.ILogger logger) + { + _requests = requests.ToDictionary(r => r.Name, r => r); + _channels = channels; + _logger = logger; + } + + public async Task Grant(string sender, RequestMessage? message) + { + if (message == null || message.Type == null) + return RequestResult.Failed("Request type does not exist."); + + var channel = _channels.Get(sender); + if (channel == null) + return RequestResult.Failed("Channel does not exist."); + + if (!_requests.TryGetValue(message.Type, out IRequest? request) || request == null) + { + _logger.Warning($"Could not find request [type: {message.Type}]"); + return RequestResult.Failed("Request does not exist."); + } + + if (request.RequiredKeys.Any()) + { + if (message.Data == null) + return RequestResult.Failed($"Request is lacking data entries."); + + foreach (var key in request.RequiredKeys) + { + if (!message.Data.ContainsKey(key)) + return RequestResult.Failed($"Request is missing '{key}' in its data entries."); + } + } + + try + { + return await request.Grant(sender, message.Data); + } + catch (Exception e) + { + _logger.Error(e, $"Failed to grant a request during processing [type: {message.Type}]"); + return RequestResult.Failed("Failed to grant request during processing: " + e.Message); + } + } + } +} \ No newline at end of file diff --git a/Requests/RequestResult.cs b/Requests/RequestResult.cs new file mode 100644 index 0000000..f685dd5 --- /dev/null +++ b/Requests/RequestResult.cs @@ -0,0 +1,26 @@ +namespace HermesSocketServer.Requests +{ + public class RequestResult + { + public bool Success; + public object? Result; + public bool NotifyClientsOnAccount; + + private RequestResult(bool success, object? result, bool notifyClientsOnAccount = true) + { + Success = success; + Result = result; + NotifyClientsOnAccount = notifyClientsOnAccount; + } + + public static RequestResult Successful(object? result, bool notifyClientsOnAccount = true) + { + return RequestResult.Successful(result, notifyClientsOnAccount); + } + + public static RequestResult Failed(string error, bool notifyClientsOnAccount = true) + { + return RequestResult.Successful(error, notifyClientsOnAccount); + } + } +} \ No newline at end of file diff --git a/Requests/UpdateDefaultTTSVoice.cs b/Requests/UpdateDefaultTTSVoice.cs index 19928e8..393738c 100644 --- a/Requests/UpdateDefaultTTSVoice.cs +++ b/Requests/UpdateDefaultTTSVoice.cs @@ -1,4 +1,3 @@ -using HermesSocketLibrary.Requests; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -7,6 +6,7 @@ namespace HermesSocketServer.Requests public class UpdateDefaultTTSVoice : IRequest { public string Name => "update_default_tts_voice"; + public string[] RequiredKeys => ["user", "voice"]; private UserStore _users; private ILogger _logger; @@ -18,21 +18,15 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - data["user"] = data["user"].ToString(); data["voice"] = data["voice"].ToString(); 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); + return RequestResult.Failed("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); + return RequestResult.Successful(null); } } } \ No newline at end of file diff --git a/Requests/UpdateTTSUser.cs b/Requests/UpdateTTSUser.cs index 700ab5f..6d1b07a 100644 --- a/Requests/UpdateTTSUser.cs +++ b/Requests/UpdateTTSUser.cs @@ -1,10 +1,6 @@ -using System.Text.Json; -using System.Threading.Channels; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketServer.Models; using HermesSocketServer.Services; -using HermesSocketServer.Store; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -12,7 +8,7 @@ namespace HermesSocketServer.Requests public class UpdateTTSUser : IRequest { public string Name => "update_tts_user"; - + public string[] RequiredKeys => ["chatter", "voice"]; private ChannelManager _channels; private Database _database; private readonly ServerConfiguration _configuration; @@ -29,34 +25,27 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - if (long.TryParse(data["chatter"].ToString(), out long chatterId)) data["chatter"] = chatterId; - if (data["voice"] is JsonElement v) - data["voice"] = v.ToString(); - data["user"] = sender; + data["voice"] = data["voice"].ToString(); var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId) - return new RequestResult(false, null); + return RequestResult.Failed("Voice is either non-existent or disabled on this channel."); var channel = _channels.Get(sender); - if (channel == null) - return new RequestResult(false, null); - - channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() + var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() { UserId = sender, ChatterId = chatterId.ToString(), VoiceId = data["voice"].ToString()! }); - _logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]"); - return new RequestResult(true, null); + if (result) + { + _logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]"); + return RequestResult.Successful(null); + } + return RequestResult.Failed("Soemthing went wrong when updating the cache."); } } } \ No newline at end of file diff --git a/Requests/UpdateTTSVoice.cs b/Requests/UpdateTTSVoice.cs index db59524..5a98d12 100644 --- a/Requests/UpdateTTSVoice.cs +++ b/Requests/UpdateTTSVoice.cs @@ -1,5 +1,3 @@ -using System.Text.Json; -using HermesSocketLibrary.Requests; using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests public class UpdateTTSVoice : IRequest { public string Name => "update_tts_voice"; + public string[] RequiredKeys => ["voice", "voiceId"]; private IStore _voices; private ILogger _logger; @@ -20,24 +19,20 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } + data["voice"] = data["voice"].ToString(); + data["voiceid"] = data["voiceid"].ToString(); - if (data["voice"] is JsonElement v) - data["voice"] = v.ToString(); - if (data["voiceid"] is JsonElement id) - data["voiceid"] = id.ToString(); - - _voices.Set(data["voiceid"].ToString(), new Voice() + var result = _voices.Set(data["voiceid"].ToString(), new Voice() { Id = data["voiceid"].ToString()!, Name = data["voice"].ToString()! }); - _logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); - return new RequestResult(true, null); + if (result) + { + _logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); + return RequestResult.Successful(null); + } + return RequestResult.Failed("Something went wrong when updating the cache."); } } } \ No newline at end of file diff --git a/Requests/UpdateTTSVoiceState.cs b/Requests/UpdateTTSVoiceState.cs index 7791181..e8adb95 100644 --- a/Requests/UpdateTTSVoiceState.cs +++ b/Requests/UpdateTTSVoiceState.cs @@ -1,6 +1,4 @@ -using System.Text.Json; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,6 +6,7 @@ namespace HermesSocketServer.Requests public class UpdateTTSVoiceState : IRequest { public string Name => "update_tts_voice_state"; + public string[] RequiredKeys => ["voice", "state"]; private Database _database; private ILogger _logger; @@ -19,22 +18,16 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (data == null) - { - _logger.Warning("Data received from request is null. Ignoring it."); - return new RequestResult(false, null); - } - - if (data["voice"] is JsonElement voice) - data["voice"] = voice.ToString(); - if (data["state"] is JsonElement state) - data["state"] = state.ToString() == "True"; + data["voice"] = data["voice"].ToString(); + data["state"] = data["state"].ToString() == "True"; data["user"] = sender; string sql = "INSERT INTO \"TtsVoiceState\" (\"userId\", \"ttsVoiceId\", state) VALUES (@user, @voice, @state) ON CONFLICT (\"userId\", \"ttsVoiceId\") DO UPDATE SET state = @state"; var result = await _database.Execute(sql, data); _logger.Information($"Updated voice's [voice id: {data["voice"]}] state [new state: {data["state"]}][channel: {data["user"]}]"); - return new RequestResult(result == 1, null); + if (result > 0) + return RequestResult.Successful(null); + return RequestResult.Failed("Something went wrong when updating the database."); } } } \ No newline at end of file diff --git a/Socket/Handlers/RequestHandler.cs b/Socket/Handlers/RequestHandler.cs index d720af5..6a816fd 100644 --- a/Socket/Handlers/RequestHandler.cs +++ b/Socket/Handlers/RequestHandler.cs @@ -1,5 +1,5 @@ -using HermesSocketLibrary.Requests; using HermesSocketLibrary.Socket.Data; +using HermesSocketServer.Requests; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Socket.Handlers