From 8277ea01540160cca32b8e9f9a0cd525608f8ad2 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 27 Dec 2024 23:31:36 +0000 Subject: [PATCH] Changed sender parameter to channel. --- Requests/CreatePolicy.cs | 13 ++++--------- Requests/CreateTTSFilter.cs | 14 +++++--------- Requests/CreateTTSUser.cs | 16 +++++----------- Requests/CreateTTSVoice.cs | 9 ++++----- Requests/DeletePolicy.cs | 12 ++++-------- Requests/DeleteTTSFilter.cs | 14 ++++++-------- Requests/DeleteTTSVoice.cs | 7 ++++--- Requests/GetChatterIds.cs | 14 +++++--------- Requests/GetConnections.cs | 10 ++++++---- Requests/GetDefaultTTSVoice.cs | 5 +++-- Requests/GetEmotes.cs | 5 +++-- Requests/GetEnabledTTSVoices.cs | 7 ++++--- Requests/GetPermissions.cs | 7 ++++--- Requests/GetPolicies.cs | 11 ++++------- Requests/GetRedeemableActions.cs | 7 ++++--- Requests/GetRedemptions.cs | 7 ++++--- Requests/GetTTSUsers.cs | 11 ++++------- Requests/GetTTSVoices.cs | 5 +++-- Requests/GetTTSWordFilters.cs | 6 +++--- Requests/IRequest.cs | 4 +++- Requests/RequestManager.cs | 2 +- Requests/UpdateDefaultTTSVoice.cs | 13 +++++++------ Requests/UpdatePolicy.cs | 7 +++---- Requests/UpdateTTSFilter.cs | 8 ++++---- Requests/UpdateTTSUser.cs | 14 ++++++-------- Requests/UpdateTTSVoice.cs | 14 +++++++------- Requests/UpdateTTSVoiceState.cs | 5 +++-- 27 files changed, 113 insertions(+), 134 deletions(-) diff --git a/Requests/CreatePolicy.cs b/Requests/CreatePolicy.cs index a760666..631254b 100644 --- a/Requests/CreatePolicy.cs +++ b/Requests/CreatePolicy.cs @@ -1,5 +1,4 @@ using HermesSocketServer.Models; -using HermesSocketServer.Services; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,16 +7,14 @@ namespace HermesSocketServer.Requests { public string Name => "create_policy"; public string[] RequiredKeys => ["groupId", "path", "count", "span"]; - private ChannelManager _channels; private ILogger _logger; - public CreatePolicy(ChannelManager channels, ILogger logger) + public CreatePolicy(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { var id = Guid.NewGuid(); string groupId = data["groupId"].ToString()!; @@ -28,19 +25,17 @@ namespace HermesSocketServer.Requests var policy = new PolicyMessage() { Id = id, - UserId = sender, + UserId = channel.Id, GroupId = Guid.Parse(groupId), Path = path, Usage = count, Span = span, }; - var channel = _channels.Get(sender); bool result = channel.Policies.Set(id.ToString(), policy); - if (result) { - _logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]"); + _logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]"); return RequestResult.Successful(policy); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/CreateTTSFilter.cs b/Requests/CreateTTSFilter.cs index e38fe27..ee86dcf 100644 --- a/Requests/CreateTTSFilter.cs +++ b/Requests/CreateTTSFilter.cs @@ -1,5 +1,5 @@ using HermesSocketLibrary.Requests.Messages; -using HermesSocketServer.Services; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,16 +8,14 @@ namespace HermesSocketServer.Requests { public string Name => "create_tts_filter"; public string[] RequiredKeys => ["search", "replace"]; - private ChannelManager _channels; private ILogger _logger; - public CreateTTSFilter(ChannelManager channels, ILogger logger) + public CreateTTSFilter(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { var id = Guid.NewGuid(); string search = data["search"].ToString()!; @@ -26,17 +24,15 @@ namespace HermesSocketServer.Requests var filter = new TTSWordFilter() { Id = id.ToString(), - UserId = sender, + UserId = channel.Id, Search = search, Replace = replace, }; - var channel = _channels.Get(sender); bool result = channel.Filters.Set(id.ToString(), filter); - if (result) { - _logger.Information($"Added filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {sender}]"); + _logger.Information($"Added filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]"); return RequestResult.Successful(filter); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/CreateTTSUser.cs b/Requests/CreateTTSUser.cs index 6d79c2e..140bb06 100644 --- a/Requests/CreateTTSUser.cs +++ b/Requests/CreateTTSUser.cs @@ -1,6 +1,5 @@ using HermesSocketLibrary.db; using HermesSocketServer.Models; -using HermesSocketServer.Services; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -9,37 +8,32 @@ namespace HermesSocketServer.Requests { public string Name => "create_tts_user"; public string[] RequiredKeys => ["chatter", "voice"]; - private ChannelManager _channels; private Database _database; private readonly ServerConfiguration _configuration; private ILogger _logger; - public CreateTTSUser(ChannelManager channels, Database database, ServerConfiguration configuration, ILogger logger) + public CreateTTSUser(Database database, ServerConfiguration configuration, ILogger logger) { _database = database; - _channels = channels; _configuration = configuration; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - if (data == null) - return RequestResult.Failed("Data received from client is null."); if (!long.TryParse(data["chatter"].ToString(), out long chatterId)) return RequestResult.Failed("Invalid Twitch user id"); - data["user"] = sender; - data["voice"] = data["voice"].ToString(); + data["user"] = channel.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 RequestResult.Failed("Voice is disabled on this channel."); - var channel = _channels.Get(sender); bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() { - UserId = sender, + UserId = channel.Id, ChatterId = chatterId, VoiceId = data["voice"].ToString()! }); diff --git a/Requests/CreateTTSVoice.cs b/Requests/CreateTTSVoice.cs index eaa07ab..f6c57f3 100644 --- a/Requests/CreateTTSVoice.cs +++ b/Requests/CreateTTSVoice.cs @@ -19,20 +19,19 @@ namespace HermesSocketServer.Requests _random = new Random(); } - - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - data["voice"] = data["voice"].ToString()!; + string voice = data["voice"].ToString()!; string id = RandomString(25); var result = _voices.Set(id, new Voice() { Id = id, - Name = data["voice"].ToString() + Name = voice }); if (result) { - _logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]"); + _logger.Information($"Added a new voice [voice: {voice}][voice id: {id}]"); return RequestResult.Successful(id); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/DeletePolicy.cs b/Requests/DeletePolicy.cs index a7b3726..cf656ef 100644 --- a/Requests/DeletePolicy.cs +++ b/Requests/DeletePolicy.cs @@ -1,4 +1,4 @@ -using HermesSocketServer.Services; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,20 +7,16 @@ namespace HermesSocketServer.Requests { public string Name => "delete_policy"; public string[] RequiredKeys => ["id"]; - private ChannelManager _channels; private ILogger _logger; - public DeletePolicy(ChannelManager channels, ILogger logger) + public DeletePolicy(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var channel = _channels.Get(sender); - - string policyId = data!["id"].ToString()!; + string policyId = data["id"].ToString()!; var policy = channel.Policies.Get(policyId); if (policy != null) { channel.Policies.Remove(policyId); diff --git a/Requests/DeleteTTSFilter.cs b/Requests/DeleteTTSFilter.cs index 3dbd6cb..528cc16 100644 --- a/Requests/DeleteTTSFilter.cs +++ b/Requests/DeleteTTSFilter.cs @@ -1,4 +1,4 @@ -using HermesSocketServer.Services; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,21 +7,19 @@ namespace HermesSocketServer.Requests { public string Name => "delete_tts_filter"; public string[] RequiredKeys => ["id"]; - private ChannelManager _channels; private ILogger _logger; - public DeleteTTSFilter(ChannelManager channels, ILogger logger) + public DeleteTTSFilter(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var channel = _channels.Get(sender); - channel.Filters.Remove(data!["id"].ToString()); + string filterId = data["id"].ToString()!; + channel.Filters.Remove(filterId); - _logger.Information($"Deleted a TTS filter by id [tts filter id: {data["id"]}]"); + _logger.Information($"Deleted a TTS filter by id [tts filter id: {filterId}]"); return RequestResult.Successful(null); } } diff --git a/Requests/DeleteTTSVoice.cs b/Requests/DeleteTTSVoice.cs index 4f82f85..dc8b600 100644 --- a/Requests/DeleteTTSVoice.cs +++ b/Requests/DeleteTTSVoice.cs @@ -17,10 +17,11 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - _voices.Remove(data!["voice"].ToString()); - _logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]"); + string voiceId = data["voice"].ToString()!; + _voices.Remove(voiceId); + _logger.Information($"Deleted a voice by id [voice id: {voiceId}]"); return RequestResult.Successful(null); } } diff --git a/Requests/GetChatterIds.cs b/Requests/GetChatterIds.cs index 38dd397..3178207 100644 --- a/Requests/GetChatterIds.cs +++ b/Requests/GetChatterIds.cs @@ -1,4 +1,4 @@ -using HermesSocketLibrary.db; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,21 +7,17 @@ namespace HermesSocketServer.Requests { public string Name => "get_chatter_ids"; public string[] RequiredKeys => []; - private Database _database; private ILogger _logger; - public GetChatterIds(Database database, ILogger logger) + public GetChatterIds(ILogger logger) { - _database = database; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - IList ids = new List(); - 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}]"); + IEnumerable ids = channel.Chatters.Get().Values.Select(c => c.ChatterId); + _logger.Information($"Fetched all chatters for channel [channel: {channel.Id}]"); return RequestResult.Successful(ids, notifyClientsOnAccount: false); } } diff --git a/Requests/GetConnections.cs b/Requests/GetConnections.cs index b8863a5..3c5aa7e 100644 --- a/Requests/GetConnections.cs +++ b/Requests/GetConnections.cs @@ -1,5 +1,7 @@ using HermesSocketLibrary.db; using HermesSocketLibrary.Socket.Data; +using HermesSocketServer.Models; +using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { @@ -8,17 +10,17 @@ namespace HermesSocketServer.Requests public string Name => "get_connections"; public string[] RequiredKeys => []; private Database _database; - private Serilog.ILogger _logger; + private ILogger _logger; - public GetConnections(Database database, Serilog.ILogger logger) + public GetConnections(Database database, ILogger logger) { _database = database; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var temp = new Dictionary() { { "user", sender } }; + var temp = new Dictionary() { { "user", channel.Id } }; var connections = new List(); string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user"; diff --git a/Requests/GetDefaultTTSVoice.cs b/Requests/GetDefaultTTSVoice.cs index de43cc1..d8f5733 100644 --- a/Requests/GetDefaultTTSVoice.cs +++ b/Requests/GetDefaultTTSVoice.cs @@ -1,3 +1,4 @@ +using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -18,9 +19,9 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var user = _users.Get(sender); + var user = _users.Get(channel.Id); if (user == null) return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false); diff --git a/Requests/GetEmotes.cs b/Requests/GetEmotes.cs index 82daf09..442253c 100644 --- a/Requests/GetEmotes.cs +++ b/Requests/GetEmotes.cs @@ -1,5 +1,6 @@ using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { IList emotes = new List(); string sql = $"SELECT id, name FROM \"Emote\""; @@ -26,7 +27,7 @@ namespace HermesSocketServer.Requests Id = r.GetString(0), Name = r.GetString(1) })); - _logger.Information($"Fetched all emotes for channel [channel: {sender}]"); + _logger.Information($"Fetched all emotes for channel [channel: {channel.Id}]"); return RequestResult.Successful(emotes, notifyClientsOnAccount: false); } } diff --git a/Requests/GetEnabledTTSVoices.cs b/Requests/GetEnabledTTSVoices.cs index 430a999..011f1a5 100644 --- a/Requests/GetEnabledTTSVoices.cs +++ b/Requests/GetEnabledTTSVoices.cs @@ -1,4 +1,5 @@ using HermesSocketLibrary.db; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -16,16 +17,16 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var temp = new Dictionary() { { "user", sender } }; + var temp = new Dictionary() { { "user", channel.Id } }; var voices = new List(); string sql = $"SELECT v.name FROM \"TtsVoiceState\" s " + "INNER JOIN \"TtsVoice\" v ON s.\"ttsVoiceId\" = v.id " + "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}]"); + _logger.Information($"Fetched all enabled TTS voice for channel [channel: {channel.Id}]"); return RequestResult.Successful(voices, notifyClientsOnAccount: false); } } diff --git a/Requests/GetPermissions.cs b/Requests/GetPermissions.cs index d4f1df3..06ff65c 100644 --- a/Requests/GetPermissions.cs +++ b/Requests/GetPermissions.cs @@ -1,5 +1,6 @@ using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -17,9 +18,9 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var temp = new Dictionary() { { "user", sender } }; + var temp = new Dictionary() { { "user", channel.Id } }; var groups = new List(); string sql = $"SELECT id, name, priority FROM \"Group\" WHERE \"userId\" = @user"; @@ -47,7 +48,7 @@ namespace HermesSocketServer.Requests Path = r.GetString(2), Allow = r.GetBoolean(3) })); - _logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); + _logger.Information($"Fetched all redemptions for channel [channel: {channel.Id}]"); var info = new GroupInfo() { diff --git a/Requests/GetPolicies.cs b/Requests/GetPolicies.cs index 0840a08..90209ad 100644 --- a/Requests/GetPolicies.cs +++ b/Requests/GetPolicies.cs @@ -1,4 +1,4 @@ -using HermesSocketServer.Services; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,21 +7,18 @@ namespace HermesSocketServer.Requests { public string Name => "get_policies"; public string[] RequiredKeys => []; - private ChannelManager _channels; private ILogger _logger; - public GetPolicies(ChannelManager channels, ILogger logger) + public GetPolicies(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var channel = _channels.Get(sender); var results = channel.Policies.Get().Values; - _logger.Information($"Fetched policies for channel [policy size: {results.Count}][channel: {sender}]"); + _logger.Information($"Fetched policies for channel [policy size: {results.Count}][channel: {channel.Id}]"); return RequestResult.Successful(results, notifyClientsOnAccount: false); } } diff --git a/Requests/GetRedeemableActions.cs b/Requests/GetRedeemableActions.cs index cda0b3a..788479a 100644 --- a/Requests/GetRedeemableActions.cs +++ b/Requests/GetRedeemableActions.cs @@ -1,6 +1,7 @@ using System.Text.Json; using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -20,9 +21,9 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var temp = new Dictionary() { { "user", sender } }; + var temp = new Dictionary() { { "user", channel.Id } }; var redemptions = new List(); string sql = $"SELECT name, type, data FROM \"Action\" WHERE \"userId\" = @user"; @@ -32,7 +33,7 @@ namespace HermesSocketServer.Requests Type = r.GetString(1), Data = JsonSerializer.Deserialize>(r.GetString(2), _options)! })); - _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]"); + _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]"); return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); } } diff --git a/Requests/GetRedemptions.cs b/Requests/GetRedemptions.cs index a4245df..d26767d 100644 --- a/Requests/GetRedemptions.cs +++ b/Requests/GetRedemptions.cs @@ -1,5 +1,6 @@ using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -17,9 +18,9 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var temp = new Dictionary() { { "user", sender } }; + var temp = new Dictionary() { { "user", channel.Id } }; var redemptions = new List(); string sql = $"SELECT id, \"redemptionId\", \"actionName\", \"order\", state FROM \"Redemption\" WHERE \"userId\" = @user"; @@ -31,7 +32,7 @@ namespace HermesSocketServer.Requests Order = r.GetInt32(3), State = r.GetBoolean(4) })); - _logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); + _logger.Information($"Fetched all redemptions for channel [channel: {channel.Id}]"); return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); } } diff --git a/Requests/GetTTSUsers.cs b/Requests/GetTTSUsers.cs index 9f83ca1..793bfb4 100644 --- a/Requests/GetTTSUsers.cs +++ b/Requests/GetTTSUsers.cs @@ -1,4 +1,4 @@ -using HermesSocketServer.Services; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -7,20 +7,17 @@ namespace HermesSocketServer.Requests { public string Name => "get_tts_users"; public string[] RequiredKeys => []; - private ChannelManager _channels; private ILogger _logger; - public GetTTSUsers(ChannelManager channels, ILogger logger) + public GetTTSUsers(ILogger logger) { - _channels = channels; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var channel = _channels.Get(sender); 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}]"); + _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]"); return RequestResult.Successful(results, notifyClientsOnAccount: false); } } diff --git a/Requests/GetTTSVoices.cs b/Requests/GetTTSVoices.cs index e75a468..5689425 100644 --- a/Requests/GetTTSVoices.cs +++ b/Requests/GetTTSVoices.cs @@ -1,4 +1,5 @@ using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { IEnumerable voices = _voices.Get().Select(v => new VoiceDetails() { @@ -25,7 +26,7 @@ namespace HermesSocketServer.Requests Name = v.Value.Name }); - _logger.Information($"Fetched all TTS voices for channel [channel: {sender}]"); + _logger.Information($"Fetched all TTS voices for channel [channel: {channel.Id}]"); return RequestResult.Successful(voices, notifyClientsOnAccount: false); } } diff --git a/Requests/GetTTSWordFilters.cs b/Requests/GetTTSWordFilters.cs index 01522a1..f20ff4f 100644 --- a/Requests/GetTTSWordFilters.cs +++ b/Requests/GetTTSWordFilters.cs @@ -1,4 +1,5 @@ using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using HermesSocketServer.Services; using ILogger = Serilog.ILogger; @@ -17,12 +18,11 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - var channel = _channels.Get(sender); IEnumerable filters = channel.Filters.Get().Values; - _logger.Information($"Fetched all word filters for channel [channel: {sender}]"); + _logger.Information($"Fetched all word filters for channel [channel: {channel.Id}]"); return RequestResult.Successful(filters, notifyClientsOnAccount: false); } } diff --git a/Requests/IRequest.cs b/Requests/IRequest.cs index 0ae4c56..298c002 100644 --- a/Requests/IRequest.cs +++ b/Requests/IRequest.cs @@ -1,3 +1,5 @@ +using HermesSocketServer.Models; + namespace HermesSocketServer.Requests { public interface IRequest @@ -5,6 +7,6 @@ namespace HermesSocketServer.Requests string Name { get; } string[] RequiredKeys { get; } - Task Grant(string sender, IDictionary? data); + Task Grant(Channel channel, IDictionary? data); } } \ No newline at end of file diff --git a/Requests/RequestManager.cs b/Requests/RequestManager.cs index 715df50..a895cf5 100644 --- a/Requests/RequestManager.cs +++ b/Requests/RequestManager.cs @@ -58,7 +58,7 @@ namespace HermesSocketServer.Requests try { - return await request.Grant(sender, message.Data); + return await request.Grant(channel, message.Data); } catch (Exception e) { diff --git a/Requests/UpdateDefaultTTSVoice.cs b/Requests/UpdateDefaultTTSVoice.cs index 393738c..542be7f 100644 --- a/Requests/UpdateDefaultTTSVoice.cs +++ b/Requests/UpdateDefaultTTSVoice.cs @@ -1,3 +1,4 @@ +using HermesSocketServer.Models; using HermesSocketServer.Store; using ILogger = Serilog.ILogger; @@ -16,16 +17,16 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - data["user"] = data["user"].ToString(); - data["voice"] = data["voice"].ToString(); + string user = data["user"].ToString()!; + string voice = data["voice"].ToString()!; - var success = _users.Modify(data["user"].ToString(), (user) => user.DefaultVoice = data["voice"].ToString()!); + var success = _users.Modify(user, (user) => user.DefaultVoice = voice); if (!success) - return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false); + return RequestResult.Failed("Unable to find user data."); - _logger.Information($"Updated default TTS voice for channel [channel: {sender}][voice: {data["voice"]}]"); + _logger.Information($"Updated default TTS voice for channel [channel: {channel.Id}][voice: {voice}]"); return RequestResult.Successful(null); } } diff --git a/Requests/UpdatePolicy.cs b/Requests/UpdatePolicy.cs index 2b0b0f9..b1d487b 100644 --- a/Requests/UpdatePolicy.cs +++ b/Requests/UpdatePolicy.cs @@ -17,7 +17,7 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { var id = Guid.Parse(data["id"].ToString()!); string groupId = data["groupId"].ToString()!; @@ -25,11 +25,10 @@ namespace HermesSocketServer.Requests int count = int.Parse(data["count"].ToString()!); int span = int.Parse(data["span"].ToString()!); - var channel = _channels.Get(sender)!; bool result = channel.Policies.Set(id.ToString(), new PolicyMessage() { Id = id, - UserId = sender, + UserId = channel.Id, GroupId = Guid.Parse(groupId), Path = path, Usage = count, @@ -39,7 +38,7 @@ namespace HermesSocketServer.Requests if (result) { var policy = channel.Policies.Get(id.ToString()); - _logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]"); + _logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]"); return RequestResult.Successful(policy); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/UpdateTTSFilter.cs b/Requests/UpdateTTSFilter.cs index fad4a72..0c1735e 100644 --- a/Requests/UpdateTTSFilter.cs +++ b/Requests/UpdateTTSFilter.cs @@ -1,4 +1,5 @@ using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Models; using HermesSocketServer.Services; using ILogger = Serilog.ILogger; @@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { var id = data["id"].ToString()!; string search = data["search"].ToString()!; @@ -26,17 +27,16 @@ namespace HermesSocketServer.Requests var filter = new TTSWordFilter() { Id = id, - UserId = sender, + UserId = channel.Id, Search = search, Replace = replace, }; - var channel = _channels.Get(sender); bool result = channel.Filters.Set(id, filter); if (result) { - _logger.Information($"Updated filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {sender}]"); + _logger.Information($"Updated filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]"); return RequestResult.Successful(filter); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/UpdateTTSUser.cs b/Requests/UpdateTTSUser.cs index 2ffeb71..8f497a9 100644 --- a/Requests/UpdateTTSUser.cs +++ b/Requests/UpdateTTSUser.cs @@ -14,36 +14,34 @@ namespace HermesSocketServer.Requests private readonly ServerConfiguration _configuration; private ILogger _logger; - public UpdateTTSUser(ChannelManager channels, Database database, ServerConfiguration configuration, ILogger logger) + public UpdateTTSUser(Database database, ServerConfiguration configuration, ILogger logger) { _database = database; - _channels = channels; _configuration = configuration; _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { if (long.TryParse(data["chatter"].ToString(), out long chatterId)) data["chatter"] = chatterId; - data["voice"] = data["voice"].ToString(); - data["user"] = sender; + data["voice"] = data["voice"].ToString()!; + data["user"] = channel.Id; 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 RequestResult.Failed("Voice is either non-existent or disabled on this channel."); - var channel = _channels.Get(sender); var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() { - UserId = sender, + UserId = channel.Id, ChatterId = chatterId, VoiceId = data["voice"].ToString()! }); if (result) { - _logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]"); + _logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {channel.Id}]"); return RequestResult.Successful(null); } return RequestResult.Failed("Soemthing went wrong when updating the cache."); diff --git a/Requests/UpdateTTSVoice.cs b/Requests/UpdateTTSVoice.cs index 5a98d12..70c1e5b 100644 --- a/Requests/UpdateTTSVoice.cs +++ b/Requests/UpdateTTSVoice.cs @@ -17,19 +17,19 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { - data["voice"] = data["voice"].ToString(); - data["voiceid"] = data["voiceid"].ToString(); + string voiceName = data["voice"].ToString()!; + string voiceId = data["voiceid"].ToString()!; - var result = _voices.Set(data["voiceid"].ToString(), new Voice() + var result = _voices.Set(voiceId, new Voice() { - Id = data["voiceid"].ToString()!, - Name = data["voice"].ToString()! + Id = voiceId, + Name = voiceName }); if (result) { - _logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); + _logger.Information($"Updated voice's [voice id: {voiceId}] name [new name: {voiceName}]"); return RequestResult.Successful(null); } return RequestResult.Failed("Something went wrong when updating the cache."); diff --git a/Requests/UpdateTTSVoiceState.cs b/Requests/UpdateTTSVoiceState.cs index e8adb95..08116cf 100644 --- a/Requests/UpdateTTSVoiceState.cs +++ b/Requests/UpdateTTSVoiceState.cs @@ -1,4 +1,5 @@ using HermesSocketLibrary.db; +using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -16,11 +17,11 @@ namespace HermesSocketServer.Requests _logger = logger; } - public async Task Grant(string sender, IDictionary? data) + public async Task Grant(Channel channel, IDictionary data) { data["voice"] = data["voice"].ToString(); data["state"] = data["state"].ToString() == "True"; - data["user"] = sender; + data["user"] = channel.Id; 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);