Added group chatters support to websocket.

This commit is contained in:
Tom
2025-01-18 17:33:15 +00:00
parent a49e52a6bb
commit 9f884f71ae
6 changed files with 153 additions and 3 deletions

View File

@ -24,9 +24,12 @@ namespace TwitchChatTTS.Chat.Groups
_groups.Add(group.Name, group); _groups.Add(group.Name, group);
} }
public void Add(long chatter, string groupName) public void Add(long chatter, string groupId)
{ {
_chatters.Add(chatter, new List<string>() { groupName }); if (_chatters.TryGetValue(chatter, out var list))
list.Add(groupId);
else
_chatters.Add(chatter, new List<string>() { groupId });
} }
public void Add(long chatter, ICollection<string> groupNames) public void Add(long chatter, ICollection<string> groupNames)

View File

@ -21,7 +21,8 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData) public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{ {
if (string.IsNullOrWhiteSpace(json)) { if (string.IsNullOrWhiteSpace(json))
{
_logger.Warning($"Group JSON data is null."); _logger.Warning($"Group JSON data is null.");
return; return;
} }

View File

@ -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 CreateGroupChatterAck : IRequestAck
{
public string Name => "create_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public CreateGroupChatterAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json))
{
_logger.Warning($"Group Chatter JSON data is null.");
return;
}
var groupChatter = JsonSerializer.Deserialize<GroupChatter>(json, _options);
if (groupChatter == null)
{
_logger.Warning($"Group Chatter data is null.");
return;
}
var group = _groups.Get(groupChatter.GroupId);
if (group == null)
{
_logger.Warning($"Group id for chatter does not exists [group id: {groupChatter.GroupId}]");
return;
}
_logger.Debug($"Adding chatter to group [group id: {groupChatter.GroupId}][group name: {group.Name}][chatter id: {groupChatter.ChatterId}][chatter label: {groupChatter.ChatterLabel}]");
_groups.Add(groupChatter.ChatterId, groupChatter.GroupId);
_logger.Information($"Chatter has been added to group [group id: {groupChatter.GroupId}][group name: {group.Name}][chatter id: {groupChatter.ChatterId}][chatter label: {groupChatter.ChatterLabel}]");
}
}
}

View File

@ -0,0 +1,51 @@
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class DeleteGroupChatterAck : IRequestAck
{
public string Name => "delete_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly ILogger _logger;
public DeleteGroupChatterAck(IChatterGroupManager groups, ILogger logger)
{
_groups = groups;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
if (long.TryParse(requestData["chatter"].ToString(), out var chatterId))
{
_logger.Warning($"Chatter Id is invalid [chatter id: {chatterId}]");
return;
}
var groupId = requestData["group"].ToString();
if (string.IsNullOrWhiteSpace(groupId))
{
_logger.Warning($"Group Id is invalid [group id: {groupId}]");
return;
}
var group = _groups.Get(groupId);
if (group == null)
{
_logger.Warning($"Group id does not exist [group id: {groupId}]");
return;
}
_logger.Debug($"Deleting chatter from group [group id: {group.Id}][chatter id: {chatterId}][group name: {group.Name}][group priority: {group.Priority}]");
_groups.Remove(chatterId, groupId);
_logger.Information($"Chatter has been deleted from group [group id: {group.Id}][chatter id: {chatterId}]");
}
}
}

View File

@ -20,6 +20,12 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData) public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{ {
if (json == null)
{
_logger.Warning("Policy JSON data is null.");
return;
}
var data = json.Split('/'); var data = json.Split('/');
if (data.Length != 2) if (data.Length != 2)
{ {

View File

@ -0,0 +1,40 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class UpdateGroupChatterAck : IRequestAck
{
public string Name => "update_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public UpdateGroupChatterAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json)) {
_logger.Warning($"Group Chatter JSON data is null.");
return;
}
var groupChatter = JsonSerializer.Deserialize<GroupChatter>(json, _options);
if (groupChatter == null)
{
_logger.Warning($"Group Chatter data is null.");
return;
}
_groups.Add(groupChatter.ChatterId, groupChatter.GroupId);
_logger.Information($"Chatter has been updated [chatter label: {groupChatter.ChatterLabel}]");
}
}
}