Added groups & permissions. Fixed TTS user creation. Better connection handling. Fixed 7tv reconnection.
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
namespace TwitchChatTTS.Seven
|
||||
{
|
||||
public class ChatterDatabase
|
||||
{
|
||||
private readonly IDictionary<string, long> _chatters;
|
||||
//private readonly HashSet<long> _chatterIds;
|
||||
|
||||
|
||||
public ChatterDatabase()
|
||||
{
|
||||
_chatters = new Dictionary<string, long>();
|
||||
//_chatterIds = new HashSet<long>();
|
||||
}
|
||||
|
||||
public void Add(string username, long chatterId)
|
||||
{
|
||||
// if (_chatterIds.TryGetValue(chatterId, out var _)) {
|
||||
// // TODO: send message to update username for id.
|
||||
// } else
|
||||
// _chatterIds.Add(chatterId);
|
||||
|
||||
if (_chatters.ContainsKey(username))
|
||||
_chatters[username] = chatterId;
|
||||
else
|
||||
_chatters.Add(username, chatterId);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_chatters.Clear();
|
||||
}
|
||||
|
||||
public long? Get(string emoteName)
|
||||
{
|
||||
return _chatters.TryGetValue(emoteName, out var chatterId) ? chatterId : null;
|
||||
}
|
||||
|
||||
public void Remove(string emoteName)
|
||||
{
|
||||
_chatters.Remove(emoteName);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,55 +0,0 @@
|
||||
namespace TwitchChatTTS.Seven
|
||||
{
|
||||
public class EmoteDatabase
|
||||
{
|
||||
private readonly IDictionary<string, string> _emotes;
|
||||
public IDictionary<string, string> Emotes { get => _emotes.AsReadOnly(); }
|
||||
|
||||
public EmoteDatabase()
|
||||
{
|
||||
_emotes = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public void Add(string emoteName, string emoteId)
|
||||
{
|
||||
if (_emotes.ContainsKey(emoteName))
|
||||
_emotes[emoteName] = emoteId;
|
||||
else
|
||||
_emotes.Add(emoteName, emoteId);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_emotes.Clear();
|
||||
}
|
||||
|
||||
public string? Get(string emoteName)
|
||||
{
|
||||
return _emotes.TryGetValue(emoteName, out string? emoteId) ? emoteId : null;
|
||||
}
|
||||
|
||||
public void Remove(string emoteName)
|
||||
{
|
||||
_emotes.Remove(emoteName);
|
||||
}
|
||||
}
|
||||
|
||||
public class EmoteSet
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int Flags { get; set; }
|
||||
public bool Immutable { get; set; }
|
||||
public bool Privileged { get; set; }
|
||||
public IList<Emote> Emotes { get; set; }
|
||||
public int EmoteCount { get; set; }
|
||||
public int Capacity { get; set; }
|
||||
}
|
||||
|
||||
public class Emote
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int Flags { get; set; }
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@ using System.Text.Json;
|
||||
using TwitchChatTTS.Helpers;
|
||||
using Serilog;
|
||||
using TwitchChatTTS.Seven;
|
||||
using TwitchChatTTS.Chat.Emotes;
|
||||
|
||||
public class SevenApiClient
|
||||
{
|
||||
|
57
Seven/SevenManager.cs
Normal file
57
Seven/SevenManager.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket
|
||||
{
|
||||
public class SevenManager
|
||||
{
|
||||
private readonly User _user;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger _logger;
|
||||
private string URL;
|
||||
|
||||
public bool Connected { get; set; }
|
||||
public bool Streaming { get; set; }
|
||||
|
||||
|
||||
public SevenManager(User user, IServiceProvider serviceProvider, ILogger logger)
|
||||
{
|
||||
_user = user;
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Initialize() {
|
||||
_logger.Information("Initializing 7tv websocket client.");
|
||||
var client = _serviceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("7tv");
|
||||
|
||||
client.OnConnected += (sender, e) => {
|
||||
Connected = true;
|
||||
_logger.Information("7tv websocket client connected.");
|
||||
};
|
||||
|
||||
client.OnDisconnected += (sender, e) => {
|
||||
Connected = false;
|
||||
_logger.Information("7tv websocket client disconnected.");
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(_user.SevenEmoteSetId))
|
||||
URL = $"{SevenApiClient.WEBSOCKET_URL}@emote_set.*<object_id={_user.SevenEmoteSetId}>";
|
||||
}
|
||||
|
||||
public async Task Connect()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_user.SevenEmoteSetId))
|
||||
{
|
||||
_logger.Warning("Cannot find 7tv data for your channel. Not connecting to 7tv websockets.");
|
||||
return;
|
||||
}
|
||||
|
||||
var client = _serviceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("7tv");
|
||||
_logger.Debug($"7tv client attempting to connect to {URL}");
|
||||
await client.ConnectAsync($"{URL}");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
namespace TwitchChatTTS.Seven.Socket.Data
|
||||
{
|
||||
public class IdentifyMessage
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@ using System.Text.Json;
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Serilog;
|
||||
using TwitchChatTTS.Chat.Emotes;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
@@ -9,14 +10,14 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
public class DispatchHandler : IWebSocketHandler
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly EmoteDatabase _emotes;
|
||||
private readonly IEmoteDatabase _emotes;
|
||||
private readonly object _lock = new object();
|
||||
public int OperationCode { get; } = 0;
|
||||
|
||||
public DispatchHandler(ILogger logger, EmoteDatabase emotes)
|
||||
public DispatchHandler(IEmoteDatabase emotes, ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_emotes = emotes;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
||||
@@ -53,12 +54,20 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
if (removing)
|
||||
{
|
||||
RemoveEmoteById(o.Id);
|
||||
if (_emotes.Get(o.Name) != o.Id) {
|
||||
_logger.Warning("Mismatched emote found while removing a 7tv emote.");
|
||||
continue;
|
||||
}
|
||||
_emotes.Remove(o.Name);
|
||||
_logger.Information($"Removed 7tv emote [name: {o.Name}][id: {o.Id}]");
|
||||
}
|
||||
else if (updater != null)
|
||||
{
|
||||
RemoveEmoteById(o.Id);
|
||||
if (_emotes.Get(o.Name) != o.Id) {
|
||||
_logger.Warning("Mismatched emote found while updating a 7tv emote.");
|
||||
continue;
|
||||
}
|
||||
_emotes.Remove(o.Name);
|
||||
var update = updater(val);
|
||||
|
||||
var u = JsonSerializer.Deserialize<EmoteField>(update.ToString(), new JsonSerializerOptions()
|
||||
@@ -85,20 +94,5 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveEmoteById(string id)
|
||||
{
|
||||
string? key = null;
|
||||
foreach (var e in _emotes.Emotes)
|
||||
{
|
||||
if (e.Value == id)
|
||||
{
|
||||
key = e.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (key != null)
|
||||
_emotes.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
@@ -41,9 +41,9 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
];
|
||||
_reconnectDelay = [
|
||||
1000,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
0,
|
||||
3000,
|
||||
1000,
|
||||
@@ -77,19 +77,17 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_user.SevenEmoteSetId))
|
||||
{
|
||||
_logger.Warning("Connected to 7tv websocket previously, but no emote set id was set.");
|
||||
_logger.Warning("Could not find the 7tv emote set id. Not reconnecting.");
|
||||
return;
|
||||
}
|
||||
|
||||
var context = _serviceProvider.GetRequiredService<ReconnectContext>();
|
||||
if (_reconnectDelay[code] > 0)
|
||||
await Task.Delay(_reconnectDelay[code]);
|
||||
|
||||
var manager = _serviceProvider.GetRequiredService<SevenManager>();
|
||||
await manager.Connect();
|
||||
|
||||
var base_url = $"@emote_set.*<object_id={_user.SevenEmoteSetId}>";
|
||||
string url = $"{SevenApiClient.WEBSOCKET_URL}{base_url}";
|
||||
_logger.Debug($"7tv websocket reconnecting to {url}.");
|
||||
|
||||
await sender.ConnectAsync(url);
|
||||
if (context.SessionId != null)
|
||||
{
|
||||
await sender.Send(34, new ResumeMessage() { SessionId = context.SessionId });
|
||||
|
@@ -23,9 +23,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
if (sender is not SevenSocketClient seven || seven == null)
|
||||
return;
|
||||
|
||||
seven.Connected = true;
|
||||
seven.ConnectionDetails = message;
|
||||
_logger.Information("Connected to 7tv websockets.");
|
||||
_logger.Debug("Received hello handshake ack.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
using TwitchChatTTS.Chat.Emotes;
|
||||
|
||||
namespace TwitchChatTTS.Seven
|
||||
{
|
||||
public class UserDetails
|
||||
|
Reference in New Issue
Block a user