Compare commits

...

2 Commits

30 changed files with 423 additions and 101 deletions

View File

@@ -7,6 +7,8 @@ namespace HermesSocketServer.Models
public required string Id { get; set; }
public required User User { get; set; }
public required ChatterStore Chatters { get; set; }
public required GroupStore Groups { get; set; }
public required GroupPermissionStore GroupPermissions { get; set; }
public required PolicyStore Policies { get; set; }
public required TTSFilterStore Filters { get; set; }
public required ActionStore Actions { get; set; }

View File

@@ -13,7 +13,7 @@ namespace HermesSocketServer.Requests
private ILogger _logger;
private Random _random;
public CreateTTSVoice(VoiceStore voices, ILogger logger)
public CreateTTSVoice(IStore<string, TTSVoice> voices, ILogger logger)
{
_voices = voices;
_logger = logger;

View File

@@ -12,7 +12,7 @@ namespace HermesSocketServer.Requests
private IStore<string, TTSVoice> _voices;
private ILogger _logger;
public DeleteTTSVoice(VoiceStore voices, ILogger logger)
public DeleteTTSVoice(IStore<string, TTSVoice> voices, ILogger logger)
{
_voices = voices;
_logger = logger;

View File

@@ -8,11 +8,11 @@ namespace HermesSocketServer.Requests
{
public string Name => "get_default_tts_voice";
public string[] RequiredKeys => [];
private readonly UserStore _users;
private readonly IStore<string, User> _users;
private readonly ServerConfiguration _configuration;
private readonly ILogger _logger;
public GetDefaultTTSVoice(UserStore users, ServerConfiguration configuration, ILogger logger)
public GetDefaultTTSVoice(IStore<string, User> users, ServerConfiguration configuration, ILogger logger)
{
_users = users;
_configuration = configuration;

View File

@@ -1,4 +1,3 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
@@ -9,54 +8,27 @@ namespace HermesSocketServer.Requests
{
public string Name => "get_permissions";
public string[] RequiredKeys => [];
private readonly Database _database;
private readonly ILogger _logger;
public GetPermissions(Database database, ILogger logger)
public GetPermissions(ILogger logger)
{
_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 groups = new List<Group>();
string sql = $"SELECT id, name, priority FROM \"Group\" WHERE \"userId\" = @user";
await _database.Execute(sql, temp, (r) => groups.Add(new Group()
{
Id = r.GetGuid(0).ToString("D"),
Name = r.GetString(1),
Priority = r.GetInt32(2)
}));
var groupChatters = new List<GroupChatter>();
sql = $"SELECT \"groupId\", \"chatterId\", \"chatterId\" FROM \"ChatterGroup\" WHERE \"userId\" = @user";
await _database.Execute(sql, temp, (r) => groupChatters.Add(new GroupChatter()
{
GroupId = r.GetGuid(0).ToString("D"),
ChatterId = r.GetInt32(1)
}));
var groupPermissions = new List<GroupPermission>();
sql = $"SELECT id, \"groupId\", \"path\", \"allow\" FROM \"GroupPermission\" WHERE \"userId\" = @user";
await _database.Execute(sql, temp, (r) => groupPermissions.Add(new GroupPermission()
{
Id = r.GetGuid(0).ToString("D"),
GroupId = r.GetGuid(1).ToString("D"),
Path = r.GetString(2),
Allow = r.GetBoolean(3)
}));
var groups = channel.Groups.Get().Values;
var groupChatters = channel.Groups.Chatters.Values.SelectMany(g => g.Get().Values);
var groupPermissions = channel.GroupPermissions.Get().Values;
_logger.Information($"Fetched all permissions for channel [channel: {channel.Id}]");
var info = new GroupInfo()
{
Groups = groups,
GroupChatters = groupChatters,
GroupPermissions = groupPermissions
GroupPermissions = groupPermissions,
};
return RequestResult.Successful(info, notifyClientsOnAccount: false);
return Task.FromResult(RequestResult.Successful(info, notifyClientsOnAccount: false));
}
}
}

View File

@@ -9,10 +9,10 @@ namespace HermesSocketServer.Requests
{
public string Name => "get_tts_voices";
public string[] RequiredKeys => [];
private VoiceStore _voices;
private IStore<string, TTSVoice> _voices;
private ILogger _logger;
public GetTTSVoices(VoiceStore voices, ILogger logger)
public GetTTSVoices(IStore<string, TTSVoice> voices, ILogger logger)
{
_voices = voices;
_logger = logger;

View File

@@ -8,10 +8,10 @@ namespace HermesSocketServer.Requests
{
public string Name => "update_default_tts_voice";
public string[] RequiredKeys => ["user", "voice"];
private UserStore _users;
private IStore<string, User> _users;
private ILogger _logger;
public UpdateDefaultTTSVoice(UserStore users, ILogger logger)
public UpdateDefaultTTSVoice(IStore<string, User> users, ILogger logger)
{
_users = users;
_logger = logger;

View File

@@ -41,11 +41,7 @@ namespace HermesSocketServer.Requests
Type = type,
};
bool result = channel.Actions.Modify(name, action =>
{
action.Type = type;
action.Data = dict;
});
bool result = channel.Actions.Modify(name, action);
if (result)
{
_logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]");

View File

@@ -1,3 +1,4 @@
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
@@ -25,16 +26,16 @@ namespace HermesSocketServer.Requests
return Task.FromResult(RequestResult.Failed("Order must be an integer."));
bool state = data["state"].ToString()?.ToLower() == "true";
bool result = channel.Redemptions.Modify(id, r =>
{
if (r.UserId != channel.Id)
return;
Redemption redemption = new Redemption() {
Id = id,
UserId = channel.Id,
RedemptionId = redemptionId,
ActionName = actionName,
Order = order,
State = state,
};
r.RedemptionId = redemptionId;
r.ActionName = actionName;
r.Order = order;
r.State = state;
});
bool result = channel.Redemptions.Modify(id, redemption);
var r = channel.Redemptions.Get(id);
if (result)

View File

@@ -35,12 +35,7 @@ namespace HermesSocketServer.Requests
Flag = flag,
};
bool result = channel.Filters.Modify(id, f => {
f.Search = search;
f.Replace = replace;
if (flag >= 0)
f.Flag = flag;
});
bool result = channel.Filters.Modify(id, filter);
if (result)
{

View File

@@ -12,7 +12,7 @@ namespace HermesSocketServer.Requests
private IStore<string, TTSVoice> _voices;
private ILogger _logger;
public UpdateTTSVoice(VoiceStore voices, ILogger logger)
public UpdateTTSVoice(IStore<string, TTSVoice> voices, ILogger logger)
{
_voices = voices;
_logger = logger;

View File

@@ -7,14 +7,14 @@ namespace HermesSocketServer.Services
{
public class ChannelManager
{
private readonly UserStore _users;
private readonly IStore<string, User> _users;
private readonly Database _database;
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
private readonly IDictionary<string, Channel> _channels;
private readonly object _lock;
public ChannelManager(UserStore users, Database database, ServerConfiguration configuration, Serilog.ILogger logger)
public ChannelManager(IStore<string, User> users, Database database, ServerConfiguration configuration, Serilog.ILogger logger)
{
_users = users;
_database = database;
@@ -38,11 +38,16 @@ namespace HermesSocketServer.Services
var actionTable = _configuration.Database.Tables["Action"];
var chatterTable = _configuration.Database.Tables["Chatter"];
//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 chatters = new ChatterStore(userId, chatterTable, _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);
@@ -50,6 +55,8 @@ namespace HermesSocketServer.Services
Task.WaitAll([
chatters.Load(),
groups.Load(),
groupPermissions.Load(),
policies.Load(),
filters.Load(),
actions.Load(),
@@ -61,6 +68,8 @@ namespace HermesSocketServer.Services
Id = userId,
User = user,
Chatters = chatters,
Groups = groups,
GroupPermissions = groupPermissions,
Policies = policies,
Filters = filters,
Actions = actions,

View File

@@ -1,4 +1,6 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using HermesSocketServer.Store;
namespace HermesSocketServer.Services
@@ -6,12 +8,12 @@ namespace HermesSocketServer.Services
public class DatabaseService : BackgroundService
{
private readonly ChannelManager _channels;
private readonly VoiceStore _voices;
private readonly UserStore _users;
private readonly IStore<string, TTSVoice> _voices;
private readonly IStore<string, User> _users;
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
public DatabaseService(ChannelManager channels, VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger)
public DatabaseService(ChannelManager channels, IStore<string, TTSVoice> voices, IStore<string, User> users, ServerConfiguration configuration, Serilog.ILogger logger)
{
_channels = channels;
_voices = voices;

View File

@@ -12,14 +12,14 @@ namespace HermesSocketServer.Socket.Handlers
public int OperationCode { get; } = 1;
private readonly ChannelManager _manager;
private readonly VoiceStore _voices;
private readonly IStore<string, TTSVoice> _voices;
private readonly ServerConfiguration _configuration;
private readonly Database _database;
private readonly HermesSocketManager _sockets;
private readonly ILogger _logger;
private readonly object _lock;
public HermesLoginHandler(ChannelManager manager, VoiceStore voices, ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
public HermesLoginHandler(ChannelManager manager, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Database database, HermesSocketManager sockets, ILogger logger)
{
_manager = manager;
_voices = voices;

View File

@@ -17,6 +17,8 @@ using HermesSocketServer.Store;
using HermesSocketServer.Services;
using HermesSocketServer.Store.Internal;
using Microsoft.Extensions.DependencyInjection;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
var yamlDeserializer = new DeserializerBuilder()
@@ -88,8 +90,8 @@ if (configuration.Database.Tables != null)
}
// Stores
s.AddSingleton<VoiceStore>();
s.AddSingleton<UserStore>();
s.AddSingleton<IStore<string, TTSVoice>, VoiceStore>();
s.AddSingleton<IStore<string, User>, UserStore>();
// Request handlers
s.AddSingleton<IRequest, CreatePolicy>();

View File

@@ -27,7 +27,7 @@ namespace HermesSocketServer.Store
await _database.Execute(sql, data, (reader) =>
{
var name = reader.GetString(0);
_store.Add(name.ToString(), new RedeemableAction()
_store.Add(name, new RedeemableAction()
{
UserId = _userId,
Name = name,
@@ -35,15 +35,28 @@ namespace HermesSocketServer.Store
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(reader.GetString(2))!
});
});
_logger.Information($"Loaded {_store.Count} TTS chatter voices from database.");
_logger.Information($"Loaded {_store.Count} redeemable actions from database.");
}
protected override void OnInitialAdd(string key, RedeemableAction value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Name, nameof(value.Name));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Type, nameof(value.Type));
ArgumentNullException.ThrowIfNull(value.Data, nameof(value.Data));
}
protected override void OnInitialModify(string key, RedeemableAction value)
protected override void OnInitialModify(string key, RedeemableAction oldValue, RedeemableAction newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Name, nameof(newValue.Name));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Type, nameof(newValue.Type));
ArgumentNullException.ThrowIfNull(newValue.Data, nameof(newValue.Data));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Name, newValue.Name, nameof(oldValue.Name));
}
}
}

View File

@@ -0,0 +1,66 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Store.Internal;
namespace HermesSocketServer.Store
{
public class ChatterGroupStore : ComplexAutoSavedStore<string, GroupChatter>
{
private readonly string _userId;
private readonly string _groupId;
private readonly Database _database;
private readonly Serilog.ILogger _logger;
public string GroupId { get => _groupId; }
public ChatterGroupStore(string userId, string groupId, DatabaseTable table, Database database, Serilog.ILogger logger)
: base(table, database, logger)
{
_userId = userId;
_groupId = groupId;
_database = database;
_logger = logger;
}
public override async Task Load()
{
var data = new Dictionary<string, object>() { { "user", _userId }, { "group", new Guid(_groupId) } };
string sql = $"SELECT \"chatterId\", \"chatterLabel\" FROM \"ChatterGroup\" WHERE \"userId\" = @user AND \"groupId\" = @group";
await _database.Execute(sql, data, (reader) =>
{
var chatterId = reader.GetInt32(0).ToString();
lock (_lock)
{
_store.Add(chatterId, new GroupChatter()
{
UserId = _userId,
GroupId = _groupId,
ChatterId = reader.GetInt32(0),
ChatterLabel = reader.GetString(1),
});
}
});
_logger.Information($"Loaded {_store.Count} group chatters from database [group id: {_groupId}]");
}
protected override void OnInitialAdd(string key, GroupChatter value)
{
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.ChatterId, nameof(value.ChatterId));
}
protected override void OnInitialModify(string key, GroupChatter oldValue, GroupChatter newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GroupId, nameof(newValue.GroupId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.GroupId, nameof(newValue.GroupId));
ArgumentNullException.ThrowIfNull(newValue.ChatterId, nameof(newValue.ChatterId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.GroupId, newValue.GroupId, nameof(oldValue.GroupId));
}
}
}

View File

@@ -38,10 +38,21 @@ namespace HermesSocketServer.Store
protected override void OnInitialAdd(string key, ChatterVoice value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.ChatterId, nameof(value.ChatterId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.VoiceId, nameof(value.VoiceId));
}
protected override void OnInitialModify(string key, ChatterVoice value)
protected override void OnInitialModify(string key, ChatterVoice oldValue, ChatterVoice newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newValue.ChatterId, nameof(newValue.ChatterId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.VoiceId, nameof(newValue.VoiceId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.ChatterId, newValue.ChatterId, nameof(oldValue.ChatterId));
}
}
}

View File

@@ -0,0 +1,64 @@
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Store.Internal;
namespace HermesSocketServer.Store
{
public class GroupPermissionStore : AutoSavedStore<string, GroupPermission>
{
private readonly string _userId;
private readonly Database _database;
private readonly Serilog.ILogger _logger;
public GroupPermissionStore(string userId, DatabaseTable table, Database database, Serilog.ILogger logger)
: base(table, database, logger)
{
_userId = userId;
_database = database;
_logger = logger;
}
public override async Task Load()
{
var data = new Dictionary<string, object>() { { "user", _userId } };
string sql = $"SELECT id, \"groupId\", path, allow FROM \"GroupPermission\" WHERE \"userId\" = @user";
await _database.Execute(sql, data, async (reader) =>
{
var id = reader.GetGuid(0).ToString();
_store.Add(id, new GroupPermission()
{
Id = id,
UserId = _userId,
GroupId = reader.GetGuid(1).ToString(),
Path = reader.GetString(2),
Allow = await reader.IsDBNullAsync(3) ? null : reader.GetBoolean(3),
});
});
_logger.Information($"Loaded {_store.Count} group permissions from database.");
}
protected override void OnInitialAdd(string key, GroupPermission value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.GroupId, nameof(value.GroupId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Path, nameof(value.Path));
}
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));
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));
}
protected override void OnPostRemove(string key, GroupPermission value)
{
}
}
}

97
Store/GroupStore.cs Normal file
View File

@@ -0,0 +1,97 @@
using System.Collections.Concurrent;
using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Store.Internal;
namespace HermesSocketServer.Store
{
public class GroupStore : AutoSavedStore<string, Group>
{
private static readonly string[] AUTO_GENERATED_GROUP_NAMES = ["everyone", "subscribers", "vip", "moderators", "broadcaster"];
private IDictionary<string, ChatterGroupStore> _chatters;
private readonly string _userId;
private readonly Database _database;
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
public IDictionary<string, ChatterGroupStore> Chatters { get => _chatters; }
public GroupStore(string userId, DatabaseTable table, Database database, ServerConfiguration configuration, Serilog.ILogger logger)
: base(table, database, logger)
{
_userId = userId;
_database = database;
_logger = logger;
_configuration = configuration;
_chatters = new ConcurrentDictionary<string, ChatterGroupStore>();
}
public override async Task Load()
{
var groups = new List<(string, string)>();
var data = new Dictionary<string, object>() { { "user", _userId } };
string sql = $"SELECT id, name, priority FROM \"Group\" WHERE \"userId\" = @user";
await _database.Execute(sql, data, (reader) =>
{
var id = reader.GetGuid(0).ToString();
var name = reader.GetString(1);
_store.Add(id, new Group()
{
Id = id,
UserId = _userId,
Name = name,
Priority = reader.GetInt32(2),
});
groups.Add((id, name));
});
_logger.Information($"Loaded {_store.Count} groups from database.");
// Load Chatter Groups
var chatterGroupTable = _configuration.Database.Tables["ChatterGroup"];
var groupChatters = groups.Select(group => new ChatterGroupStore(_userId, group.Item1, chatterGroupTable, _database, _logger)).ToArray();
List<Task> tasks = new List<Task>();
for (var i = 0; i < groups.Count; i++)
{
var store = groupChatters[i];
if (AUTO_GENERATED_GROUP_NAMES.All(n => n != groups[i].Item2))
tasks.Add(store.Load());
_chatters.Add(store.GroupId, store);
}
await Task.WhenAll(tasks);
}
public override async Task Save()
{
List<Task> tasks = _chatters.Values.Select(c => c.Save()).ToList();
tasks.Add(base.Save());
await Task.WhenAll(base.Save());
}
protected override void OnInitialAdd(string key, Group value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Name, nameof(value.Name));
ArgumentNullException.ThrowIfNull(value.Priority, nameof(value.Priority));
}
protected override void OnInitialModify(string key, Group oldValue, Group newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Name, nameof(newValue.Name));
ArgumentNullException.ThrowIfNull(newValue.Priority, nameof(newValue.Priority));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
if (AUTO_GENERATED_GROUP_NAMES.Any(s => s == oldValue.Name))
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Name, newValue.Name, nameof(oldValue.Name));
}
protected override void OnPostRemove(string key, Group value)
{
}
}
}

View File

@@ -5,9 +5,10 @@ namespace HermesSocketServer.Store
V? Get(K key);
IDictionary<K, V> Get();
Task Load();
bool Modify(K? key, Action<V> action);
bool Modify(K? key, Action<V> modify);
bool Modify(K? key, V value);
bool Remove(K? key);
Task Save();
bool Set(K? key, V? value);
bool Set(K? key, V value);
}
}

View File

@@ -48,7 +48,7 @@ namespace HermesSocketServer.Store.Internal
async (query, list) => await _generator.DoPreparedStatement(_database, query, list, _table.KeyColumns));
}
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V>, Task<int>> execute)
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V?>, Task<int>> execute)
{
ImmutableList<K>? list = null;
lock (_lock)

View File

@@ -18,7 +18,8 @@ namespace HermesSocketServer.Store.Internal
public GroupSaveSqlGenerator(IDictionary<string, string> columnsToProperties, IDictionary<string, string> columnTypes, Serilog.ILogger logger)
{
_columnPropertyRelations = columnsToProperties.ToDictionary(p => p.Key, p => typeof(T).GetProperty(p.Value));
var type = typeof(T);
_columnPropertyRelations = columnsToProperties.ToDictionary(p => p.Key, p => type.GetProperty(p.Value));
_columnTypes = columnTypes;
_logger = logger;

View File

@@ -24,7 +24,7 @@ namespace HermesSocketServer.Store.Internal
public abstract Task Load();
protected abstract void OnInitialAdd(K key, V value);
protected abstract void OnInitialModify(K key, V value);
protected abstract void OnInitialModify(K key, V value, V newValue);
protected abstract void OnPostRemove(K key, V value);
public abstract Task Save();
@@ -46,7 +46,28 @@ namespace HermesSocketServer.Store.Internal
}
}
public bool Modify(K? key, Action<V> action)
public bool Modify(K? key, V value)
{
if (key == null)
return false;
lock (_lock)
{
if (_store.TryGetValue(key, out V? oldValue))
{
OnInitialModify(key, oldValue, value);
_store[key] = value;
if (!_added.Contains(key) && !_modified.Contains(key))
{
_modified.Add(key);
}
return true;
}
}
return false;
}
public bool Modify(K? key, Action<V> modify)
{
if (key == null)
return false;
@@ -55,11 +76,7 @@ namespace HermesSocketServer.Store.Internal
{
if (_store.TryGetValue(key, out V? value))
{
if (value == null)
return false;
OnInitialModify(key, value);
action(value);
modify(value);
if (!_added.Contains(key) && !_modified.Contains(key))
{
_modified.Add(key);
@@ -97,9 +114,9 @@ namespace HermesSocketServer.Store.Internal
return false;
}
public bool Set(K? key, V? value)
public bool Set(K? key, V value)
{
if (key == null || value == null)
if (key == null)
return false;
lock (_lock)
@@ -108,7 +125,7 @@ namespace HermesSocketServer.Store.Internal
{
if (fetched != value)
{
OnInitialModify(key, value);
OnInitialModify(key, fetched, value);
_store[key] = value;
if (!_added.Contains(key) && !_modified.Contains(key))
{

View File

@@ -41,10 +41,29 @@ namespace HermesSocketServer.Store
protected override void OnInitialAdd(string key, Policy value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentOutOfRangeException.ThrowIfEqual(value.GroupId, default, nameof(value.GroupId));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Usage, nameof(value.Usage));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Usage, 99, nameof(value.Usage));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value.Span, nameof(value.Span));
ArgumentOutOfRangeException.ThrowIfLessThan(value.Span, 1000, nameof(value.Span));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Span, 86400, nameof(value.Span));
}
protected override void OnInitialModify(string key, Policy value)
protected override void OnInitialModify(string key, Policy oldValue, Policy newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentOutOfRangeException.ThrowIfEqual(newValue.GroupId, default, nameof(newValue.GroupId));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newValue.Usage, nameof(newValue.Usage));
ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Usage, 99, nameof(newValue.Usage));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newValue.Span, nameof(newValue.Span));
ArgumentOutOfRangeException.ThrowIfLessThan(newValue.Span, 1000, nameof(newValue.Span));
ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Span, 86400, nameof(newValue.Span));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
}
protected override void OnPostRemove(string key, Policy value)

View File

@@ -36,18 +36,37 @@ namespace HermesSocketServer.Store
ActionName = reader.GetString(4),
});
});
_logger.Information($"Loaded {_store.Count} TTS chatter voices from database.");
_logger.Information($"Loaded {_store.Count} redemptions from database.");
}
protected override void OnInitialAdd(string key, Redemption value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.RedemptionId, nameof(value.RedemptionId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.ActionName, nameof(value.ActionName));
ArgumentNullException.ThrowIfNull(value.State, nameof(value.State));
ArgumentNullException.ThrowIfNull(value.Order, nameof(value.Order));
ArgumentOutOfRangeException.ThrowIfNegative(value.Order, nameof(value.Order));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Order, 99, nameof(value.Order));
}
protected override void OnInitialModify(string key, Redemption value)
protected override void OnInitialModify(string key, Redemption oldValue, Redemption newValue)
{
ArgumentNullException.ThrowIfNull(newValue);
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.RedemptionId, nameof(newValue.RedemptionId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.ActionName, nameof(newValue.ActionName));
ArgumentNullException.ThrowIfNull(newValue.State, nameof(newValue.State));
ArgumentNullException.ThrowIfNull(newValue.Order, nameof(newValue.Order));
ArgumentOutOfRangeException.ThrowIfNegative(newValue.Order, nameof(newValue.Order));
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));
}
protected override void OnPostRemove(string key, Redemption value)
protected override void OnPostRemove(string key, Redemption? value)
{
}
}

View File

@@ -35,15 +35,32 @@ namespace HermesSocketServer.Store
Flag = reader.GetInt32(3),
});
});
_logger.Information($"Loaded {_store.Count} TTS chatter voices from database.");
_logger.Information($"Loaded {_store.Count} TTS filters from database.");
}
protected override void OnInitialAdd(string key, TTSWordFilter value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Search, nameof(value.Search));
ArgumentNullException.ThrowIfNull(value.Replace, nameof(value.Replace));
ArgumentOutOfRangeException.ThrowIfNegative(value.Flag, nameof(value.Flag));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Flag, 2047, nameof(value.Flag));
}
protected override void OnInitialModify(string key, TTSWordFilter value)
protected override void OnInitialModify(string key, TTSWordFilter oldValue, TTSWordFilter newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Id, nameof(newValue.Id));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Search, nameof(newValue.Search));
ArgumentNullException.ThrowIfNull(newValue.Replace, nameof(newValue.Replace));
ArgumentOutOfRangeException.ThrowIfNegative(newValue.Flag, nameof(newValue.Flag));
ArgumentOutOfRangeException.ThrowIfGreaterThan(newValue.Flag, 2047, nameof(newValue.Flag));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
}
protected override void OnPostRemove(string key, TTSWordFilter value)

View File

@@ -39,7 +39,7 @@ namespace HermesSocketServer.Store
{
}
protected override void OnInitialModify(string key, User value)
protected override void OnInitialModify(string key, User oldValue, User newValue)
{
}

View File

@@ -41,11 +41,23 @@ namespace HermesSocketServer.Store
protected override void OnInitialAdd(string key, TTSVoiceState value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
_idValidator.Check(value.Id);
ArgumentNullException.ThrowIfNull(value, nameof(value));
ArgumentException.ThrowIfNullOrWhiteSpace(value.Id, nameof(value.Id));
ArgumentException.ThrowIfNullOrWhiteSpace(value.UserId, nameof(value.UserId));
ArgumentNullException.ThrowIfNull(value.Enabled, nameof(value.Enabled));
}
protected override void OnInitialModify(string key, TTSVoiceState value)
protected override void OnInitialModify(string key, TTSVoiceState oldValue, TTSVoiceState newValue)
{
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.Id, nameof(newValue.Id));
ArgumentException.ThrowIfNullOrWhiteSpace(newValue.UserId, nameof(newValue.UserId));
ArgumentNullException.ThrowIfNull(newValue.Enabled, nameof(newValue.Enabled));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id));
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.UserId, newValue.UserId, nameof(oldValue.UserId));
ArgumentOutOfRangeException.ThrowIfEqual(oldValue.Enabled, newValue.Enabled, nameof(oldValue.Enabled));
}
protected override void OnPostRemove(string key, TTSVoiceState value)

View File

@@ -39,13 +39,19 @@ namespace HermesSocketServer.Store
protected override void OnInitialAdd(string key, TTSVoice value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key));
ArgumentNullException.ThrowIfNull(value, nameof(value));
_idValidator.Check(value.Id);
_nameValidator.Check(value.Name);
}
protected override void OnInitialModify(string key, TTSVoice value)
protected override void OnInitialModify(string key, TTSVoice oldValue, TTSVoice newValue)
{
_nameValidator.Check(value.Name);
ArgumentNullException.ThrowIfNull(newValue, nameof(newValue));
_idValidator.Check(newValue.Id);
_nameValidator.Check(newValue.Name);
ArgumentOutOfRangeException.ThrowIfNotEqual(oldValue.Id, newValue.Id, nameof(oldValue.Id));
ArgumentOutOfRangeException.ThrowIfEqual(oldValue.Name, newValue.Name, nameof(oldValue.Name));
}
protected override void OnPostRemove(string key, TTSVoice value)