2024-03-12 14:05:27 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
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
|
|
|
|
{
|
|
|
|
private ILogger Logger { get; }
|
2024-03-15 08:27:35 -04:00
|
|
|
private EmoteDatabase Emotes { get; }
|
2024-06-16 20:19:31 -04:00
|
|
|
private object _lock = new object();
|
2024-03-12 14:05:27 -04:00
|
|
|
public int OperationCode { get; set; } = 0;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public DispatchHandler(ILogger logger, EmoteDatabase emotes)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
Logger = logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
Emotes = emotes;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not DispatchMessage obj || obj == null)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
ApplyChanges(obj?.Body?.Pulled, cf => cf.OldValue, true);
|
|
|
|
ApplyChanges(obj?.Body?.Pushed, cf => cf.Value, false);
|
2024-06-16 20:19:31 -04:00
|
|
|
ApplyChanges(obj?.Body?.Removed, cf => cf.OldValue, true);
|
|
|
|
ApplyChanges(obj?.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);
|
|
|
|
Logger.Information($"Removed 7tv emote: {o.Name} (id: {o.Id})");
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Emotes.Add(u.Name, u.Id);
|
|
|
|
Logger.Information($"Updated 7tv emote: from '{o.Name}' to '{u.Name}' (id: {u.Id})");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Warning("Failed to update 7tv emote.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Emotes.Add(o.Name, o.Id);
|
|
|
|
Logger.Information($"Added 7tv emote: {o.Name} (id: {o.Id})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveEmoteById(string id)
|
|
|
|
{
|
|
|
|
string? key = null;
|
|
|
|
foreach (var e in Emotes.Emotes)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
Emotes.Remove(key);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|