Added several request messages. Added redemptions. Added life TTS voice changes. A bunch of stuffs.

This commit is contained in:
Tom
2024-08-10 19:36:32 +00:00
parent 95bc073a73
commit c771a56971
36 changed files with 552 additions and 235 deletions

View File

@@ -1,4 +1,5 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketLibrary.Socket.Data;
using ILogger = Serilog.ILogger;
@@ -6,15 +7,17 @@ namespace HermesSocketServer.Socket.Handlers
{
public class HermesLoginHandler : ISocketHandler
{
public int OpCode { get; } = 1;
public int OperationCode { get; } = 1;
private readonly ServerConfiguration _configuration;
private readonly Database _database;
private readonly HermesSocketManager _sockets;
private readonly ILogger _logger;
private readonly object _lock;
public HermesLoginHandler(Database database, HermesSocketManager sockets, ILogger logger)
public HermesLoginHandler(ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
{
_configuration = configuration;
_database = database;
_sockets = sockets;
_logger = logger;
@@ -36,48 +39,89 @@ namespace HermesSocketServer.Socket.Handlers
if (userId == null)
return;
var recipients = _sockets.GetSockets(userId).ToList();
lock (_lock)
{
if (sender.Id != null)
return;
sender.Id = userId;
sender.ApiKey = data.ApiKey;
sender.WebLogin = data.WebLogin;
}
string sql2 = "select \"name\" from \"User\" where id = @user";
var result2 = await _database.ExecuteScalar(sql2, new Dictionary<string, object>() { { "user", userId } });
string? name = result2?.ToString();
if (string.IsNullOrEmpty(name))
return;
sender.Name = name;
await sender.Send(2, new LoginAckMessage()
string sql2 = "select name, role from \"User\" where id = @user";
await _database.Execute(sql2, new Dictionary<string, object>() { { "user", userId } }, sql =>
{
UserId = userId
sender.Name = sql.GetString(0);
sender.Admin = sql.GetString(1) == "ADMIN";
});
if (string.IsNullOrEmpty(sender.Name))
{
_logger.Error($"Could not find username using the user id [user id: {userId}][api key: {data.ApiKey}]");
return;
}
var ack = new LoginAckMessage()
{
AnotherClient = true,
UserId = userId
UserId = userId,
OwnerId = _configuration.OwnerId,
Admin = sender.Admin,
WebLogin = data.WebLogin,
};
var connections = new List<Connection>();
string sql3 = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";
await _database.Execute(sql3, new Dictionary<string, object>() { { "user", userId } }, sql =>
connections.Add(new Connection()
{
Name = sql.GetString(0),
Type = sql.GetString(1),
ClientId = sql.GetString(2),
AccessToken = sql.GetString(3),
GrantType = sql.GetString(4),
Scope = sql.GetString(5),
ExpiresIn = sql.GetDateTime(6),
Default = sql.GetBoolean(7)
})
);
ack.Connections = connections.ToArray();
IList<VoiceDetails> voices = new List<VoiceDetails>();
string sql4 = "SELECT id, name FROM \"TtsVoice\"";
await _database.Execute(sql4, (IDictionary<string, object>?) null, (r) => voices.Add(new VoiceDetails()
{
Id = r.GetString(0),
Name = r.GetString(1)
}));
ack.TTSVoicesAvailable = voices.ToDictionary(e => e.Id, e => e.Name);
await sender.Send(2, ack);
string version = data.MajorVersion == null ? "unknown" : $"{data.MajorVersion}.{data.MinorVersion}";
_logger.Information($"Hermes client logged in {(sender.Admin ? "as administrator " : "")}[name: {sender.Name}][id: {userId}][ip: {sender.IPAddress}][version: {version}][web: {data.WebLogin}]");
ack = new LoginAckMessage()
{
AnotherClient = true,
UserId = userId,
OwnerId = _configuration.OwnerId,
WebLogin = data.WebLogin
};
var recipients = _sockets.GetSockets(userId).ToList().Where(s => s.UID != sender.UID);
var tasks = new List<Task>();
foreach (var socket in recipients)
{
try
{
await socket.Send(2, ack);
tasks.Add(socket.Send(2, ack));
}
catch (Exception)
{
}
}
_logger.Information($"Hermes client logged in [name: {name}][id: {userId}][ip: {sender.IPAddress}]");
await Task.WhenAll(tasks);
}
}
}