Added stores for groups, group chatters and group permissions. Fixed some logging text.
This commit is contained in:
@ -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,7 +35,7 @@ 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)
|
||||
|
66
Store/ChatterGroupStore.cs
Normal file
66
Store/ChatterGroupStore.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
64
Store/GroupPermissionStore.cs
Normal file
64
Store/GroupPermissionStore.cs
Normal 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
97
Store/GroupStore.cs
Normal 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -36,7 +36,7 @@ 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)
|
||||
|
@ -1,4 +1,3 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Store.Internal;
|
||||
@ -36,7 +35,7 @@ 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)
|
||||
|
Reference in New Issue
Block a user