Fixed compiler warnings.

This commit is contained in:
Tom 2024-12-28 17:24:02 +00:00
parent 525725a7e5
commit 538bf07454
30 changed files with 108 additions and 105 deletions

View File

@ -4,10 +4,12 @@ namespace HermesSocketServer.Models
{
public class Channel
{
public string Id { get; set; }
public User User { get; set; }
public ChatterStore Chatters { get; set; }
public PolicyStore Policies { get; set; }
public TTSFilterStore Filters { get; set; }
public required string Id { get; set; }
public required User User { get; set; }
public required ChatterStore Chatters { get; set; }
public required PolicyStore Policies { get; set; }
public required TTSFilterStore Filters { get; set; }
public required ActionStore Actions { get; set; }
public required RedemptionStore Redemptions { get; set; }
}
}

View File

@ -3,7 +3,7 @@ namespace HermesSocketServer.Models
public class ChatterVoice
{
public long ChatterId { get; set; }
public string UserId { get; set; }
public string VoiceId { get; set; }
public required string UserId { get; set; }
public required string VoiceId { get; set; }
}
}

View File

@ -2,10 +2,10 @@ namespace HermesSocketServer.Models
{
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Role { get; set; }
public string DefaultVoice { get; set; }
public required string Id { get; set; }
public required string Name { get; set; }
public required string Email { get; set; }
public required string Role { get; set; }
public required string DefaultVoice { get; set; }
}
}

View File

@ -2,7 +2,7 @@ namespace HermesSocketServer.Models
{
public class Voice
{
public string Id { get; set; }
public string Name { get; set; }
public required string Id { get; set; }
public required string Name { get; set; }
}
}

View File

@ -51,12 +51,15 @@ namespace HermesSocketServer.Quests
});
string sql2 = "SELECT id FROM \"Quest\" WHERE type = @type AND start = @start";
int? questId = (int?)await _database.ExecuteScalar(sql, c =>
int? questId = (int?)await _database.ExecuteScalar(sql2, c =>
{
c.Parameters.AddWithValue("@type", temp.Type);
c.Parameters.AddWithValue("@start", temp.StartTime);
});
if (questId == null)
return;
var quest = new DailyQuest((short)questId.Value, task, date);
_quests.Add(quest.Id, quest);
}
@ -88,12 +91,15 @@ namespace HermesSocketServer.Quests
});
string sql2 = "SELECT id FROM \"Quest\" WHERE type = @type AND start = @start";
int? questId = (int?)await _database.ExecuteScalar(sql, c =>
int? questId = (int?)await _database.ExecuteScalar(sql2, c =>
{
c.Parameters.AddWithValue("@type", temp.Type);
c.Parameters.AddWithValue("@start", temp.StartTime);
});
if (questId == null)
return;
var quest = new WeeklyQuest((short)questId.Value, task, date);
_quests.Add(quest.Id, quest);
}

View File

@ -14,7 +14,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = Guid.NewGuid();
string groupId = data["groupId"].ToString()!;
@ -36,9 +36,9 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]");
return RequestResult.Successful(policy);
return Task.FromResult(RequestResult.Successful(policy));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -15,7 +15,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = Guid.NewGuid();
string search = data["search"].ToString()!;
@ -33,9 +33,9 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Added filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]");
return RequestResult.Successful(filter);
return Task.FromResult(RequestResult.Successful(filter));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -19,7 +19,7 @@ namespace HermesSocketServer.Requests
_random = new Random();
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string voice = data["voice"].ToString()!;
string id = RandomString(25);
@ -32,9 +32,9 @@ namespace HermesSocketServer.Requests
if (result) {
_logger.Information($"Added a new voice [voice: {voice}][voice id: {id}]");
return RequestResult.Successful(id);
return Task.FromResult(RequestResult.Successful(id));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
private string RandomString(int length)

View File

@ -14,18 +14,18 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string policyId = data["id"].ToString()!;
var policy = channel.Policies.Get(policyId);
if (policy != null) {
channel.Policies.Remove(policyId);
_logger.Information($"Deleted a policy by id [policy id: {data["id"]}]");
return RequestResult.Successful(policy.GroupId + "/" + policy.Path);
return Task.FromResult(RequestResult.Successful(policy.GroupId + "/" + policy.Path));
}
_logger.Warning("Failed to find policy by id ");
return RequestResult.Failed("Cannot find the policy by id.");
_logger.Warning($"Failed to find policy by id [id: {policyId}]");
return Task.FromResult(RequestResult.Failed("Cannot find the policy by id."));
}
}
}

View File

@ -14,13 +14,13 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string filterId = data["id"].ToString()!;
channel.Filters.Remove(filterId);
_logger.Information($"Deleted a TTS filter by id [tts filter id: {filterId}]");
return RequestResult.Successful(null);
return Task.FromResult(RequestResult.Successful(null));
}
}
}

View File

@ -17,12 +17,12 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string voiceId = data["voice"].ToString()!;
_voices.Remove(voiceId);
_logger.Information($"Deleted a voice by id [voice id: {voiceId}]");
return RequestResult.Successful(null);
return Task.FromResult(RequestResult.Successful(null));
}
}
}

View File

@ -14,11 +14,11 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
IEnumerable<long> ids = channel.Chatters.Get().Values.Select(c => c.ChatterId);
_logger.Information($"Fetched all chatters for channel [channel: {channel.Id}]");
return RequestResult.Successful(ids, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(ids, notifyClientsOnAccount: false));
}
}
}

View File

@ -19,13 +19,13 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var user = _users.Get(channel.Id);
if (user == null)
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false));
return RequestResult.Successful(user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(user.DefaultVoice ?? _configuration.Tts.DefaultTtsVoice, notifyClientsOnAccount: false));
}
}
}

View File

@ -14,12 +14,12 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var results = channel.Policies.Get().Values;
_logger.Information($"Fetched policies for channel [policy size: {results.Count}][channel: {channel.Id}]");
return RequestResult.Successful(results, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(results, notifyClientsOnAccount: false));
}
}
}

View File

@ -1,6 +1,3 @@
using System.Text.Json;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
@ -10,31 +7,18 @@ namespace HermesSocketServer.Requests
{
public string Name => "get_redeemable_actions";
public string[] RequiredKeys => [];
private readonly JsonSerializerOptions _options;
private readonly Database _database;
private readonly ILogger _logger;
public GetRedeemableActions(JsonSerializerOptions options, Database database, ILogger logger)
public GetRedeemableActions(ILogger logger)
{
_options = options;
_database = database;
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var redemptions = new List<RedeemableAction>();
string sql = $"SELECT name, type, data FROM \"Action\" WHERE \"userId\" = @user";
await _database.Execute(sql, temp, (r) => redemptions.Add(new RedeemableAction()
{
Name = r.GetString(0),
Type = r.GetString(1),
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(r.GetString(2), _options)!
}));
var redemptions = channel.Actions.Get().Values;
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]");
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(redemptions, notifyClientsOnAccount: false));
}
}
}

View File

@ -1,5 +1,4 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
@ -18,11 +17,11 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var redemptions = new List<Redemption>();
var redemptions = channel.Redemptions.Get().Values; /* new List<Redemption>();
string sql = $"SELECT id, \"redemptionId\", \"actionName\", \"order\", state FROM \"Redemption\" WHERE \"userId\" = @user";
await _database.Execute(sql, temp, (r) => redemptions.Add(new Redemption()
{
@ -31,9 +30,9 @@ namespace HermesSocketServer.Requests
ActionName = r.GetString(2),
Order = r.GetInt32(3),
State = r.GetBoolean(4)
}));
}));*/
_logger.Information($"Fetched all redemptions for channel [channel: {channel.Id}]");
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(redemptions, notifyClientsOnAccount: false));
}
}
}

View File

@ -14,11 +14,11 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]");
return RequestResult.Successful(results, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(results, notifyClientsOnAccount: false));
}
}
}

View File

@ -18,7 +18,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
IEnumerable<VoiceDetails> voices = _voices.Get().Select(v => new VoiceDetails()
{
@ -27,7 +27,7 @@ namespace HermesSocketServer.Requests
});
_logger.Information($"Fetched all TTS voices for channel [channel: {channel.Id}]");
return RequestResult.Successful(voices, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(voices, notifyClientsOnAccount: false));
}
}
}

View File

@ -15,12 +15,12 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
IEnumerable<TTSWordFilter> filters = channel.Filters.Get().Values;
_logger.Information($"Fetched all word filters for channel [channel: {channel.Id}]");
return RequestResult.Successful(filters, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(filters, notifyClientsOnAccount: false));
}
}
}

View File

@ -7,6 +7,6 @@ namespace HermesSocketServer.Requests
string Name { get; }
string[] RequiredKeys { get; }
Task<RequestResult> Grant(Channel channel, IDictionary<string, object>? data);
Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data);
}
}

View File

@ -21,7 +21,7 @@ namespace HermesSocketServer.Requests
{
if (message == null || message.Type == null)
{
_logger.Debug($"Request type does not exist [id: {message.RequestId}][nounce: {message.Nounce}]");
_logger.Debug($"Request type does not exist [id: {message?.RequestId ?? "null"}][nounce: {message?.Nounce ?? "null"}]");
return RequestResult.Failed("Request type does not exist.");
}
@ -58,7 +58,7 @@ namespace HermesSocketServer.Requests
try
{
return await request.Grant(channel, message.Data);
return await request.Grant(channel, message.Data ?? new Dictionary<string, object>());
}
catch (Exception e)
{

View File

@ -17,17 +17,17 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string user = data["user"].ToString()!;
string voice = data["voice"].ToString()!;
var success = _users.Modify(user, (user) => user.DefaultVoice = voice);
if (!success)
return RequestResult.Failed("Unable to find user data.");
return Task.FromResult(RequestResult.Failed("Unable to find user data."));
_logger.Information($"Updated default TTS voice for channel [channel: {channel.Id}][voice: {voice}]");
return RequestResult.Successful(null);
return Task.FromResult(RequestResult.Successful(null));
}
}
}

View File

@ -14,7 +14,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = Guid.Parse(data["id"].ToString()!);
string groupId = data["groupId"].ToString()!;
@ -36,9 +36,9 @@ namespace HermesSocketServer.Requests
{
var policy = channel.Policies.Get(id.ToString());
_logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]");
return RequestResult.Successful(policy);
return Task.FromResult(RequestResult.Successful(policy));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -16,7 +16,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
var id = data["id"].ToString()!;
string search = data["search"].ToString()!;
@ -35,9 +35,9 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Updated filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]");
return RequestResult.Successful(filter);
return Task.FromResult(RequestResult.Successful(filter));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -17,7 +17,7 @@ namespace HermesSocketServer.Requests
_logger = logger;
}
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string voiceName = data["voice"].ToString()!;
string voiceId = data["voiceid"].ToString()!;
@ -30,9 +30,9 @@ namespace HermesSocketServer.Requests
if (result)
{
_logger.Information($"Updated voice's [voice id: {voiceId}] name [new name: {voiceName}]");
return RequestResult.Successful(null);
return Task.FromResult(RequestResult.Successful(null));
}
return RequestResult.Failed("Something went wrong when updating the cache.");
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}

View File

@ -19,7 +19,7 @@ namespace HermesSocketServer.Requests
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
data["voice"] = data["voice"].ToString();
data["voice"] = data["voice"].ToString()!;
data["state"] = data["state"].ToString() == "True";
data["user"] = channel.Id;

View File

@ -2,28 +2,28 @@ namespace HermesSocketServer
{
public class ServerConfiguration
{
public string Environment;
public WebsocketServerConfiguration WebsocketServer;
public DatabaseConfiguration Database;
public TTSConfiguration Tts;
public string AdminPassword;
public required string Environment;
public required WebsocketServerConfiguration WebsocketServer;
public required DatabaseConfiguration Database;
public required TTSConfiguration Tts;
public string? AdminPassword;
}
public class WebsocketServerConfiguration
{
public string Host;
public string Port;
public required string Host;
public required string Port;
}
public class DatabaseConfiguration
{
public string ConnectionString;
public required string ConnectionString;
public int SaveDelayInSeconds;
}
public class TTSConfiguration
{
public long OwnerId;
public string DefaultTtsVoice;
public required string DefaultTtsVoice;
}
}

View File

@ -23,27 +23,32 @@ namespace HermesSocketServer.Services
}
public async Task<Channel?> Add(string userId)
public Task<Channel?> Add(string userId)
{
var user = _users.Get(userId);
if (user == null)
{
return null;
return Task.FromResult<Channel?>(null);
}
lock (_lock)
{
if (_channels.ContainsKey(userId))
{
return null;
return Task.FromResult<Channel?>(null);
}
var chatters = new ChatterStore(userId, _database, _logger);
var policies = new PolicyStore(userId, _database, _logger);
var filters = new TTSFilterStore(userId, _database, _logger);
var actions = new ActionStore(userId, _database, _logger);
var redemptions = new RedemptionStore(userId, _database, _logger);
Task.WaitAll([
chatters.Load(),
policies.Load(),
filters.Load(),
actions.Load(),
redemptions.Load(),
]);
var channel = new Channel()
@ -53,10 +58,12 @@ namespace HermesSocketServer.Services
Chatters = chatters,
Policies = policies,
Filters = filters,
Actions = actions,
Redemptions = redemptions,
};
_channels.Add(userId, channel);
return channel;
return Task.FromResult<Channel?>(channel);
}
}
@ -76,6 +83,8 @@ namespace HermesSocketServer.Services
channel.Chatters.Save(),
channel.Policies.Save(),
channel.Filters.Save(),
channel.Actions.Save(),
channel.Redemptions.Save(),
]);
}
@ -88,6 +97,8 @@ namespace HermesSocketServer.Services
channel.Chatters.Save(),
channel.Policies.Save(),
channel.Filters.Save(),
channel.Actions.Save(),
channel.Redemptions.Save(),
]);
}
}

View File

@ -15,11 +15,11 @@ namespace HermesSocketServer.Socket.Handlers
}
public async Task Execute<T>(WebSocketUser sender, T data, HermesSocketManager sockets)
public Task Execute<T>(WebSocketUser sender, T data, HermesSocketManager sockets)
{
if (data is not LoggingMessage message || sender.Id == null)
return;
return Task.CompletedTask;
Action<Exception?, string> logging;
if (message.Level == HermesLoggingLevel.Trace)
logging = _logger.Verbose;
@ -35,10 +35,11 @@ namespace HermesSocketServer.Socket.Handlers
logging = _logger.Fatal;
else {
_logger.Warning("Failed to receive a logging level from client.");
return;
return Task.CompletedTask;
}
logging.Invoke(message.Exception, message.Message + $" [ip: {sender.IPAddress}][id: {sender.Id}][name: {sender.Name}][token: {sender.ApiKey}][uid: {sender.UID}]");
return Task.CompletedTask;
}
}
}

View File

@ -23,7 +23,7 @@ namespace HermesSocketServer.Socket
public IPAddress? IPAddress { get => _ipAddress; }
public bool Connected { get => _connected; }
public string UID { get; }
public string ApiKey { get; set; }
public string? ApiKey { get; set; }
public string? Id { get; set; }
public string? Name { get; set; }
public bool Admin { get; set; }