Added missing websocket support for Redemptions and Actions. Fixed Ad Break actions. Cleaned some code.
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using System.Timers;
|
||||
using CommonSocketLibrary.Abstract;
|
||||
@ -9,6 +8,7 @@ using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
using TwitchChatTTS.Bus;
|
||||
using TwitchChatTTS.Hermes.Socket.Handlers;
|
||||
|
||||
namespace TwitchChatTTS.Hermes.Socket
|
||||
@ -19,6 +19,7 @@ namespace TwitchChatTTS.Hermes.Socket
|
||||
|
||||
private readonly User _user;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly ServiceBusCentral _bus;
|
||||
private readonly ICallbackManager<HermesRequestData> _callbackManager;
|
||||
private string URL;
|
||||
|
||||
@ -33,10 +34,13 @@ namespace TwitchChatTTS.Hermes.Socket
|
||||
public bool LoggedIn { get; set; }
|
||||
public bool Ready { get; set; }
|
||||
|
||||
private bool _attempting;
|
||||
|
||||
|
||||
public HermesSocketClient(
|
||||
User user,
|
||||
Configuration configuration,
|
||||
ServiceBusCentral bus,
|
||||
ICallbackManager<HermesRequestData> callbackManager,
|
||||
[FromKeyedServices("hermes")] IBackoff backoff,
|
||||
[FromKeyedServices("hermes")] IEnumerable<IWebSocketHandler> handlers,
|
||||
@ -50,6 +54,7 @@ namespace TwitchChatTTS.Hermes.Socket
|
||||
{
|
||||
_user = user;
|
||||
_configuration = configuration;
|
||||
_bus = bus;
|
||||
_callbackManager = callbackManager;
|
||||
_backoff = backoff;
|
||||
_heartbeatTimer = new System.Timers.Timer(TimeSpan.FromSeconds(15));
|
||||
@ -59,6 +64,20 @@ namespace TwitchChatTTS.Hermes.Socket
|
||||
URL = $"wss://{BASE_URL}";
|
||||
|
||||
_lock = new object();
|
||||
|
||||
var ttsCreateUserVoice = _bus.GetTopic("tts.user.voice.create");
|
||||
ttsCreateUserVoice.Subscribe(new ServiceBusObserver(async data => await Send(3, new RequestMessage()
|
||||
{
|
||||
Type = "create_tts_user",
|
||||
Data = (IDictionary<string, object>) data.Value!
|
||||
}), logger));
|
||||
|
||||
var ttsUpdateUserVoice = _bus.GetTopic("tts.user.voice.update");
|
||||
ttsUpdateUserVoice.Subscribe(new ServiceBusObserver(async data => await Send(3, new RequestMessage()
|
||||
{
|
||||
Type = "update_tts_user",
|
||||
Data = (IDictionary<string, object>) data.Value!
|
||||
}), logger));
|
||||
}
|
||||
|
||||
|
||||
@ -66,23 +85,29 @@ namespace TwitchChatTTS.Hermes.Socket
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (Connected)
|
||||
if (Connected || _attempting)
|
||||
return;
|
||||
|
||||
_attempting = true;
|
||||
}
|
||||
|
||||
_logger.Debug($"Attempting to connect to {URL}");
|
||||
await ConnectAsync(URL);
|
||||
_attempting = false;
|
||||
}
|
||||
|
||||
private async Task Disconnect()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!Connected)
|
||||
if (!Connected || _attempting)
|
||||
return;
|
||||
|
||||
_attempting = true;
|
||||
}
|
||||
|
||||
await DisconnectAsync(new SocketDisconnectionEventArgs("Normal disconnection", "Disconnection was executed"));
|
||||
_attempting = false;
|
||||
}
|
||||
|
||||
public async Task CreateTTSVoice(string voiceName)
|
||||
|
35
Hermes/Socket/Requests/CreateRedemptionAck.cs
Normal file
35
Hermes/Socket/Requests/CreateRedemptionAck.cs
Normal 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}]");
|
||||
}
|
||||
}
|
||||
}
|
@ -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"]}]");
|
||||
|
@ -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))
|
||||
|
39
Hermes/Socket/Requests/DeleteRedeemableActionAck.cs
Normal file
39
Hermes/Socket/Requests/DeleteRedeemableActionAck.cs
Normal 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}]");
|
||||
}
|
||||
}
|
||||
}
|
39
Hermes/Socket/Requests/DeleteRedemptionAck.cs
Normal file
39
Hermes/Socket/Requests/DeleteRedemptionAck.cs
Normal 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}]");
|
||||
}
|
||||
}
|
||||
}
|
@ -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))
|
||||
{
|
||||
|
@ -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))
|
||||
{
|
||||
|
@ -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)
|
||||
});
|
||||
|
@ -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()}]");
|
||||
}
|
||||
|
@ -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))
|
||||
{
|
||||
|
@ -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}]");
|
||||
}
|
||||
}
|
||||
}
|
37
Hermes/Socket/Requests/UpdateRedeemptionAck.cs
Normal file
37
Hermes/Socket/Requests/UpdateRedeemptionAck.cs
Normal 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}]");
|
||||
}
|
||||
}
|
||||
}
|
@ -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}]");
|
||||
|
Reference in New Issue
Block a user