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:
		| @@ -23,7 +23,7 @@ namespace HermesSocketServer.Store | ||||
|         public override async Task Load() | ||||
|         { | ||||
|             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) => | ||||
|             { | ||||
|                 var name = reader.GetString(0); | ||||
| @@ -32,7 +32,8 @@ namespace HermesSocketServer.Store | ||||
|                     UserId = _userId, | ||||
|                     Name = name, | ||||
|                     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."); | ||||
|   | ||||
| @@ -7,14 +7,16 @@ namespace HermesSocketServer.Store | ||||
|     public class GroupPermissionStore : AutoSavedStore<string, GroupPermission> | ||||
|     { | ||||
|         private readonly string _userId; | ||||
|         private readonly IStore<string, Group> _groups; | ||||
|         private readonly Database _database; | ||||
|         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) | ||||
|         { | ||||
|             _userId = userId; | ||||
|             _groups = groups; | ||||
|             _database = database; | ||||
|             _logger = logger; | ||||
|         } | ||||
| @@ -30,7 +32,7 @@ namespace HermesSocketServer.Store | ||||
|                 { | ||||
|                     Id = id, | ||||
|                     UserId = _userId, | ||||
|                     GroupId = reader.GetGuid(1).ToString(), | ||||
|                     GroupId = reader.GetGuid(1), | ||||
|                     Path = reader.GetString(2), | ||||
|                     Allow = await reader.IsDBNullAsync(3) ? null : reader.GetBoolean(3), | ||||
|                 }); | ||||
| @@ -43,15 +45,18 @@ namespace HermesSocketServer.Store | ||||
|             ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key)); | ||||
|             ArgumentNullException.ThrowIfNull(value, nameof(value)); | ||||
|             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)); | ||||
|  | ||||
|             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) | ||||
|         { | ||||
|             ArgumentNullException.ThrowIfNull(newValue, nameof(newValue)); | ||||
|             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)); | ||||
|             ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId)); | ||||
|             ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId)); | ||||
|   | ||||
| @@ -7,7 +7,7 @@ namespace HermesSocketServer.Store | ||||
|         Task Load(); | ||||
|         bool Modify(K? key, Action<V> modify); | ||||
|         bool Modify(K? key, V value); | ||||
|         bool Remove(K? key); | ||||
|         bool Remove(K? key, bool fromCascade = false); | ||||
|         Task Save(); | ||||
|         bool Set(K? key, V value); | ||||
|     } | ||||
|   | ||||
| @@ -87,7 +87,7 @@ namespace HermesSocketServer.Store.Internal | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         public bool Remove(K? key) | ||||
|         public bool Remove(K? key, bool fromCascade = false) | ||||
|         { | ||||
|             if (key == null) | ||||
|                 return false; | ||||
| @@ -102,7 +102,7 @@ namespace HermesSocketServer.Store.Internal | ||||
|                         if (!_added.Remove(key)) | ||||
|                         { | ||||
|                             _modified.Remove(key); | ||||
|                             if (!_deleted.Contains(key)) | ||||
|                             if (!fromCascade && !_deleted.Contains(key)) | ||||
|                             { | ||||
|                                 _deleted.Add(key); | ||||
|                             } | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| using HermesSocketLibrary.db; | ||||
| using HermesSocketLibrary.Requests.Messages; | ||||
| using HermesSocketServer.Messages; | ||||
| using HermesSocketServer.Store.Internal; | ||||
|  | ||||
| @@ -7,14 +8,16 @@ namespace HermesSocketServer.Store | ||||
|     public class PolicyStore : AutoSavedStore<string, Policy> | ||||
|     { | ||||
|         private readonly string _userId; | ||||
|         private readonly IStore<string, Group> _groups; | ||||
|         private readonly Database _database; | ||||
|         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) | ||||
|         { | ||||
|             _userId = userId; | ||||
|             _groups = groups; | ||||
|             _database = database; | ||||
|             _logger = logger; | ||||
|         } | ||||
| @@ -50,6 +53,9 @@ namespace HermesSocketServer.Store | ||||
|             ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Span, nameof(value.Span)); | ||||
|             ArgumentOutOfRangeException.ThrowIfLessThan(value.Span, 1000, 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) | ||||
|   | ||||
| @@ -7,14 +7,16 @@ namespace HermesSocketServer.Store | ||||
|     public class RedemptionStore : AutoSavedStore<string, Redemption> | ||||
|     { | ||||
|         private readonly string _userId; | ||||
|         private readonly IStore<string, RedeemableAction> _actions; | ||||
|         private readonly Database _database; | ||||
|         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) | ||||
|         { | ||||
|             _userId = userId; | ||||
|             _actions = actions; | ||||
|             _database = database; | ||||
|             _logger = logger; | ||||
|         } | ||||
| @@ -50,6 +52,9 @@ namespace HermesSocketServer.Store | ||||
|             ArgumentNullException.ThrowIfNull(value.Order, nameof(value.Order)); | ||||
|             ArgumentOutOfRangeException.ThrowIfNegative(value.Order, 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) | ||||
| @@ -64,6 +69,9 @@ namespace HermesSocketServer.Store | ||||
|             ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Order, 99, nameof(newValue.Order)); | ||||
|             ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(newValue.Id)); | ||||
|             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) | ||||
|   | ||||
| @@ -8,18 +8,21 @@ namespace HermesSocketServer.Store | ||||
|     public class VoiceStateStore : ComplexAutoSavedStore<string, TTSVoiceState> | ||||
|     { | ||||
|         private readonly string _userId; | ||||
|         private readonly IStore<string, TTSVoice> _voices; | ||||
|         private readonly VoiceIdValidator _idValidator; | ||||
|         private readonly Database _database; | ||||
|         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) | ||||
|         { | ||||
|             _userId = userId; | ||||
|             _idValidator = voiceIdValidator; | ||||
|             _voices = voices; | ||||
|             _database = database; | ||||
|             _logger = logger; | ||||
|  | ||||
|             _idValidator = new VoiceIdValidator(); | ||||
|         } | ||||
|  | ||||
|         public override async Task Load() | ||||
| @@ -47,6 +50,7 @@ namespace HermesSocketServer.Store | ||||
|             ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id)); | ||||
|             ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId)); | ||||
|             ArgumentNullException.ThrowIfNull(value.Enabled, nameof(value.Enabled)); | ||||
|             ArgumentNullException.ThrowIfNull(_voices.Get(value.Id)); | ||||
|         } | ||||
|  | ||||
|         protected override void OnInitialModify(string key, TTSVoiceState oldValue, TTSVoiceState newValue) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user