From 6176e6f3b9816bd63c7bb2f026f1b490ac76bfd7 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 27 Dec 2024 22:28:22 +0000 Subject: [PATCH] Added TTS Filter cache. --- Models/Channel.cs | 1 + Requests/CreateTTSFilter.cs | 45 ++++++++++++++ Requests/CreateTTSUser.cs | 9 +-- Requests/DeleteTTSFilter.cs | 28 +++++++++ Requests/GetTTSWordFilters.cs | 21 +++---- Requests/UpdateTTSFilter.cs | 45 ++++++++++++++ Services/ChannelManager.cs | 36 +++++------ Startup.cs | 4 +- Store/TTSFilterStore.cs | 109 ++++++++++++++++++++++++++++++++++ 9 files changed, 262 insertions(+), 36 deletions(-) create mode 100644 Requests/CreateTTSFilter.cs create mode 100644 Requests/DeleteTTSFilter.cs create mode 100644 Requests/UpdateTTSFilter.cs create mode 100644 Store/TTSFilterStore.cs diff --git a/Models/Channel.cs b/Models/Channel.cs index a575583..dac297c 100644 --- a/Models/Channel.cs +++ b/Models/Channel.cs @@ -8,5 +8,6 @@ namespace HermesSocketServer.Models public User User { get; set; } public ChatterStore Chatters { get; set; } public PolicyStore Policies { get; set; } + public TTSFilterStore Filters { get; set; } } } \ No newline at end of file diff --git a/Requests/CreateTTSFilter.cs b/Requests/CreateTTSFilter.cs new file mode 100644 index 0000000..e38fe27 --- /dev/null +++ b/Requests/CreateTTSFilter.cs @@ -0,0 +1,45 @@ +using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Services; +using ILogger = Serilog.ILogger; + +namespace HermesSocketServer.Requests +{ + public class CreateTTSFilter : IRequest + { + public string Name => "create_tts_filter"; + public string[] RequiredKeys => ["search", "replace"]; + private ChannelManager _channels; + private ILogger _logger; + + public CreateTTSFilter(ChannelManager channels, ILogger logger) + { + _channels = channels; + _logger = logger; + } + + public async Task Grant(string sender, IDictionary? data) + { + var id = Guid.NewGuid(); + string search = data["search"].ToString()!; + string replace = data["replace"].ToString()!; + + var filter = new TTSWordFilter() + { + Id = id.ToString(), + UserId = sender, + 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}]"); + return RequestResult.Successful(filter); + } + return RequestResult.Failed("Something went wrong when updating the cache."); + } + } +} \ No newline at end of file diff --git a/Requests/CreateTTSUser.cs b/Requests/CreateTTSUser.cs index 1ec9115..6d79c2e 100644 --- a/Requests/CreateTTSUser.cs +++ b/Requests/CreateTTSUser.cs @@ -24,11 +24,12 @@ namespace HermesSocketServer.Requests public async Task Grant(string sender, IDictionary? data) { - if (long.TryParse(data["chatter"].ToString(), out long chatterId)) - data["chatter"] = chatterId; - else + 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(); var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; diff --git a/Requests/DeleteTTSFilter.cs b/Requests/DeleteTTSFilter.cs new file mode 100644 index 0000000..3dbd6cb --- /dev/null +++ b/Requests/DeleteTTSFilter.cs @@ -0,0 +1,28 @@ +using HermesSocketServer.Services; +using ILogger = Serilog.ILogger; + +namespace HermesSocketServer.Requests +{ + public class DeleteTTSFilter : IRequest + { + public string Name => "delete_tts_filter"; + public string[] RequiredKeys => ["id"]; + private ChannelManager _channels; + private ILogger _logger; + + public DeleteTTSFilter(ChannelManager channels, ILogger logger) + { + _channels = channels; + _logger = logger; + } + + public async Task Grant(string sender, IDictionary? data) + { + var channel = _channels.Get(sender); + channel.Filters.Remove(data!["id"].ToString()); + + _logger.Information($"Deleted a TTS filter by id [tts filter id: {data["id"]}]"); + return RequestResult.Successful(null); + } + } +} \ No newline at end of file diff --git a/Requests/GetTTSWordFilters.cs b/Requests/GetTTSWordFilters.cs index 498e4ae..01522a1 100644 --- a/Requests/GetTTSWordFilters.cs +++ b/Requests/GetTTSWordFilters.cs @@ -1,5 +1,5 @@ -using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Services; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests @@ -8,27 +8,20 @@ namespace HermesSocketServer.Requests { public string Name => "get_tts_word_filters"; public string[] RequiredKeys => []; - private readonly Database _database; - private readonly ILogger _logger; + private ChannelManager _channels; + private ILogger _logger; - public GetTTSWordFilters(Database database, ILogger logger) + public GetTTSWordFilters(ChannelManager channels, ILogger logger) { - _database = database; + _channels = channels; _logger = logger; } public async Task Grant(string sender, IDictionary? data) { - var temp = new Dictionary() { { "user", sender } }; + var channel = _channels.Get(sender); + IEnumerable filters = channel.Filters.Get().Values; - IList filters = new List(); - string sql = $"SELECT id, search, replace FROM \"TtsWordFilter\" WHERE \"userId\" = @user"; - await _database.Execute(sql, temp, (r) => filters.Add(new TTSWordFilter() - { - Id = r.GetString(0), - Search = r.GetString(1), - Replace = r.GetString(2) - })); _logger.Information($"Fetched all word filters for channel [channel: {sender}]"); return RequestResult.Successful(filters, notifyClientsOnAccount: false); } diff --git a/Requests/UpdateTTSFilter.cs b/Requests/UpdateTTSFilter.cs new file mode 100644 index 0000000..fad4a72 --- /dev/null +++ b/Requests/UpdateTTSFilter.cs @@ -0,0 +1,45 @@ +using HermesSocketLibrary.Requests.Messages; +using HermesSocketServer.Services; +using ILogger = Serilog.ILogger; + +namespace HermesSocketServer.Requests +{ + public class UpdateTTSFilter : IRequest + { + public string Name => "update_tts_filter"; + public string[] RequiredKeys => ["id", "search", "replace"]; + private ChannelManager _channels; + private ILogger _logger; + + public UpdateTTSFilter(ChannelManager channels, ILogger logger) + { + _channels = channels; + _logger = logger; + } + + public async Task Grant(string sender, IDictionary? data) + { + var id = data["id"].ToString()!; + string search = data["search"].ToString()!; + string replace = data["replace"].ToString()!; + + var filter = new TTSWordFilter() + { + Id = id, + UserId = sender, + 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}]"); + return RequestResult.Successful(filter); + } + return RequestResult.Failed("Something went wrong when updating the cache."); + } + } +} \ No newline at end of file diff --git a/Services/ChannelManager.cs b/Services/ChannelManager.cs index 73a543b..dcd8973 100644 --- a/Services/ChannelManager.cs +++ b/Services/ChannelManager.cs @@ -36,28 +36,28 @@ namespace HermesSocketServer.Services { return null; } - } - var chatters = new ChatterStore(userId, _database, _logger); - var policies = new PolicyStore(userId, _database, _logger); - await Task.WhenAll([ - chatters.Load(), - policies.Load(), - ]); + var chatters = new ChatterStore(userId, _database, _logger); + var policies = new PolicyStore(userId, _database, _logger); + var filters = new TTSFilterStore(userId, _database, _logger); + Task.WaitAll([ + chatters.Load(), + policies.Load(), + filters.Load(), + ]); - var channel = new Channel() - { - Id = userId, - User = user, - Chatters = chatters, - Policies = policies - }; + var channel = new Channel() + { + Id = userId, + User = user, + Chatters = chatters, + Policies = policies, + Filters = filters, + }; - lock (_lock) - { _channels.Add(userId, channel); + return channel; } - return channel; } public Channel? Get(string channelId) @@ -75,6 +75,7 @@ namespace HermesSocketServer.Services await Task.WhenAll([ channel.Chatters.Save(), channel.Policies.Save(), + channel.Filters.Save(), ]); } @@ -86,6 +87,7 @@ namespace HermesSocketServer.Services await Task.WhenAll([ channel.Chatters.Save(), channel.Policies.Save(), + channel.Filters.Save(), ]); } } diff --git a/Startup.cs b/Startup.cs index 3cc7f1a..3887c3d 100644 --- a/Startup.cs +++ b/Startup.cs @@ -2,7 +2,6 @@ using System.Net; using System.Text.Json; using HermesSocketLibrary; using HermesSocketLibrary.db; -using HermesSocketLibrary.Requests; using HermesSocketServer; using HermesSocketServer.Requests; using HermesSocketServer.Socket; @@ -85,9 +84,11 @@ s.AddSingleton(); // Request handlers s.AddSingleton(); +s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); +s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); @@ -101,6 +102,7 @@ s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); +s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); s.AddSingleton(); diff --git a/Store/TTSFilterStore.cs b/Store/TTSFilterStore.cs new file mode 100644 index 0000000..37c3815 --- /dev/null +++ b/Store/TTSFilterStore.cs @@ -0,0 +1,109 @@ +using System.Collections.Immutable; +using HermesSocketLibrary.db; +using HermesSocketLibrary.Requests.Messages; + +namespace HermesSocketServer.Store +{ + public class TTSFilterStore : GroupSaveStore + { + private readonly string _userId; + private readonly Database _database; + private readonly Serilog.ILogger _logger; + private readonly GroupSaveSqlGenerator _generator; + + + public TTSFilterStore(string userId, Database database, Serilog.ILogger logger) : base(logger) + { + _userId = userId; + _database = database; + _logger = logger; + + var ctp = new Dictionary + { + { "id", "Id" }, + { "userId", "UserId" }, + { "search", "Search" }, + { "replace", "Replace" }, + }; + _generator = new GroupSaveSqlGenerator(ctp, _logger); + } + + public override async Task Load() + { + var data = new Dictionary() { { "user", _userId } }; + string sql = $"SELECT id, search, replace FROM \"TtsWordFilter\" WHERE \"userId\" = @user"; + await _database.Execute(sql, data, (reader) => + { + var id = reader.GetString(0); + _store.Add(id.ToString(), new TTSWordFilter() + { + Id = id, + Search = reader.GetString(1), + Replace = reader.GetString(2) + }); + }); + _logger.Information($"Loaded {_store.Count} TTS chatter voices from database."); + } + + protected override void OnInitialAdd(string key, TTSWordFilter value) + { + } + + protected override void OnInitialModify(string key, TTSWordFilter value) + { + } + + protected override void OnInitialRemove(string key) + { + } + + public override async Task Save() + { + int count = 0; + string sql = string.Empty; + ImmutableList? list = null; + + if (_added.Any()) + { + lock (_lock) + { + list = _added.ToImmutableList(); + _added.Clear(); + } + count = list.Count; + sql = _generator.GeneratePreparedInsertSql("TtsWordFilter", count, ["id", "userId", "search", "replace"]); + + _logger.Debug($"TTS Filter - Adding {count} rows to database: {sql}"); + var values = list.Select(id => _store[id]).Where(v => v != null); + await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "search", "replace"]); + } + if (_modified.Any()) + { + lock (_lock) + { + list = _modified.ToImmutableList(); + _modified.Clear(); + } + count = list.Count; + sql = _generator.GeneratePreparedUpdateSql("TtsWordFilter", count, ["id"], ["userId", "search", "replace"]); + + _logger.Debug($"TTS Filter - Modifying {count} rows in database: {sql}"); + var values = list.Select(id => _store[id]).Where(v => v != null); + await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "search", "replace"]); + } + if (_deleted.Any()) + { + lock (_lock) + { + list = _deleted.ToImmutableList(); + _deleted.Clear(); + } + count = list.Count; + sql = _generator.GeneratePreparedDeleteSql("TtsWordFilter", count, ["id"]); + + _logger.Debug($"TTS Filter - Deleting {count} rows from database: {sql}"); + await _generator.DoPreparedStatementRaw(_database, sql, list, ["id"]); + } + } + } +} \ No newline at end of file