Added missing websocket support for Redemptions and Actions. Fixed Ad Break actions. Cleaned some code.

This commit is contained in:
Tom
2025-01-07 15:30:13 +00:00
parent 77b37f04b6
commit 64cb0c1f6d
17 changed files with 227 additions and 36 deletions

View File

@ -0,0 +1,35 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Twitch.Redemptions;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class CreateRedemptionAck : IRequestAck
{
public string Name => "create_redemption";
private readonly IRedemptionManager _redemptions;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public CreateRedemptionAck(IRedemptionManager redemptions, JsonSerializerOptions options, ILogger logger)
{
_redemptions = redemptions;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var redemption = JsonSerializer.Deserialize<Redemption>(json, _options);
if (redemption == null)
{
_logger.Warning($"Redemption data received is null.");
return;
}
_redemptions.Add(redemption);
_logger.Information($"A new redemption has been created [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]");
}
}
}

View File

@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger.Warning("Request data is null.");
return;
}
if (!long.TryParse(requestData["chatter"].ToString(), out long chatterId))
{
_logger.Warning($"Failed to parse chatter id [chatter id: {requestData["chatter"]}]");

View File

@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger.Warning("Request data is null.");
return;
}
var voice = requestData["voice"].ToString()!;
var voiceId = json;
if (string.IsNullOrEmpty(voice))

View File

@ -0,0 +1,39 @@
using Serilog;
using TwitchChatTTS.Twitch.Redemptions;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class DeleteRedeemableActionAck : IRequestAck
{
public string Name => "delete_redeemable_action";
private readonly IRedemptionManager _redemptions;
private readonly ILogger _logger;
public DeleteRedeemableActionAck(IRedemptionManager redemptions, ILogger logger)
{
_redemptions = redemptions;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
var name = requestData["name"].ToString();
if (string.IsNullOrEmpty(name))
{
_logger.Warning($"Action name is invalid [action name: {name}]");
return;
}
if (_redemptions.RemoveAction(name))
_logger.Information($"Deleted a redeemable action [action name: {name}]");
else
_logger.Warning($"Failed to delete a redeemable action [action name: {name}]");
}
}
}

View File

@ -0,0 +1,39 @@
using Serilog;
using TwitchChatTTS.Twitch.Redemptions;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class DeleteRedemptionAck : IRequestAck
{
public string Name => "delete_redemption";
private readonly IRedemptionManager _redemptions;
private readonly ILogger _logger;
public DeleteRedemptionAck(IRedemptionManager redemptions, ILogger logger)
{
_redemptions = redemptions;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
var id = requestData["id"].ToString();
if (string.IsNullOrEmpty(id))
{
_logger.Warning($"Redemption Id is invalid [redemption id: {id}]");
return;
}
if (_redemptions.RemoveRedemption(id))
_logger.Information($"Deleted a redemption [redemption id: {id}]");
else
_logger.Warning($"Failed to delete a redemption [redemption id: {id}]");
}
}
}

View File

@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger.Warning("Request data is null.");
return;
}
var id = requestData["id"].ToString();
if (string.IsNullOrEmpty(id))
{

View File

@ -21,7 +21,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger.Warning("Request data is null.");
return;
}
var voice = requestData["voice"].ToString();
if (string.IsNullOrEmpty(voice))
{

View File

@ -46,7 +46,8 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
}
_logger.Information($"Redeemable actions loaded [count: {actions.Count()}]");
_bus.Send(this, "redemptions_initiation", new RedemptionInitiation() {
_bus.Send(this, "redemptions_initiation", new RedemptionInitiation()
{
Redemptions = redemptions,
Actions = actions.ToDictionary(a => a.Name, a => a)
});

View File

@ -27,7 +27,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
var temp = new ConcurrentDictionary<long, string>();
foreach (var entry in users)
temp.TryAdd(entry.Key, entry.Value);
_user.VoicesSelected = temp;
_logger.Information($"Updated chatters' selected voice [count: {temp.Count()}]");
}

View File

@ -13,7 +13,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger = logger;
}
public void Fulfill(string type, string requestId, string? data, IDictionary<string, object>? requestData)
public void Fulfill(string type, string requestId, string data, IDictionary<string, object>? requestData)
{
if (!_acknowledgements.TryGetValue(type, out var ack))
{

View File

@ -28,8 +28,10 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
return;
}
_redemptions.Update(action);
_logger.Information($"A new redeemable action has been created [action name: {action.Name}]");
if (_redemptions.Update(action))
_logger.Information($"A redeemable action has been updated [action name: {action.Name}]");
else
_logger.Warning($"Failed to update an existing redeemable action [action name: {action.Name}]");
}
}
}

View File

@ -0,0 +1,37 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Twitch.Redemptions;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class UpdateRedemptionAck : IRequestAck
{
public string Name => "update_redemption";
private readonly IRedemptionManager _redemptions;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public UpdateRedemptionAck(IRedemptionManager redemptions, JsonSerializerOptions options, ILogger logger)
{
_redemptions = redemptions;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var redemption = JsonSerializer.Deserialize<Redemption>(json, _options);
if (redemption == null)
{
_logger.Warning($"Redemption data received is null.");
return;
}
if (_redemptions.Update(redemption))
_logger.Information($"A redemption has been updated [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]");
else
_logger.Warning($"Failed to update an existing redemption [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]");
}
}
}

View File

@ -26,14 +26,15 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
_logger.Warning($"TTS Filter data failed: null");
return;
}
_logger.Debug($"Filter data [filter id: {filter.Id}][search: {filter.Search}][group id: {filter.Replace}]");
var previous = _user.RegexFilters.FirstOrDefault(f => f.Id == filter.Id);
if (previous == null) {
if (previous == null)
{
_logger.Warning($"TTS Filter doest exist by id [filter id: {filter.Id}]");
return;
}
previous.Search = filter.Search;
previous.Replace = filter.Replace;
_logger.Information($"Filter has been updated [filter id: {filter.Id}]");