From a49e52a6bb369d81baebdd969f1fa1b05f25b343 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 18 Jan 2025 16:37:58 +0000 Subject: [PATCH] Added group support for websockets. --- Chat/Groups/ChatterGroupManager.cs | 10 ++++ Chat/Groups/IChatterGroupManager.cs | 2 + Hermes/Socket/Requests/CreateGroupAck.cs | 48 ++++++++++++++++++ Hermes/Socket/Requests/CreatePolicyAck.cs | 7 ++- Hermes/Socket/Requests/DeleteGroupAck.cs | 48 ++++++++++++++++++ Hermes/Socket/Requests/UpdateGroupAck.cs | 49 +++++++++++++++++++ OBS/Socket/Handlers/RequestResponseHandler.cs | 2 +- 7 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 Hermes/Socket/Requests/CreateGroupAck.cs create mode 100644 Hermes/Socket/Requests/DeleteGroupAck.cs create mode 100644 Hermes/Socket/Requests/UpdateGroupAck.cs diff --git a/Chat/Groups/ChatterGroupManager.cs b/Chat/Groups/ChatterGroupManager.cs index 7413d5c..8105451 100644 --- a/Chat/Groups/ChatterGroupManager.cs +++ b/Chat/Groups/ChatterGroupManager.cs @@ -77,6 +77,16 @@ namespace TwitchChatTTS.Chat.Groups return 0; } + public void Modify(Group group) + { + _groups[group.Name] = group; + } + + public bool Remove(string groupId) + { + return _groups.Remove(groupId); + } + public bool Remove(long chatterId, string groupId) { if (_chatters.TryGetValue(chatterId, out var groups)) diff --git a/Chat/Groups/IChatterGroupManager.cs b/Chat/Groups/IChatterGroupManager.cs index 3388b81..dc861f9 100644 --- a/Chat/Groups/IChatterGroupManager.cs +++ b/Chat/Groups/IChatterGroupManager.cs @@ -12,6 +12,8 @@ namespace TwitchChatTTS.Chat.Groups IEnumerable GetGroupNamesFor(long chatter); int GetPriorityFor(long chatter); int GetPriorityFor(IEnumerable groupIds); + void Modify(Group group); + bool Remove(string groupId); bool Remove(long chatter, string groupId); } } \ No newline at end of file diff --git a/Hermes/Socket/Requests/CreateGroupAck.cs b/Hermes/Socket/Requests/CreateGroupAck.cs new file mode 100644 index 0000000..569f0c1 --- /dev/null +++ b/Hermes/Socket/Requests/CreateGroupAck.cs @@ -0,0 +1,48 @@ +using System.Text.Json; +using HermesSocketLibrary.Requests.Messages; +using Serilog; +using TwitchChatTTS.Chat.Groups; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class CreateGroupAck : IRequestAck + { + public string Name => "create_group"; + private readonly IChatterGroupManager _groups; + private readonly JsonSerializerOptions _options; + private readonly ILogger _logger; + + public CreateGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger) + { + _groups = groups; + _options = options; + _logger = logger; + } + + public void Acknowledge(string requestId, string? json, IDictionary? requestData) + { + if (string.IsNullOrWhiteSpace(json)) { + _logger.Warning($"Group JSON data is null."); + return; + } + + var group = JsonSerializer.Deserialize(json, _options); + if (group == null) + { + _logger.Warning($"Group data is null."); + return; + } + + var exists = _groups.Get(group.Id); + if (exists != null) + { + _logger.Warning($"Group id already exists [group id: {exists.Id}][group name: {exists.Name} / {group.Name}][group priority: {exists.Priority} / {group.Priority}]"); + return; + } + + _logger.Debug($"Adding group [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]"); + _groups.Add(group); + _logger.Information($"Group has been created [group id: {group.Id}]"); + } + } +} \ No newline at end of file diff --git a/Hermes/Socket/Requests/CreatePolicyAck.cs b/Hermes/Socket/Requests/CreatePolicyAck.cs index f1ec60d..cce259f 100644 --- a/Hermes/Socket/Requests/CreatePolicyAck.cs +++ b/Hermes/Socket/Requests/CreatePolicyAck.cs @@ -24,10 +24,15 @@ namespace TwitchChatTTS.Hermes.Socket.Requests public void Acknowledge(string requestId, string? json, IDictionary? requestData) { + if (string.IsNullOrWhiteSpace(json)) { + _logger.Warning($"Policy JSON data is null."); + return; + } + var policy = JsonSerializer.Deserialize(json, _options); if (policy == null) { - _logger.Warning($"Policy JSON data is null."); + _logger.Warning($"Policy data is null."); return; } diff --git a/Hermes/Socket/Requests/DeleteGroupAck.cs b/Hermes/Socket/Requests/DeleteGroupAck.cs new file mode 100644 index 0000000..a0e9709 --- /dev/null +++ b/Hermes/Socket/Requests/DeleteGroupAck.cs @@ -0,0 +1,48 @@ +using System.Text.Json; +using Serilog; +using TwitchChatTTS.Chat.Groups; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class DeleteGroupAck : IRequestAck + { + public string Name => "delete_group"; + private readonly IChatterGroupManager _groups; + private readonly JsonSerializerOptions _options; + private readonly ILogger _logger; + + public DeleteGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger) + { + _groups = groups; + _options = options; + _logger = logger; + } + + public void Acknowledge(string requestId, string? json, IDictionary? requestData) + { + if (requestData == null) + { + _logger.Warning("Request data is null."); + return; + } + + var groupId = requestData["group"].ToString(); + if (string.IsNullOrEmpty(groupId)) + { + _logger.Warning($"Action name is invalid [action name: {groupId}]"); + return; + } + + var exists = _groups.Get(groupId); + if (exists == null) + { + _logger.Warning($"Group id does not exist [group id: {exists}]"); + return; + } + + _logger.Debug($"Removing group [group id: {exists.Id}][group name: {exists.Name}][group priority: {exists.Priority}]"); + _groups.Remove(exists.Id); + _logger.Information($"Group has been updated [group id: {exists.Id}]"); + } + } +} \ No newline at end of file diff --git a/Hermes/Socket/Requests/UpdateGroupAck.cs b/Hermes/Socket/Requests/UpdateGroupAck.cs new file mode 100644 index 0000000..991028d --- /dev/null +++ b/Hermes/Socket/Requests/UpdateGroupAck.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using HermesSocketLibrary.Requests.Messages; +using Serilog; +using TwitchChatTTS.Chat.Groups; + +namespace TwitchChatTTS.Hermes.Socket.Requests +{ + public class UpdateGroupAck : IRequestAck + { + public string Name => "create_group"; + private readonly IChatterGroupManager _groups; + private readonly JsonSerializerOptions _options; + private readonly ILogger _logger; + + public UpdateGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger) + { + _groups = groups; + _options = options; + _logger = logger; + } + + public void Acknowledge(string requestId, string? json, IDictionary? requestData) + { + if (string.IsNullOrWhiteSpace(json)) + { + _logger.Warning($"Group JSON data is null."); + return; + } + + var group = JsonSerializer.Deserialize(json, _options); + if (group == null) + { + _logger.Warning($"Group data is null."); + return; + } + + var exists = _groups.Get(group.Id); + if (exists == null) + { + _logger.Warning($"Group id does not exist [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]"); + return; + } + + _logger.Debug($"Updating group [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]"); + _groups.Modify(group); + _logger.Information($"Group has been updated [group id: {group.Id}]"); + } + } +} \ No newline at end of file diff --git a/OBS/Socket/Handlers/RequestResponseHandler.cs b/OBS/Socket/Handlers/RequestResponseHandler.cs index 3574ddd..8e64260 100644 --- a/OBS/Socket/Handlers/RequestResponseHandler.cs +++ b/OBS/Socket/Handlers/RequestResponseHandler.cs @@ -68,7 +68,7 @@ namespace TwitchChatTTS.OBS.Socket.Handlers } _logger.Debug($"Found the scene item by name [scene: {sceneName}][source: {sourceName}][id: {sceneItemId}][obs request id: {message.RequestId}]."); - obs.AddSourceId(sourceName.ToString()!, (long)sceneItemId); + obs.AddSourceId(sourceName.ToString()!, long.Parse(sceneItemId.ToString()!)); requestData.ResponseValues = message.ResponseData; break;