Cleaned up request acks. Added internal service bus for internal messaging.
This commit is contained in:
52
Hermes/Socket/Requests/GetConnectionsAck.cs
Normal file
52
Hermes/Socket/Requests/GetConnectionsAck.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
using Serilog;
|
||||
|
||||
namespace TwitchChatTTS.Hermes.Socket.Requests
|
||||
{
|
||||
public class GetConnectionsAck : IRequestAck
|
||||
{
|
||||
public string Name => "get_connections";
|
||||
private readonly TwitchApiClient _twitch;
|
||||
private readonly NightbotApiClient _nightbot;
|
||||
private readonly User _user;
|
||||
private readonly JsonSerializerOptions _options;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public GetConnectionsAck
|
||||
(
|
||||
TwitchApiClient twitch,
|
||||
NightbotApiClient nightbot,
|
||||
User user,
|
||||
JsonSerializerOptions options,
|
||||
ILogger logger
|
||||
)
|
||||
{
|
||||
_twitch = twitch;
|
||||
_nightbot = nightbot;
|
||||
_user = user;
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
|
||||
{
|
||||
var connections = JsonSerializer.Deserialize<IEnumerable<Connection>>(json, _options);
|
||||
if (connections == null)
|
||||
{
|
||||
_logger.Error("Null value was given when attempting to fetch connections.");
|
||||
return;
|
||||
}
|
||||
|
||||
_user.TwitchConnection = connections.FirstOrDefault(c => c.Type == "twitch" && c.Default);
|
||||
_user.NightbotConnection = connections.FirstOrDefault(c => c.Type == "nightbot" && c.Default);
|
||||
|
||||
_logger.Information($"Fetched connections from TTS account [count: {connections.Count()}][twitch: {_user.TwitchConnection != null}][nightbot: {_user.NightbotConnection != null}]");
|
||||
|
||||
if (_user.TwitchConnection != null)
|
||||
_twitch.Initialize(_user.TwitchConnection.ClientId, _user.TwitchConnection.AccessToken);
|
||||
if (_user.NightbotConnection != null)
|
||||
_nightbot.Initialize(_user.NightbotConnection.ClientId, _user.NightbotConnection.AccessToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user