using HermesSocketLibrary.db; using HermesSocketLibrary.Requests.Messages; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class GetPermissions : IRequest { public string Name => "get_permissions"; public string[] RequiredKeys => []; private readonly Database _database; private readonly ILogger _logger; public GetPermissions(Database database, ILogger logger) { _database = database; _logger = logger; } public async Task Grant(string sender, IDictionary? data) { var temp = new Dictionary() { { "user", sender } }; var groups = new List(); 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(); 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(); 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) })); _logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); var info = new GroupInfo() { Groups = groups, GroupChatters = groupChatters, GroupPermissions = groupPermissions }; return RequestResult.Successful(info, notifyClientsOnAccount: false); } } }