2024-03-12 14:05:27 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.Seven.Socket.Data;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Seven.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class DispatchHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly EmoteDatabase _emotes;
|
|
|
|
private readonly object _lock = new object();
|
|
|
|
public int OperationCode { get; } = 0;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public DispatchHandler(ILogger logger, EmoteDatabase emotes)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger = logger;
|
|
|
|
_emotes = emotes;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-12 14:05:27 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not DispatchMessage message || message == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
ApplyChanges(message?.Body?.Pulled, cf => cf.OldValue, true);
|
|
|
|
ApplyChanges(message?.Body?.Pushed, cf => cf.Value, false);
|
|
|
|
ApplyChanges(message?.Body?.Removed, cf => cf.OldValue, true);
|
|
|
|
ApplyChanges(message?.Body?.Updated, cf => cf.OldValue, false, cf => cf.Value);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
private void ApplyChanges(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter, bool removing, Func<ChangeField, object>? updater = null)
|
|
|
|
{
|
|
|
|
if (fields == null || !fields.Any() || removing && updater != null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
foreach (var val in fields)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
var value = getter(val);
|
|
|
|
if (value == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
continue;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
var o = JsonSerializer.Deserialize<EmoteField>(value.ToString(), new JsonSerializerOptions()
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
});
|
2024-06-16 20:19:31 -04:00
|
|
|
if (o == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (removing)
|
|
|
|
{
|
|
|
|
RemoveEmoteById(o.Id);
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Information($"Removed 7tv emote [name: {o.Name}][id: {o.Id}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else if (updater != null)
|
|
|
|
{
|
|
|
|
RemoveEmoteById(o.Id);
|
|
|
|
var update = updater(val);
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
var u = JsonSerializer.Deserialize<EmoteField>(update.ToString(), new JsonSerializerOptions()
|
|
|
|
{
|
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
});
|
|
|
|
|
|
|
|
if (u != null)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_emotes.Add(u.Name, u.Id);
|
|
|
|
_logger.Information($"Updated 7tv emote [old name: {o.Name}][new name: {u.Name}][id: {u.Id}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Warning("Failed to update 7tv emote.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_emotes.Add(o.Name, o.Id);
|
|
|
|
_logger.Information($"Added 7tv emote [name: {o.Name}][id: {o.Id}]");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveEmoteById(string id)
|
|
|
|
{
|
|
|
|
string? key = null;
|
2024-06-24 18:11:36 -04:00
|
|
|
foreach (var e in _emotes.Emotes)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
if (e.Value == id)
|
|
|
|
{
|
|
|
|
key = e.Key;
|
|
|
|
break;
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
if (key != null)
|
2024-06-24 18:11:36 -04:00
|
|
|
_emotes.Remove(key);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|