Redid stores. Added user store. Added Channel Manager, to manage data from a single channel.

This commit is contained in:
Tom
2024-10-19 01:50:46 +00:00
parent 3f3ba63554
commit 4d0b38babd
22 changed files with 654 additions and 475 deletions

View File

@ -1,6 +1,8 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketLibrary.Socket.Data;
using HermesSocketServer.Services;
using HermesSocketServer.Store;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Socket.Handlers
@ -9,14 +11,18 @@ namespace HermesSocketServer.Socket.Handlers
{
public int OperationCode { get; } = 1;
private readonly ChannelManager _manager;
private readonly VoiceStore _voices;
private readonly ServerConfiguration _configuration;
private readonly Database _database;
private readonly HermesSocketManager _sockets;
private readonly ILogger _logger;
private readonly object _lock;
public HermesLoginHandler(ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
public HermesLoginHandler(ChannelManager manager, VoiceStore voices, ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
{
_manager = manager;
_voices = voices;
_configuration = configuration;
_database = database;
_sockets = sockets;
@ -49,30 +55,31 @@ namespace HermesSocketServer.Socket.Handlers
sender.WebLogin = data.WebLogin;
}
var userIdDict = new Dictionary<string, object>() { { "user", userId } };
string? ttsDefaultVoice = null;
string sql2 = "select name, role, \"ttsDefaultVoice\" from \"User\" where id = @user";
await _database.Execute(sql2, userIdDict, sql =>
{
sender.Name = sql.GetString(0);
sender.Admin = sql.GetString(1) == "ADMIN";
ttsDefaultVoice = sql.GetString(2);
});
await _manager.Add(userId);
var channel = _manager.Get(userId);
if (channel == null)
return;
sender.Name = channel.User.Name;
sender.Admin = channel.User.Role == "ADMIN";
if (string.IsNullOrEmpty(sender.Name))
{
_logger.Error($"Could not find username using the user id [user id: {userId}][api key: {data.ApiKey}]");
_logger.Error($"Could not find username for a certain user [user id: {userId}][api key: {data.ApiKey}]");
return;
}
if (string.IsNullOrEmpty(channel.User.DefaultVoice))
_logger.Warning($"No default voice was set for an user [user id: {userId}][api key: {data.ApiKey}]");
var ack = new LoginAckMessage()
{
UserId = userId,
OwnerId = _configuration.OwnerId,
OwnerId = _configuration.Tts.OwnerId,
Admin = sender.Admin,
WebLogin = data.WebLogin,
};
var userIdDict = new Dictionary<string, object>() { { "user", userId } };
ack.Connections = new List<Connection>();
string sql3 = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";
await _database.Execute(sql3, userIdDict, sql =>
@ -89,9 +96,7 @@ namespace HermesSocketServer.Socket.Handlers
})
);
ack.TTSVoicesAvailable = new Dictionary<string, string>();
string sql4 = "SELECT id, name FROM \"TtsVoice\"";
await _database.Execute(sql4, (IDictionary<string, object>?) null, (r) => ack.TTSVoicesAvailable.Add(r.GetString(0), r.GetString(1)));
ack.TTSVoicesAvailable = _voices.Get().ToDictionary(v => v.Key, v => v.Value.Name);
ack.EnabledTTSVoices = new List<string>();
string sql5 = $"SELECT v.name FROM \"TtsVoiceState\" s "
@ -108,8 +113,7 @@ namespace HermesSocketServer.Socket.Handlers
Replace = r.GetString(2)
}));
if (ttsDefaultVoice != null)
ack.DefaultTTSVoice = ttsDefaultVoice;
ack.DefaultTTSVoice = channel.User.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice;
await sender.Send(2, ack);
@ -120,7 +124,7 @@ namespace HermesSocketServer.Socket.Handlers
{
AnotherClient = true,
UserId = userId,
OwnerId = _configuration.OwnerId,
OwnerId = _configuration.Tts.OwnerId,
WebLogin = data.WebLogin
};