hermes-client/Hermes/Socket/Requests/GetConnectionsAck.cs

52 lines
2.0 KiB
C#
Raw Permalink Normal View History

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);
}
}
}