Compare commits
No commits in common. "e3c78d96fa99e16bcd421ef2b8a2e44cdbfbb8ea" and "0fa0ddcc450a94c12b03db8f9f3f1678eb0d0f9e" have entirely different histories.
e3c78d96fa
...
0fa0ddcc45
@ -7,6 +7,5 @@ namespace HermesSocketServer.Models
|
||||
public string Id { get; set; }
|
||||
public User User { get; set; }
|
||||
public ChatterStore Chatters { get; set; }
|
||||
public PolicyStore Policies { get; set; }
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
namespace HermesSocketServer.Store
|
||||
{
|
||||
public class Policy
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string GroupId { get; set; }
|
||||
public string Path { get; set; }
|
||||
public int Usage { get; set; }
|
||||
public TimeSpan Span { get; set; }
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Services;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -8,8 +11,6 @@ namespace HermesSocketServer.Requests
|
||||
public class CreateTTSUser : IRequest
|
||||
{
|
||||
public string Name => "create_tts_user";
|
||||
|
||||
public string[] RequiredKeys => ["chatter", "voice"];
|
||||
private ChannelManager _channels;
|
||||
private Database _database;
|
||||
private readonly ServerConfiguration _configuration;
|
||||
@ -25,31 +26,40 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
if (long.TryParse(data["chatter"].ToString(), out long chatterId))
|
||||
data["chatter"] = chatterId;
|
||||
else
|
||||
return RequestResult.Failed("Invalid Twitch user id");
|
||||
return new RequestResult(false, "Invalid Twitch user id");
|
||||
|
||||
data["voice"] = data["voice"].ToString();
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
else
|
||||
return new RequestResult(false, "Invalid voice id");
|
||||
|
||||
data["user"] = sender;
|
||||
|
||||
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
|
||||
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
|
||||
return RequestResult.Failed("Voice is disabled on this channel.");
|
||||
return new RequestResult(false, "Voice is disabled on this channel.");
|
||||
|
||||
var channel = _channels.Get(sender);
|
||||
bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null);
|
||||
|
||||
channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
{
|
||||
UserId = sender,
|
||||
ChatterId = chatterId.ToString(),
|
||||
VoiceId = data["voice"].ToString()!
|
||||
});
|
||||
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]");
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
return RequestResult.Failed("Something went wrong when updating the cache.");
|
||||
return new RequestResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class CreateTTSVoice : IRequest
|
||||
{
|
||||
public string Name => "create_tts_voice";
|
||||
public string[] RequiredKeys => ["voice"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private ILogger _logger;
|
||||
private Random _random;
|
||||
@ -22,20 +23,27 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
data["voice"] = data["voice"].ToString()!;
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
else
|
||||
return new RequestResult(false, "Invalid voice name.");
|
||||
|
||||
string id = RandomString(25);
|
||||
|
||||
var result = _voices.Set(id, new Voice()
|
||||
_voices.Set(id, new Voice()
|
||||
{
|
||||
Id = id,
|
||||
Name = data["voice"].ToString()
|
||||
Name = data["voice"].ToString()!
|
||||
});
|
||||
|
||||
if (result) {
|
||||
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]");
|
||||
return RequestResult.Successful(id);
|
||||
}
|
||||
return RequestResult.Failed("Something went wrong when updating the cache.");
|
||||
|
||||
return new RequestResult(true, id);
|
||||
}
|
||||
|
||||
private string RandomString(int length)
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class DeleteTTSVoice : IRequest
|
||||
{
|
||||
public string Name => "delete_tts_voice";
|
||||
public string[] RequiredKeys => ["voice"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -19,9 +20,18 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
_voices.Remove(data!["voice"].ToString());
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
|
||||
_voices.Remove(data["voice"].ToString());
|
||||
_logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]");
|
||||
return RequestResult.Successful(null);
|
||||
return new RequestResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -6,7 +7,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetChatterIds : IRequest
|
||||
{
|
||||
public string Name => "get_chatter_ids";
|
||||
public string[] RequiredKeys => [];
|
||||
private Database _database;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -22,7 +22,7 @@ namespace HermesSocketServer.Requests
|
||||
string sql = $"SELECT id FROM \"Chatter\"";
|
||||
await _database.Execute(sql, (IDictionary<string, object>?) null, (r) => ids.Add(r.GetInt64(0)));
|
||||
_logger.Information($"Fetched all chatters for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(ids, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, ids, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -6,7 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetConnections : IRequest
|
||||
{
|
||||
public string Name => "get_connections";
|
||||
public string[] RequiredKeys => [];
|
||||
|
||||
private Database _database;
|
||||
private Serilog.ILogger _logger;
|
||||
|
||||
@ -21,8 +22,8 @@ namespace HermesSocketServer.Requests
|
||||
var temp = new Dictionary<string, object>() { { "user", sender } };
|
||||
|
||||
var connections = new List<Connection>();
|
||||
string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";
|
||||
await _database.Execute(sql, temp, sql =>
|
||||
string sql3 = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";
|
||||
await _database.Execute(sql3, temp, sql =>
|
||||
connections.Add(new Connection()
|
||||
{
|
||||
Name = sql.GetString(0),
|
||||
@ -35,7 +36,7 @@ namespace HermesSocketServer.Requests
|
||||
Default = sql.GetBoolean(7)
|
||||
})
|
||||
);
|
||||
return RequestResult.Successful(connections, false);
|
||||
return new RequestResult(true, connections, false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -6,7 +7,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetDefaultTTSVoice : IRequest
|
||||
{
|
||||
public string Name => "get_default_tts_voice";
|
||||
public string[] RequiredKeys => [];
|
||||
private readonly UserStore _users;
|
||||
private readonly ServerConfiguration _configuration;
|
||||
private readonly ILogger _logger;
|
||||
@ -22,9 +22,9 @@ namespace HermesSocketServer.Requests
|
||||
{
|
||||
var user = _users.Get(sender);
|
||||
if (user == null)
|
||||
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);
|
||||
return new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
|
||||
|
||||
return RequestResult.Successful(user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetEmotes : IRequest
|
||||
{
|
||||
public string Name => "get_emotes";
|
||||
public string[] RequiredKeys => [];
|
||||
private Database _database;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -27,7 +28,7 @@ namespace HermesSocketServer.Requests
|
||||
Name = r.GetString(1)
|
||||
}));
|
||||
_logger.Information($"Fetched all emotes for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(emotes, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, emotes, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -6,7 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetEnabledTTSVoices : IRequest
|
||||
{
|
||||
public string Name => "get_enabled_tts_voices";
|
||||
public string[] RequiredKeys => [];
|
||||
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -26,7 +27,7 @@ namespace HermesSocketServer.Requests
|
||||
+ "WHERE \"userId\" = @user AND state = true";
|
||||
await _database.Execute(sql, temp, (r) => voices.Add(r.GetString(0)));
|
||||
_logger.Information($"Fetched all enabled TTS voice for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(voices, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, voices, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,7 +8,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetPermissions : IRequest
|
||||
{
|
||||
public string Name => "get_permissions";
|
||||
public string[] RequiredKeys => [];
|
||||
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -55,7 +56,7 @@ namespace HermesSocketServer.Requests
|
||||
GroupChatters = groupChatters,
|
||||
GroupPermissions = groupPermissions
|
||||
};
|
||||
return RequestResult.Successful(info, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, info, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -8,7 +9,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetRedeemableActions : IRequest
|
||||
{
|
||||
public string Name => "get_redeemable_actions";
|
||||
public string[] RequiredKeys => [];
|
||||
|
||||
private readonly JsonSerializerOptions _options;
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
@ -33,7 +34,7 @@ namespace HermesSocketServer.Requests
|
||||
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(r.GetString(2), _options)!
|
||||
}));
|
||||
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, redemptions, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,7 +8,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetRedemptions : IRequest
|
||||
{
|
||||
public string Name => "get_redemptions";
|
||||
public string[] RequiredKeys => [];
|
||||
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -32,7 +33,7 @@ namespace HermesSocketServer.Requests
|
||||
State = r.GetBoolean(4)
|
||||
}));
|
||||
_logger.Information($"Fetched all redemptions for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, redemptions, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Services;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -6,7 +7,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetTTSUsers : IRequest
|
||||
{
|
||||
public string Name => "get_tts_users";
|
||||
public string[] RequiredKeys => [];
|
||||
private ChannelManager _channels;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -19,9 +19,12 @@ namespace HermesSocketServer.Requests
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
var channel = _channels.Get(sender);
|
||||
var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null, notifyClientsOnAccount: false);
|
||||
|
||||
var temp = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
|
||||
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(results, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, temp, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetTTSVoices : IRequest
|
||||
{
|
||||
public string Name => "get_tts_voices";
|
||||
public string[] RequiredKeys => [];
|
||||
private VoiceStore _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -26,7 +27,7 @@ namespace HermesSocketServer.Requests
|
||||
});
|
||||
|
||||
_logger.Information($"Fetched all TTS voices for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(voices, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, voices, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class GetTTSWordFilters : IRequest
|
||||
{
|
||||
public string Name => "get_tts_word_filters";
|
||||
public string[] RequiredKeys => [];
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -30,7 +31,7 @@ namespace HermesSocketServer.Requests
|
||||
Replace = r.GetString(2)
|
||||
}));
|
||||
_logger.Information($"Fetched all word filters for channel [channel: {sender}]");
|
||||
return RequestResult.Successful(filters, notifyClientsOnAccount: false);
|
||||
return new RequestResult(true, filters, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public interface IRequest
|
||||
{
|
||||
string Name { get; }
|
||||
string[] RequiredKeys { get; }
|
||||
|
||||
Task<RequestResult> Grant(string sender, IDictionary<string, object>? data);
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public interface IRequestManager
|
||||
{
|
||||
Task<RequestResult> Grant(string sender, RequestMessage? message);
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
using HermesSocketServer.Services;
|
||||
using Serilog;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class RequestManager : IRequestManager
|
||||
{
|
||||
private readonly IDictionary<string, IRequest> _requests;
|
||||
private readonly ChannelManager _channels;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
|
||||
|
||||
public RequestManager(IEnumerable<IRequest> requests, ChannelManager channels, Serilog.ILogger logger)
|
||||
{
|
||||
_requests = requests.ToDictionary(r => r.Name, r => r);
|
||||
_channels = channels;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, RequestMessage? message)
|
||||
{
|
||||
if (message == null || message.Type == null)
|
||||
return RequestResult.Failed("Request type does not exist.");
|
||||
|
||||
var channel = _channels.Get(sender);
|
||||
if (channel == null)
|
||||
return RequestResult.Failed("Channel does not exist.");
|
||||
|
||||
if (!_requests.TryGetValue(message.Type, out IRequest? request) || request == null)
|
||||
{
|
||||
_logger.Warning($"Could not find request [type: {message.Type}]");
|
||||
return RequestResult.Failed("Request does not exist.");
|
||||
}
|
||||
|
||||
if (request.RequiredKeys.Any())
|
||||
{
|
||||
if (message.Data == null)
|
||||
return RequestResult.Failed($"Request is lacking data entries.");
|
||||
|
||||
foreach (var key in request.RequiredKeys)
|
||||
{
|
||||
if (!message.Data.ContainsKey(key))
|
||||
return RequestResult.Failed($"Request is missing '{key}' in its data entries.");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return await request.Grant(sender, message.Data);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, $"Failed to grant a request during processing [type: {message.Type}]");
|
||||
return RequestResult.Failed("Failed to grant request during processing: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class RequestResult
|
||||
{
|
||||
public bool Success;
|
||||
public object? Result;
|
||||
public bool NotifyClientsOnAccount;
|
||||
|
||||
private RequestResult(bool success, object? result, bool notifyClientsOnAccount = true)
|
||||
{
|
||||
Success = success;
|
||||
Result = result;
|
||||
NotifyClientsOnAccount = notifyClientsOnAccount;
|
||||
}
|
||||
|
||||
public static RequestResult Successful(object? result, bool notifyClientsOnAccount = true)
|
||||
{
|
||||
return RequestResult.Successful(result, notifyClientsOnAccount);
|
||||
}
|
||||
|
||||
public static RequestResult Failed(string error, bool notifyClientsOnAccount = true)
|
||||
{
|
||||
return RequestResult.Successful(error, notifyClientsOnAccount);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -6,7 +7,6 @@ namespace HermesSocketServer.Requests
|
||||
public class UpdateDefaultTTSVoice : IRequest
|
||||
{
|
||||
public string Name => "update_default_tts_voice";
|
||||
public string[] RequiredKeys => ["user", "voice"];
|
||||
private UserStore _users;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -18,15 +18,21 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
data["user"] = data["user"].ToString();
|
||||
data["voice"] = data["voice"].ToString();
|
||||
|
||||
var success = _users.Modify(data["user"].ToString(), (user) => user.DefaultVoice = data["voice"].ToString()!);
|
||||
if (!success)
|
||||
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);
|
||||
return new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
|
||||
|
||||
_logger.Information($"Updated default TTS voice for channel [channel: {sender}][voice: {data["voice"]}]");
|
||||
return RequestResult.Successful(null);
|
||||
return new RequestResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
using System.Text.Json;
|
||||
using System.Threading.Channels;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Services;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -8,7 +12,7 @@ namespace HermesSocketServer.Requests
|
||||
public class UpdateTTSUser : IRequest
|
||||
{
|
||||
public string Name => "update_tts_user";
|
||||
public string[] RequiredKeys => ["chatter", "voice"];
|
||||
|
||||
private ChannelManager _channels;
|
||||
private Database _database;
|
||||
private readonly ServerConfiguration _configuration;
|
||||
@ -25,27 +29,34 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
if (long.TryParse(data["chatter"].ToString(), out long chatterId))
|
||||
data["chatter"] = chatterId;
|
||||
data["voice"] = data["voice"].ToString();
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
data["user"] = sender;
|
||||
|
||||
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
|
||||
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
|
||||
return RequestResult.Failed("Voice is either non-existent or disabled on this channel.");
|
||||
return new RequestResult(false, null);
|
||||
|
||||
var channel = _channels.Get(sender);
|
||||
var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null);
|
||||
|
||||
channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
{
|
||||
UserId = sender,
|
||||
ChatterId = chatterId.ToString(),
|
||||
VoiceId = data["voice"].ToString()!
|
||||
});
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]");
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
return RequestResult.Failed("Soemthing went wrong when updating the cache.");
|
||||
return new RequestResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -7,7 +9,6 @@ namespace HermesSocketServer.Requests
|
||||
public class UpdateTTSVoice : IRequest
|
||||
{
|
||||
public string Name => "update_tts_voice";
|
||||
public string[] RequiredKeys => ["voice", "voiceId"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -19,20 +20,24 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
data["voice"] = data["voice"].ToString();
|
||||
data["voiceid"] = data["voiceid"].ToString();
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
var result = _voices.Set(data["voiceid"].ToString(), new Voice()
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
if (data["voiceid"] is JsonElement id)
|
||||
data["voiceid"] = id.ToString();
|
||||
|
||||
_voices.Set(data["voiceid"].ToString(), new Voice()
|
||||
{
|
||||
Id = data["voiceid"].ToString()!,
|
||||
Name = data["voice"].ToString()!
|
||||
});
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]");
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
return RequestResult.Failed("Something went wrong when updating the cache.");
|
||||
return new RequestResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -6,7 +8,6 @@ namespace HermesSocketServer.Requests
|
||||
public class UpdateTTSVoiceState : IRequest
|
||||
{
|
||||
public string Name => "update_tts_voice_state";
|
||||
public string[] RequiredKeys => ["voice", "state"];
|
||||
private Database _database;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -18,16 +19,22 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
data["voice"] = data["voice"].ToString();
|
||||
data["state"] = data["state"].ToString() == "True";
|
||||
if (data == null)
|
||||
{
|
||||
_logger.Warning("Data received from request is null. Ignoring it.");
|
||||
return new RequestResult(false, null);
|
||||
}
|
||||
|
||||
if (data["voice"] is JsonElement voice)
|
||||
data["voice"] = voice.ToString();
|
||||
if (data["state"] is JsonElement state)
|
||||
data["state"] = state.ToString() == "True";
|
||||
data["user"] = sender;
|
||||
|
||||
string sql = "INSERT INTO \"TtsVoiceState\" (\"userId\", \"ttsVoiceId\", state) VALUES (@user, @voice, @state) ON CONFLICT (\"userId\", \"ttsVoiceId\") DO UPDATE SET state = @state";
|
||||
var result = await _database.Execute(sql, data);
|
||||
_logger.Information($"Updated voice's [voice id: {data["voice"]}] state [new state: {data["state"]}][channel: {data["user"]}]");
|
||||
if (result > 0)
|
||||
return RequestResult.Successful(null);
|
||||
return RequestResult.Failed("Something went wrong when updating the database.");
|
||||
return new RequestResult(result == 1, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ namespace HermesSocketServer.Services
|
||||
private readonly Database _database;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
private readonly IDictionary<string, Channel> _channels;
|
||||
private readonly object _lock;
|
||||
|
||||
public ChannelManager(UserStore users, Database database, Serilog.ILogger logger)
|
||||
{
|
||||
@ -19,46 +18,33 @@ namespace HermesSocketServer.Services
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
_channels = new ConcurrentDictionary<string, Channel>();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
|
||||
public async Task<Channel?> Add(string userId)
|
||||
public async Task Add(string userId)
|
||||
{
|
||||
var user = _users.Get(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
lock (_lock)
|
||||
{
|
||||
if (_channels.ContainsKey(userId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var chatters = new ChatterStore(userId, _database, _logger);
|
||||
var policies = new PolicyStore(userId, _database, _logger);
|
||||
await Task.WhenAll([
|
||||
chatters.Load(),
|
||||
policies.Load(),
|
||||
]);
|
||||
await chatters.Load();
|
||||
|
||||
var channel = new Channel()
|
||||
{
|
||||
Id = userId,
|
||||
User = user,
|
||||
Chatters = chatters,
|
||||
Policies = policies
|
||||
Chatters = chatters
|
||||
};
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_channels.Add(userId, channel);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
public Channel? Get(string channelId)
|
||||
{
|
||||
@ -66,28 +52,5 @@ namespace HermesSocketServer.Services
|
||||
return channel;
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task Save(string userId)
|
||||
{
|
||||
if (!_channels.TryGetValue(userId, out var channel))
|
||||
return;
|
||||
|
||||
await Task.WhenAll([
|
||||
channel.Chatters.Save(),
|
||||
channel.Policies.Save(),
|
||||
]);
|
||||
}
|
||||
|
||||
public async Task Save()
|
||||
{
|
||||
foreach (var channel in _channels.Values)
|
||||
{
|
||||
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
|
||||
await Task.WhenAll([
|
||||
channel.Chatters.Save(),
|
||||
channel.Policies.Save(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,15 +4,13 @@ namespace HermesSocketServer.Services
|
||||
{
|
||||
public class DatabaseService : BackgroundService
|
||||
{
|
||||
private readonly ChannelManager _channels;
|
||||
private readonly VoiceStore _voices;
|
||||
private readonly UserStore _users;
|
||||
private readonly ServerConfiguration _configuration;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
|
||||
public DatabaseService(ChannelManager channels, VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
|
||||
public DatabaseService(VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
|
||||
{
|
||||
_channels = channels;
|
||||
_voices = voices;
|
||||
_users = users;
|
||||
_configuration = configuration;
|
||||
@ -28,16 +26,14 @@ namespace HermesSocketServer.Services
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
|
||||
|
||||
while (true)
|
||||
{
|
||||
await Task.WhenAll([
|
||||
_voices.Save(),
|
||||
_users.Save(),
|
||||
_channels.Save(),
|
||||
Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)),
|
||||
]);
|
||||
tasks.Add(_voices.Save());
|
||||
tasks.Add(_users.Save());
|
||||
tasks.Add(Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)));
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -55,13 +55,10 @@ namespace HermesSocketServer.Socket.Handlers
|
||||
sender.WebLogin = data.WebLogin;
|
||||
}
|
||||
|
||||
await _manager.Add(userId);
|
||||
var channel = _manager.Get(userId);
|
||||
if (channel == null)
|
||||
{
|
||||
channel = await _manager.Add(userId);
|
||||
if (channel == null)
|
||||
return;
|
||||
}
|
||||
|
||||
sender.Name = channel.User.Name;
|
||||
sender.Admin = channel.User.Role == "ADMIN";
|
||||
|
@ -1,5 +1,5 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
using HermesSocketServer.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Socket.Handlers
|
||||
|
@ -69,7 +69,7 @@ namespace HermesSocketServer.Store
|
||||
_added.Clear();
|
||||
}
|
||||
|
||||
_logger.Debug($"TtsChatVoice - Adding {count} rows to database: {sql}");
|
||||
_logger.Debug($"ADD {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
@ -80,7 +80,7 @@ namespace HermesSocketServer.Store
|
||||
sql = _generator.GenerateUpdateSql("TtsChatVoice", _modified.Select(m => _store[m]), ["userId", "chatterId"], ["ttsVoiceId"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsChatVoice - Modifying {count} rows in database: {sql}");
|
||||
_logger.Debug($"MOD {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
@ -91,7 +91,7 @@ namespace HermesSocketServer.Store
|
||||
sql = _generator.GenerateDeleteSql("TtsChatVoice", _deleted, ["userId", "chatterId"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsChatVoice - Deleting {count} rows from database: {sql}");
|
||||
_logger.Debug($"DEL {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
return true;
|
||||
|
@ -1,105 +0,0 @@
|
||||
using HermesSocketLibrary.db;
|
||||
|
||||
namespace HermesSocketServer.Store
|
||||
{
|
||||
public class PolicyStore : GroupSaveStore<string, Policy>
|
||||
{
|
||||
private readonly string _userId;
|
||||
private readonly Database _database;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
private readonly GroupSaveSqlGenerator<Policy> _generator;
|
||||
|
||||
|
||||
public PolicyStore(string userId, Database database, Serilog.ILogger logger) : base(logger)
|
||||
{
|
||||
_userId = userId;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
|
||||
var ctp = new Dictionary<string, string>
|
||||
{
|
||||
{ "id", "Id" },
|
||||
{ "userId", "UserId" },
|
||||
{ "groupId", "GroupId" },
|
||||
{ "path", "Path" },
|
||||
{ "count", "Usage" },
|
||||
{ "timespan", "Span" },
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<Policy>(ctp);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
{
|
||||
var data = new Dictionary<string, object>() { { "user", _userId } };
|
||||
string sql = $"SELECT id, \"groupId\", path, count, timespan FROM \"GroupPermissionPolicy\" WHERE \"userId\" = @user";
|
||||
await _database.Execute(sql, data, (reader) =>
|
||||
{
|
||||
string id = reader.GetString(0).ToString();
|
||||
_store.Add(id, new Policy()
|
||||
{
|
||||
Id = id,
|
||||
UserId = _userId,
|
||||
GroupId = reader.GetString(1),
|
||||
Path = reader.GetString(2),
|
||||
Usage = reader.GetInt32(3),
|
||||
Span = TimeSpan.FromMilliseconds(reader.GetInt32(4)),
|
||||
});
|
||||
});
|
||||
_logger.Information($"Loaded {_store.Count} policies from database.");
|
||||
}
|
||||
|
||||
protected override void OnInitialAdd(string key, Policy value)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnInitialModify(string key, Policy value)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnInitialRemove(string key)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("GroupPermissionPolicy", _added.Select(a => _store[a]), ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
_added.Clear();
|
||||
}
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Adding {count} rows to database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("GroupPermissionPolicy", _modified.Select(m => _store[m]), ["id"], ["userId", "groupId", "path", "count", "timespan"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
_logger.Debug($"GroupPermissionPolicy - Modifying {count} rows in database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("GroupPermissionPolicy", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
_logger.Debug($"GroupPermissionPolicy - Deleting {count} rows from database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -58,40 +58,34 @@ namespace HermesSocketServer.Store
|
||||
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
string sql = string.Empty;
|
||||
lock (_lock)
|
||||
{
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("User", _added.Select(a => _store[a]), ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_added.Clear();
|
||||
}
|
||||
_logger.Debug($"User - Adding {count} rows to database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
string sql = string.Empty;
|
||||
lock (_lock)
|
||||
{
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("User", _modified.Select(m => _store[m]), ["id"], ["name", "email", "role", "ttsDefaultVoice"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
string sql = string.Empty;
|
||||
lock (_lock)
|
||||
{
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("User", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
return true;
|
||||
|
@ -72,7 +72,7 @@ namespace HermesSocketServer.Store
|
||||
_added.Clear();
|
||||
}
|
||||
|
||||
_logger.Debug($"TtsVoice - Adding {count} rows to database: {sql}");
|
||||
_logger.Debug($"ADD {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
@ -83,7 +83,7 @@ namespace HermesSocketServer.Store
|
||||
sql = _generator.GenerateUpdateSql("TtsVoice", _modified.Select(m => _store[m]), ["id"], ["name"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsVoice - Modifying {count} rows in database: {sql}");
|
||||
_logger.Debug($"MOD {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
@ -94,7 +94,7 @@ namespace HermesSocketServer.Store
|
||||
sql = _generator.GenerateDeleteSql("TtsVoice", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsVoice - Deleting {count} rows from database: {sql}");
|
||||
_logger.Debug($"DEL {count} " + sql);
|
||||
await _database.ExecuteScalar(sql);
|
||||
}
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user