Moved Requests classes to here. Added some checks to requests.
This commit is contained in:
parent
a9cdb65895
commit
e3c78d96fa
@ -1,9 +1,6 @@
|
||||
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
|
||||
@ -11,6 +8,8 @@ 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;
|
||||
@ -26,40 +25,31 @@ 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 new RequestResult(false, "Invalid Twitch user id");
|
||||
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
else
|
||||
return new RequestResult(false, "Invalid voice id");
|
||||
|
||||
data["user"] = sender;
|
||||
return RequestResult.Failed("Invalid Twitch user id");
|
||||
|
||||
data["voice"] = data["voice"].ToString();
|
||||
|
||||
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 new RequestResult(false, "Voice is disabled on this channel.");
|
||||
return RequestResult.Failed("Voice is disabled on this channel.");
|
||||
|
||||
var channel = _channels.Get(sender);
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null);
|
||||
|
||||
channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
{
|
||||
UserId = sender,
|
||||
ChatterId = chatterId.ToString(),
|
||||
VoiceId = data["voice"].ToString()!
|
||||
});
|
||||
_logger.Information($"Selected a tts voice [voice: {data["voice"]}] for user [chatter: {data["chatter"]}] in channel [channel: {data["user"]}]");
|
||||
return new RequestResult(true, null);
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -9,6 +7,7 @@ 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;
|
||||
@ -23,27 +22,20 @@ 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 (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
else
|
||||
return new RequestResult(false, "Invalid voice name.");
|
||||
|
||||
data["voice"] = data["voice"].ToString()!;
|
||||
string id = RandomString(25);
|
||||
|
||||
_voices.Set(id, new Voice()
|
||||
var result = _voices.Set(id, new Voice()
|
||||
{
|
||||
Id = id,
|
||||
Name = data["voice"].ToString()!
|
||||
Name = data["voice"].ToString()
|
||||
});
|
||||
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]");
|
||||
|
||||
return new RequestResult(true, id);
|
||||
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.");
|
||||
}
|
||||
|
||||
private string RandomString(int length)
|
||||
|
@ -1,5 +1,3 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -9,6 +7,7 @@ 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;
|
||||
|
||||
@ -20,18 +19,9 @@ 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 (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
|
||||
_voices.Remove(data["voice"].ToString());
|
||||
_voices.Remove(data!["voice"].ToString());
|
||||
_logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]");
|
||||
return new RequestResult(true, null);
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -7,6 +6,7 @@ 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 new RequestResult(true, ids, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(ids, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -7,7 +6,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetConnections : IRequest
|
||||
{
|
||||
public string Name => "get_connections";
|
||||
|
||||
public string[] RequiredKeys => [];
|
||||
private Database _database;
|
||||
private Serilog.ILogger _logger;
|
||||
|
||||
@ -22,8 +21,8 @@ namespace HermesSocketServer.Requests
|
||||
var temp = new Dictionary<string, object>() { { "user", sender } };
|
||||
|
||||
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, temp, sql =>
|
||||
string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";
|
||||
await _database.Execute(sql, temp, sql =>
|
||||
connections.Add(new Connection()
|
||||
{
|
||||
Name = sql.GetString(0),
|
||||
@ -36,7 +35,7 @@ namespace HermesSocketServer.Requests
|
||||
Default = sql.GetBoolean(7)
|
||||
})
|
||||
);
|
||||
return new RequestResult(true, connections, false);
|
||||
return RequestResult.Successful(connections, false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,6 +6,7 @@ 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 new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
|
||||
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);
|
||||
|
||||
return new RequestResult(true, user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetEmotes : IRequest
|
||||
{
|
||||
public string Name => "get_emotes";
|
||||
public string[] RequiredKeys => [];
|
||||
private Database _database;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -28,7 +27,7 @@ namespace HermesSocketServer.Requests
|
||||
Name = r.GetString(1)
|
||||
}));
|
||||
_logger.Information($"Fetched all emotes for channel [channel: {sender}]");
|
||||
return new RequestResult(true, emotes, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(emotes, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -7,7 +6,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;
|
||||
|
||||
@ -27,7 +26,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 new RequestResult(true, voices, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(voices, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -8,7 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetPermissions : IRequest
|
||||
{
|
||||
public string Name => "get_permissions";
|
||||
|
||||
public string[] RequiredKeys => [];
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -56,7 +55,7 @@ namespace HermesSocketServer.Requests
|
||||
GroupChatters = groupChatters,
|
||||
GroupPermissions = groupPermissions
|
||||
};
|
||||
return new RequestResult(true, info, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(info, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -9,7 +8,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;
|
||||
@ -34,7 +33,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 new RequestResult(true, redemptions, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -8,7 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetRedemptions : IRequest
|
||||
{
|
||||
public string Name => "get_redemptions";
|
||||
|
||||
public string[] RequiredKeys => [];
|
||||
private readonly Database _database;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
@ -33,7 +32,7 @@ namespace HermesSocketServer.Requests
|
||||
State = r.GetBoolean(4)
|
||||
}));
|
||||
_logger.Information($"Fetched all redemptions for channel [channel: {sender}]");
|
||||
return new RequestResult(true, redemptions, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Services;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,6 +6,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetTTSUsers : IRequest
|
||||
{
|
||||
public string Name => "get_tts_users";
|
||||
public string[] RequiredKeys => [];
|
||||
private ChannelManager _channels;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -19,12 +19,9 @@ namespace HermesSocketServer.Requests
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
var channel = _channels.Get(sender);
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null, notifyClientsOnAccount: false);
|
||||
|
||||
var temp = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
|
||||
var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
|
||||
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]");
|
||||
return new RequestResult(true, temp, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(results, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -9,6 +7,7 @@ namespace HermesSocketServer.Requests
|
||||
public class GetTTSVoices : IRequest
|
||||
{
|
||||
public string Name => "get_tts_voices";
|
||||
public string[] RequiredKeys => [];
|
||||
private VoiceStore _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
@ -27,7 +26,7 @@ namespace HermesSocketServer.Requests
|
||||
});
|
||||
|
||||
_logger.Information($"Fetched all TTS voices for channel [channel: {sender}]");
|
||||
return new RequestResult(true, voices, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(voices, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -9,6 +7,7 @@ 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;
|
||||
|
||||
@ -31,7 +30,7 @@ namespace HermesSocketServer.Requests
|
||||
Replace = r.GetString(2)
|
||||
}));
|
||||
_logger.Information($"Fetched all word filters for channel [channel: {sender}]");
|
||||
return new RequestResult(true, filters, notifyClientsOnAccount: false);
|
||||
return RequestResult.Successful(filters, notifyClientsOnAccount: false);
|
||||
}
|
||||
}
|
||||
}
|
10
Requests/IRequest.cs
Normal file
10
Requests/IRequest.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public interface IRequest
|
||||
{
|
||||
string Name { get; }
|
||||
string[] RequiredKeys { get; }
|
||||
|
||||
Task<RequestResult> Grant(string sender, IDictionary<string, object>? data);
|
||||
}
|
||||
}
|
9
Requests/IRequestManager.cs
Normal file
9
Requests/IRequestManager.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public interface IRequestManager
|
||||
{
|
||||
Task<RequestResult> Grant(string sender, RequestMessage? message);
|
||||
}
|
||||
}
|
59
Requests/RequestManager.cs
Normal file
59
Requests/RequestManager.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
26
Requests/RequestResult.cs
Normal file
26
Requests/RequestResult.cs
Normal file
@ -0,0 +1,26 @@
|
||||
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,4 +1,3 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
@ -7,6 +6,7 @@ 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,21 +18,15 @@ 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 new RequestResult(false, "Unable to find user data.", notifyClientsOnAccount: false);
|
||||
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);
|
||||
|
||||
_logger.Information($"Updated default TTS voice for channel [channel: {sender}][voice: {data["voice"]}]");
|
||||
return new RequestResult(true, null);
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
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
|
||||
@ -12,7 +8,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;
|
||||
@ -29,34 +25,27 @@ 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;
|
||||
if (data["voice"] is JsonElement v)
|
||||
data["voice"] = v.ToString();
|
||||
data["user"] = sender;
|
||||
data["voice"] = data["voice"].ToString();
|
||||
|
||||
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 new RequestResult(false, null);
|
||||
return RequestResult.Failed("Voice is either non-existent or disabled on this channel.");
|
||||
|
||||
var channel = _channels.Get(sender);
|
||||
if (channel == null)
|
||||
return new RequestResult(false, null);
|
||||
|
||||
channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
|
||||
{
|
||||
UserId = sender,
|
||||
ChatterId = chatterId.ToString(),
|
||||
VoiceId = data["voice"].ToString()!
|
||||
});
|
||||
_logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]");
|
||||
return new RequestResult(true, null);
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -9,6 +7,7 @@ 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;
|
||||
|
||||
@ -20,24 +19,20 @@ 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["voice"] = data["voice"].ToString();
|
||||
data["voiceid"] = data["voiceid"].ToString();
|
||||
|
||||
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()
|
||||
var result = _voices.Set(data["voiceid"].ToString(), new Voice()
|
||||
{
|
||||
Id = data["voiceid"].ToString()!,
|
||||
Name = data["voice"].ToString()!
|
||||
});
|
||||
_logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]");
|
||||
return new RequestResult(true, null);
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
@ -8,6 +6,7 @@ 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;
|
||||
|
||||
@ -19,22 +18,16 @@ 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 (data["voice"] is JsonElement voice)
|
||||
data["voice"] = voice.ToString();
|
||||
if (data["state"] is JsonElement state)
|
||||
data["state"] = state.ToString() == "True";
|
||||
data["voice"] = data["voice"].ToString();
|
||||
data["state"] = data["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"]}]");
|
||||
return new RequestResult(result == 1, null);
|
||||
if (result > 0)
|
||||
return RequestResult.Successful(null);
|
||||
return RequestResult.Failed("Something went wrong when updating the database.");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
using HermesSocketLibrary.Requests;
|
||||
using HermesSocketLibrary.Socket.Data;
|
||||
using HermesSocketServer.Requests;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Socket.Handlers
|
||||
|
Loading…
Reference in New Issue
Block a user