Added stores for groups, group chatters and group permissions. Fixed some logging text.

This commit is contained in:
Tom
2025-01-16 23:17:04 +00:00
parent ee3f128a9f
commit 422cd91db2
10 changed files with 251 additions and 41 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

@ -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

@ -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

@ -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)

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

@ -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

@ -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

@ -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)

View File

@ -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)