hermes-client/Hermes/Socket/Requests/GetPermissionsAck.cs

67 lines
2.8 KiB
C#

using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
using TwitchChatTTS.Chat.Groups.Permissions;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class GetPermissionsAck : IRequestAck
{
public string Name => "get_permissions";
private readonly IGroupPermissionManager _permissions;
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public GetPermissionsAck(
IGroupPermissionManager permissions,
IChatterGroupManager groups,
JsonSerializerOptions options,
ILogger logger)
{
_permissions = permissions;
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
var groupInfo = JsonSerializer.Deserialize<GroupInfo>(json, _options);
if (groupInfo == null)
{
_logger.Error("Failed to load groups & permissions.");
return;
}
_permissions.Clear();
_groups.Clear();
var groupsById = groupInfo.Groups.ToDictionary(g => g.Id, g => g);
foreach (var group in groupInfo.Groups)
_groups.Add(group);
foreach (var permission in groupInfo.GroupPermissions)
{
_logger.Debug($"Adding group permission [permission id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}][allow: {permission.Allow?.ToString() ?? "null"}]");
if (!groupsById.TryGetValue(permission.GroupId, out var group))
{
_logger.Warning($"Failed to find group by id [permission id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}]");
continue;
}
var path = $"{group.Name}.{permission.Path}";
_permissions.Set(path, permission.Allow);
_logger.Debug($"Added group permission [id: {permission.Id}][group id: {permission.GroupId}][path: {permission.Path}]");
}
_logger.Information($"Groups [count: {groupInfo.Groups.Count()}] & Permissions [count: {groupInfo.GroupPermissions.Count()}] have been loaded.");
foreach (var chatter in groupInfo.GroupChatters)
if (groupsById.TryGetValue(chatter.GroupId, out var group))
_groups.Add(chatter.ChatterId, group.Name);
_logger.Information($"Users in each group [count: {groupInfo.GroupChatters.Count()}] have been loaded.");
}
}
}