Added checks for non-user foreign keys in stores. Load/Saving stores' order is now based on table dependencies. Added ability to use chat message when using redemption.

This commit is contained in:
Tom
2025-01-28 19:12:14 +00:00
parent 6d955f245a
commit 3e717522c2
18 changed files with 146 additions and 102 deletions

View File

@ -18,7 +18,7 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data) public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = Guid.NewGuid(); var id = Guid.NewGuid();
string groupId = data["group"].ToString()!; Guid groupId = new Guid(data["group"].ToString()!);
string path = data["path"].ToString()!; string path = data["path"].ToString()!;
bool? allow = bool.TryParse(data["allow"].ToString()!, out bool a) ? a : null; bool? allow = bool.TryParse(data["allow"].ToString()!, out bool a) ? a : null;

View File

@ -8,7 +8,7 @@ namespace HermesSocketServer.Requests
public class CreateRedeemableAction : IRequest public class CreateRedeemableAction : IRequest
{ {
public string Name => "create_redeemable_action"; public string Name => "create_redeemable_action";
public string[] RequiredKeys => ["name", "data", "type"]; public string[] RequiredKeys => ["name", "has_message", "type", "data"];
private ILogger _logger; private ILogger _logger;
public CreateRedeemableAction(ILogger logger) public CreateRedeemableAction(ILogger logger)
@ -19,8 +19,9 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data) public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
string name = data["name"].ToString()!; string name = data["name"].ToString()!;
string d = data["data"].ToString()!;
string type = data["type"].ToString()!; string type = data["type"].ToString()!;
bool hasMessage = data["has_message"].ToString()!.ToLower() == "true";
string d = data["data"].ToString()!;
IDictionary<string, string> dict = new Dictionary<string, string>(); IDictionary<string, string> dict = new Dictionary<string, string>();
try try
@ -29,7 +30,7 @@ namespace HermesSocketServer.Requests
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex, $"Failed to parse data on redeemable action while creating action [name: {name}][type: {type}][data: {d}]"); _logger.Error(ex, $"Failed to parse data on redeemable action while creating action [name: {name}][type: {type}][has message: {hasMessage}][data: {d}]");
return Task.FromResult(RequestResult.Failed("Could not parse the data on this action.")); return Task.FromResult(RequestResult.Failed("Could not parse the data on this action."));
} }
@ -37,14 +38,15 @@ namespace HermesSocketServer.Requests
{ {
UserId = channel.Id, UserId = channel.Id,
Name = name, Name = name,
Data = dict,
Type = type, Type = type,
HasMessage = hasMessage,
Data = dict,
}; };
bool result = channel.Actions.Set(name, action); bool result = channel.Actions.Set(name, action);
if (result) if (result)
{ {
_logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]"); _logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][has message: {hasMessage}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(action)); return Task.FromResult(RequestResult.Successful(action));
} }
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache.")); return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));

View File

@ -22,7 +22,7 @@ namespace HermesSocketServer.Requests
if (result) if (result)
{ {
var permissions = channel.GroupPermissions.Get().Values var permissions = channel.GroupPermissions.Get().Values
.Where(p => p.GroupId == groupId); .Where(p => p.GroupId.ToString() == groupId);
Task? chattersSave = null; Task? chattersSave = null;
if (channel.Groups.Chatters.TryGetValue(groupId, out var chatters)) if (channel.Groups.Chatters.TryGetValue(groupId, out var chatters))
@ -32,7 +32,7 @@ namespace HermesSocketServer.Requests
{ {
foreach (var chatter in filteredChatters) foreach (var chatter in filteredChatters)
{ {
var res = chatters.Remove(chatter.ChatterId.ToString()); var res = chatters.Remove(chatter.ChatterId.ToString(), fromCascade: true);
if (!res) if (!res)
_logger.Warning($"Failed to delete group chatter by id [group chatter id: {chatter.ChatterId}]"); _logger.Warning($"Failed to delete group chatter by id [group chatter id: {chatter.ChatterId}]");
} }
@ -43,7 +43,7 @@ namespace HermesSocketServer.Requests
foreach (var permission in permissions) foreach (var permission in permissions)
{ {
var res = channel.GroupPermissions.Remove(permission.Id); var res = channel.GroupPermissions.Remove(permission.Id, fromCascade: true);
if (!res) if (!res)
_logger.Warning($"Failed to delete group permission by id [group chatter id: {permission.Id}]"); _logger.Warning($"Failed to delete group permission by id [group chatter id: {permission.Id}]");
} }

View File

@ -6,7 +6,7 @@ namespace HermesSocketServer.Requests
public class DeleteGroupChatter : IRequest public class DeleteGroupChatter : IRequest
{ {
public string Name => "delete_group_chatter"; public string Name => "delete_group_chatter";
public string[] RequiredKeys => ["id", "group"]; public string[] RequiredKeys => ["chatter", "group"];
private ILogger _logger; private ILogger _logger;
public DeleteGroupChatter(ILogger logger) public DeleteGroupChatter(ILogger logger)
@ -16,7 +16,7 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data) public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var chatterId = data["id"].ToString()!; var chatterId = data["chatter"].ToString()!;
var groupId = data["group"].ToString()!; var groupId = data["group"].ToString()!;
if (!channel.Groups.Chatters.TryGetValue(groupId, out var chatters)) if (!channel.Groups.Chatters.TryGetValue(groupId, out var chatters))

View File

@ -30,8 +30,8 @@ namespace HermesSocketServer.Requests
private class GroupDetails private class GroupDetails
{ {
public required Group Group; public required Group Group { get; set; }
public required IEnumerable<GroupChatter> Chatters; public required IEnumerable<GroupChatter> Chatters { get; set; }
} }
} }
} }

View File

@ -20,7 +20,7 @@ namespace HermesSocketServer.Requests
var id = Guid.NewGuid(); var id = Guid.NewGuid();
string groupId = data["group"].ToString()!; string groupId = data["group"].ToString()!;
if (!int.TryParse(data["chatter"].ToString()!, out var chatterId)) if (!int.TryParse(data["chatter"].ToString()!, out var chatterId))
return Task.FromResult(RequestResult.Failed("Priority needs to be an integer.")); return Task.FromResult(RequestResult.Failed("Chatter Id needs to be an integer."));
string chatterLabel = data["label"].ToString()!; string chatterLabel = data["label"].ToString()!;
var groupChatter = new GroupChatter() var groupChatter = new GroupChatter()

View File

@ -18,7 +18,7 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data) public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = data["id"].ToString()!; var id = data["id"].ToString()!;
string groupId = data["group"].ToString()!; Guid groupId = new Guid(data["group"].ToString()!);
string path = data["path"].ToString()!; string path = data["path"].ToString()!;
bool? allow = bool.TryParse(data["allow"].ToString()!, out bool a) ? a : null; bool? allow = bool.TryParse(data["allow"].ToString()!, out bool a) ? a : null;

View File

@ -19,8 +19,9 @@ namespace HermesSocketServer.Requests
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data) public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
string name = data["name"].ToString()!; string name = data["name"].ToString()!;
string d = data["data"].ToString()!;
string type = data["type"].ToString()!; string type = data["type"].ToString()!;
bool hasMessage = data["has_message"].ToString()!.ToLower() == "true";
string d = data["data"].ToString()!;
IDictionary<string, string> dict = new Dictionary<string, string>(); IDictionary<string, string> dict = new Dictionary<string, string>();
try try
@ -29,7 +30,7 @@ namespace HermesSocketServer.Requests
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex, $"Failed to parse data on redeemable action while updating action [name: {name}][type: {type}][data: {d}]"); _logger.Error(ex, $"Failed to parse data on redeemable action while updating action [name: {name}][type: {type}][has message: {hasMessage}][data: {d}]");
return Task.FromResult(RequestResult.Failed("Could not parse the data on this action.")); return Task.FromResult(RequestResult.Failed("Could not parse the data on this action."));
} }
@ -37,14 +38,15 @@ namespace HermesSocketServer.Requests
{ {
UserId = channel.Id, UserId = channel.Id,
Name = name, Name = name,
Data = dict,
Type = type, Type = type,
HasMessage = hasMessage,
Data = dict,
}; };
bool result = channel.Actions.Modify(name, action); bool result = channel.Actions.Modify(name, action);
if (result) if (result)
{ {
_logger.Information($"Updated redeemable action on channel [name: {name}][type: {type}][channel: {channel.Id}]"); _logger.Information($"Updated redeemable action on channel [name: {name}][type: {type}][has message: {hasMessage}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(action)); return Task.FromResult(RequestResult.Successful(action));
} }
if (channel.Actions.Get(name) == null) if (channel.Actions.Get(name) == null)

View File

@ -1,8 +1,8 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models; using HermesSocketServer.Models;
using HermesSocketServer.Store; using HermesSocketServer.Store;
using HermesSocketServer.Validators;
namespace HermesSocketServer.Services namespace HermesSocketServer.Services
{ {
@ -10,15 +10,17 @@ namespace HermesSocketServer.Services
{ {
private readonly IStore<string, User> _users; private readonly IStore<string, User> _users;
private readonly Database _database; private readonly Database _database;
private readonly IStore<string, TTSVoice> _voices;
private readonly ServerConfiguration _configuration; private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger; private readonly Serilog.ILogger _logger;
private readonly IDictionary<string, Channel> _channels; private readonly IDictionary<string, Channel> _channels;
private readonly object _lock; private readonly object _lock;
public ChannelManager(IStore<string, User> users, Database database, ServerConfiguration configuration, Serilog.ILogger logger) public ChannelManager(IStore<string, User> users, Database database, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Serilog.ILogger logger)
{ {
_users = users; _users = users;
_database = database; _database = database;
_voices = voices;
_configuration = configuration; _configuration = configuration;
_logger = logger; _logger = logger;
_channels = new ConcurrentDictionary<string, Channel>(); _channels = new ConcurrentDictionary<string, Channel>();
@ -26,68 +28,74 @@ namespace HermesSocketServer.Services
} }
public Task<Channel?> Add(string userId) public async Task<Channel?> Add(string userId)
{ {
var user = _users.Get(userId); var user = _users.Get(userId);
if (user == null) if (user == null)
return Task.FromResult<Channel?>(null); return null;
lock (_lock) return await Task.Run(() =>
{ {
if (_channels.ContainsKey(userId)) lock (_lock)
return Task.FromResult<Channel?>(null);
var actionTable = _configuration.Database.Tables["Action"];
var chatterTable = _configuration.Database.Tables["Chatter"];
var connectionTable = _configuration.Database.Tables["Connection"];
//var chatterGroupTable = _configuration.Database.Tables["ChatterGroup"];
var groupTable = _configuration.Database.Tables["Group"];
var groupPermissionTable = _configuration.Database.Tables["GroupPermission"];
var policyTable = _configuration.Database.Tables["Policy"];
var redemptionTable = _configuration.Database.Tables["Redemption"];
var ttsFilterTable = _configuration.Database.Tables["TtsFilter"];
var ttsVoiceStateTable = _configuration.Database.Tables["VoiceState"];
var chatters = new ChatterStore(userId, chatterTable, _database, _logger);
var connections = new ConnectionStore(userId, connectionTable, _database, _logger);
var groups = new GroupStore(userId, groupTable, _database, _configuration, _logger);
var groupPermissions = new GroupPermissionStore(userId, groupPermissionTable, _database, _logger);
var policies = new PolicyStore(userId, policyTable, _database, _logger);
var filters = new TTSFilterStore(userId, ttsFilterTable, _database, _logger);
var actions = new ActionStore(userId, actionTable, _database, _logger);
var redemptions = new RedemptionStore(userId, redemptionTable, _database, _logger);
var voiceStates = new VoiceStateStore(userId, new VoiceIdValidator(), ttsVoiceStateTable, _database, _logger);
Task.WaitAll([
chatters.Load(),
connections.Load(),
groups.Load(),
groupPermissions.Load(),
policies.Load(),
filters.Load(),
actions.Load(),
redemptions.Load(),
voiceStates.Save(),
]);
var channel = new Channel()
{ {
Id = userId, if (_channels.ContainsKey(userId))
User = user, return Task.FromResult<Channel?>(null);
Chatters = chatters,
Connections = connections,
Groups = groups,
GroupPermissions = groupPermissions,
Policies = policies,
Filters = filters,
Actions = actions,
Redemptions = redemptions,
VoiceStates = voiceStates,
};
_channels.Add(userId, channel); var actionTable = _configuration.Database.Tables["Action"];
return Task.FromResult<Channel?>(channel); var chatterTable = _configuration.Database.Tables["Chatter"];
} var connectionTable = _configuration.Database.Tables["Connection"];
//var chatterGroupTable = _configuration.Database.Tables["ChatterGroup"];
var groupTable = _configuration.Database.Tables["Group"];
var groupPermissionTable = _configuration.Database.Tables["GroupPermission"];
var policyTable = _configuration.Database.Tables["Policy"];
var redemptionTable = _configuration.Database.Tables["Redemption"];
var ttsFilterTable = _configuration.Database.Tables["TtsFilter"];
var ttsVoiceStateTable = _configuration.Database.Tables["VoiceState"];
var chatters = new ChatterStore(userId, chatterTable, _database, _logger);
var connections = new ConnectionStore(userId, connectionTable, _database, _logger);
var groups = new GroupStore(userId, groupTable, _database, _configuration, _logger);
var groupPermissions = new GroupPermissionStore(userId, groupPermissionTable, groups, _database, _logger);
var policies = new PolicyStore(userId, policyTable, groups, _database, _logger);
var filters = new TTSFilterStore(userId, ttsFilterTable, _database, _logger);
var actions = new ActionStore(userId, actionTable, _database, _logger);
var redemptions = new RedemptionStore(userId, redemptionTable, actions, _database, _logger);
var voiceStates = new VoiceStateStore(userId, ttsVoiceStateTable, _voices, _database, _logger);
var channel = new Channel()
{
Id = userId,
User = user,
Chatters = chatters,
Connections = connections,
Groups = groups,
GroupPermissions = groupPermissions,
Policies = policies,
Filters = filters,
Actions = actions,
Redemptions = redemptions,
VoiceStates = voiceStates,
};
Task.WaitAll([
channel.Actions.Load(),
channel.Chatters.Load(),
channel.Connections.Load(),
channel.Groups.Load(),
channel.Filters.Load(),
channel.VoiceStates.Load(),
]);
Task.WaitAll([
channel.GroupPermissions.Load(),
channel.Policies.Load(),
channel.Redemptions.Load(),
]);
_channels.Add(userId, channel);
return Task.FromResult<Channel?>(channel);
}
});
} }
public Channel? Get(string channelId) public Channel? Get(string channelId)
@ -121,17 +129,25 @@ namespace HermesSocketServer.Services
foreach (var channel in _channels.Values) foreach (var channel in _channels.Values)
{ {
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]"); _logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
await Task.WhenAll([ var genericTablesTask = Task.WhenAll([
channel.Chatters.Save(), channel.Chatters.Save(),
channel.Connections.Save(), channel.Connections.Save(),
channel.Filters.Save(),
channel.VoiceStates.Save(),
]).ConfigureAwait(false);
await Task.WhenAll([
channel.Actions.Save(),
channel.Groups.Save(), channel.Groups.Save(),
]).ConfigureAwait(false);
await Task.WhenAll([
channel.GroupPermissions.Save(), channel.GroupPermissions.Save(),
channel.Policies.Save(), channel.Policies.Save(),
channel.Filters.Save(),
channel.Actions.Save(),
channel.Redemptions.Save(), channel.Redemptions.Save(),
channel.VoiceStates.Save(), ]).ConfigureAwait(false);
]);
await genericTablesTask;
} }
} }
} }

View File

@ -1,4 +1,3 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models; using HermesSocketServer.Models;
using HermesSocketServer.Store; using HermesSocketServer.Store;
@ -24,10 +23,10 @@ namespace HermesSocketServer.Services
protected override async Task ExecuteAsync(CancellationToken cancellationToken) protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{ {
_logger.Information("Loading TTS voices...");
await _voices.Load();
_logger.Information("Loading users..."); _logger.Information("Loading users...");
await _users.Load(); await _users.Load();
_logger.Information("Loading TTS voices...");
await _voices.Load();
await Task.Run(async () => await Task.Run(async () =>
{ {
@ -35,11 +34,10 @@ namespace HermesSocketServer.Services
while (true) while (true)
{ {
await Task.WhenAll([ await _users.Save();
_voices.Save(), await _voices.Save();
_users.Save(), await _channels.Save();
_channels.Save(),
]);
await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)); await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds));
} }
}); });

View File

@ -118,6 +118,8 @@ s.AddSingleton<IRequest, GetConnections>();
s.AddSingleton<IRequest, GetDefaultTTSVoice>(); s.AddSingleton<IRequest, GetDefaultTTSVoice>();
s.AddSingleton<IRequest, GetEmotes>(); s.AddSingleton<IRequest, GetEmotes>();
s.AddSingleton<IRequest, GetEnabledTTSVoices>(); s.AddSingleton<IRequest, GetEnabledTTSVoices>();
s.AddSingleton<IRequest, GetGroups>();
s.AddSingleton<IRequest, GetGroupPermissions>();
s.AddSingleton<IRequest, GetPermissions>(); s.AddSingleton<IRequest, GetPermissions>();
s.AddSingleton<IRequest, GetRedeemableActions>(); s.AddSingleton<IRequest, GetRedeemableActions>();
s.AddSingleton<IRequest, GetRedemptions>(); s.AddSingleton<IRequest, GetRedemptions>();

View File

@ -23,7 +23,7 @@ namespace HermesSocketServer.Store
public override async Task Load() public override async Task Load()
{ {
var data = new Dictionary<string, object>() { { "user", _userId } }; var data = new Dictionary<string, object>() { { "user", _userId } };
string sql = $"SELECT name, type, data FROM \"Action\" WHERE \"userId\" = @user"; string sql = $"SELECT name, type, has_message, data FROM \"Action\" WHERE \"userId\" = @user";
await _database.Execute(sql, data, (reader) => await _database.Execute(sql, data, (reader) =>
{ {
var name = reader.GetString(0); var name = reader.GetString(0);
@ -32,7 +32,8 @@ namespace HermesSocketServer.Store
UserId = _userId, UserId = _userId,
Name = name, Name = name,
Type = reader.GetString(1), Type = reader.GetString(1),
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(reader.GetString(2))! HasMessage = reader.GetBoolean(2),
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(reader.GetString(3))!
}); });
}); });
_logger.Information($"Loaded {_store.Count} redeemable actions from database."); _logger.Information($"Loaded {_store.Count} redeemable actions from database.");

View File

@ -7,14 +7,16 @@ namespace HermesSocketServer.Store
public class GroupPermissionStore : AutoSavedStore<string, GroupPermission> public class GroupPermissionStore : AutoSavedStore<string, GroupPermission>
{ {
private readonly string _userId; private readonly string _userId;
private readonly IStore<string, Group> _groups;
private readonly Database _database; private readonly Database _database;
private readonly Serilog.ILogger _logger; private readonly Serilog.ILogger _logger;
public GroupPermissionStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger) public GroupPermissionStore(string userId, DatabaseTable table, IStore<string, Group> groups, Database database, Serilog.ILogger logger)
: base(table, database, logger) : base(table, database, logger)
{ {
_userId = userId; _userId = userId;
_groups = groups;
_database = database; _database = database;
_logger = logger; _logger = logger;
} }
@ -30,7 +32,7 @@ namespace HermesSocketServer.Store
{ {
Id = id, Id = id,
UserId = _userId, UserId = _userId,
GroupId = reader.GetGuid(1).ToString(), GroupId = reader.GetGuid(1),
Path = reader.GetString(2), Path = reader.GetString(2),
Allow = await reader.IsDBNullAsync(3) ? null : reader.GetBoolean(3), Allow = await reader.IsDBNullAsync(3) ? null : reader.GetBoolean(3),
}); });
@ -43,15 +45,18 @@ namespace HermesSocketServer.Store
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key)); ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value)); ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId)); ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.GroupId, nameof(value.GroupId)); ArgumentNullException.ThrowIfNull(value.GroupId, nameof(value.GroupId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Path, nameof(value.Path)); ArgumentException.ThrowIfNullOrWhiteSpace(value.Path, nameof(value.Path));
if (_groups.Get(value.GroupId.ToString()) == null)
throw new ArgumentException("The group id does not exist.");
} }
protected override void OnInitialModify(string key, GroupPermission oldValue, GroupPermission newValue) protected override void OnInitialModify(string key, GroupPermission oldValue, GroupPermission newValue)
{ {
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue)); ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId)); ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GroupId, nameof(newValue.GroupId)); ArgumentNullException.ThrowIfNull(newValue.GroupId, nameof(newValue.GroupId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Path, nameof(newValue.Path)); ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Path, nameof(newValue.Path));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId));

View File

@ -7,7 +7,7 @@ namespace HermesSocketServer.Store
Task Load(); Task Load();
bool Modify(K? key, Action<V> modify); bool Modify(K? key, Action<V> modify);
bool Modify(K? key, V value); bool Modify(K? key, V value);
bool Remove(K? key); bool Remove(K? key, bool fromCascade = false);
Task Save(); Task Save();
bool Set(K? key, V value); bool Set(K? key, V value);
} }

View File

@ -87,7 +87,7 @@ namespace HermesSocketServer.Store.Internal
return false; return false;
} }
public bool Remove(K? key) public bool Remove(K? key, bool fromCascade = false)
{ {
if (key == null) if (key == null)
return false; return false;
@ -102,7 +102,7 @@ namespace HermesSocketServer.Store.Internal
if (!_added.Remove(key)) if (!_added.Remove(key))
{ {
_modified.Remove(key); _modified.Remove(key);
if (!_deleted.Contains(key)) if (!fromCascade && !_deleted.Contains(key))
{ {
_deleted.Add(key); _deleted.Add(key);
} }

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Messages; using HermesSocketServer.Messages;
using HermesSocketServer.Store.Internal; using HermesSocketServer.Store.Internal;
@ -7,14 +8,16 @@ namespace HermesSocketServer.Store
public class PolicyStore : AutoSavedStore<string, Policy> public class PolicyStore : AutoSavedStore<string, Policy>
{ {
private readonly string _userId; private readonly string _userId;
private readonly IStore<string, Group> _groups;
private readonly Database _database; private readonly Database _database;
private readonly Serilog.ILogger _logger; private readonly Serilog.ILogger _logger;
public PolicyStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger) public PolicyStore(string userId, DatabaseTable table, IStore<string, Group> groups, Database database, Serilog.ILogger logger)
: base(table, database, logger) : base(table, database, logger)
{ {
_userId = userId; _userId = userId;
_groups = groups;
_database = database; _database = database;
_logger = logger; _logger = logger;
} }
@ -50,6 +53,9 @@ namespace HermesSocketServer.Store
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Span, nameof(value.Span)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Span, nameof(value.Span));
ArgumentOutOfRangeException.ThrowIfLessThan(value.Span, 1000, nameof(value.Span)); ArgumentOutOfRangeException.ThrowIfLessThan(value.Span, 1000, nameof(value.Span));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Span, 86400, nameof(value.Span)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Span, 86400, nameof(value.Span));
if (_groups.Get(value.GroupId.ToString()) == null)
throw new ArgumentException("The group id does not exist.");
} }
protected override void OnInitialModify(string key, Policy oldValue, Policy newValue) protected override void OnInitialModify(string key, Policy oldValue, Policy newValue)

View File

@ -7,14 +7,16 @@ namespace HermesSocketServer.Store
public class RedemptionStore : AutoSavedStore<string, Redemption> public class RedemptionStore : AutoSavedStore<string, Redemption>
{ {
private readonly string _userId; private readonly string _userId;
private readonly IStore<string, RedeemableAction> _actions;
private readonly Database _database; private readonly Database _database;
private readonly Serilog.ILogger _logger; private readonly Serilog.ILogger _logger;
public RedemptionStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger) public RedemptionStore(string userId, DatabaseTable table, IStore<string, RedeemableAction> actions, Database database, Serilog.ILogger logger)
: base(table, database, logger) : base(table, database, logger)
{ {
_userId = userId; _userId = userId;
_actions = actions;
_database = database; _database = database;
_logger = logger; _logger = logger;
} }
@ -50,6 +52,9 @@ namespace HermesSocketServer.Store
ArgumentNullException.ThrowIfNull(value.Order, nameof(value.Order)); ArgumentNullException.ThrowIfNull(value.Order, nameof(value.Order));
ArgumentOutOfRangeException.ThrowIfNegative(value.Order, nameof(value.Order)); ArgumentOutOfRangeException.ThrowIfNegative(value.Order, nameof(value.Order));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Order, 99, nameof(value.Order)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Order, 99, nameof(value.Order));
if (_actions.Get(value.ActionName) == null)
throw new ArgumentException("The action name does not exist.");
} }
protected override void OnInitialModify(string key, Redemption oldValue, Redemption newValue) protected override void OnInitialModify(string key, Redemption oldValue, Redemption newValue)
@ -64,6 +69,9 @@ namespace HermesSocketServer.Store
ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Order, 99, nameof(newValue.Order)); ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Order, 99, nameof(newValue.Order));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(newValue.Id)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(newValue.Id));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(newValue.UserId)); ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(newValue.UserId));
if (oldValue.ActionName != newValue.ActionName && _actions.Get(newValue.ActionName) == null)
throw new ArgumentException("The action name does not exist.");
} }
protected override void OnPostRemove(string key, Redemption? value) protected override void OnPostRemove(string key, Redemption? value)

View File

@ -8,18 +8,21 @@ namespace HermesSocketServer.Store
public class VoiceStateStore : ComplexAutoSavedStore<string, TTSVoiceState> public class VoiceStateStore : ComplexAutoSavedStore<string, TTSVoiceState>
{ {
private readonly string _userId; private readonly string _userId;
private readonly IStore<string, TTSVoice> _voices;
private readonly VoiceIdValidator _idValidator; private readonly VoiceIdValidator _idValidator;
private readonly Database _database; private readonly Database _database;
private readonly Serilog.ILogger _logger; private readonly Serilog.ILogger _logger;
public VoiceStateStore(string userId, VoiceIdValidator voiceIdValidator, DatabaseTable table, Database database, Serilog.ILogger logger) public VoiceStateStore(string userId, DatabaseTable table, IStore<string, TTSVoice> voices, Database database, Serilog.ILogger logger)
: base(table, database, logger) : base(table, database, logger)
{ {
_userId = userId; _userId = userId;
_idValidator = voiceIdValidator; _voices = voices;
_database = database; _database = database;
_logger = logger; _logger = logger;
_idValidator = new VoiceIdValidator();
} }
public override async Task Load() public override async Task Load()
@ -47,6 +50,7 @@ namespace HermesSocketServer.Store
ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id)); ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId)); ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentNullException.ThrowIfNull(value.Enabled, nameof(value.Enabled)); ArgumentNullException.ThrowIfNull(value.Enabled, nameof(value.Enabled));
ArgumentNullException.ThrowIfNull(_voices.Get(value.Id));
} }
protected override void OnInitialModify(string key, TTSVoiceState oldValue, TTSVoiceState newValue) protected override void OnInitialModify(string key, TTSVoiceState oldValue, TTSVoiceState newValue)